diff --git a/flavor_test.go b/flavor_test.go index ff9520c..3d21d8a 100644 --- a/flavor_test.go +++ b/flavor_test.go @@ -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%' // } diff --git a/interpolate.go b/interpolate.go index c6eb1db..59abaa6 100644 --- a/interpolate.go +++ b/interpolate.go @@ -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]...) } diff --git a/interpolate_test.go b/interpolate_test.go index eafd81c..454dfb4 100644 --- a/interpolate_test.go +++ b/interpolate_test.go @@ -120,12 +120,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,