class-file-tree-producer.php
2 weeks ago
class-hmac-client.php
2 weeks ago
class-hmac-server.php
2 weeks ago
class-http-server.php
2 weeks ago
class-mysql-dump-producer.php
2 weeks ago
class-pdo-polyfill.php
2 weeks ago
class-sqlite-driver-pdo.php
2 weeks ago
class-staged-artifacts.php
2 weeks ago
class-staged-endpoints.php
2 weeks ago
class-staged-push-stream-protocol.php
2 weeks ago
class-wpdb-driver-pdo.php
2 weeks ago
export.php
2 weeks ago
utils.php
2 weeks ago
class-sqlite-driver-pdo.php
236 lines
| 1 | <?php |
| 2 | /** |
| 3 | * PDO-compatible adapter for WP_SQLite_Driver. |
| 4 | * |
| 5 | * MySQLDumpProducer expects a PDO connection — prepare(), query(), quote(), |
| 6 | * and the statement methods fetch(), fetchAll(), fetchColumn(), execute(). |
| 7 | * On SQLite sites, the WP_SQLite_Driver is already loaded by WordPress |
| 8 | * (via the sqlite-database-integration plugin's db.php drop-in) and is |
| 9 | * available at $wpdb->dbh. This adapter wraps that driver so the dump |
| 10 | * producer can use it without knowing it's talking to SQLite. |
| 11 | * |
| 12 | * Every MySQL query goes through the driver's AST-based translator, which |
| 13 | * converts it to SQLite on the fly. The result is transparent: the dump |
| 14 | * producer sends MySQL queries, gets rows back, and produces valid MySQL |
| 15 | * SQL output. |
| 16 | */ |
| 17 | |
| 18 | /** |
| 19 | * Wraps a WP_SQLite_Driver instance to look like a PDO connection. |
| 20 | * |
| 21 | * Only the methods that MySQLDumpProducer and the export endpoints actually |
| 22 | * use are implemented. Anything else will trigger a clear PHP error rather |
| 23 | * than silently misbehaving. |
| 24 | */ |
| 25 | class SqliteDriverPDO |
| 26 | { |
| 27 | /** @var WP_SQLite_Driver */ |
| 28 | private $driver; |
| 29 | |
| 30 | /** @var PDO The raw SQLite PDO for quote() delegation. */ |
| 31 | private $raw_pdo; |
| 32 | |
| 33 | public function __construct(WP_SQLite_Driver $driver, PDO $raw_pdo) |
| 34 | { |
| 35 | $this->driver = $driver; |
| 36 | $this->raw_pdo = $raw_pdo; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Prepares a statement for execution. |
| 41 | * |
| 42 | * Returns a SqliteDriverPDOStatement that will substitute parameters |
| 43 | * and execute through the driver when execute() is called. |
| 44 | */ |
| 45 | public function prepare(string $sql): SqliteDriverPDOStatement |
| 46 | { |
| 47 | return new SqliteDriverPDOStatement($this->driver, $this->raw_pdo, $sql); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Executes a query immediately and returns the result set. |
| 52 | */ |
| 53 | public function query(string $sql): SqliteDriverPDOStatement |
| 54 | { |
| 55 | $stmt = new SqliteDriverPDOStatement($this->driver, $this->raw_pdo, $sql); |
| 56 | $stmt->execute(); |
| 57 | return $stmt; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Quotes a string for safe inclusion in a query. |
| 62 | * Delegates to the underlying raw SQLite PDO. |
| 63 | */ |
| 64 | public function quote(string $value, int $type = PDO::PARAM_STR): string |
| 65 | { |
| 66 | return $this->raw_pdo->quote($value, $type); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * PDOStatement-compatible wrapper for WP_SQLite_Driver query results. |
| 72 | * |
| 73 | * Collects all result rows eagerly after execution and serves them |
| 74 | * through fetch/fetchAll/fetchColumn. |
| 75 | */ |
| 76 | class SqliteDriverPDOStatement |
| 77 | { |
| 78 | /** @var WP_SQLite_Driver */ |
| 79 | private $driver; |
| 80 | |
| 81 | /** @var PDO The raw SQLite PDO for quote() delegation. */ |
| 82 | private $raw_pdo; |
| 83 | |
| 84 | /** @var string */ |
| 85 | private $sql; |
| 86 | |
| 87 | /** @var array Stored result rows after execution. */ |
| 88 | private $rows = []; |
| 89 | |
| 90 | /** @var int Current position for fetch(). */ |
| 91 | private $position = 0; |
| 92 | |
| 93 | /** @var array|null Parameters bound via bindValue(). */ |
| 94 | private $bound_params = null; |
| 95 | |
| 96 | public function __construct(WP_SQLite_Driver $driver, PDO $raw_pdo, string $sql) |
| 97 | { |
| 98 | $this->driver = $driver; |
| 99 | $this->raw_pdo = $raw_pdo; |
| 100 | $this->sql = $sql; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Executes the prepared statement. |
| 105 | * |
| 106 | * Substitutes bound parameters into the query, sends it through |
| 107 | * WP_SQLite_Driver::query(), and stores the result rows. |
| 108 | * |
| 109 | * @param array|null $params Positional or named parameters. |
| 110 | * @return bool True on success. |
| 111 | */ |
| 112 | public function execute($params = null): bool |
| 113 | { |
| 114 | // Merge in any parameters set via bindValue(). |
| 115 | if ($params === null && $this->bound_params !== null) { |
| 116 | $params = $this->bound_params; |
| 117 | } |
| 118 | |
| 119 | $sql = $this->sql; |
| 120 | |
| 121 | if ($params !== null && count($params) > 0) { |
| 122 | // Find ? placeholder positions outside of string literals. |
| 123 | $positions = []; |
| 124 | $len = strlen($sql); |
| 125 | $in_single = false; |
| 126 | $in_double = false; |
| 127 | for ($i = 0; $i < $len; $i++) { |
| 128 | $ch = $sql[$i]; |
| 129 | if ($ch === "'" && !$in_double) { |
| 130 | $in_single = !$in_single; |
| 131 | } elseif ($ch === '"' && !$in_single) { |
| 132 | $in_double = !$in_double; |
| 133 | } elseif ($ch === '?' && !$in_single && !$in_double) { |
| 134 | $positions[] = $i; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // Replace positional placeholders from right to left so |
| 139 | // earlier offsets stay valid. |
| 140 | for ($i = count($positions) - 1; $i >= 0; $i--) { |
| 141 | if (!array_key_exists($i, $params)) { |
| 142 | continue; |
| 143 | } |
| 144 | $quoted = $this->raw_pdo->quote($params[$i]); |
| 145 | $sql = substr_replace($sql, $quoted, $positions[$i], 1); |
| 146 | } |
| 147 | |
| 148 | // Named parameters (:name style). |
| 149 | foreach ($params as $key => $value) { |
| 150 | if (!is_string($key)) { |
| 151 | continue; |
| 152 | } |
| 153 | $sql = str_replace($key, $this->raw_pdo->quote($value), $sql); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | $this->driver->query($sql); |
| 158 | $result = $this->driver->get_query_results(); |
| 159 | $this->rows = is_array($result) ? $result : []; |
| 160 | |
| 161 | // WP_SQLite_Driver returns results as arrays of objects (PDO::FETCH_OBJ |
| 162 | // by default). Convert to associative arrays for PDO compatibility. |
| 163 | foreach ($this->rows as $i => $row) { |
| 164 | if (is_object($row)) { |
| 165 | $this->rows[$i] = (array) $row; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | $this->position = 0; |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Fetches the next row from the result set. |
| 175 | * |
| 176 | * @param int $mode Fetch mode (ignored — always returns associative array). |
| 177 | * @return array|false Associative array or false when exhausted. |
| 178 | */ |
| 179 | public function fetch($mode = PDO::FETCH_ASSOC) |
| 180 | { |
| 181 | if ($this->position >= count($this->rows)) { |
| 182 | return false; |
| 183 | } |
| 184 | return $this->rows[$this->position++]; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Returns all remaining rows from the result set. |
| 189 | * |
| 190 | * @param int $mode Fetch mode. Supports FETCH_ASSOC (default) and FETCH_COLUMN. |
| 191 | * @return array |
| 192 | */ |
| 193 | public function fetchAll($mode = PDO::FETCH_ASSOC) |
| 194 | { |
| 195 | $remaining = array_slice($this->rows, $this->position); |
| 196 | $this->position = count($this->rows); |
| 197 | |
| 198 | if ($mode === PDO::FETCH_COLUMN) { |
| 199 | return array_map(function ($row) { |
| 200 | return reset($row); |
| 201 | }, $remaining); |
| 202 | } |
| 203 | |
| 204 | return $remaining; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Returns a single column from the next row. |
| 209 | * |
| 210 | * @param int $column_number 0-indexed column number. |
| 211 | * @return mixed|false The column value, or false if no more rows. |
| 212 | */ |
| 213 | public function fetchColumn(int $column_number = 0) |
| 214 | { |
| 215 | $row = $this->fetch(); |
| 216 | if ($row === false) { |
| 217 | return false; |
| 218 | } |
| 219 | $values = array_values($row); |
| 220 | return $values[$column_number] ?? false; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Binds a value to a named or positional parameter. |
| 225 | */ |
| 226 | public function bindValue($parameter, $value, int $type = PDO::PARAM_STR): bool |
| 227 | { |
| 228 | if ($this->bound_params === null) { |
| 229 | $this->bound_params = []; |
| 230 | } |
| 231 | $this->bound_params[$parameter] = $value; |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | } |
| 236 |