xref: /sqlite-3.40.0/test/shell1.test (revision 8e9297fb)
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}
300# The .dump command now accepts multiple arguments
301#do_test shell1-3.4.3 {
302#  # too many arguments
303#  catchcmd "test.db" ".dump FOO BAD"
304#} {1 {Usage: .dump ?--preserve-rowids? ?--newlines? ?LIKE-PATTERN?}}
305
306# .echo ON|OFF           Turn command echo on or off
307do_test shell1-3.5.1 {
308  catchcmd "test.db" ".echo"
309} {1 {Usage: .echo on|off}}
310do_test shell1-3.5.2 {
311  catchcmd "test.db" ".echo ON"
312} {0 {}}
313do_test shell1-3.5.3 {
314  catchcmd "test.db" ".echo OFF"
315} {0 {}}
316do_test shell1-3.5.4 {
317  # too many arguments
318  catchcmd "test.db" ".echo OFF BAD"
319} {1 {Usage: .echo on|off}}
320
321# .exit                  Exit this program
322do_test shell1-3.6.1 {
323  catchcmd "test.db" ".exit"
324} {0 {}}
325
326# .explain ON|OFF        Turn output mode suitable for EXPLAIN on or off.
327do_test shell1-3.7.1 {
328  catchcmd "test.db" ".explain"
329  # explain is the exception to the booleans.  without an option, it turns it on.
330} {0 {}}
331do_test shell1-3.7.2 {
332  catchcmd "test.db" ".explain ON"
333} {0 {}}
334do_test shell1-3.7.3 {
335  catchcmd "test.db" ".explain OFF"
336} {0 {}}
337do_test shell1-3.7.4 {
338  # extra arguments ignored
339  catchcmd "test.db" ".explain OFF BAD"
340} {0 {}}
341
342
343# .header(s) ON|OFF      Turn display of headers on or off
344do_test shell1-3.9.1 {
345  catchcmd "test.db" ".header"
346} {1 {Usage: .headers on|off}}
347do_test shell1-3.9.2 {
348  catchcmd "test.db" ".header ON"
349} {0 {}}
350do_test shell1-3.9.3 {
351  catchcmd "test.db" ".header OFF"
352} {0 {}}
353do_test shell1-3.9.4 {
354  # too many arguments
355  catchcmd "test.db" ".header OFF BAD"
356} {1 {Usage: .headers on|off}}
357
358do_test shell1-3.9.5 {
359  catchcmd "test.db" ".headers"
360} {1 {Usage: .headers on|off}}
361do_test shell1-3.9.6 {
362  catchcmd "test.db" ".headers ON"
363} {0 {}}
364do_test shell1-3.9.7 {
365  catchcmd "test.db" ".headers OFF"
366} {0 {}}
367do_test shell1-3.9.8 {
368  # too many arguments
369  catchcmd "test.db" ".headers OFF BAD"
370} {1 {Usage: .headers on|off}}
371
372# .help                  Show this message
373do_test shell1-3.10.1 {
374  set res [catchcmd "test.db" ".help"]
375  # look for a few of the possible help commands
376  list [regexp {.help} $res] \
377       [regexp {.quit} $res] \
378       [regexp {.show} $res]
379} {1 1 1}
380do_test shell1-3.10.2 {
381  # we allow .help to take extra args (it is help after all)
382  set res [catchcmd "test.db" ".help *"]
383  # look for a few of the possible help commands
384  list [regexp {.help} $res] \
385       [regexp {.quit} $res] \
386       [regexp {.show} $res]
387} {1 1 1}
388
389# .import FILE TABLE     Import data from FILE into TABLE
390do_test shell1-3.11.1 {
391  catchcmd "test.db" ".import"
392} {/1 .ERROR: missing FILE argument.*/}
393do_test shell1-3.11.2 {
394  catchcmd "test.db" ".import FOO"
395} {/1 .ERROR: missing TABLE argument.*/}
396do_test shell1-3.11.3 {
397  # too many arguments
398  catchcmd "test.db" ".import FOO BAR BAD"
399} {/1 .ERROR: extra argument: "BAD".*./}
400
401# .indexes ?TABLE?       Show names of all indexes
402#                          If TABLE specified, only show indexes for tables
403#                          matching LIKE pattern TABLE.
404do_test shell1-3.12.1 {
405  catchcmd "test.db" ".indexes"
406} {0 {}}
407do_test shell1-3.12.2 {
408  catchcmd "test.db" ".indexes FOO"
409} {0 {}}
410do_test shell1-3.12.2-legacy {
411  catchcmd "test.db" ".indices FOO"
412} {0 {}}
413do_test shell1-3.12.3 {
414  # too many arguments
415  catchcmd "test.db" ".indexes FOO BAD"
416} {1 {Usage: .indexes ?LIKE-PATTERN?}}
417
418# .mode MODE ?TABLE?     Set output mode where MODE is one of:
419#                          ascii    Columns/rows delimited by 0x1F and 0x1E
420#                          csv      Comma-separated values
421#                          column   Left-aligned columns.  (See .width)
422#                          html     HTML <table> code
423#                          insert   SQL insert statements for TABLE
424#                          line     One value per line
425#                          list     Values delimited by .separator strings
426#                          tabs     Tab-separated values
427#                          tcl      TCL list elements
428do_test shell1-3.13.1 {
429  catchcmd "test.db" ".mode"
430} {0 {current output mode: list}}
431do_test shell1-3.13.2 {
432  catchcmd "test.db" ".mode FOO"
433} {1 {Error: mode should be one of: ascii column csv html insert line list quote tabs tcl}}
434do_test shell1-3.13.3 {
435  catchcmd "test.db" ".mode csv"
436} {0 {}}
437do_test shell1-3.13.4 {
438  catchcmd "test.db" ".mode column"
439} {0 {}}
440do_test shell1-3.13.5 {
441  catchcmd "test.db" ".mode html"
442} {0 {}}
443do_test shell1-3.13.6 {
444  catchcmd "test.db" ".mode insert"
445} {0 {}}
446do_test shell1-3.13.7 {
447  catchcmd "test.db" ".mode line"
448} {0 {}}
449do_test shell1-3.13.8 {
450  catchcmd "test.db" ".mode list"
451} {0 {}}
452do_test shell1-3.13.9 {
453  catchcmd "test.db" ".mode tabs"
454} {0 {}}
455do_test shell1-3.13.10 {
456  catchcmd "test.db" ".mode tcl"
457} {0 {}}
458do_test shell1-3.13.11 {
459  # extra arguments ignored
460  catchcmd "test.db" ".mode tcl BAD"
461} {0 {}}
462
463# don't allow partial mode type matches
464do_test shell1-3.13.12 {
465  catchcmd "test.db" ".mode l"
466} {1 {Error: mode should be one of: ascii column csv html insert line list quote tabs tcl}}
467do_test shell1-3.13.13 {
468  catchcmd "test.db" ".mode li"
469} {1 {Error: mode should be one of: ascii column csv html insert line list quote tabs tcl}}
470do_test shell1-3.13.14 {
471  catchcmd "test.db" ".mode lin"
472} {0 {}}
473
474# .nullvalue STRING      Print STRING in place of NULL values
475do_test shell1-3.14.1 {
476  catchcmd "test.db" ".nullvalue"
477} {1 {Usage: .nullvalue STRING}}
478do_test shell1-3.14.2 {
479  catchcmd "test.db" ".nullvalue FOO"
480} {0 {}}
481do_test shell1-3.14.3 {
482  # too many arguments
483  catchcmd "test.db" ".nullvalue FOO BAD"
484} {1 {Usage: .nullvalue STRING}}
485
486# .output FILENAME       Send output to FILENAME
487do_test shell1-3.15.1 {
488  catchcmd "test.db" ".output"
489} {0 {}}
490do_test shell1-3.15.2 {
491  catchcmd "test.db" ".output FOO"
492} {0 {}}
493do_test shell1-3.15.3 {
494  # too many arguments
495  catchcmd "test.db" ".output FOO BAD"
496} {1 {Usage: .output [-e|-x|FILE]}}
497
498# .output stdout         Send output to the screen
499do_test shell1-3.16.1 {
500  catchcmd "test.db" ".output stdout"
501} {0 {}}
502do_test shell1-3.16.2 {
503  # too many arguments
504  catchcmd "test.db" ".output stdout BAD"
505} {1 {Usage: .output [-e|-x|FILE]}}
506
507# .prompt MAIN CONTINUE  Replace the standard prompts
508do_test shell1-3.17.1 {
509  catchcmd "test.db" ".prompt"
510} {0 {}}
511do_test shell1-3.17.2 {
512  catchcmd "test.db" ".prompt FOO"
513} {0 {}}
514do_test shell1-3.17.3 {
515  catchcmd "test.db" ".prompt FOO BAR"
516} {0 {}}
517do_test shell1-3.17.4 {
518  # too many arguments
519  catchcmd "test.db" ".prompt FOO BAR BAD"
520} {0 {}}
521
522# .quit                  Exit this program
523do_test shell1-3.18.1 {
524  catchcmd "test.db" ".quit"
525} {0 {}}
526do_test shell1-3.18.2 {
527  # too many arguments
528  catchcmd "test.db" ".quit BAD"
529} {0 {}}
530
531# .read FILENAME         Execute SQL in FILENAME
532do_test shell1-3.19.1 {
533  catchcmd "test.db" ".read"
534} {1 {Usage: .read FILE}}
535do_test shell1-3.19.2 {
536  forcedelete FOO
537  catchcmd "test.db" ".read FOO"
538} {1 {Error: cannot open "FOO"}}
539do_test shell1-3.19.3 {
540  # too many arguments
541  catchcmd "test.db" ".read FOO BAD"
542} {1 {Usage: .read FILE}}
543
544# .restore ?DB? FILE     Restore content of DB (default "main") from FILE
545do_test shell1-3.20.1 {
546  catchcmd "test.db" ".restore"
547} {1 {Usage: .restore ?DB? FILE}}
548do_test shell1-3.20.2 {
549  catchcmd "test.db" ".restore FOO"
550} {0 {}}
551do_test shell1-3.20.3 {
552  catchcmd "test.db" ".restore FOO BAR"
553} {1 {Error: unknown database FOO}}
554do_test shell1-3.20.4 {
555  # too many arguments
556  catchcmd "test.db" ".restore FOO BAR BAD"
557} {1 {Usage: .restore ?DB? FILE}}
558
559ifcapable vtab {
560# .schema ?TABLE?        Show the CREATE statements
561#                          If TABLE specified, only show tables matching
562#                          LIKE pattern TABLE.
563do_test shell1-3.21.1 {
564  catchcmd "test.db" ".schema"
565} {0 {}}
566do_test shell1-3.21.2 {
567  catchcmd "test.db" ".schema FOO"
568} {0 {}}
569do_test shell1-3.21.3 {
570  # too many arguments
571  catchcmd "test.db" ".schema FOO BAD"
572} {1 {Usage: .schema ?--indent? ?LIKE-PATTERN?}}
573
574do_test shell1-3.21.4 {
575  catchcmd "test.db" {
576     CREATE TABLE t1(x);
577     CREATE VIEW v2 AS SELECT x+1 AS y FROM t1;
578     CREATE VIEW v1 AS SELECT y+1 FROM v2;
579  }
580  catchcmd "test.db" ".schema"
581} {0 {CREATE TABLE t1(x);
582CREATE VIEW v2 AS SELECT x+1 AS y FROM t1
583/* v2(y) */;
584CREATE VIEW v1 AS SELECT y+1 FROM v2
585/* v1("y+1") */;}}
586db eval {DROP VIEW v1; DROP VIEW v2; DROP TABLE t1;}
587}
588
589# .separator STRING  Change column separator used by output and .import
590do_test shell1-3.22.1 {
591  catchcmd "test.db" ".separator"
592} {1 {Usage: .separator COL ?ROW?}}
593do_test shell1-3.22.2 {
594  catchcmd "test.db" ".separator FOO"
595} {0 {}}
596do_test shell1-3.22.3 {
597  catchcmd "test.db" ".separator ABC XYZ"
598} {0 {}}
599do_test shell1-3.22.4 {
600  # too many arguments
601  catchcmd "test.db" ".separator FOO BAD BAD2"
602} {1 {Usage: .separator COL ?ROW?}}
603
604# .show                  Show the current values for various settings
605do_test shell1-3.23.1 {
606  set res [catchcmd "test.db" ".show"]
607  list [regexp {echo:} $res] \
608       [regexp {explain:} $res] \
609       [regexp {headers:} $res] \
610       [regexp {mode:} $res] \
611       [regexp {nullvalue:} $res] \
612       [regexp {output:} $res] \
613       [regexp {colseparator:} $res] \
614       [regexp {rowseparator:} $res] \
615       [regexp {stats:} $res] \
616       [regexp {width:} $res]
617} {1 1 1 1 1 1 1 1 1 1}
618do_test shell1-3.23.2 {
619  # too many arguments
620  catchcmd "test.db" ".show BAD"
621} {1 {Usage: .show}}
622
623# .stats ON|OFF          Turn stats on or off
624#do_test shell1-3.23b.1 {
625#  catchcmd "test.db" ".stats"
626#} {1 {Usage: .stats on|off}}
627do_test shell1-3.23b.2 {
628  catchcmd "test.db" ".stats ON"
629} {0 {}}
630do_test shell1-3.23b.3 {
631  catchcmd "test.db" ".stats OFF"
632} {0 {}}
633do_test shell1-3.23b.4 {
634  # too many arguments
635  catchcmd "test.db" ".stats OFF BAD"
636} {1 {Usage: .stats ?on|off?}}
637
638# Ticket 7be932dfa60a8a6b3b26bcf7623ec46e0a403ddb 2018-06-07
639# Adverse interaction between .stats and .eqp
640#
641do_test shell1-3.23b.5 {
642  catchcmd "test.db" [string map {"\n    " "\n"} {
643    CREATE TEMP TABLE t1(x);
644    INSERT INTO t1 VALUES(1),(2);
645    .stats on
646    .eqp full
647    SELECT * FROM t1;
648  }]
649} {/1\n2\n/}
650
651# .tables ?TABLE?        List names of tables
652#                          If TABLE specified, only list tables matching
653#                          LIKE pattern TABLE.
654do_test shell1-3.24.1 {
655  catchcmd "test.db" ".tables"
656} {0 {}}
657do_test shell1-3.24.2 {
658  catchcmd "test.db" ".tables FOO"
659} {0 {}}
660do_test shell1-3.24.3 {
661  # too many arguments
662  catchcmd "test.db" ".tables FOO BAD"
663} {0 {}}
664
665# .timeout MS            Try opening locked tables for MS milliseconds
666do_test shell1-3.25.1 {
667  catchcmd "test.db" ".timeout"
668} {0 {}}
669do_test shell1-3.25.2 {
670  catchcmd "test.db" ".timeout zzz"
671  # this should be treated the same as a '0' timeout
672} {0 {}}
673do_test shell1-3.25.3 {
674  catchcmd "test.db" ".timeout 1"
675} {0 {}}
676do_test shell1-3.25.4 {
677  # too many arguments
678  catchcmd "test.db" ".timeout 1 BAD"
679} {0 {}}
680
681# .width NUM NUM ...     Set column widths for "column" mode
682do_test shell1-3.26.1 {
683  catchcmd "test.db" ".width"
684} {0 {}}
685do_test shell1-3.26.2 {
686  catchcmd "test.db" ".width xxx"
687  # this should be treated the same as a '0' width for col 1
688} {0 {}}
689do_test shell1-3.26.3 {
690  catchcmd "test.db" ".width xxx yyy"
691  # this should be treated the same as a '0' width for col 1 and 2
692} {0 {}}
693do_test shell1-3.26.4 {
694  catchcmd "test.db" ".width 1 1"
695  # this should be treated the same as a '1' width for col 1 and 2
696} {0 {}}
697do_test shell1-3.26.5 {
698  catchcmd "test.db" ".mode column\n.width 10 -10\nSELECT 'abcdefg', 123456;"
699  # this should be treated the same as a '1' width for col 1 and 2
700} {0 {abcdefg         123456}}
701do_test shell1-3.26.6 {
702  catchcmd "test.db" ".mode column\n.width -10 10\nSELECT 'abcdefg', 123456;"
703  # this should be treated the same as a '1' width for col 1 and 2
704} {0 {   abcdefg  123456    }}
705
706
707# .timer ON|OFF          Turn the CPU timer measurement on or off
708do_test shell1-3.27.1 {
709  catchcmd "test.db" ".timer"
710} {1 {Usage: .timer on|off}}
711do_test shell1-3.27.2 {
712  catchcmd "test.db" ".timer ON"
713} {0 {}}
714do_test shell1-3.27.3 {
715  catchcmd "test.db" ".timer OFF"
716} {0 {}}
717do_test shell1-3.27.4 {
718  # too many arguments
719  catchcmd "test.db" ".timer OFF BAD"
720} {1 {Usage: .timer on|off}}
721
722do_test shell1-3-28.1 {
723  catchcmd test.db \
724     ".log stdout\nSELECT coalesce(sqlite_log(123,'hello'),'456');"
725} "0 {(123) hello\n456}"
726
727do_test shell1-3-29.1 {
728  catchcmd "test.db" ".print this is a test"
729} {0 {this is a test}}
730
731# dot-command argument quoting
732do_test shell1-3-30.1 {
733  catchcmd {test.db} {.print "this\"is'a\055test" 'this\"is\\a\055test'}
734} {0 {this"is'a-test this\"is\\a\055test}}
735do_test shell1-3-31.1 {
736  catchcmd {test.db} {.print "this\nis\ta\\test" 'this\nis\ta\\test'}
737} [list 0 "this\nis\ta\\test this\\nis\\ta\\\\test"]
738
739
740# Test the output of the ".dump" command
741#
742do_test shell1-4.1 {
743  db close
744  forcedelete test.db
745  sqlite3 db test.db
746  db eval {
747    PRAGMA encoding=UTF16;
748    CREATE TABLE t1(x);
749    INSERT INTO t1 VALUES(null), (''), (1), (2.25), ('hello'), (x'807f');
750    CREATE TABLE t3(x,y);
751    INSERT INTO t3 VALUES(1,null), (2,''), (3,1),
752                         (4,2.25), (5,'hello'), (6,x'807f');
753  }
754  catchcmd test.db {.dump}
755} {0 {PRAGMA foreign_keys=OFF;
756BEGIN TRANSACTION;
757CREATE TABLE t1(x);
758INSERT INTO t1 VALUES(NULL);
759INSERT INTO t1 VALUES('');
760INSERT INTO t1 VALUES(1);
761INSERT INTO t1 VALUES(2.25);
762INSERT INTO t1 VALUES('hello');
763INSERT INTO t1 VALUES(X'807f');
764CREATE TABLE t3(x,y);
765INSERT INTO t3 VALUES(1,NULL);
766INSERT INTO t3 VALUES(2,'');
767INSERT INTO t3 VALUES(3,1);
768INSERT INTO t3 VALUES(4,2.25);
769INSERT INTO t3 VALUES(5,'hello');
770INSERT INTO t3 VALUES(6,X'807f');
771COMMIT;}}
772
773
774ifcapable vtab {
775
776# The --preserve-rowids option to .dump
777#
778do_test shell1-4.1.1 {
779  catchcmd test.db {.dump --preserve-rowids}
780} {0 {PRAGMA foreign_keys=OFF;
781BEGIN TRANSACTION;
782CREATE TABLE t1(x);
783INSERT INTO t1(rowid,x) VALUES(1,NULL);
784INSERT INTO t1(rowid,x) VALUES(2,'');
785INSERT INTO t1(rowid,x) VALUES(3,1);
786INSERT INTO t1(rowid,x) VALUES(4,2.25);
787INSERT INTO t1(rowid,x) VALUES(5,'hello');
788INSERT INTO t1(rowid,x) VALUES(6,X'807f');
789CREATE TABLE t3(x,y);
790INSERT INTO t3(rowid,x,y) VALUES(1,1,NULL);
791INSERT INTO t3(rowid,x,y) VALUES(2,2,'');
792INSERT INTO t3(rowid,x,y) VALUES(3,3,1);
793INSERT INTO t3(rowid,x,y) VALUES(4,4,2.25);
794INSERT INTO t3(rowid,x,y) VALUES(5,5,'hello');
795INSERT INTO t3(rowid,x,y) VALUES(6,6,X'807f');
796COMMIT;}}
797
798# If the table contains an INTEGER PRIMARY KEY, do not record a separate
799# rowid column in the output.
800#
801do_test shell1-4.1.2 {
802  db close
803  forcedelete test2.db
804  sqlite3 db test2.db
805  db eval {
806    CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
807    INSERT INTO t1 VALUES(1,null), (2,''), (3,1),
808                         (4,2.25), (5,'hello'), (6,x'807f');
809  }
810  catchcmd test2.db {.dump --preserve-rowids}
811} {0 {PRAGMA foreign_keys=OFF;
812BEGIN TRANSACTION;
813CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
814INSERT INTO t1 VALUES(1,NULL);
815INSERT INTO t1 VALUES(2,'');
816INSERT INTO t1 VALUES(3,1);
817INSERT INTO t1 VALUES(4,2.25);
818INSERT INTO t1 VALUES(5,'hello');
819INSERT INTO t1 VALUES(6,X'807f');
820COMMIT;}}
821
822# Verify that the table named [table] is correctly quoted and that
823# an INTEGER PRIMARY KEY DESC is not an alias for the rowid.
824#
825do_test shell1-4.1.3 {
826  db close
827  forcedelete test2.db
828  sqlite3 db test2.db
829  db eval {
830    CREATE TABLE [table](x INTEGER PRIMARY KEY DESC, y);
831    INSERT INTO [table] VALUES(1,null), (12,''), (23,1),
832                         (34,2.25), (45,'hello'), (56,x'807f');
833  }
834  catchcmd test2.db {.dump --preserve-rowids}
835} {0 {PRAGMA foreign_keys=OFF;
836BEGIN TRANSACTION;
837CREATE TABLE [table](x INTEGER PRIMARY KEY DESC, y);
838INSERT INTO "table"(rowid,x,y) VALUES(1,1,NULL);
839INSERT INTO "table"(rowid,x,y) VALUES(2,12,'');
840INSERT INTO "table"(rowid,x,y) VALUES(3,23,1);
841INSERT INTO "table"(rowid,x,y) VALUES(4,34,2.25);
842INSERT INTO "table"(rowid,x,y) VALUES(5,45,'hello');
843INSERT INTO "table"(rowid,x,y) VALUES(6,56,X'807f');
844COMMIT;}}
845
846# Do not record rowids for a WITHOUT ROWID table.  Also check correct quoting
847# of table names that contain odd characters.
848#
849do_test shell1-4.1.4 {
850  db close
851  forcedelete test2.db
852  sqlite3 db test2.db
853  db eval {
854    CREATE TABLE [ta<>ble](x INTEGER PRIMARY KEY, y) WITHOUT ROWID;
855    INSERT INTO [ta<>ble] VALUES(1,null), (12,''), (23,1),
856                         (34,2.25), (45,'hello'), (56,x'807f');
857  }
858  catchcmd test2.db {.dump --preserve-rowids}
859} {0 {PRAGMA foreign_keys=OFF;
860BEGIN TRANSACTION;
861CREATE TABLE [ta<>ble](x INTEGER PRIMARY KEY, y) WITHOUT ROWID;
862INSERT INTO "ta<>ble" VALUES(1,NULL);
863INSERT INTO "ta<>ble" VALUES(12,'');
864INSERT INTO "ta<>ble" VALUES(23,1);
865INSERT INTO "ta<>ble" VALUES(34,2.25);
866INSERT INTO "ta<>ble" VALUES(45,'hello');
867INSERT INTO "ta<>ble" VALUES(56,X'807f');
868COMMIT;}}
869
870# Do not record rowids if the rowid is inaccessible
871#
872do_test shell1-4.1.5 {
873  db close
874  forcedelete test2.db
875  sqlite3 db test2.db
876  db eval {
877    CREATE TABLE t1(_ROWID_,rowid,oid);
878    INSERT INTO t1 VALUES(1,null,'alpha'), (12,'',99), (23,1,x'b0b1b2');
879  }
880  catchcmd test2.db {.dump --preserve-rowids}
881} {0 {PRAGMA foreign_keys=OFF;
882BEGIN TRANSACTION;
883CREATE TABLE t1(_ROWID_,rowid,oid);
884INSERT INTO t1 VALUES(1,NULL,'alpha');
885INSERT INTO t1 VALUES(12,'',99);
886INSERT INTO t1 VALUES(23,1,X'b0b1b2');
887COMMIT;}}
888
889} else {
890
891do_test shell1-4.1.6 {
892  db close
893  forcedelete test2.db
894  sqlite3 db test2.db
895  db eval {
896    CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
897    INSERT INTO t1 VALUES(1,null), (2,''), (3,1),
898                         (4,2.25), (5,'hello'), (6,x'807f');
899  }
900  catchcmd test2.db {.dump --preserve-rowids}
901} {1 {The --preserve-rowids option is not compatible with SQLITE_OMIT_VIRTUALTABLE}}
902
903}
904
905
906# Test the output of ".mode insert"
907#
908do_test shell1-4.2.1 {
909  catchcmd test.db ".mode insert t1\nselect * from t1;"
910} {0 {INSERT INTO t1 VALUES(NULL);
911INSERT INTO t1 VALUES('');
912INSERT INTO t1 VALUES(1);
913INSERT INTO t1 VALUES(2.25);
914INSERT INTO t1 VALUES('hello');
915INSERT INTO t1 VALUES(X'807f');}}
916
917# Test the output of ".mode insert" with headers
918#
919do_test shell1-4.2.2 {
920  catchcmd test.db ".mode insert t1\n.headers on\nselect * from t1;"
921} {0 {INSERT INTO t1(x) VALUES(NULL);
922INSERT INTO t1(x) VALUES('');
923INSERT INTO t1(x) VALUES(1);
924INSERT INTO t1(x) VALUES(2.25);
925INSERT INTO t1(x) VALUES('hello');
926INSERT INTO t1(x) VALUES(X'807f');}}
927
928# Test the output of ".mode insert"
929#
930do_test shell1-4.2.3 {
931  catchcmd test.db ".mode insert t3\nselect * from t3;"
932} {0 {INSERT INTO t3 VALUES(1,NULL);
933INSERT INTO t3 VALUES(2,'');
934INSERT INTO t3 VALUES(3,1);
935INSERT INTO t3 VALUES(4,2.25);
936INSERT INTO t3 VALUES(5,'hello');
937INSERT INTO t3 VALUES(6,X'807f');}}
938
939# Test the output of ".mode insert" with headers
940#
941do_test shell1-4.2.4 {
942  catchcmd test.db ".mode insert t3\n.headers on\nselect * from t3;"
943} {0 {INSERT INTO t3(x,y) VALUES(1,NULL);
944INSERT INTO t3(x,y) VALUES(2,'');
945INSERT INTO t3(x,y) VALUES(3,1);
946INSERT INTO t3(x,y) VALUES(4,2.25);
947INSERT INTO t3(x,y) VALUES(5,'hello');
948INSERT INTO t3(x,y) VALUES(6,X'807f');}}
949
950# Test the output of ".mode tcl"
951#
952do_test shell1-4.3 {
953  db close
954  forcedelete test.db
955  sqlite3 db test.db
956  db eval {
957    PRAGMA encoding=UTF8;
958    CREATE TABLE t1(x);
959    INSERT INTO t1 VALUES(null), (''), (1), (2.25), ('hello'), (x'807f');
960  }
961  catchcmd test.db ".mode tcl\nselect * from t1;"
962} {0 {""
963""
964"1"
965"2.25"
966"hello"
967"\200\177"}}
968
969# Test the output of ".mode tcl" with multiple columns
970#
971do_test shell1-4.4 {
972  db eval {
973    CREATE TABLE t2(x,y);
974    INSERT INTO t2 VALUES(null, ''), (1, 2.25), ('hello', x'807f');
975  }
976  catchcmd test.db ".mode tcl\nselect * from t2;"
977} {0 {"" ""
978"1" "2.25"
979"hello" "\200\177"}}
980
981# Test the output of ".mode tcl" with ".nullvalue"
982#
983do_test shell1-4.5 {
984  catchcmd test.db ".mode tcl\n.nullvalue NULL\nselect * from t2;"
985} {0 {"NULL" ""
986"1" "2.25"
987"hello" "\200\177"}}
988
989# Test the output of ".mode tcl" with Tcl reserved characters
990#
991do_test shell1-4.6 {
992  db eval {
993    CREATE TABLE tcl1(x);
994    INSERT INTO tcl1 VALUES('"'), ('['), (']'), ('\{'), ('\}'), (';'), ('$');
995  }
996  foreach {x y} [catchcmd test.db ".mode tcl\nselect * from tcl1;"] break
997  list $x $y [llength $y]
998} {0 {"\""
999"["
1000"]"
1001"\\{"
1002"\\}"
1003";"
1004"$"} 7}
1005
1006# Test using arbitrary byte data with the shell via standard input/output.
1007#
1008do_test shell1-5.0 {
1009  #
1010  # NOTE: Skip NUL byte because it appears to be incompatible with command
1011  #       shell argument parsing.
1012  #
1013  for {set i 1} {$i < 256} {incr i} {
1014    #
1015    # NOTE: Due to how the Tcl [exec] command works (i.e. where it treats
1016    #       command channels opened for it as textual ones), the carriage
1017    #       return character (and on Windows, the end-of-file character)
1018    #       cannot be used here.
1019    #
1020    if {$i==0x0D || ($tcl_platform(platform)=="windows" && $i==0x1A)} {
1021      continue
1022    }
1023    # Tcl 8.7 maps 0x80 through 0x9f into valid UTF8.  So skip those tests.
1024    if {$i>=0x80 && $i<=0x9f} continue
1025    if {$i>=0xE0 && $tcl_platform(os)=="OpenBSD"}  continue
1026    if {$i>=0xE0 && $i<=0xEF && $tcl_platform(os)=="Linux"}  continue
1027    set hex [format %02X $i]
1028    set char [subst \\x$hex]; set oldChar $char
1029    set escapes [list]
1030    if {$tcl_platform(platform)=="windows"} {
1031      #
1032      # NOTE: On Windows, we need to escape all the whitespace characters,
1033      #       the alarm (\a) character, and those with special meaning to
1034      #       the SQLite shell itself.
1035      #
1036      set escapes [list \
1037          \a \\a \b \\b \t \\t \n \\n \v \\v \f \\f \r \\r \
1038          " " "\" \"" \" \\\" ' \"'\" \\ \\\\]
1039    } else {
1040      #
1041      # NOTE: On Unix, we need to escape most of the whitespace characters
1042      #       and those with special meaning to the SQLite shell itself.
1043      #       The alarm (\a), backspace (\b), and carriage-return (\r)
1044      #       characters do not appear to require escaping on Unix.  For
1045      #       the alarm and backspace characters, this is probably due to
1046      #       differences in the command shell.  For the carriage-return,
1047      #       it is probably due to differences in how Tcl handles command
1048      #       channel end-of-line translations.
1049      #
1050      set escapes [list \
1051          \t \\t \n \\n \v \\v \f \\f \
1052          " " "\" \"" \" \\\" ' \"'\" \\ \\\\]
1053    }
1054    set char [string map $escapes $char]
1055    set x [catchcmdex test.db ".print $char\n"]
1056    set code [lindex $x 0]
1057    set res [lindex $x 1]
1058    if {$code ne "0"} {
1059      error "failed with error: $res"
1060    }
1061    if {$res ne "$oldChar\n"} {
1062      if {[llength $res] > 0} {
1063        set got [format %02X [scan $res %c]]
1064      } else {
1065        set got <empty>
1066      }
1067      error "failed with byte $hex mismatch, got $got"
1068    }
1069  }
1070} {}
1071
1072# These test cases do not work on MinGW
1073if 0 {
1074
1075# The string used here is the word "test" in Chinese.
1076# In UTF-8, it is encoded as: \xE6\xB5\x8B\xE8\xAF\x95
1077set test \u6D4B\u8BD5
1078
1079do_test shell1-6.0 {
1080  set fileName $test; append fileName .db
1081  catch {forcedelete $fileName}
1082  set x [catchcmdex $fileName "CREATE TABLE t1(x);\n.schema\n"]
1083  set code [lindex $x 0]
1084  set res [string trim [lindex $x 1]]
1085  if {$code ne "0"} {
1086    error "failed with error: $res"
1087  }
1088  if {$res ne "CREATE TABLE t1(x);"} {
1089    error "failed with mismatch: $res"
1090  }
1091  if {![file exists $fileName]} {
1092    error "file \"$fileName\" (Unicode) does not exist"
1093  }
1094  forcedelete $fileName
1095} {}
1096
1097do_test shell1-6.1 {
1098  catch {forcedelete test3.db}
1099  set x [catchcmdex test3.db \
1100      "CREATE TABLE [encoding convertto utf-8 $test](x);\n.schema\n"]
1101  set code [lindex $x 0]
1102  set res [string trim [lindex $x 1]]
1103  if {$code ne "0"} {
1104    error "failed with error: $res"
1105  }
1106  if {$res ne "CREATE TABLE ${test}(x);"} {
1107    error "failed with mismatch: $res"
1108  }
1109  forcedelete test3.db
1110} {}
1111}
1112
1113db close
1114forcedelete test.db test.db-journal test.db-wal
1115sqlite3 db test.db
1116
1117# The shell tool ".schema" command uses virtual table "pragma_database_list"
1118#
1119ifcapable vtab {
1120
1121do_test shell1-7.1.1 {
1122  db eval {
1123    CREATE TABLE Z (x TEXT PRIMARY KEY);
1124    CREATE TABLE _ (x TEXT PRIMARY KEY);
1125    CREATE TABLE YY (x TEXT PRIMARY KEY);
1126    CREATE TABLE __ (x TEXT PRIMARY KEY);
1127    CREATE TABLE WWW (x TEXT PRIMARY KEY);
1128    CREATE TABLE ___ (x TEXT PRIMARY KEY);
1129  }
1130} {}
1131do_test shell1-7.1.2 {
1132  catchcmd "test.db" ".schema _"
1133} {0 {CREATE TABLE Z (x TEXT PRIMARY KEY);
1134CREATE TABLE _ (x TEXT PRIMARY KEY);}}
1135do_test shell1-7.1.3 {
1136  catchcmd "test.db" ".schema \\\\_"
1137} {0 {CREATE TABLE _ (x TEXT PRIMARY KEY);}}
1138do_test shell1-7.1.4 {
1139  catchcmd "test.db" ".schema __"
1140} {0 {CREATE TABLE YY (x TEXT PRIMARY KEY);
1141CREATE TABLE __ (x TEXT PRIMARY KEY);}}
1142do_test shell1-7.1.5 {
1143  catchcmd "test.db" ".schema \\\\_\\\\_"
1144} {0 {CREATE TABLE __ (x TEXT PRIMARY KEY);}}
1145do_test shell1-7.1.6 {
1146  catchcmd "test.db" ".schema ___"
1147} {0 {CREATE TABLE WWW (x TEXT PRIMARY KEY);
1148CREATE TABLE ___ (x TEXT PRIMARY KEY);}}
1149do_test shell1-7.1.7 {
1150  catchcmd "test.db" ".schema \\\\_\\\\_\\\\_"
1151} {0 {CREATE TABLE ___ (x TEXT PRIMARY KEY);}}
1152
1153}
1154
1155finish_test
1156