-
Notifications
You must be signed in to change notification settings - Fork 47
Add support for CURRENT_TIMESTAMP()
calls with parentheses
#151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for CURRENT_TIMESTAMP()
calls with parentheses
#151
Conversation
While SQLite does support CURRENT_TIMESTAMP function calls natively, it doesn't support calling the function with parentheses in the form of CURRENT_TIMESTAMP(), so we need to remove them.
$tokens = ( new WP_SQLite_Lexer( $query ) )->tokens; | ||
|
||
// SQLite does not support CURRENT_TIMESTAMP() calls with parentheses. | ||
// Since CURRENT_TIMESTAMP() can appear in most types of SQL queries, | ||
// let's remove the parentheses globally before further processing. | ||
foreach ( $tokens as $i => $token ) { | ||
if ( WP_SQLite_Token::TYPE_KEYWORD === $token->type && 'CURRENT_TIMESTAMP' === $token->keyword ) { | ||
$paren_open = $tokens[ $i + 1 ] ?? null; | ||
$paren_close = $tokens[ $i + 2 ] ?? null; | ||
if ( WP_SQLite_Token::TYPE_OPERATOR === $paren_open->type && '(' === $paren_open->value | ||
&& WP_SQLite_Token::TYPE_OPERATOR === $paren_close->type && ')' === $paren_close->value ) { | ||
unset( $tokens[ $i + 1 ], $tokens[ $i + 2 ] ); | ||
} | ||
} | ||
} | ||
$tokens = array_values( $tokens ); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, I have the opposite feedback. This Lexer class is a direct port of the PHPMyAdmin Lexer and is meant to tokenize MySQL queries. parentheses are valid in MySQL and removing them in there sounds like an abstraction leak. Not that it matters with the AST Parser/Lexer explorations coming. I'm happy to get this change in as it is as I don't think we'll stick to the current lexer in the longer term, but if you were keen to relocate it then its original location in the Translator class seem fine to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adamziel If you prefer the variant from the first commit, I can just drop the last one and repush, and we can go with that for now. I guess if the new approach replaces the old one, it can be still valuable to add the test case, and we can have the functionality earlier.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes please and agreed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adamziel Thanks! Repushed.
Co-authored-by: Bero <[email protected]>
12ef9ae
to
43b628d
Compare
43b628d
to
2b1111c
Compare
Thank you! |
While SQLite does support
CURRENT_TIMESTAMP
function calls natively, it doesn't support calling the function with parentheses in the form ofCURRENT_TIMESTAMP()
.This pull request removes the parentheses after
CURRENT_TIMESTAMP
keyword for all types of queries.The following types of queries will now work:
Closes #129.