Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

IN (?) expression not expanded when using pointer slice #682

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go-sqlcmd
Submodule go-sqlcmd added at 992aa2
20 changes: 10 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)
Expand Down
58 changes: 53 additions & 5 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,68 @@
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
// GORM_BRANCH: master
// 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())
}
})
}
}
Loading