@@ -580,6 +580,9 @@ zSkipValidUtf8(const char *z, int nAccept, long ccm);
580
580
#ifndef HAVE_CONSOLE_IO_H
581
581
# include "console_io.h"
582
582
#endif
583
+ #if defined(_MSC_VER)
584
+ # pragma warning(disable : 4204)
585
+ #endif
583
586
584
587
#ifndef SQLITE_CIO_NO_TRANSLATE
585
588
# if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
@@ -678,6 +681,10 @@ static short streamOfConsole(FILE *pf, /* out */ PerStreamTags *ppst){
678
681
# endif
679
682
}
680
683
684
+ # ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
685
+ # define ENABLE_VIRTUAL_TERMINAL_PROCESSING (0x4)
686
+ # endif
687
+
681
688
# if CIO_WIN_WC_XLATE
682
689
/* Define console modes for use with the Windows Console API. */
683
690
# define SHELL_CONI_MODE \
@@ -1228,6 +1235,10 @@ SQLITE_INTERNAL_LINKAGE char* fGetsUtf8(char *cBuf, int ncMax, FILE *pfIn){
1228
1235
}
1229
1236
#endif /* !defined(SQLITE_CIO_NO_TRANSLATE) */
1230
1237
1238
+ #if defined(_MSC_VER)
1239
+ # pragma warning(default : 4204)
1240
+ #endif
1241
+
1231
1242
#undef SHELL_INVALID_FILE_PTR
1232
1243
1233
1244
/************************* End ../ext/consio/console_io.c ********************/
@@ -20619,6 +20630,7 @@ static void exec_prepared_stmt_columnar(
20619
20630
rc = sqlite3_step(pStmt);
20620
20631
if( rc!=SQLITE_ROW ) return;
20621
20632
nColumn = sqlite3_column_count(pStmt);
20633
+ if( nColumn==0 ) goto columnar_end;
20622
20634
nAlloc = nColumn*4;
20623
20635
if( nAlloc<=0 ) nAlloc = 1;
20624
20636
azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
@@ -20704,7 +20716,6 @@ static void exec_prepared_stmt_columnar(
20704
20716
if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
20705
20717
}
20706
20718
if( seenInterrupt ) goto columnar_end;
20707
- if( nColumn==0 ) goto columnar_end;
20708
20719
switch( p->cMode ){
20709
20720
case MODE_Column: {
20710
20721
colSep = " ";
@@ -25553,16 +25564,15 @@ static int do_meta_command(char *zLine, ShellState *p){
25553
25564
#ifndef SQLITE_SHELL_FIDDLE
25554
25565
if( c=='i' && cli_strncmp(azArg[0], "import", n)==0 ){
25555
25566
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 */
25557
25568
char *zFile = 0; /* Name of file to extra content from */
25558
25569
sqlite3_stmt *pStmt = NULL; /* A statement */
25559
25570
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 */
25561
25572
int i, j; /* Loop counters */
25562
25573
int needCommit; /* True to COMMIT or ROLLBACK at end */
25563
25574
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 */
25566
25576
ImportCtx sCtx; /* Reader context */
25567
25577
char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
25568
25578
int eVerbose = 0; /* Larger for more console output */
@@ -25696,24 +25706,14 @@ static int do_meta_command(char *zLine, ShellState *p){
25696
25706
while( (nSkip--)>0 ){
25697
25707
while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
25698
25708
}
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);
25711
25709
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. */
25713
25712
sqlite3 *dbCols = 0;
25714
25713
char *zRenames = 0;
25715
25714
char *zColDefs;
25716
- zCreate = sqlite3_mprintf("CREATE TABLE %s", zFullTabName);
25715
+ zCreate = sqlite3_mprintf("CREATE TABLE \"%w\".\"%w\"",
25716
+ zSchema ? zSchema : "main", zTable);
25717
25717
while( xRead(&sCtx) ){
25718
25718
zAutoColumn(sCtx.z, &dbCols, 0);
25719
25719
if( sCtx.cTerm!=sCtx.cColSep ) break;
@@ -25728,34 +25728,50 @@ static int do_meta_command(char *zLine, ShellState *p){
25728
25728
assert(dbCols==0);
25729
25729
if( zColDefs==0 ){
25730
25730
eputf("%s: empty file\n", sCtx.zFile);
25731
- import_fail:
25732
- sqlite3_free(zCreate);
25733
- sqlite3_free(zSql);
25734
- sqlite3_free(zFullTabName);
25735
25731
import_cleanup(&sCtx);
25736
25732
rc = 1;
25737
25733
goto meta_command_exit;
25738
25734
}
25739
25735
zCreate = sqlite3_mprintf("%z%z\n", zCreate, zColDefs);
25736
+ if( zCreate==0 ){
25737
+ import_cleanup(&sCtx);
25738
+ shell_out_of_memory();
25739
+ }
25740
25740
if( eVerbose>=1 ){
25741
25741
oputf("%s\n", zCreate);
25742
25742
}
25743
25743
rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
25744
+ sqlite3_free(zCreate);
25745
+ zCreate = 0;
25744
25746
if( rc ){
25745
25747
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;
25747
25751
}
25748
- sqlite3_free(zCreate);
25749
- zCreate = 0;
25750
- rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
25751
25752
}
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;
25752
25763
if( rc ){
25753
25764
if (pStmt) sqlite3_finalize(pStmt);
25754
25765
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;
25756
25774
}
25757
- sqlite3_free(zSql);
25758
- nCol = sqlite3_column_count(pStmt);
25759
25775
sqlite3_finalize(pStmt);
25760
25776
pStmt = 0;
25761
25777
if( nCol==0 ) return 0; /* no columns, no error */
@@ -25764,7 +25780,12 @@ static int do_meta_command(char *zLine, ShellState *p){
25764
25780
import_cleanup(&sCtx);
25765
25781
shell_out_of_memory();
25766
25782
}
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
+ }
25768
25789
j = strlen30(zSql);
25769
25790
for(i=1; i<nCol; i++){
25770
25791
zSql[j++] = ',';
@@ -25776,13 +25797,15 @@ static int do_meta_command(char *zLine, ShellState *p){
25776
25797
oputf("Insert using: %s\n", zSql);
25777
25798
}
25778
25799
rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
25800
+ sqlite3_free(zSql);
25801
+ zSql = 0;
25779
25802
if( rc ){
25780
25803
eputf("Error: %s\n", sqlite3_errmsg(p->db));
25781
25804
if (pStmt) sqlite3_finalize(pStmt);
25782
- goto import_fail;
25805
+ import_cleanup(&sCtx);
25806
+ rc = 1;
25807
+ goto meta_command_exit;
25783
25808
}
25784
- sqlite3_free(zSql);
25785
- sqlite3_free(zFullTabName);
25786
25809
needCommit = sqlite3_get_autocommit(p->db);
25787
25810
if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
25788
25811
do{
0 commit comments