use single quote for strings

This commit is contained in:
lqs 2020-06-30 12:56:51 +08:00
parent 4fa39899a0
commit 49f6472d51
4 changed files with 18 additions and 15 deletions

View File

@ -174,10 +174,11 @@ func (e expression) GetSQL(scope scope) (string, error) {
return e.builder(scope)
}
func escape(s string) string {
func quoteString(s string) string {
bytes := []byte(s)
n := 0
buf := make([]byte, len(s)*2)
buf := make([]byte, len(s)*2+2)
buf[0] = '\''
n := 1
for _, b := range bytes {
switch b {
@ -188,6 +189,8 @@ func escape(s string) string {
buf[n] = b
n++
}
buf[n] = '\''
n++
return string(buf[:n])
}
@ -200,7 +203,7 @@ func getSQL(scope scope, value interface{}) (sql string, priority int, err error
case int:
sql = strconv.Itoa(value.(int))
case string:
sql = "\"" + escape(value.(string)) + "\""
sql = quoteString(value.(string))
case Expression:
sql, err = value.(Expression).GetSQL(scope)
priority = value.(Expression).getOperatorPriority()
@ -256,7 +259,7 @@ func getSQLFromReflectValue(scope scope, v reflect.Value) (sql string, priority
case reflect.Float32, reflect.Float64:
sql = strconv.FormatFloat(v.Float(), 'g', -1, 64)
case reflect.String:
sql = "\"" + escape(v.String()) + "\""
sql = quoteString(v.String())
case reflect.Array, reflect.Slice:
length := v.Len()
values := make([]interface{}, length)

View File

@ -43,11 +43,11 @@ func TestExpression(t *testing.T) {
assertValue(t, float64(2), "2")
assertValue(t, float64(-2), "-2")
assertValue(t, "abc", "\"abc\"")
assertValue(t, "", "\"\"")
assertValue(t, "a' or 'a'='a", "\"a\\' or \\'a\\'=\\'a\"")
assertValue(t, "\n", "\"\\\n\"")
assertValue(t, CustomString("abc"), "\"abc\"")
assertValue(t, "abc", "'abc'")
assertValue(t, "", "''")
assertValue(t, "a' or 'a'='a", "'a\\' or \\'a\\'=\\'a'")
assertValue(t, "\n", "'\\\n'")
assertValue(t, CustomString("abc"), "'abc'")
x := 3
px := &x
@ -102,10 +102,10 @@ func TestFunc(t *testing.T) {
assertValue(t, e.NotIn([]int64{1}), "<> <> 1")
assertValue(t, e.NotIn([]int64{1, 2, 3}), "<> NOT IN (1, 2, 3)")
assertValue(t, e.Like("%A%"), "<> LIKE \"%A%\"")
assertValue(t, e.Contains("\n"), "LOCATE(\"\\\n\", <>) > 0")
assertValue(t, e.Like("%A%"), "<> LIKE '%A%'")
assertValue(t, e.Contains("\n"), "LOCATE('\\\n', <>) > 0")
assertValue(t, []interface{}{1, 2, 3, "d"}, "(1, 2, 3, \"d\")")
assertValue(t, []interface{}{1, 2, 3, "d"}, "(1, 2, 3, 'd')")
assertValue(t, e.IsNull(), "<> IS NULL")
assertValue(t, e.IsNotNull(), "<> IS NOT NULL")

View File

@ -5,5 +5,5 @@ import "testing"
func TestField(t *testing.T) {
assertValue(t, NewNumberField("t1", "f1").Equals(1), "`t1`.`f1` = 1")
assertValue(t, NewBooleanField("t1", "f1").Equals(true), "`t1`.`f1` = 1")
assertValue(t, NewStringField("t1", "f1").Equals("x"), "`t1`.`f1` = \"x\"")
assertValue(t, NewStringField("t1", "f1").Equals("x"), "`t1`.`f1` = 'x'")
}

View File

@ -64,7 +64,7 @@ func TestInsert(t *testing.T) {
if _, err := db.InsertInto(Test).Models(model, &model, []Model{model}).Execute(); err != nil {
t.Error(err)
}
assertLastSql(t, "INSERT INTO `test` (`f1`, `f2`) VALUES (1, \"test\"), (1, \"test\"), (1, \"test\")")
assertLastSql(t, "INSERT INTO `test` (`f1`, `f2`) VALUES (1, 'test'), (1, 'test'), (1, 'test')")
if _, err := db.InsertInto(Test).Models(model, &model, []interface{}{model, "invalid type"}).Execute(); err == nil {
t.Error("should get error here")