/ Check-in [a71e2a72c5]
Login

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

Overview
Comment:Add the ".open" command to the command-line shell. Cherrypick from [21eccb919441].
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | branch-3.7.11
Files: files | file ages | folders
SHA1: a71e2a72c592f014051a00af9c7cb5c390612cb2
User & Date: drh 2015-05-21 00:50:37
Context
2015-05-21
01:04
Do not allow virtual table constructors to be called recursively. Cherrypick [0a72726da21581ab] check-in: 0f0694e424 user: drh tags: branch-3.7.11
00:50
Add the ".open" command to the command-line shell. Cherrypick from [21eccb919441]. check-in: a71e2a72c5 user: drh tags: branch-3.7.11
2013-10-24
15:20
Add the ".open" command to the command-line shell. check-in: 21eccb9194 user: drh tags: trunk
2012-03-20
11:35
Version 3.7.11 check-in: 00bb9c9ce4 user: drh tags: trunk, release, version-3.7.11
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/shell.c.

   430    430     char nullvalue[20];    /* The text to print when a NULL comes back from
   431    431                            ** the database */
   432    432     struct previous_mode_data explainPrev;
   433    433                            /* Holds the mode information just before
   434    434                            ** .explain ON */
   435    435     char outfile[FILENAME_MAX]; /* Filename for *out */
   436    436     const char *zDbFilename;    /* name of the database file */
          437  +  char *zFreeOnClose;         /* Filename to free when closing */
   437    438     const char *zVfs;           /* Name of VFS to use */
   438    439     sqlite3_stmt *pStmt;   /* Current statement if any. */
   439    440     FILE *pLog;            /* Write log output here */
   440    441   };
   441    442   
   442    443   /*
   443    444   ** These are the allowed modes.
................................................................................
  1412   1413     "                         html     HTML <table> code\n"
  1413   1414     "                         insert   SQL insert statements for TABLE\n"
  1414   1415     "                         line     One value per line\n"
  1415   1416     "                         list     Values delimited by .separator string\n"
  1416   1417     "                         tabs     Tab-separated values\n"
  1417   1418     "                         tcl      TCL list elements\n"
  1418   1419     ".nullvalue STRING      Print STRING in place of NULL values\n"
         1420  +  ".open ?FILENAME?       Close existing database and reopen FILENAME\n"
  1419   1421     ".output FILENAME       Send output to FILENAME\n"
  1420   1422     ".output stdout         Send output to the screen\n"
  1421   1423     ".prompt MAIN CONTINUE  Replace the standard prompts\n"
  1422   1424     ".quit                  Exit this program\n"
  1423   1425     ".read FILENAME         Execute SQL in FILENAME\n"
  1424   1426     ".restore ?DB? FILE     Restore content of DB (default \"main\") from FILE\n"
  1425   1427     ".schema ?TABLE?        Show the CREATE statements\n"
................................................................................
  1443   1445   /* Forward reference */
  1444   1446   static int process_input(struct callback_data *p, FILE *in);
  1445   1447   
  1446   1448   /*
  1447   1449   ** Make sure the database is open.  If it is not, then open it.  If
  1448   1450   ** the database fails to open, print an error message and exit.
  1449   1451   */
  1450         -static void open_db(struct callback_data *p){
         1452  +static void open_db(struct callback_data *p, int keepAlive){
  1451   1453     if( p->db==0 ){
  1452   1454       sqlite3_open(p->zDbFilename, &p->db);
  1453   1455       db = p->db;
  1454   1456       if( db && sqlite3_errcode(db)==SQLITE_OK ){
  1455   1457         sqlite3_create_function(db, "shellstatic", 0, SQLITE_UTF8, 0,
  1456   1458             shellstaticFunc, 0, 0);
  1457   1459       }
  1458   1460       if( db==0 || SQLITE_OK!=sqlite3_errcode(db) ){
  1459   1461         fprintf(stderr,"Error: unable to open database \"%s\": %s\n", 
  1460   1462             p->zDbFilename, sqlite3_errmsg(db));
         1463  +      if( keepAlive ) return;
  1461   1464         exit(1);
  1462   1465       }
  1463   1466   #ifndef SQLITE_OMIT_LOAD_EXTENSION
  1464   1467       sqlite3_enable_load_extension(p->db, 1);
  1465   1468   #endif
  1466   1469     }
  1467   1470   }
................................................................................
  1574   1577       }
  1575   1578       rc = sqlite3_open(zDestFile, &pDest);
  1576   1579       if( rc!=SQLITE_OK ){
  1577   1580         fprintf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
  1578   1581         sqlite3_close(pDest);
  1579   1582         return 1;
  1580   1583       }
  1581         -    open_db(p);
         1584  +    open_db(p, 0);
  1582   1585       pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
  1583   1586       if( pBackup==0 ){
  1584   1587         fprintf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
  1585   1588         sqlite3_close(pDest);
  1586   1589         return 1;
  1587   1590       }
  1588   1591       while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
