Skip to content

Commit def2400

Browse files
authored
Implement INSERT and UPDATE value type casting (#276)
2 parents 13a43a8 + 2d1f6b6 commit def2400

File tree

5 files changed

+1535
-142
lines changed

5 files changed

+1535
-142
lines changed

tests/WP_SQLite_Driver_Query_Tests.php

Lines changed: 88 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,87 @@
22

33
use PHPUnit\Framework\TestCase;
44

5+
$tables = <<<'SQL'
6+
CREATE TABLE wp_users (
7+
ID bigint(20) unsigned NOT NULL auto_increment,
8+
user_login varchar(60) NOT NULL default '',
9+
user_pass varchar(255) NOT NULL default '',
10+
user_nicename varchar(50) NOT NULL default '',
11+
user_email varchar(100) NOT NULL default '',
12+
user_url varchar(100) NOT NULL default '',
13+
user_registered datetime NOT NULL default '0000-00-00 00:00:00',
14+
user_activation_key varchar(255) NOT NULL default '',
15+
user_status int(11) NOT NULL default '0',
16+
display_name varchar(250) NOT NULL default '',
17+
PRIMARY KEY (ID),
18+
KEY user_login_key (user_login),
19+
KEY user_nicename (user_nicename),
20+
KEY user_email (user_email)
21+
) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
22+
23+
CREATE TABLE wp_usermeta (
24+
umeta_id bigint(20) unsigned NOT NULL auto_increment,
25+
user_id bigint(20) unsigned NOT NULL default '0',
26+
meta_key varchar(255) default NULL,
27+
meta_value longtext,
28+
PRIMARY KEY (umeta_id),
29+
KEY user_id (user_id),
30+
KEY meta_key (meta_key(191))
31+
) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
32+
33+
CREATE TABLE wp_posts (
34+
ID bigint(20) unsigned NOT NULL auto_increment,
35+
post_author bigint(20) unsigned NOT NULL default '0',
36+
post_date datetime NOT NULL default '0000-00-00 00:00:00',
37+
post_date_gmt datetime NOT NULL default '0000-00-00 00:00:00',
38+
post_content longtext NOT NULL,
39+
post_title text NOT NULL,
40+
post_excerpt text NOT NULL,
41+
post_status varchar(20) NOT NULL default 'publish',
42+
comment_status varchar(20) NOT NULL default 'open',
43+
ping_status varchar(20) NOT NULL default 'open',
44+
post_password varchar(255) NOT NULL default '',
45+
post_name varchar(200) NOT NULL default '',
46+
to_ping text NOT NULL,
47+
pinged text NOT NULL,
48+
post_modified datetime NOT NULL default '0000-00-00 00:00:00',
49+
post_modified_gmt datetime NOT NULL default '0000-00-00 00:00:00',
50+
post_content_filtered longtext NOT NULL,
51+
post_parent bigint(20) unsigned NOT NULL default '0',
52+
guid varchar(255) NOT NULL default '',
53+
menu_order int(11) NOT NULL default '0',
54+
post_type varchar(20) NOT NULL default 'post',
55+
post_mime_type varchar(100) NOT NULL default '',
56+
comment_count bigint(20) NOT NULL default '0',
57+
PRIMARY KEY (ID),
58+
KEY post_name (post_name(191)),
59+
KEY type_status_date (post_type,post_status,post_date,ID),
60+
KEY post_parent (post_parent),
61+
KEY post_author (post_author),
62+
KEY type_status_author (post_type,post_status,post_author)
63+
) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
64+
65+
CREATE TABLE wp_postmeta (
66+
meta_id bigint(20) unsigned NOT NULL auto_increment,
67+
post_id bigint(20) unsigned NOT NULL default '0',
68+
meta_key varchar(255) default NULL,
69+
meta_value longtext,
70+
PRIMARY KEY (meta_id),
71+
KEY post_id (post_id),
72+
KEY meta_key (meta_key(191))
73+
) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
74+
75+
CREATE TABLE wp_options (
76+
option_id bigint(20) unsigned NOT NULL auto_increment,
77+
option_name varchar(191) NOT NULL default '',
78+
option_value longtext NOT NULL,
79+
autoload varchar(20) NOT NULL default 'yes',
80+
PRIMARY KEY (option_id),
81+
UNIQUE KEY option_name (option_name),
82+
KEY autoload (autoload)
83+
) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
84+
SQL;
85+
586
/**
687
* Unit tests using the WordPress table definitions.
788
*/
@@ -20,38 +101,31 @@ class WP_SQLite_Driver_Query_Tests extends TestCase {
20101
*/
21102
public function setUp(): void {
22103
/* This is the DDL for WordPress tables in SQLite syntax. */
23-
global $blog_tables;
24-
$queries = explode( ';', $blog_tables );
104+
global $tables;
105+
$queries = explode( ';', $tables );
25106

26107
$this->sqlite = new PDO( 'sqlite::memory:' );
27108
$this->engine = new WP_SQLite_Driver(
28109
new WP_SQLite_Connection( array( 'pdo' => $this->sqlite ) ),
29110
'wp'
30111
);
31112

32-
$translator = $this->engine;
33-
34113
try {
35-
$translator->begin_transaction();
114+
$this->engine->begin_transaction();
36115
foreach ( $queries as $query ) {
37116
$query = trim( $query );
38117
if ( empty( $query ) ) {
39118
continue;
40119
}
41-
42-
$translator->execute_sqlite_query( $query );
120+
$this->engine->query( $query );
43121
}
44-
$translator->commit();
45-
} catch ( PDOException $err ) {
46-
$err_data =
47-
$err->errorInfo; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
48-
$err_code = $err_data[1];
49-
$translator->rollback();
122+
$this->engine->commit();
123+
} catch ( Throwable $e ) {
50124
$message = sprintf(
51125
'Error occurred while creating tables or indexes...<br />Query was: %s<br />',
52126
var_export( $query, true )
53127
);
54-
$message .= sprintf( 'Error message is: %s', $err_data[2] );
128+
$message .= sprintf( 'Error message is: %s', $e->getMessage() );
55129
wp_die( $message, 'Database Error!' );
56130
}
57131

0 commit comments

Comments
 (0)