]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
For CLI .import, revert to importing into temp or main when given table is found...
authorlarrybr <larrybr@noemail.net>
Sun, 6 Mar 2022 23:41:21 +0000 (23:41 +0000)
committerlarrybr <larrybr@noemail.net>
Sun, 6 Mar 2022 23:41:21 +0000 (23:41 +0000)
FossilOrigin-Name: bf9d1278846dce9255f9a11ddfc5dfac1acea2eadcb20816a19d59f7bccaec0f

manifest
manifest.uuid
src/shell.c.in
test/shell5.test

index 5916def05f883e9fb3a36f9d3607a699c8e59723..8702de82a3c5f6ecfd3d62c01d8a9ca763b9bedd 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Fix\sobsolete\sbut\sharmless\scomments\sin\sbtree.\s\sNo\schanges\sto\scode.
-D 2022-03-06T20:22:24.862
+C For\sCLI\s.import,\srevert\sto\simporting\sinto\stemp\sor\smain\swhen\sgiven\stable\sis\sfound\sthere\sand\sno\s-schema\soption\sused.\sAnd\splug\san\sobscure\sleak.
+D 2022-03-06T23:41:21.849
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -553,7 +553,7 @@ F src/random.c 097dc8b31b8fba5a9aca1697aeb9fd82078ec91be734c16bffda620ced7ab83c
 F src/resolve.c ea935b87d6fb36c78b70cdc7b28561dc8f33f2ef37048389549c7b5ef9b0ba5e
 F src/rowset.c ba9515a922af32abe1f7d39406b9d35730ed65efab9443dc5702693b60854c92
 F src/select.c 4890a3cfee0bc60ff231c3a44db37968859ab0be156983dbcc0c096109832cdd
-F src/shell.c.in 14cdfba32c73cb06169e50cd448632c28359f2bab2a0f803dc4a7f46dfc5b6fa
+F src/shell.c.in 3b4cfb17b0683831b3b42f92f411c311a8757d4e6fac7a1524258a29bbfbadbf
 F src/sqlite.h.in e82ac380b307659d0892f502b742f825504e78729f4edaadce946003b9c00816
 F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
 F src/sqlite3ext.h a95cb9ed106e3d39e2118e4dcc15a14faec3fa50d0093425083d340d9dfd96e6
@@ -1392,7 +1392,7 @@ F test/shell1.test b224e0793c5f48aa3749e65d8c64b93a30731bd206f2e41e6c5f1bee1bdb1
 F test/shell2.test 89e4b2db062d52baed75022227b462d085cff495809de1699652779d8e0257d6
 F test/shell3.test a50628ab1d78d90889d9d3f32fb2c084ee15674771e96afe954aaa0accd1de3c
 F test/shell4.test 8f6c0fce4abed19a8a7f7262517149812a04caa905d01bdc8f5e92573504b759
-F test/shell5.test 2b521446f55146c9aafccd0946bdb44ae288b0d25bd48f722e041974fdeeb04a
+F test/shell5.test 0a9920d81fae28c45cd5dbd1deb809487a23c5f4b422a49f9d31c85f926d4a9c
 F test/shell6.test 1ceb51b2678c472ba6cf1e5da96679ce8347889fe2c3bf93a0e0fa73f00b00d3
 F test/shell7.test 115132f66d0463417f408562cc2cf534f6bbc6d83a6d50f0072a9eb171bae97f
 F test/shell8.test 388471d16e4de767333107e30653983f186232c0e863f4490bb230419e830aae
@@ -1944,8 +1944,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P e1a185e60afd32d3b25278dee42049920759ccd8fe709161007f5daa4a048693
-R 3b376b7bd2b0a7789dc2fc49263ee5af
-U drh
-Z 746f08a842d71959509f58033103e467
+P 4838b888e431f794b8a5ee65e797b3bf0616c03261de4e1fc9499287eb3e1265
+R 48c484e9a49cdc0f439e995f4d0daf72
+U larrybr
+Z b5862c83f76331b7720ff8f2e107fcd4
 # Remove this line to create a well-formed Fossil manifest.
