Skip to content

Commit d37e0df

Browse files
committed
Simplify export query execution
Route both PDO-backed and compatibility-driver data queries through get_pdo() and use the common query API for record checks and row iteration.
1 parent e9e9fbe commit d37e0df

2 files changed

Lines changed: 8 additions & 17 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: 4 additions & 16 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_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,14 +314,8 @@ 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_pdo()->query( 'SELECT COUNT(*) FROM ' . $table_name );
331319
return $stmt->fetchColumn() > 0;
332320
}
333321

0 commit comments

Comments
 (0)