From 80f207ff64129e814c490b39f1b7f9a8a3bcdc31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gonc=CC=A7alo=20Amaral?= Date: Sun, 14 Jan 2024 19:58:56 +0000 Subject: [PATCH] Issue example --- go-sqlcmd | 1 + go.mod | 20 +++++++++--------- main_test.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 64 insertions(+), 15 deletions(-) create mode 160000 go-sqlcmd diff --git a/go-sqlcmd b/go-sqlcmd new file mode 160000 index 00000000..992aa2c4 --- /dev/null +++ b/go-sqlcmd @@ -0,0 +1 @@ +Subproject commit 992aa2c4c24e532e55595e2e0e57e153af413593 diff --git a/go.mod b/go.mod index f194634f..eafe3604 100644 --- a/go.mod +++ b/go.mod @@ -6,8 +6,8 @@ toolchain go1.23.3 require ( gorm.io/driver/mysql v1.5.7 - gorm.io/driver/postgres v1.5.10 - gorm.io/driver/sqlite v1.5.6 + gorm.io/driver/postgres v1.5.11 + gorm.io/driver/sqlite v1.5.7 gorm.io/driver/sqlserver v1.5.4 gorm.io/gen v0.3.26 gorm.io/gorm v1.25.12 @@ -21,19 +21,19 @@ require ( github.com/google/uuid v1.6.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect - github.com/jackc/pgx/v5 v5.7.1 // indirect + github.com/jackc/pgx/v5 v5.7.2 // indirect github.com/jackc/puddle/v2 v2.2.2 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect github.com/mattn/go-sqlite3 v1.14.24 // indirect - github.com/microsoft/go-mssqldb v1.7.2 // indirect - golang.org/x/crypto v0.29.0 // indirect + github.com/microsoft/go-mssqldb v1.8.0 // indirect + golang.org/x/crypto v0.32.0 // indirect golang.org/x/mod v0.22.0 // indirect - golang.org/x/sync v0.9.0 // indirect - golang.org/x/sys v0.27.0 // indirect - golang.org/x/text v0.20.0 // indirect - golang.org/x/tools v0.27.0 // indirect - gorm.io/datatypes v1.2.4 // indirect + golang.org/x/sync v0.10.0 // indirect + golang.org/x/sys v0.29.0 // indirect + golang.org/x/text v0.21.0 // indirect + golang.org/x/tools v0.29.0 // indirect + gorm.io/datatypes v1.2.5 // indirect gorm.io/hints v1.1.2 // indirect gorm.io/plugin/dbresolver v1.5.3 // indirect ) diff --git a/main_test.go b/main_test.go index 60a388f7..01b3e974 100644 --- a/main_test.go +++ b/main_test.go @@ -1,7 +1,14 @@ package main import ( + "fmt" + "sync" "testing" + + "gorm.io/gorm" + "gorm.io/gorm/clause" + "gorm.io/gorm/schema" + "gorm.io/gorm/utils/tests" ) // GORM_REPO: https://github.com/go-gorm/gorm.git @@ -9,12 +16,53 @@ import ( // TEST_DRIVERS: sqlite, mysql, postgres, sqlserver func TestGORM(t *testing.T) { - user := User{Name: "jinzhu"} - - DB.Create(&user) + var users []User + var id uint = 1 + // Works with a value + if err := DB.Table("users").Where("id = ?", id).Find(&users).Error; err != nil { + t.Errorf("Failed, got error: %v", err) + } + // Works with a pointer + if err := DB.Table("users").Where("id = ?", &id).Find(&users).Error; err != nil { + t.Errorf("Failed, got error: %v", err) + } - var result User - if err := DB.First(&result, user.ID).Error; err != nil { + ids := []uint{1, 2, 3} + // Works with a slice + if err := DB.Table("users").Where("id IN (?)", ids).Find(&users).Error; err != nil { t.Errorf("Failed, got error: %v", err) } + // Does not work with a slice pointer + if err := DB.Table("users").Where("id IN (?)", &ids).Find(&users).Error; err != nil { + t.Errorf("Failed, got error: %v", err) + } +} + +// From gorm/clause/expression_test.go +func TestExpr(t *testing.T) { + ids := []uint{1, 2, 3} + results := []struct { + SQL string + Result string + Vars []interface{} + }{{ + SQL: "SELECT * FROM users WHERE users.id IN (?)", + Vars: []interface{}{ids}, + Result: "SELECT * FROM users WHERE users.id IN (?,?,?)", // Correctly expanded + }, { + SQL: "SELECT * FROM users WHERE users.id IN (?)", + Vars: []interface{}{&ids}, + Result: "SELECT * FROM users WHERE users.id IN (?,?,?)", // Expected this to expand aswell + }} + + for idx, result := range results { + t.Run(fmt.Sprintf("case #%v", idx), func(t *testing.T) { + user, _ := schema.Parse(&tests.User{}, &sync.Map{}, DB.NamingStrategy) + stmt := &gorm.Statement{DB: DB, Table: user.Table, Schema: user, Clauses: map[string]clause.Clause{}} + clause.Expr{SQL: result.SQL, Vars: result.Vars}.Build(stmt) + if stmt.SQL.String() != result.Result { + t.Errorf("generated SQL is not equal, expects %v, but got %v", result.Result, stmt.SQL.String()) + } + }) + } }