index 8116d99ea50f9763996fa4535297bd3a8c3db692..dc324d3a48ef4ec7b058ff8300a1a24da22fa945 100644 (file)
@@ -1 +1 @@
-4838b888e431f794b8a5ee65e797b3bf0616c03261de4e1fc9499287eb3e1265
\ No newline at end of file
+bf9d1278846dce9255f9a11ddfc5dfac1acea2eadcb20816a19d59f7bccaec0f
\ No newline at end of file
index 354c9a849bb324cde8b6fcf1ef0ba41e3cb08059..90445c9cf4429d40f332fd0aad2e74e281d813b1 100644 (file)
@@ -8827,7 +8827,7 @@ static int do_meta_command(char *zLine, ShellState *p){
 
   if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
     char *zTable = 0;           /* Insert data into this table */
-    char *zSchema = "main";     /* within this schema */
+    char *zSchema = 0;          /* within this schema (may default to "main") */
     char *zFile = 0;            /* Name of file to extra content from */
     sqlite3_stmt *pStmt = NULL; /* A statement */
     int nCol;                   /* Number of columns in the table */
@@ -8963,7 +8963,6 @@ static int do_meta_command(char *zLine, ShellState *p){
       import_cleanup(&sCtx);
       goto meta_command_exit;
     }
-    /* Below, resources must be freed before exit. */
     if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
       char zSep[2];
       zSep[1] = 0;
@@ -8975,11 +8974,17 @@ static int do_meta_command(char *zLine, ShellState *p){
       output_c_string(p->out, zSep);
       utf8_printf(p->out, "\n");
     }
+    /* Below, resources must be freed before exit. */
     while( (nSkip--)>0 ){
       while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
     }
-    zSql = sqlite3_mprintf("SELECT * FROM \"%w\".\"%w\"", zSchema, zTable);
-    if( zSql==0 ){
+    if( zSchema!=0 ){
+      zSchema = sqlite3_mprintf("\"%w\".\"%w\"", zSchema, zTable);
+    }else{
+      zSchema = sqlite3_mprintf("\"%w\"", zTable);
+    }
+    zSql = sqlite3_mprintf("SELECT * FROM %s", zSchema);
+    if( zSql==0 || zSchema==0 ){
       import_cleanup(&sCtx);
       shell_out_of_memory();
     }
@@ -8987,8 +8992,7 @@ static int do_meta_command(char *zLine, ShellState *p){
     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
     import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
     if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
-      char *zCreate = sqlite3_mprintf("CREATE TABLE \"%w\".\"%w\"",
-                                      zSchema, zTable);
+      char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zSchema);
       sqlite3 *dbCols = 0;
       char *zRenames = 0;
       char *zColDefs;
@@ -9005,9 +9009,12 @@ static int do_meta_command(char *zLine, ShellState *p){
       }
       assert(dbCols==0);
       if( zColDefs==0 ){
+        utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
+      import_fail:
         sqlite3_free(zCreate);
+        sqlite3_free(zSql);
+        sqlite3_free(zSchema);
         import_cleanup(&sCtx);
-        utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
         rc = 1;
         goto meta_command_exit;
       }
@@ -9018,22 +9025,18 @@ static int do_meta_command(char *zLine, ShellState *p){
       rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
       if( rc ){
         utf8_printf(stderr, "%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db));
-        sqlite3_free(zCreate);
-        import_cleanup(&sCtx);
-        rc = 1;
-        goto meta_command_exit;
+        goto import_fail;
       }
       sqlite3_free(zCreate);
+      zCreate = 0;
       rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
     }
-    sqlite3_free(zSql);
     if( rc ){
       if (pStmt) sqlite3_finalize(pStmt);
       utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
-      import_cleanup(&sCtx);
-      rc = 1;
-      goto meta_command_exit;
+      goto import_fail;
     }
+    sqlite3_free(zSql);
     nCol = sqlite3_column_count(pStmt);
     sqlite3_finalize(pStmt);
     pStmt = 0;
@@ -9043,8 +9046,7 @@ static int do_meta_command(char *zLine, ShellState *p){
       import_cleanup(&sCtx);
       shell_out_of_memory();
     }
-    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\".\"%w\" VALUES(?",
-                     zSchema, zTable);
+    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO %s VALUES(?", zSchema);
     j = strlen30(zSql);
     for(i=1; i<nCol; i++){
       zSql[j++] = ',';
@@ -9056,14 +9058,13 @@ static int do_meta_command(char *zLine, ShellState *p){
       utf8_printf(p->out, "Insert using: %s\n", zSql);
     }
     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
-    sqlite3_free(zSql);
     if( rc ){
       utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
       if (pStmt) sqlite3_finalize(pStmt);
-      import_cleanup(&sCtx);
-      rc = 1;
-      goto meta_command_exit;
+      goto import_fail;
     }
+    sqlite3_free(zSql);
+    sqlite3_free(zSchema);
     needCommit = sqlite3_get_autocommit(p->db);
     if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
     do{
index 2237e685d5107a3ef54bb29889f8f451f8ed79c1..d63a0a20d3edd86086b4dc5eebd450757fa1532c 100644 (file)
@@ -262,7 +262,7 @@ do_test shell5-1.7.1 {
 SELECT COUNT(*) FROM t3;}]
 } [list 0 $rows]
 
-# Inport from a pipe.  (Unix only, as it requires "awk")
+# Import from a pipe.  (Unix only, as it requires "awk")
 if {$tcl_platform(platform)=="unix"} {
   do_test shell5-1.8 {
     forcedelete test.db
@@ -476,6 +476,20 @@ CREATE TABLE t8(a, b, c);
   db eval { SELECT * FROM t8 }
 } {1 2 3}
 
+do_test shell5-4.4 {
+  forcedelete shell5.csv
+  set fd [open shell5.csv w]
+  puts $fd "1,2,3"
+  close $fd
+  catchcmd test.db [string trim {
+.mode csv
+CREATE TEMP TABLE t8(a, b, c);
+.import shell5.csv t8
+.nullvalue #
+SELECT * FROM temp.t8
+  }]
+} {0 1,2,3}
+
 #----------------------------------------------------------------------------
 # Tests for the shell automatic column rename.
 #