mirror of
				https://github.com/python/cpython.git
				synced 2025-11-03 19:34:08 +00:00 
			
		
		
		
	gh-64662: Add virtual table support to sqlite3.Connection.iterdump (#108340)
Co-authored-by: Aviv Palivoda <palaviv@gmail.com>
This commit is contained in:
		
							parent
							
								
									fecb9faf0b
								
							
						
					
					
						commit
						d0160c7c22
					
				
					 4 changed files with 38 additions and 7 deletions
				
			
		| 
						 | 
				
			
			@ -165,6 +165,9 @@ sqlite3
 | 
			
		|||
  object is not :meth:`closed <sqlite3.Connection.close>` explicitly.
 | 
			
		||||
  (Contributed by Erlend E. Aasland in :gh:`105539`.)
 | 
			
		||||
 | 
			
		||||
* Add support for virtual tables to :meth:`sqlite3.Connection.iterdump`.
 | 
			
		||||
  (Contributed by Aviv Palivoda in :gh:`64662`.)
 | 
			
		||||
 | 
			
		||||
tkinter
 | 
			
		||||
-------
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -24,6 +24,7 @@ def _iterdump(connection):
 | 
			
		|||
    directly but instead called from the Connection method, iterdump().
 | 
			
		||||
    """
 | 
			
		||||
 | 
			
		||||
    writeable_schema = False
 | 
			
		||||
    cu = connection.cursor()
 | 
			
		||||
    yield('BEGIN TRANSACTION;')
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -50,13 +51,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(
 | 
			
		||||
                      _quote_value(table_name),
 | 
			
		||||
                      _quote_value(sql),
 | 
			
		||||
                  ))
 | 
			
		||||
        else:
 | 
			
		||||
            yield('{0};'.format(sql))
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -85,6 +88,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:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -113,6 +113,26 @@ class DumpTests(MemoryDatabaseMixin, 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()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,2 @@
 | 
			
		|||
Add support for virtual tables to :meth:`sqlite3.Connection.iterdump`. Patch
 | 
			
		||||
by Aviv Palivoda.
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue