Fix the ".dump" command in the shell. Ticket #2072. Also ticket #2065. (CVS 3515)

FossilOrigin-Name: 9fdc249609a4745715a2bf49bbf1376ea243a20a
This commit is contained in:
drh 2006-11-20 16:21:10 +00:00
parent c28e3a1828
commit 45e29d8a1e
3 changed files with 40 additions and 23 deletions

View File

@ -1,5 +1,5 @@
C Make\ssure\sVACUUM\scleans\sup\safter\sitself.\s\sTicket\s#2071.\s(CVS\s3514) C Fix\sthe\s".dump"\scommand\sin\sthe\sshell.\s\sTicket\s#2072.\s\sAlso\sticket\s#2065.\s(CVS\s3515)
D 2006-11-18T20:20:22 D 2006-11-20T16:21:10
F Makefile.in 8e14898d41a53033ecb687d93c9cd5d109fb9ae3 F Makefile.in 8e14898d41a53033ecb687d93c9cd5d109fb9ae3
F Makefile.linux-gcc 2d8574d1ba75f129aba2019f0b959db380a90935 F Makefile.linux-gcc 2d8574d1ba75f129aba2019f0b959db380a90935
F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028 F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028
@ -94,7 +94,7 @@ F src/printf.c b179b6ed12f793e028dd169e2e2e2b2a37eedc63
F src/random.c d40f8d356cecbd351ccfab6eaedd7ec1b54f5261 F src/random.c d40f8d356cecbd351ccfab6eaedd7ec1b54f5261
F src/select.c 6ba6d8ead43d0575ce1f8b418cc039f8f301389a F src/select.c 6ba6d8ead43d0575ce1f8b418cc039f8f301389a
F src/server.c 087b92a39d883e3fa113cae259d64e4c7438bc96 F src/server.c 087b92a39d883e3fa113cae259d64e4c7438bc96
F src/shell.c 41513f31dce4252198944c712b79ea4c92df2592 F src/shell.c 934cdcf67eccee6f5c1be87c98a477a2e263db3c
F src/sqlite.h.in 2931f7ee2415e7a49fd12f386c23575046f0f540 F src/sqlite.h.in 2931f7ee2415e7a49fd12f386c23575046f0f540
F src/sqlite3ext.h 2c2156cc32a158e2b7bd9042d42accf94bff2e40 F src/sqlite3ext.h 2c2156cc32a158e2b7bd9042d42accf94bff2e40
F src/sqliteInt.h f6bac44ee7b8ee2614b046974551c22999ec674f F src/sqliteInt.h f6bac44ee7b8ee2614b046974551c22999ec674f
@ -421,7 +421,7 @@ F www/tclsqlite.tcl bb0d1357328a42b1993d78573e587c6dcbc964b9
F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0 F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0
F www/version3.tcl 890248cf7b70e60c383b0e84d77d5132b3ead42b F www/version3.tcl 890248cf7b70e60c383b0e84d77d5132b3ead42b
F www/whentouse.tcl 97e2b5cd296f7d8057e11f44427dea8a4c2db513 F www/whentouse.tcl 97e2b5cd296f7d8057e11f44427dea8a4c2db513
P f6e0b080dcfaf554b2c05df5e7d4db69d012fba3 P 2fdc147d0059dcdfff2da33bd9fedb0bee057aa1
R ad774b2735ecc59c61fa373df002cbdb R 794dc2fbb3e0e83e5207db81c11bb327
U drh U drh
Z bebd3b34dccaadcb360a71274b94137f Z 79a147f5026204797ae394029f543a2e

View File

@ -1 +1 @@
2fdc147d0059dcdfff2da33bd9fedb0bee057aa1 9fdc249609a4745715a2bf49bbf1376ea243a20a

View File

