xref: /sqlite-3.40.0/test/shell1.test (revision ccb37816)
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
24set CLI [test_find_cli]
25db close
26forcedelete test.db test.db-journal test.db-wal
27sqlite3 db test.db
28
29#----------------------------------------------------------------------------
30# Test cases shell1-1.*: Basic command line option handling.
31#
32
33# invalid option
34do_test shell1-1.1.1 {
35  set res [catchcmd "-bad test.db" ""]
36  set rc [lindex $res 0]
37  list $rc \
38       [regexp {Error: unknown option: -bad} $res]
39} {1 1}
40do_test shell1-1.1.1b {
41  set res [catchcmd "test.db -bad" ""]
42  set rc [lindex $res 0]
43  list $rc \
44       [regexp {Error: unknown option: -bad} $res]
45} {1 1}
46# error on extra options
47do_test shell1-1.1.2 {
48  catchcmd "test.db \"select+3\" \"select+4\"" ""
49} {0 {3
504}}
51# error on extra options
52do_test shell1-1.1.3 {
53  catchcmd "test.db FOO test.db BAD" ".quit"
54} {1 {Error: near "FOO": syntax error}}
55
56# -help
57do_test shell1-1.2.1 {
58  set res [catchcmd "-help test.db" ""]
59  set rc [lindex $res 0]
60  list $rc \
61       [regexp {Usage} $res] \
62       [regexp {\-init} $res] \
63       [regexp {\-version} $res]
64} {1 1 1 1}
65
66# -init filename       read/process named file
67do_test shell1-1.3.1 {
68  catchcmd "-init FOO test.db" ""
69} {0 {}}
70do_test shell1-1.3.2 {
71  catchcmd "-init FOO test.db .quit BAD" ""
72} {0 {}}
73do_test shell1-1.3.3 {
74  catchcmd "-init FOO test.db BAD .quit" ""
75} {1 {Error: near "BAD": syntax error}}
76
77# -echo                print commands before execution
78do_test shell1-1.4.1 {
79  catchcmd "-echo test.db" ""
80} {0 {}}
81
82# -[no]header          turn headers on or off
83do_test shell1-1.5.1 {
84  catchcmd "-header test.db" ""
85} {0 {}}
86do_test shell1-1.5.2 {
87  catchcmd "-noheader test.db" ""
88} {0 {}}
89
90# -bail                stop after hitting an error
91do_test shell1-1.6.1 {
92  catchcmd "-bail test.db" ""
93} {0 {}}
94
95# -interactive         force interactive I/O
96do_test shell1-1.7.1 {
97  set res [catchcmd "-interactive test.db" ".quit"]
98  set rc [lindex $res 0]
99  list $rc \
100       [regexp {SQLite version} $res] \
101       [regexp {Enter ".help" for usage hints} $res]
102} {0 1 1}
103
104# -batch               force batch I/O
105do_test shell1-1.8.1 {
106  catchcmd "-batch test.db" ""
107} {0 {}}
108
109# -column              set output mode to 'column'
110do_test shell1-1.9.1 {
111  catchcmd "-column test.db" ""
112} {0 {}}
113
114# -csv                 set output mode to 'csv'
115do_test shell1-1.10.1 {
116  catchcmd "-csv test.db" ""
117} {0 {}}
118
119# -html                set output mode to HTML
120do_test shell1-1.11.1 {
121  catchcmd "-html test.db" ""
122} {0 {}}
123
124# -line                set output mode to 'line'
125do_test shell1-1.12.1 {
126  catchcmd "-line test.db" ""
127} {0 {}}
128
129# -list                set output mode to 'list'
130do_test shell1-1.13.1 {
131  catchcmd "-list test.db" ""
132} {0 {}}
133
134# -separator 'x'       set output field separator (|)
135do_test shell1-1.14.1 {
136  catchcmd "-separator 'x' test.db" ""
137} {0 {}}
138do_test shell1-1.14.2 {
139  catchcmd "-separator x test.db" ""
140} {0 {}}
141do_test shell1-1.14.3 {
142  set res [catchcmd "-separator" ""]
143  set rc [lindex $res 0]
144  list $rc \
145       [regexp {Error: missing argument to -separator} $res]
146} {1 1}
147
148# -stats               print memory stats before each finalize
149do_test shell1-1.14b.1 {
150  catchcmd "-stats test.db" ""
151} {0 {}}
152
153# -nullvalue 'text'    set text string for NULL values
154do_test shell1-1.15.1 {
155  catchcmd "-nullvalue 'x' test.db" ""
156} {0 {}}
157do_test shell1-1.15.2 {
158  catchcmd "-nullvalue x test.db" ""
159} {0 {}}
160do_test shell1-1.15.3 {
161  set res [catchcmd "-nullvalue" ""]
162  set rc [lindex $res 0]
163  list $rc \
164       [regexp {Error: missing argument to -nullvalue} $res]
165} {1 1}
166
167# -version             show SQLite version
168do_test shell1-1.16.1 {
169  set x [catchcmd "-version test.db" ""]
170} {/3.[0-9.]+ 20\d\d-[01]\d-\d\d \d\d:\d\d:\d\d [0-9a-f]+/}
171
172#----------------------------------------------------------------------------
173# Test cases shell1-2.*: Basic "dot" command token parsing.
174#
175
176# check first token handling
177do_test shell1-2.1.1 {
178  catchcmd "test.db" ".foo"
179} {1 {Error: unknown command or invalid arguments:  "foo". Enter ".help" for help}}
180do_test shell1-2.1.2 {
181  catchcmd "test.db" ".\"foo OFF\""
182} {1 {Error: unknown command or invalid arguments:  "foo OFF". Enter ".help" for help}}
183do_test shell1-2.1.3 {
184  catchcmd "test.db" ".\'foo OFF\'"
185} {1 {Error: unknown command or invalid arguments:  "foo OFF". Enter ".help" for help}}
186
187# unbalanced quotes
188do_test shell1-2.2.1 {
189  catchcmd "test.db" ".\"foo OFF"
190} {1 {Error: unknown command or invalid arguments:  "foo OFF". Enter ".help" for help}}
191do_test shell1-2.2.2 {
192  catchcmd "test.db" ".\'foo OFF"
193} {1 {Error: unknown command or invalid arguments:  "foo OFF". Enter ".help" for help}}
194do_test shell1-2.2.3 {
195  catchcmd "test.db" ".explain \"OFF"
196} {0 {}}
197do_test shell1-2.2.4 {
198  catchcmd "test.db" ".explain \'OFF"
199} {0 {}}
200do_test shell1-2.2.5 {
201  catchcmd "test.db" ".mode \"insert FOO"
202} {1 {Error: mode should be one of: ascii column csv html insert line list quote tabs tcl}}
203do_test shell1-2.2.6 {
204  catchcmd "test.db" ".mode \'insert FOO"
205} {1 {Error: mode should be one of: ascii column csv html insert line list quote tabs tcl}}
206
207# check multiple tokens, and quoted tokens
208do_test shell1-2.3.1 {
209  catchcmd "test.db" ".explain 1"
210} {0 {}}
211do_test shell1-2.3.2 {
212  catchcmd "test.db" ".explain on"
213} {0 {}}
214do_test shell1-2.3.3 {
215  catchcmd "test.db" ".explain \"1 2 3\""
216} {1 {ERROR: Not a boolean value: "1 2 3". Assuming "no".}}
217do_test shell1-2.3.4 {
218  catchcmd "test.db" ".explain \"OFF\""
219} {0 {}}
220do_test shell1-2.3.5 {
221  catchcmd "test.db" ".\'explain\' \'OFF\'"
222} {0 {}}
223do_test shell1-2.3.6 {
224  catchcmd "test.db" ".explain \'OFF\'"
225} {0 {}}
226do_test shell1-2.3.7 {
227  catchcmd "test.db" ".\'explain\' \'OFF\'"
228} {0 {}}
229
230# check quoted args are unquoted
231do_test shell1-2.4.1 {
232  catchcmd "test.db" ".mode FOO"
233} {1 {Error: mode should be one of: ascii column csv html insert line list quote tabs tcl}}
234do_test shell1-2.4.2 {
235  catchcmd "test.db" ".mode csv"
236} {0 {}}
237do_test shell1-2.4.2 {
238  catchcmd "test.db" ".mode \"csv\""
239} {0 {}}
240
241
242#----------------------------------------------------------------------------
243# Test cases shell1-3.*: Basic test that "dot" command can be called.
244#
245
246# .backup ?DB? FILE      Backup DB (default "main") to FILE
247do_test shell1-3.1.1 {
248  catchcmd "test.db" ".backup"
249} {1 {missing FILENAME argument on .backup}}
250do_test shell1-3.1.2 {
251  catchcmd "test.db" ".backup FOO"
252} {0 {}}
253do_test shell1-3.1.3 {
254  catchcmd "test.db" ".backup FOO BAR"
255} {1 {Error: unknown database FOO}}
256do_test shell1-3.1.4 {
257  # too many arguments
258  catchcmd "test.db" ".backup FOO BAR BAD"
259} {1 {Usage: .backup ?DB? ?OPTIONS? FILENAME}}
260
261# .bail ON|OFF           Stop after hitting an error.  Default OFF
262do_test shell1-3.2.1 {
263  catchcmd "test.db" ".bail"
264} {1 {Usage: .bail on|off}}
265do_test shell1-3.2.2 {
266  catchcmd "test.db" ".bail ON"
267} {0 {}}
268do_test shell1-3.2.3 {
269  catchcmd "test.db" ".bail OFF"
270} {0 {}}
271do_test shell1-3.2.4 {
272  # too many arguments
273  catchcmd "test.db" ".bail OFF BAD"
274} {1 {Usage: .bail on|off}}
275
276ifcapable vtab {
277# .databases             List names and files of attached databases
278do_test shell1-3.3.1 {
279  catchcmd "-csv test.db" ".databases"
280} "/0.+main.+[string map {/ ".{1,2}"} [string range [get_pwd] 0 10]].*/"
281do_test shell1-3.3.2 {
282  # extra arguments ignored
283  catchcmd "test.db" ".databases BAD"
284} "/0.+main.+[string map {/ ".{1,2}"} [string range [get_pwd] 0 10]].*/"
285}
286
287# .dump ?TABLE? ...      Dump the database in an SQL text format
288#                          If TABLE specified, only dump tables matching
289#                          LIKE pattern TABLE.
290do_test shell1-3.4.1 {
291  set res [catchcmd "test.db" ".dump"]
292  list [regexp {BEGIN TRANSACTION;} $res] \
293       [regexp {COMMIT;} $res]
294} {1 1}
295do_test shell1-3.4.2 {
296  set res [catchcmd "test.db" ".dump FOO"]
297  list [regexp {BEGIN TRANSACTION;} $res] \
298       [regexp {COMMIT;} $res]
299} {1 1}
300do_test shell1-3.4.3 {
301  # too many arguments
302  catchcmd "test.db" ".dump FOO BAD"
303} {1 {Usage: .dump ?--preserve-rowids? ?--newlines? ?LIKE-PATTERN?}}
304
305# .echo ON|OFF           Turn command echo on or off
306do_test shell1-3.5.1 {
307  catchcmd "test.db" ".echo"
308} {1 {Usage: .echo on|off}}
309do_test shell1-3.5.2 {
310  catchcmd "test.db" ".echo ON"
311} {0 {}}
312do_test shell1-3.5.3 {
313  catchcmd "test.db" ".echo OFF"
314} {0 {}}
315do_test shell1-3.5.4 {
316  # too many arguments
317  catchcmd "test.db" ".echo OFF BAD"
318} {1 {Usage: .echo on|off}}
319
320# .exit                  Exit this program
321do_test shell1-3.6.1 {
322  catchcmd "test.db" ".exit"
323} {0 {}}
324
325# .explain ON|OFF        Turn output mode suitable for EXPLAIN on or off.
326do_test shell1-3.7.1 {
327  catchcmd "test.db" ".explain"
328  # explain is the exception to the booleans.  without an option, it turns it on.
329} {0 {}}
330do_test shell1-3.7.2 {
331  catchcmd "test.db" ".explain ON"
332} {0 {}}
333do_test shell1-3.7.3 {
334  catchcmd "test.db" ".explain OFF"
335} {0 {}}
336do_test shell1-3.7.4 {
337  # extra arguments ignored
338  catchcmd "test.db" ".explain OFF BAD"
339} {0 {}}
340
341
342# .header(s) ON|OFF      Turn display of headers on or off
343do_test shell1-3.9.1 {
344  catchcmd "test.db" ".header"
345} {1 {Usage: .headers on|off}}
346do_test shell1-3.9.2 {
347  catchcmd "test.db" ".header ON"
348} {0 {}}
349do_test shell1-3.9.3 {
350  catchcmd "test.db" ".header OFF"
351} {0 {}}
352do_test shell1-3.9.4 {
353  # too many arguments
354  catchcmd "test.db" ".header OFF BAD"
355} {1 {Usage: .headers on|off}}
356
357do_test shell1-3.9.5 {
358  catchcmd "test.db" ".headers"
359} {1 {Usage: .headers on|off}}
360do_test shell1-3.9.6 {
361  catchcmd "test.db" ".headers ON"
362} {0 {}}
363do_test shell1-3.9.7 {
364  catchcmd "test.db" ".headers OFF"
365} {0 {}}
366do_test shell1-3.9.8 {
367  # too many arguments
368  catchcmd "test.db" ".headers OFF BAD"
369} {1 {Usage: .headers on|off}}
370
371# .help                  Show this message
372do_test shell1-3.10.1 {
373  set res [catchcmd "test.db" ".help"]
374  # look for a few of the possible help commands
375  list [regexp {.help} $res] \
376       [regexp {.quit} $res] \
377       [regexp {.show} $res]
378} {1 1 1}
379do_test shell1-3.10.2 {
380  # we allow .help to take extra args (it is help after all)
381  set res [catchcmd "test.db" ".help *"]
382  # look for a few of the possible help commands
383  list [regexp {.help} $res] \
384       [regexp {.quit} $res] \
385       [regexp {.show} $res]
386} {1 1 1}
387
388# .import FILE TABLE     Import data from FILE into TABLE
389do_test shell1-3.11.1 {
390  catchcmd "test.db" ".import"
391} {/1 .ERROR: missing FILE argument.*/}
392do_test shell1-3.11.2 {
393  catchcmd "test.db" ".import FOO"
394} {/1 .ERROR: missing TABLE argument.*/}
395do_test shell1-3.11.3 {
396  # too many arguments
397  catchcmd "test.db" ".import FOO BAR BAD"
398} {/1 .ERROR: extra argument: "BAD".*./}
399
400# .indexes ?TABLE?       Show names of all indexes
401#                          If TABLE specified, only show indexes for tables
402#                          matching LIKE pattern TABLE.
403do_test shell1-3.12.1 {
404  catchcmd "test.db" ".indexes"
405} {0 {}}
406do_test shell1-3.12.2 {
407  catchcmd "test.db" ".indexes FOO"
408} {0 {}}
409do_test shell1-3.12.2-legacy {
410  catchcmd "test.db" ".indices FOO"
411} {0 {}}
412do_test shell1-3.12.3 {
413  # too many arguments
414  catchcmd "test.db" ".indexes FOO BAD"
415} {1 {Usage: .indexes ?LIKE-PATTERN?}}
416
417# .mode MODE ?TABLE?     Set output mode where MODE is one of:
418#                          ascii    Columns/rows delimited by 0x1F and 0x1E
419#                          csv      Comma-separated values
420#                          column   Left-aligned columns.  (See .width)
421#                          html     HTML <table> code
422#                          insert   SQL insert statements for TABLE
423#                          line     One value per line
424#                          list     Values delimited by .separator strings
425#                          tabs     Tab-separated values
426#                          tcl      TCL list elements
427do_test shell1-3.13.1 {
428  catchcmd "test.db" ".mode"
429} {0 {current output mode: list}}
430do_test shell1-3.13.2 {
431  catchcmd "test.db" ".mode FOO"
432} {1 {Error: mode should be one of: ascii column csv html insert line list quote tabs tcl}}
433do_test shell1-3.13.3 {
434  catchcmd "test.db" ".mode csv"
435} {0 {}}
436do_test shell1-3.13.4 {
437  catchcmd "test.db" ".mode column"
438} {0 {}}
439do_test shell1-3.13.5 {
440  catchcmd "test.db" ".mode html"
441} {0 {}}
442do_test shell1-3.13.6 {
443  catchcmd "test.db" ".mode insert"
444} {0 {}}
445do_test shell1-3.13.7 {
446  catchcmd "test.db" ".mode line"
447} {0 {}}
448do_test shell1-3.13.8 {
449  catchcmd "test.db" ".mode list"
450} {0 {}}
451do_test shell1-3.13.9 {
452  catchcmd "test.db" ".mode tabs"
453} {0 {}}
454do_test shell1-3.13.10 {
455  catchcmd "test.db" ".mode tcl"
456} {0 {}}
457do_test shell1-3.13.11 {
458  # extra arguments ignored
459  catchcmd "test.db" ".mode tcl BAD"
460} {0 {}}
461
462# don't allow partial mode type matches
463do_test shell1-3.13.12 {
464  catchcmd "test.db" ".mode l"
465} {1 {Error: mode should be one of: ascii column csv html insert line list quote tabs tcl}}
466do_test shell1-3.13.13 {
467  catchcmd "test.db" ".mode li"
468} {1 {Error: mode should be one of: ascii column csv html insert line list quote tabs tcl}}
469do_test shell1-3.13.14 {
470  catchcmd "test.db" ".mode lin"
471} {0 {}}
472
473# .nullvalue STRING      Print STRING in place of NULL values
474do_test shell1-3.14.1 {
475  catchcmd "test.db" ".nullvalue"
476} {1 {Usage: .nullvalue STRING}}
477do_test shell1-3.14.2 {
478  catchcmd "test.db" ".nullvalue FOO"
479} {0 {}}
480do_test shell1-3.14.3 {
481  # too many arguments
482  catchcmd "test.db" ".nullvalue FOO BAD"
483} {1 {Usage: .nullvalue STRING}}
484
485# .output FILENAME       Send output to FILENAME
486do_test shell1-3.15.1 {
487  catchcmd "test.db" ".output"
488} {0 {}}
489do_test shell1-3.15.2 {
490  catchcmd "test.db" ".output FOO"
491} {0 {}}
492do_test shell1-3.15.3 {
493  # too many arguments
494  catchcmd "test.db" ".output FOO BAD"
495} {1 {Usage: .output [-e|-x|FILE]}}
496
497# .output stdout         Send output to the screen
498do_test shell1-3.16.1 {
499  catchcmd "test.db" ".output stdout"
500} {0 {}}
501do_test shell1-3.16.2 {
502  # too many arguments
503  catchcmd "test.db" ".output stdout BAD"
504} {1 {Usage: .output [-e|-x|FILE]}}
505
506# .prompt MAIN CONTINUE  Replace the standard prompts
507do_test shell1-3.17.1 {
508  catchcmd "test.db" ".prompt"
509} {0 {}}
510do_test shell1-3.17.2 {
511  catchcmd "test.db" ".prompt FOO"
512} {0 {}}
513do_test shell1-3.17.3 {
514  catchcmd "test.db" ".prompt FOO BAR"
515} {0 {}}
516do_test shell1-3.17.4 {
517  # too many arguments
518  catchcmd "test.db" ".prompt FOO BAR BAD"
519} {0 {}}
520
521# .quit                  Exit this program
522do_test shell1-3.18.1 {
523  catchcmd "test.db" ".quit"
524} {0 {}}
525do_test shell1-3.18.2 {
526  # too many arguments
527  catchcmd "test.db" ".quit BAD"
528} {0 {}}
529
530# .read FILENAME         Execute SQL in FILENAME
531do_test shell1-3.19.1 {
532  catchcmd "test.db" ".read"
533} {1 {Usage: .read FILE}}
534do_test shell1-3.19.2 {
535  forcedelete FOO
536  catchcmd "test.db" ".read FOO"
537} {1 {Error: cannot open "FOO"}}
538do_test shell1-3.19.3 {
539  # too many arguments
540  catchcmd "test.db" ".read FOO BAD"
541} {1 {Usage: .read FILE}}
542
543# .restore ?DB? FILE     Restore content of DB (default "main") from FILE
544do_test shell1-3.20.1 {
545  catchcmd "test.db" ".restore"
546} {1 {Usage: .restore ?DB? FILE}}
547do_test shell1-3.20.2 {
548  catchcmd "test.db" ".restore FOO"
549} {0 {}}
550do_test shell1-3.20.3 {
551  catchcmd "test.db" ".restore FOO BAR"
552} {1 {Error: unknown database FOO}}
553do_test shell1-3.20.4 {
554  # too many arguments
555  catchcmd "test.db" ".restore FOO BAR BAD"
556} {1 {Usage: .restore ?DB? FILE}}
557
558ifcapable vtab {
559# .schema ?TABLE?        Show the CREATE statements
560#                          If TABLE specified, only show tables matching
561#                          LIKE pattern TABLE.
562do_test shell1-3.21.1 {
563  catchcmd "test.db" ".schema"
564} {0 {}}
565do_test shell1-3.21.2 {
566  catchcmd "test.db" ".schema FOO"
567} {0 {}}
568do_test shell1-3.21.3 {
569  # too many arguments
570  catchcmd "test.db" ".schema FOO BAD"
571} {1 {Usage: .schema ?--indent? ?LIKE-PATTERN?}}
572
573do_test shell1-3.21.4 {
574  catchcmd "test.db" {
575     CREATE TABLE t1(x);
576     CREATE VIEW v2 AS SELECT x+1 AS y FROM t1;
577     CREATE VIEW v1 AS SELECT y+1 FROM v2;
578  }
579  catchcmd "test.db" ".schema"
580} {0 {CREATE TABLE t1(x);
581CREATE VIEW v2 AS SELECT x+1 AS y FROM t1
582/* v2(y) */;
583CREATE VIEW v1 AS SELECT y+1 FROM v2
584/* v1("y+1") */;}}
585db eval {DROP VIEW v1; DROP VIEW v2; DROP TABLE t1;}
586}
587
588# .separator STRING  Change column separator used by output and .import
589do_test shell1-3.22.1 {
590  catchcmd "test.db" ".separator"
591} {1 {Usage: .separator COL ?ROW?}}
592do_test shell1-3.22.2 {
593  catchcmd "test.db" ".separator FOO"
594} {0 {}}
595do_test shell1-3.22.3 {
596  catchcmd "test.db" ".separator ABC XYZ"
597} {0 {}}
598do_test shell1-3.22.4 {
599  # too many arguments
600  catchcmd "test.db" ".separator FOO BAD BAD2"
601} {1 {Usage: .separator COL ?ROW?}}
602
603# .show                  Show the current values for various settings
604do_test shell1-3.23.1 {
605  set res [catchcmd "test.db" ".show"]
606  list [regexp {echo:} $res] \
607       [regexp {explain:} $res] \
608       [regexp {headers:} $res] \
609       [regexp {mode:} $res] \
610       [regexp {nullvalue:} $res] \
611       [regexp {output:} $res] \
612       [regexp {colseparator:} $res] \
613       [regexp {rowseparator:} $res] \
614       [regexp {stats:} $res] \
615       [regexp {width:} $res]
616} {1 1 1 1 1 1 1 1 1 1}
617do_test shell1-3.23.2 {
618  # too many arguments
619  catchcmd "test.db" ".show BAD"
620} {1 {Usage: .show}}
621
622# .stats ON|OFF          Turn stats on or off
623#do_test shell1-3.23b.1 {
624#  catchcmd "test.db" ".stats"
625#} {1 {Usage: .stats on|off}}
626do_test shell1-3.23b.2 {
627  catchcmd "test.db" ".stats ON"
628} {0 {}}
629do_test shell1-3.23b.3 {
630  catchcmd "test.db" ".stats OFF"
631} {0 {}}
632do_test shell1-3.23b.4 {
633  # too many arguments
634  catchcmd "test.db" ".stats OFF BAD"
635} {1 {Usage: .stats ?on|off?}}
636
637# Ticket 7be932dfa60a8a6b3b26bcf7623ec46e0a403ddb 2018-06-07
638# Adverse interaction between .stats and .eqp
639#
640do_test shell1-3.23b.5 {
641  catchcmd "test.db" [string map {"\n    " "\n"} {
642    CREATE TEMP TABLE t1(x);
643    INSERT INTO t1 VALUES(1),(2);
644    .stats on
645    .eqp full
646    SELECT * FROM t1;
647  }]
648} {/1\n2\n/}
649
650# .tables ?TABLE?        List names of tables
651#                          If TABLE specified, only list tables matching
652#                          LIKE pattern TABLE.
653do_test shell1-3.24.1 {
654  catchcmd "test.db" ".tables"
655} {0 {}}
656do_test shell1-3.24.2 {
657  catchcmd "test.db" ".tables FOO"
658} {0 {}}
659do_test shell1-3.24.3 {
660  # too many arguments
661  catchcmd "test.db" ".tables FOO BAD"
662} {0 {}}
663
664# .timeout MS            Try opening locked tables for MS milliseconds
665do_test shell1-3.25.1 {
666  catchcmd "test.db" ".timeout"
667} {0 {}}
668do_test shell1-3.25.2 {
669  catchcmd "test.db" ".timeout zzz"
670  # this should be treated the same as a '0' timeout
671} {0 {}}
672do_test shell1-3.25.3 {
673  catchcmd "test.db" ".timeout 1"
674} {0 {}}
675do_test shell1-3.25.4 {
676  # too many arguments
677  catchcmd "test.db" ".timeout 1 BAD"
678} {0 {}}
679
680# .width NUM NUM ...     Set column widths for "column" mode
681do_test shell1-3.26.1 {
682  catchcmd "test.db" ".width"
683} {0 {}}
684do_test shell1-3.26.2 {
685  catchcmd "test.db" ".width xxx"
686  # this should be treated the same as a '0' width for col 1
687} {0 {}}
688do_test shell1-3.26.3 {
689  catchcmd "test.db" ".width xxx yyy"
690  # this should be treated the same as a '0' width for col 1 and 2
691} {0 {}}
692do_test shell1-3.26.4 {
693  catchcmd "test.db" ".width 1 1"
694  # this should be treated the same as a '1' width for col 1 and 2
695} {0 {}}
696do_test shell1-3.26.5 {
697  catchcmd "test.db" ".mode column\n.width 10 -10\nSELECT 'abcdefg', 123456;"
698  # this should be treated the same as a '1' width for col 1 and 2
699} {0 {abcdefg         123456}}
700do_test shell1-3.26.6 {
701  catchcmd "test.db" ".mode column\n.width -10 10\nSELECT 'abcdefg', 123456;"
702  # this should be treated the same as a '1' width for col 1 and 2
703} {0 {   abcdefg  123456    }}
704
705
706# .timer ON|OFF          Turn the CPU timer measurement on or off
707do_test shell1-3.27.1 {
708  catchcmd "test.db" ".timer"
709} {1 {Usage: .timer on|off}}
710do_test shell1-3.27.2 {
711  catchcmd "test.db" ".timer ON"
712} {0 {}}
713do_test shell1-3.27.3 {
714  catchcmd "test.db" ".timer OFF"
715} {0 {}}
716do_test shell1-3.27.4 {
717  # too many arguments
718  catchcmd "test.db" ".timer OFF BAD"
719} {1 {Usage: .timer on|off}}
720
721do_test shell1-3-28.1 {
722  catchcmd test.db \
723     ".log stdout\nSELECT coalesce(sqlite_log(123,'hello'),'456');"
724} "0 {(123) hello\n456}"
725
726do_test shell1-3-29.1 {
727  catchcmd "test.db" ".print this is a test"
728} {0 {this is a test}}
729
730# dot-command argument quoting
731do_test shell1-3-30.1 {
732  catchcmd {test.db} {.print "this\"is'a\055test" 'this\"is\\a\055test'}
733} {0 {this"is'a-test this\"is\\a\055test}}
734do_test shell1-3-31.1 {
735  catchcmd {test.db} {.print "this\nis\ta\\test" 'this\nis\ta\\test'}
736} [list 0 "this\nis\ta\\test this\\nis\\ta\\\\test"]
737
738
739# Test the output of the ".dump" command
740#
741do_test shell1-4.1 {
742  db close
743  forcedelete test.db
744  sqlite3 db test.db
745  db eval {
746    PRAGMA encoding=UTF16;
747    CREATE TABLE t1(x);
748    INSERT INTO t1 VALUES(null), (''), (1), (2.25), ('hello'), (x'807f');
749    CREATE TABLE t3(x,y);
750    INSERT INTO t3 VALUES(1,null), (2,''), (3,1),
751                         (4,2.25), (5,'hello'), (6,x'807f');
752  }
753  catchcmd test.db {.dump}
754} {0 {PRAGMA foreign_keys=OFF;
755BEGIN TRANSACTION;
756CREATE TABLE t1(x);
757INSERT INTO t1 VALUES(NULL);
758INSERT INTO t1 VALUES('');
759INSERT INTO t1 VALUES(1);
760INSERT INTO t1 VALUES(2.25);
761INSERT INTO t1 VALUES('hello');
762INSERT INTO t1 VALUES(X'807f');
763CREATE TABLE t3(x,y);
764INSERT INTO t3 VALUES(1,NULL);
765INSERT INTO t3 VALUES(2,'');
766INSERT INTO t3 VALUES(3,1);
767INSERT INTO t3 VALUES(4,2.25);
768INSERT INTO t3 VALUES(5,'hello');
769INSERT INTO t3 VALUES(6,X'807f');
770COMMIT;}}
771
772
773ifcapable vtab {
774
775# The --preserve-rowids option to .dump
776#
777do_test shell1-4.1.1 {
778  catchcmd test.db {.dump --preserve-rowids}
779} {0 {PRAGMA foreign_keys=OFF;
780BEGIN TRANSACTION;
781CREATE TABLE t1(x);
782INSERT INTO t1(rowid,x) VALUES(1,NULL);
783INSERT INTO t1(rowid,x) VALUES(2,'');
784INSERT INTO t1(rowid,x) VALUES(3,1);
785INSERT INTO t1(rowid,x) VALUES(4,2.25);
786INSERT INTO t1(rowid,x) VALUES(5,'hello');
787INSERT INTO t1(rowid,x) VALUES(6,X'807f');
788CREATE TABLE t3(x,y);
789INSERT INTO t3(rowid,x,y) VALUES(1,1,NULL);
790INSERT INTO t3(rowid,x,y) VALUES(2,2,'');
791INSERT INTO t3(rowid,x,y) VALUES(3,3,1);
792INSERT INTO t3(rowid,x,y) VALUES(4,4,2.25);
793INSERT INTO t3(rowid,x,y) VALUES(5,5,'hello');
794INSERT INTO t3(rowid,x,y) VALUES(6,6,X'807f');
795COMMIT;}}
796
797# If the table contains an INTEGER PRIMARY KEY, do not record a separate
798# rowid column in the output.
799#
800do_test shell1-4.1.2 {
801  db close
802  forcedelete test2.db
803  sqlite3 db test2.db
804  db eval {
805    CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
806    INSERT INTO t1 VALUES(1,null), (2,''), (3,1),
807                         (4,2.25), (5,'hello'), (6,x'807f');
808  }
809  catchcmd test2.db {.dump --preserve-rowids}
810} {0 {PRAGMA foreign_keys=OFF;
811BEGIN TRANSACTION;
812CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
813INSERT INTO t1 VALUES(1,NULL);
814INSERT INTO t1 VALUES(2,'');
815INSERT INTO t1 VALUES(3,1);
816INSERT INTO t1 VALUES(4,2.25);
817INSERT INTO t1 VALUES(5,'hello');
818INSERT INTO t1 VALUES(6,X'807f');
819COMMIT;}}
820
821# Verify that the table named [table] is correctly quoted and that
822# an INTEGER PRIMARY KEY DESC is not an alias for the rowid.
823#
824do_test shell1-4.1.3 {
825  db close
826  forcedelete test2.db
827  sqlite3 db test2.db
828  db eval {
829    CREATE TABLE [table](x INTEGER PRIMARY KEY DESC, y);
830    INSERT INTO [table] VALUES(1,null), (12,''), (23,1),
831                         (34,2.25), (45,'hello'), (56,x'807f');
832  }
833  catchcmd test2.db {.dump --preserve-rowids}
834} {0 {PRAGMA foreign_keys=OFF;
835BEGIN TRANSACTION;
836CREATE TABLE [table](x INTEGER PRIMARY KEY DESC, y);
837INSERT INTO "table"(rowid,x,y) VALUES(1,1,NULL);
838INSERT INTO "table"(rowid,x,y) VALUES(2,12,'');
839INSERT INTO "table"(rowid,x,y) VALUES(3,23,1);
840INSERT INTO "table"(rowid,x,y) VALUES(4,34,2.25);
841INSERT INTO "table"(rowid,x,y) VALUES(5,45,'hello');
842INSERT INTO "table"(rowid,x,y) VALUES(6,56,X'807f');
843COMMIT;}}
844
845# Do not record rowids for a WITHOUT ROWID table.  Also check correct quoting
846# of table names that contain odd characters.
847#
848do_test shell1-4.1.4 {
849  db close
850  forcedelete test2.db
851  sqlite3 db test2.db
852  db eval {
853    CREATE TABLE [ta<>ble](x INTEGER PRIMARY KEY, y) WITHOUT ROWID;
854    INSERT INTO [ta<>ble] VALUES(1,null), (12,''), (23,1),
855                         (34,2.25), (45,'hello'), (56,x'807f');
856  }
857  catchcmd test2.db {.dump --preserve-rowids}
858} {0 {PRAGMA foreign_keys=OFF;
859BEGIN TRANSACTION;
860CREATE TABLE [ta<>ble](x INTEGER PRIMARY KEY, y) WITHOUT ROWID;
861INSERT INTO "ta<>ble" VALUES(1,NULL);
862INSERT INTO "ta<>ble" VALUES(12,'');
863INSERT INTO "ta<>ble" VALUES(23,1);
864INSERT INTO "ta<>ble" VALUES(34,2.25);
865INSERT INTO "ta<>ble" VALUES(45,'hello');
866INSERT INTO "ta<>ble" VALUES(56,X'807f');
867COMMIT;}}
868
869# Do not record rowids if the rowid is inaccessible
870#
871do_test shell1-4.1.5 {
872  db close
873  forcedelete test2.db
874  sqlite3 db test2.db
875  db eval {
876    CREATE TABLE t1(_ROWID_,rowid,oid);
877    INSERT INTO t1 VALUES(1,null,'alpha'), (12,'',99), (23,1,x'b0b1b2');
878  }
879  catchcmd test2.db {.dump --preserve-rowids}
880} {0 {PRAGMA foreign_keys=OFF;
881BEGIN TRANSACTION;
882CREATE TABLE t1(_ROWID_,rowid,oid);
883INSERT INTO t1 VALUES(1,NULL,'alpha');
884INSERT INTO t1 VALUES(12,'',99);
885INSERT INTO t1 VALUES(23,1,X'b0b1b2');
886COMMIT;}}
887
888} else {
889
890do_test shell1-4.1.6 {
891  db close
892  forcedelete test2.db
893  sqlite3 db test2.db
894  db eval {
895    CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
896    INSERT INTO t1 VALUES(1,null), (2,''), (3,1),
897                         (4,2.25), (5,'hello'), (6,x'807f');
898  }
899  catchcmd test2.db {.dump --preserve-rowids}
900} {1 {The --preserve-rowids option is not compatible with SQLITE_OMIT_VIRTUALTABLE}}
901
902}
903
904
905# Test the output of ".mode insert"
906#
907do_test shell1-4.2.1 {
908  catchcmd test.db ".mode insert t1\nselect * from t1;"
909} {0 {INSERT INTO t1 VALUES(NULL);
910INSERT INTO t1 VALUES('');
911INSERT INTO t1 VALUES(1);
912INSERT INTO t1 VALUES(2.25);
913INSERT INTO t1 VALUES('hello');
914INSERT INTO t1 VALUES(X'807f');}}
915
916# Test the output of ".mode insert" with headers
917#
918do_test shell1-4.2.2 {
919  catchcmd test.db ".mode insert t1\n.headers on\nselect * from t1;"
920} {0 {INSERT INTO t1(x) VALUES(NULL);
921INSERT INTO t1(x) VALUES('');
922INSERT INTO t1(x) VALUES(1);
923INSERT INTO t1(x) VALUES(2.25);
924INSERT INTO t1(x) VALUES('hello');
925INSERT INTO t1(x) VALUES(X'807f');}}
926
927# Test the output of ".mode insert"
928#
929do_test shell1-4.2.3 {
930  catchcmd test.db ".mode insert t3\nselect * from t3;"
931} {0 {INSERT INTO t3 VALUES(1,NULL);
932INSERT INTO t3 VALUES(2,'');
933INSERT INTO t3 VALUES(3,1);
934INSERT INTO t3 VALUES(4,2.25);
935INSERT INTO t3 VALUES(5,'hello');
936INSERT INTO t3 VALUES(6,X'807f');}}
937
938# Test the output of ".mode insert" with headers
939#
940do_test shell1-4.2.4 {
941  catchcmd test.db ".mode insert t3\n.headers on\nselect * from t3;"
942} {0 {INSERT INTO t3(x,y) VALUES(1,NULL);
943INSERT INTO t3(x,y) VALUES(2,'');
944INSERT INTO t3(x,y) VALUES(3,1);
945INSERT INTO t3(x,y) VALUES(4,2.25);
946INSERT INTO t3(x,y) VALUES(5,'hello');
947INSERT INTO t3(x,y) VALUES(6,X'807f');}}
948
949# Test the output of ".mode tcl"
950#
951do_test shell1-4.3 {
952  db close
953  forcedelete test.db
954  sqlite3 db test.db
955  db eval {
956    PRAGMA encoding=UTF8;
957    CREATE TABLE t1(x);
958    INSERT INTO t1 VALUES(null), (''), (1), (2.25), ('hello'), (x'807f');
959  }
960  catchcmd test.db ".mode tcl\nselect * from t1;"
961} {0 {""
962""
963"1"
964"2.25"
965"hello"
966"\200\177"}}
967
968# Test the output of ".mode tcl" with multiple columns
969#
970do_test shell1-4.4 {
971  db eval {
972    CREATE TABLE t2(x,y);
973    INSERT INTO t2 VALUES(null, ''), (1, 2.25), ('hello', x'807f');
974  }
975  catchcmd test.db ".mode tcl\nselect * from t2;"
976} {0 {"" ""
977"1" "2.25"
978"hello" "\200\177"}}
979
980# Test the output of ".mode tcl" with ".nullvalue"
981#
982do_test shell1-4.5 {
983  catchcmd test.db ".mode tcl\n.nullvalue NULL\nselect * from t2;"
984} {0 {"NULL" ""
985"1" "2.25"
986"hello" "\200\177"}}
987
988# Test the output of ".mode tcl" with Tcl reserved characters
989#
990do_test shell1-4.6 {
991  db eval {
992    CREATE TABLE tcl1(x);
993    INSERT INTO tcl1 VALUES('"'), ('['), (']'), ('\{'), ('\}'), (';'), ('$');
994  }
995  foreach {x y} [catchcmd test.db ".mode tcl\nselect * from tcl1;"] break
996  list $x $y [llength $y]
997} {0 {"\""
998"["
999"]"
1000"\\{"
1001"\\}"
1002";"
1003"$"} 7}
1004
1005# Test using arbitrary byte data with the shell via standard input/output.
1006#
1007do_test shell1-5.0 {
1008  #
1009  # NOTE: Skip NUL byte because it appears to be incompatible with command
1010  #       shell argument parsing.
1011  #
1012  for {set i 1} {$i < 256} {incr i} {
1013    #
1014    # NOTE: Due to how the Tcl [exec] command works (i.e. where it treats
1015    #       command channels opened for it as textual ones), the carriage
1016    #       return character (and on Windows, the end-of-file character)
1017    #       cannot be used here.
1018    #
1019    if {$i==0x0D || ($tcl_platform(platform)=="windows" && $i==0x1A)} {
1020      continue
1021    }
1022    # Tcl 8.7 maps 0x80 through 0x9f into valid UTF8.  So skip those tests.
1023    if {$i>=0x80 && $i<=0x9f} continue
1024    if {$i>=0xE0 && $tcl_platform(os)=="OpenBSD"}  continue
1025    if {$i>=0xE0 && $i<=0xEF && $tcl_platform(os)=="Linux"}  continue
1026    set hex [format %02X $i]
1027    set char [subst \\x$hex]; set oldChar $char
1028    set escapes [list]
1029    if {$tcl_platform(platform)=="windows"} {
1030      #
1031      # NOTE: On Windows, we need to escape all the whitespace characters,
1032      #       the alarm (\a) character, and those with special meaning to
1033      #       the SQLite shell itself.
1034      #
1035      set escapes [list \
1036          \a \\a \b \\b \t \\t \n \\n \v \\v \f \\f \r \\r \
1037          " " "\" \"" \" \\\" ' \"'\" \\ \\\\]
1038    } else {
1039      #
1040      # NOTE: On Unix, we need to escape most of the whitespace characters
1041      #       and those with special meaning to the SQLite shell itself.
1042      #       The alarm (\a), backspace (\b), and carriage-return (\r)
1043      #       characters do not appear to require escaping on Unix.  For
1044      #       the alarm and backspace characters, this is probably due to
1045      #       differences in the command shell.  For the carriage-return,
1046      #       it is probably due to differences in how Tcl handles command
1047      #       channel end-of-line translations.
1048      #
1049      set escapes [list \
1050          \t \\t \n \\n \v \\v \f \\f \
1051          " " "\" \"" \" \\\" ' \"'\" \\ \\\\]
1052    }
1053    set char [string map $escapes $char]
1054    set x [catchcmdex test.db ".print $char\n"]
1055    set code [lindex $x 0]
1056    set res [lindex $x 1]
1057    if {$code ne "0"} {
1058      error "failed with error: $res"
1059    }
1060    if {$res ne "$oldChar\n"} {
1061      if {[llength $res] > 0} {
1062        set got [format %02X [scan $res %c]]
1063      } else {
1064        set got <empty>
1065      }
1066      error "failed with byte $hex mismatch, got $got"
1067    }
1068  }
1069} {}
1070
1071# These test cases do not work on MinGW
1072if 0 {
1073
1074# The string used here is the word "test" in Chinese.
1075# In UTF-8, it is encoded as: \xE6\xB5\x8B\xE8\xAF\x95
1076set test \u6D4B\u8BD5
1077
1078do_test shell1-6.0 {
1079  set fileName $test; append fileName .db
1080  catch {forcedelete $fileName}
1081  set x [catchcmdex $fileName "CREATE TABLE t1(x);\n.schema\n"]
1082  set code [lindex $x 0]
1083  set res [string trim [lindex $x 1]]
1084  if {$code ne "0"} {
1085    error "failed with error: $res"
1086  }
1087  if {$res ne "CREATE TABLE t1(x);"} {
1088    error "failed with mismatch: $res"
1089  }
1090  if {![file exists $fileName]} {
1091    error "file \"$fileName\" (Unicode) does not exist"
1092  }
1093  forcedelete $fileName
1094} {}
1095
1096do_test shell1-6.1 {
1097  catch {forcedelete test3.db}
1098  set x [catchcmdex test3.db \
1099      "CREATE TABLE [encoding convertto utf-8 $test](x);\n.schema\n"]
1100  set code [lindex $x 0]
1101  set res [string trim [lindex $x 1]]
1102  if {$code ne "0"} {
1103    error "failed with error: $res"
1104  }
1105  if {$res ne "CREATE TABLE ${test}(x);"} {
1106    error "failed with mismatch: $res"
1107  }
1108  forcedelete test3.db
1109} {}
1110}
1111
1112db close
1113forcedelete test.db test.db-journal test.db-wal
1114sqlite3 db test.db
1115
1116# The shell tool ".schema" command uses virtual table "pragma_database_list"
1117#
1118ifcapable vtab {
1119
1120do_test shell1-7.1.1 {
1121  db eval {
1122    CREATE TABLE Z (x TEXT PRIMARY KEY);
1123    CREATE TABLE _ (x TEXT PRIMARY KEY);
1124    CREATE TABLE YY (x TEXT PRIMARY KEY);
1125    CREATE TABLE __ (x TEXT PRIMARY KEY);
1126    CREATE TABLE WWW (x TEXT PRIMARY KEY);
1127    CREATE TABLE ___ (x TEXT PRIMARY KEY);
1128  }
1129} {}
1130do_test shell1-7.1.2 {
1131  catchcmd "test.db" ".schema _"
1132} {0 {CREATE TABLE Z (x TEXT PRIMARY KEY);
1133CREATE TABLE _ (x TEXT PRIMARY KEY);}}
1134do_test shell1-7.1.3 {
1135  catchcmd "test.db" ".schema \\\\_"
1136} {0 {CREATE TABLE _ (x TEXT PRIMARY KEY);}}
1137do_test shell1-7.1.4 {
1138  catchcmd "test.db" ".schema __"
1139} {0 {CREATE TABLE YY (x TEXT PRIMARY KEY);
1140CREATE TABLE __ (x TEXT PRIMARY KEY);}}
1141do_test shell1-7.1.5 {
1142  catchcmd "test.db" ".schema \\\\_\\\\_"
1143} {0 {CREATE TABLE __ (x TEXT PRIMARY KEY);}}
1144do_test shell1-7.1.6 {
1145  catchcmd "test.db" ".schema ___"
1146} {0 {CREATE TABLE WWW (x TEXT PRIMARY KEY);
1147CREATE TABLE ___ (x TEXT PRIMARY KEY);}}
1148do_test shell1-7.1.7 {
1149  catchcmd "test.db" ".schema \\\\_\\\\_\\\\_"
1150} {0 {CREATE TABLE ___ (x TEXT PRIMARY KEY);}}
1151
1152}
1153
1154finish_test
1155