Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(2285)

Side by Side Diff: Lib/sqlite3/dump.py

Issue 15545: sqlite3.Connection.iterdump() does not work with row_factory = sqlite3.Row
Patch Set: Created 9 months, 2 weeks ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | Lib/sqlite3/test/dump.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Mimic the sqlite3 console shell's .dump command 1 # Mimic the sqlite3 console shell's .dump command
2 # Author: Paul Kippes <kippesp@gmail.com> 2 # Author: Paul Kippes <kippesp@gmail.com>
3 3
4 # Every identifier in sql is quoted based on a comment in sqlite 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 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 6 # takes on new features. So to prevent your code from being broken by
7 # future enhancements, you should normally quote any identifier that 7 # future enhancements, you should normally quote any identifier that
8 # is an English language word, even if you do not have to." 8 # is an English language word, even if you do not have to."
9 9
10 def _iterdump(connection): 10 def _iterdump(connection):
11 """ 11 """
12 Returns an iterator to the dump of the database in an SQL text format. 12 Returns an iterator to the dump of the database in an SQL text format.
13 13
14 Used to produce an SQL dump of the database. Useful to save an in-memory 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 15 database for later restoration. This function should not be called
16 directly but instead called from the Connection method, iterdump(). 16 directly but instead called from the Connection method, iterdump().
17 """ 17 """
18 18
19 cu = connection.cursor() 19 cu = connection.cursor()
20 yield('BEGIN TRANSACTION;') 20 yield('BEGIN TRANSACTION;')
21 21
22 # sqlite_master table contains the SQL CREATE statements for the database. 22 # sqlite_master table contains the SQL CREATE statements for the database.
23 q = """ 23 q = """
24 SELECT "name", "type", "sql" 24 SELECT "name", "type", "sql"
25 FROM "sqlite_master" 25 FROM "sqlite_master"
26 WHERE "sql" NOT NULL AND 26 WHERE "sql" NOT NULL AND
27 "type" == 'table' 27 "type" == 'table'
28 ORDER BY "name";
28 """ 29 """
29 schema_res = cu.execute(q) 30 schema_res = cu.execute(q).fetchall()
30 for table_name, type, sql in sorted(schema_res.fetchall()): 31 for table_name, type, sql in schema_res:
31 if table_name == 'sqlite_sequence': 32 if table_name == 'sqlite_sequence':
32 yield('DELETE FROM "sqlite_sequence";') 33 yield('DELETE FROM "sqlite_sequence";')
33 elif table_name == 'sqlite_stat1': 34 elif table_name == 'sqlite_stat1':
34 yield('ANALYZE "sqlite_master";') 35 yield('ANALYZE "sqlite_master";')
35 elif table_name.startswith('sqlite_'): 36 elif table_name.startswith('sqlite_'):
36 continue 37 continue
37 # NOTE: Virtual table support not implemented 38 # NOTE: Virtual table support not implemented
38 #elif sql.startswith('CREATE VIRTUAL TABLE'): 39 #elif sql.startswith('CREATE VIRTUAL TABLE'):
39 # qtable = table_name.replace("'", "''") 40 # qtable = table_name.replace("'", "''")
40 # yield("INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"\ 41 # yield("INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"\
(...skipping 19 matching lines...) Expand all
60 SELECT "name", "type", "sql" 61 SELECT "name", "type", "sql"
61 FROM "sqlite_master" 62 FROM "sqlite_master"
62 WHERE "sql" NOT NULL AND 63 WHERE "sql" NOT NULL AND
63 "type" IN ('index', 'trigger', 'view') 64 "type" IN ('index', 'trigger', 'view')
64 """ 65 """
65 schema_res = cu.execute(q) 66 schema_res = cu.execute(q)
66 for name, type, sql in schema_res.fetchall(): 67 for name, type, sql in schema_res.fetchall():
67 yield('{0};'.format(sql)) 68 yield('{0};'.format(sql))
68 69
69 yield('COMMIT;') 70 yield('COMMIT;')
OLDNEW
« no previous file with comments | « no previous file | Lib/sqlite3/test/dump.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld cbc36f91f3f7