Skip to content

Commit

Permalink
Support use clause.Expression as argument
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Jan 28, 2022
1 parent e5894ca commit cec0d32
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
17 changes: 17 additions & 0 deletions clause/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@ func TestSelect(t *testing.T) {
}, clause.From{}},
"SELECT `id`, `name`, LENGTH(`mobile`) FROM `users`", nil,
},
{
[]clause.Interface{clause.Select{
Expression: clause.CommaExpression{
Exprs: []clause.Expression{
clause.Expr{
SQL: "? as name",
Vars: []interface{}{clause.Eq{
Column: clause.Column{Name: "age"},
Value: 18,
},
},
},
},
},
}, clause.From{}},
"SELECT `age` = ? as name FROM `users`", []interface{}{18},
},
}

for idx, result := range results {
Expand Down
2 changes: 2 additions & 0 deletions statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ func (stmt *Statement) AddVar(writer clause.Writer, vars ...interface{}) {
v.Build(stmt)
case *clause.Expr:
v.Build(stmt)
case clause.Expression:
v.Build(stmt)
case driver.Valuer:
stmt.Vars = append(stmt.Vars, v)
stmt.DB.Dialector.BindVarTo(writer, stmt, v)
Expand Down
4 changes: 3 additions & 1 deletion tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ module gorm.io/gorm/tests
go 1.14

require (
github.com/denisenkom/go-mssqldb v0.12.0 // indirect
github.com/google/uuid v1.3.0
github.com/jackc/pgx/v4 v4.14.1 // indirect
github.com/jinzhu/now v1.1.4
github.com/lib/pq v1.10.4
golang.org/x/crypto v0.0.0-20211209193657-4570a0811e8b // indirect
github.com/mattn/go-sqlite3 v1.14.10 // indirect
golang.org/x/crypto v0.0.0-20220126234351-aa10faf2a1f8 // indirect
gorm.io/driver/mysql v1.2.3
gorm.io/driver/postgres v1.2.3
gorm.io/driver/sqlite v1.2.6
Expand Down

0 comments on commit cec0d32

Please sign in to comment.