1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-11-06 15:49:35 +03:00

Update the ".table" command in the shell to show all tables in all

attached databases, and to avoid using the deprecated sqlite3_get_table()
function.

FossilOrigin-Name: ce2d06e2533763a8008e7a405630293d8f9a3108
This commit is contained in:
drh
2012-04-23 12:38:05 +00:00
parent 9b8d3572a9
commit 9878123752
3 changed files with 70 additions and 43 deletions

View File

@@ -1,5 +1,5 @@
C If\sterminating\sinteractive\sinput\sto\sthe\scommand-line\sshell\swith\s^D,\sissue\nan\sextra\s\\n\sto\smove\sthe\scursor\sto\sthe\snext\sline\sbefore\sexiting. C Update\sthe\s".table"\scommand\sin\sthe\sshell\sto\sshow\sall\stables\sin\sall\nattached\sdatabases,\sand\sto\savoid\susing\sthe\sdeprecated\ssqlite3_get_table()\nfunction.
D 2012-04-21T11:33:39.769 D 2012-04-23T12:38:05.598
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
F Makefile.in 2f37e468503dbe79d35c9f6dffcf3fae1ae9ec20 F Makefile.in 2f37e468503dbe79d35c9f6dffcf3fae1ae9ec20
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -174,7 +174,7 @@ F src/random.c cd4a67b3953b88019f8cd4ccd81394a8ddfaba50
F src/resolve.c 969ec2bc52db1b068054ecf5ddc74f244102a71d F src/resolve.c 969ec2bc52db1b068054ecf5ddc74f244102a71d
F src/rowset.c f6a49f3e9579428024662f6e2931832511f831a1 F src/rowset.c f6a49f3e9579428024662f6e2931832511f831a1
F src/select.c d7b9018b7dd2e821183d69477ab55c39b8272335 F src/select.c d7b9018b7dd2e821183d69477ab55c39b8272335
F src/shell.c dec1a1896ffa9eaedd6d9907cd43aca4b9d3295d F src/shell.c 151a17fe8464e40097c13672b6f756a38988147a
F src/sqlite.h.in 4338f299fc83dada8407358d585c0e240ecb76a3 F src/sqlite.h.in 4338f299fc83dada8407358d585c0e240ecb76a3
F src/sqlite3ext.h 6904f4aadf976f95241311fbffb00823075d9477 F src/sqlite3ext.h 6904f4aadf976f95241311fbffb00823075d9477
F src/sqliteInt.h c5e917c4f1453f3972b1fd0c81105dfe4f09cc32 F src/sqliteInt.h c5e917c4f1453f3972b1fd0c81105dfe4f09cc32
@@ -995,7 +995,7 @@ F tool/tostr.awk e75472c2f98dd76e06b8c9c1367f4ab07e122d06
F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f
F tool/warnings-clang.sh a8a0a3babda96dfb1ff51adda3cbbf3dfb7266c2 F tool/warnings-clang.sh a8a0a3babda96dfb1ff51adda3cbbf3dfb7266c2
F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381 F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381
P 7b8548b1872cc1225355ba8311e93dd08d6526e2 P feff1ef0b8f7b51ae80a9d34380b46a5103bf6cd
R 6a28d821935f4caf8f00acbe95308ad8 R 684c337b6696cf99874c83d471c520fc
U drh U drh
Z a4a6f8d23b8334a25a8498dbc291bb1a Z 459397b23e497549b544849ef1acdc2b

View File

@@ -1 +1 @@
feff1ef0b8f7b51ae80a9d34380b46a5103bf6cd ce2d06e2533763a8008e7a405630293d8f9a3108

View File

