| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 84 |
|
| 85 |
for row in sqlite_sequence: |
| 86 |
yield('{0};'.format(row)) |
| 87 |
|
| 88 |
yield('COMMIT;') |