Skip to content

Commit 087e83f

Browse files
authored
Add ANSI_QUOTES and SQL mode validation (#452)
## Summary This PR improves SQL mode handling and adds **`ANSI_QUOTES`** and **`ANSII`** support. This includes: - **SQL mode storage:** SQL modes use MySQL's native bitmap and canonical order. - **Validation:** Named and numeric assignments are validated with MySQL-compatible errors. - **ANSI_QUOTES:** `ANSI_QUOTES` is supported by the driver, standalone parser, and native lexer. - **ANSI** Composite `ANSI` SQL mode is expanded into its component modes. - **Quoted identifiers:** Fixed backslash behavior in quoted identifiers. - **Native builds:** Lexer constants are regenerated, and the generator's monorepo path is fixed. ## Why The `ANSI_QUOTES` SQL mode support keeps reappearing in our recent performance optimization and parser experiments. This PR extracts and completes that functionality. Additionally, the driver stored arbitrary mode names, accepted invalid assignments, always treated double quotes as string delimiters, did not expand did not expand composite SQL modes, and applied string escaping to quoted identifiers. The standalone parser and native lexer lacked equivalent quoted-identifier handling.
1 parent 38a6921 commit 087e83f

13 files changed

Lines changed: 907 additions & 58 deletions

‎packages/mysql-on-sqlite/src/mysql/class-wp-mysql-lexer.php‎

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class WP_MySQL_Lexer {
3232
const SQL_MODE_PIPES_AS_CONCAT = 2;
3333
const SQL_MODE_IGNORE_SPACE = 4;
3434
const SQL_MODE_NO_BACKSLASH_ESCAPES = 8;
35+
const SQL_MODE_ANSI_QUOTES = 16;
3536

3637
/**
3738
* Character masks for frequently used character classes.
@@ -2209,6 +2210,19 @@ public function __construct(
22092210
$this->sql_modes |= self::SQL_MODE_IGNORE_SPACE;
22102211
} elseif ( 'NO_BACKSLASH_ESCAPES' === $sql_mode ) {
22112212
$this->sql_modes |= self::SQL_MODE_NO_BACKSLASH_ESCAPES;
2213+
} elseif ( 'ANSI_QUOTES' === $sql_mode ) {
2214+
$this->sql_modes |= self::SQL_MODE_ANSI_QUOTES;
2215+
} elseif ( 'ANSI' === $sql_mode ) {
2216+
/*
2217+
* Expand the composite ANSI mode into its lexer-relevant components.
2218+
* The ANSI mode also implies REAL_AS_FLOAT and ONLY_FULL_GROUP_BY,
2219+
* which do not affect the lexer.
2220+
*
2221+
* See: https://dev.mysql.com/doc/refman/8.4/en/sql-mode.html#sqlmode_ansi
2222+
*/
2223+
$this->sql_modes |= self::SQL_MODE_PIPES_AS_CONCAT
2224+
| self::SQL_MODE_IGNORE_SPACE
2225+
| self::SQL_MODE_ANSI_QUOTES;
22122226
}
22132227
}
22142228
}
@@ -2891,15 +2905,30 @@ private function read_number(): ?int {
28912905
*
28922906
* Rules:
28932907
* 1. Quotes can be escaped by doubling them ('', "", ``).
2894-
* 2. Backslashes escape the next character, unless NO_BACKSLASH_ESCAPES is set.
2908+
* 2. In string literals, backslashes escape the next character,
2909+
* unless the NO_BACKSLASH_ESCAPES SQL mode is set.
2910+
* 3. In identifiers, backslashes are always literal and never escape.
28952911
*/
28962912
private function read_quoted_text(): ?int {
28972913
$quote = $this->sql[ $this->bytes_already_read ];
28982914
$this->bytes_already_read += 1; // Consume the quote.
28992915

2900-
$no_backslash_escapes = $this->is_sql_mode_active(
2901-
self::SQL_MODE_NO_BACKSLASH_ESCAPES
2902-
);
2916+
/*
2917+
* Determine whether the quote opens an identifier or a string literal.
2918+
* An identifier is quoted with a backtick or a double quote when the
2919+
* ANSI_QUOTES SQL mode is active. Otherwise, it is a string literal.
2920+
*
2921+
* See: https://dev.mysql.com/doc/refman/8.4/en/sql-mode.html#sqlmode_ansi_quotes
2922+
*/
2923+
$is_identifier_quote = '`' === $quote
2924+
|| ( '"' === $quote && $this->is_sql_mode_active( self::SQL_MODE_ANSI_QUOTES ) );
2925+
2926+
/*
2927+
* Backslash escapes apply only to string literals, and only when the
2928+
* NO_BACKSLASH_ESCAPES SQL mode is not set.
2929+
*/
2930+
$backslash_is_escape = ! $is_identifier_quote
2931+
&& ! $this->is_sql_mode_active( self::SQL_MODE_NO_BACKSLASH_ESCAPES );
29032932

29042933
// We need to look for the closing quote in a loop, as it can be escaped,
29052934
// in which case the escape sequence is consumed and the loop continues.
@@ -2912,9 +2941,9 @@ private function read_quoted_text(): ?int {
29122941
$at = $quote_at;
29132942

29142943
/*
2915-
* By default, quotes can be escaped with a "\".
2916-
* When NO_BACKSLASH_ESCAPES SQL mode is active, the "\" treated as
2917-
* a regular character.
2944+
* In string literals, quotes can be escaped with a backslash. When
2945+
* NO_BACKSLASH_ESCAPES SQL mode is active, the backslash is treated
2946+
* as a regular character. Identifiers never use backslash escaping.
29182947
*
29192948
* The quote is escaped only when the number of preceding backslashes
29202949
* is odd - "\" is an escape sequence, "\\" is an escaped backslash,
@@ -2925,7 +2954,7 @@ private function read_quoted_text(): ?int {
29252954
* sits at the very start of the input. The `?? null` covers
29262955
* positive out-of-range indexes belt-and-suspenders.
29272956
*/
2928-
if ( ! $no_backslash_escapes ) {
2957+
if ( $backslash_is_escape ) {
29292958
$i = 0;
29302959
while ( ( $at - $i - 1 ) >= 0 && '\\' === ( $this->sql[ $at - $i - 1 ] ?? null ) ) {
29312960
$i += 1;
@@ -2948,13 +2977,10 @@ private function read_quoted_text(): ?int {
29482977

29492978
$this->bytes_already_read = $at;
29502979

2951-
if ( '`' === $quote ) {
2980+
if ( $is_identifier_quote ) {
29522981
return self::BACK_TICK_QUOTED_ID;
2953-
} elseif ( '"' === $quote ) {
2954-
return self::DOUBLE_QUOTED_TEXT;
2955-
} else {
2956-
return self::SINGLE_QUOTED_TEXT;
29572982
}
2983+
return '"' === $quote ? self::DOUBLE_QUOTED_TEXT : self::SINGLE_QUOTED_TEXT;
29582984
}
29592985

29602986
private function read_line_comment(): int {

‎packages/mysql-on-sqlite/src/mysql/class-wp-mysql-token.php‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,16 @@ public function get_value(): string {
7272
$value = substr( $value, 1, -1 );
7373

7474
/*
75-
* When the NO_BACKSLASH_ESCAPES SQL mode is enabled, we only need to
76-
* handle escaped bounding quotes, as the other characters preserve
77-
* their literal values.
75+
* Quoted identifiers escape their bounding quote by doubling it, while
76+
* backslashes remain literal. Strings in NO_BACKSLASH_ESCAPES mode use
77+
* the same unquoting rule.
78+
*
79+
* See: https://dev.mysql.com/doc/refman/8.4/en/identifiers.html
7880
*/
79-
if ( $this->sql_mode_no_backslash_escapes_enabled ) {
81+
if (
82+
WP_MySQL_Lexer::BACK_TICK_QUOTED_ID === $this->id
83+
|| $this->sql_mode_no_backslash_escapes_enabled
84+
) {
8085
return str_replace( $quote . $quote, $quote, $value );
8186
}
8287

0 commit comments

Comments
 (0)