Simplify and cleanup the implementation of the new ASCII mode for the shell.

FossilOrigin-Name: 66a28f7abaeaf1ebe6e43d856af88ac64b1ff230
This commit is contained in:
mistachkin 2014-12-11 03:12:33 +00:00
parent 31868243e2
commit e0d6885f17
5 changed files with 38 additions and 88 deletions

View File

@ -1,5 +1,5 @@
C Merge\supdates\sfrom\strunk.
D 2014-12-11T02:28:42.201
C Simplify\sand\scleanup\sthe\simplementation\sof\sthe\snew\sASCII\smode\sfor\sthe\sshell.
D 2014-12-11T03:12:33.799
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
F Makefile.in 6c4f961fa91d0b4fa121946a19f9e5eac2f2f809
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@ -229,7 +229,7 @@ F src/random.c ba2679f80ec82c4190062d756f22d0c358180696
F src/resolve.c f6c46d3434439ab2084618d603e6d6dbeb0d6ada
F src/rowset.c eccf6af6d620aaa4579bd3b72c1b6395d9e9fa1e
F src/select.c f377fb8a5c73c10678ea74f3400f7913943e3d75
F src/shell.c 1c7787d1066d1a858b8a4812663fde0e4a85b755
F src/shell.c 84359f80c432f8b34f0af4fdee8055c9fc2639a2
F src/sqlite.h.in 2e699aabd1df6042aff4265cd93bdc8812629f11
F src/sqlite3.rc 992c9f5fb8285ae285d6be28240a7e8d3a7f2bad
F src/sqlite3ext.h 17d487c3c91b0b8c584a32fbeb393f6f795eea7d
@ -851,11 +851,11 @@ F test/sharedA.test 0cdf1a76dfa00e6beee66af5b534b1e8df2720f5
F test/sharedB.test 16cc7178e20965d75278f410943109b77b2e645e
F test/shared_err.test 2f2aee20db294b9924e81f6ccbe60f19e21e8506
F test/sharedlock.test 5ede3c37439067c43b0198f580fd374ebf15d304
F test/shell1.test 0d2cece602988da3fa066c00adf19cda0b1306ad
F test/shell1.test cdeb849acc2c37aada70d084564b0cc0a2c7df08
F test/shell2.test 12b8bf901b0e3a8ac58cf5c0c63a0a388d4d1862
F test/shell3.test 5e8545ec72c4413a0e8d4c6be56496e3c257ca29
F test/shell4.test 8a9c08976291e6c6c808b4d718f4a8b299f339f5
F test/shell5.test 7dbc03751ceda1e5ce75553dff735a7ef0dea15c
F test/shell5.test d17a02d2327aadc6c062d5ba16b737d32601b11b
F test/shortread1.test bb591ef20f0fd9ed26d0d12e80eee6d7ac8897a3
F test/show_speedtest1_rtree.tcl 32e6c5f073d7426148a6936a0408f4b5b169aba5
F test/shrink.test 8c70f62b6e8eb4d54533de6d65bd06b1b9a17868
@ -1227,7 +1227,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
F tool/warnings.sh 0abfd78ceb09b7f7c27c688c8e3fe93268a13b32
F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
P 51f33cf1290cf767c1c6ba0228f4f30e4059c994 d9f916ba09f1a61684b4d59548ab6cf71cdb6a37
R 15ef4ed782851554150951631e8659fc
P 5b5d3e4d0d158594c0db05ddbf4d926b65825042
R 7df644943b04f66f737e7f900957f44a
U mistachkin
Z e24780e51cf083f098b96480beceaf1e
Z bc502efd9af4cfe5719e73721b9180f9

View File

@ -1 +1 @@
5b5d3e4d0d158594c0db05ddbf4d926b65825042
66a28f7abaeaf1ebe6e43d856af88ac64b1ff230

View File

