From c930baab81ff6d8f6eff96ed42467af27260dc59 Mon Sep 17 00:00:00 2001 From: rankgice <209192670@qq.com> Date: Wed, 25 Sep 2024 21:03:49 +0800 Subject: [PATCH 1/5] Fixed bug where '@' was incorrectly identified as a named variable in `'`or`"` --- clause/expression.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/clause/expression.go b/clause/expression.go index 3140846e..fc1da2b0 100644 --- a/clause/expression.go +++ b/clause/expression.go @@ -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] From 599aad63a1d6960813dbee05bd2eba19d4329623 Mon Sep 17 00:00:00 2001 From: rankgice <209192670@qq.com> Date: Mon, 30 Sep 2024 19:02:57 +0800 Subject: [PATCH 2/5] Added test file raw_test.go --- tests/raw_test.go | 110 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 tests/raw_test.go diff --git a/tests/raw_test.go b/tests/raw_test.go new file mode 100644 index 00000000..1e70f0ac --- /dev/null +++ b/tests/raw_test.go @@ -0,0 +1,110 @@ +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 + errFlag bool + }{ + { + "raw_test1", + `select * from users where name like @name and age = 18`, + map[string]interface{}{ + "name": "raw1", + }, + []User{ + users[0], + }, + false, + }, + { + "raw_test2", + `select * from users where name like @name and age = 18`, + map[string]interface{}{ + "name": "@name", + }, + []User{ + users[3], + }, + false, + }, + { + "raw_test3", + `select * from users where name like @name and age = 18`, + map[string]interface{}{ + "name": "@age", + }, + []User{ + users[4], + }, + false, + }, + { + "raw_test4", + `select * from users where name like "@name" and age = 18`, + map[string]interface{}{ + "name": "raw1", + }, + []User{ + users[3], + }, + false, + }, + { + "raw_test5", + `select * from users where name like "@name" and age = 18`, + map[string]interface{}{ + "name": "@raw", + }, + []User{ + users[3], + }, + false, + }, + { + "raw_test6", + `select * from users where name like "@name" and age = 18`, + map[string]interface{}{ + "name": "@age", + }, + []User{ + users[3], + }, + false, + }, + } + 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 && !test.errFlag { + 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]) + } + } + } + }) + } +} From 202cbae492cbe1dce03378454b7456dda2dd3171 Mon Sep 17 00:00:00 2001 From: rankgice <209192670@qq.com> Date: Mon, 30 Sep 2024 19:30:02 +0800 Subject: [PATCH 3/5] Added test file raw_test.go --- tests/raw_test.go | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/tests/raw_test.go b/tests/raw_test.go index 1e70f0ac..fa0dd090 100644 --- a/tests/raw_test.go +++ b/tests/raw_test.go @@ -22,7 +22,6 @@ func TestRawSelect(t *testing.T) { Sql string args map[string]interface{} Expect []User - errFlag bool }{ { "raw_test1", @@ -33,7 +32,6 @@ func TestRawSelect(t *testing.T) { []User{ users[0], }, - false, }, { "raw_test2", @@ -44,7 +42,6 @@ func TestRawSelect(t *testing.T) { []User{ users[3], }, - false, }, { "raw_test3", @@ -55,7 +52,6 @@ func TestRawSelect(t *testing.T) { []User{ users[4], }, - false, }, { "raw_test4", @@ -66,7 +62,6 @@ func TestRawSelect(t *testing.T) { []User{ users[3], }, - false, }, { "raw_test5", @@ -77,7 +72,6 @@ func TestRawSelect(t *testing.T) { []User{ users[3], }, - false, }, { "raw_test6", @@ -88,13 +82,12 @@ func TestRawSelect(t *testing.T) { []User{ users[3], }, - false, }, } 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 && !test.errFlag { + 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) { From fa23822f686760b0352543592ebea2731a1b3ed4 Mon Sep 17 00:00:00 2001 From: rankgice <209192670@qq.com> Date: Mon, 30 Sep 2024 19:51:44 +0800 Subject: [PATCH 4/5] Added test file raw_test.go --- tests/raw_test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/raw_test.go b/tests/raw_test.go index fa0dd090..f18a05c8 100644 --- a/tests/raw_test.go +++ b/tests/raw_test.go @@ -55,19 +55,19 @@ func TestRawSelect(t *testing.T) { }, { "raw_test4", - `select * from users where name like "@name" and age = 18`, + `select * from users where name like "raw3" and age = @age`, map[string]interface{}{ - "name": "raw1", + "age": 18, }, []User{ - users[3], + users[2], }, }, { "raw_test5", - `select * from users where name like "@name" and age = 18`, + `select * from users where name like "@name" and age = @age`, map[string]interface{}{ - "name": "@raw", + "age": 18, }, []User{ users[3], @@ -75,12 +75,12 @@ func TestRawSelect(t *testing.T) { }, { "raw_test6", - `select * from users where name like "@name" and age = 18`, + `select * from users where name like "@age" and age = @age`, map[string]interface{}{ - "name": "@age", + "age": 18, }, []User{ - users[3], + users[4], }, }, } From 8e9c1358b5d22c7cac67ac2157f598b2ccb79a6c Mon Sep 17 00:00:00 2001 From: rankgice <209192670@qq.com> Date: Mon, 30 Sep 2024 20:05:52 +0800 Subject: [PATCH 5/5] update test file raw_test.go --- tests/raw_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/raw_test.go b/tests/raw_test.go index f18a05c8..abc9b7ef 100644 --- a/tests/raw_test.go +++ b/tests/raw_test.go @@ -55,7 +55,7 @@ func TestRawSelect(t *testing.T) { }, { "raw_test4", - `select * from users where name like "raw3" and age = @age`, + `select * from users where name like 'raw3' and age = @age`, map[string]interface{}{ "age": 18, }, @@ -65,7 +65,7 @@ func TestRawSelect(t *testing.T) { }, { "raw_test5", - `select * from users where name like "@name" and age = @age`, + `select * from users where name like '@name' and age = @age`, map[string]interface{}{ "age": 18, }, @@ -75,7 +75,7 @@ func TestRawSelect(t *testing.T) { }, { "raw_test6", - `select * from users where name like "@age" and age = @age`, + `select * from users where name like '@age' and age = @age`, map[string]interface{}{ "age": 18, },