[3.12] gh-64662: Fix virtual table support in sqlite3.Connection.iterdump (#108340) (#108563)

* [3.12] gh-64662: Add virtual table support to sqlite3.Connection.iterdump (#108340)

(cherry picked from commit d0160c7c22)

Co-authored-by: Aviv Palivoda <palaviv@gmail.com>

* The _quote_value helper is not part of 3.12; spell out the replacement

* With quotes

* Ok, let's use explicit quoting

---------

Co-authored-by: Aviv Palivoda <palaviv@gmail.com>
This commit is contained in:
Erlend E. Aasland 2023-08-29 00:25:35 +02:00 committed by GitHub
parent 5531d03d99
commit b451e9020d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 7 deletions

View file

@ -16,6 +16,7 @@ def _iterdump(connection):
directly but instead called from the Connection method, iterdump(). directly but instead called from the Connection method, iterdump().
""" """
writeable_schema = False
cu = connection.cursor() cu = connection.cursor()
yield('BEGIN TRANSACTION;') yield('BEGIN TRANSACTION;')
@ -42,13 +43,15 @@ def _iterdump(connection):
yield('ANALYZE "sqlite_master";') yield('ANALYZE "sqlite_master";')
elif table_name.startswith('sqlite_'): elif table_name.startswith('sqlite_'):
continue continue
# NOTE: Virtual table support not implemented elif sql.startswith('CREATE VIRTUAL TABLE'):
#elif sql.startswith('CREATE VIRTUAL TABLE'): if not writeable_schema:
# qtable = table_name.replace("'", "''") writeable_schema = True
# yield("INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"\ yield('PRAGMA writable_schema=ON;')
# "VALUES('table','{0}','{0}',0,'{1}');".format( yield("INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
# qtable, "VALUES('table','{0}','{0}',0,'{1}');".format(
# sql.replace("''"))) table_name.replace("'", "''"),
sql.replace("'", "''"),
))
else: else:
yield('{0};'.format(sql)) yield('{0};'.format(sql))
@ -74,6 +77,9 @@ def _iterdump(connection):
for name, type, sql in schema_res.fetchall(): for name, type, sql in schema_res.fetchall():
yield('{0};'.format(sql)) yield('{0};'.format(sql))
if writeable_schema:
yield('PRAGMA writable_schema=OFF;')
# gh-79009: Yield statements concerning the sqlite_sequence table at the # gh-79009: Yield statements concerning the sqlite_sequence table at the
# end of the transaction. # end of the transaction.
for row in sqlite_sequence: for row in sqlite_sequence:

View file

@ -117,6 +117,26 @@ class DumpTests(unittest.TestCase):
got = list(self.cx.iterdump()) got = list(self.cx.iterdump())
self.assertEqual(expected, got) self.assertEqual(expected, got)
def test_dump_virtual_tables(self):
# gh-64662
expected = [
"BEGIN TRANSACTION;",
"PRAGMA writable_schema=ON;",
("INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
"VALUES('table','test','test',0,'CREATE VIRTUAL TABLE test USING fts4(example)');"),
"CREATE TABLE 'test_content'(docid INTEGER PRIMARY KEY, 'c0example');",
"CREATE TABLE 'test_docsize'(docid INTEGER PRIMARY KEY, size BLOB);",
("CREATE TABLE 'test_segdir'(level INTEGER,idx INTEGER,start_block INTEGER,"
"leaves_end_block INTEGER,end_block INTEGER,root BLOB,PRIMARY KEY(level, idx));"),
"CREATE TABLE 'test_segments'(blockid INTEGER PRIMARY KEY, block BLOB);",
"CREATE TABLE 'test_stat'(id INTEGER PRIMARY KEY, value BLOB);",
"PRAGMA writable_schema=OFF;",
"COMMIT;"
]
self.cu.execute("CREATE VIRTUAL TABLE test USING fts4(example)")
actual = list(self.cx.iterdump())
self.assertEqual(expected, actual)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View file

@ -0,0 +1,2 @@
Fix support for virtual tables in :meth:`sqlite3.Connection.iterdump`. Patch
by Aviv Palivoda.