Compare commits

...

3 Commits

Author SHA1 Message Date
Tri Dao 384aa4a389
Merge 400d51c927 into 6bfccf8afa 2024-11-22 09:40:24 +08:00
Jinzhu 6bfccf8afa Refactor all tests script 2024-11-21 17:03:31 +08:00
Tri Dao 400d51c927 feat(expression): support case-when expression 2024-11-12 12:23:58 +07:00
7 changed files with 136 additions and 22 deletions

54
clause/expr_case.go Normal file
View File

@ -0,0 +1,54 @@
package clause
type ExprCaseCondition struct {
When string
Then string
Vars []any
}
type ExprCaseElse struct {
Then string
Vars []any
}
type ExprCase struct {
Cases []*ExprCaseCondition
Else *ExprCaseElse
}
func (expr ExprCase) Name() string {
return "CASE"
}
func (expr ExprCase) Build(builder Builder) {
var vars []any
for idx, condition := range expr.Cases {
if idx > 0 {
_ = builder.WriteByte(' ')
}
_, _ = builder.WriteString("WHEN ")
_, _ = builder.WriteString(condition.When)
_, _ = builder.WriteString(" THEN ")
_, _ = builder.WriteString(condition.Then)
if len(condition.Vars) > 0 {
vars = append(vars, condition.Vars...)
}
}
if expr.Else != nil {
elseExpr := expr.Else
_, _ = builder.WriteString(" ELSE ")
_, _ = builder.WriteString(elseExpr.Then)
if len(elseExpr.Vars) > 0 {
vars = append(vars, elseExpr.Vars...)
}
}
_, _ = builder.WriteString(" END")
clauseExpr := Expr{SQL: "", Vars: vars}
clauseExpr.Build(builder)
}
func (expr ExprCase) MergeClause(clause *Clause) {
clause.Expression = expr
}

60
clause/expr_case_test.go Normal file
View File

@ -0,0 +1,60 @@
package clause_test
import (
"testing"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
func Test_ExprCase(t *testing.T) {
type exampleUser struct {
ID string
Name string
}
inputUsers := []*exampleUser{
{
ID: "user-001",
Name: "user-name-001",
},
{
ID: "user-002",
Name: "user-name-002",
},
}
userIDs := make([]string, len(inputUsers))
userNameCases := make([]*clause.ExprCaseCondition, len(inputUsers))
for idx, user := range inputUsers {
userIDs[idx] = user.ID
userNameCases[idx] = &clause.ExprCaseCondition{
When: "user_id=?",
Then: "?",
Vars: []any{
user.ID,
user.Name,
},
}
}
sqlQuery := db.ToSQL(func(db *gorm.DB) *gorm.DB {
return db.
Table("users").
Where("user_id IN (?)", userIDs).
UpdateColumns(map[string]any{
"user_name": clause.ExprCase{
Cases: userNameCases,
Else: &clause.ExprCaseElse{
Then: "user_name",
Vars: nil,
},
},
})
})
expectedSQLQuery := "UPDATE `users` SET `user_name`=CASE WHEN user_id=\"user-001\" THEN \"user-name-001\" WHEN user_id=\"user-002\" THEN \"user-name-002\" ELSE user_name END WHERE user_id IN (\"user-001\",\"user-002\")"
if sqlQuery != expectedSQLQuery {
t.Errorf("SQLQuery is mismatch actual: %v expected:%v\n", sqlQuery, expectedSQLQuery)
}
}

2
go.mod
View File

@ -5,5 +5,5 @@ go 1.18
require ( require (
github.com/jinzhu/inflection v1.0.0 github.com/jinzhu/inflection v1.0.0
github.com/jinzhu/now v1.1.5 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/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 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= 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.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= 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 { if config.Dialector != nil {
err = config.Dialector.Initialize(db) err = config.Dialector.Initialize(db)
if err != nil { if err != nil {
if db, _ := db.DB(); db != nil { if db, _ := db.DB(); db != nil {
_ = db.Close() _ = db.Close()

View File

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

View File

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