From 135f5fb7470fb9aff5d6aa8ad6f5a92ffb4f285b Mon Sep 17 00:00:00 2001 From: drh <> Date: Mon, 7 Mar 2022 01:28:13 +0000 Subject: [PATCH] Fix the .import problem in the CLI reported by [forum:/forumpost/72a024c957|forum post 72a024c957]. FossilOrigin-Name: d753285bcbb68bdf8f6d95a64af58b20ce6da9f048734fdb0ae49150eb7befd4 --- manifest | 17 +++++++++-------- manifest.uuid | 2 +- src/shell.c.in | 44 +++++++++++++++++++++++--------------------- test/shell5.test | 16 +++++++++++++++- 4 files changed, 48 insertions(+), 31 deletions(-) diff --git a/manifest b/manifest index 3ad93c2550..316982b352 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C The\ssqlite_offset()\sfunction\sshould\sbe\snon-deterministic.\ndbsqlfuzz\s3df8230bb940870db87ffca2c0fc759c1e7fa356. -D 2022-03-06T11:44:14.626 +C Fix\sthe\s.import\sproblem\sin\sthe\sCLI\sreported\sby\n[forum:/forumpost/72a024c957|forum\spost\s72a024c957]. +D 2022-03-07T01:28:13.580 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 3baa9dd8cf240654773c7974e2bcce398ac9dd24419c36684156963defe43b35 -F src/shell.c.in 14cdfba32c73cb06169e50cd448632c28359f2bab2a0f803dc4a7f46dfc5b6fa +F src/shell.c.in 0a9c4b9226143365c8aea42eb9a62e055e9fffffc5e4559da5c9dadc0bedaa25 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,9 +1944,10 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P bf2501e26c8e4c08d679ea930bab89b9510808439aee9412ab176a9d29635cc7 -Q +e1a185e60afd32d3b25278dee42049920759ccd8fe709161007f5daa4a048693 -R 2a94d5312fd267ccae72929f32727159 +P 8083757606920151171530f337919553fa8ba902a51744d7b21eeba1558db9fa +Q +4c3a02600f10926da1f88ddbd457bb1486e6e02dee366b5cfc89e498a10daa6f +Q +bf9d1278846dce9255f9a11ddfc5dfac1acea2eadcb20816a19d59f7bccaec0f +R 019f54d2f127da970daad0bc2fb20027 U drh -Z dd643661c30fd8bc27a62cbef867675e +Z 6fea8bddc882893a47ea3bb1db9d8f05 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index ea6594fbd7..a98b6dea17 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -8083757606920151171530f337919553fa8ba902a51744d7b21eeba1558db9fa \ No newline at end of file +d753285bcbb68bdf8f6d95a64af58b20ce6da9f048734fdb0ae49150eb7befd4 \ No newline at end of file diff --git a/src/shell.c.in b/src/shell.c.in index 354c9a849b..b0f3b02dc4 100644 --- a/src/shell.c.in +++ b/src/shell.c.in @@ -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 */ @@ -8836,6 +8836,7 @@ static int do_meta_command(char *zLine, ShellState *p){ int needCommit; /* True to COMMIT or ROLLBACK at end */ int nSep; /* Number of bytes in p->colSeparator[] */ char *zSql; /* An SQL statement */ + char *zFullTabName; /* Table name with schema if applicable */ ImportCtx sCtx; /* Reader context */ char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */ int eVerbose = 0; /* Larger for more console output */ @@ -8963,7 +8964,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 +8975,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 ){ + zFullTabName = sqlite3_mprintf("\"%w\".\"%w\"", zSchema, zTable); + }else{ + zFullTabName = sqlite3_mprintf("\"%w\"", zTable); + } + zSql = sqlite3_mprintf("SELECT * FROM %s", zFullTabName); + if( zSql==0 || zFullTabName==0 ){ import_cleanup(&sCtx); shell_out_of_memory(); } @@ -8987,8 +8993,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", zFullTabName); sqlite3 *dbCols = 0; char *zRenames = 0; char *zColDefs; @@ -9005,9 +9010,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(zFullTabName); import_cleanup(&sCtx); - utf8_printf(stderr,"%s: empty file\n", sCtx.zFile); rc = 1; goto meta_command_exit; } @@ -9018,22 +9026,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 +9047,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(?", zFullTabName); j = strlen30(zSql); for(i=1; iout, "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(zFullTabName); needCommit = sqlite3_get_autocommit(p->db); if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0); do{ diff --git a/test/shell5.test b/test/shell5.test index 2237e685d5..d63a0a20d3 100644 --- a/test/shell5.test +++ b/test/shell5.test @@ -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. # -- 2.47.2