/ Check-in [009efad0]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:In shell, modified "import" handling to ensure error code returned correctly on exit. Ticket [bd770b2c52].
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 009efad0f4293dd08a6f2f16d8eb9e94e2f962ca
User & Date: shane 2009-10-23 00:37:16
References
2009-10-23
00:37 Fixed ticket [bd770b2c]: shell doesn't return error exit code on import errors plus 3 other changes artifact: aeaae621 user: shane
Context
2009-10-23
01:27
In shell, ensure that do_meta_command() returns consistent error values. Adjusted the text of some error message to be more consistent. Ticket [beb2dd69ad]. check-in: 1ebac9ed user: shane tags: trunk
00:37
In shell, modified "import" handling to ensure error code returned correctly on exit. Ticket [bd770b2c52]. check-in: 009efad0 user: shane tags: trunk
2009-10-22
21:23
In shell, reworked .header and .echo handling. Updated shell_exec() to (really) handle multiple statements. Tickets [72adc99de9], [7b61b6c6ce], and [eb620916be]. check-in: 790402c1 user: shane tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/shell.c.

  2481   2481         fprintf(stderr,"%s",zTimerHelp);
  2482   2482       }
  2483   2483     }else
  2484   2484   
  2485   2485     if( c=='i' && strncmp(azArg[0], "import", n)==0 && nArg>=3 ){
  2486   2486       char *zTable = azArg[2];    /* Insert data into this table */
  2487   2487       char *zFile = azArg[1];     /* The file from which to extract data */
  2488         -    sqlite3_stmt *pStmt;        /* A statement */
         2488  +    sqlite3_stmt *pStmt = NULL; /* A statement */
  2489   2489       int rc;                     /* Result code */
  2490   2490       int nCol;                   /* Number of columns in the table */
  2491   2491       int nByte;                  /* Number of bytes in an SQL string */
  2492   2492       int i, j;                   /* Loop counters */
  2493   2493       int nSep;                   /* Number of bytes in p->separator[] */
  2494   2494       char *zSql;                 /* An SQL statement */
  2495   2495       char *zLine;                /* A single line of input from the file */
................................................................................
  2497   2497       char *zCommit;              /* How to commit changes */   
  2498   2498       FILE *in;                   /* The input file */
  2499   2499       int lineno = 0;             /* Line number of input file */
  2500   2500   
  2501   2501       open_db(p);
  2502   2502       nSep = strlen30(p->separator);
  2503   2503       if( nSep==0 ){
  2504         -      fprintf(stderr, "non-null separator required for import\n");
  2505         -      return 0;
         2504  +      fprintf(stderr, "Error: non-null separator required for import\n");
         2505  +      return 1;
  2506   2506       }
  2507   2507       zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable);
  2508         -    if( zSql==0 ) return 0;
         2508  +    if( zSql==0 ){
         2509  +      fprintf(stderr, "Error: out of memory\n");
         2510  +      return 1;
         2511  +    }
  2509   2512       nByte = strlen30(zSql);
  2510   2513       rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
  2511   2514       sqlite3_free(zSql);
  2512   2515       if( rc ){
         2516  +      if (pStmt) sqlite3_finalize(pStmt);
  2513   2517         fprintf(stderr,"Error: %s\n", sqlite3_errmsg(db));
  2514         -      nCol = 0;
  2515         -      rc = 1;
  2516         -    }else{
  2517         -      nCol = sqlite3_column_count(pStmt);
         2518  +      return 1;
  2518   2519       }
         2520  +    nCol = sqlite3_column_count(pStmt);
  2519   2521       sqlite3_finalize(pStmt);
         2522  +    pStmt = 0;
  2520   2523       if( nCol==0 ) return 0;
  2521   2524       zSql = malloc( nByte + 20 + nCol*2 );
  2522         -    if( zSql==0 ) return 0;
         2525  +    if( zSql==0 ){
         2526  +      fprintf(stderr, "Error: out of memory\n");
         2527  +      return 1;
         2528  +    }
  2523   2529       sqlite3_snprintf(nByte+20, zSql, "INSERT INTO '%q' VALUES(?", zTable);
  2524   2530       j = strlen30(zSql);
  2525   2531       for(i=1; i<nCol; i++){
  2526   2532         zSql[j++] = ',';
  2527   2533         zSql[j++] = '?';
  2528   2534       }
  2529   2535       zSql[j++] = ')';
  2530   2536       zSql[j] = 0;
  2531   2537       rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
  2532   2538       free(zSql);
  2533   2539       if( rc ){
  2534   2540         fprintf(stderr, "Error: %s\n", sqlite3_errmsg(db));
  2535         -      sqlite3_finalize(pStmt);
         2541  +      if (pStmt) sqlite3_finalize(pStmt);
  2536   2542         return 1;
  2537   2543       }
  2538   2544       in = fopen(zFile, "rb");
  2539   2545       if( in==0 ){
  2540         -      fprintf(stderr, "cannot open file: %s\n", zFile);
         2546  +      fprintf(stderr, "Error: cannot open file: %s\n", zFile);
  2541   2547         sqlite3_finalize(pStmt);
  2542         -      return 0;
         2548  +      return 1;
  2543   2549       }
  2544   2550       azCol = malloc( sizeof(azCol[0])*(nCol+1) );
  2545   2551       if( azCol==0 ){
         2552  +      fprintf(stderr, "Error: out of memory\n");
  2546   2553         fclose(in);
  2547         -      return 0;
         2554  +      sqlite3_finalize(pStmt);
         2555  +      return 1;
  2548   2556       }
  2549   2557       sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
  2550   2558       zCommit = "COMMIT";
  2551   2559       while( (zLine = local_getline(0, in))!=0 ){
  2552   2560         char *z;
  2553   2561         i = 0;
  2554   2562         lineno++;
................................................................................
  2558   2566             *z = 0;
  2559   2567             i++;
  2560   2568             if( i<nCol ){
  2561   2569               azCol[i] = &z[nSep];
  2562   2570               z += nSep-1;
  2563   2571             }
  2564   2572           }
  2565         -      }
         2573  +      } /* end for */
  2566   2574         *z = 0;
  2567   2575         if( i+1!=nCol ){
  2568         -        fprintf(stderr,"%s line %d: expected %d columns of data but found %d\n",
  2569         -           zFile, lineno, nCol, i+1);
         2576  +        fprintf(stderr,
         2577  +                "Error: %s line %d: expected %d columns of data but found %d\n",
         2578  +                zFile, lineno, nCol, i+1);
  2570   2579           zCommit = "ROLLBACK";
  2571   2580           free(zLine);
  2572         -        break;
         2581  +        rc = 1;
         2582  +        break; /* from while */
  2573   2583         }
  2574   2584         for(i=0; i<nCol; i++){
  2575   2585           sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC);
  2576   2586         }
  2577   2587         sqlite3_step(pStmt);
  2578   2588         rc = sqlite3_reset(pStmt);
  2579   2589         free(zLine);
  2580   2590         if( rc!=SQLITE_OK ){
  2581   2591           fprintf(stderr,"Error: %s\n", sqlite3_errmsg(db));
  2582   2592           zCommit = "ROLLBACK";
  2583   2593           rc = 1;
  2584         -        break;
         2594  +        break; /* from while */
  2585   2595         }
  2586         -    }
         2596  +    } /* end while */
  2587   2597       free(azCol);
  2588   2598       fclose(in);
  2589   2599       sqlite3_finalize(pStmt);
  2590   2600       sqlite3_exec(p->db, zCommit, 0, 0, 0);
  2591   2601     }else
  2592   2602   
  2593   2603     if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg>1 ){
................................................................................
  3047   3057         seenInterrupt = 0;
  3048   3058       }
  3049   3059       lineno++;
  3050   3060       if( (zSql==0 || zSql[0]==0) && _all_whitespace(zLine) ) continue;
  3051   3061       if( zLine && zLine[0]=='.' && nSql==0 ){
  3052   3062         if( p->echoOn ) printf("%s\n", zLine);
  3053   3063         rc = do_meta_command(zLine, p);
  3054         -      if( rc==2 ){
         3064  +      if( rc==2 ){ /* exit requested */
  3055   3065           break;
  3056   3066         }else if( rc ){
  3057   3067           errCnt++;
  3058   3068         }
  3059   3069         continue;
  3060   3070       }
  3061   3071       if( _is_command_terminator(zLine) && _is_complete(zSql, nSql) ){
................................................................................
  3401   3411       }
  3402   3412     }
  3403   3413   
  3404   3414     if( zFirstCmd ){
  3405   3415       /* Run just the command that follows the database name
  3406   3416       */
  3407   3417       if( zFirstCmd[0]=='.' ){
  3408         -      do_meta_command(zFirstCmd, &data);
  3409         -      exit(0);
         3418  +      rc = do_meta_command(zFirstCmd, &data);
         3419  +      exit(rc);
  3410   3420       }else{
  3411   3421         int rc;
  3412   3422         open_db(&data);
  3413   3423         rc = shell_exec(data.db, zFirstCmd, shell_callback, &data, &zErrMsg);
  3414   3424         if( rc!=0 && zErrMsg!=0 ){
  3415   3425           fprintf(stderr,"SQL error: %s\n", zErrMsg);
  3416   3426           exit(1);