................................................................................
  1599   1602     if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 && nArg>1 && nArg<3 ){
  1600   1603       bail_on_error = booleanValue(azArg[1]);
  1601   1604     }else
  1602   1605   
  1603   1606     if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 && nArg==1 ){
  1604   1607       struct callback_data data;
  1605   1608       char *zErrMsg = 0;
  1606         -    open_db(p);
         1609  +    open_db(p, 0);
  1607   1610       memcpy(&data, p, sizeof(data));
  1608   1611       data.showHeader = 1;
  1609   1612       data.mode = MODE_Column;
  1610   1613       data.colWidth[0] = 3;
  1611   1614       data.colWidth[1] = 15;
  1612   1615       data.colWidth[2] = 58;
  1613   1616       data.cnt = 0;
................................................................................
  1616   1619         fprintf(stderr,"Error: %s\n", zErrMsg);
  1617   1620         sqlite3_free(zErrMsg);
  1618   1621         rc = 1;
  1619   1622       }
  1620   1623     }else
  1621   1624   
  1622   1625     if( c=='d' && strncmp(azArg[0], "dump", n)==0 && nArg<3 ){
  1623         -    open_db(p);
         1626  +    open_db(p, 0);
  1624   1627       /* When playing back a "dump", the content might appear in an order
  1625   1628       ** which causes immediate foreign key constraints to be violated.
  1626   1629       ** So disable foreign-key constraint enforcement to prevent problems. */
  1627   1630       fprintf(p->out, "PRAGMA foreign_keys=OFF;\n");
  1628   1631       fprintf(p->out, "BEGIN TRANSACTION;\n");
  1629   1632       p->writableSchema = 0;
  1630   1633       sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
................................................................................
  1734   1737       char *zSql;                 /* An SQL statement */
  1735   1738       char *zLine;                /* A single line of input from the file */
  1736   1739       char **azCol;               /* zLine[] broken up into columns */
  1737   1740       char *zCommit;              /* How to commit changes */   
  1738   1741       FILE *in;                   /* The input file */
  1739   1742       int lineno = 0;             /* Line number of input file */
  1740   1743   
  1741         -    open_db(p);
         1744  +    open_db(p, 0);
  1742   1745       nSep = strlen30(p->separator);
  1743   1746       if( nSep==0 ){
  1744   1747         fprintf(stderr, "Error: non-null separator required for import\n");
  1745   1748         return 1;
  1746   1749       }
  1747   1750       zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
  1748   1751       if( zSql==0 ){
................................................................................
  1849   1852       sqlite3_finalize(pStmt);
  1850   1853       sqlite3_exec(p->db, zCommit, 0, 0, 0);
  1851   1854     }else
  1852   1855   
  1853   1856     if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg<3 ){
  1854   1857       struct callback_data data;
  1855   1858       char *zErrMsg = 0;
  1856         -    open_db(p);
         1859  +    open_db(p, 0);
  1857   1860       memcpy(&data, p, sizeof(data));
  1858   1861       data.showHeader = 0;
  1859   1862       data.mode = MODE_List;
  1860   1863       if( nArg==1 ){
  1861   1864         rc = sqlite3_exec(p->db,
  1862   1865           "SELECT name FROM sqlite_master "
  1863   1866           "WHERE type='index' AND name NOT LIKE 'sqlite_%' "
................................................................................
  1915   1918   
  1916   1919   #ifndef SQLITE_OMIT_LOAD_EXTENSION
  1917   1920     if( c=='l' && strncmp(azArg[0], "load", n)==0 && nArg>=2 ){
  1918   1921       const char *zFile, *zProc;
  1919   1922       char *zErrMsg = 0;
  1920   1923       zFile = azArg[1];
  1921   1924       zProc = nArg>=3 ? azArg[2] : 0;
  1922         -    open_db(p);
         1925  +    open_db(p, 0);
  1923   1926       rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
  1924   1927       if( rc!=SQLITE_OK ){
  1925   1928         fprintf(stderr, "Error: %s\n", zErrMsg);
  1926   1929         sqlite3_free(zErrMsg);
  1927   1930         rc = 1;
  1928   1931       }
  1929   1932     }else
................................................................................
  1993   1996       }
  1994   1997     }else
  1995   1998   
  1996   1999     if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 && nArg==2 ) {
  1997   2000       sqlite3_snprintf(sizeof(p->nullvalue), p->nullvalue,
  1998   2001                        "%.*s", (int)ArraySize(p->nullvalue)-1, azArg[1]);
  1999   2002     }else
         2003  +
         2004  +  if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
         2005  +    sqlite3 *savedDb = p->db;
         2006  +    const char *zSavedFilename = p->zDbFilename;
         2007  +    char *zNewFilename = 0;
         2008  +    p->db = 0;
         2009  +    if( nArg>=2 ){
         2010  +      p->zDbFilename = zNewFilename = sqlite3_mprintf("%s", azArg[1]);
         2011  +    }
         2012  +    open_db(p, 1);
         2013  +    if( p->db!=0 ){
         2014  +      sqlite3_close(savedDb);
         2015  +      sqlite3_free(p->zFreeOnClose);
         2016  +      p->zFreeOnClose = zNewFilename;
         2017  +    }else{
         2018  +      sqlite3_free(zNewFilename);
         2019  +      p->db = savedDb;
         2020  +      p->zDbFilename = zSavedFilename;
         2021  +    }
         2022  +  }else
  2000   2023   
  2001   2024     if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){
  2002   2025       if( p->out!=stdout ){
  2003   2026         fclose(p->out);
  2004   2027       }
  2005   2028       if( strcmp(azArg[1],"stdout")==0 ){
  2006   2029         p->out = stdout;
................................................................................
  2057   2080       }
  2058   2081       rc = sqlite3_open(zSrcFile, &pSrc);
  2059   2082       if( rc!=SQLITE_OK ){
  2060   2083         fprintf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
  2061   2084         sqlite3_close(pSrc);
  2062   2085         return 1;
  2063   2086       }
  2064         -    open_db(p);
         2087  +    open_db(p, 0);
  2065   2088       pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
  2066   2089       if( pBackup==0 ){
  2067   2090         fprintf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
  2068   2091         sqlite3_close(pSrc);
  2069   2092         return 1;
  2070   2093       }
  2071   2094       while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
................................................................................
  2087   2110       }
  2088   2111       sqlite3_close(pSrc);
  2089   2112     }else
  2090   2113   
  2091   2114     if( c=='s' && strncmp(azArg[0], "schema", n)==0 && nArg<3 ){
  2092   2115       struct callback_data data;
  2093   2116       char *zErrMsg = 0;
  2094         -    open_db(p);
         2117  +    open_db(p, 0);
  2095   2118       memcpy(&data, p, sizeof(data));
  2096   2119       data.showHeader = 0;
  2097   2120       data.mode = MODE_Semi;
  2098   2121       if( nArg>1 ){
  2099   2122         int i;
  2100   2123         for(i=0; azArg[1][i]; i++) azArg[1][i] = ToLower(azArg[1][i]);
  2101   2124         if( strcmp(azArg[1],"sqlite_master")==0 ){
................................................................................
  2193   2216       p->statsOn = booleanValue(azArg[1]);
  2194   2217     }else
  2195   2218   
  2196   2219     if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 && nArg<3 ){
  2197   2220       char **azResult;
  2198   2221       int nRow;
  2199   2222       char *zErrMsg;
  2200         -    open_db(p);
         2223  +    open_db(p, 0);
  2201   2224       if( nArg==1 ){
  2202   2225         rc = sqlite3_get_table(p->db,
  2203   2226           "SELECT name FROM sqlite_master "
  2204   2227           "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%' "
  2205   2228           "UNION ALL "
  2206   2229           "SELECT name FROM sqlite_temp_master "
  2207   2230           "WHERE type IN ('table','view') "
................................................................................
  2269   2292         { "optimizations",         SQLITE_TESTCTRL_OPTIMIZATIONS          },
  2270   2293         { "iskeyword",             SQLITE_TESTCTRL_ISKEYWORD              },
  2271   2294         { "scratchmalloc",         SQLITE_TESTCTRL_SCRATCHMALLOC          },
  2272   2295       };
  2273   2296       int testctrl = -1;
  2274   2297       int rc = 0;
  2275   2298       int i, n;
  2276         -    open_db(p);
         2299  +    open_db(p, 0);
  2277   2300   
  2278   2301       /* convert testctrl text option to value. allow any unique prefix
  2279   2302       ** of the option name, or a numerical value. */
  2280   2303       n = strlen30(azArg[1]);
  2281   2304       for(i=0; i<(int)(sizeof(aCtrl)/sizeof(aCtrl[0])); i++){
  2282   2305         if( strncmp(azArg[1], aCtrl[i].zCtrlName, n)==0 ){
  2283   2306           if( testctrl<0 ){
................................................................................
  2368   2391                     azArg[1]);
  2369   2392             break;
  2370   2393         }
  2371   2394       }
  2372   2395     }else
  2373   2396   
  2374   2397     if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 && nArg==2 ){
  2375         -    open_db(p);
         2398  +    open_db(p, 0);
  2376   2399       sqlite3_busy_timeout(p->db, atoi(azArg[1]));
  2377   2400     }else
  2378   2401       
  2379   2402     if( HAS_TIMER && c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0
  2380   2403      && nArg==2
  2381   2404     ){
  2382   2405       enableTimer = booleanValue(azArg[1]);
  2383   2406     }else
  2384         -  
         2407  +
  2385   2408     if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
  2386   2409       printf("SQLite %s %s\n" /*extra-version-info*/,
  2387   2410           sqlite3_libversion(), sqlite3_sourceid());
  2388   2411     }else
  2389   2412   
  2390   2413     if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
  2391   2414       const char *zDbName = nArg==2 ? azArg[1] : "main";
