Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b1b9382

Browse files
committed
Added missing files for new iterdump method.
1 parent bbe741d commit b1b9382

2 files changed

Lines changed: 115 additions & 0 deletions

File tree

Lib/sqlite3/dump.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Mimic the sqlite3 console shell's .dump command
2+
# Author: Paul Kippes <[email protected]>
3+
4+
def _iterdump(connection):
5+
"""
6+
Returns an iterator to the dump of the database in an SQL text format.
7+
8+
Used to produce an SQL dump of the database. Useful to save an in-memory
9+
database for later restoration. This function should not be called
10+
directly but instead called from the Connection method, iterdump().
11+
"""
12+
13+
cu = connection.cursor()
14+
yield('BEGIN TRANSACTION;')
15+
16+
# sqlite_master table contains the SQL CREATE statements for the database.
17+
q = """
18+
SELECT name, type, sql
19+
FROM sqlite_master
20+
WHERE sql NOT NULL AND
21+
type == 'table'
22+
"""
23+
schema_res = cu.execute(q)
24+
for table_name, type, sql in schema_res.fetchall():
25+
if table_name == 'sqlite_sequence':
26+
yield('DELETE FROM sqlite_sequence;')
27+
elif table_name == 'sqlite_stat1':
28+
yield('ANALYZE sqlite_master;')
29+
elif table_name.startswith('sqlite_'):
30+
continue
31+
# NOTE: Virtual table support not implemented
32+
#elif sql.startswith('CREATE VIRTUAL TABLE'):
33+
# qtable = table_name.replace("'", "''")
34+
# yield("INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"\
35+
# "VALUES('table','%s','%s',0,'%s');" %
36+
# qtable,
37+
# qtable,
38+
# sql.replace("''"))
39+
else:
40+
yield('%s;' % sql)
41+
42+
# Build the insert statement for each row of the current table
43+
res = cu.execute("PRAGMA table_info('%s')" % table_name)
44+
column_names = [str(table_info[1]) for table_info in res.fetchall()]
45+
q = "SELECT 'INSERT INTO \"%(tbl_name)s\" VALUES("
46+
q += ",".join(["'||quote(" + col + ")||'" for col in column_names])
47+
q += ")' FROM '%(tbl_name)s'"
48+
query_res = cu.execute(q % {'tbl_name': table_name})
49+
for row in query_res:
50+
yield("%s;" % row[0])
51+
52+
# Now when the type is 'index', 'trigger', or 'view'
53+
q = """
54+
SELECT name, type, sql
55+
FROM sqlite_master
56+
WHERE sql NOT NULL AND
57+
type IN ('index', 'trigger', 'view')
58+
"""
59+
schema_res = cu.execute(q)
60+
for name, type, sql in schema_res.fetchall():
61+
yield('%s;' % sql)
62+
63+
yield('COMMIT;')

Lib/sqlite3/test/dump.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Author: Paul Kippes <[email protected]>
2+
3+
import unittest
4+
import sqlite3 as sqlite
5+
6+
class DumpTests(unittest.TestCase):
7+
def setUp(self):
8+
self.cx = sqlite.connect(":memory:")
9+
self.cu = self.cx.cursor()
10+
11+
def tearDown(self):
12+
self.cx.close()
13+
14+
def CheckTableDump(self):
15+
expected_sqls = [
16+
"CREATE TABLE t1(id integer primary key, s1 text, " \
17+
"t1_i1 integer not null, i2 integer, unique (s1), " \
18+
"constraint t1_idx1 unique (i2));"
19+
,
20+
"INSERT INTO \"t1\" VALUES(1,'foo',10,20);"
21+
,
22+
"INSERT INTO \"t1\" VALUES(2,'foo2',30,30);"
23+
,
24+
"CREATE TABLE t2(id integer, t2_i1 integer, " \
25+
"t2_i2 integer, primary key (id)," \
26+
"foreign key(t2_i1) references t1(t1_i1));"
27+
,
28+
"CREATE TRIGGER trigger_1 update of t1_i1 on t1 " \
29+
"begin " \
30+
"update t2 set t2_i1 = new.t1_i1 where t2_i1 = old.t1_i1; " \
31+
"end;"
32+
,
33+
"CREATE VIEW v1 as select * from t1 left join t2 " \
34+
"using (id);"
35+
]
36+
[self.cu.execute(s) for s in expected_sqls]
37+
i = self.cx.iterdump()
38+
actual_sqls = [s for s in i]
39+
expected_sqls = ['BEGIN TRANSACTION;'] + expected_sqls + \
40+
['COMMIT;']
41+
[self.assertEqual(expected_sqls[i], actual_sqls[i])
42+
for i in range(len(expected_sqls))]
43+
44+
def suite():
45+
return unittest.TestSuite(unittest.makeSuite(DumpTests, "Check"))
46+
47+
def test():
48+
runner = unittest.TextTestRunner()
49+
runner.run(suite())
50+
51+
if __name__ == "__main__":
52+
test()

0 commit comments

Comments
 (0)