@@ -2248,46 +2248,71 @@ static int do_meta_command(char *zLine, struct callback_data *p){
}else }else
if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 && nArg<3 ){ if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 && nArg<3 ){
sqlite3_stmt *pStmt;
char **azResult; char **azResult;
int nRow; int nRow, nAlloc;
char *zErrMsg; char *zSql = 0;
int ii;
open_db(p); open_db(p);
if( nArg==1 ){ rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
rc = sqlite3_get_table(p->db, if( rc ) return rc;
"SELECT name FROM sqlite_master " zSql = sqlite3_mprintf(
"WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%' " "SELECT name FROM sqlite_master"
"UNION ALL " " WHERE type IN ('table','view')"
"SELECT name FROM sqlite_temp_master " " AND name NOT LIKE 'sqlite_%%'"
"WHERE type IN ('table','view') " " AND name LIKE ?1");
"ORDER BY 1", while( sqlite3_step(pStmt)==SQLITE_ROW ){
&azResult, &nRow, 0, &zErrMsg const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
); if( zDbName==0 || strcmp(zDbName,"main")==0 ) continue;
if( strcmp(zDbName,"temp")==0 ){
zSql = sqlite3_mprintf(
"%z UNION ALL "
"SELECT 'temp.' || name FROM sqlite_temp_master"
" WHERE type IN ('table','view')"
" AND name NOT LIKE 'sqlite_%%'"
" AND name LIKE ?1", zSql);
}else{ }else{
zShellStatic = azArg[1]; zSql = sqlite3_mprintf(
rc = sqlite3_get_table(p->db, "%z UNION ALL "
"SELECT name FROM sqlite_master " "SELECT '%q.' || name FROM \"%w\".sqlite_master"
"WHERE type IN ('table','view') AND name LIKE shellstatic() " " WHERE type IN ('table','view')"
"UNION ALL " " AND name NOT LIKE 'sqlite_%%'"
"SELECT name FROM sqlite_temp_master " " AND name LIKE ?1", zSql, zDbName, zDbName);
"WHERE type IN ('table','view') AND name LIKE shellstatic() "
"ORDER BY 1",
&azResult, &nRow, 0, &zErrMsg
);
zShellStatic = 0;
} }
if( zErrMsg ){ }
fprintf(stderr,"Error: %s\n", zErrMsg); sqlite3_finalize(pStmt);
sqlite3_free(zErrMsg); zSql = sqlite3_mprintf("%z ORDER BY 1", zSql);
rc = 1; rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
}else if( rc != SQLITE_OK ){ sqlite3_free(zSql);
fprintf(stderr,"Error: querying sqlite_master and sqlite_temp_master\n"); if( rc ) return rc;
rc = 1; nRow = nAlloc = 0;
azResult = 0;
if( nArg>1 ){
sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
}else{ }else{
sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
}
while( sqlite3_step(pStmt)==SQLITE_ROW ){
if( nRow>=nAlloc ){
char **azNew;
int n = nAlloc*2 + 10;
azNew = sqlite3_realloc(azResult, sizeof(azResult[0])*n);
if( azNew==0 ){
fprintf(stderr, "Error: out of memory\n");
break;
}
nAlloc = n;
azResult = azNew;
}
azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
if( azResult[nRow] ) nRow++;
}
sqlite3_finalize(pStmt);
if( nRow>0 ){
int len, maxlen = 0; int len, maxlen = 0;
int i, j; int i, j;
int nPrintCol, nPrintRow; int nPrintCol, nPrintRow;
for(i=1; i<=nRow; i++){ for(i=0; i<nRow; i++){
if( azResult[i]==0 ) continue;
len = strlen30(azResult[i]); len = strlen30(azResult[i]);
if( len>maxlen ) maxlen = len; if( len>maxlen ) maxlen = len;
} }
@@ -2295,14 +2320,15 @@ static int do_meta_command(char *zLine, struct callback_data *p){
if( nPrintCol<1 ) nPrintCol = 1; if( nPrintCol<1 ) nPrintCol = 1;
nPrintRow = (nRow + nPrintCol - 1)/nPrintCol; nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
for(i=0; i<nPrintRow; i++){ for(i=0; i<nPrintRow; i++){
for(j=i+1; j<=nRow; j+=nPrintRow){ for(j=i; j<nRow; j+=nPrintRow){
char *zSp = j<=nPrintRow ? "" : " "; char *zSp = j<nPrintRow ? "" : " ";
printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : ""); printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : "");
} }
printf("\n"); printf("\n");
} }
} }
sqlite3_free_table(azResult); for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
sqlite3_free(azResult);
}else }else
if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 && nArg>=2 ){ if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 && nArg>=2 ){
@@ -2437,6 +2463,7 @@ static int do_meta_command(char *zLine, struct callback_data *p){
}else }else
if( c=='t' && strncmp(azArg[0], "trace", n)==0 && nArg>1 ){ if( c=='t' && strncmp(azArg[0], "trace", n)==0 && nArg>1 ){
open_db(p);
output_file_close(p->traceOut); output_file_close(p->traceOut);
p->traceOut = output_file_open(azArg[1]); p->traceOut = output_file_open(azArg[1]);
#ifndef SQLITE_OMIT_TRACE #ifndef SQLITE_OMIT_TRACE