PluginProbe
SQLite Database Integration / 2.2.3
SQLite Database Integration v2.2.3
3.0.2 3.0.1 trunk 2.1.13 2.1.14 2.1.15 2.1.16 2.2.0 2.2.1 2.2.10 2.2.11 2.2.12 2.2.13 2.2.14 2.2.15 2.2.16 2.2.17 2.2.18 2.2.19 2.2.2 2.2.20 2.2.21 2.2.22 2.2.23 2.2.3 All 32 releases
sqlite-database-integration / wp-includes / sqlite-ast / class-wp-sqlite-configurator.php

class-wp-sqlite-configurator.php in SQLite Database Integration 2.2.3, at wp-includes/sqlite-ast/class-wp-sqlite-configurator.php

125 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * SQLite database configurator.
5 *
6 * This class initializes and configures the SQLite database, so that it can be
7 * used by the SQLite driver to translate and emulate MySQL queries in SQLite.
8 *
9 * The configurator ensures that tables required for emulating MySQL behaviors
10 * are created and populated with necessary data. It is also able to partially
11 * repair and update these tables and metadata in case of database corruption.
12 */
13 class WP_SQLite_Configurator {
14 /**
15 * The SQLite driver instance.
16 *
17 * @var WP_SQLite_Driver
18 */
19 private $driver;
20
21 /**
22 * A service for managing MySQL INFORMATION_SCHEMA tables in SQLite.
23 *
24 * @var WP_SQLite_Information_Schema_Builder
25 */
26 private $schema_builder;
27
28 /**
29 * A service for reconstructing the MySQL INFORMATION_SCHEMA tables in SQLite.
30 *
31 * @var WP_SQLite_Information_Schema_Reconstructor
32 */
33 private $schema_reconstructor;
34
35 /**
36 * Constructor.
37 *
38 * @param WP_SQLite_Driver $driver The SQLite driver instance.
39 * @param WP_SQLite_Information_Schema_Builder $schema_builder The information schema builder instance.
40 */
41 public function __construct(
42 WP_SQLite_Driver $driver,
43 WP_SQLite_Information_Schema_Builder $schema_builder
44 ) {
45 $this->driver = $driver;
46 $this->schema_builder = $schema_builder;
47 $this->schema_reconstructor = new WP_SQLite_Information_Schema_Reconstructor(
48 $driver,
49 $schema_builder
50 );
51 }
52
53 /**
54 * Ensure that the SQLite database is configured.
55 *
56 * This method checks if the database is configured for the latest SQLite
57 * driver version, and if it is not, it will configure the database.
58 */
59 public function ensure_database_configured(): void {
60 $version = SQLITE_DRIVER_VERSION;
61 $db_version = $this->driver->get_saved_driver_version();
62 if ( version_compare( $version, $db_version ) > 0 ) {
63 $this->configure_database();
64 }
65 }
66
67 /**
68 * Configure the SQLite database.
69 *
70 * This method creates tables used for emulating MySQL behaviors in SQLite,
71 * and populates them with necessary data. When it is used with an already
72 * configured database, it will update the configuration as per the current
73 * SQLite driver version and attempt to repair any configuration corruption.
74 */
75 public function configure_database(): void {
76 // Use an EXCLUSIVE transaction to prevent multiple connections
77 // from attempting to configure the database at the same time.
78 $this->driver->execute_sqlite_query( 'BEGIN EXCLUSIVE TRANSACTION' );
79 try {
80 $this->ensure_global_variables_table();
81 $this->schema_builder->ensure_information_schema_tables();
82 $this->schema_reconstructor->ensure_correct_information_schema();
83 $this->save_current_driver_version();
84 } catch ( Throwable $e ) {
85 $this->driver->execute_sqlite_query( 'ROLLBACK' );
86 throw $e;
87 }
88 $this->driver->execute_sqlite_query( 'COMMIT' );
89 }
90
91 /**
92 * Ensure that the global variables table exists.
93 *
94 * This method configures a database table to store MySQL global variables
95 * and other internal configuration values.
96 */
97 private function ensure_global_variables_table(): void {
98 $this->driver->execute_sqlite_query(
99 sprintf(
100 'CREATE TABLE IF NOT EXISTS %s (name TEXT PRIMARY KEY, value TEXT)',
101 WP_SQLite_Driver::GLOBAL_VARIABLES_TABLE_NAME
102 )
103 );
104 }
105
106 /**
107 * Save the current SQLite driver version.
108 *
109 * This method saves the current SQLite driver version to the database.
110 */
111 private function save_current_driver_version(): void {
112 $this->driver->execute_sqlite_query(
113 sprintf(
114 'INSERT INTO %s (name, value) VALUES (?, ?) ON CONFLICT(name) DO UPDATE SET value = ?',
115 WP_SQLite_Driver::GLOBAL_VARIABLES_TABLE_NAME
116 ),
117 array(
118 WP_SQLite_Driver::DRIVER_VERSION_VARIABLE_NAME,
119 SQLITE_DRIVER_VERSION,
120 SQLITE_DRIVER_VERSION,
121 )
122 );
123 }
124 }
125