/ Check-in [41fd9dd2]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Improvements to the 'tcl' shell output mode. Escape double quotes, set separator to space when mode is set, and skip separator after final column.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | tclMode
Files: files | file ages | folders
SHA1: 41fd9dd29034b2269e4b7f2626350124d37b5303
User & Date: mistachkin 2012-12-04 00:23:43
Context
2012-12-04
00:59
Improvements to the 'tcl' shell output mode. Escape doublequotes, set separator to space when mode is set, and skip separator after final column. check-in: 487ba753 user: drh tags: trunk
00:23
Improvements to the 'tcl' shell output mode. Escape double quotes, set separator to space when mode is set, and skip separator after final column. Closed-Leaf check-in: 41fd9dd2 user: mistachkin tags: tclMode
2012-12-03
19:42
Remove an unreachable condition. Replace it with an assert(). check-in: 7d5fc1a3 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/shell.c.

   537    537   static void output_c_string(FILE *out, const char *z){
   538    538     unsigned int c;
   539    539     fputc('"', out);
   540    540     while( (c = *(z++))!=0 ){
   541    541       if( c=='\\' ){
   542    542         fputc(c, out);
   543    543         fputc(c, out);
          544  +    }else if( c=='"' ){
          545  +      fputc('\\', out);
          546  +      fputc('"', out);
   544    547       }else if( c=='\t' ){
   545    548         fputc('\\', out);
   546    549         fputc('t', out);
   547    550       }else if( c=='\n' ){
   548    551         fputc('\\', out);
   549    552         fputc('n', out);
   550    553       }else if( c=='\r' ){
................................................................................
   792    795         fprintf(p->out,"</TR>\n");
   793    796         break;
   794    797       }
   795    798       case MODE_Tcl: {
   796    799         if( p->cnt++==0 && p->showHeader ){
   797    800           for(i=0; i<nArg; i++){
   798    801             output_c_string(p->out,azCol[i] ? azCol[i] : "");
   799         -          fprintf(p->out, "%s", p->separator);
          802  +          if(i<nArg-1) fprintf(p->out, "%s", p->separator);
   800    803           }
   801    804           fprintf(p->out,"\n");
   802    805         }
   803    806         if( azArg==0 ) break;
   804    807         for(i=0; i<nArg; i++){
   805    808           output_c_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);
   806         -        fprintf(p->out, "%s", p->separator);
          809  +        if(i<nArg-1) fprintf(p->out, "%s", p->separator);
   807    810         }
   808    811         fprintf(p->out,"\n");
   809    812         break;
   810    813       }
   811    814       case MODE_Csv: {
   812    815         if( p->cnt++==0 && p->showHeader ){
   813    816           for(i=0; i<nArg; i++){
................................................................................
  2014   2017         p->mode = MODE_Column;
  2015   2018       }else if( n2==4 && strncmp(azArg[1],"list",n2)==0 ){
  2016   2019         p->mode = MODE_List;
  2017   2020       }else if( n2==4 && strncmp(azArg[1],"html",n2)==0 ){
  2018   2021         p->mode = MODE_Html;
  2019   2022       }else if( n2==3 && strncmp(azArg[1],"tcl",n2)==0 ){
  2020   2023         p->mode = MODE_Tcl;
         2024  +      sqlite3_snprintf(sizeof(p->separator), p->separator, " ");
  2021   2025       }else if( n2==3 && strncmp(azArg[1],"csv",n2)==0 ){
  2022   2026         p->mode = MODE_Csv;
  2023   2027         sqlite3_snprintf(sizeof(p->separator), p->separator, ",");
  2024   2028       }else if( n2==4 && strncmp(azArg[1],"tabs",n2)==0 ){
  2025   2029         p->mode = MODE_List;
  2026   2030         sqlite3_snprintf(sizeof(p->separator), p->separator, "\t");
  2027   2031       }else if( n2==6 && strncmp(azArg[1],"insert",n2)==0 ){

Changes to test/shell1.test.

   715    715   } {0 {this is a test}}
   716    716   
   717    717   # Test the output of the ".dump" command
   718    718   #
   719    719   do_test shell1-4.1 {
   720    720     db eval {
   721    721       CREATE TABLE t1(x);
   722         -    INSERT INTO t1 VALUES(null), (1), (2.25), ('hello'), (x'807f');
          722  +    INSERT INTO t1 VALUES(null), (''), (1), (2.25), ('hello'), (x'807f');
   723    723     }
   724    724     catchcmd test.db {.dump}
   725    725   } {0 {PRAGMA foreign_keys=OFF;
   726    726   BEGIN TRANSACTION;
   727    727   CREATE TABLE t1(x);
   728    728   INSERT INTO "t1" VALUES(NULL);
          729  +INSERT INTO "t1" VALUES('');
   729    730   INSERT INTO "t1" VALUES(1);
   730    731   INSERT INTO "t1" VALUES(2.25);
   731    732   INSERT INTO "t1" VALUES('hello');
   732    733   INSERT INTO "t1" VALUES(X'807F');
   733    734   COMMIT;}}
   734    735   
   735    736   # Test the output of ".mode insert"
   736    737   #
   737    738   do_test shell1-4.2 {
   738    739     catchcmd test.db ".mode insert t1\nselect * from t1;"
   739    740   } {0 {INSERT INTO t1 VALUES(NULL);
          741  +INSERT INTO t1 VALUES('');
   740    742   INSERT INTO t1 VALUES(1);
   741    743   INSERT INTO t1 VALUES(2.25);
   742    744   INSERT INTO t1 VALUES('hello');
   743    745   INSERT INTO t1 VALUES(X'807f');}}
   744    746   
          747  +# Test the output of ".mode tcl"
          748  +#
          749  +do_test shell1-4.3 {
          750  +  catchcmd test.db ".mode tcl\nselect * from t1;"
          751  +} {0 {""
          752  +""
          753  +"1"
          754  +"2.25"
          755  +"hello"
          756  +"\200\177"}}
          757  +
          758  +# Test the output of ".mode tcl" with multiple columns
          759  +#
          760  +do_test shell1-4.4 {
          761  +  db eval {
          762  +    CREATE TABLE t2(x,y);
          763  +    INSERT INTO t2 VALUES(null, ''), (1, 2.25), ('hello', x'807f');
          764  +  }
          765  +  catchcmd test.db ".mode tcl\nselect * from t2;"
          766  +} {0 {"" ""
          767  +"1" "2.25"
          768  +"hello" "\200\177"}}
          769  +
          770  +# Test the output of ".mode tcl" with ".nullvalue"
          771  +#
          772  +do_test shell1-4.5 {
          773  +  catchcmd test.db ".mode tcl\n.nullvalue NULL\nselect * from t2;"
          774  +} {0 {"NULL" ""
          775  +"1" "2.25"
          776  +"hello" "\200\177"}}
          777  +
          778  +# Test the output of ".mode tcl" with Tcl reserved characters
          779  +#
          780  +do_test shell1-4.6 {
          781  +  db eval {
          782  +    CREATE TABLE tcl1(x);
          783  +    INSERT INTO tcl1 VALUES('"'), ('['), (']'), ('\{'), ('\}'), (';'), ('$');
          784  +  }
          785  +  foreach {x y} [catchcmd test.db ".mode tcl\nselect * from tcl1;"] break
          786  +  list $x $y [llength $y]
          787  +} {0 {"\""
          788  +"["
          789  +"]"
          790  +"\\{"
          791  +"\\}"
          792  +";"
          793  +"$"} 7}
   745    794   
   746    795   finish_test