]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-64662: Fix virtual table support in sqlite3.Connection.iterdump (#108340...
authorErlend E. Aasland <erlend@python.org>
Mon, 28 Aug 2023 13:09:33 +0000 (15:09 +0200)
committerGitHub <noreply@github.com>
Mon, 28 Aug 2023 13:09:33 +0000 (13:09 +0000)
(cherry picked from commit d0160c7c22c8dff0a61c49b5304244df6e36465e)

Co-authored-by: Aviv Palivoda <palaviv@gmail.com>
Lib/sqlite3/dump.py
Lib/test/test_sqlite3/test_dump.py
Misc/NEWS.d/next/Library/2023-08-22-22-29-42.gh-issue-64662.jHl_Bt.rst [new file with mode: 0644]

index 07b9da10b920f937ea2edcdfe2d6ea3d5a23c150..1cf8759f8970f1ea7259b1c849d82e6d64d78e57 100644 (file)
@@ -16,6 +16,7 @@ def _iterdump(connection):
     directly but instead called from the Connection method, iterdump().
     """
 
+    writeable_schema = False
     cu = connection.cursor()
     yield('BEGIN TRANSACTION;')
 
@@ -42,13 +43,15 @@ def _iterdump(connection):
             yield('ANALYZE "sqlite_master";')
         elif table_name.startswith('sqlite_'):
             continue
-        # NOTE: Virtual table support not implemented
-        #elif sql.startswith('CREATE VIRTUAL TABLE'):
-        #    qtable = table_name.replace("'", "''")
-        #    yield("INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"\
-        #        "VALUES('table','{0}','{0}',0,'{1}');".format(
-        #        qtable,
-        #        sql.replace("''")))
+        elif sql.startswith('CREATE VIRTUAL TABLE'):
+            if not writeable_schema:
+                writeable_schema = True
+                yield('PRAGMA writable_schema=ON;')
+            yield("INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
+                  "VALUES('table','{0}','{0}',0,'{1}');".format(
+                      table_name.replace("'", "''"),
+                      sql.replace("'", "''"),
+                  ))
         else:
             yield('{0};'.format(sql))
 
@@ -74,6 +77,9 @@ def _iterdump(connection):
     for name, type, sql in schema_res.fetchall():
         yield('{0};'.format(sql))
 
+    if writeable_schema:
+        yield('PRAGMA writable_schema=OFF;')
+
     # gh-79009: Yield statements concerning the sqlite_sequence table at the
     # end of the transaction.
     for row in sqlite_sequence:
index d0c24b9c60e61392902fcafb6dd1ab03f4a7d7be..c3ed3aefef04458409803b2fc32c0f38e0e5a36f 100644 (file)
@@ -117,6 +117,26 @@ class DumpTests(unittest.TestCase):
         got = list(self.cx.iterdump())
         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__":
     unittest.main()
diff --git a/Misc/NEWS.d/next/Library/2023-08-22-22-29-42.gh-issue-64662.jHl_Bt.rst b/Misc/NEWS.d/next/Library/2023-08-22-22-29-42.gh-issue-64662.jHl_Bt.rst
new file mode 100644 (file)
index 0000000..1b4c33a
--- /dev/null
@@ -0,0 +1,2 @@
+Fix support for virtual tables in :meth:`sqlite3.Connection.iterdump`. Patch
+by Aviv Palivoda.