Skip to content

Commit fb644ce

Browse files
committed
Add SQLite tabels command supporting list and csv
1 parent 9dd8078 commit fb644ce

2 files changed

Lines changed: 133 additions & 0 deletions

File tree

‎src/SQLite_Command.php‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use WP_CLI;
66
use WP_CLI_Command;
7+
use PDO;
78

89
class SQLite_Command extends WP_CLI_Command {
910

@@ -97,4 +98,57 @@ public function export( $args, $assoc_args ) {
9798

9899
$export->run( $result_file, $assoc_args );
99100
}
101+
102+
/**
103+
* Lists the database tables.
104+
*
105+
* Defaults to all tables in the SQLite database.
106+
*
107+
* ## OPTIONS
108+
*
109+
* [<table>...]
110+
* : List tables based on wildcard search, e.g. 'wp_*_options' or 'wp_post?'.
111+
*
112+
* [--format=<format>]
113+
* : Render output in a particular format.
114+
* ---
115+
* default: list
116+
* options:
117+
* - list
118+
* - csv
119+
* ---
120+
*
121+
* ## EXAMPLES
122+
*
123+
* # List all tables in the database
124+
* $ wp sqlite tables
125+
* wp_commentmeta
126+
* wp_comments
127+
* wp_links
128+
* wp_options
129+
* wp_postmeta
130+
* wp_posts
131+
* wp_terms
132+
* wp_termmeta
133+
* wp_term_relationships
134+
* wp_term_taxonomy
135+
* wp_usermeta
136+
* wp_users
137+
*
138+
* # List all tables matching a wildcard
139+
* $ wp sqlite tables wp_post*
140+
* wp_postmeta
141+
* wp_posts
142+
*
143+
* @when before_wp_load
144+
*/
145+
public function tables($args, $assoc_args) {
146+
if (!Base::get_sqlite_plugin_version()) {
147+
WP_CLI::error('The SQLite integration plugin is not installed or activated.');
148+
}
149+
150+
$tables = new Tables();
151+
$pattern = !empty($args) ? $args[0] : null;
152+
$tables->run($pattern, $assoc_args);
153+
}
100154
}

‎src/Tables.php‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace Automattic\WP_CLI\SQLite;
4+
5+
use WP_CLI;
6+
use PDO;
7+
use WP_SQLite_Translator;
8+
9+
/**
10+
* Class Tables
11+
* Handles listing tables in the SQLite database.
12+
*/
13+
class Tables extends Base {
14+
15+
protected $translator;
16+
protected $args = array();
17+
protected $is_stdout = false;
18+
19+
public function __construct() {
20+
$this->load_dependencies();
21+
$this->translator = new WP_SQLite_Translator();
22+
}
23+
24+
/**
25+
* Get the PDO instance.
26+
*
27+
* @return PDO
28+
*/
29+
protected function get_pdo() {
30+
return $this->translator->get_pdo();
31+
}
32+
33+
/**
34+
* Lists all tables in the SQLite database.
35+
*
36+
* @param string|null $pattern Optional wildcard pattern to filter tables.
37+
* @param array $assoc_args Associative array of options.
38+
* @return void
39+
*/
40+
public function run($pattern = null, $assoc_args = []) {
41+
$pdo = $this->get_pdo();
42+
43+
// Get all tables
44+
$stmt = $pdo->query("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'");
45+
$tables = $stmt->fetchAll(PDO::FETCH_COLUMN);
46+
47+
// 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($tables, function($table) use ($pattern) {
53+
return fnmatch($pattern, $table, FNM_CASEFOLD);
54+
});
55+
}
56+
57+
// Remove system tables
58+
$tables_to_exclude = array('_mysql_data_types_cache');
59+
$tables = array_diff($tables, $tables_to_exclude);
60+
61+
if (empty($tables)) {
62+
if ($pattern) {
63+
WP_CLI::error("No tables found matching: " . $pattern);
64+
} else {
65+
WP_CLI::error("No tables found in the database.");
66+
}
67+
}
68+
69+
$format = \WP_CLI\Utils\get_flag_value( $assoc_args, 'format' );
70+
71+
if ( 'csv' === $format ) {
72+
WP_CLI::line( implode( ',', $tables ) );
73+
} else {
74+
foreach ( $tables as $table ) {
75+
WP_CLI::line( $table );
76+
}
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)