Skip to content

Commit ebc2ea0

Browse files
committed
Translate SUBSTRING() to SUBSTR(); avoid SUBSTRING() due to lack of support on old SQLite versions
1 parent 49e482f commit ebc2ea0

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

tests/WP_SQLite_Driver_Tests.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11181,6 +11181,20 @@ public function testVersionFunction(): void {
1118111181
$this->assertSame( '8.0.38', $result[0]->{'VERSION()'} );
1118211182
}
1118311183

11184+
public function testSubstringFunction(): void {
11185+
$result = $this->assertQuery( "SELECT SUBSTRING('abcdef', 1, 3) AS s" );
11186+
$this->assertSame( 'abc', $result[0]->s );
11187+
11188+
$result = $this->assertQuery( "SELECT SUBSTRING('abcdef', 4) AS s" );
11189+
$this->assertSame( 'def', $result[0]->s );
11190+
11191+
$result = $this->assertQuery( "SELECT SUBSTRING('abcdef' FROM 1 FOR 3) AS s" );
11192+
$this->assertSame( 'abc', $result[0]->s );
11193+
11194+
$result = $this->assertQuery( "SELECT SUBSTRING('abcdef' FROM 4) AS s" );
11195+
$this->assertSame( 'def', $result[0]->s );
11196+
}
11197+
1118411198
public function testAsdf(): void {
1118511199
$this->assertQuery( 'CREATE TABLE t (key1 INT, key2 INT)' );
1118611200
$this->assertQuery( 'CREATE UNIQUE INDEX idx_key1_key2 ON t (key1, key2)' );

wp-includes/sqlite-ast/class-wp-pdo-mysql-on-sqlite.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3496,6 +3496,22 @@ private function translate( $node ): ?string {
34963496
return $this->translate_runtime_function_call( $node );
34973497
case 'functionCall':
34983498
return $this->translate_function_call( $node );
3499+
case 'substringFunction':
3500+
$nodes = $node->get_child_nodes();
3501+
if ( count( $nodes ) === 2 ) {
3502+
return sprintf(
3503+
'SUBSTR(%s, %s)',
3504+
$this->translate( $nodes[0] ),
3505+
$this->translate( $nodes[1] )
3506+
);
3507+
} else {
3508+
return sprintf(
3509+
'SUBSTR(%s, %s, %s)',
3510+
$this->translate( $nodes[0] ),
3511+
$this->translate( $nodes[1] ),
3512+
$this->translate( $nodes[2] )
3513+
);
3514+
}
34993515
case 'systemVariable':
35003516
$var_ident_type = $node->get_first_child_node( 'varIdentType' );
35013517
$type_token = $var_ident_type ? $var_ident_type->get_first_child_token() : null;
@@ -4116,7 +4132,7 @@ private function translate_runtime_function_call( WP_Parser_Node $node ): string
41164132
case WP_MySQL_Lexer::LEFT_SYMBOL:
41174133
$nodes = $node->get_child_nodes();
41184134
return sprintf(
4119-
'SUBSTRING(%s, 1, %s)',
4135+
'SUBSTR(%s, 1, %s)',
41204136
$this->translate( $nodes[0] ),
41214137
$this->translate( $nodes[1] )
41224138
);

0 commit comments

Comments
 (0)