Skip to content

Commit 21cf114

Browse files
authored
Merge pull request #228 from shoriwe/fix/sqlite-strings
fix #227: sqlite: string interpolation incorrectly uses backslash escapes
2 parents 0183453 + 2994d27 commit 21cf114

3 files changed

Lines changed: 55 additions & 23 deletions

File tree

flavor_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func ExampleFlavor_Interpolate_sqlite() {
105105
fmt.Println(err)
106106

107107
// Output:
108-
// SELECT name FROM user WHERE id <> 1234 AND name = 'Charmy Liu' AND desc LIKE '%mother\'s day%'
108+
// SELECT name FROM user WHERE id <> 1234 AND name = 'Charmy Liu' AND desc LIKE '%mother''s day%'
109109
// <nil>
110110
}
111111

interpolate.go

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -785,36 +785,68 @@ func quoteStringValue(buf []byte, s string, flavor Flavor) []byte {
785785
for ; sz != 0; r, sz = utf8.DecodeRuneInString(s) {
786786
switch r {
787787
case '\x00':
788-
buf = append(buf, "\\0"...)
789-
788+
switch flavor {
789+
case SQLite:
790+
buf = append(buf, `'||char(0)||'`...)
791+
default:
792+
buf = append(buf, `\0`...)
793+
}
790794
case '\b':
791-
buf = append(buf, "\\b"...)
792-
795+
switch flavor {
796+
case SQLite:
797+
buf = append(buf, '\b')
798+
default:
799+
buf = append(buf, `\b`...)
800+
}
793801
case '\n':
794-
buf = append(buf, "\\n"...)
795-
802+
switch flavor {
803+
case SQLite:
804+
buf = append(buf, '\n')
805+
default:
806+
buf = append(buf, `\n`...)
807+
}
796808
case '\r':
797-
buf = append(buf, "\\r"...)
798-
809+
switch flavor {
810+
case SQLite:
811+
buf = append(buf, '\r')
812+
default:
813+
buf = append(buf, `\r`...)
814+
}
799815
case '\t':
800-
buf = append(buf, "\\t"...)
801-
816+
switch flavor {
817+
case SQLite:
818+
buf = append(buf, '\t')
819+
default:
820+
buf = append(buf, `\t`...)
821+
}
802822
case '\x1a':
803-
buf = append(buf, "\\Z"...)
804-
823+
switch flavor {
824+
case SQLite:
825+
buf = append(buf, '\x1a')
826+
default:
827+
buf = append(buf, `\Z`...)
828+
}
805829
case '\'':
806-
if flavor == CQL {
830+
switch flavor {
831+
case SQLite, CQL:
807832
buf = append(buf, "''"...)
808-
} else {
809-
buf = append(buf, "\\'"...)
833+
default:
834+
buf = append(buf, `\'`...)
810835
}
811-
812836
case '"':
813-
buf = append(buf, "\\\""...)
814-
837+
switch flavor {
838+
case SQLite:
839+
buf = append(buf, '"')
840+
default:
841+
buf = append(buf, `\"`...)
842+
}
815843
case '\\':
816-
buf = append(buf, "\\\\"...)
817-
844+
switch flavor {
845+
case SQLite:
846+
buf = append(buf, '\\')
847+
default:
848+
buf = append(buf, `\\`...)
849+
}
818850
default:
819851
buf = append(buf, s[:sz]...)
820852
}

interpolate_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,12 @@ func TestFlavorInterpolate(t *testing.T) {
121121
{
122122
SQLite,
123123
"SELECT * FROM a WHERE name = ? AND state IN (?, ?, ?, ?, ?)", []interface{}{"I'm fine", 42, int8(8), int16(-16), int32(32), int64(64)},
124-
"SELECT * FROM a WHERE name = 'I\\'m fine' AND state IN (42, 8, -16, 32, 64)", nil,
124+
"SELECT * FROM a WHERE name = 'I''m fine' AND state IN (42, 8, -16, 32, 64)", nil,
125125
},
126126
{
127127
SQLite,
128128
"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"},
129-
"SELECT * FROM `a?` WHERE name = \"?\" AND state IN ('\\r\\n\\b\\t\\Z\\0\\\\\\\"\\'', '?', 42, 8, 16, 32, 64)", nil,
129+
"SELECT * FROM `a?` WHERE name = \"?\" AND state IN ('\r\n\b\t\x1a'||char(0)||'\\\"''', '?', 42, 8, 16, 32, 64)", nil,
130130
},
131131
{
132132
SQLite,

0 commit comments

Comments
 (0)