Skip to content

Commit

Permalink
Improved name quoting and escaping in the auxiliary column info section
Browse files Browse the repository at this point in the history
of the ".schema" output for views and virtual tables.
  • Loading branch information
D. Richard Hipp committed Jan 1, 2018
1 parent 8402a9a commit 03ad833
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
34 changes: 25 additions & 9 deletions src/shell.c.in
Original file line number Diff line number Diff line change
Expand Up @@ -741,19 +741,34 @@ static char *shellFakeCrTab(
){
sqlite3_stmt *pStmt = 0;
char *zSql;
char *z = 0;
ShellText s;
char cQuote;
char *zDiv = "(";

zSql = sqlite3_mprintf("SELECT group_concat(name,',')"
" FROM pragma_table_info(%Q,%Q);",
zName, zSchema);
zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
zSchema ? zSchema : "main", zName);
sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
sqlite3_free(zSql);
if( sqlite3_step(pStmt)==SQLITE_ROW ){
z = sqlite3_mprintf("/* %s.%s(%s) */",
zSchema ? zSchema : "main", zName, sqlite3_column_text(pStmt, 0));
initText(&s);
if( zSchema ){
cQuote = quoteChar(zSchema);
if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
appendText(&s, zSchema, cQuote);
appendText(&s, ".", 0);
}
cQuote = quoteChar(zName);
appendText(&s, zName, cQuote);
while( sqlite3_step(pStmt)==SQLITE_ROW ){
const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
appendText(&s, zDiv, 0);
zDiv = ",";
cQuote = quoteChar(zCol);
appendText(&s, zCol, cQuote);
}
appendText(&s, ")", 0);

sqlite3_finalize(pStmt);
return z;
return s.z;
}

/*
Expand Down Expand Up @@ -808,7 +823,8 @@ static void shellAddSchemaName(
if( aPrefix[i][0]=='V' ){
const char *zName = (const char*)sqlite3_value_text(apVal[2]);
if( z==0 ) z = sqlite3_mprintf("%s", zIn);
z = sqlite3_mprintf("%z\n%z", z, shellFakeCrTab(db, zSchema, zName));
z = sqlite3_mprintf("%z\n/* %z */", z,
shellFakeCrTab(db, zSchema, zName));
}
if( z ){
sqlite3_result_text(pCtx, z, -1, sqlite3_free);
Expand Down
4 changes: 2 additions & 2 deletions test/shell1.test
Original file line number Diff line number Diff line change
Expand Up @@ -582,9 +582,9 @@ do_test shell1-3.21.4 {
catchcmd "test.db" ".schema"
} {0 {CREATE TABLE t1(x);
CREATE VIEW v2 AS SELECT x+1 AS y FROM t1
/* main.v2(y) */;
/* v2(y) */;
CREATE VIEW v1 AS SELECT y+1 FROM v2
/* main.v1(y+1) */;}}
/* v1("y+1") */;}}
db eval {DROP VIEW v1; DROP VIEW v2; DROP TABLE t1;}
}

Expand Down

0 comments on commit 03ad833

Please sign in to comment.