mirror of
https://github.com/hanzoai/sqlbuilder.git
synced 2026-10-01 00:37:42 +00:00
Merge pull request #228 from shoriwe/fix/sqlite-strings
fix #227: sqlite: string interpolation incorrectly uses backslash escapes
This commit is contained in:
+1
-1
@@ -105,7 +105,7 @@ func ExampleFlavor_Interpolate_sqlite() {
|
||||
fmt.Println(err)
|
||||
|
||||
// Output:
|
||||
// SELECT name FROM user WHERE id <> 1234 AND name = 'Charmy Liu' AND desc LIKE '%mother\'s day%'
|
||||
// SELECT name FROM user WHERE id <> 1234 AND name = 'Charmy Liu' AND desc LIKE '%mother''s day%'
|
||||
// <nil>
|
||||
}
|
||||
|
||||
|
||||
+52
-20
@@ -785,36 +785,68 @@ func quoteStringValue(buf []byte, s string, flavor Flavor) []byte {
|
||||
for ; sz != 0; r, sz = utf8.DecodeRuneInString(s) {
|
||||
switch r {
|
||||
case '\x00':
|
||||
buf = append(buf, "\\0"...)
|
||||
|
||||
switch flavor {
|
||||
case SQLite:
|
||||
buf = append(buf, `'||char(0)||'`...)
|
||||
default:
|
||||
buf = append(buf, `\0`...)
|
||||
}
|
||||
case '\b':
|
||||
buf = append(buf, "\\b"...)
|
||||
|
||||
switch flavor {
|
||||
case SQLite:
|
||||
buf = append(buf, '\b')
|
||||
default:
|
||||
buf = append(buf, `\b`...)
|
||||
}
|
||||
case '\n':
|
||||
buf = append(buf, "\\n"...)
|
||||
|
||||
switch flavor {
|
||||
case SQLite:
|
||||
buf = append(buf, '\n')
|
||||
default:
|
||||
buf = append(buf, `\n`...)
|
||||
}
|
||||
case '\r':
|
||||
buf = append(buf, "\\r"...)
|
||||
|
||||
switch flavor {
|
||||
case SQLite:
|
||||
buf = append(buf, '\r')
|
||||
default:
|
||||
buf = append(buf, `\r`...)
|
||||
}
|
||||
case '\t':
|
||||
buf = append(buf, "\\t"...)
|
||||
|
||||
switch flavor {
|
||||
case SQLite:
|
||||
buf = append(buf, '\t')
|
||||
default:
|
||||
buf = append(buf, `\t`...)
|
||||
}
|
||||
case '\x1a':
|
||||
buf = append(buf, "\\Z"...)
|
||||
|
||||
switch flavor {
|
||||
case SQLite:
|
||||
buf = append(buf, '\x1a')
|
||||
default:
|
||||
buf = append(buf, `\Z`...)
|
||||
}
|
||||
case '\'':
|
||||
if flavor == CQL {
|
||||
switch flavor {
|
||||
case SQLite, CQL:
|
||||
buf = append(buf, "''"...)
|
||||
} else {
|
||||
buf = append(buf, "\\'"...)
|
||||
default:
|
||||
buf = append(buf, `\'`...)
|
||||
}
|
||||
|
||||
case '"':
|
||||
buf = append(buf, "\\\""...)
|
||||
|
||||
switch flavor {
|
||||
case SQLite:
|
||||
buf = append(buf, '"')
|
||||
default:
|
||||
buf = append(buf, `\"`...)
|
||||
}
|
||||
case '\\':
|
||||
buf = append(buf, "\\\\"...)
|
||||
|
||||
switch flavor {
|
||||
case SQLite:
|
||||
buf = append(buf, '\\')
|
||||
default:
|
||||
buf = append(buf, `\\`...)
|
||||
}
|
||||
default:
|
||||
buf = append(buf, s[:sz]...)
|
||||
}
|
||||
|
||||
+2
-2
@@ -121,12 +121,12 @@ func TestFlavorInterpolate(t *testing.T) {
|
||||
{
|
||||
SQLite,
|
||||
"SELECT * FROM a WHERE name = ? AND state IN (?, ?, ?, ?, ?)", []interface{}{"I'm fine", 42, int8(8), int16(-16), int32(32), int64(64)},
|
||||
"SELECT * FROM a WHERE name = 'I\\'m fine' AND state IN (42, 8, -16, 32, 64)", nil,
|
||||
"SELECT * FROM a WHERE name = 'I''m fine' AND state IN (42, 8, -16, 32, 64)", nil,
|
||||
},
|
||||
{
|
||||
SQLite,
|
||||
"SELECT * FROM `a?` WHERE name = \"?\" AND state IN (?, '?', ?, ?, ?, ?, ?)", []interface{}{"\r\n\b\t\x1a\x00\\\"'", uint(42), uint8(8), uint16(16), uint32(32), uint64(64), "useless"},
|
||||
"SELECT * FROM `a?` WHERE name = \"?\" AND state IN ('\\r\\n\\b\\t\\Z\\0\\\\\\\"\\'', '?', 42, 8, 16, 32, 64)", nil,
|
||||
"SELECT * FROM `a?` WHERE name = \"?\" AND state IN ('\r\n\b\t\x1a'||char(0)||'\\\"''', '?', 42, 8, 16, 32, 64)", nil,
|
||||
},
|
||||
{
|
||||
SQLite,
|
||||
|
||||
Reference in New Issue
Block a user