]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
In shell, modified "import" handling to ensure error code returned correctly on exit.
authorshane <shane@noemail.net>
Fri, 23 Oct 2009 00:37:15 +0000 (00:37 +0000)
committershane <shane@noemail.net>
Fri, 23 Oct 2009 00:37:15 +0000 (00:37 +0000)
Ticket [bd770b2c52].

FossilOrigin-Name: 009efad0f4293dd08a6f2f16d8eb9e94e2f962ca

manifest
manifest.uuid
src/shell.c

index 12177790e1e2dfd2565442eaf6c241c35676c416..ff36b082a146f7a221d9fd3ca0af8280881afab1 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C In\sshell,\sreworked\s.header\sand\s.echo\shandling.\s\s\s\nUpdated\sshell_exec()\sto\s(really)\shandle\smultiple\sstatements.\nTickets\s[72adc99de9],\s[7b61b6c6ce],\sand\s[eb620916be].
-D 2009-10-22T21:23:35
+C In\sshell,\smodified\s"import"\shandling\sto\sensure\serror\scode\sreturned\scorrectly\son\sexit.\nTicket\s[bd770b2c52].
+D 2009-10-23T00:37:16
 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0
 F Makefile.in a77dfde96ad86aafd3f71651a4333a104debe86a
 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654
@@ -161,7 +161,7 @@ F src/random.c 676b9d7ac820fe81e6fb2394ac8c10cff7f38628
 F src/resolve.c 3ac31c7181fab03732125fdedf7c2091a5c07f1b
 F src/rowset.c c64dafba1f9fd876836c8db8682966b9d197eb1f
 F src/select.c cbe366a0ce114856e66f5daf0f848d7c48a88298
-F src/shell.c 3a2b0649426d75b5cd2937ed90d06b25951a91e4
+F src/shell.c 4b1d54c6deae0b9a9e477a5dbed1e9853f958f91
 F src/sqlite.h.in 5853e42a4066a6c9c3bf6592a9d57d0012bfdb90
 F src/sqlite3ext.h 1db7d63ab5de4b3e6b83dd03d1a4e64fef6d2a17
 F src/sqliteInt.h 3b00a3ce79e60c5a47c342b738c8b75013f3ec84
@@ -760,7 +760,7 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
 F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224
 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
 F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f
-P a024c0a85b6f2288c455a7192f6ca7a8493b621a
-R 3ab26891a50ecbe8d9dce9e787f45010
+P 790402c150e2026cd0c147a4cadbe9b9ab97b688
+R ca40506625a393ee29409c8b893be283
 U shane
