forked from mirror/gorm
Fix named argument with multiple line SQL, fix #3336
This commit is contained in:
parent
9b0ad4730f
commit
496db1f13e
|
@ -94,7 +94,7 @@ func (expr NamedExpr) Build(builder Builder) {
|
||||||
if v == '@' && !inName {
|
if v == '@' && !inName {
|
||||||
inName = true
|
inName = true
|
||||||
name = []byte{}
|
name = []byte{}
|
||||||
} else if v == ' ' || v == ',' || v == ')' || v == '"' || v == '\'' || v == '`' {
|
} else if v == ' ' || v == ',' || v == ')' || v == '"' || v == '\'' || v == '`' || v == '\n' {
|
||||||
if inName {
|
if inName {
|
||||||
if nv, ok := namedMap[string(name)]; ok {
|
if nv, ok := namedMap[string(name)]; ok {
|
||||||
builder.AddVar(builder, nv)
|
builder.AddVar(builder, nv)
|
||||||
|
|
|
@ -116,7 +116,7 @@ func (tx *PreparedStmtTX) ExecContext(ctx context.Context, query string, args ..
|
||||||
func (tx *PreparedStmtTX) QueryContext(ctx context.Context, query string, args ...interface{}) (rows *sql.Rows, err error) {
|
func (tx *PreparedStmtTX) QueryContext(ctx context.Context, query string, args ...interface{}) (rows *sql.Rows, err error) {
|
||||||
stmt, err := tx.PreparedStmtDB.prepare(ctx, query)
|
stmt, err := tx.PreparedStmtDB.prepare(ctx, query)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
rows, err = tx.Tx.StmtContext(ctx, stmt).QueryContext(ctx, args...)
|
rows, err = tx.Tx.Stmt(stmt).QueryContext(ctx, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.PreparedStmtDB.Mux.Lock()
|
tx.PreparedStmtDB.Mux.Lock()
|
||||||
stmt.Close()
|
stmt.Close()
|
||||||
|
|
|
@ -14,3 +14,5 @@ require (
|
||||||
)
|
)
|
||||||
|
|
||||||
replace gorm.io/gorm => ../
|
replace gorm.io/gorm => ../
|
||||||
|
|
||||||
|
replace github.com/jackc/pgx/v4 => github.com/jinzhu/pgx/v4 v4.8.2
|
||||||
|
|
|
@ -48,10 +48,22 @@ func TestNamedArg(t *testing.T) {
|
||||||
t.Errorf("failed to update with named arg")
|
t.Errorf("failed to update with named arg")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namedUser.Name1 = "jinzhu-new"
|
||||||
|
namedUser.Name2 = "jinzhu-new2"
|
||||||
|
namedUser.Name3 = "jinzhu-new"
|
||||||
|
|
||||||
var result5 NamedUser
|
var result5 NamedUser
|
||||||
if err := DB.Raw("SELECT * FROM named_users WHERE (name1 = @name AND name3 = @name) AND name2 = @name2", map[string]interface{}{"name": "jinzhu-new", "name2": "jinzhu-new2"}).Find(&result5).Error; err != nil {
|
if err := DB.Raw("SELECT * FROM named_users WHERE (name1 = @name AND name3 = @name) AND name2 = @name2", map[string]interface{}{"name": "jinzhu-new", "name2": "jinzhu-new2"}).Find(&result5).Error; err != nil {
|
||||||
t.Errorf("failed to update with named arg")
|
t.Errorf("failed to update with named arg")
|
||||||
}
|
}
|
||||||
|
|
||||||
AssertEqual(t, result4, namedUser)
|
AssertEqual(t, result5, namedUser)
|
||||||
|
|
||||||
|
var result6 NamedUser
|
||||||
|
if err := DB.Raw(`SELECT * FROM named_users WHERE (name1 = @name
|
||||||
|
AND name3 = @name) AND name2 = @name2`, map[string]interface{}{"name": "jinzhu-new", "name2": "jinzhu-new2"}).Find(&result6).Error; err != nil {
|
||||||
|
t.Errorf("failed to update with named arg")
|
||||||
|
}
|
||||||
|
|
||||||
|
AssertEqual(t, result6, namedUser)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue