Repository navigation
Expand file tree
/
Copy pathclass-wp-sqlite-configurator.php
More file actions
273 lines (253 loc) · 9.66 KB
/
Copy pathclass-wp-sqlite-configurator.php
File metadata and controls
273 lines (253 loc) · 9.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
/**
* SQLite database configurator.
*
* This class initializes and configures the SQLite database, so that it can be
* used by the SQLite driver to translate and emulate MySQL queries in SQLite.
*
* The configurator ensures that tables required for emulating MySQL behaviors
* are created and populated with necessary data. It is also able to partially
* repair and update these tables and metadata in case of database corruption.
*/
class WP_SQLite_Configurator {
/**
* The SQLite driver instance.
*
* @var WP_MySQL_On_SQLite
*/
private $driver;
/**
* A service for managing MySQL INFORMATION_SCHEMA tables in SQLite.
*
* @var WP_SQLite_Information_Schema_Builder
*/
private $schema_builder;
/**
* A service for reconstructing the MySQL INFORMATION_SCHEMA tables in SQLite.
*
* @var WP_SQLite_Information_Schema_Reconstructor
*/
private $schema_reconstructor;
/**
* Constructor.
*
* @param WP_MySQL_On_SQLite $driver The SQLite driver instance.
* @param WP_SQLite_Information_Schema_Builder $schema_builder The information schema builder instance.
*/
public function __construct(
WP_MySQL_On_SQLite $driver,
WP_SQLite_Information_Schema_Builder $schema_builder
) {
$this->driver = $driver;
$this->schema_builder = $schema_builder;
$this->schema_reconstructor = new WP_SQLite_Information_Schema_Reconstructor(
$driver,
$schema_builder
);
}
/**
* Ensure that the SQLite database is configured.
*
* This method checks if the database is configured for the latest SQLite
* driver version, and if it is not, it will configure the database.
*/
public function ensure_database_configured(): void {
$version = SQLITE_DRIVER_VERSION;
$db_version = $this->driver->get_saved_driver_version();
if ( version_compare( $version, $db_version ) > 0 ) {
$this->configure_database();
}
}
/**
* Configure the SQLite database.
*
* This method creates tables used for emulating MySQL behaviors in SQLite,
* and populates them with necessary data. When it is used with an already
* configured database, it will update the configuration as per the current
* SQLite driver version and attempt to repair any configuration corruption.
*/
public function configure_database(): void {
// Use an EXCLUSIVE transaction to prevent multiple connections
// from attempting to configure the database at the same time.
$this->driver->execute_sqlite_query( 'BEGIN EXCLUSIVE TRANSACTION' );
try {
$this->ensure_global_variables_table();
$this->schema_builder->ensure_information_schema_tables();
$this->schema_reconstructor->ensure_correct_information_schema();
$this->save_current_driver_version();
$this->ensure_database_data();
} catch ( Throwable $e ) {
$this->driver->execute_sqlite_query( 'ROLLBACK' );
throw $e;
}
$this->driver->execute_sqlite_query( 'COMMIT' );
}
/**
* Ensure that the global variables table exists.
*
* This method configures a database table to store MySQL global variables
* and other internal configuration values.
*/
private function ensure_global_variables_table(): void {
$this->driver->execute_sqlite_query(
sprintf(
'CREATE TABLE IF NOT EXISTS %s (name TEXT PRIMARY KEY, value TEXT)',
$this->driver->get_connection()->quote_identifier(
WP_MySQL_On_SQLite::GLOBAL_VARIABLES_TABLE_NAME
)
)
);
}
/**
* Ensure that the database data is correctly populated.
*
* This method ensures that the "INFORMATION_SCHEMA.SCHEMATA" table contains
* records for both the "INFORMATION_SCHEMA" database and the user database.
* At the moment, only a single user database is supported.
*
* Additionally, this method ensures that the user database name is stored
* correctly in all the information schema tables.
*/
public function ensure_database_data(): void {
// Get all databases from the "SCHEMATA" table.
$schemata_table = $this->schema_builder->get_table_name( false, 'schemata' );
$databases = $this->driver->execute_sqlite_query(
sprintf(
'SELECT SCHEMA_NAME FROM %s',
$this->driver->get_connection()->quote_identifier( $schemata_table )
)
)->fetchAll( PDO::FETCH_COLUMN ); // phpcs:disable WordPress.DB.RestrictedClasses.mysql__PDO
// Ensure that the "INFORMATION_SCHEMA" database record exists.
if ( ! in_array( 'information_schema', $databases, true ) ) {
$this->driver->execute_sqlite_query(
sprintf(
'INSERT INTO %s (SCHEMA_NAME, DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME) VALUES (?, ?, ?)',
$this->driver->get_connection()->quote_identifier( $schemata_table )
),
// The "INFORMATION_SCHEMA" database stays on "utf8mb3" even in MySQL 8 and 9.
array( 'information_schema', 'utf8mb3', 'utf8mb3_general_ci' )
);
}
// Get the existing user database name.
$existing_user_db_name = null;
foreach ( $databases as $database ) {
if ( 'information_schema' !== strtolower( $database ) ) {
$existing_user_db_name = $database;
break;
}
}
// Ensure that the user database record exists.
if ( null === $existing_user_db_name ) {
$existing_user_db_name = WP_SQLite_Information_Schema_Builder::SAVED_DATABASE_NAME;
$this->driver->execute_sqlite_query(
sprintf(
'INSERT INTO %s (SCHEMA_NAME, DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME) VALUES (?, ?, ?)',
$this->driver->get_connection()->quote_identifier( $schemata_table )
),
// @TODO: This should probably be version-dependent.
// Before MySQL 8, the default was different.
array( $existing_user_db_name, 'utf8mb4', 'utf8mb4_0900_ai_ci' )
);
}
// Migrate from older versions without dynamic database names.
$saved_database_name = WP_SQLite_Information_Schema_Builder::SAVED_DATABASE_NAME;
if ( $saved_database_name !== $existing_user_db_name ) {
// INFORMATION_SCHEMA.SCHEMATA
$this->driver->execute_sqlite_query(
sprintf(
"UPDATE %s SET SCHEMA_NAME = ? WHERE SCHEMA_NAME != 'information_schema'",
$this->driver->get_connection()->quote_identifier( $schemata_table )
),
array( $saved_database_name )
);
// INFORMATION_SCHEMA.TABLES
$tables_table = $this->schema_builder->get_table_name( false, 'tables' );
$this->driver->execute_sqlite_query(
sprintf(
"UPDATE %s SET TABLE_SCHEMA = ? WHERE TABLE_SCHEMA != 'information_schema'",
$this->driver->get_connection()->quote_identifier( $tables_table )
),
array( $saved_database_name )
);
// INFORMATION_SCHEMA.COLUMNS
$columns_table = $this->schema_builder->get_table_name( false, 'columns' );
$this->driver->execute_sqlite_query(
sprintf(
"UPDATE %s SET TABLE_SCHEMA = ? WHERE TABLE_SCHEMA != 'information_schema'",
$this->driver->get_connection()->quote_identifier( $columns_table )
),
array( $saved_database_name )
);
// INFORMATION_SCHEMA.STATISTICS
$statistics_table = $this->schema_builder->get_table_name( false, 'statistics' );
$this->driver->execute_sqlite_query(
sprintf(
"UPDATE %s SET TABLE_SCHEMA = ?, INDEX_SCHEMA = ? WHERE TABLE_SCHEMA != 'information_schema'",
$this->driver->get_connection()->quote_identifier( $statistics_table )
),
array( $saved_database_name, $saved_database_name )
);
// INFORMATION_SCHEMA.TABLE_CONSTRAINTS
$table_constraints_table = $this->schema_builder->get_table_name( false, 'table_constraints' );
$this->driver->execute_sqlite_query(
sprintf(
"UPDATE %s SET TABLE_SCHEMA = ?, CONSTRAINT_SCHEMA = ? WHERE TABLE_SCHEMA != 'information_schema'",
$this->driver->get_connection()->quote_identifier( $table_constraints_table )
),
array( $saved_database_name, $saved_database_name )
);
// INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
$referential_constraints_table = $this->schema_builder->get_table_name( false, 'referential_constraints' );
$this->driver->execute_sqlite_query(
sprintf(
"UPDATE %s SET CONSTRAINT_SCHEMA = ?, UNIQUE_CONSTRAINT_SCHEMA = ? WHERE CONSTRAINT_SCHEMA != 'information_schema'",
$this->driver->get_connection()->quote_identifier( $referential_constraints_table )
),
array( $saved_database_name, $saved_database_name )
);
// INFORMATION_SCHEMA.KEY_COLUMN_USAGE
$key_column_usage_table = $this->schema_builder->get_table_name( false, 'key_column_usage' );
$this->driver->execute_sqlite_query(
sprintf(
"UPDATE %s
SET
TABLE_SCHEMA = ?,
CONSTRAINT_SCHEMA = ?,
REFERENCED_TABLE_SCHEMA = CASE WHEN REFERENCED_TABLE_SCHEMA IS NULL THEN NULL ELSE ? END
WHERE TABLE_SCHEMA != 'information_schema'",
$this->driver->get_connection()->quote_identifier( $key_column_usage_table )
),
array( $saved_database_name, $saved_database_name, $saved_database_name )
);
// INFORMATION_SCHEMA.CHECK_CONSTRAINTS
$check_constraints_table = $this->schema_builder->get_table_name( false, 'check_constraints' );
$this->driver->execute_sqlite_query(
sprintf(
"UPDATE %s SET CONSTRAINT_SCHEMA = ? WHERE CONSTRAINT_SCHEMA != 'information_schema'",
$this->driver->get_connection()->quote_identifier( $check_constraints_table )
),
array( $saved_database_name )
);
}
}
/**
* Save the current SQLite driver version.
*
* This method saves the current SQLite driver version to the database.
*/
private function save_current_driver_version(): void {
$this->driver->execute_sqlite_query(
sprintf(
'INSERT INTO %s (name, value) VALUES (?, ?) ON CONFLICT(name) DO UPDATE SET value = ?',
$this->driver->get_connection()->quote_identifier(
WP_MySQL_On_SQLite::GLOBAL_VARIABLES_TABLE_NAME
)
),
array(
WP_MySQL_On_SQLite::DRIVER_VERSION_VARIABLE_NAME,
SQLITE_DRIVER_VERSION,
SQLITE_DRIVER_VERSION,
)
);
}
}