@@ -944,6 +944,22 @@ class WP_MySQL_Lexer {
944944 const WHITESPACE = 0 ;
945945 const EOF = -1 ;
946946
947+ /**
948+ * Lexer states that a token sets for the next one (see "next_state").
949+ *
950+ * They match MySQL's lexer states in sql/sql_lex.cc, with full words
951+ * instead of MySQL's abbreviations:
952+ *
953+ * - STATE_START: MY_LEX_START
954+ * - STATE_IDENTIFIER_SEPARATOR: MY_LEX_IDENT_SEP
955+ * - STATE_IDENTIFIER_START: MY_LEX_IDENT_START
956+ * - STATE_IDENTIFIER_OR_KEYWORD: MY_LEX_IDENT_OR_KEYWORD
957+ */
958+ private const STATE_START = -2 ;
959+ private const STATE_IDENTIFIER_SEPARATOR = -3 ;
960+ private const STATE_IDENTIFIER_START = -4 ;
961+ private const STATE_IDENTIFIER_OR_KEYWORD = -5 ;
962+
947963 /**
948964 * A map of SQL keyword string values to their corresponding token types.
949965 *
@@ -2176,6 +2192,26 @@ class WP_MySQL_Lexer {
21762192 */
21772193 private $ token_type ;
21782194
2195+ /**
2196+ * The lexer state that the previous token set for the next one.
2197+ *
2198+ * Like MySQL's "next_state", each token reads and resets it:
2199+ *
2200+ * - STATE_IDENTIFIER_SEPARATOR: Set by a name before "." and a name
2201+ * character. The "." is then a separator, even before digits ("t.1"),
2202+ * and the name is an identifier, even if it's a keyword ("order.id").
2203+ * - STATE_IDENTIFIER_START: Set by a "." before a name character. The
2204+ * name is then always an identifier ("t.order", "t.1", "t.x'41'").
2205+ * - STATE_IDENTIFIER_OR_KEYWORD: Set by "@@" before a name character.
2206+ * The name then stays a keyword, even before a "." ("@@session.x").
2207+ *
2208+ * A "." that ends a number sets no state, so "1.FROM" is a number and
2209+ * a keyword.
2210+ *
2211+ * @var int
2212+ */
2213+ private $ next_state = self ::STATE_START ;
2214+
21792215 /**
21802216 * Whether the tokenizer is inside an active MySQL-specific comment.
21812217 *
@@ -2420,6 +2456,10 @@ public static function get_token_name( int $token_id ): ?string {
24202456 }
24212457
24222458 private function read_next_token (): ?int {
2459+ // Read and reset the state that the previous token set.
2460+ $ state = $ this ->next_state ;
2461+ $ this ->next_state = self ::STATE_START ;
2462+
24232463 $ byte = $ this ->sql [ $ this ->bytes_already_read ] ?? null ;
24242464 $ next_byte = $ this ->sql [ $ this ->bytes_already_read + 1 ] ?? null ;
24252465
@@ -2444,21 +2484,22 @@ private function read_next_token(): ?int {
24442484 // restricts the accepted identifier codepoints to U+0080-U+FFFF.
24452485 // `"'" !== $next_byte` defers x'..', n'..' and similar special
24462486 // literals to their dedicated branches below; only single quotes
2447- // form those, regardless of SQL mode.
2487+ // form those, regardless of SQL mode. After a "." separator, the
2488+ // letter is a name instead ("x" in "t.x'41'").
24482489 if (
24492490 (
24502491 ( $ byte >= 'a ' && $ byte <= 'z ' )
24512492 || ( $ byte >= 'A ' && $ byte <= 'Z ' )
24522493 || $ byte > "\x7F"
24532494 )
2454- && "' " !== $ next_byte
2495+ && ( "' " !== $ next_byte || self :: STATE_IDENTIFIER_START === $ state )
24552496 ) {
24562497 $ started_at = $ this ->bytes_already_read ;
24572498 $ type = $ this ->read_identifier ();
24582499 if (
24592500 self ::IDENTIFIER === $ type
2460- // When preceded by a dot , it is always an identifier.
2461- && ! ( $ started_at > 0 && ' . ' === $ this -> sql [ $ started_at - 1 ] )
2501+ // After a "." separator , it is always an identifier.
2502+ && self :: STATE_IDENTIFIER_START !== $ state
24622503 ) {
24632504 // Inline the keyword lookup on the hot identifier path: most
24642505 // identifiers are not keywords, so this avoids two method calls
@@ -2467,7 +2508,15 @@ private function read_next_token(): ?int {
24672508 substr ( $ this ->sql , $ started_at , $ this ->bytes_already_read - $ started_at )
24682509 ) ] ?? self ::IDENTIFIER ;
24692510 if ( self ::IDENTIFIER !== $ keyword ) {
2470- $ type = $ this ->resolve_keyword_type ( $ keyword );
2511+ // Before "." and a name, a keyword is an identifier ("order.id"),
2512+ // except for a system variable scope ("@@session.x"). The next
2513+ // state that read_identifier() has just set tells what follows.
2514+ if (
2515+ self ::STATE_IDENTIFIER_SEPARATOR !== $ this ->next_state
2516+ || self ::STATE_IDENTIFIER_OR_KEYWORD === $ state
2517+ ) {
2518+ $ type = $ this ->resolve_keyword_type ( $ keyword );
2519+ }
24712520 }
24722521 }
24732522 } elseif ( null !== $ byte && isset ( $ single_byte_ops [ $ byte ] ) ) {
@@ -2477,13 +2526,28 @@ private function read_next_token(): ?int {
24772526 } elseif ( "' " === $ byte || '" ' === $ byte || '` ' === $ byte ) {
24782527 $ type = $ this ->read_quoted_text ();
24792528 } elseif ( null !== $ byte && strspn ( $ byte , self ::DIGIT_MASK ) > 0 ) {
2480- $ type = $ this ->read_number ();
2529+ // After a "." separator, it is a qualified name part ("1" in "t.1").
2530+ if ( self ::STATE_IDENTIFIER_START === $ state ) {
2531+ $ type = $ this ->read_identifier ();
2532+ } else {
2533+ $ type = $ this ->read_number ();
2534+ }
24812535 } elseif ( '. ' === $ byte ) {
2482- if ( null !== $ next_byte && strspn ( $ next_byte , self ::DIGIT_MASK ) > 0 ) {
2536+ if (
2537+ null !== $ next_byte
2538+ && strspn ( $ next_byte , self ::DIGIT_MASK ) > 0
2539+ // Right after a name, it is a qualified name separator.
2540+ && self ::STATE_IDENTIFIER_SEPARATOR !== $ state
2541+ ) {
24832542 $ type = $ this ->read_number ();
24842543 } else {
24852544 $ this ->bytes_already_read += 1 ;
24862545 $ type = self ::DOT_SYMBOL ;
2546+
2547+ // A name character right after it starts a qualified name part.
2548+ if ( null !== $ next_byte && ( strspn ( $ next_byte , self ::IDENTIFIER_MASK ) > 0 || $ next_byte > "\x7F" ) ) {
2549+ $ this ->next_state = self ::STATE_IDENTIFIER_START ;
2550+ }
24872551 }
24882552 } elseif ( ': ' === $ byte ) {
24892553 $ this ->bytes_already_read += 1 ; // Consume the ':'.
@@ -2604,6 +2668,14 @@ private function read_next_token(): ?int {
26042668 if ( '@ ' === $ next_byte ) {
26052669 $ this ->bytes_already_read += 1 ; // Consume the second '@'.
26062670 $ type = self ::AT_AT_SIGN_SYMBOL ;
2671+
2672+ // The system variable name after it is read with keywords.
2673+ if (
2674+ strspn ( $ this ->sql , self ::IDENTIFIER_MASK , $ this ->bytes_already_read , 1 ) > 0
2675+ || ( $ this ->sql [ $ this ->bytes_already_read ] ?? '' ) > "\x7F"
2676+ ) {
2677+ $ this ->next_state = self ::STATE_IDENTIFIER_OR_KEYWORD ;
2678+ }
26072679 } else {
26082680 /**
26092681 * Check whether the '@' marks an unquoted user-defined variable:
@@ -2648,8 +2720,8 @@ private function read_next_token(): ?int {
26482720 $ started_at = $ this ->bytes_already_read ;
26492721 $ type = $ this ->read_identifier ();
26502722 if ( self ::IDENTIFIER === $ type ) {
2651- // When preceded by a dot , it is always an identifier.
2652- if ( $ started_at > 0 && ' . ' === $ this -> sql [ $ started_at - 1 ] ) {
2723+ // After a "." separator , it is always an identifier.
2724+ if ( self :: STATE_IDENTIFIER_START === $ state ) {
26532725 $ type = self ::IDENTIFIER ;
26542726 } elseif ( '_ ' === $ byte && isset ( self ::UNDERSCORE_CHARSETS [ strtolower ( $ this ->get_current_token_bytes () ) ] ) ) {
26552727 $ type = self ::UNDERSCORE_CHARSET ;
@@ -2673,13 +2745,13 @@ private function get_current_token_bytes(): string {
26732745 * Read an unquoted identifier.
26742746 *
26752747 * This function reads characters that are allowed in an unquoted identifier.
2676- * An identifier cannot consist solely of digits, but this function doesn't
2677- * ensure that explicitly, as numbers are processed before identifiers in
2678- * the tokenization process, recognizing all digit-only sequences as numbers.
2748+ * It accepts digit-only sequences too; callers scan those as numbers, except
2749+ * for qualified name parts.
26792750 *
26802751 * Rules:
26812752 * 1. Allowed characters are ASCII a-z, A-Z, 0-9, _, $, and Unicode U+0080-U+FFFF.
26822753 * 2. Unquoted identifiers may begin with a digit but may not consist solely of digits.
2754+ * An exception is a part of a qualified name, such as "1" in "t.1".
26832755 *
26842756 * See:
26852757 * https://dev.mysql.com/doc/refman/8.4/en/identifiers.html
@@ -2738,17 +2810,25 @@ private function read_identifier(): ?int {
27382810 break ;
27392811 }
27402812
2741- // An identifier cannot consist solely of digits, but we don't need to
2742- // ensure that explicitly, as numbers are processed before identifiers.
2813+ if ( $ this ->bytes_already_read === $ started_at ) {
2814+ return null ; // Invalid input.
2815+ }
27432816
2744- return $ this ->bytes_already_read - $ started_at > 0
2745- ? self ::IDENTIFIER
2746- : null ; // Invalid input.
2817+ // When "." and a name character follow, the "." is a separator.
2818+ // "$byte_1" already holds the byte after the name.
2819+ if (
2820+ 0x2E === $ byte_1
2821+ && (
2822+ strspn ( $ this ->sql , self ::IDENTIFIER_MASK , $ this ->bytes_already_read + 1 , 1 ) > 0
2823+ || ( $ this ->sql [ $ this ->bytes_already_read + 1 ] ?? '' ) > "\x7F"
2824+ )
2825+ ) {
2826+ $ this ->next_state = self ::STATE_IDENTIFIER_SEPARATOR ;
2827+ }
2828+ return self ::IDENTIFIER ;
27472829 }
27482830
27492831 private function read_number (): ?int {
2750- // @TODO: Support numeric-only identifier parts after "." (e.g., 1ea10.1).
2751-
27522832 $ byte = $ this ->sql [ $ this ->bytes_already_read ] ?? null ;
27532833 $ next_byte = $ this ->sql [ $ this ->bytes_already_read + 1 ] ?? null ;
27542834 $ third_byte = $ this ->sql [ $ this ->bytes_already_read + 2 ] ?? null ;
0 commit comments