]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
In the command-line shell, add the --new option to the ".open" command.
authordrh <drh@noemail.net>
Fri, 16 Sep 2016 00:26:08 +0000 (00:26 +0000)
committerdrh <drh@noemail.net>
Fri, 16 Sep 2016 00:26:08 +0000 (00:26 +0000)
Also, report the current database filename as part of the ".show" command.

FossilOrigin-Name: 8e5c92039128a430e0509f4f06ea80ba39c35bda

manifest
manifest.uuid
src/shell.c

index 2812980f6905d3d404a289f0c3509fa2d89b8502..d7517fc830a622fcc9e9939475479be0ba65f62a 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Add\sthe\s".testcase"\sand\s".check"\sdot-commands\sin\sthe\sshell,\swhen\scompiled\nusing\sSQLITE_DEBUG.
-D 2016-09-15T21:35:24.532
+C In\sthe\scommand-line\sshell,\sadd\sthe\s--new\soption\sto\sthe\s".open"\scommand.\nAlso,\sreport\sthe\scurrent\sdatabase\sfilename\sas\spart\sof\sthe\s".show"\scommand.
+D 2016-09-16T00:26:08.684
 F Makefile.in 6fd48ffcf7c2deea7499062d1f3747f986c19678
 F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434
 F Makefile.msc e1aa788e84f926e42239ee167c53f785bedacacd
@@ -387,7 +387,7 @@ F src/random.c ba2679f80ec82c4190062d756f22d0c358180696
 F src/resolve.c 24f40fd0c3475821d1ad762a3f2c3455cc839b42
 F src/rowset.c 7b7e7e479212e65b723bf40128c7b36dc5afdfac
 F src/select.c 244f9cc5e4662987cd2ef5c22d1b7027560f3425
-F src/shell.c 89a3adbfcaac17070372ce631161bbbf21ec2010
+F src/shell.c 1de20c816872f5443a104bdae238ff142e887f9e
 F src/sqlite.h.in 46ed821aeed0ba45559fb15597d9a400083154a2
 F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
 F src/sqlite3ext.h 8648034aa702469afb553231677306cc6492a1ae
@@ -1525,7 +1525,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P 7b10461370828b9c57acaaaea518031d53986fa3
-R 1d8691a695a03272769e683d2ed4d0b4
+P 62289f27ee276090a855982bd8216a465e7d0a27
+R d4e2f1f820d6f2b9a8e052f36c44661c
 U drh
-Z 15bb0e2011d42f4d3abf5a593439e046
+Z d98a4b3e23d9525e29325cc221e2ffa0
index d6648be1c233c52e843272d5606b3722c45d4753..1013449e87b6a7fb262d50eceb1c70c2f5206d5c 100644 (file)
@@ -1 +1 @@
-62289f27ee276090a855982bd8216a465e7d0a27
\ No newline at end of file
+8e5c92039128a430e0509f4f06ea80ba39c35bda
\ No newline at end of file
index 2991e7c13f04cc0294256909250c7ae1185ae998..6c9bf7cd32655fd1f1dc4c59a741201339332933 100644 (file)
@@ -2176,7 +2176,8 @@ static char zHelp[] =
   "                         tcl      TCL list elements\n"
   ".nullvalue STRING      Use STRING in place of NULL values\n"
   ".once FILENAME         Output for the next SQL command only to FILENAME\n"
-  ".open ?FILENAME?       Close existing database and reopen FILENAME\n"
+  ".open ?-new? ?FILE?    Close existing database and reopen FILE\n"
+  "                         The --new starts with an empty file\n"
   ".output ?FILENAME?     Send output to FILENAME or stdout\n"
   ".print STRING...       Print literal STRING\n"
   ".prompt MAIN CONTINUE  Replace the standard prompts\n"
@@ -3193,6 +3194,21 @@ static int optionMatch(const char *zStr, const char *zOpt){
   return strcmp(zStr, zOpt)==0;
 }
 
+/*
+** Delete a file.
+*/
+int shellDeleteFile(const char *zFilename){
+  int rc;
+#ifdef _WIN32
+  wchar_t *z = sqlite3_utf8_to_path(zFilename, 0);
+  rc = _wunlink(z);
+  sqlite3_free(z);
+#else
+  rc = unlink(zFilename);
+#endif
+  return rc;
+}
+
 /*
 ** If an input line begins with "." then invoke this routine to
 ** process that line.
@@ -3975,22 +3991,35 @@ static int do_meta_command(char *zLine, ShellState *p){
   }else
 
   if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
-    sqlite3 *savedDb = p->db;
-    const char *zSavedFilename = p->zDbFilename;
-    char *zNewFilename = 0;
+    char *zNewFilename;  /* Name of the database file to open */
+    int iName = 1;       /* Index in azArg[] of the filename */
+    /* Close the existing database */
+    session_close_all(p);
+    sqlite3_close(p->db);
     p->db = 0;
-    if( nArg>=2 ) zNewFilename = sqlite3_mprintf("%s", azArg[1]);
-    p->zDbFilename = zNewFilename;
-    open_db(p, 1);
-    if( p->db!=0 ){
-      session_close_all(p);
-      sqlite3_close(savedDb);
-      sqlite3_free(p->zFreeOnClose);
-      p->zFreeOnClose = zNewFilename;
-    }else{
-      sqlite3_free(zNewFilename);
-      p->db = savedDb;
-      p->zDbFilename = zSavedFilename;
+    sqlite3_free(p->zFreeOnClose);
+    p->zFreeOnClose = 0;
+    /* Start a new database file if the --new flag is present */
+    if( nArg>2 && optionMatch(azArg[1],"new") ){
+      iName++;
+      if( nArg>iName ) shellDeleteFile(azArg[iName]);
+    }
+    /* If a filename is specified, try to open it first */
+    zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
+    if( zNewFilename ){
+      p->zDbFilename = zNewFilename;
+      open_db(p, 1);
+      if( p->db==0 ){
+        utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
+        sqlite3_free(zNewFilename);
+      }else{
+        p->zFreeOnClose = zNewFilename;
+      }
+    }
+    if( p->db==0 ){
+      /* As a fall-back open a TEMP database */
+      p->zDbFilename = 0;
+      open_db(p, 0);
     }
   }else
 
@@ -4520,6 +4549,8 @@ static int do_meta_command(char *zLine, ShellState *p){
       raw_printf(p->out, "%d ", p->colWidth[i]);
     }
     raw_printf(p->out, "\n");
+    utf8_printf(p->out, "%12.12s: %s\n", "filename",
+                p->zDbFilename ? p->zDbFilename : "");
   }else
 
   if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){