3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
12 # The focus of this file is testing the CLI shell tool.
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.
22 set testdir [file dirname $argv0]
23 source $testdir/tester.tcl
24 set CLI [test_find_cli]
26 forcedelete test.db test.db-journal test.db-wal
29 #----------------------------------------------------------------------------
30 # Test cases shell1-1.*: Basic command line option handling.
34 do_test shell1-1.1.1 {
35 set res [catchcmd "-bad test.db" ""]
36 set rc [lindex $res 0]
38 [regexp {Error: unknown option: -bad} $res]
40 do_test shell1-1.1.1b {
41 set res [catchcmd "test.db -bad" ""]
42 set rc [lindex $res 0]
44 [regexp {Error: unknown option: -bad} $res]
46 # error on extra options
47 do_test shell1-1.1.2 {
48 catchcmd "test.db \"select+3\" \"select+4\"" ""
51 # error on extra options
52 do_test shell1-1.1.3 {
53 catchcmd "test.db FOO test.db BAD" ".quit"
54 } {1 {Error: near "FOO": syntax error}}
57 do_test shell1-1.2.1 {
58 set res [catchcmd "-help test.db" ""]
59 set rc [lindex $res 0]
61 [regexp {Usage} $res] \
62 [regexp {\-init} $res] \
63 [regexp {\-version} $res]
66 # -init filename read/process named file
67 do_test shell1-1.3.1 {
68 catchcmd "-init FOO test.db" ""
70 do_test shell1-1.3.2 {
71 catchcmd "-init FOO test.db .quit BAD" ""
73 do_test shell1-1.3.3 {
74 catchcmd "-init FOO test.db BAD .quit" ""
75 } {1 {Error: near "BAD": syntax error}}
77 # -echo print commands before execution
78 do_test shell1-1.4.1 {
79 catchcmd "-echo test.db" ""
82 # -[no]header turn headers on or off
83 do_test shell1-1.5.1 {
84 catchcmd "-header test.db" ""
86 do_test shell1-1.5.2 {
87 catchcmd "-noheader test.db" ""
90 # -bail stop after hitting an error
91 do_test shell1-1.6.1 {
92 catchcmd "-bail test.db" ""
95 # -interactive force interactive I/O
96 do_test shell1-1.7.1 {
97 set res [catchcmd "-interactive test.db" ".quit"]
98 set rc [lindex $res 0]
100 [regexp {SQLite version} $res] \
101 [regexp {Enter ".help" for usage hints} $res]
104 # -batch force batch I/O
105 do_test shell1-1.8.1 {
106 catchcmd "-batch test.db" ""
109 # -column set output mode to 'column'
110 do_test shell1-1.9.1 {
111 catchcmd "-column test.db" ""
114 # -csv set output mode to 'csv'
115 do_test shell1-1.10.1 {
116 catchcmd "-csv test.db" ""
119 # -html set output mode to HTML
120 do_test shell1-1.11.1 {
121 catchcmd "-html test.db" ""
124 # -line set output mode to 'line'
125 do_test shell1-1.12.1 {
126 catchcmd "-line test.db" ""
129 # -list set output mode to 'list'
130 do_test shell1-1.13.1 {
131 catchcmd "-list test.db" ""
134 # -separator 'x' set output field separator (|)
135 do_test shell1-1.14.1 {
136 catchcmd "-separator 'x' test.db" ""
138 do_test shell1-1.14.2 {
139 catchcmd "-separator x test.db" ""
141 do_test shell1-1.14.3 {
142 set res [catchcmd "-separator" ""]
143 set rc [lindex $res 0]
145 [regexp {Error: missing argument to -separator} $res]
148 # -stats print memory stats before each finalize
149 do_test shell1-1.14b.1 {
150 catchcmd "-stats test.db" ""
153 # -nullvalue 'text' set text string for NULL values
154 do_test shell1-1.15.1 {
155 catchcmd "-nullvalue 'x' test.db" ""
157 do_test shell1-1.15.2 {
158 catchcmd "-nullvalue x test.db" ""
160 do_test shell1-1.15.3 {
161 set res [catchcmd "-nullvalue" ""]
162 set rc [lindex $res 0]
164 [regexp {Error: missing argument to -nullvalue} $res]
167 # -version show SQLite version
168 do_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]+/}
172 #----------------------------------------------------------------------------
173 # Test cases shell1-2.*: Basic "dot" command token parsing.
176 # check first token handling
177 do_test shell1-2.1.1 {
178 catchcmd "test.db" ".foo"
179 } {1 {Error: unknown command or invalid arguments: "foo". Enter ".help" for help}}
180 do_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}}
183 do_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}}
188 do_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}}
191 do_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}}
194 do_test shell1-2.2.3 {
195 catchcmd "test.db" ".explain \"OFF"
197 do_test shell1-2.2.4 {
198 catchcmd "test.db" ".explain \'OFF"
200 do_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}}
203 do_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}}
207 # check multiple tokens, and quoted tokens
208 do_test shell1-2.3.1 {
209 catchcmd "test.db" ".explain 1"
211 do_test shell1-2.3.2 {
212 catchcmd "test.db" ".explain on"
214 do_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".}}
217 do_test shell1-2.3.4 {
218 catchcmd "test.db" ".explain \"OFF\""
220 do_test shell1-2.3.5 {
221 catchcmd "test.db" ".\'explain\' \'OFF\'"
223 do_test shell1-2.3.6 {
224 catchcmd "test.db" ".explain \'OFF\'"
226 do_test shell1-2.3.7 {
227 catchcmd "test.db" ".\'explain\' \'OFF\'"
230 # check quoted args are unquoted
231 do_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}}
234 do_test shell1-2.4.2 {
235 catchcmd "test.db" ".mode csv"
237 do_test shell1-2.4.2 {
238 catchcmd "test.db" ".mode \"csv\""
242 #----------------------------------------------------------------------------
243 # Test cases shell1-3.*: Basic test that "dot" command can be called.
246 # .backup ?DB? FILE Backup DB (default "main") to FILE
247 do_test shell1-3.1.1 {
248 catchcmd "test.db" ".backup"
249 } {1 {missing FILENAME argument on .backup}}
250 do_test shell1-3.1.2 {
251 catchcmd "test.db" ".backup FOO"
253 do_test shell1-3.1.3 {
254 catchcmd "test.db" ".backup FOO BAR"
255 } {1 {Error: unknown database FOO}}
256 do_test shell1-3.1.4 {
258 catchcmd "test.db" ".backup FOO BAR BAD"
259 } {1 {Usage: .backup ?DB? ?--append? FILENAME}}
261 # .bail ON|OFF Stop after hitting an error. Default OFF
262 do_test shell1-3.2.1 {
263 catchcmd "test.db" ".bail"
264 } {1 {Usage: .bail on|off}}
265 do_test shell1-3.2.2 {
266 catchcmd "test.db" ".bail ON"
268 do_test shell1-3.2.3 {
269 catchcmd "test.db" ".bail OFF"
271 do_test shell1-3.2.4 {
273 catchcmd "test.db" ".bail OFF BAD"
274 } {1 {Usage: .bail on|off}}
277 # .databases List names and files of attached databases
278 do_test shell1-3.3.1 {
279 catchcmd "-csv test.db" ".databases"
280 } "/0.+main.+[string map {/ ".{1,2}"} [string range [get_pwd] 0 10]].*/"
281 do_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]].*/"
287 # .dump ?TABLE? ... Dump the database in an SQL text format
288 # If TABLE specified, only dump tables matching
289 # LIKE pattern TABLE.
290 do_test shell1-3.4.1 {
291 set res [catchcmd "test.db" ".dump"]
292 list [regexp {BEGIN TRANSACTION;} $res] \
293 [regexp {COMMIT;} $res]
295 do_test shell1-3.4.2 {
296 set res [catchcmd "test.db" ".dump FOO"]
297 list [regexp {BEGIN TRANSACTION;} $res] \
298 [regexp {COMMIT;} $res]
300 do_test shell1-3.4.3 {
302 catchcmd "test.db" ".dump FOO BAD"
303 } {1 {Usage: .dump ?--preserve-rowids? ?--newlines? ?LIKE-PATTERN?}}
305 # .echo ON|OFF Turn command echo on or off
306 do_test shell1-3.5.1 {
307 catchcmd "test.db" ".echo"
308 } {1 {Usage: .echo on|off}}
309 do_test shell1-3.5.2 {
310 catchcmd "test.db" ".echo ON"
312 do_test shell1-3.5.3 {
313 catchcmd "test.db" ".echo OFF"
315 do_test shell1-3.5.4 {
317 catchcmd "test.db" ".echo OFF BAD"
318 } {1 {Usage: .echo on|off}}
320 # .exit Exit this program
321 do_test shell1-3.6.1 {
322 catchcmd "test.db" ".exit"
325 # .explain ON|OFF Turn output mode suitable for EXPLAIN on or off.
326 do_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.
330 do_test shell1-3.7.2 {
331 catchcmd "test.db" ".explain ON"
333 do_test shell1-3.7.3 {
334 catchcmd "test.db" ".explain OFF"
336 do_test shell1-3.7.4 {
337 # extra arguments ignored
338 catchcmd "test.db" ".explain OFF BAD"
342 # .header(s) ON|OFF Turn display of headers on or off
343 do_test shell1-3.9.1 {
344 catchcmd "test.db" ".header"
345 } {1 {Usage: .headers on|off}}
346 do_test shell1-3.9.2 {
347 catchcmd "test.db" ".header ON"
349 do_test shell1-3.9.3 {
350 catchcmd "test.db" ".header OFF"
352 do_test shell1-3.9.4 {
354 catchcmd "test.db" ".header OFF BAD"
355 } {1 {Usage: .headers on|off}}
357 do_test shell1-3.9.5 {
358 catchcmd "test.db" ".headers"
359 } {1 {Usage: .headers on|off}}
360 do_test shell1-3.9.6 {
361 catchcmd "test.db" ".headers ON"
363 do_test shell1-3.9.7 {
364 catchcmd "test.db" ".headers OFF"
366 do_test shell1-3.9.8 {
368 catchcmd "test.db" ".headers OFF BAD"
369 } {1 {Usage: .headers on|off}}
371 # .help Show this message
372 do_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]
379 do_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 BAD"]
382 # look for a few of the possible help commands
383 list [regexp {.help} $res] \
384 [regexp {.quit} $res] \
385 [regexp {.show} $res]
388 # .import FILE TABLE Import data from FILE into TABLE
389 do_test shell1-3.11.1 {
390 catchcmd "test.db" ".import"
391 } {1 {Usage: .import FILE TABLE}}
392 do_test shell1-3.11.2 {
393 catchcmd "test.db" ".import FOO"
394 } {1 {Usage: .import FILE TABLE}}
395 #do_test shell1-3.11.2 {
396 # catchcmd "test.db" ".import FOO BAR"
397 #} {1 {Error: no such table: BAR}}
398 do_test shell1-3.11.3 {
400 catchcmd "test.db" ".import FOO BAR BAD"
401 } {1 {Usage: .import FILE TABLE}}
403 # .indexes ?TABLE? Show names of all indexes
404 # If TABLE specified, only show indexes for tables
405 # matching LIKE pattern TABLE.
406 do_test shell1-3.12.1 {
407 catchcmd "test.db" ".indexes"
409 do_test shell1-3.12.2 {
410 catchcmd "test.db" ".indexes FOO"
412 do_test shell1-3.12.2-legacy {
413 catchcmd "test.db" ".indices FOO"
415 do_test shell1-3.12.3 {
417 catchcmd "test.db" ".indexes FOO BAD"
418 } {1 {Usage: .indexes ?LIKE-PATTERN?}}
420 # .mode MODE ?TABLE? Set output mode where MODE is one of:
421 # ascii Columns/rows delimited by 0x1F and 0x1E
422 # csv Comma-separated values
423 # column Left-aligned columns. (See .width)
424 # html HTML <table> code
425 # insert SQL insert statements for TABLE
426 # line One value per line
427 # list Values delimited by .separator strings
428 # tabs Tab-separated values
429 # tcl TCL list elements
430 do_test shell1-3.13.1 {
431 catchcmd "test.db" ".mode"
432 } {0 {current output mode: list}}
433 do_test shell1-3.13.2 {
434 catchcmd "test.db" ".mode FOO"
435 } {1 {Error: mode should be one of: ascii column csv html insert line list quote tabs tcl}}
436 do_test shell1-3.13.3 {
437 catchcmd "test.db" ".mode csv"
439 do_test shell1-3.13.4 {
440 catchcmd "test.db" ".mode column"
442 do_test shell1-3.13.5 {
443 catchcmd "test.db" ".mode html"
445 do_test shell1-3.13.6 {
446 catchcmd "test.db" ".mode insert"
448 do_test shell1-3.13.7 {
449 catchcmd "test.db" ".mode line"
451 do_test shell1-3.13.8 {
452 catchcmd "test.db" ".mode list"
454 do_test shell1-3.13.9 {
455 catchcmd "test.db" ".mode tabs"
457 do_test shell1-3.13.10 {
458 catchcmd "test.db" ".mode tcl"
460 do_test shell1-3.13.11 {
461 # extra arguments ignored
462 catchcmd "test.db" ".mode tcl BAD"
465 # don't allow partial mode type matches
466 do_test shell1-3.13.12 {
467 catchcmd "test.db" ".mode l"
468 } {1 {Error: mode should be one of: ascii column csv html insert line list quote tabs tcl}}
469 do_test shell1-3.13.13 {
470 catchcmd "test.db" ".mode li"
471 } {1 {Error: mode should be one of: ascii column csv html insert line list quote tabs tcl}}
472 do_test shell1-3.13.14 {
473 catchcmd "test.db" ".mode lin"
476 # .nullvalue STRING Print STRING in place of NULL values
477 do_test shell1-3.14.1 {
478 catchcmd "test.db" ".nullvalue"
479 } {1 {Usage: .nullvalue STRING}}
480 do_test shell1-3.14.2 {
481 catchcmd "test.db" ".nullvalue FOO"
483 do_test shell1-3.14.3 {
485 catchcmd "test.db" ".nullvalue FOO BAD"
486 } {1 {Usage: .nullvalue STRING}}
488 # .output FILENAME Send output to FILENAME
489 do_test shell1-3.15.1 {
490 catchcmd "test.db" ".output"
492 do_test shell1-3.15.2 {
493 catchcmd "test.db" ".output FOO"
495 do_test shell1-3.15.3 {
497 catchcmd "test.db" ".output FOO BAD"
498 } {1 {Usage: .output [-e|-x|FILE]}}
500 # .output stdout Send output to the screen
501 do_test shell1-3.16.1 {
502 catchcmd "test.db" ".output stdout"
504 do_test shell1-3.16.2 {
506 catchcmd "test.db" ".output stdout BAD"
507 } {1 {Usage: .output [-e|-x|FILE]}}
509 # .prompt MAIN CONTINUE Replace the standard prompts
510 do_test shell1-3.17.1 {
511 catchcmd "test.db" ".prompt"
513 do_test shell1-3.17.2 {
514 catchcmd "test.db" ".prompt FOO"
516 do_test shell1-3.17.3 {
517 catchcmd "test.db" ".prompt FOO BAR"
519 do_test shell1-3.17.4 {
521 catchcmd "test.db" ".prompt FOO BAR BAD"
524 # .quit Exit this program
525 do_test shell1-3.18.1 {
526 catchcmd "test.db" ".quit"
528 do_test shell1-3.18.2 {
530 catchcmd "test.db" ".quit BAD"
533 # .read FILENAME Execute SQL in FILENAME
534 do_test shell1-3.19.1 {
535 catchcmd "test.db" ".read"
536 } {1 {Usage: .read FILE}}
537 do_test shell1-3.19.2 {
539 catchcmd "test.db" ".read FOO"
540 } {1 {Error: cannot open "FOO"}}
541 do_test shell1-3.19.3 {
543 catchcmd "test.db" ".read FOO BAD"
544 } {1 {Usage: .read FILE}}
546 # .restore ?DB? FILE Restore content of DB (default "main") from FILE
547 do_test shell1-3.20.1 {
548 catchcmd "test.db" ".restore"
549 } {1 {Usage: .restore ?DB? FILE}}
550 do_test shell1-3.20.2 {
551 catchcmd "test.db" ".restore FOO"
553 do_test shell1-3.20.3 {
554 catchcmd "test.db" ".restore FOO BAR"
555 } {1 {Error: unknown database FOO}}
556 do_test shell1-3.20.4 {
558 catchcmd "test.db" ".restore FOO BAR BAD"
559 } {1 {Usage: .restore ?DB? FILE}}
562 # .schema ?TABLE? Show the CREATE statements
563 # If TABLE specified, only show tables matching
564 # LIKE pattern TABLE.
565 do_test shell1-3.21.1 {
566 catchcmd "test.db" ".schema"
568 do_test shell1-3.21.2 {
569 catchcmd "test.db" ".schema FOO"
571 do_test shell1-3.21.3 {
573 catchcmd "test.db" ".schema FOO BAD"
574 } {1 {Usage: .schema ?--indent? ?LIKE-PATTERN?}}
576 do_test shell1-3.21.4 {
579 CREATE VIEW v2 AS SELECT x+1 AS y FROM t1;
580 CREATE VIEW v1 AS SELECT y+1 FROM v2;
582 catchcmd "test.db" ".schema"
583 } {0 {CREATE TABLE t1(x);
584 CREATE VIEW v2 AS SELECT x+1 AS y FROM t1
586 CREATE VIEW v1 AS SELECT y+1 FROM v2
588 db eval {DROP VIEW v1; DROP VIEW v2; DROP TABLE t1;}
591 # .separator STRING Change column separator used by output and .import
592 do_test shell1-3.22.1 {
593 catchcmd "test.db" ".separator"
594 } {1 {Usage: .separator COL ?ROW?}}
595 do_test shell1-3.22.2 {
596 catchcmd "test.db" ".separator FOO"
598 do_test shell1-3.22.3 {
599 catchcmd "test.db" ".separator ABC XYZ"
601 do_test shell1-3.22.4 {
603 catchcmd "test.db" ".separator FOO BAD BAD2"
604 } {1 {Usage: .separator COL ?ROW?}}
606 # .show Show the current values for various settings
607 do_test shell1-3.23.1 {
608 set res [catchcmd "test.db" ".show"]
609 list [regexp {echo:} $res] \
610 [regexp {explain:} $res] \
611 [regexp {headers:} $res] \
612 [regexp {mode:} $res] \
613 [regexp {nullvalue:} $res] \
614 [regexp {output:} $res] \
615 [regexp {colseparator:} $res] \
616 [regexp {rowseparator:} $res] \
617 [regexp {stats:} $res] \
618 [regexp {width:} $res]
619 } {1 1 1 1 1 1 1 1 1 1}
620 do_test shell1-3.23.2 {
622 catchcmd "test.db" ".show BAD"
625 # .stats ON|OFF Turn stats on or off
626 #do_test shell1-3.23b.1 {
627 # catchcmd "test.db" ".stats"
628 #} {1 {Usage: .stats on|off}}
629 do_test shell1-3.23b.2 {
630 catchcmd "test.db" ".stats ON"
632 do_test shell1-3.23b.3 {
633 catchcmd "test.db" ".stats OFF"
635 do_test shell1-3.23b.4 {
637 catchcmd "test.db" ".stats OFF BAD"
638 } {1 {Usage: .stats ?on|off?}}
640 # Ticket 7be932dfa60a8a6b3b26bcf7623ec46e0a403ddb 2018-06-07
641 # Adverse interaction between .stats and .eqp
643 do_test shell1-3.23b.5 {
644 catchcmd "test.db" [string map {"\n " "\n"} {
645 CREATE TEMP TABLE t1(x);
646 INSERT INTO t1 VALUES(1),(2);
653 # .tables ?TABLE? List names of tables
654 # If TABLE specified, only list tables matching
655 # LIKE pattern TABLE.
656 do_test shell1-3.24.1 {
657 catchcmd "test.db" ".tables"
659 do_test shell1-3.24.2 {
660 catchcmd "test.db" ".tables FOO"
662 do_test shell1-3.24.3 {
664 catchcmd "test.db" ".tables FOO BAD"
667 # .timeout MS Try opening locked tables for MS milliseconds
668 do_test shell1-3.25.1 {
669 catchcmd "test.db" ".timeout"
671 do_test shell1-3.25.2 {
672 catchcmd "test.db" ".timeout zzz"
673 # this should be treated the same as a '0' timeout
675 do_test shell1-3.25.3 {
676 catchcmd "test.db" ".timeout 1"
678 do_test shell1-3.25.4 {
680 catchcmd "test.db" ".timeout 1 BAD"
683 # .width NUM NUM ... Set column widths for "column" mode
684 do_test shell1-3.26.1 {
685 catchcmd "test.db" ".width"
687 do_test shell1-3.26.2 {
688 catchcmd "test.db" ".width xxx"
689 # this should be treated the same as a '0' width for col 1
691 do_test shell1-3.26.3 {
692 catchcmd "test.db" ".width xxx yyy"
693 # this should be treated the same as a '0' width for col 1 and 2
695 do_test shell1-3.26.4 {
696 catchcmd "test.db" ".width 1 1"
697 # this should be treated the same as a '1' width for col 1 and 2
699 do_test shell1-3.26.5 {
700 catchcmd "test.db" ".mode column\n.width 10 -10\nSELECT 'abcdefg', 123456;"
701 # this should be treated the same as a '1' width for col 1 and 2
702 } {0 {abcdefg 123456}}
703 do_test shell1-3.26.6 {
704 catchcmd "test.db" ".mode column\n.width -10 10\nSELECT 'abcdefg', 123456;"
705 # this should be treated the same as a '1' width for col 1 and 2
706 } {0 { abcdefg 123456 }}
709 # .timer ON|OFF Turn the CPU timer measurement on or off
710 do_test shell1-3.27.1 {
711 catchcmd "test.db" ".timer"
712 } {1 {Usage: .timer on|off}}
713 do_test shell1-3.27.2 {
714 catchcmd "test.db" ".timer ON"
716 do_test shell1-3.27.3 {
717 catchcmd "test.db" ".timer OFF"
719 do_test shell1-3.27.4 {
721 catchcmd "test.db" ".timer OFF BAD"
722 } {1 {Usage: .timer on|off}}
724 do_test shell1-3-28.1 {
726 ".log stdout\nSELECT coalesce(sqlite_log(123,'hello'),'456');"
727 } "0 {(123) hello\n456}"
729 do_test shell1-3-29.1 {
730 catchcmd "test.db" ".print this is a test"
731 } {0 {this is a test}}
733 # dot-command argument quoting
734 do_test shell1-3-30.1 {
735 catchcmd {test.db} {.print "this\"is'a\055test" 'this\"is\\a\055test'}
736 } {0 {this"is'a-test this\"is\\a\055test}}
737 do_test shell1-3-31.1 {
738 catchcmd {test.db} {.print "this\nis\ta\\test" 'this\nis\ta\\test'}
739 } [list 0 "this\nis\ta\\test this\\nis\\ta\\\\test"]
742 # Test the output of the ".dump" command
749 PRAGMA encoding=UTF16;
751 INSERT INTO t1 VALUES(null), (''), (1), (2.25), ('hello'), (x'807f');
752 CREATE TABLE t3(x,y);
753 INSERT INTO t3 VALUES(1,null), (2,''), (3,1),
754 (4,2.25), (5,'hello'), (6,x'807f');
756 catchcmd test.db {.dump}
757 } {0 {PRAGMA foreign_keys=OFF;
760 INSERT INTO t1 VALUES(NULL);
761 INSERT INTO t1 VALUES('');
762 INSERT INTO t1 VALUES(1);
763 INSERT INTO t1 VALUES(2.25);
764 INSERT INTO t1 VALUES('hello');
765 INSERT INTO t1 VALUES(X'807f');
766 CREATE TABLE t3(x,y);
767 INSERT INTO t3 VALUES(1,NULL);
768 INSERT INTO t3 VALUES(2,'');
769 INSERT INTO t3 VALUES(3,1);
770 INSERT INTO t3 VALUES(4,2.25);
771 INSERT INTO t3 VALUES(5,'hello');
772 INSERT INTO t3 VALUES(6,X'807f');
778 # The --preserve-rowids option to .dump
780 do_test shell1-4.1.1 {
781 catchcmd test.db {.dump --preserve-rowids}
782 } {0 {PRAGMA foreign_keys=OFF;
785 INSERT INTO t1(rowid,x) VALUES(1,NULL);
786 INSERT INTO t1(rowid,x) VALUES(2,'');
787 INSERT INTO t1(rowid,x) VALUES(3,1);
788 INSERT INTO t1(rowid,x) VALUES(4,2.25);
789 INSERT INTO t1(rowid,x) VALUES(5,'hello');
790 INSERT INTO t1(rowid,x) VALUES(6,X'807f');
791 CREATE TABLE t3(x,y);
792 INSERT INTO t3(rowid,x,y) VALUES(1,1,NULL);
793 INSERT INTO t3(rowid,x,y) VALUES(2,2,'');
794 INSERT INTO t3(rowid,x,y) VALUES(3,3,1);
795 INSERT INTO t3(rowid,x,y) VALUES(4,4,2.25);
796 INSERT INTO t3(rowid,x,y) VALUES(5,5,'hello');
797 INSERT INTO t3(rowid,x,y) VALUES(6,6,X'807f');
800 # If the table contains an INTEGER PRIMARY KEY, do not record a separate
801 # rowid column in the output.
803 do_test shell1-4.1.2 {
808 CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
809 INSERT INTO t1 VALUES(1,null), (2,''), (3,1),
810 (4,2.25), (5,'hello'), (6,x'807f');
812 catchcmd test2.db {.dump --preserve-rowids}
813 } {0 {PRAGMA foreign_keys=OFF;
815 CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
816 INSERT INTO t1 VALUES(1,NULL);
817 INSERT INTO t1 VALUES(2,'');
818 INSERT INTO t1 VALUES(3,1);
819 INSERT INTO t1 VALUES(4,2.25);
820 INSERT INTO t1 VALUES(5,'hello');
821 INSERT INTO t1 VALUES(6,X'807f');
824 # Verify that the table named [table] is correctly quoted and that
825 # an INTEGER PRIMARY KEY DESC is not an alias for the rowid.
827 do_test shell1-4.1.3 {
832 CREATE TABLE [table](x INTEGER PRIMARY KEY DESC, y);
833 INSERT INTO [table] VALUES(1,null), (12,''), (23,1),
834 (34,2.25), (45,'hello'), (56,x'807f');
836 catchcmd test2.db {.dump --preserve-rowids}
837 } {0 {PRAGMA foreign_keys=OFF;
839 CREATE TABLE [table](x INTEGER PRIMARY KEY DESC, y);
840 INSERT INTO "table"(rowid,x,y) VALUES(1,1,NULL);
841 INSERT INTO "table"(rowid,x,y) VALUES(2,12,'');
842 INSERT INTO "table"(rowid,x,y) VALUES(3,23,1);
843 INSERT INTO "table"(rowid,x,y) VALUES(4,34,2.25);
844 INSERT INTO "table"(rowid,x,y) VALUES(5,45,'hello');
845 INSERT INTO "table"(rowid,x,y) VALUES(6,56,X'807f');
848 # Do not record rowids for a WITHOUT ROWID table. Also check correct quoting
849 # of table names that contain odd characters.
851 do_test shell1-4.1.4 {
856 CREATE TABLE [ta<>ble](x INTEGER PRIMARY KEY, y) WITHOUT ROWID;
857 INSERT INTO [ta<>ble] VALUES(1,null), (12,''), (23,1),
858 (34,2.25), (45,'hello'), (56,x'807f');
860 catchcmd test2.db {.dump --preserve-rowids}
861 } {0 {PRAGMA foreign_keys=OFF;
863 CREATE TABLE [ta<>ble](x INTEGER PRIMARY KEY, y) WITHOUT ROWID;
864 INSERT INTO "ta<>ble" VALUES(1,NULL);
865 INSERT INTO "ta<>ble" VALUES(12,'');
866 INSERT INTO "ta<>ble" VALUES(23,1);
867 INSERT INTO "ta<>ble" VALUES(34,2.25);
868 INSERT INTO "ta<>ble" VALUES(45,'hello');
869 INSERT INTO "ta<>ble" VALUES(56,X'807f');
872 # Do not record rowids if the rowid is inaccessible
874 do_test shell1-4.1.5 {
879 CREATE TABLE t1(_ROWID_,rowid,oid);
880 INSERT INTO t1 VALUES(1,null,'alpha'), (12,'',99), (23,1,x'b0b1b2');
882 catchcmd test2.db {.dump --preserve-rowids}
883 } {0 {PRAGMA foreign_keys=OFF;
885 CREATE TABLE t1(_ROWID_,rowid,oid);
886 INSERT INTO t1 VALUES(1,NULL,'alpha');
887 INSERT INTO t1 VALUES(12,'',99);
888 INSERT INTO t1 VALUES(23,1,X'b0b1b2');
893 do_test shell1-4.1.6 {
898 CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
899 INSERT INTO t1 VALUES(1,null), (2,''), (3,1),
900 (4,2.25), (5,'hello'), (6,x'807f');
902 catchcmd test2.db {.dump --preserve-rowids}
903 } {1 {The --preserve-rowids option is not compatible with SQLITE_OMIT_VIRTUALTABLE}}
908 # Test the output of ".mode insert"
910 do_test shell1-4.2.1 {
911 catchcmd test.db ".mode insert t1\nselect * from t1;"
912 } {0 {INSERT INTO t1 VALUES(NULL);
913 INSERT INTO t1 VALUES('');
914 INSERT INTO t1 VALUES(1);
915 INSERT INTO t1 VALUES(2.25);
916 INSERT INTO t1 VALUES('hello');
917 INSERT INTO t1 VALUES(X'807f');}}
919 # Test the output of ".mode insert" with headers
921 do_test shell1-4.2.2 {
922 catchcmd test.db ".mode insert t1\n.headers on\nselect * from t1;"
923 } {0 {INSERT INTO t1(x) VALUES(NULL);
924 INSERT INTO t1(x) VALUES('');
925 INSERT INTO t1(x) VALUES(1);
926 INSERT INTO t1(x) VALUES(2.25);
927 INSERT INTO t1(x) VALUES('hello');
928 INSERT INTO t1(x) VALUES(X'807f');}}
930 # Test the output of ".mode insert"
932 do_test shell1-4.2.3 {
933 catchcmd test.db ".mode insert t3\nselect * from t3;"
934 } {0 {INSERT INTO t3 VALUES(1,NULL);
935 INSERT INTO t3 VALUES(2,'');
936 INSERT INTO t3 VALUES(3,1);
937 INSERT INTO t3 VALUES(4,2.25);
938 INSERT INTO t3 VALUES(5,'hello');
939 INSERT INTO t3 VALUES(6,X'807f');}}
941 # Test the output of ".mode insert" with headers
943 do_test shell1-4.2.4 {
944 catchcmd test.db ".mode insert t3\n.headers on\nselect * from t3;"
945 } {0 {INSERT INTO t3(x,y) VALUES(1,NULL);
946 INSERT INTO t3(x,y) VALUES(2,'');
947 INSERT INTO t3(x,y) VALUES(3,1);
948 INSERT INTO t3(x,y) VALUES(4,2.25);
949 INSERT INTO t3(x,y) VALUES(5,'hello');
950 INSERT INTO t3(x,y) VALUES(6,X'807f');}}
952 # Test the output of ".mode tcl"
959 PRAGMA encoding=UTF8;
961 INSERT INTO t1 VALUES(null), (''), (1), (2.25), ('hello'), (x'807f');
963 catchcmd test.db ".mode tcl\nselect * from t1;"
971 # Test the output of ".mode tcl" with multiple columns
975 CREATE TABLE t2(x,y);
976 INSERT INTO t2 VALUES(null, ''), (1, 2.25), ('hello', x'807f');
978 catchcmd test.db ".mode tcl\nselect * from t2;"
983 # Test the output of ".mode tcl" with ".nullvalue"
986 catchcmd test.db ".mode tcl\n.nullvalue NULL\nselect * from t2;"
991 # Test the output of ".mode tcl" with Tcl reserved characters
995 CREATE TABLE tcl1(x);
996 INSERT INTO tcl1 VALUES('"'), ('['), (']'), ('\{'), ('\}'), (';'), ('$');
998 foreach {x y} [catchcmd test.db ".mode tcl\nselect * from tcl1;"] break
999 list $x $y [llength $y]
1008 # Test using arbitrary byte data with the shell via standard input/output.
1010 do_test shell1-5.0 {
1012 # NOTE: Skip NUL byte because it appears to be incompatible with command
1013 # shell argument parsing.
1015 for {set i 1} {$i < 256} {incr i} {
1017 # NOTE: Due to how the Tcl [exec] command works (i.e. where it treats
1018 # command channels opened for it as textual ones), the carriage
1019 # return character (and on Windows, the end-of-file character)
1020 # cannot be used here.
1022 if {$i==0x0D || ($tcl_platform(platform)=="windows" && $i==0x1A)} {
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
1030 if {$tcl_platform(platform)=="windows"} {
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.
1037 \a \\a \b \\b \t \\t \n \\n \v \\v \f \\f \r \\r \
1038 " " "\" \"" \" \\\" ' \"'\" \\ \\\\]
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.
1051 \t \\t \n \\n \v \\v \f \\f \
1052 " " "\" \"" \" \\\" ' \"'\" \\ \\\\]
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]
1059 error "failed with error: $res"
1061 if {$res ne "$oldChar\n"} {
1062 if {[llength $res] > 0} {
1063 set got [format %02X [scan $res %c]]
1067 error "failed with byte $hex mismatch, got $got"
1072 # These test cases do not work on MinGW
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
1077 set test \u6D4B\u8BD5
1079 do_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]]
1086 error "failed with error: $res"
1088 if {$res ne "CREATE TABLE t1(x);"} {
1089 error "failed with mismatch: $res"
1091 if {![file exists $fileName]} {
1092 error "file \"$fileName\" (Unicode) does not exist"
1094 forcedelete $fileName
1097 do_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]]
1104 error "failed with error: $res"
1106 if {$res ne "CREATE TABLE ${test}(x);"} {
1107 error "failed with mismatch: $res"
1109 forcedelete test3.db
1114 forcedelete test.db test.db-journal test.db-wal
1117 # The shell tool ".schema" command uses virtual table "pragma_database_list"
1121 do_test shell1-7.1.1 {
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);
1131 do_test shell1-7.1.2 {
1132 catchcmd "test.db" ".schema _"
1133 } {0 {CREATE TABLE Z (x TEXT PRIMARY KEY);
1134 CREATE TABLE _ (x TEXT PRIMARY KEY);}}
1135 do_test shell1-7.1.3 {
1136 catchcmd "test.db" ".schema \\\\_"
1137 } {0 {CREATE TABLE _ (x TEXT PRIMARY KEY);}}
1138 do_test shell1-7.1.4 {
1139 catchcmd "test.db" ".schema __"
1140 } {0 {CREATE TABLE YY (x TEXT PRIMARY KEY);
1141 CREATE TABLE __ (x TEXT PRIMARY KEY);}}
1142 do_test shell1-7.1.5 {
1143 catchcmd "test.db" ".schema \\\\_\\\\_"
1144 } {0 {CREATE TABLE __ (x TEXT PRIMARY KEY);}}
1145 do_test shell1-7.1.6 {
1146 catchcmd "test.db" ".schema ___"
1147 } {0 {CREATE TABLE WWW (x TEXT PRIMARY KEY);
1148 CREATE TABLE ___ (x TEXT PRIMARY KEY);}}
1149 do_test shell1-7.1.7 {
1150 catchcmd "test.db" ".schema \\\\_\\\\_\\\\_"
1151 } {0 {CREATE TABLE ___ (x TEXT PRIMARY KEY);}}