diff options
author | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2024-02-06 11:34:56 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-06 11:34:56 (GMT) |
commit | 1a10437a14b13100bdf41cbdab819c33258deb65 (patch) | |
tree | 1810d94e1a64b264d28865f5cab1cf08b13c38ea /Lib/sqlite3 | |
parent | 4bf41879d03b1da3c6d38c39a04331e3ae2e7545 (diff) | |
download | cpython-1a10437a14b13100bdf41cbdab819c33258deb65.zip cpython-1a10437a14b13100bdf41cbdab819c33258deb65.tar.gz cpython-1a10437a14b13100bdf41cbdab819c33258deb65.tar.bz2 |
gh-91602: Add iterdump() support for filtering database objects (#114501)
Add optional 'filter' parameter to iterdump() that allows a "LIKE"
pattern for filtering database objects to dump.
Co-authored-by: Erlend E. Aasland <erlend@python.org>
Diffstat (limited to 'Lib/sqlite3')
-rw-r--r-- | Lib/sqlite3/dump.py | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/Lib/sqlite3/dump.py b/Lib/sqlite3/dump.py index 719dfc8..9dcce7d 100644 --- a/Lib/sqlite3/dump.py +++ b/Lib/sqlite3/dump.py @@ -15,7 +15,7 @@ def _quote_value(value): return "'{0}'".format(value.replace("'", "''")) -def _iterdump(connection): +def _iterdump(connection, *, filter=None): """ Returns an iterator to the dump of the database in an SQL text format. @@ -32,15 +32,23 @@ def _iterdump(connection): yield('PRAGMA foreign_keys=OFF;') yield('BEGIN TRANSACTION;') + if filter: + # Return database objects which match the filter pattern. + filter_name_clause = 'AND "name" LIKE ?' + params = [filter] + else: + filter_name_clause = "" + params = [] # sqlite_master table contains the SQL CREATE statements for the database. - q = """ + q = f""" SELECT "name", "type", "sql" FROM "sqlite_master" WHERE "sql" NOT NULL AND "type" == 'table' + {filter_name_clause} ORDER BY "name" """ - schema_res = cu.execute(q) + schema_res = cu.execute(q, params) sqlite_sequence = [] for table_name, type, sql in schema_res.fetchall(): if table_name == 'sqlite_sequence': @@ -82,13 +90,14 @@ def _iterdump(connection): yield("{0};".format(row[0])) # Now when the type is 'index', 'trigger', or 'view' - q = """ + q = f""" SELECT "name", "type", "sql" FROM "sqlite_master" WHERE "sql" NOT NULL AND "type" IN ('index', 'trigger', 'view') + {filter_name_clause} """ - schema_res = cu.execute(q) + schema_res = cu.execute(q, params) for name, type, sql in schema_res.fetchall(): yield('{0};'.format(sql)) |