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.
21 # shell1-{4-8}.*: Test various "dot" commands's functionality.
22 # shell1-9.*: Basic test that "dot" commands and SQL intermix ok.
24 set testdir [file dirname $argv0]
25 source $testdir/tester.tcl
26 set CLI [test_cli_invocation]
28 forcedelete test.db test.db-journal test.db-wal
31 #----------------------------------------------------------------------------
32 # Test cases shell1-1.*: Basic command line option handling.
36 do_test shell1-1.1.1 {
37 set res [catchcmd "-bad test.db" ""]
38 set rc [lindex $res 0]
40 [regexp {Error: unknown option: -bad} $res]
42 do_test shell1-1.1.1b {
43 set res [catchcmd "test.db -bad" ""]
44 set rc [lindex $res 0]
46 [regexp {Error: unknown option: -bad} $res]
48 # error on extra options
49 do_test shell1-1.1.2 {
50 catchcmd "test.db \"select+3\" \"select+4\"" ""
53 # error on extra options
54 do_test shell1-1.1.3 {
55 catchcmd "test.db FOO test.db BAD" ".quit"
56 } {/1 .Error: in prepare, near "FOO": syntax error*/}
59 do_test shell1-1.2.1 {
60 set res [catchcmd "-help test.db" ""]
61 set rc [lindex $res 0]
63 [regexp {Usage} $res] \
64 [regexp {\-init} $res] \
65 [regexp {\-version} $res]
68 # -init filename read/process named file
73 do_test shell1-1.3.1 {
74 catchcmd "-init FOO test.db" ""
76 do_test shell1-1.3.2 {
77 catchcmd "-init FOO test.db .quit BAD" ""
79 do_test shell1-1.3.3 {
80 catchcmd "-init FOO test.db BAD .quit" ""
81 } {/1 .Error: in prepare, near "BAD": syntax error*/}
83 # -echo print commands before execution
84 do_test shell1-1.4.1 {
85 catchcmd "-echo test.db" ""
88 # -[no]header turn headers on or off
89 do_test shell1-1.5.1 {
90 catchcmd "-header test.db" ""
92 do_test shell1-1.5.2 {
93 catchcmd "-noheader test.db" ""
96 # -bail stop after hitting an error
97 do_test shell1-1.6.1 {
98 catchcmd "-bail test.db" ""
101 # -interactive force interactive I/O
102 do_test shell1-1.7.1 {
103 set res [catchcmd "-interactive test.db" ".quit"]
104 set rc [lindex $res 0]
106 [regexp {SQLite version} $res] \
107 [regexp {Enter ".help" for usage hints} $res]
110 # -batch force batch I/O
111 do_test shell1-1.8.1 {
112 catchcmd "-batch test.db" ""
115 # -column set output mode to 'column'
116 do_test shell1-1.9.1 {
117 catchcmd "-column test.db" ""
120 # -csv set output mode to 'csv'
121 do_test shell1-1.10.1 {
122 catchcmd "-csv test.db" ""
125 # -html set output mode to HTML
126 do_test shell1-1.11.1 {
127 catchcmd "-html test.db" ""
130 # -line set output mode to 'line'
131 do_test shell1-1.12.1 {
132 catchcmd "-line test.db" ""
135 # -list set output mode to 'list'
136 do_test shell1-1.13.1 {
137 catchcmd "-list test.db" ""
140 # -separator 'x' set output field separator (|)
141 do_test shell1-1.14.1 {
142 catchcmd "-separator 'x' test.db" ""
144 do_test shell1-1.14.2 {
145 catchcmd "-separator x test.db" ""
147 do_test shell1-1.14.3 {
148 set res [catchcmd "-separator" ""]
149 set rc [lindex $res 0]
151 [regexp {Error: missing argument to -separator} $res]
154 # -stats print memory stats before each finalize
155 do_test shell1-1.14b.1 {
156 catchcmd "-stats test.db" ""
159 # -nullvalue 'text' set text string for NULL values
160 do_test shell1-1.15.1 {
161 catchcmd "-nullvalue 'x' test.db" ""
163 do_test shell1-1.15.2 {
164 catchcmd "-nullvalue x test.db" ""
166 do_test shell1-1.15.3 {
167 set res [catchcmd "-nullvalue" ""]
168 set rc [lindex $res 0]
170 [regexp {Error: missing argument to -nullvalue} $res]
173 # -version show SQLite version
174 do_test shell1-1.16.1 {
175 set x [catchcmd "-version test.db" ""]
176 } {/3.[0-9.]+ 20\d\d-[01]\d-\d\d \d\d:\d\d:\d\d [0-9a-f]+/}
178 # Handle no-more-options option
180 do_test shell1-1.17.1 {
181 catchcmd {-- --db "CREATE TABLE T(c1);"}
183 do_test shell1-1.17.2 {
184 catchcmd {-- --db "SELECT name from sqlite_schema;"}
188 #----------------------------------------------------------------------------
189 # Test cases shell1-2.*: Basic "dot" command token parsing.
192 # check first token handling
193 do_test shell1-2.1.1 {
194 catchcmd "test.db" ".foo"
195 } {1 {Error: unknown command or invalid arguments: "foo". Enter ".help" for help}}
196 do_test shell1-2.1.2 {
197 catchcmd "test.db" ".\"foo OFF\""
198 } {1 {Error: unknown command or invalid arguments: "foo OFF". Enter ".help" for help}}
199 do_test shell1-2.1.3 {
200 catchcmd "test.db" ".\'foo OFF\'"
201 } {1 {Error: unknown command or invalid arguments: "foo OFF". Enter ".help" for help}}
204 do_test shell1-2.2.1 {
205 catchcmd "test.db" ".\"foo OFF"
206 } {1 {Error: unknown command or invalid arguments: "foo OFF". Enter ".help" for help}}
207 do_test shell1-2.2.2 {
208 catchcmd "test.db" ".\'foo OFF"
209 } {1 {Error: unknown command or invalid arguments: "foo OFF". Enter ".help" for help}}
210 do_test shell1-2.2.3 {
211 catchcmd "test.db" ".explain \"OFF"
213 do_test shell1-2.2.4 {
214 catchcmd "test.db" ".explain \'OFF"
216 do_test shell1-2.2.5 {
217 catchcmd "test.db" ".mode \"insert FOO"
218 } {1 {Error: mode should be one of: ascii box column csv html insert json line list markdown qbox quote table tabs tcl}}
219 do_test shell1-2.2.6 {
220 catchcmd "test.db" ".mode \'insert FOO"
221 } {1 {Error: mode should be one of: ascii box column csv html insert json line list markdown qbox quote table tabs tcl}}
223 # check multiple tokens, and quoted tokens
224 do_test shell1-2.3.1 {
225 catchcmd "test.db" ".explain 1"
227 do_test shell1-2.3.2 {
228 catchcmd "test.db" ".explain on"
230 do_test shell1-2.3.3 {
231 catchcmd "test.db" ".explain \"1 2 3\""
232 } {1 {ERROR: Not a boolean value: "1 2 3". Assuming "no".}}
233 do_test shell1-2.3.4 {
234 catchcmd "test.db" ".explain \"OFF\""
236 do_test shell1-2.3.5 {
237 catchcmd "test.db" ".\'explain\' \'OFF\'"
239 do_test shell1-2.3.6 {
240 catchcmd "test.db" ".explain \'OFF\'"
242 do_test shell1-2.3.7 {
243 catchcmd "test.db" ".\'explain\' \'OFF\'"
246 # check quoted args are unquoted
247 do_test shell1-2.4.1 {
248 catchcmd "test.db" ".mode FOO"
249 } {1 {Error: mode should be one of: ascii box column csv html insert json line list markdown qbox quote table tabs tcl}}
250 do_test shell1-2.4.2 {
251 catchcmd "test.db" ".mode csv"
253 do_test shell1-2.4.2 {
254 catchcmd "test.db" ".mode \"csv\""
257 # check that certain quoted arg escapes work
258 do_test shell1-2.5.1 {
259 catchcmd ":memory:" ".print \"\\060\\077 \\x3f\\x30 \\a\\t\""
260 } [list 0 "0? ?0 \a\t"]
263 #----------------------------------------------------------------------------
264 # Test cases shell1-3.*: Basic test that "dot" command can be called.
267 # .backup ?DB? FILE Backup DB (default "main") to FILE
268 do_test shell1-3.1.1 {
269 catchcmd "test.db" ".backup"
270 } {1 {missing FILENAME argument on .backup}}
272 do_test shell1-3.1.2 {
273 catchcmd "test.db" ".backup FOO"
275 do_test shell1-3.1.3 {
276 catchcmd "test.db" ".backup FOO BAR"
277 } {1 {Error: unknown database FOO}}
278 do_test shell1-3.1.4 {
280 catchcmd "test.db" ".backup FOO BAR BAD"
281 } {1 {Usage: .backup ?DB? ?OPTIONS? FILENAME}}
283 # .bail ON|OFF Stop after hitting an error. Default OFF
284 do_test shell1-3.2.1 {
285 catchcmd "test.db" ".bail"
286 } {1 {Usage: .bail on|off}}
287 do_test shell1-3.2.2 {
288 catchcmd "test.db" ".bail ON"
290 do_test shell1-3.2.3 {
291 catchcmd "test.db" ".bail OFF"
293 do_test shell1-3.2.4 {
295 catchcmd "test.db" ".bail OFF BAD"
296 } {1 {Usage: .bail on|off}}
299 # .databases List names and files of attached databases
300 do_test shell1-3.3.1 {
301 catchcmd "-csv test.db" ".databases"
302 } "/0.+main.+[string map {/ ".{1,2}"} [string range [get_pwd] 0 10]].*/"
303 do_test shell1-3.3.2 {
304 # extra arguments ignored
305 catchcmd "test.db" ".databases BAD"
306 } "/0.+main.+[string map {/ ".{1,2}"} [string range [get_pwd] 0 10]].*/"
309 # .dump ?TABLE? ... Dump the database in an SQL text format
310 # If TABLE specified, only dump tables matching
311 # LIKE pattern TABLE.
312 do_test shell1-3.4.1 {
313 set res [catchcmd "test.db" ".dump"]
314 list [regexp {BEGIN TRANSACTION;} $res] \
315 [regexp {COMMIT;} $res]
317 do_test shell1-3.4.2 {
318 set res [catchcmd "test.db" ".dump FOO"]
319 list [regexp {BEGIN TRANSACTION;} $res] \
320 [regexp {COMMIT;} $res]
322 # The .dump command now accepts multiple arguments
323 #do_test shell1-3.4.3 {
324 # # too many arguments
325 # catchcmd "test.db" ".dump FOO BAD"
326 #} {1 {Usage: .dump ?--preserve-rowids? ?--newlines? ?LIKE-PATTERN?}}
328 # .echo ON|OFF Turn command echo on or off
329 do_test shell1-3.5.1 {
330 catchcmd "test.db" ".echo"
331 } {1 {Usage: .echo on|off}}
332 do_test shell1-3.5.2 {
333 catchcmd "test.db" ".echo ON"
335 do_test shell1-3.5.3 {
336 catchcmd "test.db" ".echo OFF"
338 do_test shell1-3.5.4 {
340 catchcmd "test.db" ".echo OFF BAD"
341 } {1 {Usage: .echo on|off}}
343 # .exit Exit this program
344 do_test shell1-3.6.1 {
345 catchcmd "test.db" ".exit"
348 # .explain ON|OFF Turn output mode suitable for EXPLAIN on or off.
349 do_test shell1-3.7.1 {
350 catchcmd "test.db" ".explain"
351 # explain is the exception to the booleans. without an option, it turns it on.
353 do_test shell1-3.7.2 {
354 catchcmd "test.db" ".explain ON"
356 do_test shell1-3.7.3 {
357 catchcmd "test.db" ".explain OFF"
359 do_test shell1-3.7.4 {
360 # extra arguments ignored
361 catchcmd "test.db" ".explain OFF BAD"
365 # .header(s) ON|OFF Turn display of headers on or off
366 do_test shell1-3.9.1 {
367 catchcmd "test.db" ".header"
368 } {1 {Usage: .headers on|off}}
369 do_test shell1-3.9.2 {
370 catchcmd "test.db" ".header ON"
372 do_test shell1-3.9.3 {
373 catchcmd "test.db" ".header OFF"
375 do_test shell1-3.9.4 {
377 catchcmd "test.db" ".header OFF BAD"
378 } {1 {Usage: .headers on|off}}
380 do_test shell1-3.9.5 {
381 catchcmd "test.db" ".headers"
382 } {1 {Usage: .headers on|off}}
383 do_test shell1-3.9.6 {
384 catchcmd "test.db" ".headers ON"
386 do_test shell1-3.9.7 {
387 catchcmd "test.db" ".headers OFF"
389 do_test shell1-3.9.8 {
391 catchcmd "test.db" ".headers OFF BAD"
392 } {1 {Usage: .headers on|off}}
394 # .help Show this message
395 do_test shell1-3.10.1 {
396 set res [catchcmd "test.db" ".help"]
397 # look for a few of the possible help commands
398 list [regexp {.help} $res] \
399 [regexp {.quit} $res] \
400 [regexp {.show} $res]
402 do_test shell1-3.10.2 {
403 # we allow .help to take extra args (it is help after all)
404 set res [catchcmd "test.db" ".help *"]
405 # look for a few of the possible help commands
406 list [regexp {.help} $res] \
407 [regexp {.quit} $res] \
408 [regexp {.show} $res]
411 # .import FILE TABLE Import data from FILE into TABLE
412 do_test shell1-3.11.1 {
413 catchcmd "test.db" ".import"
414 } {/1 .ERROR: missing FILE argument.*/}
415 do_test shell1-3.11.2 {
416 catchcmd "test.db" ".import FOO"
417 } {/1 .ERROR: missing TABLE argument.*/}
418 do_test shell1-3.11.3 {
420 catchcmd "test.db" ".import FOO BAR BAD"
421 } {/1 .ERROR: extra argument: "BAD".*./}
423 # .indexes ?TABLE? Show names of all indexes
424 # If TABLE specified, only show indexes for tables
425 # matching LIKE pattern TABLE.
426 do_test shell1-3.12.1 {
427 catchcmd "test.db" ".indexes"
429 do_test shell1-3.12.2 {
430 catchcmd "test.db" ".indexes FOO"
432 do_test shell1-3.12.2-legacy {
433 catchcmd "test.db" ".indices FOO"
435 do_test shell1-3.12.3 {
437 catchcmd "test.db" ".indexes FOO BAD"
438 } {1 {Usage: .indexes ?LIKE-PATTERN?}}
440 # .mode MODE ?TABLE? Set output mode where MODE is one of:
441 # ascii Columns/rows delimited by 0x1F and 0x1E
442 # csv Comma-separated values
443 # column Left-aligned columns. (See .width)
444 # html HTML <table> code
445 # insert SQL insert statements for TABLE
446 # line One value per line
447 # list Values delimited by .separator strings
448 # tabs Tab-separated values
449 # tcl TCL list elements
450 do_test shell1-3.13.1 {
451 catchcmd "test.db" ".mode"
452 } {0 {current output mode: list}}
453 do_test shell1-3.13.2 {
454 catchcmd "test.db" ".mode FOO"
455 } {1 {Error: mode should be one of: ascii box column csv html insert json line list markdown qbox quote table tabs tcl}}
456 do_test shell1-3.13.3 {
457 catchcmd "test.db" ".mode csv"
459 do_test shell1-3.13.4 {
460 catchcmd "test.db" ".mode column"
462 do_test shell1-3.13.5 {
463 catchcmd "test.db" ".mode html"
465 do_test shell1-3.13.6 {
466 catchcmd "test.db" ".mode insert"
468 do_test shell1-3.13.7 {
469 catchcmd "test.db" ".mode line"
471 do_test shell1-3.13.8 {
472 catchcmd "test.db" ".mode list"
474 do_test shell1-3.13.9 {
475 catchcmd "test.db" ".mode tabs"
477 do_test shell1-3.13.10 {
478 catchcmd "test.db" ".mode tcl"
480 do_test shell1-3.13.11 {
481 # extra arguments ignored
482 catchcmd "test.db" ".mode tcl BAD"
485 # .nullvalue STRING Print STRING in place of NULL values
486 do_test shell1-3.14.1 {
487 catchcmd "test.db" ".nullvalue"
488 } {1 {Usage: .nullvalue STRING}}
489 do_test shell1-3.14.2 {
490 catchcmd "test.db" ".nullvalue FOO"
492 do_test shell1-3.14.3 {
494 catchcmd "test.db" ".nullvalue FOO BAD"
495 } {1 {Usage: .nullvalue STRING}}
497 # .output FILENAME Send output to FILENAME
498 do_test shell1-3.15.1 {
499 catchcmd "test.db" ".output
502 do_test shell1-3.15.2 {
503 catchcmd "test.db" ".output FOO
506 SELECT readfile('FOO');"
509 do_test shell1-3.15.3 {
511 catchcmd "test.db" ".output FOO BAD"
512 } {1 {ERROR: extra parameter: "BAD". Usage:
513 .output ?FILE? Send output to FILE or stdout if FILE is omitted
514 If FILE begins with '|' then open it as a pipe.
516 --bom Prefix output with a UTF8 byte-order mark
517 -e Send output to the system text editor
518 -x Send output as CSV to a spreadsheet
519 child process exited abnormally}}
521 # .output stdout Send output to the screen
522 do_test shell1-3.16.1 {
523 catchcmd "test.db" ".output stdout"
525 do_test shell1-3.16.2 {
527 catchcmd "test.db" ".output stdout BAD"
528 } {1 {ERROR: extra parameter: "BAD". Usage:
529 .output ?FILE? Send output to FILE or stdout if FILE is omitted
530 If FILE begins with '|' then open it as a pipe.
532 --bom Prefix output with a UTF8 byte-order mark
533 -e Send output to the system text editor
534 -x Send output as CSV to a spreadsheet
535 child process exited abnormally}}
537 # .prompt MAIN CONTINUE Replace the standard prompts
538 do_test shell1-3.17.1 {
539 catchcmd "test.db" ".prompt"
541 do_test shell1-3.17.2 {
542 catchcmd "test.db" ".prompt FOO"
544 do_test shell1-3.17.3 {
545 catchcmd "test.db" ".prompt FOO BAR"
547 do_test shell1-3.17.4 {
549 catchcmd "test.db" ".prompt FOO BAR BAD"
552 # .quit Exit this program
553 do_test shell1-3.18.1 {
554 catchcmd "test.db" ".quit"
556 do_test shell1-3.18.2 {
558 catchcmd "test.db" ".quit BAD"
561 # .read FILENAME Execute SQL in FILENAME
562 do_test shell1-3.19.1 {
563 catchcmd "test.db" ".read"
564 } {1 {Usage: .read FILE}}
565 do_test shell1-3.19.2 {
567 catchcmd "test.db" ".read FOO"
568 } {1 {Error: cannot open "FOO"}}
569 do_test shell1-3.19.3 {
571 catchcmd "test.db" ".read FOO BAD"
572 } {1 {Usage: .read FILE}}
574 # .restore ?DB? FILE Restore content of DB (default "main") from FILE
575 do_test shell1-3.20.1 {
576 catchcmd "test.db" ".restore"
577 } {1 {Usage: .restore ?DB? FILE}}
578 do_test shell1-3.20.2 {
579 catchcmd "test.db" ".restore FOO"
581 do_test shell1-3.20.3 {
582 catchcmd "test.db" ".restore FOO BAR"
583 } {1 {Error: unknown database FOO}}
584 do_test shell1-3.20.4 {
586 catchcmd "test.db" ".restore FOO BAR BAD"
587 } {1 {Usage: .restore ?DB? FILE}}
590 # .schema ?TABLE? Show the CREATE statements
591 # If TABLE specified, only show tables matching
592 # LIKE pattern TABLE.
593 do_test shell1-3.21.1 {
594 catchcmd "test.db" ".schema"
596 do_test shell1-3.21.2 {
597 catchcmd "test.db" ".schema FOO"
599 do_test shell1-3.21.3 {
601 catchcmd "test.db" ".schema FOO BAD"
602 } {1 {Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?}}
604 do_test shell1-3.21.4 {
607 CREATE VIEW v2 AS SELECT x+1 AS y FROM t1;
608 CREATE VIEW v1 AS SELECT y+1 FROM v2;
610 catchcmd "test.db" ".schema"
611 } {0 {CREATE TABLE t1(x);
612 CREATE VIEW v2 AS SELECT x+1 AS y FROM t1
614 CREATE VIEW v1 AS SELECT y+1 FROM v2
617 catch {db eval {DROP VIEW v1; DROP VIEW v2; DROP TABLE t1;}}
620 # .separator STRING Change column separator used by output and .import
621 do_test shell1-3.22.1 {
622 catchcmd "test.db" ".separator"
623 } {1 {Usage: .separator COL ?ROW?}}
624 do_test shell1-3.22.2 {
625 catchcmd "test.db" ".separator FOO"
627 do_test shell1-3.22.3 {
628 catchcmd "test.db" ".separator ABC XYZ"
630 do_test shell1-3.22.4 {
632 catchcmd "test.db" ".separator FOO BAD BAD2"
633 } {1 {Usage: .separator COL ?ROW?}}
635 # .show Show the current values for various settings
636 do_test shell1-3.23.1 {
637 set res [catchcmd "test.db" ".show"]
638 list [regexp {echo:} $res] \
639 [regexp {explain:} $res] \
640 [regexp {headers:} $res] \
641 [regexp {mode:} $res] \
642 [regexp {nullvalue:} $res] \
643 [regexp {output:} $res] \
644 [regexp {colseparator:} $res] \
645 [regexp {rowseparator:} $res] \
646 [regexp {stats:} $res] \
647 [regexp {width:} $res]
648 } {1 1 1 1 1 1 1 1 1 1}
649 do_test shell1-3.23.2 {
651 catchcmd "test.db" ".show BAD"
654 # .stats ON|OFF Turn stats on or off
655 #do_test shell1-3.23b.1 {
656 # catchcmd "test.db" ".stats"
657 #} {1 {Usage: .stats on|off|stmt|vmstep}}
658 do_test shell1-3.23b.2 {
659 catchcmd "test.db" ".stats ON"
661 do_test shell1-3.23b.3 {
662 catchcmd "test.db" ".stats OFF"
664 do_test shell1-3.23b.4 {
666 catchcmd "test.db" ".stats OFF BAD"
667 } {1 {Usage: .stats ?on|off|stmt|vmstep?}}
669 # Ticket 7be932dfa60a8a6b3b26bcf7623ec46e0a403ddb 2018-06-07
670 # Adverse interaction between .stats and .eqp
672 do_test shell1-3.23b.5 {
673 catchcmd "test.db" [string map {"\n " "\n"} {
674 CREATE TEMP TABLE t1(x);
675 INSERT INTO t1 VALUES(1),(2);
682 # .tables ?TABLE? List names of tables
683 # If TABLE specified, only list tables matching
684 # LIKE pattern TABLE.
685 do_test shell1-3.24.1 {
686 catchcmd "test.db" ".tables"
688 do_test shell1-3.24.2 {
689 catchcmd "test.db" ".tables FOO"
691 do_test shell1-3.24.3 {
693 catchcmd "test.db" ".tables FOO BAD"
696 # .timeout MS Try opening locked tables for MS milliseconds
697 do_test shell1-3.25.1 {
698 catchcmd "test.db" ".timeout"
700 do_test shell1-3.25.2 {
701 catchcmd "test.db" ".timeout zzz"
702 # this should be treated the same as a '0' timeout
704 do_test shell1-3.25.3 {
705 catchcmd "test.db" ".timeout 1"
707 do_test shell1-3.25.4 {
709 catchcmd "test.db" ".timeout 1 BAD"
712 # .width NUM NUM ... Set column widths for "column" mode
713 do_test shell1-3.26.1 {
714 catchcmd "test.db" ".width"
716 do_test shell1-3.26.2 {
717 catchcmd "test.db" ".width xxx"
718 # this should be treated the same as a '0' width for col 1
720 do_test shell1-3.26.3 {
721 catchcmd "test.db" ".width xxx yyy"
722 # this should be treated the same as a '0' width for col 1 and 2
724 do_test shell1-3.26.4 {
725 catchcmd "test.db" ".width 1 1"
726 # this should be treated the same as a '1' width for col 1 and 2
728 do_test shell1-3.26.5 {
729 catchcmd "test.db" ".mode column\n.header off\n.width 10 -10\nSELECT 'abcdefg', 123456;"
730 # this should be treated the same as a '1' width for col 1 and 2
731 } {0 {abcdefg 123456}}
732 do_test shell1-3.26.6 {
733 catchcmd "test.db" ".mode column\n.header off\n.width -10 10\nSELECT 'abcdefg', 123456;"
734 # this should be treated the same as a '1' width for col 1 and 2
735 } {0 { abcdefg 123456 }}
738 # .timer ON|OFF Turn the CPU timer measurement on or off
739 do_test shell1-3.27.1 {
740 catchcmd "test.db" ".timer"
741 } {1 {Usage: .timer on|off}}
742 do_test shell1-3.27.2 {
743 catchcmd "test.db" ".timer ON"
745 do_test shell1-3.27.3 {
746 catchcmd "test.db" ".timer OFF"
748 do_test shell1-3.27.4 {
750 catchcmd "test.db" ".timer OFF BAD"
751 } {1 {Usage: .timer on|off}}
753 do_test shell1-3-28.1 {
755 ".log stdout\nSELECT coalesce(sqlite_log(123,'hello'),'456');"
756 } "0 {(123) hello\n456}"
758 do_test shell1-3-29.1 {
759 catchcmd "test.db" ".print this is a test"
760 } {0 {this is a test}}
762 # dot-command argument quoting
763 do_test shell1-3-30.1 {
764 catchcmd {test.db} {.print "this\"is'a\055test" 'this\"is\\a\055test'}
765 } {0 {this"is'a-test this\"is\\a\055test}}
766 do_test shell1-3-31.1 {
767 catchcmd {test.db} {.print "this\nis\ta\\test" 'this\nis\ta\\test'}
768 } [list 0 "this\nis\ta\\test this\\nis\\ta\\\\test"]
771 # Test the output of the ".dump" command
778 PRAGMA encoding=UTF16;
780 INSERT INTO t1 VALUES(null), (''), (1), (2.25), ('hello'), (x'807f');
781 CREATE TABLE t3(x,y);
782 INSERT INTO t3 VALUES(1,null), (2,''), (3,1),
783 (4,2.25), (5,'hello'), (6,x'807f');
785 catchcmd test.db {.dump}
786 } {0 {PRAGMA foreign_keys=OFF;
789 INSERT INTO t1 VALUES(NULL);
790 INSERT INTO t1 VALUES('');
791 INSERT INTO t1 VALUES(1);
792 INSERT INTO t1 VALUES(2.25);
793 INSERT INTO t1 VALUES('hello');
794 INSERT INTO t1 VALUES(X'807f');
795 CREATE TABLE t3(x,y);
796 INSERT INTO t3 VALUES(1,NULL);
797 INSERT INTO t3 VALUES(2,'');
798 INSERT INTO t3 VALUES(3,1);
799 INSERT INTO t3 VALUES(4,2.25);
800 INSERT INTO t3 VALUES(5,'hello');
801 INSERT INTO t3 VALUES(6,X'807f');
807 # The --preserve-rowids option to .dump
809 do_test shell1-4.1.1 {
810 catchcmd test.db {.dump --preserve-rowids}
811 } {0 {PRAGMA foreign_keys=OFF;
814 INSERT INTO t1(rowid,x) VALUES(1,NULL);
815 INSERT INTO t1(rowid,x) VALUES(2,'');
816 INSERT INTO t1(rowid,x) VALUES(3,1);
817 INSERT INTO t1(rowid,x) VALUES(4,2.25);
818 INSERT INTO t1(rowid,x) VALUES(5,'hello');
819 INSERT INTO t1(rowid,x) VALUES(6,X'807f');
820 CREATE TABLE t3(x,y);
821 INSERT INTO t3(rowid,x,y) VALUES(1,1,NULL);
822 INSERT INTO t3(rowid,x,y) VALUES(2,2,'');
823 INSERT INTO t3(rowid,x,y) VALUES(3,3,1);
824 INSERT INTO t3(rowid,x,y) VALUES(4,4,2.25);
825 INSERT INTO t3(rowid,x,y) VALUES(5,5,'hello');
826 INSERT INTO t3(rowid,x,y) VALUES(6,6,X'807f');
829 # If the table contains an INTEGER PRIMARY KEY, do not record a separate
830 # rowid column in the output.
832 do_test shell1-4.1.2 {
837 CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
838 INSERT INTO t1 VALUES(1,null), (2,''), (3,1),
839 (4,2.25), (5,'hello'), (6,x'807f');
841 catchcmd test2.db {.dump --preserve-rowids}
842 } {0 {PRAGMA foreign_keys=OFF;
844 CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
845 INSERT INTO t1 VALUES(1,NULL);
846 INSERT INTO t1 VALUES(2,'');
847 INSERT INTO t1 VALUES(3,1);
848 INSERT INTO t1 VALUES(4,2.25);
849 INSERT INTO t1 VALUES(5,'hello');
850 INSERT INTO t1 VALUES(6,X'807f');
853 # Verify that the table named [table] is correctly quoted and that
854 # an INTEGER PRIMARY KEY DESC is not an alias for the rowid.
856 do_test shell1-4.1.3 {
861 CREATE TABLE [table](x INTEGER PRIMARY KEY DESC, y);
862 INSERT INTO [table] VALUES(1,null), (12,''), (23,1),
863 (34,2.25), (45,'hello'), (56,x'807f');
865 catchcmd test2.db {.dump --preserve-rowids}
866 } {0 {PRAGMA foreign_keys=OFF;
868 CREATE TABLE [table](x INTEGER PRIMARY KEY DESC, y);
869 INSERT INTO "table"(rowid,x,y) VALUES(1,1,NULL);
870 INSERT INTO "table"(rowid,x,y) VALUES(2,12,'');
871 INSERT INTO "table"(rowid,x,y) VALUES(3,23,1);
872 INSERT INTO "table"(rowid,x,y) VALUES(4,34,2.25);
873 INSERT INTO "table"(rowid,x,y) VALUES(5,45,'hello');
874 INSERT INTO "table"(rowid,x,y) VALUES(6,56,X'807f');
877 # Do not record rowids for a WITHOUT ROWID table. Also check correct quoting
878 # of table names that contain odd characters.
880 do_test shell1-4.1.4 {
885 CREATE TABLE [ta<>ble](x INTEGER PRIMARY KEY, y) WITHOUT ROWID;
886 INSERT INTO [ta<>ble] VALUES(1,null), (12,''), (23,1),
887 (34,2.25), (45,'hello'), (56,x'807f');
889 catchcmd test2.db {.dump --preserve-rowids}
890 } {0 {PRAGMA foreign_keys=OFF;
892 CREATE TABLE [ta<>ble](x INTEGER PRIMARY KEY, y) WITHOUT ROWID;
893 INSERT INTO "ta<>ble" VALUES(1,NULL);
894 INSERT INTO "ta<>ble" VALUES(12,'');
895 INSERT INTO "ta<>ble" VALUES(23,1);
896 INSERT INTO "ta<>ble" VALUES(34,2.25);
897 INSERT INTO "ta<>ble" VALUES(45,'hello');
898 INSERT INTO "ta<>ble" VALUES(56,X'807f');
901 # Do not record rowids if the rowid is inaccessible
903 do_test shell1-4.1.5 {
908 CREATE TABLE t1(_ROWID_,rowid,oid);
909 INSERT INTO t1 VALUES(1,null,'alpha'), (12,'',99), (23,1,x'b0b1b2');
911 catchcmd test2.db {.dump --preserve-rowids}
912 } {0 {PRAGMA foreign_keys=OFF;
914 CREATE TABLE t1(_ROWID_,rowid,oid);
915 INSERT INTO t1 VALUES(1,NULL,'alpha');
916 INSERT INTO t1 VALUES(12,'',99);
917 INSERT INTO t1 VALUES(23,1,X'b0b1b2');
922 do_test shell1-4.1.6 {
927 CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
928 INSERT INTO t1 VALUES(1,null), (2,''), (3,1),
929 (4,2.25), (5,'hello'), (6,x'807f');
931 catchcmd test2.db {.dump --preserve-rowids}
932 } {1 {The --preserve-rowids option is not compatible with SQLITE_OMIT_VIRTUALTABLE}}
937 # Test the output of ".mode insert"
939 do_test shell1-4.2.1 {
940 catchcmd test.db ".mode insert t1\nselect * from t1;"
941 } {0 {INSERT INTO t1 VALUES(NULL);
942 INSERT INTO t1 VALUES('');
943 INSERT INTO t1 VALUES(1);
944 INSERT INTO t1 VALUES(2.25);
945 INSERT INTO t1 VALUES('hello');
946 INSERT INTO t1 VALUES(X'807f');}}
948 # Test the output of ".mode insert" with headers
950 do_test shell1-4.2.2 {
951 catchcmd test.db ".mode insert t1\n.headers on\nselect * from t1;"
952 } {0 {INSERT INTO t1(x) VALUES(NULL);
953 INSERT INTO t1(x) VALUES('');
954 INSERT INTO t1(x) VALUES(1);
955 INSERT INTO t1(x) VALUES(2.25);
956 INSERT INTO t1(x) VALUES('hello');
957 INSERT INTO t1(x) VALUES(X'807f');}}
959 # Test the output of ".mode insert"
961 do_test shell1-4.2.3 {
962 catchcmd test.db ".mode insert t3\nselect * from t3;"
963 } {0 {INSERT INTO t3 VALUES(1,NULL);
964 INSERT INTO t3 VALUES(2,'');
965 INSERT INTO t3 VALUES(3,1);
966 INSERT INTO t3 VALUES(4,2.25);
967 INSERT INTO t3 VALUES(5,'hello');
968 INSERT INTO t3 VALUES(6,X'807f');}}
970 # Test the output of ".mode insert" with headers
972 do_test shell1-4.2.4 {
973 catchcmd test.db ".mode insert t3\n.headers on\nselect * from t3;"
974 } {0 {INSERT INTO t3(x,y) VALUES(1,NULL);
975 INSERT INTO t3(x,y) VALUES(2,'');
976 INSERT INTO t3(x,y) VALUES(3,1);
977 INSERT INTO t3(x,y) VALUES(4,2.25);
978 INSERT INTO t3(x,y) VALUES(5,'hello');
979 INSERT INTO t3(x,y) VALUES(6,X'807f');}}
981 # Test the output of ".mode tcl"
988 PRAGMA encoding=UTF8;
990 INSERT INTO t1 VALUES(null), (''), (1), (2.25), ('hello'), (x'807f');
992 catchcmd test.db ".mode tcl\nselect * from t1;"
1000 # Test the output of ".mode tcl" with multiple columns
1002 do_test shell1-4.4 {
1004 CREATE TABLE t2(x,y);
1005 INSERT INTO t2 VALUES(null, ''), (1, 2.25), ('hello', x'807f');
1007 catchcmd test.db ".mode tcl\nselect * from t2;"
1010 "hello" "\200\177"}}
1012 # Test the output of ".mode tcl" with ".nullvalue"
1014 do_test shell1-4.5 {
1015 catchcmd test.db ".mode tcl\n.nullvalue NULL\nselect * from t2;"
1018 "hello" "\200\177"}}
1020 # Test the output of ".mode tcl" with Tcl reserved characters
1022 do_test shell1-4.6 {
1024 CREATE TABLE tcl1(x);
1025 INSERT INTO tcl1 VALUES('"'), ('['), (']'), ('\{'), ('\}'), (';'), ('$');
1027 foreach {x y} [catchcmd test.db ".mode tcl\nselect * from tcl1;"] break
1028 list $x $y [llength $y]
1037 # Test the output of ".mode quote"
1039 do_test shell1-4.7 {
1040 catchcmd test.db ".mode quote\nselect x'0123456789ABCDEF';"
1041 } {0 X'0123456789abcdef'}
1043 # Test using arbitrary byte data with the shell via standard input/output.
1045 do_test shell1-5.0 {
1047 # NOTE: Skip NUL byte because it appears to be incompatible with command
1048 # shell argument parsing.
1050 for {set i 1} {$i < 256} {incr i} {
1052 # NOTE: Due to how the Tcl [exec] command works (i.e. where it treats
1053 # command channels opened for it as textual ones), the carriage
1054 # return character (and on Windows, the end-of-file character)
1055 # cannot be used here.
1057 if {$i==0x0D || ($tcl_platform(platform)=="windows" && $i==0x1A)} {
1060 # Tcl 8.7 maps 0x80 through 0x9f into valid UTF8. So skip those tests.
1061 if {$i>=0x80 && $i<=0x9f} continue
1062 if {$i>=0xE0 && $tcl_platform(os)=="OpenBSD"} continue
1063 if {$i>=0xE0 && $i<=0xEF && $tcl_platform(os)=="Linux"} continue
1064 set hex [format %02X $i]
1065 set char [subst \\x$hex]; set oldChar $char
1067 if {$tcl_platform(platform)=="windows"} {
1069 # NOTE: On Windows, we need to escape all the whitespace characters,
1070 # the alarm (\a) character, and those with special meaning to
1071 # the SQLite shell itself.
1074 \a \\a \b \\b \t \\t \n \\n \v \\v \f \\f \r \\r \
1075 " " "\" \"" \" \\\" \\ \\\\]
1078 # NOTE: On Unix, we need to escape most of the whitespace characters
1079 # and those with special meaning to the SQLite shell itself.
1080 # The alarm (\a), backspace (\b), and carriage-return (\r)
1081 # characters do not appear to require escaping on Unix. For
1082 # the alarm and backspace characters, this is probably due to
1083 # differences in the command shell. For the carriage-return,
1084 # it is probably due to differences in how Tcl handles command
1085 # channel end-of-line translations.
1088 \t \\t \n \\n \v \\v \f \\f \
1089 " " "\" \"" \" \\\" \\ \\\\]
1091 set char [string map $escapes $char]
1092 set x [catchcmdex test.db ".print \"$char\"\n"]
1093 set code [lindex $x 0]
1094 set res [lindex $x 1]
1096 error "failed with error: $res"
1098 if {$res ne "$oldChar\n"} {
1099 if {[llength $res] > 0} {
1100 set got [format %02X [scan $res %c]]
1104 error "failed with byte $hex mismatch, got $got"
1109 # These test cases do not work on MinGW
1112 # The string used here is the word "test" in Chinese.
1113 # In UTF-8, it is encoded as: \xE6\xB5\x8B\xE8\xAF\x95
1114 set test \u6D4B\u8BD5
1116 do_test shell1-6.0 {
1117 set fileName $test; append fileName .db
1118 catch {forcedelete $fileName}
1119 set x [catchcmdex $fileName "CREATE TABLE t1(x);\n.schema\n"]
1120 set code [lindex $x 0]
1121 set res [string trim [lindex $x 1]]
1123 error "failed with error: $res"
1125 if {$res ne "CREATE TABLE t1(x);"} {
1126 error "failed with mismatch: $res"
1128 if {![file exists $fileName]} {
1129 error "file \"$fileName\" (Unicode) does not exist"
1131 forcedelete $fileName
1134 do_test shell1-6.1 {
1135 catch {forcedelete test3.db}
1136 set x [catchcmdex test3.db \
1137 "CREATE TABLE [encoding convertto utf-8 $test](x);\n.schema\n"]
1138 set code [lindex $x 0]
1139 set res [string trim [lindex $x 1]]
1141 error "failed with error: $res"
1143 if {$res ne "CREATE TABLE ${test}(x);"} {
1144 error "failed with mismatch: $res"
1146 forcedelete test3.db
1151 forcedelete test.db test.db-journal test.db-wal
1154 # The shell tool ".schema" command uses virtual table "pragma_database_list"
1158 do_test shell1-7.1.1 {
1160 CREATE TABLE Z (x TEXT PRIMARY KEY);
1161 CREATE TABLE _ (x TEXT PRIMARY KEY);
1162 CREATE TABLE YY (x TEXT PRIMARY KEY);
1163 CREATE TABLE __ (x TEXT PRIMARY KEY);
1164 CREATE TABLE WWW (x TEXT PRIMARY KEY);
1165 CREATE TABLE ___ (x TEXT PRIMARY KEY);
1168 do_test shell1-7.1.2 {
1169 catchcmd "test.db" ".schema _"
1170 } {0 {CREATE TABLE Z (x TEXT PRIMARY KEY);
1171 CREATE TABLE _ (x TEXT PRIMARY KEY);}}
1172 do_test shell1-7.1.3 {
1173 catchcmd "test.db" ".schema \"\\\\_\""
1174 } {0 {CREATE TABLE _ (x TEXT PRIMARY KEY);}}
1175 do_test shell1-7.1.4 {
1176 catchcmd "test.db" ".schema __"
1177 } {0 {CREATE TABLE YY (x TEXT PRIMARY KEY);
1178 CREATE TABLE __ (x TEXT PRIMARY KEY);}}
1179 do_test shell1-7.1.5 {
1180 catchcmd "test.db" ".schema \"\\\\_\\\\_\""
1181 } {0 {CREATE TABLE __ (x TEXT PRIMARY KEY);}}
1182 do_test shell1-7.1.6 {
1183 catchcmd "test.db" ".schema ___"
1184 } {0 {CREATE TABLE WWW (x TEXT PRIMARY KEY);
1185 CREATE TABLE ___ (x TEXT PRIMARY KEY);}}
1186 do_test shell1-7.1.7 {
1187 catchcmd "test.db" ".schema \"\\\\_\\\\_\\\\_\""
1188 } {0 {CREATE TABLE ___ (x TEXT PRIMARY KEY);}}
1192 # Test case for the ieee754 and decimal extensions in the shell.
1193 # See the "floatingpoint.html" file in the documentation for more
1196 do_test shell1-8.1 {
1197 catchcmd ":memory:" {
1198 -- The pow2 table will hold all the necessary powers of two.
1199 CREATE TABLE pow2(x INTEGER PRIMARY KEY, v TEXT);
1200 WITH RECURSIVE c(x,v) AS (
1203 SELECT x+1, decimal_mul(v,'2') FROM c WHERE x+1<=971
1204 ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
1205 WITH RECURSIVE c(x,v) AS (
1208 SELECT x-1, decimal_mul(v,'0.5') FROM c WHERE x-1>=-1075
1209 ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
1211 -- This query finds the decimal representation of each value in the "c" table.
1212 WITH c(n) AS (VALUES(47.49))
1213 ----XXXXX----------- Replace with whatever you want
1214 SELECT decimal_mul(ieee754_mantissa(c.n),pow2.v)
1215 FROM pow2, c WHERE pow2.x=ieee754_exponent(c.n);
1217 } {0 47.49000000000000198951966012828052043914794921875}
1218 do_test shell1-8.2 {
1221 SELECT ieee754(47.49) AS x;
1223 } {0 {┌───────────────────────────────┐
1225 ├───────────────────────────────┤
1226 │ ieee754(6683623321994527,-47) │
1227 └───────────────────────────────┘}}
1228 do_test shell1-8.3 {
1229 catchcmd ":memory: --box" {
1230 select ieee754(6683623321994527,-47) as x;
1237 do_test shell1-8.4 {
1238 catchcmd ":memory: --table" {SELECT ieee754_mantissa(47.49) AS M, ieee754_exponent(47.49) AS E;}
1239 } {0 {+------------------+-----+
1241 +------------------+-----+
1242 | 6683623321994527 | -47 |
1243 +------------------+-----+}}
1244 do_test shell1-8.5 {
1245 catchcmd ":memory: --box" {
1246 create table t(a text, b int);
1247 insert into t values ('too long for one line', 1), ('shorter', NULL);
1252 } {0 {┌────────────┬────────────┐
1254 ├────────────┼────────────┤
1258 ├────────────┼────────────┤
1260 └────────────┴────────────┘}}
1262 #----------------------------------------------------------------------------
1263 # Test cases shell1-9.*: Basic test that "dot" commands and SQL intermix ok.
1265 do_test shell1-9.1 {
1269 x */ select 1,2; --x
1274 select 2,1; select 3,4;
1280 #----------------------------------------------------------------------------
1281 # Test cases shell1-10.*: Test that certain static extensions are there.
1283 do_test shell1-10.1 {
1287 select base64(base64(cast('digity-doo' as blob))),
1288 base85(base85(cast('digity-doo' as blob)));
1290 } {0 digity-doo|digity-doo}