................................................................................
  2551   2574         zSql[nSql++] = '\n';
  2552   2575         memcpy(&zSql[nSql], zLine, len+1);
  2553   2576         nSql += len;
  2554   2577       }
  2555   2578       if( zSql && _contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
  2556   2579                   && sqlite3_complete(zSql) ){
  2557   2580         p->cnt = 0;
  2558         -      open_db(p);
         2581  +      open_db(p, 0);
  2559   2582         BEGIN_TIMER;
  2560   2583         rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg);
  2561   2584         END_TIMER;
  2562   2585         if( rc || zErrMsg ){
  2563   2586           char zPrefix[100];
  2564   2587           if( in!=0 || !stdin_is_interactive ){
  2565   2588             sqlite3_snprintf(sizeof(zPrefix), zPrefix, 
................................................................................
  2879   2902   
  2880   2903     /* Go ahead and open the database file if it already exists.  If the
  2881   2904     ** file does not exist, delay opening it.  This prevents empty database
  2882   2905     ** files from being created if a user mistypes the database name argument
  2883   2906     ** to the sqlite command-line tool.
  2884   2907     */
  2885   2908     if( access(data.zDbFilename, 0)==0 ){
  2886         -    open_db(&data);
         2909  +    open_db(&data, 0);
  2887   2910     }
  2888   2911   
  2889   2912     /* Process the initialization file if there is one.  If no -init option
  2890   2913     ** is given on the command line, look for a file named ~/.sqliterc and
  2891   2914     ** try to process it.
  2892   2915     */
  2893   2916     rc = process_sqliterc(&data,zInitFile);
................................................................................
  2971   2994         if( i==argc-1 ) break;
  2972   2995         i++;
  2973   2996         z = argv[i];
  2974   2997         if( z[0]=='.' ){
  2975   2998           rc = do_meta_command(z, &data);
  2976   2999           if( rc && bail_on_error ) return rc;
  2977   3000         }else{
  2978         -        open_db(&data);
         3001  +        open_db(&data, 0);
  2979   3002           rc = shell_exec(data.db, z, shell_callback, &data, &zErrMsg);
  2980   3003           if( zErrMsg!=0 ){
  2981   3004             fprintf(stderr,"Error: %s\n", zErrMsg);
  2982   3005             if( bail_on_error ) return rc!=0 ? rc : 1;
  2983   3006           }else if( rc!=0 ){
  2984   3007             fprintf(stderr,"Error: unable to process SQL \"%s\"\n", z);
  2985   3008             if( bail_on_error ) return rc;
................................................................................
  2994   3017   
  2995   3018     if( zFirstCmd ){
  2996   3019       /* Run just the command that follows the database name
  2997   3020       */
  2998   3021       if( zFirstCmd[0]=='.' ){
  2999   3022         rc = do_meta_command(zFirstCmd, &data);
  3000   3023       }else{
  3001         -      open_db(&data);
         3024  +      open_db(&data, 0);
  3002   3025         rc = shell_exec(data.db, zFirstCmd, shell_callback, &data, &zErrMsg);
  3003   3026         if( zErrMsg!=0 ){
  3004   3027           fprintf(stderr,"Error: %s\n", zErrMsg);
  3005   3028           return rc!=0 ? rc : 1;
  3006   3029         }else if( rc!=0 ){
  3007   3030           fprintf(stderr,"Error: unable to process SQL \"%s\"\n", zFirstCmd);
  3008   3031           return rc;
................................................................................
  3042   3065         rc = process_input(&data, stdin);
  3043   3066       }
  3044   3067     }
  3045   3068     set_table_name(&data, 0);
  3046   3069     if( data.db ){
  3047   3070       sqlite3_close(data.db);
  3048   3071     }
         3072  +  sqlite3_free(data.zFreeOnClose); 
  3049   3073     return rc;
  3050   3074   }