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.
19 # shell1-1.*: Basic command line option handling.
20 # shell1-2.*: Basic "dot" command token parsing.
21 # shell1-3.*: Basic test that "dot" command can be called.
22 # shell1-{4-8}.*: Test various "dot" commands's functionality.
23 # shell1-9.*: Basic test that "dot" commands and SQL intermix ok.
25 set testdir [file dirname $argv0]
26 source $testdir/tester.tcl
27 set CLI [test_cli_invocation]
29 forcedelete test.db test.db-journal test.db-wal
32 #----------------------------------------------------------------------------
33 # Test cases shell1-1.*: Basic command line option handling.
37 do_test shell1-1.1.1 {
38 set res [catchcmd "-bad test.db" ""]
39 set rc [lindex $res 0]
41 [regexp {Error: unknown option: -bad} $res]
43 do_test shell1-1.1.1b {
44 set res [catchcmd "test.db -bad" ""]
45 set rc [lindex $res 0]
47 [regexp {Error: unknown option: -bad} $res]
49 # error on extra options
50 do_test shell1-1.1.2 {
51 catchcmd "test.db \"select+3\" \"select+4\"" ""
54 # error on extra options
55 do_test shell1-1.1.3 {
56 catchcmd "test.db FOO test.db BAD" ".quit"
57 } {/1 .Error: in prepare, near "FOO": syntax error*/}
60 do_test shell1-1.2.1 {
61 set res [catchcmd "-help test.db" ""]
62 set rc [lindex $res 0]
64 [regexp {Usage} $res] \
65 [regexp {\-init} $res] \
66 [regexp {\-version} $res]
69 # -init filename read/process named file
74 do_test shell1-1.3.1 {
75 catchcmd "-init FOO test.db" ""
77 do_test shell1-1.3.2 {
78 catchcmd "-init FOO test.db .quit BAD" ""
80 do_test shell1-1.3.3 {
81 catchcmd "-init FOO test.db BAD .quit" ""
82 } {/1 .Error: in prepare, near "BAD": syntax error*/}
84 # -echo print commands before execution
85 do_test shell1-1.4.1 {
86 catchcmd "-echo test.db" ""
89 # -[no]header turn headers on or off
90 do_test shell1-1.5.1 {
91 catchcmd "-header test.db" ""
93 do_test shell1-1.5.2 {
94 catchcmd "-noheader test.db" ""
97 # -bail stop after hitting an error
98 do_test shell1-1.6.1 {
99 catchcmd "-bail test.db" ""
102 # -interactive force interactive I/O
103 do_test shell1-1.7.1 {
104 set res [catchcmd "-interactive test.db" ".quit"]
105 set rc [lindex $res 0]
107 [regexp {SQLite version} $res] \
108 [regexp {Enter ".help" for usage hints} $res]
111 # -batch force batch I/O
112 do_test shell1-1.8.1 {
113 catchcmd "-batch test.db" ""
116 # -column set output mode to 'column'
117 do_test shell1-1.9.1 {
118 catchcmd "-column test.db" ""
121 # -csv set output mode to 'csv'
122 do_test shell1-1.10.1 {
123 catchcmd "-csv test.db" ""
126 # -html set output mode to HTML
127 do_test shell1-1.11.1 {
128 catchcmd "-html test.db" ""
131 # -line set output mode to 'line'
132 do_test shell1-1.12.1 {
133 catchcmd "-line test.db" ""
136 # -list set output mode to 'list'
137 do_test shell1-1.13.1 {
138 catchcmd "-list test.db" ""
141 # -separator 'x' set output field separator (|)
142 do_test shell1-1.14.1 {
143 catchcmd "-separator 'x' test.db" ""
145 do_test shell1-1.14.2 {
146 catchcmd "-separator x test.db" ""
148 do_test shell1-1.14.3 {
149 set res [catchcmd "-separator" ""]
150 set rc [lindex $res 0]
152 [regexp {Error: missing argument to -separator} $res]
155 # -stats print memory stats before each finalize
156 do_test shell1-1.14b.1 {
157 catchcmd "-stats test.db" ""
160 # -nullvalue 'text' set text string for NULL values
161 do_test shell1-1.15.1 {
162 catchcmd "-nullvalue 'x' test.db" ""
164 do_test shell1-1.15.2 {
165 catchcmd "-nullvalue x test.db" ""
167 do_test shell1-1.15.3 {
168 set res [catchcmd "-nullvalue" ""]
169 set rc [lindex $res 0]
171 [regexp {Error: missing argument to -nullvalue} $res]
174 # -version show SQLite version
175 do_test shell1-1.16.1 {
176 set x [catchcmd "-version test.db" ""]
177 } {/3.[0-9.]+ 20\d\d-[01]\d-\d\d \d\d:\d\d:\d\d [0-9a-f]+/}
179 # Handle no-more-options option
181 do_test shell1-1.17.1 {
182 catchcmd {-- --db "CREATE TABLE T(c1);"}
184 do_test shell1-1.17.2 {
185 catchcmd {-- --db "SELECT name from sqlite_schema;"}
189 #----------------------------------------------------------------------------
190 # Test cases shell1-2.*: Basic "dot" command token parsing.
193 # check first token handling
194 do_test shell1-2.1.1 {
195 catchcmd "test.db" ".foo"
196 } {1 {Error: unknown command or invalid arguments: "foo". Enter ".help" for help}}
197 do_test shell1-2.1.2 {
198 catchcmd "test.db" ".\"foo OFF\""
199 } {1 {Error: unknown command or invalid arguments: "foo OFF". Enter ".help" for help}}
200 do_test shell1-2.1.3 {
201 catchcmd "test.db" ".\'foo OFF\'"
202 } {1 {Error: unknown command or invalid arguments: "foo OFF". Enter ".help" for help}}
205 do_test shell1-2.2.1 {
206 catchcmd "test.db" ".\"foo OFF"
207 } {1 {Error: unknown command or invalid arguments: "foo OFF". Enter ".help" for help}}
208 do_test shell1-2.2.2 {
209 catchcmd "test.db" ".\'foo OFF"
210 } {1 {Error: unknown command or invalid arguments: "foo OFF". Enter ".help" for help}}
211 do_test shell1-2.2.3 {
212 catchcmd "test.db" ".explain \"OFF"
214 do_test shell1-2.2.4 {
215 catchcmd "test.db" ".explain \'OFF"
217 do_test shell1-2.2.5 {
218 catchcmd "test.db" ".mode \"insert FOO"
219 } {1 {Error: mode should be one of: ascii box column csv html insert json line list markdown qbox quote table tabs tcl}}
220 do_test shell1-2.2.6 {
221 catchcmd "test.db" ".mode \'insert FOO"
222 } {1 {Error: mode should be one of: ascii box column csv html insert json line list markdown qbox quote table tabs tcl}}
224 # check multiple tokens, and quoted tokens
225 do_test shell1-2.3.1 {
226 catchcmd "test.db" ".explain 1"
228 do_test shell1-2.3.2 {
229 catchcmd "test.db" ".explain on"
231 do_test shell1-2.3.3 {
232 catchcmd "test.db" ".explain \"1 2 3\""
233 } {1 {ERROR: Not a boolean value: "1 2 3". Assuming "no".}}
234 do_test shell1-2.3.4 {
235 catchcmd "test.db" ".explain \"OFF\""
237 do_test shell1-2.3.5 {
238 catchcmd "test.db" ".\'explain\' \'OFF\'"
240 do_test shell1-2.3.6 {
241 catchcmd "test.db" ".explain \'OFF\'"
243 do_test shell1-2.3.7 {
244 catchcmd "test.db" ".\'explain\' \'OFF\'"
247 # check quoted args are unquoted
248 do_test shell1-2.4.1 {
249 catchcmd "test.db" ".mode FOO"
250 } {1 {Error: mode should be one of: ascii box column csv html insert json line list markdown qbox quote table tabs tcl}}
251 do_test shell1-2.4.2 {
252 catchcmd "test.db" ".mode csv"
254 do_test shell1-2.4.2 {
255 catchcmd "test.db" ".mode \"csv\""
258 # check that certain quoted arg escapes work
259 do_test shell1-2.5.1 {
260 catchcmd ":memory:" ".print \"\\060\\077 \\x3f\\x30 \\a\\t\""
261 } [list 0 "0? ?0 \a\t"]
264 #----------------------------------------------------------------------------
265 # Test cases shell1-3.*: Basic test that "dot" command can be called.
268 # .backup ?DB? FILE Backup DB (default "main") to FILE
269 do_test shell1-3.1.1 {
270 catchcmd "test.db" ".backup"
271 } {1 {missing FILENAME argument on .backup}}
273 do_test shell1-3.1.2 {
274 catchcmd "test.db" ".backup FOO"
276 do_test shell1-3.1.3 {
277 catchcmd "test.db" ".backup FOO BAR"
278 } {1 {Error: unknown database FOO}}
279 do_test shell1-3.1.4 {
281 catchcmd "test.db" ".backup FOO BAR BAD"
282 } {1 {Usage: .backup ?DB? ?OPTIONS? FILENAME}}
284 # .bail ON|OFF Stop after hitting an error. Default OFF
285 do_test shell1-3.2.1 {
286 catchcmd "test.db" ".bail"
287 } {1 {Usage: .bail on|off}}
288 do_test shell1-3.2.2 {
289 catchcmd "test.db" ".bail ON"
291 do_test shell1-3.2.3 {
292 catchcmd "test.db" ".bail OFF"
294 do_test shell1-3.2.4 {
296 catchcmd "test.db" ".bail OFF BAD"
297 } {1 {Usage: .bail on|off}}
300 # .databases List names and files of attached databases
301 do_test shell1-3.3.1 {
302 catchcmd "-csv test.db" ".databases"
303 } "/0.+main.+[string map {/ ".{1,2}"} [string range [get_pwd] 0 10]].*/"
304 do_test shell1-3.3.2 {
305 # extra arguments ignored
306 catchcmd "test.db" ".databases BAD"
307 } "/0.+main.+[string map {/ ".{1,2}"} [string range [get_pwd] 0 10]].*/"
310 # .dump ?TABLE? ... Dump the database in an SQL text format
311 # If TABLE specified, only dump tables matching
312 # LIKE pattern TABLE.
313 do_test shell1-3.4.1 {
314 set res [catchcmd "test.db" ".dump"]
315 list [regexp {BEGIN TRANSACTION;} $res] \
316 [regexp {COMMIT;} $res]
318 do_test shell1-3.4.2 {
319 set res [catchcmd "test.db" ".dump FOO"]
320 list [regexp {BEGIN TRANSACTION;} $res] \
321 [regexp {COMMIT;} $res]
323 # The .dump command now accepts multiple arguments
324 #do_test shell1-3.4.3 {
325 # # too many arguments
326 # catchcmd "test.db" ".dump FOO BAD"
327 #} {1 {Usage: .dump ?--preserve-rowids? ?--newlines? ?LIKE-PATTERN?}}
329 # .echo ON|OFF Turn command echo on or off
330 do_test shell1-3.5.1 {
331 catchcmd "test.db" ".echo"
332 } {1 {Usage: .echo on|off}}
333 do_test shell1-3.5.2 {
334 catchcmd "test.db" ".echo ON"
336 do_test shell1-3.5.3 {
337 catchcmd "test.db" ".echo OFF"
339 do_test shell1-3.5.4 {
341 catchcmd "test.db" ".echo OFF BAD"
342 } {1 {Usage: .echo on|off}}
344 # .exit Exit this program
345 do_test shell1-3.6.1 {
346 catchcmd "test.db" ".exit"
349 # .explain ON|OFF Turn output mode suitable for EXPLAIN on or off.
350 do_test shell1-3.7.1 {
351 catchcmd "test.db" ".explain"
352 # explain is the exception to the booleans. without an option, it turns it on.
354 do_test shell1-3.7.2 {
355 catchcmd "test.db" ".explain ON"
357 do_test shell1-3.7.3 {
358 catchcmd "test.db" ".explain OFF"
360 do_test shell1-3.7.4 {
361 # extra arguments ignored
362 catchcmd "test.db" ".explain OFF BAD"
366 # .header(s) ON|OFF Turn display of headers on or off
367 do_test shell1-3.9.1 {
368 catchcmd "test.db" ".header"
369 } {1 {Usage: .headers on|off}}
370 do_test shell1-3.9.2 {
371 catchcmd "test.db" ".header ON"
373 do_test shell1-3.9.3 {
374 catchcmd "test.db" ".header OFF"
376 do_test shell1-3.9.4 {
378 catchcmd "test.db" ".header OFF BAD"
379 } {1 {Usage: .headers on|off}}
381 do_test shell1-3.9.5 {
382 catchcmd "test.db" ".headers"
383 } {1 {Usage: .headers on|off}}
384 do_test shell1-3.9.6 {
385 catchcmd "test.db" ".headers ON"
387 do_test shell1-3.9.7 {
388 catchcmd "test.db" ".headers OFF"
390 do_test shell1-3.9.8 {
392 catchcmd "test.db" ".headers OFF BAD"
393 } {1 {Usage: .headers on|off}}
395 # .help Show this message
396 do_test shell1-3.10.1 {
397 set res [catchcmd "test.db" ".help"]
398 # look for a few of the possible help commands
399 list [regexp {.help} $res] \
400 [regexp {.quit} $res] \
401 [regexp {.show} $res]
403 do_test shell1-3.10.2 {
404 # we allow .help to take extra args (it is help after all)
405 set res [catchcmd "test.db" ".help *"]
406 # look for a few of the possible help commands
407 list [regexp {.help} $res] \
408 [regexp {.quit} $res] \
409 [regexp {.show} $res]
412 # .import FILE TABLE Import data from FILE into TABLE
413 do_test shell1-3.11.1 {
414 catchcmd "test.db" ".import"
415 } {/1 .ERROR: missing FILE argument.*/}
416 do_test shell1-3.11.2 {
417 catchcmd "test.db" ".import FOO"
418 } {/1 .ERROR: missing TABLE argument.*/}
419 do_test shell1-3.11.3 {
421 catchcmd "test.db" ".import FOO BAR BAD"
422 } {/1 .ERROR: extra argument: "BAD".*./}
424 # .indexes ?TABLE? Show names of all indexes
425 # If TABLE specified, only show indexes for tables
426 # matching LIKE pattern TABLE.
427 do_test shell1-3.12.1 {
428 catchcmd "test.db" ".indexes"
430 do_test shell1-3.12.2 {
431 catchcmd "test.db" ".indexes FOO"
433 do_test shell1-3.12.2-legacy {
434 catchcmd "test.db" ".indices FOO"
436 do_test shell1-3.12.3 {
438 catchcmd "test.db" ".indexes FOO BAD"
439 } {1 {Usage: .indexes ?LIKE-PATTERN?}}
441 # .mode MODE ?TABLE? Set output mode where MODE is one of:
442 # ascii Columns/rows delimited by 0x1F and 0x1E
443 # csv Comma-separated values
444 # column Left-aligned columns. (See .width)
445 # html HTML <table> code
446 # insert SQL insert statements for TABLE
447 # line One value per line
448 # list Values delimited by .separator strings
449 # tabs Tab-separated values
450 # tcl TCL list elements
451 do_test shell1-3.13.1 {
452 catchcmd "test.db" ".mode"
453 } {0 {current output mode: list}}
454 do_test shell1-3.13.2 {
455 catchcmd "test.db" ".mode FOO"
456 } {1 {Error: mode should be one of: ascii box column csv html insert json line list markdown qbox quote table tabs tcl}}
457 do_test shell1-3.13.3 {
458 catchcmd "test.db" ".mode csv"
460 do_test shell1-3.13.4 {
461 catchcmd "test.db" ".mode column"
463 do_test shell1-3.13.5 {
464 catchcmd "test.db" ".mode html"
466 do_test shell1-3.13.6 {
467 catchcmd "test.db" ".mode insert"
469 do_test shell1-3.13.7 {
470 catchcmd "test.db" ".mode line"
472 do_test shell1-3.13.8 {
473 catchcmd "test.db" ".mode list"
475 do_test shell1-3.13.9 {
476 catchcmd "test.db" ".mode tabs"
478 do_test shell1-3.13.10 {
479 catchcmd "test.db" ".mode tcl"
481 do_test shell1-3.13.11 {
482 # extra arguments ignored
483 catchcmd "test.db" ".mode tcl BAD"
486 # .nullvalue STRING Print STRING in place of NULL values
487 do_test shell1-3.14.1 {
488 catchcmd "test.db" ".nullvalue"
489 } {1 {Usage: .nullvalue STRING}}
490 do_test shell1-3.14.2 {
491 catchcmd "test.db" ".nullvalue FOO"
493 do_test shell1-3.14.3 {
495 catchcmd "test.db" ".nullvalue FOO BAD"
496 } {1 {Usage: .nullvalue STRING}}
498 # .output FILENAME Send output to FILENAME
499 do_test shell1-3.15.1 {
500 catchcmd "test.db" ".output
503 do_test shell1-3.15.2 {
504 catchcmd "test.db" ".output FOO
507 SELECT readfile('FOO');"
510 do_test shell1-3.15.3 {
512 catchcmd "test.db" ".output FOO BAD"
513 } {1 {ERROR: extra parameter: "BAD". Usage:
514 .output ?FILE? Send output to FILE or stdout if FILE is omitted
515 If FILE begins with '|' then open it as a pipe.
517 --bom Prefix output with a UTF8 byte-order mark
518 -e Send output to the system text editor
519 --plain Use text/plain for -w option
520 -w Send output to a web browser
521 -x Send output as CSV to a spreadsheet
522 child process exited abnormally}}
524 # .output stdout Send output to the screen
525 do_test shell1-3.16.1 {
526 catchcmd "test.db" ".output stdout"
528 do_test shell1-3.16.2 {
530 catchcmd "test.db" ".output stdout BAD"
531 } {1 {ERROR: extra parameter: "BAD". Usage:
532 .output ?FILE? Send output to FILE or stdout if FILE is omitted
533 If FILE begins with '|' then open it as a pipe.
535 --bom Prefix output with a UTF8 byte-order mark
536 -e Send output to the system text editor
537 --plain Use text/plain for -w option
538 -w Send output to a web browser
539 -x Send output as CSV to a spreadsheet
540 child process exited abnormally}}
542 # .prompt MAIN CONTINUE Replace the standard prompts
543 do_test shell1-3.17.1 {
544 catchcmd "test.db" ".prompt"
546 do_test shell1-3.17.2 {
547 catchcmd "test.db" ".prompt FOO"
549 do_test shell1-3.17.3 {
550 catchcmd "test.db" ".prompt FOO BAR"
552 do_test shell1-3.17.4 {
554 catchcmd "test.db" ".prompt FOO BAR BAD"
557 # .quit Exit this program
558 do_test shell1-3.18.1 {
559 catchcmd "test.db" ".quit"
561 do_test shell1-3.18.2 {
563 catchcmd "test.db" ".quit BAD"
566 # .read FILENAME Execute SQL in FILENAME
567 do_test shell1-3.19.1 {
568 catchcmd "test.db" ".read"
569 } {1 {Usage: .read FILE}}
570 do_test shell1-3.19.2 {
572 catchcmd "test.db" ".read FOO"
573 } {1 {Error: cannot open "FOO"}}
574 do_test shell1-3.19.3 {
576 catchcmd "test.db" ".read FOO BAD"
577 } {1 {Usage: .read FILE}}
579 # .restore ?DB? FILE Restore content of DB (default "main") from FILE
580 do_test shell1-3.20.1 {
581 catchcmd "test.db" ".restore"
582 } {1 {Usage: .restore ?DB? FILE}}
583 do_test shell1-3.20.2 {
584 catchcmd "test.db" ".restore FOO"
586 do_test shell1-3.20.3 {
587 catchcmd "test.db" ".restore FOO BAR"
588 } {1 {Error: unknown database FOO}}
589 do_test shell1-3.20.4 {
591 catchcmd "test.db" ".restore FOO BAR BAD"
592 } {1 {Usage: .restore ?DB? FILE}}
595 # .schema ?TABLE? Show the CREATE statements
596 # If TABLE specified, only show tables matching
597 # LIKE pattern TABLE.
598 do_test shell1-3.21.1 {
599 catchcmd "test.db" ".schema"
601 do_test shell1-3.21.2 {
602 catchcmd "test.db" ".schema FOO"
604 do_test shell1-3.21.3 {
606 catchcmd "test.db" ".schema FOO BAD"
607 } {1 {Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?}}
609 do_test shell1-3.21.4 {
612 CREATE VIEW v2 AS SELECT x+1 AS y FROM t1;
613 CREATE VIEW v1 AS SELECT y+1 FROM v2;
615 catchcmd "test.db" ".schema"
616 } {0 {CREATE TABLE t1(x);
617 CREATE VIEW v2 AS SELECT x+1 AS y FROM t1
619 CREATE VIEW v1 AS SELECT y+1 FROM v2
622 catch {db eval {DROP VIEW v1; DROP VIEW v2; DROP TABLE t1;}}
625 # .separator STRING Change column separator used by output and .import
626 do_test shell1-3.22.1 {
627 catchcmd "test.db" ".separator"
628 } {1 {Usage: .separator COL ?ROW?}}
629 do_test shell1-3.22.2 {
630 catchcmd "test.db" ".separator FOO"
632 do_test shell1-3.22.3 {
633 catchcmd "test.db" ".separator ABC XYZ"
635 do_test shell1-3.22.4 {
637 catchcmd "test.db" ".separator FOO BAD BAD2"
638 } {1 {Usage: .separator COL ?ROW?}}
640 # .show Show the current values for various settings
641 do_test shell1-3.23.1 {
642 set res [catchcmd "test.db" ".show"]
643 list [regexp {echo:} $res] \
644 [regexp {explain:} $res] \
645 [regexp {headers:} $res] \
646 [regexp {mode:} $res] \
647 [regexp {nullvalue:} $res] \
648 [regexp {output:} $res] \
649 [regexp {colseparator:} $res] \
650 [regexp {rowseparator:} $res] \
651 [regexp {stats:} $res] \
652 [regexp {width:} $res]
653 } {1 1 1 1 1 1 1 1 1 1}
654 do_test shell1-3.23.2 {
656 catchcmd "test.db" ".show BAD"
659 # .stats ON|OFF Turn stats on or off
660 #do_test shell1-3.23b.1 {
661 # catchcmd "test.db" ".stats"
662 #} {1 {Usage: .stats on|off|stmt|vmstep}}
663 do_test shell1-3.23b.2 {
664 catchcmd "test.db" ".stats ON"
666 do_test shell1-3.23b.3 {
667 catchcmd "test.db" ".stats OFF"
669 do_test shell1-3.23b.4 {
671 catchcmd "test.db" ".stats OFF BAD"
672 } {1 {Usage: .stats ?on|off|stmt|vmstep?}}
674 # Ticket 7be932dfa60a8a6b3b26bcf7623ec46e0a403ddb 2018-06-07
675 # Adverse interaction between .stats and .eqp
677 do_test shell1-3.23b.5 {
678 catchcmd "test.db" [string map {"\n " "\n"} {
679 CREATE TEMP TABLE t1(x);
680 INSERT INTO t1 VALUES(1),(2);
687 # .tables ?TABLE? List names of tables
688 # If TABLE specified, only list tables matching
689 # LIKE pattern TABLE.
690 do_test shell1-3.24.1 {
691 catchcmd "test.db" ".tables"
693 do_test shell1-3.24.2 {
694 catchcmd "test.db" ".tables FOO"
696 do_test shell1-3.24.3 {
698 catchcmd "test.db" ".tables FOO BAD"
701 # .timeout MS Try opening locked tables for MS milliseconds
702 do_test shell1-3.25.1 {
703 catchcmd "test.db" ".timeout"
705 do_test shell1-3.25.2 {
706 catchcmd "test.db" ".timeout zzz"
707 # this should be treated the same as a '0' timeout
709 do_test shell1-3.25.3 {
710 catchcmd "test.db" ".timeout 1"
712 do_test shell1-3.25.4 {
714 catchcmd "test.db" ".timeout 1 BAD"
717 # .width NUM NUM ... Set column widths for "column" mode
718 do_test shell1-3.26.1 {
719 catchcmd "test.db" ".width"
721 do_test shell1-3.26.2 {
722 catchcmd "test.db" ".width xxx"
723 # this should be treated the same as a '0' width for col 1
725 do_test shell1-3.26.3 {
726 catchcmd "test.db" ".width xxx yyy"
727 # this should be treated the same as a '0' width for col 1 and 2
729 do_test shell1-3.26.4 {
730 catchcmd "test.db" ".width 1 1"
731 # this should be treated the same as a '1' width for col 1 and 2
733 do_test shell1-3.26.5 {
734 catchcmd "test.db" ".mode column\n.header off\n.width 10 -10\nSELECT 'abcdefg', 123456;"
735 # this should be treated the same as a '1' width for col 1 and 2
736 } {0 {abcdefg 123456}}
737 do_test shell1-3.26.6 {
738 catchcmd "test.db" ".mode column\n.header off\n.width -10 10\nSELECT 'abcdefg', 123456;"
739 # this should be treated the same as a '1' width for col 1 and 2
740 } {0 { abcdefg 123456 }}
743 # .timer ON|OFF Turn the CPU timer measurement on or off
744 do_test shell1-3.27.1 {
745 catchcmd "test.db" ".timer"
746 } {1 {Usage: .timer on|off}}
747 do_test shell1-3.27.2 {
748 catchcmd "test.db" ".timer ON"
750 do_test shell1-3.27.3 {
751 catchcmd "test.db" ".timer OFF"
753 do_test shell1-3.27.4 {
755 catchcmd "test.db" ".timer OFF BAD"
756 } {1 {Usage: .timer on|off}}
758 do_test shell1-3-28.1 {
760 ".log stdout\nSELECT coalesce(sqlite_log(123,'hello'),'456');"
761 } "0 {(123) hello\n456}"
763 do_test shell1-3-29.1 {
764 catchcmd "test.db" ".print this is a test"
765 } {0 {this is a test}}
767 # dot-command argument quoting
768 do_test shell1-3-30.1 {
769 catchcmd {test.db} {.print "this\"is'a\055test" 'this\"is\\a\055test'}
770 } {0 {this"is'a-test this\"is\\a\055test}}
771 do_test shell1-3-31.1 {
772 catchcmd {test.db} {.print "this\nis\ta\\test" 'this\nis\ta\\test'}
773 } [list 0 "this\nis\ta\\test this\\nis\\ta\\\\test"]
776 # Test the output of the ".dump" command
783 PRAGMA encoding=UTF16;
785 INSERT INTO t1 VALUES(null), (''), (1), (2.25), ('hello'), (x'807f');
786 CREATE TABLE t3(x,y);
787 INSERT INTO t3 VALUES(1,null), (2,''), (3,1),
788 (4,2.25), (5,'hello'), (6,x'807f');
790 catchcmd test.db {.dump}
791 } {0 {PRAGMA foreign_keys=OFF;
794 INSERT INTO t1 VALUES(NULL);
795 INSERT INTO t1 VALUES('');
796 INSERT INTO t1 VALUES(1);
797 INSERT INTO t1 VALUES(2.25);
798 INSERT INTO t1 VALUES('hello');
799 INSERT INTO t1 VALUES(X'807f');
800 CREATE TABLE t3(x,y);
801 INSERT INTO t3 VALUES(1,NULL);
802 INSERT INTO t3 VALUES(2,'');
803 INSERT INTO t3 VALUES(3,1);
804 INSERT INTO t3 VALUES(4,2.25);
805 INSERT INTO t3 VALUES(5,'hello');
806 INSERT INTO t3 VALUES(6,X'807f');
812 # The --preserve-rowids option to .dump
814 do_test shell1-4.1.1 {
815 catchcmd test.db {.dump --preserve-rowids}
816 } {0 {PRAGMA foreign_keys=OFF;
819 INSERT INTO t1(rowid,x) VALUES(1,NULL);
820 INSERT INTO t1(rowid,x) VALUES(2,'');
821 INSERT INTO t1(rowid,x) VALUES(3,1);
822 INSERT INTO t1(rowid,x) VALUES(4,2.25);
823 INSERT INTO t1(rowid,x) VALUES(5,'hello');
824 INSERT INTO t1(rowid,x) VALUES(6,X'807f');
825 CREATE TABLE t3(x,y);
826 INSERT INTO t3(rowid,x,y) VALUES(1,1,NULL);
827 INSERT INTO t3(rowid,x,y) VALUES(2,2,'');
828 INSERT INTO t3(rowid,x,y) VALUES(3,3,1);
829 INSERT INTO t3(rowid,x,y) VALUES(4,4,2.25);
830 INSERT INTO t3(rowid,x,y) VALUES(5,5,'hello');
831 INSERT INTO t3(rowid,x,y) VALUES(6,6,X'807f');
834 # If the table contains an INTEGER PRIMARY KEY, do not record a separate
835 # rowid column in the output.
837 do_test shell1-4.1.2 {
842 CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
843 INSERT INTO t1 VALUES(1,null), (2,''), (3,1),
844 (4,2.25), (5,'hello'), (6,x'807f');
846 catchcmd test2.db {.dump --preserve-rowids}
847 } {0 {PRAGMA foreign_keys=OFF;
849 CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
850 INSERT INTO t1 VALUES(1,NULL);
851 INSERT INTO t1 VALUES(2,'');
852 INSERT INTO t1 VALUES(3,1);
853 INSERT INTO t1 VALUES(4,2.25);
854 INSERT INTO t1 VALUES(5,'hello');
855 INSERT INTO t1 VALUES(6,X'807f');
858 # Verify that the table named [table] is correctly quoted and that
859 # an INTEGER PRIMARY KEY DESC is not an alias for the rowid.
861 do_test shell1-4.1.3 {
866 CREATE TABLE [table](x INTEGER PRIMARY KEY DESC, y);
867 INSERT INTO [table] VALUES(1,null), (12,''), (23,1),
868 (34,2.25), (45,'hello'), (56,x'807f');
870 catchcmd test2.db {.dump --preserve-rowids}
871 } {0 {PRAGMA foreign_keys=OFF;
873 CREATE TABLE [table](x INTEGER PRIMARY KEY DESC, y);
874 INSERT INTO "table"(rowid,x,y) VALUES(1,1,NULL);
875 INSERT INTO "table"(rowid,x,y) VALUES(2,12,'');
876 INSERT INTO "table"(rowid,x,y) VALUES(3,23,1);
877 INSERT INTO "table"(rowid,x,y) VALUES(4,34,2.25);
878 INSERT INTO "table"(rowid,x,y) VALUES(5,45,'hello');
879 INSERT INTO "table"(rowid,x,y) VALUES(6,56,X'807f');
882 # Do not record rowids for a WITHOUT ROWID table. Also check correct quoting
883 # of table names that contain odd characters.
885 do_test shell1-4.1.4 {
890 CREATE TABLE [ta<>ble](x INTEGER PRIMARY KEY, y) WITHOUT ROWID;
891 INSERT INTO [ta<>ble] VALUES(1,null), (12,''), (23,1),
892 (34,2.25), (45,'hello'), (56,x'807f');
894 catchcmd test2.db {.dump --preserve-rowids}
895 } {0 {PRAGMA foreign_keys=OFF;
897 CREATE TABLE [ta<>ble](x INTEGER PRIMARY KEY, y) WITHOUT ROWID;
898 INSERT INTO "ta<>ble" VALUES(1,NULL);
899 INSERT INTO "ta<>ble" VALUES(12,'');
900 INSERT INTO "ta<>ble" VALUES(23,1);
901 INSERT INTO "ta<>ble" VALUES(34,2.25);
902 INSERT INTO "ta<>ble" VALUES(45,'hello');
903 INSERT INTO "ta<>ble" VALUES(56,X'807f');
906 # Do not record rowids if the rowid is inaccessible
908 do_test shell1-4.1.5 {
913 CREATE TABLE t1(_ROWID_,rowid,oid);
914 INSERT INTO t1 VALUES(1,null,'alpha'), (12,'',99), (23,1,x'b0b1b2');
916 catchcmd test2.db {.dump --preserve-rowids}
917 } {0 {PRAGMA foreign_keys=OFF;
919 CREATE TABLE t1(_ROWID_,rowid,oid);
920 INSERT INTO t1 VALUES(1,NULL,'alpha');
921 INSERT INTO t1 VALUES(12,'',99);
922 INSERT INTO t1 VALUES(23,1,X'b0b1b2');
927 do_test shell1-4.1.6 {
932 CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
933 INSERT INTO t1 VALUES(1,null), (2,''), (3,1),
934 (4,2.25), (5,'hello'), (6,x'807f');
936 catchcmd test2.db {.dump --preserve-rowids}
937 } {1 {The --preserve-rowids option is not compatible with SQLITE_OMIT_VIRTUALTABLE}}
942 # Test the output of ".mode insert"
944 do_test shell1-4.2.1 {
945 catchcmd test.db ".mode insert t1\nselect * from t1;"
946 } {0 {INSERT INTO t1 VALUES(NULL);
947 INSERT INTO t1 VALUES('');
948 INSERT INTO t1 VALUES(1);
949 INSERT INTO t1 VALUES(2.25);
950 INSERT INTO t1 VALUES('hello');
951 INSERT INTO t1 VALUES(X'807f');}}
953 # Test the output of ".mode insert" with headers
955 do_test shell1-4.2.2 {
956 catchcmd test.db ".mode insert t1\n.headers on\nselect * from t1;"
957 } {0 {INSERT INTO t1(x) VALUES(NULL);
958 INSERT INTO t1(x) VALUES('');
959 INSERT INTO t1(x) VALUES(1);
960 INSERT INTO t1(x) VALUES(2.25);
961 INSERT INTO t1(x) VALUES('hello');
962 INSERT INTO t1(x) VALUES(X'807f');}}
964 # Test the output of ".mode insert"
966 do_test shell1-4.2.3 {
967 catchcmd test.db ".mode insert t3\nselect * from t3;"
968 } {0 {INSERT INTO t3 VALUES(1,NULL);
969 INSERT INTO t3 VALUES(2,'');
970 INSERT INTO t3 VALUES(3,1);
971 INSERT INTO t3 VALUES(4,2.25);
972 INSERT INTO t3 VALUES(5,'hello');
973 INSERT INTO t3 VALUES(6,X'807f');}}
975 # Test the output of ".mode insert" with headers
977 do_test shell1-4.2.4 {
978 catchcmd test.db ".mode insert t3\n.headers on\nselect * from t3;"
979 } {0 {INSERT INTO t3(x,y) VALUES(1,NULL);
980 INSERT INTO t3(x,y) VALUES(2,'');
981 INSERT INTO t3(x,y) VALUES(3,1);
982 INSERT INTO t3(x,y) VALUES(4,2.25);
983 INSERT INTO t3(x,y) VALUES(5,'hello');
984 INSERT INTO t3(x,y) VALUES(6,X'807f');}}
986 # Test the output of ".mode tcl"
993 PRAGMA encoding=UTF8;
995 INSERT INTO t1 VALUES(null), (''), (1), (2.25), ('hello'), (x'807f');
997 catchcmd test.db ".mode tcl\nselect * from t1;"
1005 # Test the output of ".mode tcl" with multiple columns
1007 do_test shell1-4.4 {
1009 CREATE TABLE t2(x,y);
1010 INSERT INTO t2 VALUES(null, ''), (1, 2.25), ('hello', x'807f');
1012 catchcmd test.db ".mode tcl\nselect * from t2;"
1015 "hello" "\200\177"}}
1017 # Test the output of ".mode tcl" with ".nullvalue"
1019 do_test shell1-4.5 {
1020 catchcmd test.db ".mode tcl\n.nullvalue NULL\nselect * from t2;"
1023 "hello" "\200\177"}}
1025 # Test the output of ".mode tcl" with Tcl reserved characters
1027 do_test shell1-4.6 {
1029 CREATE TABLE tcl1(x);
1030 INSERT INTO tcl1 VALUES('"'), ('['), (']'), ('\{'), ('\}'), (';'), ('$');
1032 foreach {x y} [catchcmd test.db ".mode tcl\nselect * from tcl1;"] break
1033 list $x $y [llength $y]
1042 # Test the output of ".mode quote"
1044 do_test shell1-4.7 {
1045 catchcmd test.db ".mode quote\nselect x'0123456789ABCDEF';"
1046 } {0 X'0123456789abcdef'}
1048 # Test using arbitrary byte data with the shell via standard input/output.
1050 do_test shell1-5.0 {
1052 # NOTE: Skip NUL byte because it appears to be incompatible with command
1053 # shell argument parsing.
1055 for {set i 1} {$i < 256} {incr i} {
1057 # NOTE: Due to how the Tcl [exec] command works (i.e. where it treats
1058 # command channels opened for it as textual ones), the carriage
1059 # return character (and on Windows, the end-of-file character)
1060 # cannot be used here.
1062 if {$i==0x0D || ($tcl_platform(platform)=="windows" && $i==0x1A)} {
1065 # Tcl 8.7 maps 0x80 through 0x9f into valid UTF8. So skip those tests.
1067 if {$i<=0x9F || $tcl_version>=9.0} continue
1068 if {$tcl_platform(platform)=="windows"} continue
1070 if {$i>=0xE0 && $tcl_platform(os)=="OpenBSD"} continue
1071 if {$i>=0xE0 && $i<=0xEF && $tcl_platform(os)=="Linux"} continue
1072 set hex [format %02X $i]
1073 set char [subst \\x$hex]; set oldChar $char
1075 if {$tcl_platform(platform)=="windows"} {
1077 # NOTE: On Windows, we need to escape all the whitespace characters,
1078 # the alarm (\a) character, and those with special meaning to
1079 # the SQLite shell itself.
1082 \a \\a \b \\b \t \\t \n \\n \v \\v \f \\f \r \\r \
1083 " " "\" \"" \" \\\" \\ \\\\]
1086 # NOTE: On Unix, we need to escape most of the whitespace characters
1087 # and those with special meaning to the SQLite shell itself.
1088 # The alarm (\a), backspace (\b), and carriage-return (\r)
1089 # characters do not appear to require escaping on Unix. For
1090 # the alarm and backspace characters, this is probably due to
1091 # differences in the command shell. For the carriage-return,
1092 # it is probably due to differences in how Tcl handles command
1093 # channel end-of-line translations.
1096 \t \\t \n \\n \v \\v \f \\f \
1097 " " "\" \"" \" \\\" \\ \\\\]
1099 set char [string map $escapes $char]
1100 set x [catchcmdex test.db ".print \"$char\"\n"]
1101 set code [lindex $x 0]
1102 set res [lindex $x 1]
1104 error "failed with error: $res"
1106 if {$res ne "$oldChar\n"} {
1107 if {[llength $res] > 0} {
1108 set got [format %02X [scan $res %c]]
1112 error "failed with byte $hex mismatch, got $got"
1117 # These test cases do not work on MinGW
1120 # The string used here is the word "test" in Chinese.
1121 # In UTF-8, it is encoded as: \xE6\xB5\x8B\xE8\xAF\x95
1122 set test \u6D4B\u8BD5
1124 do_test shell1-6.0 {
1125 set fileName $test; append fileName .db
1126 catch {forcedelete $fileName}
1127 set x [catchcmdex $fileName "CREATE TABLE t1(x);\n.schema\n"]
1128 set code [lindex $x 0]
1129 set res [string trim [lindex $x 1]]
1131 error "failed with error: $res"
1133 if {$res ne "CREATE TABLE t1(x);"} {
1134 error "failed with mismatch: $res"
1136 if {![file exists $fileName]} {
1137 error "file \"$fileName\" (Unicode) does not exist"
1139 forcedelete $fileName
1142 do_test shell1-6.1 {
1143 catch {forcedelete test3.db}
1144 set x [catchcmdex test3.db \
1145 "CREATE TABLE [encoding convertto utf-8 $test](x);\n.schema\n"]
1146 set code [lindex $x 0]
1147 set res [string trim [lindex $x 1]]
1149 error "failed with error: $res"
1151 if {$res ne "CREATE TABLE ${test}(x);"} {
1152 error "failed with mismatch: $res"
1154 forcedelete test3.db
1159 forcedelete test.db test.db-journal test.db-wal
1162 # The shell tool ".schema" command uses virtual table "pragma_database_list"
1166 do_test shell1-7.1.1 {
1168 CREATE TABLE Z (x TEXT PRIMARY KEY);
1169 CREATE TABLE _ (x TEXT PRIMARY KEY);
1170 CREATE TABLE YY (x TEXT PRIMARY KEY);
1171 CREATE TABLE __ (x TEXT PRIMARY KEY);
1172 CREATE TABLE WWW (x TEXT PRIMARY KEY);
1173 CREATE TABLE ___ (x TEXT PRIMARY KEY);
1176 do_test shell1-7.1.2 {
1177 catchcmd "test.db" ".schema _"
1178 } {0 {CREATE TABLE Z (x TEXT PRIMARY KEY);
1179 CREATE TABLE _ (x TEXT PRIMARY KEY);}}
1180 do_test shell1-7.1.3 {
1181 catchcmd "test.db" ".schema \"\\\\_\""
1182 } {0 {CREATE TABLE _ (x TEXT PRIMARY KEY);}}
1183 do_test shell1-7.1.4 {
1184 catchcmd "test.db" ".schema __"
1185 } {0 {CREATE TABLE YY (x TEXT PRIMARY KEY);
1186 CREATE TABLE __ (x TEXT PRIMARY KEY);}}
1187 do_test shell1-7.1.5 {
1188 catchcmd "test.db" ".schema \"\\\\_\\\\_\""
1189 } {0 {CREATE TABLE __ (x TEXT PRIMARY KEY);}}
1190 do_test shell1-7.1.6 {
1191 catchcmd "test.db" ".schema ___"
1192 } {0 {CREATE TABLE WWW (x TEXT PRIMARY KEY);
1193 CREATE TABLE ___ (x TEXT PRIMARY KEY);}}
1194 do_test shell1-7.1.7 {
1195 catchcmd "test.db" ".schema \"\\\\_\\\\_\\\\_\""
1196 } {0 {CREATE TABLE ___ (x TEXT PRIMARY KEY);}}
1200 # Test case for the ieee754 and decimal extensions in the shell.
1201 # See the "floatingpoint.html" file in the documentation for more
1204 do_test shell1-8.1 {
1205 catchcmd ":memory:" {
1206 -- The pow2 table will hold all the necessary powers of two.
1207 CREATE TABLE pow2(x INTEGER PRIMARY KEY, v TEXT);
1208 WITH RECURSIVE c(x,v) AS (
1211 SELECT x+1, decimal_mul(v,'2') FROM c WHERE x+1<=971
1212 ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
1213 WITH RECURSIVE c(x,v) AS (
1216 SELECT x-1, decimal_mul(v,'0.5') FROM c WHERE x-1>=-1075
1217 ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
1219 -- This query finds the decimal representation of each value in the "c" table.
1220 WITH c(n) AS (VALUES(47.49))
1221 ----XXXXX----------- Replace with whatever you want
1222 SELECT decimal_mul(ieee754_mantissa(c.n),pow2.v)
1223 FROM pow2, c WHERE pow2.x=ieee754_exponent(c.n);
1225 } {0 47.49000000000000198951966012828052043914794921875}
1226 do_test shell1-8.2 {
1229 SELECT ieee754(47.49) AS x;
1231 } {0 {┌───────────────────────────────┐
1233 ├───────────────────────────────┤
1234 │ ieee754(6683623321994527,-47) │
1235 └───────────────────────────────┘}}
1236 do_test shell1-8.3 {
1237 catchcmd ":memory: --box" {
1238 select ieee754(6683623321994527,-47) as x;
1245 do_test shell1-8.4 {
1246 catchcmd ":memory: --table" {SELECT ieee754_mantissa(47.49) AS M, ieee754_exponent(47.49) AS E;}
1247 } {0 {+------------------+-----+
1249 +------------------+-----+
1250 | 6683623321994527 | -47 |
1251 +------------------+-----+}}
1252 do_test shell1-8.5 {
1253 catchcmd ":memory: --box" {
1254 create table t(a text, b int);
1255 insert into t values ('too long for one line', 1), ('shorter', NULL);
1260 } {0 {┌────────────┬────────────┐
1262 ├────────────┼────────────┤
1266 ├────────────┼────────────┤
1268 └────────────┴────────────┘}}
1270 #----------------------------------------------------------------------------
1271 # Test cases shell1-9.*: Basic test that "dot" commands and SQL intermix ok.
1273 do_test shell1-9.1 {
1277 x */ select 1,2; --x
1282 select 2,1; select 3,4;
1288 #----------------------------------------------------------------------------
1289 # Test cases shell1-10.*: Test that certain static extensions are there.
1291 do_test shell1-10.1 {
1295 select base64(base64(cast('digity-doo' as blob))),
1296 base85(base85(cast('digity-doo' as blob)));
1298 } {0 digity-doo|digity-doo}