1 # Mimic the sqlite3 console shell's .dump command
2 # Author: Paul Kippes <kippesp@gmail.com>
3
4 # Every identifier in sql is quoted based on a comment in sqlite
5 # documentation "SQLite adds new keywords from time to time when it
6 # takes on new features. So to prevent your code from being broken by
7 # future enhancements, you should normally quote any identifier that
8 # is an English language word, even if you do not have to."
9
10 def _iterdump(connection):
11 """
12 Returns an iterator to the dump of the database in an SQL text format.
13
14 Used to produce an SQL dump of the database. Useful to save an in-memory
15 database for later restoration. This function should not be called
16 directly but instead called from the Connection method, iterdump().
17 """
18
19 writeable_schema = False
20 cu = connection.cursor()
21 yield('BEGIN TRANSACTION;')
22
23 # sqlite_master table contains the SQL CREATE statements for the database.
24 q = """
25 SELECT "name", "type", "sql"
26 FROM "sqlite_master"
27 WHERE "sql" NOT NULL AND
28 "type" == 'table'
29 ORDER BY "name"
30 """
31 schema_res = cu.execute(q)
32 sqlite_sequence = []
33 for table_name, type, sql in schema_res.fetchall():
34 if table_name == 'sqlite_sequence':
35 rows = cu.execute('SELECT * FROM "sqlite_sequence";').fetchall()
36 sqlite_sequence = ['DELETE FROM "sqlite_sequence"']
37 sqlite_sequence += [
38 f'INSERT INTO "sqlite_sequence" VALUES(\'{row[0]}\',{row[1]})'
39 for row in rows
40 ]
41 continue
42 elif table_name == 'sqlite_stat1':
43 yield('ANALYZE "sqlite_master";')
44 elif table_name.startswith('sqlite_'):
45 continue
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 ))
55 else:
56 yield('{0};'.format(sql))
57
58 # Build the insert statement for each row of the current table
59 table_name_ident = table_name.replace('"', '""')
60 res = cu.execute('PRAGMA table_info("{0}")'.format(table_name_ident))
61 column_names = [str(table_info[1]) for table_info in res.fetchall()]
62 q = """SELECT 'INSERT INTO "{0}" VALUES({1})' FROM "{0}";""".format(
63 table_name_ident,
64 ",".join("""'||quote("{0}")||'""".format(col.replace('"', '""')) for col in column_names))
65 query_res = cu.execute(q)
66 for row in query_res:
67 yield("{0};".format(row[0]))
68
69 # Now when the type is 'index', 'trigger', or 'view'
70 q = """
71 SELECT "name", "type", "sql"
72 FROM "sqlite_master"
73 WHERE "sql" NOT NULL AND
74 "type" IN ('index', 'trigger', 'view')
75 """
76 schema_res = cu.execute(q)
77 for name, type, sql in schema_res.fetchall():
78 yield('{0};'.format(sql))
79
80 if writeable_schema:
81 yield('PRAGMA writable_schema=OFF;')
82
83 # gh-79009: Yield statements concerning the sqlite_sequence table at the
84 # end of the transaction.
85 for row in sqlite_sequence:
86 yield('{0};'.format(row))
87
88 yield('COMMIT;')