@@ -23,6 +23,13 @@ class WP_SQLite_Information_Schema_Reconstructor {
23
23
*/
24
24
private $ driver ;
25
25
26
+ /**
27
+ * An instance of the SQLite connection.
28
+ *
29
+ * @var WP_SQLite_Connection
30
+ */
31
+ private $ connection ;
32
+
26
33
/**
27
34
* A service for managing MySQL INFORMATION_SCHEMA tables in SQLite.
28
35
*
@@ -41,6 +48,7 @@ public function __construct(
41
48
WP_SQLite_Information_Schema_Builder $ schema_builder
42
49
) {
43
50
$ this ->driver = $ driver ;
51
+ $ this ->connection = $ driver ->get_connection ();
44
52
$ this ->schema_builder = $ schema_builder ;
45
53
}
46
54
@@ -78,7 +86,7 @@ public function ensure_correct_information_schema(): void {
78
86
// Remove information schema records for tables that don't exist.
79
87
foreach ( $ information_schema_tables as $ table ) {
80
88
if ( ! in_array ( $ table , $ sqlite_tables , true ) ) {
81
- $ sql = sprintf ( 'DROP TABLE %s ' , $ this ->quote_sqlite_identifier ( $ table ) );
89
+ $ sql = sprintf ( 'DROP TABLE %s ' , $ this ->connection -> quote_identifier ( $ table ) ); // TODO: mysql quote
82
90
$ ast = $ this ->driver ->create_parser ( $ sql )->parse ();
83
91
if ( null === $ ast ) {
84
92
throw new WP_SQLite_Driver_Exception ( $ this ->driver , 'Failed to parse the MySQL query. ' );
@@ -124,7 +132,7 @@ private function get_information_schema_table_names(): array {
124
132
return $ this ->driver ->execute_sqlite_query (
125
133
sprintf (
126
134
'SELECT table_name FROM %s ORDER BY table_name ' ,
127
- $ this ->quote_sqlite_identifier ( $ tables_table )
135
+ $ this ->connection -> quote_identifier ( $ tables_table )
128
136
)
129
137
)->fetchAll ( PDO ::FETCH_COLUMN );
130
138
}
@@ -212,7 +220,7 @@ private function generate_create_table_statement( string $table_name ): string {
212
220
$ columns = $ this ->driver ->execute_sqlite_query (
213
221
sprintf (
214
222
'PRAGMA table_xinfo(%s) ' ,
215
- $ this ->quote_sqlite_identifier ( $ table_name )
223
+ $ this ->connection -> quote_identifier ( $ table_name )
216
224
)
217
225
)->fetchAll ( PDO ::FETCH_ASSOC );
218
226
@@ -244,7 +252,7 @@ private function generate_create_table_statement( string $table_name ): string {
244
252
if ( count ( $ pk_columns ) > 0 ) {
245
253
$ quoted_pk_columns = array ();
246
254
foreach ( $ pk_columns as $ pk_column ) {
247
- $ quoted_pk_columns [] = $ this ->quote_sqlite_identifier ( $ pk_column );
255
+ $ quoted_pk_columns [] = $ this ->connection -> quote_identifier ( $ pk_column );
248
256
}
249
257
$ definitions [] = sprintf ( 'PRIMARY KEY (%s) ' , implode ( ', ' , $ quoted_pk_columns ) );
250
258
}
@@ -253,7 +261,7 @@ private function generate_create_table_statement( string $table_name ): string {
253
261
$ keys = $ this ->driver ->execute_sqlite_query (
254
262
sprintf (
255
263
'PRAGMA index_list(%s) ' ,
256
- $ this ->quote_sqlite_identifier ( $ table_name )
264
+ $ this ->connection -> quote_identifier ( $ table_name )
257
265
)
258
266
)->fetchAll ( PDO ::FETCH_ASSOC );
259
267
@@ -268,7 +276,7 @@ private function generate_create_table_statement( string $table_name ): string {
268
276
269
277
return sprintf (
270
278
"CREATE TABLE %s ( \n %s \n) " ,
271
- $ this ->quote_sqlite_identifier ( $ table_name ),
279
+ $ this ->connection -> quote_identifier ( $ table_name ),
272
280
implode ( ", \n " , $ definitions )
273
281
);
274
282
}
@@ -284,7 +292,7 @@ private function generate_create_table_statement( string $table_name ): string {
284
292
*/
285
293
private function generate_column_definition ( string $ table_name , array $ column_info ): string {
286
294
$ definition = array ();
287
- $ definition [] = $ this ->quote_sqlite_identifier ( $ column_info ['name ' ] );
295
+ $ definition [] = $ this ->connection -> quote_identifier ( $ column_info ['name ' ] );
288
296
289
297
// Data type.
290
298
$ mysql_type = $ this ->get_cached_mysql_data_type ( $ table_name , $ column_info ['name ' ] );
@@ -379,14 +387,14 @@ private function generate_key_definition( string $table_name, array $key_info, a
379
387
* the generated MySQL definition, they follow implicit MySQL naming.
380
388
*/
381
389
if ( ! str_starts_with ( $ name , 'sqlite_autoindex_ ' ) ) {
382
- $ definition [] = $ this ->quote_sqlite_identifier ( $ name );
390
+ $ definition [] = $ this ->connection -> quote_identifier ( $ name );
383
391
}
384
392
385
393
// Key columns.
386
394
$ key_columns = $ this ->driver ->execute_sqlite_query (
387
395
sprintf (
388
396
'PRAGMA index_info(%s) ' ,
389
- $ this ->quote_sqlite_identifier ( $ key_info ['name ' ] )
397
+ $ this ->connection -> quote_identifier ( $ key_info ['name ' ] )
390
398
)
391
399
)->fetchAll ( PDO ::FETCH_ASSOC );
392
400
$ cols = array ();
@@ -423,11 +431,11 @@ private function generate_key_definition( string $table_name, array $key_info, a
423
431
) {
424
432
$ cols [] = sprintf (
425
433
'%s(%d) ' ,
426
- $ this ->quote_sqlite_identifier ( $ column ['name ' ] ),
434
+ $ this ->connection -> quote_identifier ( $ column ['name ' ] ),
427
435
min ( $ column_length ?? $ max_prefix_length , $ max_prefix_length )
428
436
);
429
437
} else {
430
- $ cols [] = $ this ->quote_sqlite_identifier ( $ column ['name ' ] );
438
+ $ cols [] = $ this ->connection -> quote_identifier ( $ column ['name ' ] );
431
439
}
432
440
}
433
441
@@ -640,18 +648,6 @@ private function escape_mysql_string_literal( string $literal ): string {
640
648
return "' " . addcslashes ( $ literal , "\0\n\r' \"\Z " ) . "' " ;
641
649
}
642
650
643
- /**
644
- * Quote an SQLite identifier.
645
- *
646
- * Wrap the identifier in backticks and escape backtick values within.
647
- *
648
- * @param string $unquoted_identifier The unquoted identifier value.
649
- * @return string The quoted identifier value.
650
- */
651
- private function quote_sqlite_identifier ( string $ unquoted_identifier ): string {
652
- return '` ' . str_replace ( '` ' , '`` ' , $ unquoted_identifier ) . '` ' ;
653
- }
654
-
655
651
/**
656
652
* Unquote a quoted MySQL identifier.
657
653
*
0 commit comments