-Z 313314dc4aa5889bc53b3806bff86b7f
+Z 5f0f64058678a5e5ac4bd6553c1be92d
index d6cf4ce994d50d871aa4c1ceb21c5f7a979d0616..b7d0685d9438f2610924a032c43e12eb12625b1c 100644 (file)
@@ -1 +1 @@
-790402c150e2026cd0c147a4cadbe9b9ab97b688
\ No newline at end of file
+009efad0f4293dd08a6f2f16d8eb9e94e2f962ca
\ No newline at end of file
index d3eb330c736120415a4f7bd06e1dbf9cfe67144f..6320015ca9f5affa6fd89cef803cc46e9cd79ed1 100644 (file)
@@ -2485,7 +2485,7 @@ static int do_meta_command(char *zLine, struct callback_data *p){
   if( c=='i' && strncmp(azArg[0], "import", n)==0 && nArg>=3 ){
     char *zTable = azArg[2];    /* Insert data into this table */
     char *zFile = azArg[1];     /* The file from which to extract data */
-    sqlite3_stmt *pStmt;        /* A statement */
+    sqlite3_stmt *pStmt = NULL; /* A statement */
     int rc;                     /* Result code */
     int nCol;                   /* Number of columns in the table */
     int nByte;                  /* Number of bytes in an SQL string */
@@ -2501,25 +2501,31 @@ static int do_meta_command(char *zLine, struct callback_data *p){
     open_db(p);
     nSep = strlen30(p->separator);
     if( nSep==0 ){
-      fprintf(stderr, "non-null separator required for import\n");
-      return 0;
+      fprintf(stderr, "Error: non-null separator required for import\n");
+      return 1;
     }
     zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable);
-    if( zSql==0 ) return 0;
+    if( zSql==0 ){
+      fprintf(stderr, "Error: out of memory\n");
+      return 1;
+    }
     nByte = strlen30(zSql);
     rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
     sqlite3_free(zSql);
     if( rc ){
+      if (pStmt) sqlite3_finalize(pStmt);
       fprintf(stderr,"Error: %s\n", sqlite3_errmsg(db));
-      nCol = 0;
-      rc = 1;
-    }else{
-      nCol = sqlite3_column_count(pStmt);
+      return 1;
     }
+    nCol = sqlite3_column_count(pStmt);
     sqlite3_finalize(pStmt);
+    pStmt = 0;
     if( nCol==0 ) return 0;
     zSql = malloc( nByte + 20 + nCol*2 );
-    if( zSql==0 ) return 0;
+    if( zSql==0 ){
+      fprintf(stderr, "Error: out of memory\n");
+      return 1;
+    }
     sqlite3_snprintf(nByte+20, zSql, "INSERT INTO '%q' VALUES(?", zTable);
     j = strlen30(zSql);
     for(i=1; i<nCol; i++){
@@ -2532,19 +2538,21 @@ static int do_meta_command(char *zLine, struct callback_data *p){
     free(zSql);
     if( rc ){
       fprintf(stderr, "Error: %s\n", sqlite3_errmsg(db));
-      sqlite3_finalize(pStmt);
+      if (pStmt) sqlite3_finalize(pStmt);
       return 1;
     }
     in = fopen(zFile, "rb");
     if( in==0 ){
-      fprintf(stderr, "cannot open file: %s\n", zFile);
+      fprintf(stderr, "Error: cannot open file: %s\n", zFile);
       sqlite3_finalize(pStmt);
-      return 0;
+      return 1;
     }
     azCol = malloc( sizeof(azCol[0])*(nCol+1) );
     if( azCol==0 ){
+      fprintf(stderr, "Error: out of memory\n");
       fclose(in);
-      return 0;
+      sqlite3_finalize(pStmt);
+      return 1;
     }
     sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
     zCommit = "COMMIT";
@@ -2562,14 +2570,16 @@ static int do_meta_command(char *zLine, struct callback_data *p){
             z += nSep-1;
           }
         }
-      }
+      } /* end for */
       *z = 0;
       if( i+1!=nCol ){
-        fprintf(stderr,"%s line %d: expected %d columns of data but found %d\n",
-           zFile, lineno, nCol, i+1);
+        fprintf(stderr,
+                "Error: %s line %d: expected %d columns of data but found %d\n",
+                zFile, lineno, nCol, i+1);
         zCommit = "ROLLBACK";
         free(zLine);
-        break;
+        rc = 1;
+        break; /* from while */
       }
       for(i=0; i<nCol; i++){
         sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC);
@@ -2581,9 +2591,9 @@ static int do_meta_command(char *zLine, struct callback_data *p){
         fprintf(stderr,"Error: %s\n", sqlite3_errmsg(db));
         zCommit = "ROLLBACK";
         rc = 1;
-        break;
+        break; /* from while */
       }
-    }
+    } /* end while */
     free(azCol);
     fclose(in);
     sqlite3_finalize(pStmt);
@@ -3051,7 +3061,7 @@ static int process_input(struct callback_data *p, FILE *in){
     if( zLine && zLine[0]=='.' && nSql==0 ){
       if( p->echoOn ) printf("%s\n", zLine);
       rc = do_meta_command(zLine, p);
-      if( rc==2 ){
+      if( rc==2 ){ /* exit requested */
         break;
       }else if( rc ){
         errCnt++;
@@ -3405,8 +3415,8 @@ int main(int argc, char **argv){
     /* Run just the command that follows the database name
     */
     if( zFirstCmd[0]=='.' ){
-      do_meta_command(zFirstCmd, &data);
-      exit(0);
+      rc = do_meta_command(zFirstCmd, &data);
+      exit(rc);
     }else{
       int rc;
       open_db(&data);