Skip to content

Commit ba22c09

Browse files
committed
Simplify export query execution
Use the underlying SQLite PDO connection for data reads in every driver mode. This keeps export streaming independent of the driver wrapper's result format.
1 parent ab6f52a commit ba22c09

2 files changed

Lines changed: 10 additions & 19 deletions

File tree

‎phpstan/stubs.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ public function query( string $sql ) {}
2626
public function get_connection(): WP_SQLite_Connection {}
2727
}
2828

29-
class WP_MySQL_On_SQLite extends PDO {} // phpcs:ignore WordPress.DB.RestrictedClasses.mysql__PDO -- Stub for the plugin's PDO subclass.
29+
// phpcs:ignore WordPress.DB.RestrictedClasses.mysql__PDO -- Stub for the plugin's PDO subclass.
30+
class WP_MySQL_On_SQLite extends PDO {
31+
public function get_connection(): WP_SQLite_Connection {}
32+
}
3033

3134
class WP_SQLite_Translator {
3235
public function __construct() {}

‎src/Export.php‎

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,8 @@ protected function get_create_statement( $table_name ) {
201201
* @return \Generator
202202
*/
203203
protected function get_insert_statements( $table_name ) {
204-
if ( $this->driver instanceof PDO ) {
205-
$quoted_table_name = '`' . str_replace( '`', '``', $table_name ) . '`';
206-
$stmt = $this->driver->query( 'SELECT * FROM ' . $quoted_table_name );
207-
} else {
208-
$pdo = $this->get_pdo();
209-
$stmt = $pdo->prepare( 'SELECT * FROM ' . $table_name );
210-
$stmt->execute();
211-
}
204+
$quoted_table_name = '`' . str_replace( '`', '``', $table_name ) . '`';
205+
$stmt = $this->get_sqlite_pdo()->query( 'SELECT * FROM ' . $quoted_table_name );
212206
// phpcs:ignore
213207
while ( $row = $stmt->fetch( PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT ) ) {
214208
yield sprintf( 'INSERT INTO `%1s` VALUES (%2s);', $table_name, $this->escape_values( $row ) );
@@ -320,23 +314,17 @@ protected function get_dump_comment( $comment ) {
320314
* @return bool
321315
*/
322316
protected function table_has_records( $table_name ) {
323-
if ( $this->driver instanceof PDO ) {
324-
$table_name = '`' . str_replace( '`', '``', $table_name ) . '`';
325-
$stmt = $this->driver->query( 'SELECT COUNT(*) FROM ' . $table_name );
326-
} else {
327-
$pdo = $this->get_pdo();
328-
$stmt = $pdo->prepare( 'SELECT COUNT(*) FROM ' . $table_name );
329-
$stmt->execute();
330-
}
317+
$table_name = '`' . str_replace( '`', '``', $table_name ) . '`';
318+
$stmt = $this->get_sqlite_pdo()->query( 'SELECT COUNT(*) FROM ' . $table_name );
331319
return $stmt->fetchColumn() > 0;
332320
}
333321

334322
/**
335-
* Get the PDO instance.
323+
* Get the underlying SQLite PDO instance.
336324
*
337325
* @return PDO
338326
*/
339-
protected function get_pdo() {
327+
protected function get_sqlite_pdo() {
340328
if ( $this->driver instanceof WP_SQLite_Translator ) {
341329
return $this->driver->get_pdo();
342330
}

0 commit comments

Comments
 (0)