@@ -580,6 +580,9 @@ zSkipValidUtf8(const char *z, int nAccept, long ccm);
580580#ifndef HAVE_CONSOLE_IO_H
581581# include "console_io.h"
582582#endif
583+ #if defined(_MSC_VER)
584+ # pragma warning(disable : 4204)
585+ #endif
583586
584587#ifndef SQLITE_CIO_NO_TRANSLATE
585588# if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
@@ -678,6 +681,10 @@ static short streamOfConsole(FILE *pf, /* out */ PerStreamTags *ppst){
678681# endif
679682}
680683
684+ # ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
685+ # define ENABLE_VIRTUAL_TERMINAL_PROCESSING (0x4)
686+ # endif
687+
681688# if CIO_WIN_WC_XLATE
682689/* Define console modes for use with the Windows Console API. */
683690# define SHELL_CONI_MODE \
@@ -1228,6 +1235,10 @@ SQLITE_INTERNAL_LINKAGE char* fGetsUtf8(char *cBuf, int ncMax, FILE *pfIn){
12281235}
12291236#endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
12301237
1238+ #if defined(_MSC_VER)
1239+ # pragma warning(default : 4204)
1240+ #endif
1241+
12311242#undef SHELL_INVALID_FILE_PTR
12321243
12331244/************************* End ../ext/consio/console_io.c ********************/
@@ -20619,6 +20630,7 @@ static void exec_prepared_stmt_columnar(
2061920630 rc = sqlite3_step(pStmt);
2062020631 if( rc!=SQLITE_ROW ) return;
2062120632 nColumn = sqlite3_column_count(pStmt);
20633+ if( nColumn==0 ) goto columnar_end;
2062220634 nAlloc = nColumn*4;
2062320635 if( nAlloc<=0 ) nAlloc = 1;
2062420636 azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
@@ -20704,7 +20716,6 @@ static void exec_prepared_stmt_columnar(
2070420716 if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
2070520717 }
2070620718 if( seenInterrupt ) goto columnar_end;
20707- if( nColumn==0 ) goto columnar_end;
2070820719 switch( p->cMode ){
2070920720 case MODE_Column: {
2071020721 colSep = " ";
@@ -25553,16 +25564,15 @@ static int do_meta_command(char *zLine, ShellState *p){
2555325564#ifndef SQLITE_SHELL_FIDDLE
2555425565 if( c=='i' && cli_strncmp(azArg[0], "import", n)==0 ){
2555525566 char *zTable = 0; /* Insert data into this table */
25556- char *zSchema = 0; /*within this schema (may default to "main") */
25567+ char *zSchema = 0; /*Schema of zTable */
2555725568 char *zFile = 0; /* Name of file to extra content from */
2555825569 sqlite3_stmt *pStmt = NULL; /* A statement */
2555925570 int nCol; /* Number of columns in the table */
25560- int nByte; /* Number of bytes in an SQL string */
25571+ i64 nByte; /* Number of bytes in an SQL string */
2556125572 int i, j; /* Loop counters */
2556225573 int needCommit; /* True to COMMIT or ROLLBACK at end */
2556325574 int nSep; /* Number of bytes in p->colSeparator[] */
25564- char *zSql; /* An SQL statement */
25565- char *zFullTabName; /* Table name with schema if applicable */
25575+ char *zSql = 0; /* An SQL statement */
2556625576 ImportCtx sCtx; /* Reader context */
2556725577 char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
2556825578 int eVerbose = 0; /* Larger for more console output */
@@ -25696,24 +25706,14 @@ static int do_meta_command(char *zLine, ShellState *p){
2569625706 while( (nSkip--)>0 ){
2569725707 while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
2569825708 }
25699- if( zSchema!=0 ){
25700- zFullTabName = sqlite3_mprintf("\"%w\".\"%w\"", zSchema, zTable);
25701- }else{
25702- zFullTabName = sqlite3_mprintf("\"%w\"", zTable);
25703- }
25704- zSql = sqlite3_mprintf("SELECT * FROM %s", zFullTabName);
25705- if( zSql==0 || zFullTabName==0 ){
25706- import_cleanup(&sCtx);
25707- shell_out_of_memory();
25708- }
25709- nByte = strlen30(zSql);
25710- rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
2571125709 import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */
25712- if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
25710+ if( sqlite3_table_column_metadata(p->db, zSchema, zTable,0,0,0,0,0,0) ){
25711+ /* Table does not exist. Create it. */
2571325712 sqlite3 *dbCols = 0;
2571425713 char *zRenames = 0;
2571525714 char *zColDefs;
25716- zCreate = sqlite3_mprintf("CREATE TABLE %s", zFullTabName);
25715+ zCreate = sqlite3_mprintf("CREATE TABLE \"%w\".\"%w\"",
25716+ zSchema ? zSchema : "main", zTable);
2571725717 while( xRead(&sCtx) ){
2571825718 zAutoColumn(sCtx.z, &dbCols, 0);
2571925719 if( sCtx.cTerm!=sCtx.cColSep ) break;
@@ -25728,34 +25728,50 @@ static int do_meta_command(char *zLine, ShellState *p){
2572825728 assert(dbCols==0);
2572925729 if( zColDefs==0 ){
2573025730 eputf("%s: empty file\n", sCtx.zFile);
25731- import_fail:
25732- sqlite3_free(zCreate);
25733- sqlite3_free(zSql);
25734- sqlite3_free(zFullTabName);
2573525731 import_cleanup(&sCtx);
2573625732 rc = 1;
2573725733 goto meta_command_exit;
2573825734 }
2573925735 zCreate = sqlite3_mprintf("%z%z\n", zCreate, zColDefs);
25736+ if( zCreate==0 ){
25737+ import_cleanup(&sCtx);
25738+ shell_out_of_memory();
25739+ }
2574025740 if( eVerbose>=1 ){
2574125741 oputf("%s\n", zCreate);
2574225742 }
2574325743 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
25744+ sqlite3_free(zCreate);
25745+ zCreate = 0;
2574425746 if( rc ){
2574525747 eputf("%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db));
25746- goto import_fail;
25748+ import_cleanup(&sCtx);
25749+ rc = 1;
25750+ goto meta_command_exit;
2574725751 }
25748- sqlite3_free(zCreate);
25749- zCreate = 0;
25750- rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
2575125752 }
25753+ zSql = sqlite3_mprintf("SELECT count(*) FROM pragma_table_info(%Q,%Q);",
25754+ zTable, zSchema);
25755+ if( zSql==0 ){
25756+ import_cleanup(&sCtx);
25757+ shell_out_of_memory();
25758+ }
25759+ nByte = strlen(zSql);
25760+ rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
25761+ sqlite3_free(zSql);
25762+ zSql = 0;
2575225763 if( rc ){
2575325764 if (pStmt) sqlite3_finalize(pStmt);
2575425765 eputf("Error: %s\n", sqlite3_errmsg(p->db));
25755- goto import_fail;
25766+ import_cleanup(&sCtx);
25767+ rc = 1;
25768+ goto meta_command_exit;
25769+ }
25770+ if( sqlite3_step(pStmt)==SQLITE_ROW ){
25771+ nCol = sqlite3_column_int(pStmt, 0);
25772+ }else{
25773+ nCol = 0;
2575625774 }
25757- sqlite3_free(zSql);
25758- nCol = sqlite3_column_count(pStmt);
2575925775 sqlite3_finalize(pStmt);
2576025776 pStmt = 0;
2576125777 if( nCol==0 ) return 0; /* no columns, no error */
@@ -25764,7 +25780,12 @@ static int do_meta_command(char *zLine, ShellState *p){
2576425780 import_cleanup(&sCtx);
2576525781 shell_out_of_memory();
2576625782 }
25767- sqlite3_snprintf(nByte+20, zSql, "INSERT INTO %s VALUES(?", zFullTabName);
25783+ if( zSchema ){
25784+ sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\".\"%w\" VALUES(?",
25785+ zSchema, zTable);
25786+ }else{
25787+ sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
25788+ }
2576825789 j = strlen30(zSql);
2576925790 for(i=1; i<nCol; i++){
2577025791 zSql[j++] = ',';
@@ -25776,13 +25797,15 @@ static int do_meta_command(char *zLine, ShellState *p){
2577625797 oputf("Insert using: %s\n", zSql);
2577725798 }
2577825799 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
25800+ sqlite3_free(zSql);
25801+ zSql = 0;
2577925802 if( rc ){
2578025803 eputf("Error: %s\n", sqlite3_errmsg(p->db));
2578125804 if (pStmt) sqlite3_finalize(pStmt);
25782- goto import_fail;
25805+ import_cleanup(&sCtx);
25806+ rc = 1;
25807+ goto meta_command_exit;
2578325808 }
25784- sqlite3_free(zSql);
25785- sqlite3_free(zFullTabName);
2578625809 needCommit = sqlite3_get_autocommit(p->db);
2578725810 if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
2578825811 do{