@ -911,13 +911,13 @@ static int shell_callback(
for(i=0; i<nArg; i++){
output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
}
fprintf(p->out, "%s", p->newline);
fprintf(p->out, "%s", p->rowSeparator);
}
if( nArg>0 ){
for(i=0; i<nArg; i++){
output_csv(p, azArg[i], i<nArg-1);
}
fprintf(p->out, "%s", p->newline);
fprintf(p->out, "%s", p->rowSeparator);
}
#if defined(WIN32) || defined(_WIN32)
fflush(p->out);
@ -1711,8 +1711,6 @@ static char zHelp[] =
".backup ?DB? FILE Backup DB (default \"main\") to FILE\n"
".bail on|off Stop after hitting an error. Default OFF\n"
".clone NEWDB Clone data into NEWDB from the existing database\n"
".colseparator STRING This is an alias for the one argument version of\n"
" .separator\n"
".databases List names and files of attached databases\n"
".dump ?TABLE? ... Dump the database in an SQL text format\n"
" If TABLE specified, only dump tables matching\n"
@ -1737,13 +1735,13 @@ static char zHelp[] =
#endif
".log FILE|off Turn logging on or off. FILE can be stderr/stdout\n"
".mode MODE ?TABLE? Set output mode where MODE is one of:\n"
" ascii Columns/rows delimited with 0x1F and 0x1E\n"
" ascii Columns/rows delimited by 0x1F and 0x1E\n"
" csv Comma-separated values\n"
" column Left-aligned columns. (See .width)\n"
" html HTML <table> code\n"
" insert SQL insert statements for TABLE\n"
" line One value per line\n"
" list Values delimited by .separator string\n"
" list Values delimited by .separator strings\n"
" tabs Tab-separated values\n"
" tcl TCL list elements\n"
".nullvalue STRING Use STRING in place of NULL values\n"
@ -1755,14 +1753,13 @@ static char zHelp[] =
".quit Exit this program\n"
".read FILENAME Execute SQL in FILENAME\n"
".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n"
".rowseparator STRING Change row separator for output mode and .import\n"
".save FILE Write in-memory database into FILE\n"
".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off\n"
".schema ?TABLE? Show the CREATE statements\n"
" If TABLE specified, only show tables matching\n"
" LIKE pattern TABLE.\n"
".separator STRING ?NL? Change column separator for output mode and .import\n"
" NL is the end-of-line mark for CSV\n"
".separator COL ?ROW? Change the column separator and optionally the row\n"
" separator for both the output mode and .import\n"
".shell CMD ARGS... Run CMD ARGS... in a system shell\n"
".show Show the current values for various settings\n"
".stats on|off Turn stats on or off\n"
@ -2764,6 +2761,14 @@ static int do_meta_command(char *zLine, ShellState *p){
fprintf(stderr, "Error: non-null row separator required for import\n");
return 1;
}
if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){
/* When importing CSV (only), if the row separator is set to the
** default output row separator, change it to the default input
** row separator. This avoids having to maintain different input
** and output row separators. */
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
nSep = strlen30(p->rowSeparator);
}
if( nSep>1 ){
fprintf(stderr, "Error: multi-character row separators not allowed"
" for import\n");
@ -3027,9 +3032,8 @@ static int do_meta_command(char *zLine, ShellState *p){
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
}else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
p->mode = MODE_Csv;
sqlite3_snprintf(sizeof(p->newline), p->newline, SEP_CrLf);
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
}else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
p->mode = MODE_List;
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
@ -3334,29 +3338,9 @@ static int do_meta_command(char *zLine, ShellState *p){
}else
#endif
if( c=='r' && strncmp(azArg[0], "rowseparator", n)==0 ){
if( nArg==2 ){
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
"%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[1]);
}else{
fprintf(stderr, "Usage: .rowseparator STRING\n");
rc = 1;
}
}else
if( c=='c' && strncmp(azArg[0], "colseparator", n)==0 ){
if( nArg==2 ){
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
"%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
}else{
fprintf(stderr, "Usage: .colseparator STRING\n");
rc = 1;
}
}else
if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
if( nArg<2 || nArg>3 ){
fprintf(stderr, "Usage: .separator SEPARATOR ?NEWLINE?\n");
fprintf(stderr, "Usage: .separator COL ?ROW?\n");
rc = 1;
}
if( nArg>=2 ){
@ -3364,8 +3348,8 @@ static int do_meta_command(char *zLine, ShellState *p){
"%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
}
if( nArg>=3 ){
sqlite3_snprintf(sizeof(p->newline), p->newline,
"%.*s", (int)ArraySize(p->newline)-1, azArg[2]);
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
"%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
}
}else
@ -3412,9 +3396,6 @@ static int do_meta_command(char *zLine, ShellState *p){
fprintf(p->out,"%12.12s: ", "rowseparator");
output_c_string(p->out, p->rowSeparator);
fprintf(p->out, "\n");
fprintf(p->out,"%12.12s: ", "newline");
output_c_string(p->out, p->newline);
fprintf(p->out, "\n");
fprintf(p->out,"%12.12s: %s\n","stats", p->statsOn ? "on" : "off");
fprintf(p->out,"%12.12s: ","width");
for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
@ -4080,7 +4061,6 @@ static const char zOptions[] =
" -ascii set output mode to 'ascii'\n"
" -bail stop after hitting an error\n"
" -batch force batch I/O\n"
" -colseparator SEP same as -separator\n"
" -column set output mode to 'column'\n"
" -cmd COMMAND run \"COMMAND\" before reading stdin\n"
" -csv set output mode to 'csv'\n"
@ -4100,12 +4080,11 @@ static const char zOptions[] =
#ifdef SQLITE_ENABLE_MULTIPLEX
" -multiplex enable the multiplexor VFS\n"
#endif
" -newline SEP set newline character(s) for CSV\n"
" -newline SEP set output row separator. Default: '\\n'\n"
" -nullvalue TEXT set text string for NULL values. Default ''\n"
" -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
" -rowseparator SEP set output line separator. Default: '\\n'\n"
" -scratch SIZE N use N slots of SZ bytes each for scratch memory\n"
" -separator SEP set output field separator. Default: '|'\n"
" -separator SEP set output column separator. Default: '|'\n"
" -stats print memory stats before each finalize\n"
" -version show SQLite version\n"
" -vfs NAME use NAME as the default VFS\n"
@ -4134,7 +4113,6 @@ static void main_init(ShellState *data) {
data->mode = MODE_List;
memcpy(data->colSeparator,SEP_Column, 2);
memcpy(data->rowSeparator,SEP_Row, 2);
memcpy(data->newline,SEP_CrLf, 3);
data->showHeader = 0;
data->shellFlgs = SHFLG_Lookaside;
sqlite3_config(SQLITE_CONFIG_URI, 1);
@ -4380,14 +4358,11 @@ int main(int argc, char **argv){
SEP_Unit);
sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
SEP_Record);
}else if( strcmp(z,"-separator")==0 || strcmp(z,"-colseparator")==0 ){
}else if( strcmp(z,"-separator")==0 ){
sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
"%s",cmdline_option_value(argc,argv,++i));
}else if( strcmp(z,"-rowseparator")==0 ){
sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
"%s",cmdline_option_value(argc,argv,++i));
}else if( strcmp(z,"-newline")==0 ){
sqlite3_snprintf(sizeof(data.newline), data.newline,
sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
"%s",cmdline_option_value(argc,argv,++i));
}else if( strcmp(z,"-nullvalue")==0 ){
sqlite3_snprintf(sizeof(data.nullvalue), data.nullvalue,

View File

@ -421,13 +421,13 @@ do_test shell1-3.12.3 {
} {1 {Usage: .indices ?LIKE-PATTERN?}}
# .mode MODE ?TABLE? Set output mode where MODE is one of:
# ascii Columns/rows delimited with 0x1F and 0x1E
# ascii Columns/rows delimited by 0x1F and 0x1E
# csv Comma-separated values
# column Left-aligned columns. (See .width)
# html HTML <table> code
# insert SQL insert statements for TABLE
# line One value per line
# list Values delimited by .separator string
# list Values delimited by .separator strings
# tabs Tab-separated values
# tcl TCL list elements
do_test shell1-3.13.1 {
@ -590,7 +590,7 @@ db eval {DROP VIEW v1; DROP VIEW v2; DROP TABLE t1;}
# .separator STRING Change column separator used by output and .import
do_test shell1-3.22.1 {
catchcmd "test.db" ".separator"
} {1 {Usage: .separator SEPARATOR ?NEWLINE?}}
} {1 {Usage: .separator COL ?ROW?}}
do_test shell1-3.22.2 {
catchcmd "test.db" ".separator FOO"
} {0 {}}
@ -600,7 +600,7 @@ do_test shell1-3.22.3 {
do_test shell1-3.22.4 {
# too many arguments
catchcmd "test.db" ".separator FOO BAD BAD2"
} {1 {Usage: .separator SEPARATOR ?NEWLINE?}}
} {1 {Usage: .separator COL ?ROW?}}
# .show Show the current values for various settings
do_test shell1-3.23.1 {
@ -613,10 +613,9 @@ do_test shell1-3.23.1 {
[regexp {output:} $res] \
[regexp {colseparator:} $res] \
[regexp {rowseparator:} $res] \
[regexp {newline:} $res] \
[regexp {stats:} $res] \
[regexp {width:} $res]
} {1 1 1 1 1 1 1 1 1 1 1}
} {1 1 1 1 1 1 1 1 1 1}
do_test shell1-3.23.2 {
# too many arguments
catchcmd "test.db" ".show BAD"
@ -816,28 +815,4 @@ do_test shell1-4.6 {
";"
"$"} 7}
# .colseparator STRING Change column separator used by output and .import
do_test shell1-5.1.1 {
catchcmd "test.db" ".colseparator"
} {1 {Usage: .colseparator STRING}}
do_test shell1-5.1.2 {
catchcmd "test.db" ".colseparator FOO"
} {0 {}}
do_test shell1-5.1.3 {
# too many arguments
catchcmd "test.db" ".colseparator FOO BAD"
} {1 {Usage: .colseparator STRING}}
# .rowseparator STRING Change row separator used by output and .import
do_test shell1-6.1.1 {
catchcmd "test.db" ".rowseparator"
} {1 {Usage: .rowseparator STRING}}
do_test shell1-6.1.2 {
catchcmd "test.db" ".rowseparator FOO"
} {0 {}}
do_test shell1-6.1.3 {
# too many arguments
catchcmd "test.db" ".rowseparator FOO BAD"
} {1 {Usage: .rowseparator STRING}}
finish_test

View File

@ -55,7 +55,7 @@ do_test shell5-1.1.3 {
# .separator STRING Change separator used by output mode and .import
do_test shell5-1.2.1 {
catchcmd "test.db" ".separator"
} {1 {Usage: .separator SEPARATOR ?NEWLINE?}}
} {1 {Usage: .separator COL ?ROW?}}
do_test shell5-1.2.2 {
catchcmd "test.db" ".separator ONE"
} {0 {}}
@ -65,7 +65,7 @@ do_test shell5-1.2.3 {
do_test shell5-1.2.4 {
# too many arguments
catchcmd "test.db" ".separator ONE TWO THREE"
} {1 {Usage: .separator SEPARATOR ?NEWLINE?}}
} {1 {Usage: .separator COL ?ROW?}}
# column separator should default to "|"
do_test shell5-1.3.1.1 {