xref: /sqlite-3.40.0/test/shell1.test (revision 45f31be8)
1# 2009 Nov 11
2#
3# The author disclaims copyright to this source code.  In place of
4# a legal notice, here is a blessing:
5#
6#    May you do good and not evil.
7#    May you find forgiveness for yourself and forgive others.
8#    May you share freely, never taking more than you give.
9#
10#***********************************************************************
11#
12# The focus of this file is testing the CLI shell tool.
13#
14#
15
16# Test plan:
17#
18#   shell1-1.*: Basic command line option handling.
19#   shell1-2.*: Basic "dot" command token parsing.
20#   shell1-3.*: Basic test that "dot" command can be called.
21#
22set testdir [file dirname $argv0]
23source $testdir/tester.tcl
24if {$tcl_platform(platform)=="windows"} {
25  set CLI "sqlite3.exe"
26} else {
27  set CLI "./sqlite3"
28}
29if {![file executable $CLI]} {
30  finish_test
31  return
32}
33db close
34forcedelete test.db test.db-journal test.db-wal
35sqlite3 db test.db
36
37#----------------------------------------------------------------------------
38# Test cases shell1-1.*: Basic command line option handling.
39#
40
41# invalid option
42do_test shell1-1.1.1 {
43  set res [catchcmd "-bad test.db" ""]
44  set rc [lindex $res 0]
45  list $rc \
46       [regexp {Error: unknown option: -bad} $res]
47} {1 1}
48do_test shell1-1.1.1b {
49  set res [catchcmd "test.db -bad" ""]
50  set rc [lindex $res 0]
51  list $rc \
52       [regexp {Error: unknown option: -bad} $res]
53} {1 1}
54# error on extra options
55do_test shell1-1.1.2 {
56  catchcmd "test.db \"select 3\" \"select 4\"" ""
57} {0 {3
584}}
59# error on extra options
60do_test shell1-1.1.3 {
61  catchcmd "test.db FOO test.db BAD" ".quit"
62} {1 {Error: near "FOO": syntax error}}
63
64# -help
65do_test shell1-1.2.1 {
66  set res [catchcmd "-help test.db" ""]
67  set rc [lindex $res 0]
68  list $rc \
69       [regexp {Usage} $res] \
70       [regexp {\-init} $res] \
71       [regexp {\-version} $res]
72} {1 1 1 1}
73
74# -init filename       read/process named file
75do_test shell1-1.3.1 {
76  catchcmd "-init FOO test.db" ""
77} {0 {}}
78do_test shell1-1.3.2 {
79  catchcmd "-init FOO test.db .quit BAD" ""
80} {0 {}}
81do_test shell1-1.3.3 {
82  catchcmd "-init FOO test.db BAD .quit" ""
83} {1 {Error: near "BAD": syntax error}}
84
85# -echo                print commands before execution
86do_test shell1-1.4.1 {
87  catchcmd "-echo test.db" ""
88} {0 {}}
89
90# -[no]header          turn headers on or off
91do_test shell1-1.5.1 {
92  catchcmd "-header test.db" ""
93} {0 {}}
94do_test shell1-1.5.2 {
95  catchcmd "-noheader test.db" ""
96} {0 {}}
97
98# -bail                stop after hitting an error
99do_test shell1-1.6.1 {
100  catchcmd "-bail test.db" ""
101} {0 {}}
102
103# -interactive         force interactive I/O
104do_test shell1-1.7.1 {
105  set res [catchcmd "-interactive test.db" ".quit"]
106  set rc [lindex $res 0]
107  list $rc \
108       [regexp {SQLite version} $res] \
109       [regexp {Enter ".help" for usage hints} $res]
110} {0 1 1}
111
112# -batch               force batch I/O
113do_test shell1-1.8.1 {
114  catchcmd "-batch test.db" ""
115} {0 {}}
116
117# -column              set output mode to 'column'
118do_test shell1-1.9.1 {
119  catchcmd "-column test.db" ""
120} {0 {}}
121
122# -csv                 set output mode to 'csv'
123do_test shell1-1.10.1 {
124  catchcmd "-csv test.db" ""
125} {0 {}}
126
127# -html                set output mode to HTML
128do_test shell1-1.11.1 {
129  catchcmd "-html test.db" ""
130} {0 {}}
131
132# -line                set output mode to 'line'
133do_test shell1-1.12.1 {
134  catchcmd "-line test.db" ""
135} {0 {}}
136
137# -list                set output mode to 'list'
138do_test shell1-1.13.1 {
139  catchcmd "-list test.db" ""
140} {0 {}}
141
142# -separator 'x'       set output field separator (|)
143do_test shell1-1.14.1 {
144  catchcmd "-separator 'x' test.db" ""
145} {0 {}}
146do_test shell1-1.14.2 {
147  catchcmd "-separator x test.db" ""
148} {0 {}}
149do_test shell1-1.14.3 {
150  set res [catchcmd "-separator" ""]
151  set rc [lindex $res 0]
152  list $rc \
153       [regexp {Error: missing argument to -separator} $res]
154} {1 1}
155
156# -stats               print memory stats before each finalize
157do_test shell1-1.14b.1 {
158  catchcmd "-stats test.db" ""
159} {0 {}}
160
161# -nullvalue 'text'    set text string for NULL values
162do_test shell1-1.15.1 {
163  catchcmd "-nullvalue 'x' test.db" ""
164} {0 {}}
165do_test shell1-1.15.2 {
166  catchcmd "-nullvalue x test.db" ""
167} {0 {}}
168do_test shell1-1.15.3 {
169  set res [catchcmd "-nullvalue" ""]
170  set rc [lindex $res 0]
171  list $rc \
172       [regexp {Error: missing argument to -nullvalue} $res]
173} {1 1}
174
175# -version             show SQLite version
176do_test shell1-1.16.1 {
177  set x [catchcmd "-version test.db" ""]
178} {/3.[0-9.]+ 20\d\d-[01]\d-\d\d \d\d:\d\d:\d\d [0-9a-f]+/}
179
180#----------------------------------------------------------------------------
181# Test cases shell1-2.*: Basic "dot" command token parsing.
182#
183
184# check first token handling
185do_test shell1-2.1.1 {
186  catchcmd "test.db" ".foo"
187} {1 {Error: unknown command or invalid arguments:  "foo". Enter ".help" for help}}
188do_test shell1-2.1.2 {
189  catchcmd "test.db" ".\"foo OFF\""
190} {1 {Error: unknown command or invalid arguments:  "foo OFF". Enter ".help" for help}}
191do_test shell1-2.1.3 {
192  catchcmd "test.db" ".\'foo OFF\'"
193} {1 {Error: unknown command or invalid arguments:  "foo OFF". Enter ".help" for help}}
194
195# unbalanced quotes
196do_test shell1-2.2.1 {
197  catchcmd "test.db" ".\"foo OFF"
198} {1 {Error: unknown command or invalid arguments:  "foo OFF". Enter ".help" for help}}
199do_test shell1-2.2.2 {
200  catchcmd "test.db" ".\'foo OFF"
201} {1 {Error: unknown command or invalid arguments:  "foo OFF". Enter ".help" for help}}
202do_test shell1-2.2.3 {
203  catchcmd "test.db" ".explain \"OFF"
204} {0 {}}
205do_test shell1-2.2.4 {
206  catchcmd "test.db" ".explain \'OFF"
207} {0 {}}
208do_test shell1-2.2.5 {
209  catchcmd "test.db" ".mode \"insert FOO"
210} {1 {Error: mode should be one of: ascii column csv html insert line list tabs tcl}}
211do_test shell1-2.2.6 {
212  catchcmd "test.db" ".mode \'insert FOO"
213} {1 {Error: mode should be one of: ascii column csv html insert line list tabs tcl}}
214
215# check multiple tokens, and quoted tokens
216do_test shell1-2.3.1 {
217  catchcmd "test.db" ".explain 1"
218} {0 {}}
219do_test shell1-2.3.2 {
220  catchcmd "test.db" ".explain on"
221} {0 {}}
222do_test shell1-2.3.3 {
223  catchcmd "test.db" ".explain \"1 2 3\""
224} {1 {ERROR: Not a boolean value: "1 2 3". Assuming "no".}}
225do_test shell1-2.3.4 {
226  catchcmd "test.db" ".explain \"OFF\""
227} {0 {}}
228do_test shell1-2.3.5 {
229  catchcmd "test.db" ".\'explain\' \'OFF\'"
230} {0 {}}
231do_test shell1-2.3.6 {
232  catchcmd "test.db" ".explain \'OFF\'"
233} {0 {}}
234do_test shell1-2.3.7 {
235  catchcmd "test.db" ".\'explain\' \'OFF\'"
236} {0 {}}
237
238# check quoted args are unquoted
239do_test shell1-2.4.1 {
240  catchcmd "test.db" ".mode FOO"
241} {1 {Error: mode should be one of: ascii column csv html insert line list tabs tcl}}
242do_test shell1-2.4.2 {
243  catchcmd "test.db" ".mode csv"
244} {0 {}}
245do_test shell1-2.4.2 {
246  catchcmd "test.db" ".mode \"csv\""
247} {0 {}}
248
249
250#----------------------------------------------------------------------------
251# Test cases shell1-3.*: Basic test that "dot" command can be called.
252#
253
254# .backup ?DB? FILE      Backup DB (default "main") to FILE
255do_test shell1-3.1.1 {
256  catchcmd "test.db" ".backup"
257} {1 {missing FILENAME argument on .backup}}
258do_test shell1-3.1.2 {
259  catchcmd "test.db" ".backup FOO"
260} {0 {}}
261do_test shell1-3.1.3 {
262  catchcmd "test.db" ".backup FOO BAR"
263} {1 {Error: unknown database FOO}}
264do_test shell1-3.1.4 {
265  # too many arguments
266  catchcmd "test.db" ".backup FOO BAR BAD"
267} {1 {too many arguments to .backup}}
268
269# .bail ON|OFF           Stop after hitting an error.  Default OFF
270do_test shell1-3.2.1 {
271  catchcmd "test.db" ".bail"
272} {1 {Usage: .bail on|off}}
273do_test shell1-3.2.2 {
274  catchcmd "test.db" ".bail ON"
275} {0 {}}
276do_test shell1-3.2.3 {
277  catchcmd "test.db" ".bail OFF"
278} {0 {}}
279do_test shell1-3.2.4 {
280  # too many arguments
281  catchcmd "test.db" ".bail OFF BAD"
282} {1 {Usage: .bail on|off}}
283
284# .databases             List names and files of attached databases
285do_test shell1-3.3.1 {
286  catchcmd "-csv test.db" ".databases"
287} "/0 +.*main +[string map {/ .} [string range [get_pwd] 0 10]].*/"
288do_test shell1-3.3.2 {
289  # extra arguments ignored
290  catchcmd "test.db" ".databases BAD"
291} "/0 +.*main +[string map {/ .} [string range [get_pwd] 0 10]].*/"
292
293# .dump ?TABLE? ...      Dump the database in an SQL text format
294#                          If TABLE specified, only dump tables matching
295#                          LIKE pattern TABLE.
296do_test shell1-3.4.1 {
297  set res [catchcmd "test.db" ".dump"]
298  list [regexp {BEGIN TRANSACTION;} $res] \
299       [regexp {COMMIT;} $res]
300} {1 1}
301do_test shell1-3.4.2 {
302  set res [catchcmd "test.db" ".dump FOO"]
303  list [regexp {BEGIN TRANSACTION;} $res] \
304       [regexp {COMMIT;} $res]
305} {1 1}
306do_test shell1-3.4.3 {
307  # too many arguments
308  catchcmd "test.db" ".dump FOO BAD"
309} {1 {Usage: .dump ?LIKE-PATTERN?}}
310
311# .echo ON|OFF           Turn command echo on or off
312do_test shell1-3.5.1 {
313  catchcmd "test.db" ".echo"
314} {1 {Usage: .echo on|off}}
315do_test shell1-3.5.2 {
316  catchcmd "test.db" ".echo ON"
317} {0 {}}
318do_test shell1-3.5.3 {
319  catchcmd "test.db" ".echo OFF"
320} {0 {}}
321do_test shell1-3.5.4 {
322  # too many arguments
323  catchcmd "test.db" ".echo OFF BAD"
324} {1 {Usage: .echo on|off}}
325
326# .exit                  Exit this program
327do_test shell1-3.6.1 {
328  catchcmd "test.db" ".exit"
329} {0 {}}
330
331# .explain ON|OFF        Turn output mode suitable for EXPLAIN on or off.
332do_test shell1-3.7.1 {
333  catchcmd "test.db" ".explain"
334  # explain is the exception to the booleans.  without an option, it turns it on.
335} {0 {}}
336do_test shell1-3.7.2 {
337  catchcmd "test.db" ".explain ON"
338} {0 {}}
339do_test shell1-3.7.3 {
340  catchcmd "test.db" ".explain OFF"
341} {0 {}}
342do_test shell1-3.7.4 {
343  # extra arguments ignored
344  catchcmd "test.db" ".explain OFF BAD"
345} {0 {}}
346
347
348# .header(s) ON|OFF      Turn display of headers on or off
349do_test shell1-3.9.1 {
350  catchcmd "test.db" ".header"
351} {1 {Usage: .headers on|off}}
352do_test shell1-3.9.2 {
353  catchcmd "test.db" ".header ON"
354} {0 {}}
355do_test shell1-3.9.3 {
356  catchcmd "test.db" ".header OFF"
357} {0 {}}
358do_test shell1-3.9.4 {
359  # too many arguments
360  catchcmd "test.db" ".header OFF BAD"
361} {1 {Usage: .headers on|off}}
362
363do_test shell1-3.9.5 {
364  catchcmd "test.db" ".headers"
365} {1 {Usage: .headers on|off}}
366do_test shell1-3.9.6 {
367  catchcmd "test.db" ".headers ON"
368} {0 {}}
369do_test shell1-3.9.7 {
370  catchcmd "test.db" ".headers OFF"
371} {0 {}}
372do_test shell1-3.9.8 {
373  # too many arguments
374  catchcmd "test.db" ".headers OFF BAD"
375} {1 {Usage: .headers on|off}}
376
377# .help                  Show this message
378do_test shell1-3.10.1 {
379  set res [catchcmd "test.db" ".help"]
380  # look for a few of the possible help commands
381  list [regexp {.help} $res] \
382       [regexp {.quit} $res] \
383       [regexp {.show} $res]
384} {1 1 1}
385do_test shell1-3.10.2 {
386  # we allow .help to take extra args (it is help after all)
387  set res [catchcmd "test.db" ".help BAD"]
388  # look for a few of the possible help commands
389  list [regexp {.help} $res] \
390       [regexp {.quit} $res] \
391       [regexp {.show} $res]
392} {1 1 1}
393
394# .import FILE TABLE     Import data from FILE into TABLE
395do_test shell1-3.11.1 {
396  catchcmd "test.db" ".import"
397} {1 {Usage: .import FILE TABLE}}
398do_test shell1-3.11.2 {
399  catchcmd "test.db" ".import FOO"
400} {1 {Usage: .import FILE TABLE}}
401#do_test shell1-3.11.2 {
402#  catchcmd "test.db" ".import FOO BAR"
403#} {1 {Error: no such table: BAR}}
404do_test shell1-3.11.3 {
405  # too many arguments
406  catchcmd "test.db" ".import FOO BAR BAD"
407} {1 {Usage: .import FILE TABLE}}
408
409# .indexes ?TABLE?       Show names of all indexes
410#                          If TABLE specified, only show indexes for tables
411#                          matching LIKE pattern TABLE.
412do_test shell1-3.12.1 {
413  catchcmd "test.db" ".indexes"
414} {0 {}}
415do_test shell1-3.12.2 {
416  catchcmd "test.db" ".indexes FOO"
417} {0 {}}
418do_test shell1-3.12.2-legacy {
419  catchcmd "test.db" ".indices FOO"
420} {0 {}}
421do_test shell1-3.12.3 {
422  # too many arguments
423  catchcmd "test.db" ".indexes FOO BAD"
424} {1 {Usage: .indexes ?LIKE-PATTERN?}}
425
426# .mode MODE ?TABLE?     Set output mode where MODE is one of:
427#                          ascii    Columns/rows delimited by 0x1F and 0x1E
428#                          csv      Comma-separated values
429#                          column   Left-aligned columns.  (See .width)
430#                          html     HTML <table> code
431#                          insert   SQL insert statements for TABLE
432#                          line     One value per line
433#                          list     Values delimited by .separator strings
434#                          tabs     Tab-separated values
435#                          tcl      TCL list elements
436do_test shell1-3.13.1 {
437  catchcmd "test.db" ".mode"
438} {1 {Error: mode should be one of: ascii column csv html insert line list tabs tcl}}
439do_test shell1-3.13.2 {
440  catchcmd "test.db" ".mode FOO"
441} {1 {Error: mode should be one of: ascii column csv html insert line list tabs tcl}}
442do_test shell1-3.13.3 {
443  catchcmd "test.db" ".mode csv"
444} {0 {}}
445do_test shell1-3.13.4 {
446  catchcmd "test.db" ".mode column"
447} {0 {}}
448do_test shell1-3.13.5 {
449  catchcmd "test.db" ".mode html"
450} {0 {}}
451do_test shell1-3.13.6 {
452  catchcmd "test.db" ".mode insert"
453} {0 {}}
454do_test shell1-3.13.7 {
455  catchcmd "test.db" ".mode line"
456} {0 {}}
457do_test shell1-3.13.8 {
458  catchcmd "test.db" ".mode list"
459} {0 {}}
460do_test shell1-3.13.9 {
461  catchcmd "test.db" ".mode tabs"
462} {0 {}}
463do_test shell1-3.13.10 {
464  catchcmd "test.db" ".mode tcl"
465} {0 {}}
466do_test shell1-3.13.11 {
467  # extra arguments ignored
468  catchcmd "test.db" ".mode tcl BAD"
469} {0 {}}
470
471# don't allow partial mode type matches
472do_test shell1-3.13.12 {
473  catchcmd "test.db" ".mode l"
474} {1 {Error: mode should be one of: ascii column csv html insert line list tabs tcl}}
475do_test shell1-3.13.13 {
476  catchcmd "test.db" ".mode li"
477} {1 {Error: mode should be one of: ascii column csv html insert line list tabs tcl}}
478do_test shell1-3.13.14 {
479  catchcmd "test.db" ".mode lin"
480} {0 {}}
481
482# .nullvalue STRING      Print STRING in place of NULL values
483do_test shell1-3.14.1 {
484  catchcmd "test.db" ".nullvalue"
485} {1 {Usage: .nullvalue STRING}}
486do_test shell1-3.14.2 {
487  catchcmd "test.db" ".nullvalue FOO"
488} {0 {}}
489do_test shell1-3.14.3 {
490  # too many arguments
491  catchcmd "test.db" ".nullvalue FOO BAD"
492} {1 {Usage: .nullvalue STRING}}
493
494# .output FILENAME       Send output to FILENAME
495do_test shell1-3.15.1 {
496  catchcmd "test.db" ".output"
497} {0 {}}
498do_test shell1-3.15.2 {
499  catchcmd "test.db" ".output FOO"
500} {0 {}}
501do_test shell1-3.15.3 {
502  # too many arguments
503  catchcmd "test.db" ".output FOO BAD"
504} {1 {Usage: .output FILE}}
505
506# .output stdout         Send output to the screen
507do_test shell1-3.16.1 {
508  catchcmd "test.db" ".output stdout"
509} {0 {}}
510do_test shell1-3.16.2 {
511  # too many arguments
512  catchcmd "test.db" ".output stdout BAD"
513} {1 {Usage: .output FILE}}
514
515# .prompt MAIN CONTINUE  Replace the standard prompts
516do_test shell1-3.17.1 {
517  catchcmd "test.db" ".prompt"
518} {0 {}}
519do_test shell1-3.17.2 {
520  catchcmd "test.db" ".prompt FOO"
521} {0 {}}
522do_test shell1-3.17.3 {
523  catchcmd "test.db" ".prompt FOO BAR"
524} {0 {}}
525do_test shell1-3.17.4 {
526  # too many arguments
527  catchcmd "test.db" ".prompt FOO BAR BAD"
528} {0 {}}
529
530# .quit                  Exit this program
531do_test shell1-3.18.1 {
532  catchcmd "test.db" ".quit"
533} {0 {}}
534do_test shell1-3.18.2 {
535  # too many arguments
536  catchcmd "test.db" ".quit BAD"
537} {0 {}}
538
539# .read FILENAME         Execute SQL in FILENAME
540do_test shell1-3.19.1 {
541  catchcmd "test.db" ".read"
542} {1 {Usage: .read FILE}}
543do_test shell1-3.19.2 {
544  forcedelete FOO
545  catchcmd "test.db" ".read FOO"
546} {1 {Error: cannot open "FOO"}}
547do_test shell1-3.19.3 {
548  # too many arguments
549  catchcmd "test.db" ".read FOO BAD"
550} {1 {Usage: .read FILE}}
551
552# .restore ?DB? FILE     Restore content of DB (default "main") from FILE
553do_test shell1-3.20.1 {
554  catchcmd "test.db" ".restore"
555} {1 {Usage: .restore ?DB? FILE}}
556do_test shell1-3.20.2 {
557  catchcmd "test.db" ".restore FOO"
558} {0 {}}
559do_test shell1-3.20.3 {
560  catchcmd "test.db" ".restore FOO BAR"
561} {1 {Error: unknown database FOO}}
562do_test shell1-3.20.4 {
563  # too many arguments
564  catchcmd "test.db" ".restore FOO BAR BAD"
565} {1 {Usage: .restore ?DB? FILE}}
566
567# .schema ?TABLE?        Show the CREATE statements
568#                          If TABLE specified, only show tables matching
569#                          LIKE pattern TABLE.
570do_test shell1-3.21.1 {
571  catchcmd "test.db" ".schema"
572} {0 {}}
573do_test shell1-3.21.2 {
574  catchcmd "test.db" ".schema FOO"
575} {0 {}}
576do_test shell1-3.21.3 {
577  # too many arguments
578  catchcmd "test.db" ".schema FOO BAD"
579} {1 {Usage: .schema ?LIKE-PATTERN?}}
580
581do_test shell1-3.21.4 {
582  catchcmd "test.db" {
583     CREATE TABLE t1(x);
584     CREATE VIEW v2 AS SELECT x+1 AS y FROM t1;
585     CREATE VIEW v1 AS SELECT y+1 FROM v2;
586  }
587  catchcmd "test.db" ".schema"
588} {0 {CREATE TABLE t1(x);
589CREATE VIEW v2 AS SELECT x+1 AS y FROM t1;
590CREATE VIEW v1 AS SELECT y+1 FROM v2;}}
591db eval {DROP VIEW v1; DROP VIEW v2; DROP TABLE t1;}
592
593# .separator STRING  Change column separator used by output and .import
594do_test shell1-3.22.1 {
595  catchcmd "test.db" ".separator"
596} {1 {Usage: .separator COL ?ROW?}}
597do_test shell1-3.22.2 {
598  catchcmd "test.db" ".separator FOO"
599} {0 {}}
600do_test shell1-3.22.3 {
601  catchcmd "test.db" ".separator ABC XYZ"
602} {0 {}}
603do_test shell1-3.22.4 {
604  # too many arguments
605  catchcmd "test.db" ".separator FOO BAD BAD2"
606} {1 {Usage: .separator COL ?ROW?}}
607
608# .show                  Show the current values for various settings
609do_test shell1-3.23.1 {
610  set res [catchcmd "test.db" ".show"]
611  list [regexp {echo:} $res] \
612       [regexp {explain:} $res] \
613       [regexp {headers:} $res] \
614       [regexp {mode:} $res] \
615       [regexp {nullvalue:} $res] \
616       [regexp {output:} $res] \
617       [regexp {colseparator:} $res] \
618       [regexp {rowseparator:} $res] \
619       [regexp {stats:} $res] \
620       [regexp {width:} $res]
621} {1 1 1 1 1 1 1 1 1 1}
622do_test shell1-3.23.2 {
623  # too many arguments
624  catchcmd "test.db" ".show BAD"
625} {1 {Usage: .show}}
626
627# .stats ON|OFF          Turn stats on or off
628do_test shell1-3.23b.1 {
629  catchcmd "test.db" ".stats"
630} {1 {Usage: .stats on|off}}
631do_test shell1-3.23b.2 {
632  catchcmd "test.db" ".stats ON"
633} {0 {}}
634do_test shell1-3.23b.3 {
635  catchcmd "test.db" ".stats OFF"
636} {0 {}}
637do_test shell1-3.23b.4 {
638  # too many arguments
639  catchcmd "test.db" ".stats OFF BAD"
640} {1 {Usage: .stats on|off}}
641
642# .tables ?TABLE?        List names of tables
643#                          If TABLE specified, only list tables matching
644#                          LIKE pattern TABLE.
645do_test shell1-3.24.1 {
646  catchcmd "test.db" ".tables"
647} {0 {}}
648do_test shell1-3.24.2 {
649  catchcmd "test.db" ".tables FOO"
650} {0 {}}
651do_test shell1-3.24.3 {
652  # too many arguments
653  catchcmd "test.db" ".tables FOO BAD"
654} {0 {}}
655
656# .timeout MS            Try opening locked tables for MS milliseconds
657do_test shell1-3.25.1 {
658  catchcmd "test.db" ".timeout"
659} {0 {}}
660do_test shell1-3.25.2 {
661  catchcmd "test.db" ".timeout zzz"
662  # this should be treated the same as a '0' timeout
663} {0 {}}
664do_test shell1-3.25.3 {
665  catchcmd "test.db" ".timeout 1"
666} {0 {}}
667do_test shell1-3.25.4 {
668  # too many arguments
669  catchcmd "test.db" ".timeout 1 BAD"
670} {0 {}}
671
672# .width NUM NUM ...     Set column widths for "column" mode
673do_test shell1-3.26.1 {
674  catchcmd "test.db" ".width"
675} {0 {}}
676do_test shell1-3.26.2 {
677  catchcmd "test.db" ".width xxx"
678  # this should be treated the same as a '0' width for col 1
679} {0 {}}
680do_test shell1-3.26.3 {
681  catchcmd "test.db" ".width xxx yyy"
682  # this should be treated the same as a '0' width for col 1 and 2
683} {0 {}}
684do_test shell1-3.26.4 {
685  catchcmd "test.db" ".width 1 1"
686  # this should be treated the same as a '1' width for col 1 and 2
687} {0 {}}
688do_test shell1-3.26.5 {
689  catchcmd "test.db" ".mode column\n.width 10 -10\nSELECT 'abcdefg', 123456;"
690  # this should be treated the same as a '1' width for col 1 and 2
691} {0 {abcdefg         123456}}
692do_test shell1-3.26.6 {
693  catchcmd "test.db" ".mode column\n.width -10 10\nSELECT 'abcdefg', 123456;"
694  # this should be treated the same as a '1' width for col 1 and 2
695} {0 {   abcdefg  123456    }}
696
697
698# .timer ON|OFF          Turn the CPU timer measurement on or off
699do_test shell1-3.27.1 {
700  catchcmd "test.db" ".timer"
701} {1 {Usage: .timer on|off}}
702do_test shell1-3.27.2 {
703  catchcmd "test.db" ".timer ON"
704} {0 {}}
705do_test shell1-3.27.3 {
706  catchcmd "test.db" ".timer OFF"
707} {0 {}}
708do_test shell1-3.27.4 {
709  # too many arguments
710  catchcmd "test.db" ".timer OFF BAD"
711} {1 {Usage: .timer on|off}}
712
713do_test shell1-3-28.1 {
714  catchcmd test.db \
715     ".log stdout\nSELECT coalesce(sqlite_log(123,'hello'),'456');"
716} "0 {(123) hello\n456}"
717
718do_test shell1-3-29.1 {
719  catchcmd "test.db" ".print this is a test"
720} {0 {this is a test}}
721
722# dot-command argument quoting
723do_test shell1-3-30.1 {
724  catchcmd {test.db} {.print "this\"is'a\055test" 'this\"is\\a\055test'}
725} {0 {this"is'a-test this\"is\\a\055test}}
726do_test shell1-3-31.1 {
727  catchcmd {test.db} {.print "this\nis\ta\\test" 'this\nis\ta\\test'}
728} [list 0 "this\nis\ta\\test this\\nis\\ta\\\\test"]
729
730
731# Test the output of the ".dump" command
732#
733do_test shell1-4.1 {
734  db close
735  forcedelete test.db
736  sqlite3 db test.db
737  db eval {
738    PRAGMA encoding=UTF16;
739    CREATE TABLE t1(x);
740    INSERT INTO t1 VALUES(null), (''), (1), (2.25), ('hello'), (x'807f');
741    CREATE TABLE t3(x,y);
742    INSERT INTO t3 VALUES(1,null), (2,''), (3,1),
743                         (4,2.25), (5,'hello'), (6,x'807f');
744  }
745  catchcmd test.db {.dump}
746} {0 {PRAGMA foreign_keys=OFF;
747BEGIN TRANSACTION;
748CREATE TABLE t1(x);
749INSERT INTO "t1" VALUES(NULL);
750INSERT INTO "t1" VALUES('');
751INSERT INTO "t1" VALUES(1);
752INSERT INTO "t1" VALUES(2.25);
753INSERT INTO "t1" VALUES('hello');
754INSERT INTO "t1" VALUES(X'807F');
755CREATE TABLE t3(x,y);
756INSERT INTO "t3" VALUES(1,NULL);
757INSERT INTO "t3" VALUES(2,'');
758INSERT INTO "t3" VALUES(3,1);
759INSERT INTO "t3" VALUES(4,2.25);
760INSERT INTO "t3" VALUES(5,'hello');
761INSERT INTO "t3" VALUES(6,X'807F');
762COMMIT;}}
763
764# Test the output of ".mode insert"
765#
766do_test shell1-4.2.1 {
767  catchcmd test.db ".mode insert t1\nselect * from t1;"
768} {0 {INSERT INTO t1 VALUES(NULL);
769INSERT INTO t1 VALUES('');
770INSERT INTO t1 VALUES(1);
771INSERT INTO t1 VALUES(2.25);
772INSERT INTO t1 VALUES('hello');
773INSERT INTO t1 VALUES(X'807f');}}
774
775# Test the output of ".mode insert" with headers
776#
777do_test shell1-4.2.2 {
778  catchcmd test.db ".mode insert t1\n.headers on\nselect * from t1;"
779} {0 {INSERT INTO t1(x) VALUES(NULL);
780INSERT INTO t1(x) VALUES('');
781INSERT INTO t1(x) VALUES(1);
782INSERT INTO t1(x) VALUES(2.25);
783INSERT INTO t1(x) VALUES('hello');
784INSERT INTO t1(x) VALUES(X'807f');}}
785
786# Test the output of ".mode insert"
787#
788do_test shell1-4.2.3 {
789  catchcmd test.db ".mode insert t3\nselect * from t3;"
790} {0 {INSERT INTO t3 VALUES(1,NULL);
791INSERT INTO t3 VALUES(2,'');
792INSERT INTO t3 VALUES(3,1);
793INSERT INTO t3 VALUES(4,2.25);
794INSERT INTO t3 VALUES(5,'hello');
795INSERT INTO t3 VALUES(6,X'807f');}}
796
797# Test the output of ".mode insert" with headers
798#
799do_test shell1-4.2.4 {
800  catchcmd test.db ".mode insert t3\n.headers on\nselect * from t3;"
801} {0 {INSERT INTO t3(x,y) VALUES(1,NULL);
802INSERT INTO t3(x,y) VALUES(2,'');
803INSERT INTO t3(x,y) VALUES(3,1);
804INSERT INTO t3(x,y) VALUES(4,2.25);
805INSERT INTO t3(x,y) VALUES(5,'hello');
806INSERT INTO t3(x,y) VALUES(6,X'807f');}}
807
808# Test the output of ".mode tcl"
809#
810do_test shell1-4.3 {
811  db close
812  forcedelete test.db
813  sqlite3 db test.db
814  db eval {
815    PRAGMA encoding=UTF8;
816    CREATE TABLE t1(x);
817    INSERT INTO t1 VALUES(null), (''), (1), (2.25), ('hello'), (x'807f');
818  }
819  catchcmd test.db ".mode tcl\nselect * from t1;"
820} {0 {""
821""
822"1"
823"2.25"
824"hello"
825"\200\177"}}
826
827# Test the output of ".mode tcl" with multiple columns
828#
829do_test shell1-4.4 {
830  db eval {
831    CREATE TABLE t2(x,y);
832    INSERT INTO t2 VALUES(null, ''), (1, 2.25), ('hello', x'807f');
833  }
834  catchcmd test.db ".mode tcl\nselect * from t2;"
835} {0 {"" ""
836"1" "2.25"
837"hello" "\200\177"}}
838
839# Test the output of ".mode tcl" with ".nullvalue"
840#
841do_test shell1-4.5 {
842  catchcmd test.db ".mode tcl\n.nullvalue NULL\nselect * from t2;"
843} {0 {"NULL" ""
844"1" "2.25"
845"hello" "\200\177"}}
846
847# Test the output of ".mode tcl" with Tcl reserved characters
848#
849do_test shell1-4.6 {
850  db eval {
851    CREATE TABLE tcl1(x);
852    INSERT INTO tcl1 VALUES('"'), ('['), (']'), ('\{'), ('\}'), (';'), ('$');
853  }
854  foreach {x y} [catchcmd test.db ".mode tcl\nselect * from tcl1;"] break
855  list $x $y [llength $y]
856} {0 {"\""
857"["
858"]"
859"\\{"
860"\\}"
861";"
862"$"} 7}
863
864# Test using arbitrary byte data with the shell via standard input/output.
865#
866do_test shell1-5.0 {
867  #
868  # NOTE: Skip NUL byte because it appears to be incompatible with command
869  #       shell argument parsing.
870  #
871  for {set i 1} {$i < 256} {incr i} {
872    #
873    # NOTE: Due to how the Tcl [exec] command works (i.e. where it treats
874    #       command channels opened for it as textual ones), the carriage
875    #       return character (and on Windows, the end-of-file character)
876    #       cannot be used here.
877    #
878    if {$i==0x0D || ($tcl_platform(platform)=="windows" && $i==0x1A)} {
879      continue
880    }
881    set hex [format %02X $i]
882    set char [subst \\x$hex]; set oldChar $char
883    set escapes [list]
884    if {$tcl_platform(platform)=="windows"} {
885      #
886      # NOTE: On Windows, we need to escape all the whitespace characters,
887      #       the alarm (\a) character, and those with special meaning to
888      #       the SQLite shell itself.
889      #
890      set escapes [list \
891          \a \\a \b \\b \t \\t \n \\n \v \\v \f \\f \r \\r \
892          " " "\" \"" \" \\\" ' \"'\" \\ \\\\]
893    } else {
894      #
895      # NOTE: On Unix, we need to escape most of the whitespace characters
896      #       and those with special meaning to the SQLite shell itself.
897      #       The alarm (\a), backspace (\b), and carriage-return (\r)
898      #       characters do not appear to require escaping on Unix.  For
899      #       the alarm and backspace characters, this is probably due to
900      #       differences in the command shell.  For the carriage-return,
901      #       it is probably due to differences in how Tcl handles command
902      #       channel end-of-line translations.
903      #
904      set escapes [list \
905          \t \\t \n \\n \v \\v \f \\f \
906          " " "\" \"" \" \\\" ' \"'\" \\ \\\\]
907    }
908    set char [string map $escapes $char]
909    set x [catchcmdex test.db ".print $char\n"]
910    set code [lindex $x 0]
911    set res [lindex $x 1]
912    if {$code ne "0"} {
913      error "failed with error: $res"
914    }
915    if {$res ne "$oldChar\n"} {
916      error "failed with byte $hex mismatch"
917    }
918  }
919} {}
920
921finish_test
922