]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Improve error handling in shell command ".tables".
authordan <dan@noemail.net>
Wed, 30 Sep 2015 11:19:05 +0000 (11:19 +0000)
committerdan <dan@noemail.net>
Wed, 30 Sep 2015 11:19:05 +0000 (11:19 +0000)
FossilOrigin-Name: 31a91ee7d32af8580a170903eb857ed9222fdb0a

manifest
manifest.uuid
src/shell.c

index cbc2fc7692db82e6b9c6f67ca172b534bfa01a60..0014c03a25732cdcbe5410591279128ace5a3212 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Use\ssymbolic\snames\sXN_ROWID\sand\sXN_EXPR\sin\splace\sof\sthe\s(-1)\sand\s(-2)\nmagic\snumbers\sin\sIndex.aiColumn[].\s\sAdd\sasserts\sto\shelp\sverify\sthat\nIndex.aiColumn[]\sis\salways\sused\scorrectly.\s\sFix\sone\splace\sin\sFK\sprocessing\nwhere\sIndex.aiColumn[]\swas\snot\sbeing\sused\scorrectly.
-D 2015-09-29T17:20:14.958
+C Improve\serror\shandling\sin\sshell\scommand\s".tables".
+D 2015-09-30T11:19:05.250
 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
 F Makefile.in 2143eeef6d0cc26006ae5fc4bb242a4a8b973412
 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -341,7 +341,7 @@ F src/random.c ba2679f80ec82c4190062d756f22d0c358180696
 F src/resolve.c 1954a0f01bf65d78d7d559aea3d5c67f33376d91
 F src/rowset.c eccf6af6d620aaa4579bd3b72c1b6395d9e9fa1e
 F src/select.c e49f4af9748c9e0cc1bf864b4190aa94841c8409
-F src/shell.c a11b20da4c6630e0e8f83c47ce36f717dd0422f0
+F src/shell.c dfd18e56fcb88dd79378774e9bc3246a9bd52243
 F src/sqlite.h.in 4b76d74d69af48c534c58fb723137dc6944bdedc
 F src/sqlite3.rc 992c9f5fb8285ae285d6be28240a7e8d3a7f2bad
 F src/sqlite3ext.h 64350bf36833a56ad675e27392a913f417c5c308
@@ -1389,7 +1389,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1
 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
 F tool/warnings.sh 48bd54594752d5be3337f12c72f28d2080cb630b
 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
-P a1d08fd3d0419da8c22355d48c6d83eed6fd7e07
-R 52625b1830d3c09e89f8505fdc133a11
-U drh
-Z 6b238da064de6e3b5f5d0b4365b7caf0
+P 7d272aa62cd4cbbf4b5d04e3b918de27671e8301
+R 182210397719a41b40c126f91ca11c51
+U dan
+Z f61744228c664ef1bbc36bc13dfb9d49
index 5810842471157f98c9675173ae3557a8ba805cf1..d393ee659676d483cf449924c8492841720b0b23 100644 (file)
@@ -1 +1 @@
-7d272aa62cd4cbbf4b5d04e3b918de27671e8301
\ No newline at end of file
+31a91ee7d32af8580a170903eb857ed9222fdb0a
\ No newline at end of file
index 02938eb1e6a7be4edf8cbfabdba7b5f1074f6fea..96d0ab65f7a4673f32813c6bd3b29b416be19ffa 100644 (file)
@@ -2612,6 +2612,22 @@ static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
   return 0;
 }
 
+/*
+** Print the current sqlite3_errmsg() value to stderr and return 1.
+*/
+static int shellDatabaseError(sqlite3 *db){
+  const char *zErr = sqlite3_errmsg(db);
+  fprintf(stderr, "Error: %s\n", zErr);
+  return 1;
+}
+
+/*
+** Print an out-of-memory message to stderr and return 1.
+*/
+static int shellNomemError(void){
+  fprintf(stderr, "Error: out of memory\n");
+  return 1;
+}
 
 /*
 ** If an input line begins with "." then invoke this routine to
@@ -3713,13 +3729,17 @@ static int do_meta_command(char *zLine, ShellState *p){
     int ii;
     open_db(p, 0);
     rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
-    if( rc ) return rc;
+    if( rc ) return shellDatabaseError(p->db);
+
+    /* Create an SQL statement to query for the list of tables in the
+    ** main and all attached databases where the table name matches the
+    ** LIKE pattern bound to variable "?1". */
     zSql = sqlite3_mprintf(
         "SELECT name FROM sqlite_master"
         " WHERE type IN ('table','view')"
         "   AND name NOT LIKE 'sqlite_%%'"
         "   AND name LIKE ?1");
-    while( sqlite3_step(pStmt)==SQLITE_ROW ){
+    while( zSql && sqlite3_step(pStmt)==SQLITE_ROW ){
       const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
       if( zDbName==0 || strcmp(zDbName,"main")==0 ) continue;
       if( strcmp(zDbName,"temp")==0 ){
@@ -3738,11 +3758,17 @@ static int do_meta_command(char *zLine, ShellState *p){
                  "   AND name LIKE ?1", zSql, zDbName, zDbName);
       }
     }
-    sqlite3_finalize(pStmt);
-    zSql = sqlite3_mprintf("%z ORDER BY 1", zSql);
-    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
+    rc = sqlite3_finalize(pStmt);
+    if( zSql && rc==SQLITE_OK ){
+      zSql = sqlite3_mprintf("%z ORDER BY 1", zSql);
+      if( zSql ) rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
+    }
     sqlite3_free(zSql);
-    if( rc ) return rc;
+    if( !zSql ) return shellNomemError();
+    if( rc ) return shellDatabaseError(p->db);
+
+    /* Run the SQL statement prepared by the above block. Store the results
+    ** as an array of nul-terminated strings in azResult[].  */
     nRow = nAlloc = 0;
     azResult = 0;
     if( nArg>1 ){
@@ -3756,17 +3782,25 @@ static int do_meta_command(char *zLine, ShellState *p){
         int n2 = nAlloc*2 + 10;
         azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
         if( azNew==0 ){
-          fprintf(stderr, "Error: out of memory\n");
+          rc = shellNomemError();
           break;
         }
         nAlloc = n2;
         azResult = azNew;
       }
       azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
-      if( azResult[nRow] ) nRow++;
+      if( 0==azResult[nRow] ){
+        rc = shellNomemError();
+        break;
+      }
+      nRow++;
+    }
+    if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
+      rc = shellDatabaseError(p->db);
     }
-    sqlite3_finalize(pStmt);        
-    if( nRow>0 ){
+
+    /* Pretty-print the contents of array azResult[] to the output */
+    if( rc==0 && nRow>0 ){
       int len, maxlen = 0;
       int i, j;
       int nPrintCol, nPrintRow;
@@ -3785,6 +3819,7 @@ static int do_meta_command(char *zLine, ShellState *p){
         fprintf(p->out, "\n");
       }
     }
+
     for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
     sqlite3_free(azResult);
   }else