Compare commits

...

7 Commits

Author SHA1 Message Date
Rankgice 5774374f55
Merge 8e9c1358b5 into 49bbaa637f 2024-11-14 17:41:58 +08:00
rankgice 8e9c1358b5 update test file raw_test.go 2024-09-30 20:05:52 +08:00
rankgice fa23822f68 Added test file raw_test.go 2024-09-30 19:51:44 +08:00
rankgice 202cbae492 Added test file raw_test.go 2024-09-30 19:30:02 +08:00
rankgice 96c8a2edba Merge remote-tracking branch 'upstream/master' 2024-09-30 19:23:32 +08:00
rankgice 599aad63a1 Added test file raw_test.go 2024-09-30 19:02:57 +08:00
rankgice c930baab81 Fixed bug where '@' was incorrectly identified as a named variable in `'`or`"` 2024-09-25 21:03:49 +08:00
2 changed files with 112 additions and 0 deletions

View File

@ -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 {
@ -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 {
inName = true
name = name[:0]

103
tests/raw_test.go Normal file
View File

@ -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])
}
}
}
})
}
}