Skip to content

Commit 836e607

Browse files
committed
Placeholders and migration splits honour backslash escapes in MySQL and E'' strings
1 parent b2f33a5 commit 836e607

7 files changed

Lines changed: 41 additions & 12 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ let found = db.all("SELECT * FROM users WHERE id IN (:ids)", .{ "ids" => ids })
8181
```
8282

8383
An empty list becomes `NULL`, so `IN (:ids)` matches nothing and `NOT IN (:ids)` matches nothing
84-
either. Text in quotes and Postgres casts such as `::int` are left alone, and a `?` of your own,
84+
either. Text in quotes (with MySQL's backslash escapes) and Postgres casts such as `::int` are left alone, and a `?` of your own,
8585
such as the Postgres JSON operator, stays what it is. A name without a value throws `syntax`.
8686
`sql.named(statement, values, dialect)` does the conversion by itself, for code that passes the
8787
statement on.

‎src/database.valk‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121

122122
// Keeps a statement whose values were all single, with the names in the order they were taken
123123
- fn remember(statement: String, text: String, args: Array[Value]) {
124-
let names = named_order(statement)
124+
let names = named_order(statement, this.driver.dialect)
125125
if names.length != args.length : return
126126
let known = this.statements ?? <{
127127
let created = Map[NamedStatement]{}

‎src/migrate.valk‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ use valk.time
103103
fn run_one(migration: Migration) !Error {
104104
let db = this.db
105105
db.begin() !>
106-
each split_statements(migration.sql) as statement {
106+
each split_statements(migration.sql, db.dialect) as statement {
107107
db.exec(statement) ! {
108108
db.rollback() _
109109
throw E { message: "Migration '" + migration.name + "' failed: " + E.message, sql: statement }
@@ -139,7 +139,7 @@ fn is_space(ch: u8) bool {
139139

140140
// Splits a file into statements on `;`, keeping text in quotes and comments together. The body
141141
// of a `CREATE TRIGGER`, `PROCEDURE` or `FUNCTION` between `BEGIN` and `END` holds `;` of its own.
142-
fn split_statements(sql: String) Array[String] {
142+
fn split_statements(sql: String, dialect: Dialect (Dialect.sqlite)) Array[String] {
143143
let out: Array[String] = .{}
144144
let current = ByteBuffer.new(sql.length)
145145
let i: uint = 0
@@ -151,7 +151,7 @@ fn split_statements(sql: String) Array[String] {
151151
let depth: uint = 0
152152
let after_end = false
153153
while i < length {
154-
let skipped = skip_quoted(sql, i)
154+
let skipped = skip_quoted(sql, i, dialect == Dialect.mysql)
155155
if skipped > i {
156156
// Line comments are left out; everything else is kept as it is
157157
if sql[i] != '-' {

‎src/named.valk‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ global no_values: Array[Value] (Array[Value]{})
88
global no_named_values: Map[Value] (Map[Value]{})
99

1010
// The names of the placeholders of `statement`, in order, a name as often as it appears
11-
fn named_order(statement: String) Array[String] {
11+
fn named_order(statement: String, dialect: Dialect) Array[String] {
1212
let names = Array[String]{}
1313
let i: uint = 0
1414
let length = statement.length
1515
while i < length {
16-
let skipped = skip_quoted(statement, i)
16+
let skipped = skip_quoted(statement, i, dialect == Dialect.mysql)
1717
if skipped > i {
1818
i = skipped
1919
continue
@@ -74,7 +74,7 @@ fn scan_named(statement: String, values: ?Map[Value], out: ByteBuffer, args: Arr
7474
let length = statement.length
7575
while i < length {
7676
// Quoted text and comments are copied as they are
77-
let skipped = skip_quoted(statement, i)
77+
let skipped = skip_quoted(statement, i, isset(dialect) && dialect == Dialect.mysql)
7878
if skipped > i {
7979
while i < skipped : out.write_byte(statement[i++])
8080
continue
@@ -106,14 +106,18 @@ fn scan_named(statement: String, values: ?Map[Value], out: ByteBuffer, args: Arr
106106
}
107107

108108
// Where the quoted text or comment that starts at `i` ends, or `i` when none starts there. A
109-
// doubled quote inside ends the text and starts it again, which comes to the same.
110-
fn skip_quoted(text: String, i: uint) uint {
109+
// doubled quote inside ends the text and starts it again, which comes to the same. A backslash
110+
// escapes the next byte in MySQL strings and in Postgres `E'...'` strings.
111+
fn skip_quoted(text: String, i: uint, backslash: bool (false)) uint {
111112
let length = text.length
112113
let ch = text[i]
113114
if ch == '\'' || ch == '"' || ch == '`' {
115+
let escapes = ch != '`' && (backslash || (ch == '\'' && is_escape_prefix(text, i)))
114116
let at = i + 1
115117
while at < length {
116-
if text[at++] == ch : return at
118+
let c = text[at++]
119+
if c == ch : return at
120+
if c == '\\' && escapes : at++
117121
}
118122
return length
119123
}
@@ -149,6 +153,12 @@ fn skip_quoted(text: String, i: uint) uint {
149153
return i
150154
}
151155

