mirror of https://github.com/go-gorm/gorm.git
Fix format SQL log, close #3465
This commit is contained in:
parent
ed1b134e1c
commit
0ec10d4907
|
@ -96,9 +96,21 @@ func ExplainSQL(sql string, numericPlaceholder *regexp.Regexp, escaper string, a
|
||||||
}
|
}
|
||||||
|
|
||||||
if numericPlaceholder == nil {
|
if numericPlaceholder == nil {
|
||||||
for _, v := range vars {
|
var idx int
|
||||||
sql = strings.Replace(sql, "?", v, 1)
|
var newSQL strings.Builder
|
||||||
|
|
||||||
|
for _, v := range []byte(sql) {
|
||||||
|
if v == '?' {
|
||||||
|
if len(vars) > idx {
|
||||||
|
newSQL.WriteString(vars[idx])
|
||||||
|
idx++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
newSQL.WriteByte(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sql = newSQL.String()
|
||||||
} else {
|
} else {
|
||||||
sql = numericPlaceholder.ReplaceAllString(sql, "$$$1$$")
|
sql = numericPlaceholder.ReplaceAllString(sql, "$$$1$$")
|
||||||
for idx, v := range vars {
|
for idx, v := range vars {
|
||||||
|
|
|
@ -29,6 +29,12 @@ func TestExplainSQL(t *testing.T) {
|
||||||
Vars: []interface{}{"jinzhu", 1, 999.99, true, []byte("12345"), tt, &tt, nil, "w@g.\"com", myrole, pwd},
|
Vars: []interface{}{"jinzhu", 1, 999.99, true, []byte("12345"), tt, &tt, nil, "w@g.\"com", myrole, pwd},
|
||||||
Result: `create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass) values ("jinzhu", 1, 999.990000, true, "12345", "2020-02-23 11:10:10", "2020-02-23 11:10:10", NULL, "w@g.\"com", "admin", "pass")`,
|
Result: `create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass) values ("jinzhu", 1, 999.990000, true, "12345", "2020-02-23 11:10:10", "2020-02-23 11:10:10", NULL, "w@g.\"com", "admin", "pass")`,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
SQL: "create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
||||||
|
NumericRegexp: nil,
|
||||||
|
Vars: []interface{}{"jinzhu?", 1, 999.99, true, []byte("12345"), tt, &tt, nil, "w@g.\"com", myrole, pwd},
|
||||||
|
Result: `create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass) values ("jinzhu?", 1, 999.990000, true, "12345", "2020-02-23 11:10:10", "2020-02-23 11:10:10", NULL, "w@g.\"com", "admin", "pass")`,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
SQL: "create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass) values (@p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11)",
|
SQL: "create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass) values (@p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11)",
|
||||||
NumericRegexp: regexp.MustCompile(`@p(\d+)`),
|
NumericRegexp: regexp.MustCompile(`@p(\d+)`),
|
||||||
|
|
Loading…
Reference in New Issue