Compare commits

...

8 Commits

Author SHA1 Message Date
Rankgice 486dad9cea
Merge 8e9c1358b5 into 6bfccf8afa 2024-11-22 09:40:24 +08:00
Jinzhu 6bfccf8afa Refactor all tests script 2024-11-21 17:03:31 +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
7 changed files with 134 additions and 22 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]

2
go.mod
View File

@ -5,5 +5,5 @@ go 1.18
require (
github.com/jinzhu/inflection v1.0.0
github.com/jinzhu/now v1.1.5
golang.org/x/text v0.14.0
golang.org/x/text v0.20.0
)

4
go.sum
View File

@ -2,5 +2,5 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=

View File

@ -183,7 +183,6 @@ func Open(dialector Dialector, opts ...Option) (db *DB, err error) {
if config.Dialector != nil {
err = config.Dialector.Initialize(db)
if err != nil {
if db, _ := db.DB(); db != nil {
_ = db.Close()

View File

@ -8,7 +8,7 @@ require (
github.com/lib/pq v1.10.9
github.com/stretchr/testify v1.9.0
gorm.io/driver/mysql v1.5.7
gorm.io/driver/postgres v1.5.9
gorm.io/driver/postgres v1.5.10
gorm.io/driver/sqlite v1.5.6
gorm.io/driver/sqlserver v1.5.4
gorm.io/gorm v1.25.12
@ -25,12 +25,12 @@ require (
github.com/jackc/pgx/v5 v5.7.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-sqlite3 v1.14.23 // indirect
github.com/mattn/go-sqlite3 v1.14.24 // indirect
github.com/microsoft/go-mssqldb v1.7.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/crypto v0.29.0 // indirect
golang.org/x/text v0.20.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

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

View File

@ -16,22 +16,23 @@ then
fi
# SqlServer for Mac M1
if [[ -z $GITHUB_ACTION ]]; then
if [ -d tests ]
then
if [[ -z $GITHUB_ACTION && -d tests ]]; then
cd tests
if [[ $(uname -a) == *" arm64" ]]; then
MSSQL_IMAGE=mcr.microsoft.com/azure-sql-edge docker compose up -d || true
MSSQL_IMAGE=mcr.microsoft.com/azure-sql-edge docker compose up -d --wait
go install github.com/microsoft/go-sqlcmd/cmd/sqlcmd@latest || true
SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930 -Q "IF DB_ID('gorm') IS NULL CREATE DATABASE gorm" > /dev/null || true
SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930 -Q "IF SUSER_ID (N'gorm') IS NULL CREATE LOGIN gorm WITH PASSWORD = 'LoremIpsum86';" > /dev/null || true
SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930 -Q "IF USER_ID (N'gorm') IS NULL CREATE USER gorm FROM LOGIN gorm; ALTER SERVER ROLE sysadmin ADD MEMBER [gorm];" > /dev/null || true
for query in \
"IF DB_ID('gorm') IS NULL CREATE DATABASE gorm" \
"IF SUSER_ID (N'gorm') IS NULL CREATE LOGIN gorm WITH PASSWORD = 'LoremIpsum86';" \
"IF USER_ID (N'gorm') IS NULL CREATE USER gorm FROM LOGIN gorm; ALTER SERVER ROLE sysadmin ADD MEMBER [gorm];"
do
SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930 -Q "$query" > /dev/null || true
done
else
MSSQL_IMAGE=mcr.microsoft.com/mssql/server docker compose up -d
MSSQL_IMAGE=mcr.microsoft.com/mssql/server docker compose up -d --wait
fi
cd ..
fi
fi
for dialect in "${dialects[@]}" ; do