blob: de9c368be3014e7e55f56a69457a003854381629 [file] [log] [blame]
Gerhard Häringb1b93822008-03-29 00:41:18 +00001# Mimic the sqlite3 console shell's .dump command
2# Author: Paul Kippes <kippesp@gmail.com>
3
Petri Lehtinen51d04d12012-02-12 21:05:31 +02004# 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
Gerhard Häringb1b93822008-03-29 00:41:18 +000010def _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 cu = connection.cursor()
20 yield('BEGIN TRANSACTION;')
21
22 # sqlite_master table contains the SQL CREATE statements for the database.
23 q = """
Petri Lehtinen51d04d12012-02-12 21:05:31 +020024 SELECT "name", "type", "sql"
25 FROM "sqlite_master"
26 WHERE "sql" NOT NULL AND
27 "type" == 'table'
R David Murrayb5231292013-01-10 11:04:09 -050028 ORDER BY "name"
Gerhard Häringb1b93822008-03-29 00:41:18 +000029 """
30 schema_res = cu.execute(q)
R David Murrayb5231292013-01-10 11:04:09 -050031 for table_name, type, sql in schema_res.fetchall():
Gerhard Häringb1b93822008-03-29 00:41:18 +000032 if table_name == 'sqlite_sequence':
Petri Lehtinen51d04d12012-02-12 21:05:31 +020033 yield('DELETE FROM "sqlite_sequence";')
Gerhard Häringb1b93822008-03-29 00:41:18 +000034 elif table_name == 'sqlite_stat1':
Petri Lehtinen51d04d12012-02-12 21:05:31 +020035 yield('ANALYZE "sqlite_master";')
Gerhard Häringb1b93822008-03-29 00:41:18 +000036 elif table_name.startswith('sqlite_'):
37 continue
38 # NOTE: Virtual table support not implemented
39 #elif sql.startswith('CREATE VIRTUAL TABLE'):
40 # qtable = table_name.replace("'", "''")
41 # yield("INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"\
Petri Lehtinen51d04d12012-02-12 21:05:31 +020042 # "VALUES('table','{0}','{0}',0,'{1}');".format(
Gerhard Häringb1b93822008-03-29 00:41:18 +000043 # qtable,
Petri Lehtinen51d04d12012-02-12 21:05:31 +020044 # sql.replace("''")))
Gerhard Häringb1b93822008-03-29 00:41:18 +000045 else:
Petri Lehtinen51d04d12012-02-12 21:05:31 +020046 yield('{0};'.format(sql))
Gerhard Häringb1b93822008-03-29 00:41:18 +000047
48 # Build the insert statement for each row of the current table
Petri Lehtinen51d04d12012-02-12 21:05:31 +020049 table_name_ident = table_name.replace('"', '""')
50 res = cu.execute('PRAGMA table_info("{0}")'.format(table_name_ident))
Gerhard Häringb1b93822008-03-29 00:41:18 +000051 column_names = [str(table_info[1]) for table_info in res.fetchall()]
Petri Lehtinen51d04d12012-02-12 21:05:31 +020052 q = """SELECT 'INSERT INTO "{0}" VALUES({1})' FROM "{0}";""".format(
53 table_name_ident,
54 ",".join("""'||quote("{0}")||'""".format(col.replace('"', '""')) for col in column_names))
55 query_res = cu.execute(q)
Gerhard Häringb1b93822008-03-29 00:41:18 +000056 for row in query_res:
Petri Lehtinen51d04d12012-02-12 21:05:31 +020057 yield("{0};".format(row[0]))
Gerhard Häringb1b93822008-03-29 00:41:18 +000058
59 # Now when the type is 'index', 'trigger', or 'view'
60 q = """
Petri Lehtinen51d04d12012-02-12 21:05:31 +020061 SELECT "name", "type", "sql"
62 FROM "sqlite_master"
63 WHERE "sql" NOT NULL AND
64 "type" IN ('index', 'trigger', 'view')
Gerhard Häringb1b93822008-03-29 00:41:18 +000065 """
66 schema_res = cu.execute(q)
67 for name, type, sql in schema_res.fetchall():
Petri Lehtinen51d04d12012-02-12 21:05:31 +020068 yield('{0};'.format(sql))
Gerhard Häringb1b93822008-03-29 00:41:18 +000069
70 yield('COMMIT;')