Skip to content

Commit 88dfafa

Browse files
committed
Name national string columns like MySQL
SELECT N'abc' named its column N'abc', while MySQL names it abc. A national string has no textStringLiteral node, so the alias logic didn't treat it as a bare string literal.
1 parent d7819c5 commit 88dfafa

3 files changed

Lines changed: 12 additions & 4 deletions

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5593,6 +5593,7 @@ private function translate_select_item( WP_Parser_Node $node ): string {
55935593
*
55945594
* Descend the AST until we reach a textStringLiteral. If at any level
55955595
* we don't have a single child node, bail out; it's not a bare literal.
5596+
* A national string, such as N'abc', is a single NCHAR_TEXT token.
55965597
*/
55975598
$current = $node;
55985599
while ( 'textStringLiteral' !== $current->rule_name ) {
@@ -5602,7 +5603,10 @@ private function translate_select_item( WP_Parser_Node $node ): string {
56025603
}
56035604
$current = $children[0];
56045605
}
5605-
if ( 'textStringLiteral' === $current->rule_name ) {
5606+
if (
5607+
'textStringLiteral' === $current->rule_name
5608+
|| ( 1 === count( $current->get_children() ) && $current->has_child_token( WP_MySQL_Lexer::NCHAR_TEXT ) )
5609+
) {
56065610
$alias = $current->get_first_child_token()->get_value();
56075611

56085612
// When the literal value contains a NULL byte, MySQL truncates the

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10638,6 +10638,10 @@ public function testSelectColumnNames(): void {
1063810638
$result = $this->assertQuery( "SELECT 'abc\0def' AS `col1`" );
1063910639
$this->assertSame( array( 'col1' ), array_keys( (array) $result[0] ) );
1064010640

10641+
// National string literals (no explicit alias).
10642+
$result = $this->assertQuery( "SELECT N'abc', n'it''s'" );
10643+
$this->assertSame( array( 'abc', "it's" ), array_keys( (array) $result[0] ) );
10644+
1064110645
// Columns (no explicit alias).
1064210646
$result = $this->assertQuery( 'SELECT id, name FROM t' );
1064310647
$this->assertSame( array( 'id', 'name' ), array_keys( (array) $result[0] ) );

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Translation_Tests.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,15 +1756,15 @@ public function testHexadecimalLiterals(): void {
17561756

17571757
public function testNationalStringLiterals(): void {
17581758
$this->assertQuery(
1759-
"SELECT 'abc' AS `N'abc'`",
1759+
"SELECT 'abc' AS `abc`",
17601760
"SELECT N'abc'"
17611761
);
17621762
$this->assertQuery(
1763-
"SELECT 'it''s' AS `n'it''s'`",
1763+
"SELECT 'it''s' AS `it's`",
17641764
"SELECT n'it''s'"
17651765
);
17661766
$this->assertQuery(
1767-
"SELECT '0 OR 1=1' AS `N'0 OR 1=1'`",
1767+
"SELECT '0 OR 1=1' AS `0 OR 1=1`",
17681768
"SELECT N'0 OR 1=1'"
17691769
);
17701770
}

0 commit comments

Comments
 (0)