Skip to content

Commit 12ef9ae

Browse files
committed
Move handling of CURRENT_TIMESTAMP() with parentheses to lexer
1 parent 2b1111c commit 12ef9ae

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,6 +1764,32 @@ public function parse_keyword() {
17641764

17651765
$this->last = $i_end;
17661766

1767+
// SQLite does not support CURRENT_TIMESTAMP() function calls with parentheses,
1768+
// but it does support the equivalent CURRENT_TIMESTAMP variant without them.
1769+
// Let's remove the parentheses and surrounding spaces.
1770+
if ( $ret && 'CURRENT_TIMESTAMP' === $ret->value ) {
1771+
$end = $this->last + 1;
1772+
$open_paren = false;
1773+
$close_paren = false;
1774+
while ( static::is_whitespace( $this->str[ $end ] ) ) {
1775+
$end += 1;
1776+
}
1777+
if ( '(' === $this->str[ $end ] ?? '' ) {
1778+
$open_paren = true;
1779+
$end += 1;
1780+
}
1781+
while ( static::is_whitespace( $this->str[ $end ] ) ) {
1782+
$end += 1;
1783+
}
1784+
if ( ')' === $this->str[ $end ] ?? '' ) {
1785+
$close_paren = true;
1786+
}
1787+
1788+
if ( $open_paren && $close_paren ) {
1789+
$this->last = $end;
1790+
}
1791+
}
1792+
17671793
return $ret;
17681794
}
17691795

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

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -772,23 +772,7 @@ public function get_return_value() {
772772
* @throws Exception If the query is not supported.
773773
*/
774774
private function execute_mysql_query( $query ) {
775-
$tokens = ( new WP_SQLite_Lexer( $query ) )->tokens;
776-
777-
// SQLite does not support CURRENT_TIMESTAMP() calls with parentheses.
778-
// Since CURRENT_TIMESTAMP() can appear in most types of SQL queries,
779-
// let's remove the parentheses globally before further processing.
780-
foreach ( $tokens as $i => $token ) {
781-
if ( WP_SQLite_Token::TYPE_KEYWORD === $token->type && 'CURRENT_TIMESTAMP' === $token->keyword ) {
782-
$paren_open = $tokens[ $i + 1 ] ?? null;
783-
$paren_close = $tokens[ $i + 2 ] ?? null;
784-
if ( WP_SQLite_Token::TYPE_OPERATOR === $paren_open->type && '(' === $paren_open->value
785-
&& WP_SQLite_Token::TYPE_OPERATOR === $paren_close->type && ')' === $paren_close->value ) {
786-
unset( $tokens[ $i + 1 ], $tokens[ $i + 2 ] );
787-
}
788-
}
789-
}
790-
$tokens = array_values( $tokens );
791-
775+
$tokens = ( new WP_SQLite_Lexer( $query ) )->tokens;
792776
$this->rewriter = new WP_SQLite_Query_Rewriter( $tokens );
793777
$this->query_type = $this->rewriter->peek()->value;
794778

0 commit comments

Comments
 (0)