Skip to content

Commit b78d0ad

Browse files
committed
Add basic support for CHAR_LENGTH
1 parent 118c57a commit b78d0ad

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

tests/WP_SQLite_Driver_Tests.php

+59
Original file line numberDiff line numberDiff line change
@@ -3598,4 +3598,63 @@ public function testLastInsertId(): void {
35983598
$this->assertQuery( "INSERT INTO t (name) VALUES ('b')" );
35993599
$this->assertEquals( 2, $this->engine->get_insert_id() );
36003600
}
3601+
3602+
public function testCharLength(): void {
3603+
$this->assertQuery(
3604+
'CREATE TABLE t (
3605+
ID INTEGER PRIMARY KEY AUTO_INCREMENT,
3606+
name VARCHAR(20)
3607+
);'
3608+
);
3609+
3610+
$this->assertQuery( "INSERT INTO t (name) VALUES ('a')" );
3611+
$this->assertQuery( "INSERT INTO t (name) VALUES ('ab')" );
3612+
$this->assertQuery( "INSERT INTO t (name) VALUES ('abc')" );
3613+
3614+
$this->assertQuery( 'SELECT CHAR_LENGTH(name) AS len FROM t' );
3615+
$this->assertEquals(
3616+
array(
3617+
(object) array( 'len' => '1' ),
3618+
(object) array( 'len' => '2' ),
3619+
(object) array( 'len' => '3' ),
3620+
),
3621+
$this->engine->get_query_results()
3622+
);
3623+
}
3624+
3625+
public function _testAsdf() {
3626+
$this->assertQuery(
3627+
'CREATE TABLE t (id INT)'
3628+
);
3629+
$this->assertQuery(
3630+
'INSERT INTO t (id) VALUES (1)'
3631+
);
3632+
$this->assertQuery(
3633+
'SELECT non_existent_column FROM t LIMIT 0'
3634+
);
3635+
var_dump( $this->engine->executed_sqlite_queries );
3636+
var_dump( $this->engine->get_error_message() );
3637+
$this->assertCount( 1, $this->engine->get_query_results() );
3638+
}
3639+
3640+
public function _testXyz() {
3641+
$this->assertQuery(
3642+
"INSERT INTO wp_actionscheduler_actions ( `hook`, `status`, `scheduled_date_gmt`, `scheduled_date_local`, `schedule`, `group_id`, `priority`, `args` ) SELECT 'action_scheduler/migration_hook', 'pending', '2025-01-28 15:14:01', '2025-01-28 15:14:01', 'O:30:\"ActionScheduler_SimpleSchedule\":2:{s:22:\"\0*\0scheduled_timestamp\";i:1738077241;s:41:\"\0ActionScheduler_SimpleSchedule\0timestamp\";i:1738077241;}', 2, 10, '[]' FROM DUAL WHERE ( SELECT NULL FROM DUAL ) IS NULL"
3643+
);
3644+
}
3645+
3646+
public function testAaa() {
3647+
$this->assertQuery(
3648+
'
3649+
CREATE TABLE t (
3650+
id int(11) unsigned NOT NULL AUTO_INCREMENT,
3651+
name varchar(90) NOT NULL,
3652+
type varchar(90) NOT NULL DEFAULT \'default\',
3653+
description varchar(250) NOT NULL DEFAULT \'\',
3654+
created_at timestamp NULL,
3655+
updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
3656+
)
3657+
'
3658+
);
3659+
}
36013660
}

wp-includes/sqlite-ast/class-wp-sqlite-driver.php

+3
Original file line numberDiff line numberDiff line change
@@ -1999,6 +1999,9 @@ private function translate_function_call( WP_Parser_Node $node ): string {
19991999
return sprintf( 'CAST(STRFTIME(%s, %s) AS FLOAT)', $format, $date );
20002000
}
20012001
return sprintf( 'STRFTIME(%s, %s)', $format, $date );
2002+
case 'CHAR_LENGTH':
2003+
// @TODO LENGTH and CHAR_LENGTH aren't always the same in MySQL for utf8 characters.
2004+
return 'LENGTH(' . $args[0] . ')';
20022005
case 'CONCAT':
20032006
return '(' . implode( ' || ', $args ) . ')';
20042007
case 'FOUND_ROWS':

0 commit comments

Comments
 (0)