| | @@ -35,11 +35,11 @@ |
| 35 | 35 | ** ext/recover/sqlite3recover.h |
| 36 | 36 | ** src/shell.c.in |
| 37 | 37 | ** |
| 38 | 38 | ** To modify this program, get a copy of the canonical SQLite source tree, |
| 39 | 39 | ** edit the src/shell.c.in file and/or some of the other files that are |
| 40 | | -** listed above, then rerun the rerun "make shell.c". |
| 40 | +** listed above, then rerun the command "make shell.c". |
| 41 | 41 | */ |
| 42 | 42 | /************************* Begin src/shell.c.in ******************/ |
| 43 | 43 | /* |
| 44 | 44 | ** 2001 September 15 |
| 45 | 45 | ** |
| | @@ -49,11 +49,11 @@ |
| 49 | 49 | ** May you do good and not evil. |
| 50 | 50 | ** May you find forgiveness for yourself and forgive others. |
| 51 | 51 | ** May you share freely, never taking more than you give. |
| 52 | 52 | ** |
| 53 | 53 | ************************************************************************* |
| 54 | | -** This file contains code to implement the "sqlite" command line |
| 54 | +** This file contains code to implement the "sqlite3" command line |
| 55 | 55 | ** utility for accessing SQLite databases. |
| 56 | 56 | */ |
| 57 | 57 | #if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS) |
| 58 | 58 | /* This needs to come before any includes for MSVC compiler */ |
| 59 | 59 | #define _CRT_SECURE_NO_WARNINGS |
| | @@ -4070,10 +4070,18 @@ |
| 4070 | 4070 | #define PROMPT_LEN_MAX 128 |
| 4071 | 4071 | /* First line prompt. default: "sqlite> " */ |
| 4072 | 4072 | static char mainPrompt[PROMPT_LEN_MAX]; |
| 4073 | 4073 | /* Continuation prompt. default: " ...> " */ |
| 4074 | 4074 | static char continuePrompt[PROMPT_LEN_MAX]; |
| 4075 | + |
| 4076 | +/* |
| 4077 | +** Write I/O traces to the following stream. |
| 4078 | +*/ |
| 4079 | +#ifdef SQLITE_ENABLE_IOTRACE |
| 4080 | +static FILE *iotrace = 0; |
| 4081 | +#endif |
| 4082 | + |
| 4075 | 4083 | |
| 4076 | 4084 | /* This is variant of the standard-library strncpy() routine with the |
| 4077 | 4085 | ** one change that the destination string is always zero-terminated, even |
| 4078 | 4086 | ** if there is no zero-terminator in the first n-1 characters of the source |
| 4079 | 4087 | ** string. |
| | @@ -4190,17 +4198,10 @@ |
| 4190 | 4198 | */ |
| 4191 | 4199 | static void shell_check_oom(const void *p){ |
| 4192 | 4200 | if( p==0 ) shell_out_of_memory(); |
| 4193 | 4201 | } |
| 4194 | 4202 | |
| 4195 | | -/* |
| 4196 | | -** Write I/O traces to the following stream. |
| 4197 | | -*/ |
| 4198 | | -#ifdef SQLITE_ENABLE_IOTRACE |
| 4199 | | -static FILE *iotrace = 0; |
| 4200 | | -#endif |
| 4201 | | - |
| 4202 | 4203 | /* |
| 4203 | 4204 | ** This routine works like printf in that its first argument is a |
| 4204 | 4205 | ** format string and subsequent arguments are values to be substituted |
| 4205 | 4206 | ** in place of % fields. The result of formatting this string |
| 4206 | 4207 | ** is written to iotrace. |
| | @@ -8351,13 +8352,13 @@ |
| 8351 | 8352 | }else if( e<-10000 ){ |
| 8352 | 8353 | e = -10000; |
| 8353 | 8354 | } |
| 8354 | 8355 | |
| 8355 | 8356 | if( m<0 ){ |
| 8357 | + if( m<(-9223372036854775807LL) ) return; |
| 8356 | 8358 | isNeg = 1; |
| 8357 | 8359 | m = -m; |
| 8358 | | - if( m<0 ) return; |
| 8359 | 8360 | }else if( m==0 && e>-1000 && e<1000 ){ |
| 8360 | 8361 | sqlite3_result_double(context, 0.0); |
| 8361 | 8362 | return; |
| 8362 | 8363 | } |
| 8363 | 8364 | while( (m>>32)&0xffe00000 ){ |
| | @@ -26848,19 +26849,31 @@ |
| 26848 | 26849 | "USAGE: .import [OPTIONS] FILE TABLE\n" |
| 26849 | 26850 | "\n" |
| 26850 | 26851 | "Import CSV or similar text from FILE into TABLE. If TABLE does\n" |
| 26851 | 26852 | "not exist, it is created using the first row of FILE as the column\n" |
| 26852 | 26853 | "names. If FILE begins with \"|\" then it is a command that is run\n" |
| 26853 | | -"and the output from the command is used as the input data.\n" |
| 26854 | +"and the output from the command is used as the input data. If\n" |
| 26855 | +"FILE begins with \"<<\" followed by a label, then content is read from\n" |
| 26856 | +"the script until the first line that matches the label.\n" |
| 26857 | +"\n" |
| 26858 | +"The content of FILE is interpreted using RFC-4180 (\"CSV\") quoting\n" |
| 26859 | +"rules unless the current mode is \"ascii\" or \"tabs\" or unless one\n" |
| 26860 | +"the --ascii option is used.\n" |
| 26854 | 26861 | "\n" |
| 26855 | | -"FILE is assumed to be in a CSV format, unless the current mode\n" |
| 26856 | | -"is \"ascii\" or \"tabs\" or unless one of the options below specify\n" |
| 26857 | | -"an alternative.\n" |
| 26862 | +"The column and row separators must be single ASCII characters. If\n" |
| 26863 | +"multiple characters or a Unicode character are specified for the\n" |
| 26864 | +"separators, then only the first byte of the separator is used. Except,\n" |
| 26865 | +"if the row separator is \\n and the mode is not --ascii, then \\r\\n is\n" |
| 26866 | +"understood as a row separator too.\n" |
| 26858 | 26867 | "\n" |
| 26859 | 26868 | "Options:\n" |
| 26860 | | -" --ascii Use \\037 and \\036 as column and row separators on input\n" |
| 26869 | +" --ascii Do not use RFC-4180 quoting. Use \\037 and \\036\n" |
| 26870 | +" as column and row separators on input, unless other\n" |
| 26871 | +" delimiters are specified using --colsep and/or --rowsep\n" |
| 26872 | +" --colsep CHAR Use CHAR as the column separator.\n" |
| 26861 | 26873 | " --csv Input is standard RFC-4180 CSV.\n" |
| 26874 | +" --rowsep CHAR Use CHAR as the row separator.\n" |
| 26862 | 26875 | " --schema S When creating TABLE, put it in schema S\n" |
| 26863 | 26876 | " --skip N Ignore the first N rows of input\n" |
| 26864 | 26877 | " -v Verbose mode\n" |
| 26865 | 26878 | }, |
| 26866 | 26879 | { ".mode", |
| | @@ -26987,11 +27000,10 @@ |
| 26987 | 27000 | "If PATTERN starts with \"<<ENDMARK\" then the actual pattern is taken from\n" |
| 26988 | 27001 | "subsequent lines of text up to the first line that begins with ENDMARK.\n" |
| 26989 | 27002 | "All pattern lines and the ENDMARK are discarded.\n" |
| 26990 | 27003 | "\n" |
| 26991 | 27004 | "Options:\n" |
| 26992 | | -" --error-prefix TEXT Change error message prefix text to TEXT\n" |
| 26993 | 27005 | " --exact Do an exact comparison including leading and\n" |
| 26994 | 27006 | " trailing whitespace.\n" |
| 26995 | 27007 | " --glob Treat PATTERN as a GLOB\n" |
| 26996 | 27008 | " --keep Do not reset the testcase. More .check commands\n" |
| 26997 | 27009 | " will follow.\n" |
| | @@ -28000,11 +28012,13 @@ |
| 28000 | 28012 | typedef struct ImportCtx ImportCtx; |
| 28001 | 28013 | struct ImportCtx { |
| 28002 | 28014 | const char *zFile; /* Name of the input file */ |
| 28003 | 28015 | FILE *in; /* Read the CSV text from this input stream */ |
| 28004 | 28016 | int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close in */ |
| 28017 | + char *zIn; /* Input text */ |
| 28005 | 28018 | char *z; /* Accumulated text for a field */ |
| 28019 | + i64 nUsed; /* Bytes of zIn[] used so far */ |
| 28006 | 28020 | i64 n; /* Number of bytes in z */ |
| 28007 | 28021 | i64 nAlloc; /* Space allocated for z[] */ |
| 28008 | 28022 | int nLine; /* Current line number */ |
| 28009 | 28023 | int nRow; /* Number of rows imported */ |
| 28010 | 28024 | int nErr; /* Number of errors encountered */ |
| | @@ -28020,13 +28034,32 @@ |
| 28020 | 28034 | p->xCloser(p->in); |
| 28021 | 28035 | p->in = 0; |
| 28022 | 28036 | } |
| 28023 | 28037 | sqlite3_free(p->z); |
| 28024 | 28038 | p->z = 0; |
| 28039 | + if( p->zIn ){ |
| 28040 | + sqlite3_free(p->zIn); |
| 28041 | + p->zIn = 0; |
| 28042 | + } |
| 28025 | 28043 | } |
| 28026 | 28044 | |
| 28027 | | -/* Append a single byte to z[] */ |
| 28045 | +/* Read a single character of the .import input text. Return EOF |
| 28046 | +** at end-of-file. |
| 28047 | +*/ |
| 28048 | +static int import_getc(ImportCtx *p){ |
| 28049 | + if( p->in ){ |
| 28050 | + return fgetc(p->in); |
| 28051 | + }else if( p->zIn && p->zIn[p->nUsed]!=0 ){ |
| 28052 | + return p->zIn[p->nUsed++]; |
| 28053 | + }else{ |
| 28054 | + return EOF; |
| 28055 | + } |
| 28056 | +} |
| 28057 | + |
| 28058 | +/* Append a single byte to the field value begin constructed |
| 28059 | +** in the p->z[] buffer |
| 28060 | +*/ |
| 28028 | 28061 | static void import_append_char(ImportCtx *p, int c){ |
| 28029 | 28062 | if( p->n+1>=p->nAlloc ){ |
| 28030 | 28063 | p->nAlloc += p->nAlloc + 100; |
| 28031 | 28064 | p->z = sqlite3_realloc64(p->z, p->nAlloc); |
| 28032 | 28065 | shell_check_oom(p->z); |
| | @@ -28038,12 +28071,12 @@ |
| 28038 | 28071 | ** with the option of having a separator other than ",". |
| 28039 | 28072 | ** |
| 28040 | 28073 | ** + Input comes from p->in. |
| 28041 | 28074 | ** + Store results in p->z of length p->n. Space to hold p->z comes |
| 28042 | 28075 | ** from sqlite3_malloc64(). |
| 28043 | | -** + Use p->cSep as the column separator. The default is ",". |
| 28044 | | -** + Use p->rSep as the row separator. The default is "\n". |
| 28076 | +** + Use p->cColSep as the column separator. The default is ",". |
| 28077 | +** + Use p->cRowSep as the row separator. The default is "\n". |
| 28045 | 28078 | ** + Keep track of the line number in p->nLine. |
| 28046 | 28079 | ** + Store the character that terminates the field in p->cTerm. Store |
| 28047 | 28080 | ** EOF on end-of-file. |
| 28048 | 28081 | ** + Report syntax errors on stderr |
| 28049 | 28082 | */ |
| | @@ -28050,11 +28083,11 @@ |
| 28050 | 28083 | static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){ |
| 28051 | 28084 | int c; |
| 28052 | 28085 | int cSep = (u8)p->cColSep; |
| 28053 | 28086 | int rSep = (u8)p->cRowSep; |
| 28054 | 28087 | p->n = 0; |
| 28055 | | - c = fgetc(p->in); |
| 28088 | + c = import_getc(p); |
| 28056 | 28089 | if( c==EOF || seenInterrupt ){ |
| 28057 | 28090 | p->cTerm = EOF; |
| 28058 | 28091 | return 0; |
| 28059 | 28092 | } |
| 28060 | 28093 | if( c=='"' ){ |
| | @@ -28061,11 +28094,11 @@ |
| 28061 | 28094 | int pc, ppc; |
| 28062 | 28095 | int startLine = p->nLine; |
| 28063 | 28096 | int cQuote = c; |
| 28064 | 28097 | pc = ppc = 0; |
| 28065 | 28098 | while( 1 ){ |
| 28066 | | - c = fgetc(p->in); |
| 28099 | + c = import_getc(p); |
| 28067 | 28100 | if( c==rSep ) p->nLine++; |
| 28068 | 28101 | if( c==cQuote ){ |
| 28069 | 28102 | if( pc==cQuote ){ |
| 28070 | 28103 | pc = 0; |
| 28071 | 28104 | continue; |
| | @@ -28097,24 +28130,24 @@ |
| 28097 | 28130 | }else{ |
| 28098 | 28131 | /* If this is the first field being parsed and it begins with the |
| 28099 | 28132 | ** UTF-8 BOM (0xEF BB BF) then skip the BOM */ |
| 28100 | 28133 | if( (c&0xff)==0xef && p->bNotFirst==0 ){ |
| 28101 | 28134 | import_append_char(p, c); |
| 28102 | | - c = fgetc(p->in); |
| 28135 | + c = import_getc(p); |
| 28103 | 28136 | if( (c&0xff)==0xbb ){ |
| 28104 | 28137 | import_append_char(p, c); |
| 28105 | | - c = fgetc(p->in); |
| 28138 | + c = import_getc(p); |
| 28106 | 28139 | if( (c&0xff)==0xbf ){ |
| 28107 | 28140 | p->bNotFirst = 1; |
| 28108 | 28141 | p->n = 0; |
| 28109 | 28142 | return csv_read_one_field(p); |
| 28110 | 28143 | } |
| 28111 | 28144 | } |
| 28112 | 28145 | } |
| 28113 | 28146 | while( c!=EOF && c!=cSep && c!=rSep ){ |
| 28114 | 28147 | import_append_char(p, c); |
| 28115 | | - c = fgetc(p->in); |
| 28148 | + c = import_getc(p); |
| 28116 | 28149 | } |
| 28117 | 28150 | if( c==rSep ){ |
| 28118 | 28151 | p->nLine++; |
| 28119 | 28152 | if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--; |
| 28120 | 28153 | } |
| | @@ -28128,12 +28161,12 @@ |
| 28128 | 28161 | /* Read a single field of ASCII delimited text. |
| 28129 | 28162 | ** |
| 28130 | 28163 | ** + Input comes from p->in. |
| 28131 | 28164 | ** + Store results in p->z of length p->n. Space to hold p->z comes |
| 28132 | 28165 | ** from sqlite3_malloc64(). |
| 28133 | | -** + Use p->cSep as the column separator. The default is "\x1F". |
| 28134 | | -** + Use p->rSep as the row separator. The default is "\x1E". |
| 28166 | +** + Use p->cColSep as the column separator. The default is "\x1F". |
| 28167 | +** + Use p->cRowSep as the row separator. The default is "\x1E". |
| 28135 | 28168 | ** + Keep track of the row number in p->nLine. |
| 28136 | 28169 | ** + Store the character that terminates the field in p->cTerm. Store |
| 28137 | 28170 | ** EOF on end-of-file. |
| 28138 | 28171 | ** + Report syntax errors on stderr |
| 28139 | 28172 | */ |
| | @@ -28140,18 +28173,18 @@ |
| 28140 | 28173 | static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){ |
| 28141 | 28174 | int c; |
| 28142 | 28175 | int cSep = (u8)p->cColSep; |
| 28143 | 28176 | int rSep = (u8)p->cRowSep; |
| 28144 | 28177 | p->n = 0; |
| 28145 | | - c = fgetc(p->in); |
| 28178 | + c = import_getc(p); |
| 28146 | 28179 | if( c==EOF || seenInterrupt ){ |
| 28147 | 28180 | p->cTerm = EOF; |
| 28148 | 28181 | return 0; |
| 28149 | 28182 | } |
| 28150 | 28183 | while( c!=EOF && c!=cSep && c!=rSep ){ |
| 28151 | 28184 | import_append_char(p, c); |
| 28152 | | - c = fgetc(p->in); |
| 28185 | + c = import_getc(p); |
| 28153 | 28186 | } |
| 28154 | 28187 | if( c==rSep ){ |
| 28155 | 28188 | p->nLine++; |
| 28156 | 28189 | } |
| 28157 | 28190 | p->cTerm = c; |
| | @@ -30452,19 +30485,31 @@ |
| 30452 | 30485 | ** USAGE: .import [OPTIONS] FILE TABLE |
| 30453 | 30486 | ** |
| 30454 | 30487 | ** Import CSV or similar text from FILE into TABLE. If TABLE does |
| 30455 | 30488 | ** not exist, it is created using the first row of FILE as the column |
| 30456 | 30489 | ** names. If FILE begins with "|" then it is a command that is run |
| 30457 | | -** and the output from the command is used as the input data. |
| 30490 | +** and the output from the command is used as the input data. If |
| 30491 | +** FILE begins with "<<" followed by a label, then content is read from |
| 30492 | +** the script until the first line that matches the label. |
| 30493 | +** |
| 30494 | +** The content of FILE is interpreted using RFC-4180 ("CSV") quoting |
| 30495 | +** rules unless the current mode is "ascii" or "tabs" or unless one |
| 30496 | +** the --ascii option is used. |
| 30458 | 30497 | ** |
| 30459 | | -** FILE is assumed to be in a CSV format, unless the current mode |
| 30460 | | -** is "ascii" or "tabs" or unless one of the options below specify |
| 30461 | | -** an alternative. |
| 30498 | +** The column and row separators must be single ASCII characters. If |
| 30499 | +** multiple characters or a Unicode character are specified for the |
| 30500 | +** separators, then only the first byte of the separator is used. Except, |
| 30501 | +** if the row separator is \n and the mode is not --ascii, then \r\n is |
| 30502 | +** understood as a row separator too. |
| 30462 | 30503 | ** |
| 30463 | 30504 | ** Options: |
| 30464 | | -** --ascii Use \037 and \036 as column and row separators on input |
| 30505 | +** --ascii Do not use RFC-4180 quoting. Use \037 and \036 |
| 30506 | +** as column and row separators on input, unless other |
| 30507 | +** delimiters are specified using --colsep and/or --rowsep |
| 30508 | +** --colsep CHAR Use CHAR as the column separator. |
| 30465 | 30509 | ** --csv Input is standard RFC-4180 CSV. |
| 30510 | +** --rowsep CHAR Use CHAR as the row separator. |
| 30466 | 30511 | ** --schema S When creating TABLE, put it in schema S |
| 30467 | 30512 | ** --skip N Ignore the first N rows of input |
| 30468 | 30513 | ** -v Verbose mode |
| 30469 | 30514 | */ |
| 30470 | 30515 | static int dotCmdImport(ShellState *p){ |
| | @@ -30476,17 +30521,16 @@ |
| 30476 | 30521 | sqlite3_stmt *pStmt = NULL; /* A statement */ |
| 30477 | 30522 | int nCol; /* Number of columns in the table */ |
| 30478 | 30523 | i64 nByte; /* Number of bytes in an SQL string */ |
| 30479 | 30524 | int i, j; /* Loop counters */ |
| 30480 | 30525 | int needCommit; /* True to COMMIT or ROLLBACK at end */ |
| 30481 | | - int nSep; /* Number of bytes in spec.zColumnSep */ |
| 30482 | 30526 | char *zSql = 0; /* An SQL statement */ |
| 30483 | 30527 | ImportCtx sCtx; /* Reader context */ |
| 30484 | 30528 | char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */ |
| 30485 | 30529 | int eVerbose = 0; /* Larger for more console output */ |
| 30486 | 30530 | i64 nSkip = 0; /* Initial lines to skip */ |
| 30487 | | - int useOutputMode = 1; /* Use output mode to determine separators */ |
| 30531 | + i64 iLineOffset = 0; /* Offset to the first line of input */ |
| 30488 | 30532 | char *zCreate = 0; /* CREATE TABLE statement text */ |
| 30489 | 30533 | int rc; /* Result code */ |
| 30490 | 30534 | |
| 30491 | 30535 | failIfSafeMode(p, "cannot run .import in safe mode"); |
| 30492 | 30536 | memset(&sCtx, 0, sizeof(sCtx)); |
| | @@ -30512,70 +30556,67 @@ |
| 30512 | 30556 | }else if( cli_strcmp(z,"-schema")==0 && i<nArg-1 ){ |
| 30513 | 30557 | zSchema = azArg[++i]; |
| 30514 | 30558 | }else if( cli_strcmp(z,"-skip")==0 && i<nArg-1 ){ |
| 30515 | 30559 | nSkip = integerValue(azArg[++i]); |
| 30516 | 30560 | }else if( cli_strcmp(z,"-ascii")==0 ){ |
| 30517 | | - sCtx.cColSep = SEP_Unit[0]; |
| 30518 | | - sCtx.cRowSep = SEP_Record[0]; |
| 30561 | + if( sCtx.cColSep==0 ) sCtx.cColSep = SEP_Unit[0]; |
| 30562 | + if( sCtx.cRowSep==0 ) sCtx.cRowSep = SEP_Record[0]; |
| 30519 | 30563 | xRead = ascii_read_one_field; |
| 30520 | | - useOutputMode = 0; |
| 30521 | 30564 | }else if( cli_strcmp(z,"-csv")==0 ){ |
| 30522 | | - sCtx.cColSep = ','; |
| 30523 | | - sCtx.cRowSep = '\n'; |
| 30565 | + if( sCtx.cColSep==0 ) sCtx.cColSep = ','; |
| 30566 | + if( sCtx.cRowSep==0 ) sCtx.cRowSep = '\n'; |
| 30524 | 30567 | xRead = csv_read_one_field; |
| 30525 | | - useOutputMode = 0; |
| 30568 | + }else if( cli_strcmp(z,"-colsep")==0 ){ |
| 30569 | + if( i==nArg-1 ){ |
| 30570 | + dotCmdError(p, i, "missing argument", 0); |
| 30571 | + return 1; |
| 30572 | + } |
| 30573 | + i++; |
| 30574 | + sCtx.cColSep = azArg[i][0]; |
| 30575 | + }else if( cli_strcmp(z,"-rowsep")==0 ){ |
| 30576 | + if( i==nArg-1 ){ |
| 30577 | + dotCmdError(p, i, "missing argument", 0); |
| 30578 | + return 1; |
| 30579 | + } |
| 30580 | + i++; |
| 30581 | + sCtx.cRowSep = azArg[i][0]; |
| 30526 | 30582 | }else{ |
| 30527 | 30583 | dotCmdError(p, i, "unknown option", 0); |
| 30528 | 30584 | return 1; |
| 30529 | 30585 | } |
| 30530 | 30586 | } |
| 30531 | 30587 | if( zTable==0 ){ |
| 30532 | | - cli_printf(p->out, "ERROR: missing %s argument\n", |
| 30588 | + dotCmdError(p, nArg, 0, "Missing %s argument\n", |
| 30533 | 30589 | zFile==0 ? "FILE" : "TABLE"); |
| 30534 | 30590 | return 1; |
| 30535 | 30591 | } |
| 30536 | 30592 | seenInterrupt = 0; |
| 30537 | 30593 | open_db(p, 0); |
| 30538 | | - if( useOutputMode ){ |
| 30539 | | - /* If neither the --csv or --ascii options are specified, then set |
| 30540 | | - ** the column and row separator characters from the output mode. */ |
| 30541 | | - if( p->mode.spec.zColumnSep==0 ){ |
| 30542 | | - modeSetStr(&p->mode.spec.zColumnSep, ","); |
| 30543 | | - nSep = 1; |
| 30544 | | - }else if( (nSep = strlen30(p->mode.spec.zColumnSep))==0 ){ |
| 30545 | | - eputz("Error: non-null column separator required for import\n"); |
| 30546 | | - return 1; |
| 30547 | | - } |
| 30548 | | - if( nSep>1 ){ |
| 30549 | | - eputz("Error: multi-character column separators not allowed" |
| 30550 | | - " for import\n"); |
| 30551 | | - return 1; |
| 30552 | | - } |
| 30553 | | - if( p->mode.spec.zRowSep==0 ){ |
| 30554 | | - modeSetStr(&p->mode.spec.zRowSep, "\n"); |
| 30555 | | - nSep = 1; |
| 30556 | | - }else if( (nSep = strlen30(p->mode.spec.zRowSep))==0 ){ |
| 30557 | | - eputz("Error: non-null row separator required for import\n"); |
| 30558 | | - return 1; |
| 30559 | | - } |
| 30560 | | - if( nSep==2 && p->mode.eMode==MODE_Csv |
| 30561 | | - && cli_strcmp(p->mode.spec.zRowSep,SEP_CrLf)==0 |
| 30562 | | - ){ |
| 30563 | | - /* When importing CSV (only), if the row separator is set to the |
| 30564 | | - ** default output row separator, change it to the default input |
| 30565 | | - ** row separator. This avoids having to maintain different input |
| 30566 | | - ** and output row separators. */ |
| 30567 | | - modeSetStr(&p->mode.spec.zRowSep, SEP_Row); |
| 30568 | | - nSep = strlen30(p->mode.spec.zRowSep); |
| 30569 | | - } |
| 30570 | | - if( nSep>1 ){ |
| 30571 | | - eputz("Error: multi-character row separators not allowed" |
| 30572 | | - " for import\n"); |
| 30573 | | - return 1; |
| 30574 | | - } |
| 30575 | | - sCtx.cColSep = (u8)p->mode.spec.zColumnSep[0]; |
| 30576 | | - sCtx.cRowSep = (u8)p->mode.spec.zRowSep[0]; |
| 30594 | + if( sCtx.cColSep==0 ){ |
| 30595 | + if( p->mode.spec.zColumnSep && p->mode.spec.zColumnSep[0]!=0 ){ |
| 30596 | + sCtx.cColSep = p->mode.spec.zColumnSep[0]; |
| 30597 | + }else{ |
| 30598 | + sCtx.cColSep = ','; |
| 30599 | + } |
| 30600 | + } |
| 30601 | + if( (sCtx.cColSep & 0x80)!=0 ){ |
| 30602 | + eputz("Error: .import column separator must be ASCII\n"); |
| 30603 | + return 1; |
| 30604 | + } |
| 30605 | + if( sCtx.cRowSep==0 ){ |
| 30606 | + if( p->mode.spec.zRowSep && p->mode.spec.zRowSep[0]!=0 ){ |
| 30607 | + sCtx.cRowSep = p->mode.spec.zRowSep[0]; |
| 30608 | + }else{ |
| 30609 | + sCtx.cRowSep = '\n'; |
| 30610 | + } |
| 30611 | + } |
| 30612 | + if( sCtx.cRowSep=='\r' && xRead!=ascii_read_one_field ){ |
| 30613 | + sCtx.cRowSep = '\n'; |
| 30614 | + } |
| 30615 | + if( (sCtx.cRowSep & 0x80)!=0 ){ |
| 30616 | + eputz("Error: .import row separator must be ASCII\n"); |
| 30617 | + return 1; |
| 30577 | 30618 | } |
| 30578 | 30619 | sCtx.zFile = zFile; |
| 30579 | 30620 | sCtx.nLine = 1; |
| 30580 | 30621 | if( sCtx.zFile[0]=='|' ){ |
| 30581 | 30622 | #ifdef SQLITE_OMIT_POPEN |
| | @@ -30584,19 +30625,58 @@ |
| 30584 | 30625 | #else |
| 30585 | 30626 | sCtx.in = sqlite3_popen(sCtx.zFile+1, "r"); |
| 30586 | 30627 | sCtx.zFile = "<pipe>"; |
| 30587 | 30628 | sCtx.xCloser = pclose; |
| 30588 | 30629 | #endif |
| 30630 | + }else if( sCtx.zFile[0]=='<' && sCtx.zFile[1]=='<' && sCtx.zFile[2]!=0 ){ |
| 30631 | + /* Input text comes from subsequent lines of script until the zFile |
| 30632 | + ** delimiter */ |
| 30633 | + int nEndMark = strlen30(zFile)-2; |
| 30634 | + char *zEndMark = &zFile[2]; |
| 30635 | + sqlite3_str *pContent = sqlite3_str_new(p->db); |
| 30636 | + int ckEnd = 1; |
| 30637 | + i64 iStart = p->lineno; |
| 30638 | + char zLine[2000]; |
| 30639 | + sCtx.zFile = p->zInFile; |
| 30640 | + sCtx.nLine = p->lineno+1; |
| 30641 | + iLineOffset = p->lineno; |
| 30642 | + while( sqlite3_fgets(zLine,sizeof(zLine),p->in) ){ |
| 30643 | + if( ckEnd && cli_strncmp(zLine,zEndMark,nEndMark)==0 ){ |
| 30644 | + ckEnd = 2; |
| 30645 | + if( strchr(zLine,'\n') ) p->lineno++; |
| 30646 | + break; |
| 30647 | + } |
| 30648 | + if( strchr(zLine,'\n') ){ |
| 30649 | + p->lineno++; |
| 30650 | + ckEnd = 1; |
| 30651 | + }else{ |
| 30652 | + ckEnd = 0; |
| 30653 | + } |
| 30654 | + sqlite3_str_appendall(pContent, zLine); |
| 30655 | + } |
| 30656 | + sCtx.zIn = sqlite3_str_finish(pContent); |
| 30657 | + if( sCtx.zIn==0 ){ |
| 30658 | + sCtx.zIn = sqlite3_mprintf(""); |
| 30659 | + } |
| 30660 | + if( ckEnd<2 ){ |
| 30661 | + i64 savedLn = p->lineno; |
| 30662 | + p->lineno = iStart; |
| 30663 | + dotCmdError(p, 0, 0,"Content terminator \"%s\" not found.",zEndMark); |
| 30664 | + p->lineno = savedLn; |
| 30665 | + import_cleanup(&sCtx); |
| 30666 | + return 1; |
| 30667 | + } |
| 30589 | 30668 | }else{ |
| 30590 | 30669 | sCtx.in = sqlite3_fopen(sCtx.zFile, "rb"); |
| 30591 | 30670 | sCtx.xCloser = fclose; |
| 30592 | 30671 | } |
| 30593 | | - if( sCtx.in==0 ){ |
| 30594 | | - cli_printf(stderr,"Error: cannot open \"%s\"\n", zFile); |
| 30672 | + if( sCtx.in==0 && sCtx.zIn==0 ){ |
| 30673 | + dotCmdError(p, 0, 0, "cannot open \"%s\"", zFile); |
| 30674 | + import_cleanup(&sCtx); |
| 30595 | 30675 | return 1; |
| 30596 | 30676 | } |
| 30597 | | - if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){ |
| 30677 | + if( eVerbose>=1 ){ |
| 30598 | 30678 | char zSep[2]; |
| 30599 | 30679 | zSep[1] = 0; |
| 30600 | 30680 | zSep[0] = sCtx.cColSep; |
| 30601 | 30681 | cli_puts("Column separator ", p->out); |
| 30602 | 30682 | output_c_string(p->out, zSep); |
| | @@ -30749,10 +30829,14 @@ |
| 30749 | 30829 | if( z==0 && (xRead==csv_read_one_field) && i==nCol-1 && i>0 ){ |
| 30750 | 30830 | z = ""; |
| 30751 | 30831 | } |
| 30752 | 30832 | sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT); |
| 30753 | 30833 | if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){ |
| 30834 | + if( i==0 && (strcmp(z,"\n")==0 || strcmp(z,"\r\n")==0) ){ |
| 30835 | + /* Ignore trailing \n or \r\n when some other row separator */ |
| 30836 | + break; |
| 30837 | + } |
| 30754 | 30838 | cli_printf(stderr,"%s:%d: expected %d columns but found %d" |
| 30755 | 30839 | " - filling the rest with NULL\n", |
| 30756 | 30840 | sCtx.zFile, startLine, nCol, i+1); |
| 30757 | 30841 | i += 2; |
| 30758 | 30842 | while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; } |
| | @@ -30772,10 +30856,11 @@ |
| 30772 | 30856 | rc = sqlite3_reset(pStmt); |
| 30773 | 30857 | if( rc!=SQLITE_OK ){ |
| 30774 | 30858 | cli_printf(stderr,"%s:%d: INSERT failed: %s\n", |
| 30775 | 30859 | sCtx.zFile, startLine, sqlite3_errmsg(p->db)); |
| 30776 | 30860 | sCtx.nErr++; |
| 30861 | + if( bail_on_error ) break; |
| 30777 | 30862 | }else{ |
| 30778 | 30863 | sCtx.nRow++; |
| 30779 | 30864 | } |
| 30780 | 30865 | } |
| 30781 | 30866 | }while( sCtx.cTerm!=EOF ); |
| | @@ -30784,13 +30869,13 @@ |
| 30784 | 30869 | sqlite3_finalize(pStmt); |
| 30785 | 30870 | if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0); |
| 30786 | 30871 | if( eVerbose>0 ){ |
| 30787 | 30872 | cli_printf(p->out, |
| 30788 | 30873 | "Added %d rows with %d errors using %d lines of input\n", |
| 30789 | | - sCtx.nRow, sCtx.nErr, sCtx.nLine-1); |
| 30874 | + sCtx.nRow, sCtx.nErr, sCtx.nLine-1-iLineOffset); |
| 30790 | 30875 | } |
| 30791 | | - return 0; |
| 30876 | + return sCtx.nErr ? 1 : 0; |
| 30792 | 30877 | } |
| 30793 | 30878 | |
| 30794 | 30879 | |
| 30795 | 30880 | /* |
| 30796 | 30881 | ** This function computes what to show the user about the configured |
| | @@ -31657,11 +31742,10 @@ |
| 31657 | 31742 | ** If PATTERN starts with "<<ENDMARK" then the actual pattern is taken from |
| 31658 | 31743 | ** subsequent lines of text up to the first line that begins with ENDMARK. |
| 31659 | 31744 | ** All pattern lines and the ENDMARK are discarded. |
| 31660 | 31745 | ** |
| 31661 | 31746 | ** Options: |
| 31662 | | -** --error-prefix TEXT Change error message prefix text to TEXT |
| 31663 | 31747 | ** --exact Do an exact comparison including leading and |
| 31664 | 31748 | ** trailing whitespace. |
| 31665 | 31749 | ** --glob Treat PATTERN as a GLOB |
| 31666 | 31750 | ** --keep Do not reset the testcase. More .check commands |
| 31667 | 31751 | ** will follow. |
| | @@ -31712,11 +31796,11 @@ |
| 31712 | 31796 | } |
| 31713 | 31797 | if( zCheck==0 ){ |
| 31714 | 31798 | dotCmdError(p, 0, "no PATTERN specified", 0); |
| 31715 | 31799 | return 1; |
| 31716 | 31800 | } |
| 31717 | | - if( cli_output_capture ){ |
| 31801 | + if( cli_output_capture && sqlite3_str_length(cli_output_capture) ){ |
| 31718 | 31802 | zTest = sqlite3_str_value(cli_output_capture); |
| 31719 | 31803 | shell_check_oom(zTest); |
| 31720 | 31804 | }else{ |
| 31721 | 31805 | zTest = ""; |
| 31722 | 31806 | } |
| | @@ -33450,12 +33534,11 @@ |
| 33450 | 33534 | " rootpage integer,\n" |
| 33451 | 33535 | " sql text\n" |
| 33452 | 33536 | ");\n", zName); |
| 33453 | 33537 | } |
| 33454 | 33538 | } |
| 33455 | | - rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list", |
| 33456 | | - -1, &pStmt, 0); |
| 33539 | + rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); |
| 33457 | 33540 | if( rc ){ |
| 33458 | 33541 | shellDatabaseError(p->db); |
| 33459 | 33542 | sqlite3_finalize(pStmt); |
| 33460 | 33543 | |
| 33461 | 33544 | rc = 1; |
| | @@ -33463,11 +33546,11 @@ |
| 33463 | 33546 | } |
| 33464 | 33547 | pSql = sqlite3_str_new(p->db); |
| 33465 | 33548 | sqlite3_str_appendf(pSql, "SELECT sql FROM", 0); |
| 33466 | 33549 | iSchema = 0; |
| 33467 | 33550 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 33468 | | - const char *zDb = (const char*)sqlite3_column_text(pStmt, 0); |
| 33551 | + const char *zDb = (const char*)sqlite3_column_text(pStmt, 1); |
| 33469 | 33552 | char zScNum[30]; |
| 33470 | 33553 | sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema); |
| 33471 | 33554 | sqlite3_str_appendall(pSql, zDiv); |
| 33472 | 33555 | zDiv = " UNION ALL "; |
| 33473 | 33556 | if( sqlite3_stricmp(zDb, "main")==0 ){ |
| | @@ -33483,11 +33566,12 @@ |
| 33483 | 33566 | " AS sql, type, tbl_name, name, rowid, %d AS snum, %Q as sname", |
| 33484 | 33567 | ++iSchema, zDb); |
| 33485 | 33568 | sqlite3_str_appendf(pSql," FROM \"%w\".sqlite_schema", zDb); |
| 33486 | 33569 | } |
| 33487 | 33570 | sqlite3_finalize(pStmt); |
| 33488 | | -#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS |
| 33571 | +#if !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS) \ |
| 33572 | + && !defined(SQLITE_OMIT_VIRTUALTABLE) |
| 33489 | 33573 | if( zName ){ |
| 33490 | 33574 | sqlite3_str_appendall(pSql, |
| 33491 | 33575 | " UNION ALL SELECT shell_module_schema(name)," |
| 33492 | 33576 | " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list"); |
| 33493 | 33577 | } |
| | @@ -35690,29 +35774,65 @@ |
| 35690 | 35774 | cli_puts(z, p->out); |
| 35691 | 35775 | fflush(p->out); |
| 35692 | 35776 | return 1; |
| 35693 | 35777 | } |
| 35694 | 35778 | |
| 35695 | | -#ifndef SQLITE_SHELL_IS_UTF8 |
| 35696 | | -# if (defined(_WIN32) || defined(WIN32)) \ |
| 35697 | | - && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__))) |
| 35698 | | -# define SQLITE_SHELL_IS_UTF8 (0) |
| 35699 | | -# else |
| 35700 | | -# define SQLITE_SHELL_IS_UTF8 (1) |
| 35701 | | -# endif |
| 35702 | | -#endif |
| 35703 | | - |
| 35779 | +/* Alternative name to the entry point for Fiddle */ |
| 35704 | 35780 | #ifdef SQLITE_SHELL_FIDDLE |
| 35705 | 35781 | # define main fiddle_main |
| 35706 | 35782 | #endif |
| 35707 | 35783 | |
| 35708 | | -#if SQLITE_SHELL_IS_UTF8 |
| 35709 | | -int SQLITE_CDECL main(int argc, char **argv){ |
| 35710 | | -#else |
| 35784 | +/* Use the wmain() entry point on Windows. Translate arguments to |
| 35785 | +** UTF8, then invoke the traditional main() entry point which is |
| 35786 | +** renamed using a #define to utf8_main() . |
| 35787 | +*/ |
| 35788 | +#if defined(_WIN32) && !defined(main) |
| 35789 | +# define main utf8_main /* Rename entry point to utf_main() */ |
| 35790 | +int SQLITE_CDECL utf8_main(int,char**); /* Forward declaration */ |
| 35711 | 35791 | int SQLITE_CDECL wmain(int argc, wchar_t **wargv){ |
| 35712 | | - char **argv; |
| 35713 | | -#endif |
| 35792 | + int rc, i; |
| 35793 | + char **argv = malloc( sizeof(char*) * (argc+1) ); |
| 35794 | + char **orig = argv; |
| 35795 | + if( argv==0 ){ |
| 35796 | + fprintf(stderr, "malloc failed\n"); |
| 35797 | + exit(1); |
| 35798 | + } |
| 35799 | + for(i=0; i<argc; i++){ |
| 35800 | + int nByte = WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, 0, 0, 0, 0); |
| 35801 | + if( nByte==0 ){ |
| 35802 | + argv[i] = 0; |
| 35803 | + }else{ |
| 35804 | + argv[i] = malloc( nByte ); |
| 35805 | + if( argv[i]==0 ){ |
| 35806 | + fprintf(stderr, "malloc failed\n"); |
| 35807 | + exit(1); |
| 35808 | + } |
| 35809 | + nByte = WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, argv[i],nByte,0,0); |
| 35810 | + if( nByte==0 ){ |
| 35811 | + free(argv[i]); |
| 35812 | + argv[i] = 0; |
| 35813 | + } |
| 35814 | + } |
| 35815 | + } |
| 35816 | + argv[argc] = 0; |
| 35817 | + rc = utf8_main(argc, argv); |
| 35818 | + for(i=0; i<argc; i++) free(orig[i]); |
| 35819 | + free(argv); |
| 35820 | + return rc; |
| 35821 | +} |
| 35822 | +#endif /* WIN32 */ |
| 35823 | + |
| 35824 | +/* |
| 35825 | +** This is the main entry point for the process. Everything starts here. |
| 35826 | +** |
| 35827 | +** The "main" identifier may have been #defined to something else: |
| 35828 | +** |
| 35829 | +** utf8_main On Windows |
| 35830 | +** fiddle_main In Fiddle |
| 35831 | +** sqlite3_shell Other projects that use shell.c as a subroutine |
| 35832 | +*/ |
| 35833 | +int SQLITE_CDECL main(int argc, char **argv){ |
| 35714 | 35834 | #ifdef SQLITE_DEBUG |
| 35715 | 35835 | sqlite3_int64 mem_main_enter = 0; |
| 35716 | 35836 | #endif |
| 35717 | 35837 | char *zErrMsg = 0; |
| 35718 | 35838 | #ifdef SQLITE_SHELL_FIDDLE |
| | @@ -35730,14 +35850,10 @@ |
| 35730 | 35850 | int nOptsEnd = argc; |
| 35731 | 35851 | int bEnableVfstrace = 0; |
| 35732 | 35852 | char **azCmd = 0; |
| 35733 | 35853 | int *aiCmd = 0; |
| 35734 | 35854 | const char *zVfs = 0; /* Value of -vfs command-line option */ |
| 35735 | | -#if !SQLITE_SHELL_IS_UTF8 |
| 35736 | | - char **argvToFree = 0; |
| 35737 | | - int argcToFree = 0; |
| 35738 | | -#endif |
| 35739 | 35855 | setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */ |
| 35740 | 35856 | |
| 35741 | 35857 | #ifdef SQLITE_SHELL_FIDDLE |
| 35742 | 35858 | stdin_is_interactive = 0; |
| 35743 | 35859 | stdout_is_console = 1; |
| | @@ -35788,36 +35904,10 @@ |
| 35788 | 35904 | exit(1); |
| 35789 | 35905 | } |
| 35790 | 35906 | #endif |
| 35791 | 35907 | main_init(&data); |
| 35792 | 35908 | |
| 35793 | | - /* On Windows, we must translate command-line arguments into UTF-8. |
| 35794 | | - ** The SQLite memory allocator subsystem has to be enabled in order to |
| 35795 | | - ** do this. But we want to run an sqlite3_shutdown() afterwards so that |
| 35796 | | - ** subsequent sqlite3_config() calls will work. So copy all results into |
| 35797 | | - ** memory that does not come from the SQLite memory allocator. |
| 35798 | | - */ |
| 35799 | | -#if !SQLITE_SHELL_IS_UTF8 |
| 35800 | | - sqlite3_initialize(); |
| 35801 | | - argvToFree = malloc(sizeof(argv[0])*argc*2); |
| 35802 | | - shell_check_oom(argvToFree); |
| 35803 | | - argcToFree = argc; |
| 35804 | | - argv = argvToFree + argc; |
| 35805 | | - for(i=0; i<argc; i++){ |
| 35806 | | - char *z = sqlite3_win32_unicode_to_utf8(wargv[i]); |
| 35807 | | - i64 n; |
| 35808 | | - shell_check_oom(z); |
| 35809 | | - n = strlen(z); |
| 35810 | | - argv[i] = malloc( n+1 ); |
| 35811 | | - shell_check_oom(argv[i]); |
| 35812 | | - memcpy(argv[i], z, n+1); |
| 35813 | | - argvToFree[i] = argv[i]; |
| 35814 | | - sqlite3_free(z); |
| 35815 | | - } |
| 35816 | | - sqlite3_shutdown(); |
| 35817 | | -#endif |
| 35818 | | - |
| 35819 | 35909 | assert( argc>=1 && argv && argv[0] ); |
| 35820 | 35910 | Argv0 = argv[0]; |
| 35821 | 35911 | |
| 35822 | 35912 | #ifdef SQLITE_SHELL_DBNAME_PROC |
| 35823 | 35913 | { |
| | @@ -36009,10 +36099,20 @@ |
| 36009 | 36099 | }else if( cli_strcmp(z,"-safe")==0 ){ |
| 36010 | 36100 | /* no-op - catch this on the second pass */ |
| 36011 | 36101 | }else if( cli_strcmp(z,"-escape")==0 && i+1<argc ){ |
| 36012 | 36102 | /* skip over the argument */ |
| 36013 | 36103 | i++; |
| 36104 | + }else if( cli_strcmp(z,"-test-argv")==0 ){ |
| 36105 | + /* Undocumented test option. Print the values in argv[] and exit. |
| 36106 | + ** Use this to verify that any translation of the argv[], for example |
| 36107 | + ** on Windows that receives wargv[] from the OS and must convert |
| 36108 | + ** to UTF8 prior to calling this routine. */ |
| 36109 | + int kk; |
| 36110 | + for(kk=0; kk<argc; kk++){ |
| 36111 | + sqlite3_fprintf(stdout,"argv[%d] = \"%s\"\n", kk, argv[kk]); |
| 36112 | + } |
| 36113 | + return 0; |
| 36014 | 36114 | } |
| 36015 | 36115 | } |
| 36016 | 36116 | #ifndef SQLITE_SHELL_FIDDLE |
| 36017 | 36117 | if( !bEnableVfstrace ) verify_uninitialized(); |
| 36018 | 36118 | #endif |
| | @@ -36427,14 +36527,10 @@ |
| 36427 | 36527 | } |
| 36428 | 36528 | find_home_dir(1); |
| 36429 | 36529 | output_reset(&data); |
| 36430 | 36530 | data.doXdgOpen = 0; |
| 36431 | 36531 | clearTempFile(&data); |
| 36432 | | -#if !SQLITE_SHELL_IS_UTF8 |
| 36433 | | - for(i=0; i<argcToFree; i++) free(argvToFree[i]); |
| 36434 | | - free(argvToFree); |
| 36435 | | -#endif |
| 36436 | 36532 | modeFree(&data.mode); |
| 36437 | 36533 | if( data.nSavedModes ){ |
| 36438 | 36534 | int ii; |
| 36439 | 36535 | for(ii=0; ii<data.nSavedModes; ii++){ |
| 36440 | 36536 | modeFree(&data.aSavedModes[ii].mode); |
| 36441 | 36537 | |