Support use clause.Expression as argument

This commit is contained in:
Jinzhu 2022-01-28 18:48:32 +08:00
parent e5894ca449
commit cec0d32aec
3 changed files with 22 additions and 1 deletions

View File

@ -43,6 +43,23 @@ func TestSelect(t *testing.T) {
}, clause.From{}}, }, clause.From{}},
"SELECT `id`, `name`, LENGTH(`mobile`) FROM `users`", nil, "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 { for idx, result := range results {

View File

@ -183,6 +183,8 @@ func (stmt *Statement) AddVar(writer clause.Writer, vars ...interface{}) {
v.Build(stmt) v.Build(stmt)
case *clause.Expr: case *clause.Expr:
v.Build(stmt) v.Build(stmt)
case clause.Expression:
v.Build(stmt)
case driver.Valuer: case driver.Valuer:
stmt.Vars = append(stmt.Vars, v) stmt.Vars = append(stmt.Vars, v)
stmt.DB.Dialector.BindVarTo(writer, stmt, v) stmt.DB.Dialector.BindVarTo(writer, stmt, v)

View File

@ -3,11 +3,13 @@ module gorm.io/gorm/tests
go 1.14 go 1.14
require ( require (
github.com/denisenkom/go-mssqldb v0.12.0 // indirect
github.com/google/uuid v1.3.0 github.com/google/uuid v1.3.0
github.com/jackc/pgx/v4 v4.14.1 // indirect github.com/jackc/pgx/v4 v4.14.1 // indirect
github.com/jinzhu/now v1.1.4 github.com/jinzhu/now v1.1.4
github.com/lib/pq v1.10.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/mysql v1.2.3
gorm.io/driver/postgres v1.2.3 gorm.io/driver/postgres v1.2.3
gorm.io/driver/sqlite v1.2.6 gorm.io/driver/sqlite v1.2.6