Skip to content

Commit 33eee43

Browse files
[3.11] gh-64662: Fix virtual table support in sqlite3.Connection.iterdump (#108340) (#108564)
(cherry picked from commit d0160c7) Co-authored-by: Aviv Palivoda <[email protected]>
1 parent 6f6171b commit 33eee43

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

Lib/sqlite3/dump.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def _iterdump(connection):
1616
directly but instead called from the Connection method, iterdump().
1717
"""
1818

19+
writeable_schema = False
1920
cu = connection.cursor()
2021
yield('BEGIN TRANSACTION;')
2122

@@ -42,13 +43,15 @@ def _iterdump(connection):
4243
yield('ANALYZE "sqlite_master";')
4344
elif table_name.startswith('sqlite_'):
4445
continue
45-
# NOTE: Virtual table support not implemented
46-
#elif sql.startswith('CREATE VIRTUAL TABLE'):
47-
# qtable = table_name.replace("'", "''")
48-
# yield("INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"\
49-
# "VALUES('table','{0}','{0}',0,'{1}');".format(
50-
# qtable,
51-
# sql.replace("''")))
46+
elif sql.startswith('CREATE VIRTUAL TABLE'):
47+
if not writeable_schema:
48+
writeable_schema = True
49+
yield('PRAGMA writable_schema=ON;')
50+
yield("INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
51+
"VALUES('table','{0}','{0}',0,'{1}');".format(
52+
table_name.replace("'", "''"),
53+
sql.replace("'", "''"),
54+
))
5255
else:
5356
yield('{0};'.format(sql))
5457

@@ -74,6 +77,9 @@ def _iterdump(connection):
7477
for name, type, sql in schema_res.fetchall():
7578
yield('{0};'.format(sql))
7679

80+
if writeable_schema:
81+
yield('PRAGMA writable_schema=OFF;')
82+
7783
# gh-79009: Yield statements concerning the sqlite_sequence table at the
7884
# end of the transaction.
7985
for row in sqlite_sequence:

Lib/test/test_sqlite3/test_dump.py

+20
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,26 @@ def __getitem__(self, index):
117117
got = list(self.cx.iterdump())
118118
self.assertEqual(expected, got)
119119

120+
def test_dump_virtual_tables(self):
121+
# gh-64662
122+
expected = [
123+
"BEGIN TRANSACTION;",
124+
"PRAGMA writable_schema=ON;",
125+
("INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
126+
"VALUES('table','test','test',0,'CREATE VIRTUAL TABLE test USING fts4(example)');"),
127+
"CREATE TABLE 'test_content'(docid INTEGER PRIMARY KEY, 'c0example');",
128+
"CREATE TABLE 'test_docsize'(docid INTEGER PRIMARY KEY, size BLOB);",
129+
("CREATE TABLE 'test_segdir'(level INTEGER,idx INTEGER,start_block INTEGER,"
130+
"leaves_end_block INTEGER,end_block INTEGER,root BLOB,PRIMARY KEY(level, idx));"),
131+
"CREATE TABLE 'test_segments'(blockid INTEGER PRIMARY KEY, block BLOB);",
132+
"CREATE TABLE 'test_stat'(id INTEGER PRIMARY KEY, value BLOB);",
133+
"PRAGMA writable_schema=OFF;",
134+
"COMMIT;"
135+
]
136+
self.cu.execute("CREATE VIRTUAL TABLE test USING fts4(example)")
137+
actual = list(self.cx.iterdump())
138+
self.assertEqual(expected, actual)
139+
120140

121141
if __name__ == "__main__":
122142
unittest.main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix support for virtual tables in :meth:`sqlite3.Connection.iterdump`. Patch
2+
by Aviv Palivoda.

0 commit comments

Comments
 (0)