]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Enhance the command-lin shell to interpret CSV files as described by
authordrh <drh@noemail.net>
Mon, 16 Jan 2012 16:56:31 +0000 (16:56 +0000)
committerdrh <drh@noemail.net>
Mon, 16 Jan 2012 16:56:31 +0000 (16:56 +0000)
the wikipedia article on CSV.

FossilOrigin-Name: 93aa17d866873e11dde5ffbefe74497f229977c1

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

index ef4a592efea1efb74ec5fd1c446e9069ae62d569..3c45dc8dad67904c5772d56cd535763bda6a3910 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Test\sthe\sfts4\scontent\sand\sprefix\soptions\stogether.
-D 2012-01-16T16:39:36.406
+C Enhance\sthe\scommand-lin\sshell\sto\sinterpret\sCSV\sfiles\sas\sdescribed\sby\nthe\swikipedia\sarticle\son\sCSV.
+D 2012-01-16T16:56:31.707
 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
 F Makefile.in 3f79a373e57c3b92dabf76f40b065e719d31ac34
 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -181,7 +181,7 @@ F src/random.c cd4a67b3953b88019f8cd4ccd81394a8ddfaba50
 F src/resolve.c 3d3e80a98f203ac6b9329e9621e29eda85ddfd40
 F src/rowset.c 69afa95a97c524ba6faf3805e717b5b7ae85a697
 F src/select.c a1d075db66a0ea42807353501b62997969e5be79
-F src/shell.c aa4183d4a5243d8110b1d3d77faa4aea7e9c9c2d
+F src/shell.c f492df9fc2de27e4700ecbaa948729fc47af88d7
 F src/sqlite.h.in 53516617d2945a411d028674d7fa20dd394b9ec0
 F src/sqlite3ext.h 6904f4aadf976f95241311fbffb00823075d9477
 F src/sqliteInt.h b8fdd9c39c8d7f5c794f4ea917293d9c75b9aff2
@@ -968,7 +968,7 @@ F tool/shell1.test 20dfe7099cf2afe37aecd69afb7678d14f7a0abf
 F tool/shell2.test 5dc76b8005b465f420fed8241621da7513060ff3
 F tool/shell3.test 4fad469e8003938426355afdf34155f08c587836
 F tool/shell4.test 35f9c3d452b4e76d5013c63e1fd07478a62f14ce
-F tool/shell5.test 62bfaf9267296da1b91e4b1c03e44e7b393f6a94
+F tool/shell5.test 0e987fb8d40638bb5c90163cb58cbe3e07dbed56
 F tool/showdb.c 43e913d954684c2f5007dcab46d1a1308852a0ad
 F tool/showjournal.c b62cecaab86a4053d944c276bb5232e4d17ece02
 F tool/showwal.c f09e5a80a293919290ec85a6a37c85a5ddcf37d9
@@ -987,7 +987,7 @@ F tool/tostr.awk e75472c2f98dd76e06b8c9c1367f4ab07e122d06
 F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f
 F tool/warnings-clang.sh 9f406d66e750e8ac031c63a9ef3248aaa347ef2a
 F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381
-P ebd01a8deffb5024a5d7494eef800d2366d97204
-R 40287a4cf4afc5ee1880263d8270c08c
-U dan
-Z 8212754ed5e92bf82a949bd6834df88c
+P de11cd5cc903e56ff641079bc64dd0ae5e133aa7
+R dacb46e8be9eb1ebee00515eda49d8d2
+U drh
+Z 9bfd5c27d81636d73cbec09996ae37dd
index 4893ac9075b12b6a3fcf14bd92eb59dec53905b8..8708fe5312a9ad177c81f78d9cf4757df305d09f 100644 (file)
@@ -1 +1 @@
-de11cd5cc903e56ff641079bc64dd0ae5e133aa7
\ No newline at end of file
+93aa17d866873e11dde5ffbefe74497f229977c1
\ No newline at end of file
index 31d3dd8fce29439c3aab89b96efa2666b81a92ed..dfdc674ac2678ac051a4970ae854c278d68a1492 100644 (file)
@@ -57,7 +57,7 @@
 # include <readline/history.h>
 #endif
 #if !defined(HAVE_EDITLINE) && (!defined(HAVE_READLINE) || HAVE_READLINE!=1)
