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