Merge pull request #228 from shoriwe/fix/sqlite-strings

fix #227: sqlite: string interpolation incorrectly uses backslash escapes
This commit is contained in:
Huan Du
2025-12-30 15:24:20 +08:00
committed by GitHub
3 changed files with 55 additions and 23 deletions
+1 -1
View File
@@ -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
View File
@@ -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
View File
@@ -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,