@@ -8,12 +8,12 @@ global no_values: Array[Value] (Array[Value]{})
88global 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+
152162fn is_word_byte(ch: u8) bool {
153163 return ch.is_alpha_numeric() || ch == '_'
154164}
0 commit comments