Skip to content

Commit 4a68770

Browse files
authored
Merge pull request #69 from WordPress/backport/sql-parser-changes
Backport fixes from the sql-parser repo
2 parents 7f45e27 + a66d7f1 commit 4a68770

File tree

2 files changed

+16
-22
lines changed

2 files changed

+16
-22
lines changed

wp-includes/sqlite/class-wp-sqlite-lexer.php

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* This file is a port of the Lexer & Tokens_List classes from the PHPMyAdmin/sql-parser library.
3+
* This file is a port of the Lexer & TokensList classes from the PHPMyAdmin/sql-parser library.
44
*
55
* @package wp-sqlite-integration
66
* @see https://github.com/phpmyadmin/sql-parser
@@ -33,7 +33,7 @@ class WP_SQLite_Lexer {
3333
*
3434
* @var string[]
3535
*/
36-
public static $parser_methods = array(
36+
protected const PARSER_METHODS = array(
3737
// It is best to put the parsers in order of their complexity
3838
// (ascending) and their occurrence rate (descending).
3939
//
@@ -77,7 +77,7 @@ class WP_SQLite_Lexer {
7777
*
7878
* @var string[]
7979
*/
80-
public $keyword_name_indicators = array(
80+
protected const KEYWORD_NAME_INDICATORS = array(
8181
'FROM',
8282
'SET',
8383
'WHERE',
@@ -89,7 +89,7 @@ class WP_SQLite_Lexer {
8989
*
9090
* @var string[]
9191
*/
92-
public $operator_name_indicators = array(
92+
protected const OPERATOR_NAME_INDICATORS = array(
9393
',',
9494
'.',
9595
);
@@ -1486,7 +1486,7 @@ public function lex() {
14861486
*/
14871487
$token = null;
14881488

1489-
foreach ( static::$parser_methods as $method ) {
1489+
foreach ( self::PARSER_METHODS as $method ) {
14901490
$token = $this->$method();
14911491

14921492
if ( $token ) {
@@ -1668,10 +1668,10 @@ private function solve_ambiguity_on_function_keywords() {
16681668
$next = $this->tokens_get_next();
16691669
if (
16701670
( WP_SQLite_Token::TYPE_KEYWORD !== $next->type
1671-
|| ! in_array( $next->value, $this->keyword_name_indicators, true )
1671+
|| ! in_array( $next->value, self::KEYWORD_NAME_INDICATORS, true )
16721672
)
16731673
&& ( WP_SQLite_Token::TYPE_OPERATOR !== $next->type
1674-
|| ! in_array( $next->value, $this->operator_name_indicators, true )
1674+
|| ! in_array( $next->value, self::OPERATOR_NAME_INDICATORS, true )
16751675
)
16761676
&& ( null !== $next->value )
16771677
) {
@@ -2068,7 +2068,7 @@ public function parse_number() {
20682068
} elseif (
20692069
$this->last + 1 < $this->string_length
20702070
&& '0' === $this->str[ $this->last ]
2071-
&& ( 'x' === $this->str[ $this->last + 1 ] || 'X' === $this->str[ $this->last + 1 ] )
2071+
&& 'x' === $this->str[ $this->last + 1 ]
20722072
) {
20732073
$token .= $this->str[ $this->last++ ];
20742074
$state = 2;
@@ -2514,15 +2514,10 @@ public static function is_separator( $str ) {
25142514
* Constructor.
25152515
*
25162516
* @param stdClass[] $tokens The initial array of tokens.
2517-
* @param int $count The count of tokens in the initial array.
25182517
*/
2519-
public function tokens( array $tokens = array(), $count = -1 ) {
2520-
if ( empty( $tokens ) ) {
2521-
return;
2522-
}
2523-
2518+
public function tokens( array $tokens = array() ) {
25242519
$this->tokens = $tokens;
2525-
$this->tokens_count = -1 === $count ? count( $tokens ) : $count;
2520+
$this->tokens_count = count( $tokens );
25262521
}
25272522

25282523
/**

wp-includes/sqlite/class-wp-sqlite-token.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class WP_SQLite_Token {
161161
*
162162
* @var mixed|string|null
163163
*/
164-
public $keyword;
164+
public $keyword = null;
165165

166166
/**
167167
* The type of this token.
@@ -195,11 +195,10 @@ class WP_SQLite_Token {
195195
* @param int $flags The flags of the token.
196196
*/
197197
public function __construct( $token, $type = 0, $flags = 0 ) {
198-
$this->token = $token;
199-
$this->type = $type;
200-
$this->flags = $flags;
201-
$this->keyword = null;
202-
$this->value = $this->extract();
198+
$this->token = $token;
199+
$this->type = $type;
200+
$this->flags = $flags;
201+
$this->value = $this->extract();
203202
}
204203

205204
/**
@@ -262,8 +261,8 @@ private function extract() {
262261
case self::TYPE_NUMBER:
263262
$ret = str_replace( '--', '', $this->token ); // e.g. ---42 === -42.
264263
if ( $this->flags & self::FLAG_NUMBER_HEX ) {
264+
$ret = str_replace( array( '-', '+' ), '', $this->token );
265265
if ( $this->flags & self::FLAG_NUMBER_NEGATIVE ) {
266-
$ret = str_replace( '-', '', $this->token );
267266
$ret = -hexdec( $ret );
268267
} else {
269268
$ret = hexdec( $ret );

0 commit comments

Comments
 (0)