@@ -434,6 +434,7 @@ struct callback_data {
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 */
@@ -1416,6 +1417,7 @@ static char zHelp[] =
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"
@@ -1447,7 +1449,7 @@ static int process_input(struct callback_data *p, FILE *in);
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 ;
@@ -1458,6 +1460,7 @@ static void open_db(struct callback_data *p){
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
@@ -1578,7 +1581,7 @@ static int do_meta_command(char *zLine, struct callback_data *p){
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 ));
@@ -1603,7 +1606,7 @@ static int do_meta_command(char *zLine, struct callback_data *p){
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 ;
@@ -1620,7 +1623,7 @@ static int do_meta_command(char *zLine, struct callback_data *p){
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. */
@@ -1738,7 +1741,7 @@ static int do_meta_command(char *zLine, struct callback_data *p){
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" );
@@ -1853,7 +1856,7 @@ static int do_meta_command(char *zLine, struct callback_data *p){
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 ;
@@ -1919,7 +1922,7 @@ static int do_meta_command(char *zLine, struct callback_data *p){
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 );
@@ -1998,6 +2001,26 @@ static int do_meta_command(char *zLine, struct callback_data *p){
1998
2001
"%.*s" , (int )ArraySize (p -> nullvalue )- 1 , azArg [1 ]);
1999
2002
}else
2000
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
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 );
@@ -2061,7 +2084,7 @@ static int do_meta_command(char *zLine, struct callback_data *p){
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 ));
@@ -2091,7 +2114,7 @@ static int do_meta_command(char *zLine, struct callback_data *p){
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 ;
@@ -2197,7 +2220,7 @@ static int do_meta_command(char *zLine, struct callback_data *p){
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 "
@@ -2273,7 +2296,7 @@ static int do_meta_command(char *zLine, struct callback_data *p){
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. */
@@ -2372,7 +2395,7 @@ static int do_meta_command(char *zLine, struct callback_data *p){
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
@@ -2381,7 +2404,7 @@ static int do_meta_command(char *zLine, struct callback_data *p){
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 ());
@@ -2555,7 +2578,7 @@ static int process_input(struct callback_data *p, FILE *in){
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 ;
@@ -2883,7 +2906,7 @@ int main(int argc, char **argv){
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
@@ -2975,7 +2998,7 @@ int main(int argc, char **argv){
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 );
@@ -2998,7 +3021,7 @@ int main(int argc, char **argv){
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 );
@@ -3046,5 +3069,6 @@ int main(int argc, char **argv){
3046
3069
if ( data .db ){
3047
3070
sqlite3_close (data .db );
3048
3071
}
3072
+ sqlite3_free (data .zFreeOnClose );
3049
3073
return rc ;
3050
3074
}
0 commit comments