@@ -577,7 +577,10 @@ public function get_saved_driver_version(): string {
577
577
$ default_version = '0.0.0 ' ;
578
578
try {
579
579
$ stmt = $ this ->execute_sqlite_query (
580
- sprintf ( 'SELECT value FROM %s WHERE name = ? ' , self ::GLOBAL_VARIABLES_TABLE_NAME ),
580
+ sprintf (
581
+ 'SELECT value FROM %s WHERE name = ? ' ,
582
+ $ this ->quote_sqlite_identifier ( self ::GLOBAL_VARIABLES_TABLE_NAME )
583
+ ),
581
584
array ( self ::DRIVER_VERSION_VARIABLE_NAME )
582
585
);
583
586
return $ stmt ->fetchColumn () ?? $ default_version ;
@@ -1212,7 +1215,11 @@ private function execute_delete_statement( WP_Parser_Node $node ): void {
1212
1215
1213
1216
$ select_list = array ();
1214
1217
foreach ( $ table_aliases as $ table ) {
1215
- $ select_list [] = "\"$ table \".rowid AS \"{$ table }_rowid \"" ;
1218
+ $ select_list [] = sprintf (
1219
+ '%s.rowid AS %s ' ,
1220
+ $ this ->quote_sqlite_identifier ( $ table ),
1221
+ $ this ->quote_sqlite_identifier ( $ table . '_rowid ' )
1222
+ );
1216
1223
}
1217
1224
1218
1225
$ ids = $ this ->execute_sqlite_query (
@@ -1288,7 +1295,10 @@ private function execute_create_table_statement( WP_Parser_Node $node ): void {
1288
1295
if ( $ subnode ->has_child_node ( 'ifNotExists ' ) ) {
1289
1296
$ tables_table = $ this ->information_schema_builder ->get_table_name ( $ table_is_temporary , 'tables ' );
1290
1297
$ table_exists = $ this ->execute_sqlite_query (
1291
- "SELECT 1 FROM $ tables_table WHERE table_schema = ? AND table_name = ? " ,
1298
+ sprintf (
1299
+ 'SELECT 1 FROM %s WHERE table_schema = ? AND table_name = ? ' ,
1300
+ $ this ->quote_sqlite_identifier ( $ tables_table )
1301
+ ),
1292
1302
array ( $ this ->db_name , $ table_name )
1293
1303
)->fetchColumn ();
1294
1304
@@ -1329,7 +1339,10 @@ private function execute_alter_table_statement( WP_Parser_Node $node ): void {
1329
1339
// Save all column names from the original table.
1330
1340
$ columns_table = $ this ->information_schema_builder ->get_table_name ( $ table_is_temporary , 'columns ' );
1331
1341
$ column_names = $ this ->execute_sqlite_query (
1332
- "SELECT COLUMN_NAME FROM $ columns_table WHERE table_schema = ? AND table_name = ? " ,
1342
+ sprintf (
1343
+ 'SELECT COLUMN_NAME FROM %s WHERE table_schema = ? AND table_name = ? ' ,
1344
+ $ this ->quote_sqlite_identifier ( $ columns_table )
1345
+ ),
1333
1346
array ( $ this ->db_name , $ table_name )
1334
1347
)->fetchAll ( PDO ::FETCH_COLUMN );
1335
1348
@@ -1437,9 +1450,9 @@ private function execute_truncate_table_statement( WP_Parser_Node $node ): void
1437
1450
$ this ->translate ( $ node ->get_first_child_node ( 'tableRef ' ) )
1438
1451
);
1439
1452
1440
- $ quoted_table_name = $ this ->quote_sqlite_identifier ( $ table_name );
1441
-
1442
- $ this -> execute_sqlite_query ( " DELETE FROM $ quoted_table_name " );
1453
+ $ this ->execute_sqlite_query (
1454
+ sprintf ( ' DELETE FROM %s ' , $ this -> quote_sqlite_identifier ( $ table_name ) )
1455
+ );
1443
1456
try {
1444
1457
$ this ->execute_sqlite_query ( 'DELETE FROM sqlite_sequence WHERE name = ? ' , array ( $ table_name ) );
1445
1458
} catch ( PDOException $ e ) {
@@ -1576,7 +1589,7 @@ private function execute_show_index_statement( string $table_name ): void {
1576
1589
INDEX_COMMENT AS `Index_comment`,
1577
1590
IS_VISIBLE AS `Visible`,
1578
1591
EXPRESSION AS `Expression`
1579
- FROM ' . $ statistics_table . '
1592
+ FROM ' . $ this -> quote_sqlite_identifier ( $ statistics_table ) . '
1580
1593
WHERE table_schema = ?
1581
1594
AND table_name = ?
1582
1595
ORDER BY
@@ -1620,7 +1633,8 @@ private function execute_show_table_status_statement( WP_Parser_Node $node ): vo
1620
1633
);
1621
1634
$ table_info = $ this ->execute_sqlite_query (
1622
1635
sprintf (
1623
- "SELECT * FROM $ tables_tables WHERE table_schema = ? %s " ,
1636
+ 'SELECT * FROM %s WHERE table_schema = ? %s ' ,
1637
+ $ this ->quote_sqlite_identifier ( $ tables_tables ),
1624
1638
$ condition ?? ''
1625
1639
),
1626
1640
array ( $ database )
@@ -1688,7 +1702,8 @@ private function execute_show_tables_statement( WP_Parser_Node $node ): void {
1688
1702
);
1689
1703
$ table_info = $ this ->execute_sqlite_query (
1690
1704
sprintf (
1691
- "SELECT * FROM $ table_tables WHERE table_schema = ? %s " ,
1705
+ 'SELECT * FROM %s WHERE table_schema = ? %s ' ,
1706
+ $ this ->quote_sqlite_identifier ( $ table_tables ),
1692
1707
$ condition ?? ''
1693
1708
),
1694
1709
array ( $ database )
@@ -1745,7 +1760,10 @@ private function execute_show_columns_statement( WP_Parser_Node $node ): void {
1745
1760
// Check if the table exists.
1746
1761
$ tables_tables = $ this ->information_schema_builder ->get_table_name ( $ table_is_temporary , 'tables ' );
1747
1762
$ table_exists = $ this ->execute_sqlite_query (
1748
- "SELECT 1 FROM $ tables_tables WHERE table_schema = ? AND table_name = ? " ,
1763
+ sprintf (
1764
+ 'SELECT 1 FROM %s WHERE table_schema = ? AND table_name = ? ' ,
1765
+ $ this ->quote_sqlite_identifier ( $ tables_tables )
1766
+ ),
1749
1767
array ( $ this ->db_name , $ table_name )
1750
1768
)->fetchColumn ();
1751
1769
@@ -1763,10 +1781,11 @@ private function execute_show_columns_statement( WP_Parser_Node $node ): void {
1763
1781
}
1764
1782
1765
1783
// Fetch column information.
1766
- $ column_info = $ this ->execute_sqlite_query (
1784
+ $ columns_table = $ this ->information_schema_builder ->get_table_name ( $ table_is_temporary , 'columns ' );
1785
+ $ column_info = $ this ->execute_sqlite_query (
1767
1786
sprintf (
1768
1787
'SELECT * FROM %s WHERE table_schema = ? AND table_name = ? %s ' ,
1769
- $ this ->information_schema_builder -> get_table_name ( $ table_is_temporary , ' columns ' ),
1788
+ $ this ->quote_sqlite_identifier ( $ columns_table ),
1770
1789
$ condition ?? ''
1771
1790
),
1772
1791
array ( $ database , $ table_name )
@@ -1808,18 +1827,18 @@ private function execute_describe_statement( WP_Parser_Node $node ): void {
1808
1827
1809
1828
$ columns_table = $ this ->information_schema_builder ->get_table_name ( $ table_is_temporary , 'columns ' );
1810
1829
$ column_info = $ this ->execute_sqlite_query (
1811
- "
1830
+ '
1812
1831
SELECT
1813
1832
column_name AS `Field`,
1814
1833
column_type AS `Type`,
1815
1834
is_nullable AS `Null`,
1816
1835
column_key AS `Key`,
1817
1836
column_default AS `Default`,
1818
1837
extra AS Extra
1819
- FROM $ columns_table
1838
+ FROM ' . $ this -> quote_sqlite_identifier ( $ columns_table ) . '
1820
1839
WHERE table_schema = ?
1821
1840
AND table_name = ?
1822
- " ,
1841
+ ' ,
1823
1842
array ( $ this ->db_name , $ table_name )
1824
1843
)->fetchAll ( PDO ::FETCH_OBJ );
1825
1844
@@ -2015,11 +2034,13 @@ private function execute_administration_statement( WP_Parser_Node $node ): void
2015
2034
try {
2016
2035
switch ( $ first_token ->id ) {
2017
2036
case WP_MySQL_Lexer::ANALYZE_SYMBOL :
2018
- $ stmt = $ this ->execute_sqlite_query ( " ANALYZE $ quoted_table_name" );
2037
+ $ stmt = $ this ->execute_sqlite_query ( sprintf ( ' ANALYZE %s ' , $ quoted_table_name ) );
2019
2038
$ errors = $ stmt ->fetchAll ( PDO ::FETCH_COLUMN );
2020
2039
break ;
2021
2040
case WP_MySQL_Lexer::CHECK_SYMBOL :
2022
- $ stmt = $ this ->execute_sqlite_query ( "PRAGMA integrity_check( $ quoted_table_name) " );
2041
+ $ stmt = $ this ->execute_sqlite_query (
2042
+ sprintf ( 'PRAGMA integrity_check(%s) ' , $ quoted_table_name )
2043
+ );
2023
2044
$ errors = $ stmt ->fetchAll ( PDO ::FETCH_COLUMN );
2024
2045
if ( 'ok ' === $ errors [0 ] ) {
2025
2046
array_shift ( $ errors );
@@ -2842,7 +2863,10 @@ private function recreate_table_from_information_schema(
2842
2863
if ( null === $ column_map ) {
2843
2864
$ columns_table = $ this ->information_schema_builder ->get_table_name ( $ table_is_temporary , 'columns ' );
2844
2865
$ column_names = $ this ->execute_sqlite_query (
2845
- "SELECT COLUMN_NAME FROM $ columns_table WHERE table_schema = ? AND table_name = ? " ,
2866
+ sprintf (
2867
+ 'SELECT COLUMN_NAME FROM %s WHERE table_schema = ? AND table_name = ? ' ,
2868
+ $ this ->quote_sqlite_identifier ( $ columns_table )
2869
+ ),
2846
2870
array ( $ this ->db_name , $ table_name )
2847
2871
)->fetchAll ( PDO ::FETCH_COLUMN );
2848
2872
$ column_map = array_combine ( $ column_names , $ column_names );
@@ -2997,13 +3021,13 @@ private function translate_insert_or_replace_body_in_non_strict_mode(
2997
3021
$ is_temporary = $ this ->information_schema_builder ->temporary_table_exists ( $ table_name );
2998
3022
$ columns_table = $ this ->information_schema_builder ->get_table_name ( $ is_temporary , 'columns ' );
2999
3023
$ columns = $ this ->execute_sqlite_query (
3000
- "
3024
+ '
3001
3025
SELECT column_name, is_nullable, column_default, data_type, extra
3002
- FROM $ columns_table
3026
+ FROM ' . $ this -> quote_sqlite_identifier ( $ columns_table ) . '
3003
3027
WHERE table_schema = ?
3004
3028
AND table_name = ?
3005
3029
ORDER BY ordinal_position
3006
- " ,
3030
+ ' ,
3007
3031
array ( $ this ->db_name , $ table_name )
3008
3032
)->fetchAll ( PDO ::FETCH_ASSOC );
3009
3033
@@ -3113,12 +3137,12 @@ private function translate_update_list_in_non_strict_mode( string $table_name, W
3113
3137
$ is_temporary = $ this ->information_schema_builder ->temporary_table_exists ( $ table_name );
3114
3138
$ columns_table = $ this ->information_schema_builder ->get_table_name ( $ is_temporary , 'columns ' );
3115
3139
$ columns = $ this ->execute_sqlite_query (
3116
- "
3140
+ '
3117
3141
SELECT column_name, is_nullable, data_type, column_default
3118
- FROM $ columns_table
3142
+ FROM ' . $ this -> quote_sqlite_identifier ( $ columns_table ) . '
3119
3143
WHERE table_schema = ?
3120
3144
AND table_name = ?
3121
- " ,
3145
+ ' ,
3122
3146
array ( $ this ->db_name , $ table_name )
3123
3147
)->fetchAll ( PDO ::FETCH_ASSOC );
3124
3148
$ column_map = array_combine ( array_column ( $ columns , 'COLUMN_NAME ' ), $ columns );
@@ -3176,9 +3200,9 @@ private function get_sqlite_create_table_statement(
3176
3200
// 1. Get table info.
3177
3201
$ tables_table = $ this ->information_schema_builder ->get_table_name ( $ table_is_temporary , 'tables ' );
3178
3202
$ table_info = $ this ->execute_sqlite_query (
3179
- "
3203
+ '
3180
3204
SELECT *
3181
- FROM $ tables_table
3205
+ FROM ' . $ this -> quote_sqlite_identifier ( $ tables_table ) . "
3182
3206
WHERE table_type = 'BASE TABLE'
3183
3207
AND table_schema = ?
3184
3208
AND table_name = ?
@@ -3196,14 +3220,20 @@ private function get_sqlite_create_table_statement(
3196
3220
// 2. Get column info.
3197
3221
$ columns_table = $ this ->information_schema_builder ->get_table_name ( $ table_is_temporary , 'columns ' );
3198
3222
$ column_info = $ this ->execute_sqlite_query (
3199
- "SELECT * FROM $ columns_table WHERE table_schema = ? AND table_name = ? " ,
3223
+ sprintf (
3224
+ 'SELECT * FROM %s WHERE table_schema = ? AND table_name = ? ' ,
3225
+ $ this ->quote_sqlite_identifier ( $ columns_table )
3226
+ ),
3200
3227
array ( $ this ->db_name , $ table_name )
3201
3228
)->fetchAll ( PDO ::FETCH_ASSOC );
3202
3229
3203
3230
// 3. Get index info, grouped by index name.
3204
3231
$ statistics_table = $ this ->information_schema_builder ->get_table_name ( $ table_is_temporary , 'statistics ' );
3205
3232
$ constraint_info = $ this ->execute_sqlite_query (
3206
- "SELECT * FROM $ statistics_table WHERE table_schema = ? AND table_name = ? " ,
3233
+ sprintf (
3234
+ 'SELECT * FROM %s WHERE table_schema = ? AND table_name = ? ' ,
3235
+ $ this ->quote_sqlite_identifier ( $ statistics_table )
3236
+ ),
3207
3237
array ( $ this ->db_name , $ table_name )
3208
3238
)->fetchAll ( PDO ::FETCH_ASSOC );
3209
3239
@@ -3366,9 +3396,9 @@ private function get_mysql_create_table_statement( bool $table_is_temporary, str
3366
3396
// 1. Get table info.
3367
3397
$ tables_table = $ this ->information_schema_builder ->get_table_name ( $ table_is_temporary , 'tables ' );
3368
3398
$ table_info = $ this ->execute_sqlite_query (
3369
- "
3399
+ '
3370
3400
SELECT *
3371
- FROM $ tables_table
3401
+ FROM ' . $ this -> quote_sqlite_identifier ( $ tables_table ) . "
3372
3402
WHERE table_type = 'BASE TABLE'
3373
3403
AND table_schema = ?
3374
3404
AND table_name = ?
@@ -3383,14 +3413,20 @@ private function get_mysql_create_table_statement( bool $table_is_temporary, str
3383
3413
// 2. Get column info.
3384
3414
$ columns_table = $ this ->information_schema_builder ->get_table_name ( $ table_is_temporary , 'columns ' );
3385
3415
$ column_info = $ this ->execute_sqlite_query (
3386
- "SELECT * FROM $ columns_table WHERE table_schema = ? AND table_name = ? " ,
3416
+ sprintf (
3417
+ 'SELECT * FROM %s WHERE table_schema = ? AND table_name = ? ' ,
3418
+ $ this ->quote_sqlite_identifier ( $ columns_table )
3419
+ ),
3387
3420
array ( $ this ->db_name , $ table_name )
3388
3421
)->fetchAll ( PDO ::FETCH_ASSOC );
3389
3422
3390
3423
// 3. Get index info, grouped by index name.
3391
3424
$ statistics_table = $ this ->information_schema_builder ->get_table_name ( $ table_is_temporary , 'statistics ' );
3392
3425
$ constraint_info = $ this ->execute_sqlite_query (
3393
- "SELECT * FROM $ statistics_table WHERE table_schema = ? AND table_name = ? " ,
3426
+ sprintf (
3427
+ 'SELECT * FROM %s WHERE table_schema = ? AND table_name = ? ' ,
3428
+ $ this ->quote_sqlite_identifier ( $ statistics_table )
3429
+ ),
3394
3430
array ( $ this ->db_name , $ table_name )
3395
3431
)->fetchAll ( PDO ::FETCH_ASSOC );
3396
3432
@@ -3518,14 +3554,20 @@ private function get_column_on_update_trigger_query( string $table, string $colu
3518
3554
// but currently that can't happen as we're not creating such tables.
3519
3555
// See: https://www.sqlite.org/rowidtable.html
3520
3556
$ trigger_name = self ::RESERVED_PREFIX . "{$ table }_ {$ column }_on_update " ;
3521
- return "
3522
- CREATE TRIGGER \"$ trigger_name \"
3523
- AFTER UPDATE ON \"$ table \"
3524
- FOR EACH ROW
3525
- BEGIN
3526
- UPDATE \"$ table \" SET \"$ column \" = CURRENT_TIMESTAMP WHERE rowid = NEW.rowid;
3527
- END
3528
- " ;
3557
+ return sprintf (
3558
+ '
3559
+ CREATE TRIGGER %s
3560
+ AFTER UPDATE ON %s
3561
+ FOR EACH ROW
3562
+ BEGIN
3563
+ UPDATE %s SET %s = CURRENT_TIMESTAMP WHERE rowid = NEW.rowid;
3564
+ END
3565
+ ' ,
3566
+ $ this ->quote_sqlite_identifier ( $ trigger_name ),
3567
+ $ this ->quote_sqlite_identifier ( $ table ),
3568
+ $ this ->quote_sqlite_identifier ( $ table ),
3569
+ $ this ->quote_sqlite_identifier ( $ column )
3570
+ );
3529
3571
}
3530
3572
3531
3573
/**
0 commit comments