156+
// `E'` or `e'`, not the end of a longer word
157+
fn is_escape_prefix(text: String, quote: uint) bool {
158+
if quote == 0 || (text[quote - 1] != 'E' && text[quote - 1] != 'e') : return false
159+
return quote == 1 || !is_word_byte(text[quote - 2])
160+
}
161+
152162
fn is_word_byte(ch: u8) bool {
153163
return ch.is_alpha_numeric() || ch == '_'
154164
}

‎src/statement.valk‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363

6464
// Reads the placeholders of `statement` once, for `Db.prepare`
6565
fn prepare_statement(db: Db, statement: String) Statement !Error {
66-
let names = named_order(statement)
66+
let names = named_order(statement, db.dialect)
6767
if names.length == 0 {
6868
return Statement { db: db, sql: statement, text: statement, names: names }
6969
}

‎tests/basics.valk‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,15 @@ test "Placeholders inside comments and dollar quotes" {
172172
assert(body == "CREATE FUNCTION f() RETURNS int AS $$ SELECT :a; $$ LANGUAGE sql; SELECT $tag$ :b $tag$, a$b, ?")
173173
assert(none.length == 1)
174174

175+
// A backslash escapes a quote in MySQL strings and in Postgres E'' strings, and nowhere else
176+
let my, my_args = sql.named("SELECT 'it\\'s :a', \"x\\\" :b\", :c", .{ "c" => 1 }, sql.Dialect.mysql) ! { println(E.message) assert(false) return }
177+
assert(my == "SELECT 'it\\'s :a', \"x\\\" :b\", ?")
178+
assert(my_args.length == 1)
179+
let pg_escaped, _ = sql.named("SELECT E'it\\'s :a', :c", .{ "c" => 1 }, sql.Dialect.postgres) ! { println(E.message) assert(false) return }
180+
assert(pg_escaped == "SELECT E'it\\'s :a', $1")
181+
let lite_path, _ = sql.named("SELECT 'C:\\', :c", .{ "c" => 1 }) ! { println(E.message) assert(false) return }
182+
assert(lite_path == "SELECT 'C:\\', ?")
183+
175184
// A prepared statement reads its names the same way
176185
let driver = fake()
177186
let db = fake_db(driver)

‎tests/migrate.valk‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,13 @@ test "Splitting bodies, block comments and dollar quotes" {
118118
assert(s.contains("CREATE PROCEDURE p() BEGIN IF 1 THEN SELECT 1; END IF; CASE 1 WHEN 1 THEN SELECT 2; END CASE; END"))
119119
assert(s.contains("SELECT 2"))
120120
}
121+
122+
test "MySQL migrations keep backslash-escaped quotes together" {
123+
let driver = fake()
124+
driver.dialect_kind = sql.Dialect.mysql
125+
let migrator = sql.Migrator.new(fake_db(driver))
126+
migrator.add("0001", "INSERT INTO t VALUES ('a\\';b');\nSELECT 1;")
127+
migrator.migrate() ! { println(E.message) assert(false) return }
128+
assert(driver.statements.contains("INSERT INTO t VALUES ('a\\';b')"))
129+
assert(driver.statements.contains("SELECT 1"))
130+
}

0 commit comments

Comments
 (0)