Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed "*DB.Raw()" bug where single or double quotes containing '@' were incorrectly identified as named variables #7207

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

Rankgice
Copy link

@Rankgice Rankgice commented Sep 25, 2024

  • [*] Do only one thing
  • [*] Non breaking API changes
  • [*] Tested

What did this pull request do?

Fixed "*DB.Raw()" has a bug where single or double quotes containing '@' are incorrectly recognized as named variables.
When I call the Raw method, if the SQL contains a quote wrapped @, then this @ will not be treated as part of the string, but as a named parameter. I have increased the judgment of quotation marks to ensure that the content inside the quotation marks will not be recognized as distinctive symbols.

User Case Description

Values

values := map[string]interface{}{
	"email": `%qq.com`,
}

Origin

select * from users where email like @email and name = "@email"

Result

D:/go/src/runtime/proc.go:271 sql: expected 1 arguments, got 2
[21.008ms] [rows:-] select * from users where email like '%qq.com' and name = "'%qq.com'"

After repair

Result

D:/go/src/runtime/proc.go:271
[29.642ms] [rows:0] select * from users where email like '%qq.com' and name = "@email"

@Rankgice
Copy link
Author

The failure of this test seems to be due to an issue with the test image container. I noticed that other PRs have also encountered the same problem as me.

@jinzhu
Copy link
Member

jinzhu commented Sep 30, 2024

Hi, can you add some tests?

@Rankgice
Copy link
Author

Hi, can you add some tests?

Hello, of course not a problem. I will add some tests later

@Rankgice
Copy link
Author

Hi, can you add some tests?

I have added some test cases. May I proceed with the next review

} else if quotationMarks == '"' || quotationMarks == '\'' {
builder.WriteByte(v)
continue
}
if v == '@' && !inName {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to add a check here to ensure that the previous character is not a " or '

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic of this code is
The second if:
When we first encounter '' or '', quotationMarks will be marked as' 'or' 'accordingly`
The first if:
When we have already been marked and encountered that mark (end mark) again, we will clear the mark
The third if:
When we have been marked and have not encountered the end mark, we will output it as it is

I didn't understand where you wanted to add a judgment

Copy link
Contributor

@daheige daheige left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants