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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions clause/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (expr NamedExpr) Build(builder Builder) {
inName bool
afterParenthesis bool
namedMap = make(map[string]interface{}, len(expr.Vars))
quotationMarks byte
)

for _, v := range expr.Vars {
Expand Down Expand Up @@ -124,6 +125,14 @@ func (expr NamedExpr) Build(builder Builder) {
name := make([]byte, 0, 10)

for _, v := range []byte(expr.SQL) {
if quotationMarks == v && (v == '"' || v == '\'') {
quotationMarks = 0
} else if quotationMarks == 0 && (v == '"' || v == '\'') {
quotationMarks = v
} 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

inName = true
name = name[:0]
Expand Down
103 changes: 103 additions & 0 deletions tests/raw_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package tests_test

import (
. "gorm.io/gorm/utils/tests"
"testing"
)

func TestRawSelect(t *testing.T) {
users := []User{
*GetUser("raw1", Config{}),
*GetUser("raw2", Config{}),
*GetUser("raw3", Config{}),
*GetUser("@name", Config{}),
*GetUser("@age", Config{}),
}

if err := DB.Create(&users).Error; err != nil {
t.Fatalf("errors happened when create users: %v", err)
}
tests := []struct {
TestName string
Sql string
args map[string]interface{}
Expect []User
}{
{
"raw_test1",
`select * from users where name like @name and age = 18`,
map[string]interface{}{
"name": "raw1",
},
[]User{
users[0],
},
},
{
"raw_test2",
`select * from users where name like @name and age = 18`,
map[string]interface{}{
"name": "@name",
},
[]User{
users[3],
},
},
{
"raw_test3",
`select * from users where name like @name and age = 18`,
map[string]interface{}{
"name": "@age",
},
[]User{
users[4],
},
},
{
"raw_test4",
`select * from users where name like 'raw3' and age = @age`,
map[string]interface{}{
"age": 18,
},
[]User{
users[2],
},
},
{
"raw_test5",
`select * from users where name like '@name' and age = @age`,
map[string]interface{}{
"age": 18,
},
[]User{
users[3],
},
},
{
"raw_test6",
`select * from users where name like '@age' and age = @age`,
map[string]interface{}{
"age": 18,
},
[]User{
users[4],
},
},
}
for _, test := range tests {
t.Run(test.TestName, func(t *testing.T) {
var results []User
if err := DB.Raw(test.Sql, test.args).Scan(&results).Error; err != nil {
t.Errorf("errors %s: %v", test.TestName, err)
} else {
if len(results) != len(test.Expect) {
t.Errorf("errors %s: %v", test.TestName, err)
} else {
for i := 0; i < len(results); i++ {
CheckUser(t, results[i], test.Expect[i])
}
}
}
})
}
}
Loading