Skip to content

Commit d520800

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

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
@@ -11231,4 +11231,18 @@ public function testVersionFunction(): void {
1123111231
$result = $this->engine->query( 'SELECT VERSION()' );
1123211232
$this->assertSame( '8.0.38', $result[0]->{'VERSION()'} );
1123311233
}
11234+
11235+
public function testSubstringFunction(): void {
11236+
$result = $this->assertQuery( "SELECT SUBSTRING('abcdef', 1, 3) AS s" );
11237+
$this->assertSame( 'abc', $result[0]->s );
11238+
11239+
$result = $this->assertQuery( "SELECT SUBSTRING('abcdef', 4) AS s" );
11240+
$this->assertSame( 'def', $result[0]->s );
11241+
11242+
$result = $this->assertQuery( "SELECT SUBSTRING('abcdef' FROM 1 FOR 3) AS s" );
11243+
$this->assertSame( 'abc', $result[0]->s );
11244+
11245+
$result = $this->assertQuery( "SELECT SUBSTRING('abcdef' FROM 4) AS s" );
11246+
$this->assertSame( 'def', $result[0]->s );
11247+
}
1123411248
}

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)