-# define readline(p) local_getline(p,stdin)
+# define readline(p) local_getline(p,stdin,0)
 # define add_history(X)
 # define read_history(X)
 # define write_history(X)
@@ -334,10 +334,11 @@ static void shellstaticFunc(
 ** The interface is like "readline" but no command-line editing
 ** is done.
 */
-static char *local_getline(char *zPrompt, FILE *in){
+static char *local_getline(char *zPrompt, FILE *in, int csvFlag){
   char *zLine;
   int nLine;
   int n;
+  int inQuote = 0;
 
   if( zPrompt && *zPrompt ){
     printf("%s",zPrompt);
@@ -361,8 +362,11 @@ static char *local_getline(char *zPrompt, FILE *in){
       zLine[n] = 0;
       break;
     }
-    while( zLine[n] ){ n++; }
-    if( n>0 && zLine[n-1]=='\n' ){
+    while( zLine[n] ){
+      if( zLine[n]=='"' ) inQuote = !inQuote;
+      n++;
+    }
+    if( n>0 && zLine[n-1]=='\n' && (!inQuote || !csvFlag) ){
       n--;
       if( n>0 && zLine[n-1]=='\r' ) n--;
       zLine[n] = 0;
@@ -383,7 +387,7 @@ static char *one_input_line(const char *zPrior, FILE *in){
   char *zPrompt;
   char *zResult;
   if( in!=0 ){
-    return local_getline(0, in);
+    return local_getline(0, in, 0);
   }
   if( zPrior && zPrior[0] ){
     zPrompt = continuePrompt;
@@ -614,8 +618,7 @@ static const char needCsvQuote[] = {
 /*
 ** Output a single term of CSV.  Actually, p->separator is used for
 ** the separator, which may or may not be a comma.  p->nullvalue is
-** the null value.  Strings are quoted using ANSI-C rules.  Numbers
-** appear outside of quotes.
+** the null value.  Strings are quoted if necessary.
 */
 static void output_csv(struct callback_data *p, const char *z, int bSep){
   FILE *out = p->out;
@@ -1769,12 +1772,15 @@ static int do_meta_command(char *zLine, struct callback_data *p){
     }
     sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
     zCommit = "COMMIT";
-    while( (zLine = local_getline(0, in))!=0 ){
-      char *z;
+    while( (zLine = local_getline(0, in, 1))!=0 ){
+      char *z, c;
+      int inQuote = 0;
       lineno++;
       azCol[0] = zLine;
-      for(i=0, z=zLine; *z && *z!='\n' && *z!='\r'; z++){
-        if( *z==p->separator[0] && strncmp(z, p->separator, nSep)==0 ){
+      for(i=0, z=zLine; (c = *z)!=0; z++){
+        if( c=='"' ) inQuote = !inQuote;
+        if( c=='\n' ) lineno++;
+        if( !inQuote && c==p->separator[0] && strncmp(z,p->separator,nSep)==0 ){
           *z = 0;
           i++;
           if( i<nCol ){
@@ -1794,6 +1800,14 @@ static int do_meta_command(char *zLine, struct callback_data *p){
         break; /* from while */
       }
       for(i=0; i<nCol; i++){
+        if( azCol[i][0]=='"' ){
+          int k;
+          for(z=azCol[i], j=1, k=0; z[j]; j++){
+            if( z[j]=='"' ){ j++; if( z[j]==0 ) break; }
+            z[k++] = z[j];
+          }
+          z[k] = 0;
+        }
         sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC);
       }
       sqlite3_step(pStmt);
index a82f9797a738d124e184be2244caa7583e1e7381..828d71c97a92e3a9e2a21c27205af973e53bee33 100644 (file)
@@ -194,7 +194,7 @@ SELECT COUNT(*) FROM t1;}]
 
 do_test shell5-1.4.10.2 {
   catchcmd "test.db" {SELECT b FROM t1 WHERE a='7';}
-} {0 {"Now is the time for all good men to come to the aid of their country."}}
+} {0 {Now is the time for all good men to come to the aid of their country.}}
 
 # check importing very long field
 do_test shell5-1.5.1 {