From cec0d32aecc8d5068873304abe7f85e9409d4b10 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Fri, 28 Jan 2022 18:48:32 +0800 Subject: [PATCH] Support use clause.Expression as argument --- clause/select_test.go | 17 +++++++++++++++++ statement.go | 2 ++ tests/go.mod | 4 +++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/clause/select_test.go b/clause/select_test.go index 9fce0783..18bc2693 100644 --- a/clause/select_test.go +++ b/clause/select_test.go @@ -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 { diff --git a/statement.go b/statement.go index 146722a9..72359da2 100644 --- a/statement.go +++ b/statement.go @@ -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) diff --git a/tests/go.mod b/tests/go.mod index 3233ea95..5415cf74 100644 --- a/tests/go.mod +++ b/tests/go.mod @@ -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