@ -12,7 +12,7 @@
** This file contains code to implement the "sqlite" command line ** This file contains code to implement the "sqlite" command line
** utility for accessing SQLite databases. ** utility for accessing SQLite databases.
** **
** $Id: shell.c,v 1.155 2006/11/08 12:25:43 drh Exp $ ** $Id: shell.c,v 1.156 2006/11/20 16:21:10 drh Exp $
*/ */
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -225,6 +225,7 @@ struct previous_mode_data {
int showHeader; int showHeader;
int colWidth[100]; int colWidth[100];
}; };
/* /*
** An pointer to an instance of this structure is passed from ** An pointer to an instance of this structure is passed from
** the main program to the callback. This is used to communicate ** the main program to the callback. This is used to communicate
@ -236,6 +237,7 @@ struct callback_data {
int cnt; /* Number of records displayed so far */ int cnt; /* Number of records displayed so far */
FILE *out; /* Write results here */ FILE *out; /* Write results here */
int mode; /* An output mode setting */ int mode; /* An output mode setting */
int writableSchema; /* True if PRAGMA writable_schema=ON */
int showHeader; /* True to show column names in List or Column mode */ int showHeader; /* True to show column names in List or Column mode */
char *zDestTable; /* Name of destination table when MODE_Insert */ char *zDestTable; /* Name of destination table when MODE_Insert */
char separator[20]; /* Separator character for MODE_List */ char separator[20]; /* Separator character for MODE_List */
@ -675,6 +677,9 @@ static char *appendText(char *zIn, char const *zAppend, char quote){
/* /*
** Execute a query statement that has a single result column. Print ** Execute a query statement that has a single result column. Print
** that result column on a line by itself with a semicolon terminator. ** that result column on a line by itself with a semicolon terminator.
**
** This is used, for example, to show the schema of the database by
** querying the SQLITE_MASTER table.
*/ */
static int run_table_dump_query(FILE *out, sqlite3 *db, const char *zSelect){ static int run_table_dump_query(FILE *out, sqlite3 *db, const char *zSelect){
sqlite3_stmt *pSelect; sqlite3_stmt *pSelect;
@ -716,6 +721,19 @@ static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
fprintf(p->out, "ANALYZE sqlite_master;\n"); fprintf(p->out, "ANALYZE sqlite_master;\n");
}else if( strncmp(zTable, "sqlite_", 7)==0 ){ }else if( strncmp(zTable, "sqlite_", 7)==0 ){
return 0; return 0;
}else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
char *zIns;
if( !p->writableSchema ){
fprintf(p->out, "PRAGMA writable_schema=ON;\n");
p->writableSchema = 1;
}
zIns = sqlite3_mprintf(
"INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
"VALUES('table','%q','%q',0,'%q');",
zTable, zTable, zSql);
fprintf(p->out, "%s\n", zIns);
sqlite3_free(zIns);
return 0;
}else{ }else{
fprintf(p->out, "%s;\n", zSql); fprintf(p->out, "%s;\n", zSql);
} }
@ -773,7 +791,9 @@ static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
} }
/* /*
** Run zQuery. Update dump_callback() as the callback routine. ** Run zQuery. Use dump_callback() as the callback routine so that
** the contents of the query are output as SQL statements.
**
** If we get a SQLITE_CORRUPT error, rerun the query after appending ** If we get a SQLITE_CORRUPT error, rerun the query after appending
** "ORDER BY rowid DESC" to the end. ** "ORDER BY rowid DESC" to the end.
*/ */
@ -979,19 +999,15 @@ static int do_meta_command(char *zLine, struct callback_data *p){
char *zErrMsg = 0; char *zErrMsg = 0;
open_db(p); open_db(p);
fprintf(p->out, "BEGIN TRANSACTION;\n"); fprintf(p->out, "BEGIN TRANSACTION;\n");
p->writableSchema = 0;
if( nArg==1 ){ if( nArg==1 ){
run_schema_dump_query(p, run_schema_dump_query(p,
"SELECT name, type, sql FROM sqlite_master " "SELECT name, type, sql FROM sqlite_master "
"WHERE sql NOT NULL AND type=='table' AND rootpage!=0", 0 "WHERE sql NOT NULL AND type=='table'", 0
);
run_schema_dump_query(p,
"SELECT name, type, sql FROM sqlite_master "
"WHERE sql NOT NULL AND "
" AND type!='table' AND type!='meta'", 0
); );
run_table_dump_query(p->out, p->db, run_table_dump_query(p->out, p->db,
"SELECT sql FROM sqlite_master " "SELECT sql FROM sqlite_master "
"WHERE sql NOT NULL AND rootpage==0" "WHERE sql NOT NULL AND type IN ('index','trigger','view')"
); );
}else{ }else{
int i; int i;
@ -1000,19 +1016,20 @@ static int do_meta_command(char *zLine, struct callback_data *p){
run_schema_dump_query(p, run_schema_dump_query(p,
"SELECT name, type, sql FROM sqlite_master " "SELECT name, type, sql FROM sqlite_master "
"WHERE tbl_name LIKE shellstatic() AND type=='table'" "WHERE tbl_name LIKE shellstatic() AND type=='table'"
" AND rootpage!=0 AND sql NOT NULL", 0); " AND sql NOT NULL", 0);
run_schema_dump_query(p,
"SELECT name, type, sql FROM sqlite_master "
"WHERE tbl_name LIKE shellstatic() AND type!='table'"
" AND type!='meta' AND sql NOT NULL", 0);
run_table_dump_query(p->out, p->db, run_table_dump_query(p->out, p->db,
"SELECT sql FROM sqlite_master " "SELECT sql FROM sqlite_master "
"WHERE sql NOT NULL AND rootpage==0" "WHERE sql NOT NULL"
" AND type IN ('index','trigger','view')"
" AND tbl_name LIKE shellstatic()" " AND tbl_name LIKE shellstatic()"
); );
zShellStatic = 0; zShellStatic = 0;
} }
} }
if( p->writableSchema ){
fprintf(p->out, "PRAGMA writable_schema=OFF;\n");
p->writableSchema = 0;
}
if( zErrMsg ){ if( zErrMsg ){
fprintf(stderr,"Error: %s\n", zErrMsg); fprintf(stderr,"Error: %s\n", zErrMsg);
sqlite3_free(zErrMsg); sqlite3_free(zErrMsg);