| 1 |
package logger_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"database/sql/driver" |
| 5 |
"encoding/json" |
| 6 |
"fmt" |
| 7 |
"regexp" |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/jinzhu/now" |
| 12 |
"gorm.io/gorm/logger" |
| 13 |
) |
| 14 |
|
| 15 |
type JSON json.RawMessage |
| 16 |
|
| 17 |
func (j JSON) Value() (driver.Value, error) { |
| 18 |
if len(j) == 0 { |
| 19 |
return nil, nil |
| 20 |
} |
| 21 |
return json.RawMessage(j).MarshalJSON() |
| 22 |
} |
| 23 |
|
| 24 |
type ExampleStruct struct { |
| 25 |
Name string |
| 26 |
Val string |
| 27 |
} |
| 28 |
|
| 29 |
func (s ExampleStruct) Value() (driver.Value, error) { |
| 30 |
return json.Marshal(s) |
| 31 |
} |
| 32 |
|
| 33 |
func format(v []byte, escaper string) string { |
| 34 |
return escaper + strings.ReplaceAll(string(v), escaper, escaper+escaper) + escaper |
| 35 |
} |
| 36 |
|
| 37 |
func TestExplainSQL(t *testing.T) { |
| 38 |
type role string |
| 39 |
type password []byte |
| 40 |
type intType int |
| 41 |
type floatType float64 |
| 42 |
var ( |
| 43 |
tt = now.MustParse("2020-02-23 11:10:10") |
| 44 |
myrole = role("admin") |
| 45 |
pwd = password("pass") |
| 46 |
jsVal = []byte(`{"Name":"test","Val":"test"}`) |
| 47 |
js = JSON(jsVal) |
| 48 |
esVal = []byte(`{"Name":"test","Val":"test"}`) |
| 49 |
es = ExampleStruct{Name: "test", Val: "test"} |
| 50 |
intVal intType = 1 |
| 51 |
floatVal floatType = 1.23 |
| 52 |
) |
| 53 |
|
| 54 |
results := []struct { |
| 55 |
SQL string |
| 56 |
NumericRegexp *regexp.Regexp |
| 57 |
Vars []interface{} |
| 58 |
Result string |
| 59 |
}{ |
| 60 |
{ |
| 61 |
SQL: "create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", |
| 62 |
NumericRegexp: nil, |
| 63 |
Vars: []interface{}{"jinzhu", 1, 999.99, true, []byte("12345"), tt, &tt, nil, "w@g.\"com", myrole, pwd}, |
| 64 |
Result: `create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass) values ("jinzhu", 1, 999.99, true, "12345", "2020-02-23 11:10:10", "2020-02-23 11:10:10", NULL, "w@g.""com", "admin", "pass")`, |
| 65 |
}, |
| 66 |
{ |
| 67 |
SQL: "create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", |
| 68 |
NumericRegexp: nil, |
| 69 |
Vars: []interface{}{"jinzhu?", 1, 999.99, true, []byte("12345"), tt, &tt, nil, "w@g.\"com", myrole, pwd}, |
| 70 |
Result: `create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass) values ("jinzhu?", 1, 999.99, true, "12345", "2020-02-23 11:10:10", "2020-02-23 11:10:10", NULL, "w@g.""com", "admin", "pass")`, |
| 71 |
}, |
| 72 |
{ |
| 73 |
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)", |
| 74 |
NumericRegexp: regexp.MustCompile(`@p(\d+)`), |
| 75 |
Vars: []interface{}{"jinzhu", 1, 999.99, true, []byte("12345"), tt, &tt, nil, "[email protected]", myrole, pwd}, |
| 76 |
Result: `create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass) values ("jinzhu", 1, 999.99, true, "12345", "2020-02-23 11:10:10", "2020-02-23 11:10:10", NULL, "[email protected]", "admin", "pass")`, |
| 77 |
}, |
| 78 |
{ |
| 79 |
SQL: "create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass) values ($3, $4, $1, $2, $7, $8, $5, $6, $9, $10, $11)", |
| 80 |
NumericRegexp: regexp.MustCompile(`\$(\d+)`), |
| 81 |
Vars: []interface{}{999.99, true, "jinzhu", 1, &tt, nil, []byte("12345"), tt, "[email protected]", myrole, pwd}, |
| 82 |
Result: `create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass) values ("jinzhu", 1, 999.99, true, "12345", "2020-02-23 11:10:10", "2020-02-23 11:10:10", NULL, "[email protected]", "admin", "pass")`, |
| 83 |
}, |
| 84 |
{ |
| 85 |
SQL: "create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass) values (@p1, @p11, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10)", |
| 86 |
NumericRegexp: regexp.MustCompile(`@p(\d+)`), |
| 87 |
Vars: []interface{}{"jinzhu", 999.99, true, []byte("12345"), tt, &tt, nil, "[email protected]", myrole, pwd, 1}, |
| 88 |
Result: `create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass) values ("jinzhu", 1, 999.99, true, "12345", "2020-02-23 11:10:10", "2020-02-23 11:10:10", NULL, "[email protected]", "admin", "pass")`, |
| 89 |
}, |
| 90 |
{ |
| 91 |
SQL: "create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass, json_struct, example_struct) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", |
| 92 |
NumericRegexp: nil, |
| 93 |
Vars: []interface{}{"jinzhu", 1, 999.99, true, []byte("12345"), tt, &tt, nil, "w@g.\"com", myrole, pwd, js, es}, |
| 94 |
Result: fmt.Sprintf(`create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass, json_struct, example_struct) values ("jinzhu", 1, 999.99, true, "12345", "2020-02-23 11:10:10", "2020-02-23 11:10:10", NULL, "w@g.""com", "admin", "pass", %v, %v)`, format(jsVal, `"`), format(esVal, `"`)), |
| 95 |
}, |
| 96 |
{ |
| 97 |
SQL: "create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass, json_struct, example_struct) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", |
| 98 |
NumericRegexp: nil, |
| 99 |
Vars: []interface{}{"jinzhu", 1, 999.99, true, []byte("12345"), tt, &tt, nil, "w@g.\"com", myrole, pwd, &js, &es}, |
| 100 |
Result: fmt.Sprintf(`create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass, json_struct, example_struct) values ("jinzhu", 1, 999.99, true, "12345", "2020-02-23 11:10:10", "2020-02-23 11:10:10", NULL, "w@g.""com", "admin", "pass", %v, %v)`, format(jsVal, `"`), format(esVal, `"`)), |
| 101 |
}, |
| 102 |
{ |
| 103 |
SQL: "create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass, json_struct, example_struct) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", |
| 104 |
NumericRegexp: nil, |
| 105 |
Vars: []interface{}{"jinzhu", 1, 0.1753607109, true, []byte("12345"), tt, &tt, nil, "w@g.\"com", myrole, pwd, &js, &es}, |
| 106 |
Result: fmt.Sprintf(`create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass, json_struct, example_struct) values ("jinzhu", 1, 0.1753607109, true, "12345", "2020-02-23 11:10:10", "2020-02-23 11:10:10", NULL, "w@g.""com", "admin", "pass", %v, %v)`, format(jsVal, `"`), format(esVal, `"`)), |
| 107 |
}, |
| 108 |
{ |
| 109 |
SQL: "create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass, json_struct, example_struct) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", |
| 110 |
NumericRegexp: nil, |
| 111 |
Vars: []interface{}{"jinzhu", 1, float32(999.99), true, []byte("12345"), tt, &tt, nil, "w@g.\"com", myrole, pwd, &js, &es}, |
| 112 |
Result: fmt.Sprintf(`create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass, json_struct, example_struct) values ("jinzhu", 1, 999.99, true, "12345", "2020-02-23 11:10:10", "2020-02-23 11:10:10", NULL, "w@g.""com", "admin", "pass", %v, %v)`, format(jsVal, `"`), format(esVal, `"`)), |
| 113 |
}, |
| 114 |
{ |
| 115 |
SQL: "create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass, int_val) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", |
| 116 |
NumericRegexp: nil, |
| 117 |
Vars: []interface{}{"jinzhu?", 1, 999.99, true, []byte("12345"), tt, &tt, nil, "w@g.\"com", myrole, pwd, intVal}, |
| 118 |
Result: `create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass, int_val) values ("jinzhu?", 1, 999.99, true, "12345", "2020-02-23 11:10:10", "2020-02-23 11:10:10", NULL, "w@g.""com", "admin", "pass", 1)`, |
| 119 |
}, |
| 120 |
{ |
| 121 |
SQL: "create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass, float_val) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", |
| 122 |
NumericRegexp: nil, |
| 123 |
Vars: []interface{}{"jinzhu?", 1, 999.99, true, []byte("12345"), tt, &tt, nil, "w@g.\"com", myrole, pwd, floatVal}, |
| 124 |
Result: `create table users (name, age, height, actived, bytes, create_at, update_at, deleted_at, email, role, pass, float_val) values ("jinzhu?", 1, 999.99, true, "12345", "2020-02-23 11:10:10", "2020-02-23 11:10:10", NULL, "w@g.""com", "admin", "pass", 1.230000)`, |
| 125 |
}, |
| 126 |
} |
| 127 |
|
| 128 |
for idx, r := range results { |
| 129 |
if result := logger.ExplainSQL(r.SQL, r.NumericRegexp, `"`, r.Vars...); result != r.Result { |
| 130 |
t.Errorf("Explain SQL #%v expects %v, but got %v", idx, r.Result, result) |
| 131 |
} |
| 132 |
} |
| 133 |
} |