mirror of
https://github.com/sqlite/sqlite.git
synced 2025-11-03 16:53:36 +03:00
Update the .dump command of the command-line shell so that it (1) avoids
putting the semicolon of a DDL statement on the same line as a comment, (2) avoids long expression, even when dumping a table with many columns, and (3) avoids unnecessary quoting of the table name. This fixes tickets [c04a8b8a4f] and [232637c465]. Shell change only; no changes to the SQLite core. FossilOrigin-Name: e6eea8d50d6c307e3e34891758ed4912d368580b
This commit is contained in:
38
src/shell.c
38
src/shell.c
@@ -937,11 +937,14 @@ static char *appendText(char *zIn, char const *zAppend, char quote){
|
||||
|
||||
|
||||
/*
|
||||
** Execute a query statement that has a single result column. Print
|
||||
** that result column on a line by itself with a semicolon terminator.
|
||||
** Execute a query statement that will generate SQL output. Print
|
||||
** the result columns, comma-separated, on a line and then add a
|
||||
** semicolon terminator to the end of that line.
|
||||
**
|
||||
** This is used, for example, to show the schema of the database by
|
||||
** querying the SQLITE_MASTER table.
|
||||
** If the number of columns is 1 and that column contains text "--"
|
||||
** then write the semicolon on a separate line. That way, if a
|
||||
** "--" comment occurs at the end of the statement, the comment
|
||||
** won't consume the semicolon terminator.
|
||||
*/
|
||||
static int run_table_dump_query(
|
||||
struct callback_data *p, /* Query context */
|
||||
@@ -950,6 +953,9 @@ static int run_table_dump_query(
|
||||
){
|
||||
sqlite3_stmt *pSelect;
|
||||
int rc;
|
||||
int nResult;
|
||||
int i;
|
||||
const char *z;
|
||||
rc = sqlite3_prepare(p->db, zSelect, -1, &pSelect, 0);
|
||||
if( rc!=SQLITE_OK || !pSelect ){
|
||||
fprintf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db));
|
||||
@@ -957,12 +963,24 @@ static int run_table_dump_query(
|
||||
return rc;
|
||||
}
|
||||
rc = sqlite3_step(pSelect);
|
||||
nResult = sqlite3_column_count(pSelect);
|
||||
while( rc==SQLITE_ROW ){
|
||||
if( zFirstRow ){
|
||||
fprintf(p->out, "%s", zFirstRow);
|
||||
zFirstRow = 0;
|
||||
}
|
||||
fprintf(p->out, "%s;\n", sqlite3_column_text(pSelect, 0));
|
||||
z = (const char*)sqlite3_column_text(pSelect, 0);
|
||||
fprintf(p->out, "%s", z);
|
||||
for(i=1; i<nResult; i++){
|
||||
fprintf(p->out, ",%s", sqlite3_column_text(pSelect, i));
|
||||
}
|
||||
if( z==0 ) z = "";
|
||||
while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
|
||||
if( z[0] ){
|
||||
fprintf(p->out, "\n;\n");
|
||||
}else{
|
||||
fprintf(p->out, ";\n");
|
||||
}
|
||||
rc = sqlite3_step(pSelect);
|
||||
}
|
||||
rc = sqlite3_finalize(pSelect);
|
||||
@@ -1269,6 +1287,7 @@ static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
|
||||
char *zTableInfo = 0;
|
||||
char *zTmp = 0;
|
||||
int nRow = 0;
|
||||
int kk;
|
||||
|
||||
zTableInfo = appendText(zTableInfo, "PRAGMA table_info(", 0);
|
||||
zTableInfo = appendText(zTableInfo, zTable, '"');
|
||||
@@ -1281,7 +1300,12 @@ static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
|
||||
}
|
||||
|
||||
zSelect = appendText(zSelect, "SELECT 'INSERT INTO ' || ", 0);
|
||||
zTmp = appendText(zTmp, zTable, '"');
|
||||
if( !isalpha(zTable[0]) ){
|
||||
kk = 0;
|
||||
}else{
|
||||
for(kk=1; isalnum(zTable[kk]); kk++){}
|
||||
}
|
||||
zTmp = appendText(zTmp, zTable, zTable[kk] ? '"' : 0);
|
||||
if( zTmp ){
|
||||
zSelect = appendText(zSelect, zTmp, '\'');
|
||||
}
|
||||
@@ -1293,7 +1317,7 @@ static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
|
||||
zSelect = appendText(zSelect, zText, '"');
|
||||
rc = sqlite3_step(pTableInfo);
|
||||
if( rc==SQLITE_ROW ){
|
||||
zSelect = appendText(zSelect, ") || ',' || ", 0);
|
||||
zSelect = appendText(zSelect, "), ", 0);
|
||||
}else{
|
||||
zSelect = appendText(zSelect, ") ", 0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user