Skip to content

Commit ba78e0d

Browse files
Support multiple table patterns
1 parent 958e454 commit ba78e0d

2 files changed

Lines changed: 17 additions & 19 deletions

File tree

‎src/SQLite_Command.php‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,10 @@ public function export( $args, $assoc_args ) {
135135
* wp_usermeta
136136
* wp_users
137137
*
138-
* # List all tables matching a wildcard
139-
* $ wp sqlite tables wp_post*
138+
* # List all tables matching wildcards
139+
* $ wp sqlite tables wp_comment* wp_post*
140+
* wp_commentmeta
141+
* wp_comments
140142
* wp_postmeta
141143
* wp_posts
142144
*
@@ -147,8 +149,7 @@ public function tables( $args, $assoc_args ) {
147149
WP_CLI::error( 'The SQLite integration plugin is not installed or activated.' );
148150
}
149151

150-
$tables = new Tables();
151-
$pattern = ! empty( $args ) ? $args[0] : null;
152-
$tables->run( $pattern, $assoc_args );
152+
$tables = new Tables();
153+
$tables->run( $args, $assoc_args );
153154
}
154155
}

‎src/Tables.php‎

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,37 +33,34 @@ protected function get_pdo() {
3333
/**
3434
* Lists all tables in the SQLite database.
3535
*
36-
* @param string|null $pattern Optional wildcard pattern to filter tables.
36+
* @param array|null $patterns Optional wildcard patterns to filter tables.
3737
* @param array $assoc_args Associative array of options.
3838
* @return void
3939
*/
40-
public function run( $pattern = null, $assoc_args = [] ) {
40+
public function run( ?array $patterns = null, $assoc_args = [] ) {
4141
$pdo = $this->get_pdo();
4242

4343
// Get all tables
4444
$stmt = $pdo->query( "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'" );
4545
$tables = $stmt->fetchAll( PDO::FETCH_COLUMN );
4646

4747
// Filter tables if wildcard pattern is provided
48-
if ( $pattern ) {
49-
$sql_pattern = str_replace( '?', '_', $pattern );
50-
$sql_pattern = str_replace( '*', '%', $sql_pattern );
51-
52-
$tables = array_filter(
53-
$tables,
54-
function ( $table ) use ( $pattern ) {
55-
return fnmatch( $pattern, $table, FNM_CASEFOLD );
56-
}
57-
);
48+
if ( ! empty( $patterns ) ) {
49+
foreach ( $patterns as $pattern ) {
50+
$tables = array_filter(
51+
$tables,
52+
fn ( $table ) => fnmatch( $pattern, $table, FNM_CASEFOLD )
53+
);
54+
}
5855
}
5956

6057
// Remove system tables
6158
$tables_to_exclude = array( '_mysql_data_types_cache' );
6259
$tables = array_diff( $tables, $tables_to_exclude );
6360

6461
if ( empty( $tables ) ) {
65-
if ( $pattern ) {
66-
WP_CLI::error( 'No tables found matching: ' . $pattern );
62+
if ( ! empty( $patterns ) ) {
63+
WP_CLI::error( 'No tables found.' );
6764
} else {
6865
WP_CLI::error( 'No tables found in the database.' );
6966
}

0 commit comments

Comments
 (0)