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
71 do_test shell1-1.3.1 {
72 catchcmd "-init FOO test.db" ""
74 do_test shell1-1.3.2 {
75 catchcmd "-init FOO test.db .quit BAD" ""
77 do_test shell1-1.3.3 {
78 catchcmd "-init FOO test.db BAD .quit" ""
79 } {1 {Error: near "BAD": syntax error}}
81 # -echo print commands before execution
82 do_test shell1-1.4.1 {
83 catchcmd "-echo test.db" ""
86 # -[no]header turn headers on or off
87 do_test shell1-1.5.1 {
88 catchcmd "-header test.db" ""
90 do_test shell1-1.5.2 {
91 catchcmd "-noheader test.db" ""
94 # -bail stop after hitting an error
95 do_test shell1-1.6.1 {
96 catchcmd "-bail test.db" ""
99 # -interactive force interactive I/O
100 do_test shell1-1.7.1 {
101 set res [catchcmd "-interactive test.db" ".quit"]
102 set rc [lindex $res 0]
104 [regexp {SQLite version} $res] \
105 [regexp {Enter ".help" for usage hints} $res]
108 # -batch force batch I/O
109 do_test shell1-1.8.1 {
110 catchcmd "-batch test.db" ""
113 # -column set output mode to 'column'
114 do_test shell1-1.9.1 {
115 catchcmd "-column test.db" ""
118 # -csv set output mode to 'csv'
119 do_test shell1-1.10.1 {
120 catchcmd "-csv test.db" ""
123 # -html set output mode to HTML
124 do_test shell1-1.11.1 {
125 catchcmd "-html test.db" ""
128 # -line set output mode to 'line'
129 do_test shell1-1.12.1 {
130 catchcmd "-line test.db" ""
133 # -list set output mode to 'list'
134 do_test shell1-1.13.1 {
135 catchcmd "-list test.db" ""
138 # -separator 'x' set output field separator (|)
139 do_test shell1-1.14.1 {
140 catchcmd "-separator 'x' test.db" ""
142 do_test shell1-1.14.2 {
143 catchcmd "-separator x test.db" ""
145 do_test shell1-1.14.3 {
146 set res [catchcmd "-separator" ""]
147 set rc [lindex $res 0]
149 [regexp {Error: missing argument to -separator} $res]
152 # -stats print memory stats before each finalize
153 do_test shell1-1.14b.1 {
154 catchcmd "-stats test.db" ""
157 # -nullvalue 'text' set text string for NULL values
158 do_test shell1-1.15.1 {
159 catchcmd "-nullvalue 'x' test.db" ""
161 do_test shell1-1.15.2 {
162 catchcmd "-nullvalue x test.db" ""
164 do_test shell1-1.15.3 {
165 set res [catchcmd "-nullvalue" ""]
166 set rc [lindex $res 0]
168 [regexp {Error: missing argument to -nullvalue} $res]
171 # -version show SQLite version
172 do_test shell1-1.16.1 {
173 set x [catchcmd "-version test.db" ""]
174 } {/3.[0-9.]+ 20\d\d-[01]\d-\d\d \d\d:\d\d:\d\d [0-9a-f]+/}
176 #----------------------------------------------------------------------------
177 # Test cases shell1-2.*: Basic "dot" command token parsing.
180 # check first token handling
181 do_test shell1-2.1.1 {
182 catchcmd "test.db" ".foo"
183 } {1 {Error: unknown command or invalid arguments: "foo". Enter ".help" for help}}
184 do_test shell1-2.1.2 {
185 catchcmd "test.db" ".\"foo OFF\""
186 } {1 {Error: unknown command or invalid arguments: "foo OFF". Enter ".help" for help}}
187 do_test shell1-2.1.3 {
188 catchcmd "test.db" ".\'foo OFF\'"
189 } {1 {Error: unknown command or invalid arguments: "foo OFF". Enter ".help" for help}}
192 do_test shell1-2.2.1 {
193 catchcmd "test.db" ".\"foo OFF"
194 } {1 {Error: unknown command or invalid arguments: "foo OFF". Enter ".help" for help}}
195 do_test shell1-2.2.2 {
196 catchcmd "test.db" ".\'foo OFF"
197 } {1 {Error: unknown command or invalid arguments: "foo OFF". Enter ".help" for help}}
198 do_test shell1-2.2.3 {
199 catchcmd "test.db" ".explain \"OFF"
201 do_test shell1-2.2.4 {
202 catchcmd "test.db" ".explain \'OFF"
204 do_test shell1-2.2.5 {
205 catchcmd "test.db" ".mode \"insert FOO"
206 } {1 {Error: mode should be one of: ascii box column csv html insert json line list markdown quote table tabs tcl}}
207 do_test shell1-2.2.6 {
208 catchcmd "test.db" ".mode \'insert FOO"
209 } {1 {Error: mode should be one of: ascii box column csv html insert json line list markdown quote table tabs tcl}}
211 # check multiple tokens, and quoted tokens
212 do_test shell1-2.3.1 {
213 catchcmd "test.db" ".explain 1"
215 do_test shell1-2.3.2 {
216 catchcmd "test.db" ".explain on"
218 do_test shell1-2.3.3 {
219 catchcmd "test.db" ".explain \"1 2 3\""
220 } {1 {ERROR: Not a boolean value: "1 2 3". Assuming "no".}}
221 do_test shell1-2.3.4 {
222 catchcmd "test.db" ".explain \"OFF\""
224 do_test shell1-2.3.5 {
225 catchcmd "test.db" ".\'explain\' \'OFF\'"
227 do_test shell1-2.3.6 {
228 catchcmd "test.db" ".explain \'OFF\'"
230 do_test shell1-2.3.7 {
231 catchcmd "test.db" ".\'explain\' \'OFF\'"
234 # check quoted args are unquoted
235 do_test shell1-2.4.1 {
236 catchcmd "test.db" ".mode FOO"
237 } {1 {Error: mode should be one of: ascii box column csv html insert json line list markdown quote table tabs tcl}}
238 do_test shell1-2.4.2 {
239 catchcmd "test.db" ".mode csv"
241 do_test shell1-2.4.2 {
242 catchcmd "test.db" ".mode \"csv\""
246 #----------------------------------------------------------------------------
247 # Test cases shell1-3.*: Basic test that "dot" command can be called.
250 # .backup ?DB? FILE Backup DB (default "main") to FILE
251 do_test shell1-3.1.1 {
252 catchcmd "test.db" ".backup"
253 } {1 {missing FILENAME argument on .backup}}
255 do_test shell1-3.1.2 {
256 catchcmd "test.db" ".backup FOO"
258 do_test shell1-3.1.3 {
259 catchcmd "test.db" ".backup FOO BAR"
260 } {1 {Error: unknown database FOO}}
261 do_test shell1-3.1.4 {
263 catchcmd "test.db" ".backup FOO BAR BAD"
264 } {1 {Usage: .backup ?DB? ?OPTIONS? FILENAME}}
266 # .bail ON|OFF Stop after hitting an error. Default OFF
267 do_test shell1-3.2.1 {
268 catchcmd "test.db" ".bail"
269 } {1 {Usage: .bail on|off}}
270 do_test shell1-3.2.2 {
271 catchcmd "test.db" ".bail ON"
273 do_test shell1-3.2.3 {
274 catchcmd "test.db" ".bail OFF"
276 do_test shell1-3.2.4 {
278 catchcmd "test.db" ".bail OFF BAD"
279 } {1 {Usage: .bail on|off}}
282 # .databases List names and files of attached databases
283 do_test shell1-3.3.1 {
284 catchcmd "-csv test.db" ".databases"
285 } "/0.+main.+[string map {/ ".{1,2}"} [string range [get_pwd] 0 10]].*/"
286 do_test shell1-3.3.2 {
287 # extra arguments ignored
288 catchcmd "test.db" ".databases BAD"
289 } "/0.+main.+[string map {/ ".{1,2}"} [string range [get_pwd] 0 10]].*/"
292 # .dump ?TABLE? ... Dump the database in an SQL text format
293 # If TABLE specified, only dump tables matching
294 # LIKE pattern TABLE.
295 do_test shell1-3.4.1 {
296 set res [catchcmd "test.db" ".dump"]
297 list [regexp {BEGIN TRANSACTION;} $res] \
298 [regexp {COMMIT;} $res]
300 do_test shell1-3.4.2 {
301 set res [catchcmd "test.db" ".dump FOO"]
302 list [regexp {BEGIN TRANSACTION;} $res] \
303 [regexp {COMMIT;} $res]
305 # The .dump command now accepts multiple arguments
306 #do_test shell1-3.4.3 {
307 # # too many arguments
308 # catchcmd "test.db" ".dump FOO BAD"
309 #} {1 {Usage: .dump ?--preserve-rowids? ?--newlines? ?LIKE-PATTERN?}}
311 # .echo ON|OFF Turn command echo on or off
312 do_test shell1-3.5.1 {
313 catchcmd "test.db" ".echo"
314 } {1 {Usage: .echo on|off}}
315 do_test shell1-3.5.2 {
316 catchcmd "test.db" ".echo ON"
318 do_test shell1-3.5.3 {
319 catchcmd "test.db" ".echo OFF"
321 do_test shell1-3.5.4 {
323 catchcmd "test.db" ".echo OFF BAD"
324 } {1 {Usage: .echo on|off}}
326 # .exit Exit this program
327 do_test shell1-3.6.1 {
328 catchcmd "test.db" ".exit"
331 # .explain ON|OFF Turn output mode suitable for EXPLAIN on or off.
332 do_test shell1-3.7.1 {
333 catchcmd "test.db" ".explain"
334 # explain is the exception to the booleans. without an option, it turns it on.
336 do_test shell1-3.7.2 {
337 catchcmd "test.db" ".explain ON"
339 do_test shell1-3.7.3 {
340 catchcmd "test.db" ".explain OFF"
342 do_test shell1-3.7.4 {
343 # extra arguments ignored
344 catchcmd "test.db" ".explain OFF BAD"
348 # .header(s) ON|OFF Turn display of headers on or off
349 do_test shell1-3.9.1 {
350 catchcmd "test.db" ".header"
351 } {1 {Usage: .headers on|off}}
352 do_test shell1-3.9.2 {
353 catchcmd "test.db" ".header ON"
355 do_test shell1-3.9.3 {
356 catchcmd "test.db" ".header OFF"
358 do_test shell1-3.9.4 {
360 catchcmd "test.db" ".header OFF BAD"
361 } {1 {Usage: .headers on|off}}
363 do_test shell1-3.9.5 {
364 catchcmd "test.db" ".headers"
365 } {1 {Usage: .headers on|off}}
366 do_test shell1-3.9.6 {
367 catchcmd "test.db" ".headers ON"
369 do_test shell1-3.9.7 {
370 catchcmd "test.db" ".headers OFF"
372 do_test shell1-3.9.8 {
374 catchcmd "test.db" ".headers OFF BAD"
375 } {1 {Usage: .headers on|off}}
377 # .help Show this message
378 do_test shell1-3.10.1 {
379 set res [catchcmd "test.db" ".help"]
380 # look for a few of the possible help commands
381 list [regexp {.help} $res] \
382 [regexp {.quit} $res] \
383 [regexp {.show} $res]
385 do_test shell1-3.10.2 {
386 # we allow .help to take extra args (it is help after all)
387 set res [catchcmd "test.db" ".help *"]
388 # look for a few of the possible help commands
389 list [regexp {.help} $res] \
390 [regexp {.quit} $res] \
391 [regexp {.show} $res]
394 # .import FILE TABLE Import data from FILE into TABLE
395 do_test shell1-3.11.1 {
396 catchcmd "test.db" ".import"
397 } {/1 .ERROR: missing FILE argument.*/}
398 do_test shell1-3.11.2 {
399 catchcmd "test.db" ".import FOO"
400 } {/1 .ERROR: missing TABLE argument.*/}
401 do_test shell1-3.11.3 {
403 catchcmd "test.db" ".import FOO BAR BAD"
404 } {/1 .ERROR: extra argument: "BAD".*./}
406 # .indexes ?TABLE? Show names of all indexes
407 # If TABLE specified, only show indexes for tables
408 # matching LIKE pattern TABLE.
409 do_test shell1-3.12.1 {
410 catchcmd "test.db" ".indexes"
412 do_test shell1-3.12.2 {
413 catchcmd "test.db" ".indexes FOO"
415 do_test shell1-3.12.2-legacy {
416 catchcmd "test.db" ".indices FOO"
418 do_test shell1-3.12.3 {
420 catchcmd "test.db" ".indexes FOO BAD"
421 } {1 {Usage: .indexes ?LIKE-PATTERN?}}
423 # .mode MODE ?TABLE? Set output mode where MODE is one of:
424 # ascii Columns/rows delimited by 0x1F and 0x1E
425 # csv Comma-separated values
426 # column Left-aligned columns. (See .width)
427 # html HTML <table> code
428 # insert SQL insert statements for TABLE
429 # line One value per line
430 # list Values delimited by .separator strings
431 # tabs Tab-separated values
432 # tcl TCL list elements
433 do_test shell1-3.13.1 {
434 catchcmd "test.db" ".mode"
435 } {0 {current output mode: list}}
436 do_test shell1-3.13.2 {
437 catchcmd "test.db" ".mode FOO"
438 } {1 {Error: mode should be one of: ascii box column csv html insert json line list markdown quote table tabs tcl}}
439 do_test shell1-3.13.3 {
440 catchcmd "test.db" ".mode csv"
442 do_test shell1-3.13.4 {
443 catchcmd "test.db" ".mode column"
445 do_test shell1-3.13.5 {
446 catchcmd "test.db" ".mode html"
448 do_test shell1-3.13.6 {
449 catchcmd "test.db" ".mode insert"
451 do_test shell1-3.13.7 {
452 catchcmd "test.db" ".mode line"
454 do_test shell1-3.13.8 {
455 catchcmd "test.db" ".mode list"
457 do_test shell1-3.13.9 {
458 catchcmd "test.db" ".mode tabs"
460 do_test shell1-3.13.10 {
461 catchcmd "test.db" ".mode tcl"
463 do_test shell1-3.13.11 {
464 # extra arguments ignored
465 catchcmd "test.db" ".mode tcl BAD"
468 # don't allow partial mode type matches
469 do_test shell1-3.13.12 {
470 catchcmd "test.db" ".mode l"
471 } {1 {Error: mode should be one of: ascii box column csv html insert json line list markdown quote table tabs tcl}}
472 do_test shell1-3.13.13 {
473 catchcmd "test.db" ".mode li"
474 } {1 {Error: mode should be one of: ascii box column csv html insert json line list markdown quote table tabs tcl}}
475 do_test shell1-3.13.14 {
476 catchcmd "test.db" ".mode lin"
479 # .nullvalue STRING Print STRING in place of NULL values
480 do_test shell1-3.14.1 {
481 catchcmd "test.db" ".nullvalue"
482 } {1 {Usage: .nullvalue STRING}}
483 do_test shell1-3.14.2 {
484 catchcmd "test.db" ".nullvalue FOO"
486 do_test shell1-3.14.3 {
488 catchcmd "test.db" ".nullvalue FOO BAD"
489 } {1 {Usage: .nullvalue STRING}}
491 # .output FILENAME Send output to FILENAME
492 do_test shell1-3.15.1 {
493 catchcmd "test.db" ".output"
495 do_test shell1-3.15.2 {
496 catchcmd "test.db" ".output FOO"
498 do_test shell1-3.15.3 {
500 catchcmd "test.db" ".output FOO BAD"
501 } {1 {ERROR: extra parameter: "BAD". Usage:
502 .output ?FILE? Send output to FILE or stdout if FILE is omitted
503 If FILE begins with '|' then open it as a pipe.
505 --bom Prefix output with a UTF8 byte-order mark
506 -e Send output to the system text editor
507 -x Send output as CSV to a spreadsheet
508 child process exited abnormally}}
510 # .output stdout Send output to the screen
511 do_test shell1-3.16.1 {
512 catchcmd "test.db" ".output stdout"
514 do_test shell1-3.16.2 {
516 catchcmd "test.db" ".output stdout BAD"
517 } {1 {ERROR: extra parameter: "BAD". Usage:
518 .output ?FILE? Send output to FILE or stdout if FILE is omitted
519 If FILE begins with '|' then open it as a pipe.
521 --bom Prefix output with a UTF8 byte-order mark
522 -e Send output to the system text editor
523 -x Send output as CSV to a spreadsheet
524 child process exited abnormally}}
526 # .prompt MAIN CONTINUE Replace the standard prompts
527 do_test shell1-3.17.1 {
528 catchcmd "test.db" ".prompt"
530 do_test shell1-3.17.2 {
531 catchcmd "test.db" ".prompt FOO"
533 do_test shell1-3.17.3 {
534 catchcmd "test.db" ".prompt FOO BAR"
536 do_test shell1-3.17.4 {
538 catchcmd "test.db" ".prompt FOO BAR BAD"
541 # .quit Exit this program
542 do_test shell1-3.18.1 {
543 catchcmd "test.db" ".quit"
545 do_test shell1-3.18.2 {
547 catchcmd "test.db" ".quit BAD"
550 # .read FILENAME Execute SQL in FILENAME
551 do_test shell1-3.19.1 {
552 catchcmd "test.db" ".read"
553 } {1 {Usage: .read FILE}}
554 do_test shell1-3.19.2 {
556 catchcmd "test.db" ".read FOO"
557 } {1 {Error: cannot open "FOO"}}
558 do_test shell1-3.19.3 {
560 catchcmd "test.db" ".read FOO BAD"
561 } {1 {Usage: .read FILE}}
563 # .restore ?DB? FILE Restore content of DB (default "main") from FILE
564 do_test shell1-3.20.1 {
565 catchcmd "test.db" ".restore"
566 } {1 {Usage: .restore ?DB? FILE}}
567 do_test shell1-3.20.2 {
568 catchcmd "test.db" ".restore FOO"
570 do_test shell1-3.20.3 {
571 catchcmd "test.db" ".restore FOO BAR"
572 } {1 {Error: unknown database FOO}}
573 do_test shell1-3.20.4 {
575 catchcmd "test.db" ".restore FOO BAR BAD"
576 } {1 {Usage: .restore ?DB? FILE}}
579 # .schema ?TABLE? Show the CREATE statements
580 # If TABLE specified, only show tables matching
581 # LIKE pattern TABLE.
582 do_test shell1-3.21.1 {
583 catchcmd "test.db" ".schema"
585 do_test shell1-3.21.2 {
586 catchcmd "test.db" ".schema FOO"
588 do_test shell1-3.21.3 {
590 catchcmd "test.db" ".schema FOO BAD"
591 } {1 {Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?}}
593 do_test shell1-3.21.4 {
596 CREATE VIEW v2 AS SELECT x+1 AS y FROM t1;
597 CREATE VIEW v1 AS SELECT y+1 FROM v2;
599 catchcmd "test.db" ".schema"
600 } {0 {CREATE TABLE t1(x);
601 CREATE VIEW v2 AS SELECT x+1 AS y FROM t1
603 CREATE VIEW v1 AS SELECT y+1 FROM v2
605 db eval {DROP VIEW v1; DROP VIEW v2; DROP TABLE t1;}
608 # .separator STRING Change column separator used by output and .import
609 do_test shell1-3.22.1 {
610 catchcmd "test.db" ".separator"
611 } {1 {Usage: .separator COL ?ROW?}}
612 do_test shell1-3.22.2 {
613 catchcmd "test.db" ".separator FOO"
615 do_test shell1-3.22.3 {
616 catchcmd "test.db" ".separator ABC XYZ"
618 do_test shell1-3.22.4 {
620 catchcmd "test.db" ".separator FOO BAD BAD2"
621 } {1 {Usage: .separator COL ?ROW?}}
623 # .show Show the current values for various settings
624 do_test shell1-3.23.1 {
625 set res [catchcmd "test.db" ".show"]
626 list [regexp {echo:} $res] \
627 [regexp {explain:} $res] \
628 [regexp {headers:} $res] \
629 [regexp {mode:} $res] \
630 [regexp {nullvalue:} $res] \
631 [regexp {output:} $res] \
632 [regexp {colseparator:} $res] \
633 [regexp {rowseparator:} $res] \
634 [regexp {stats:} $res] \
635 [regexp {width:} $res]
636 } {1 1 1 1 1 1 1 1 1 1}
637 do_test shell1-3.23.2 {
639 catchcmd "test.db" ".show BAD"
642 # .stats ON|OFF Turn stats on or off
643 #do_test shell1-3.23b.1 {
644 # catchcmd "test.db" ".stats"
645 #} {1 {Usage: .stats on|off}}
646 do_test shell1-3.23b.2 {
647 catchcmd "test.db" ".stats ON"
649 do_test shell1-3.23b.3 {
650 catchcmd "test.db" ".stats OFF"
652 do_test shell1-3.23b.4 {
654 catchcmd "test.db" ".stats OFF BAD"
655 } {1 {Usage: .stats ?on|off?}}
657 # Ticket 7be932dfa60a8a6b3b26bcf7623ec46e0a403ddb 2018-06-07
658 # Adverse interaction between .stats and .eqp
660 do_test shell1-3.23b.5 {
661 catchcmd "test.db" [string map {"\n " "\n"} {
662 CREATE TEMP TABLE t1(x);
663 INSERT INTO t1 VALUES(1),(2);
670 # .tables ?TABLE? List names of tables
671 # If TABLE specified, only list tables matching
672 # LIKE pattern TABLE.
673 do_test shell1-3.24.1 {
674 catchcmd "test.db" ".tables"
676 do_test shell1-3.24.2 {
677 catchcmd "test.db" ".tables FOO"
679 do_test shell1-3.24.3 {
681 catchcmd "test.db" ".tables FOO BAD"
684 # .timeout MS Try opening locked tables for MS milliseconds
685 do_test shell1-3.25.1 {
686 catchcmd "test.db" ".timeout"
688 do_test shell1-3.25.2 {
689 catchcmd "test.db" ".timeout zzz"
690 # this should be treated the same as a '0' timeout
692 do_test shell1-3.25.3 {
693 catchcmd "test.db" ".timeout 1"
695 do_test shell1-3.25.4 {
697 catchcmd "test.db" ".timeout 1 BAD"
700 # .width NUM NUM ... Set column widths for "column" mode
701 do_test shell1-3.26.1 {
702 catchcmd "test.db" ".width"
704 do_test shell1-3.26.2 {
705 catchcmd "test.db" ".width xxx"
706 # this should be treated the same as a '0' width for col 1
708 do_test shell1-3.26.3 {
709 catchcmd "test.db" ".width xxx yyy"
710 # this should be treated the same as a '0' width for col 1 and 2
712 do_test shell1-3.26.4 {
713 catchcmd "test.db" ".width 1 1"
714 # this should be treated the same as a '1' width for col 1 and 2
716 do_test shell1-3.26.5 {
717 catchcmd "test.db" ".mode column\n.header off\n.width 10 -10\nSELECT 'abcdefg', 123456;"
718 # this should be treated the same as a '1' width for col 1 and 2
719 } {0 {abcdefg 123456}}
720 do_test shell1-3.26.6 {
721 catchcmd "test.db" ".mode column\n.header off\n.width -10 10\nSELECT 'abcdefg', 123456;"
722 # this should be treated the same as a '1' width for col 1 and 2
723 } {0 { abcdefg 123456 }}
726 # .timer ON|OFF Turn the CPU timer measurement on or off
727 do_test shell1-3.27.1 {
728 catchcmd "test.db" ".timer"
729 } {1 {Usage: .timer on|off}}
730 do_test shell1-3.27.2 {
731 catchcmd "test.db" ".timer ON"
733 do_test shell1-3.27.3 {
734 catchcmd "test.db" ".timer OFF"
736 do_test shell1-3.27.4 {
738 catchcmd "test.db" ".timer OFF BAD"
739 } {1 {Usage: .timer on|off}}
741 do_test shell1-3-28.1 {
743 ".log stdout\nSELECT coalesce(sqlite_log(123,'hello'),'456');"
744 } "0 {(123) hello\n456}"
746 do_test shell1-3-29.1 {
747 catchcmd "test.db" ".print this is a test"
748 } {0 {this is a test}}
750 # dot-command argument quoting
751 do_test shell1-3-30.1 {
752 catchcmd {test.db} {.print "this\"is'a\055test" 'this\"is\\a\055test'}
753 } {0 {this"is'a-test this\"is\\a\055test}}
754 do_test shell1-3-31.1 {
755 catchcmd {test.db} {.print "this\nis\ta\\test" 'this\nis\ta\\test'}
756 } [list 0 "this\nis\ta\\test this\\nis\\ta\\\\test"]
759 # Test the output of the ".dump" command
766 PRAGMA encoding=UTF16;
768 INSERT INTO t1 VALUES(null), (''), (1), (2.25), ('hello'), (x'807f');
769 CREATE TABLE t3(x,y);
770 INSERT INTO t3 VALUES(1,null), (2,''), (3,1),
771 (4,2.25), (5,'hello'), (6,x'807f');
773 catchcmd test.db {.dump}
774 } {0 {PRAGMA foreign_keys=OFF;
777 INSERT INTO t1 VALUES(NULL);
778 INSERT INTO t1 VALUES('');
779 INSERT INTO t1 VALUES(1);
780 INSERT INTO t1 VALUES(2.25);
781 INSERT INTO t1 VALUES('hello');
782 INSERT INTO t1 VALUES(X'807f');
783 CREATE TABLE t3(x,y);
784 INSERT INTO t3 VALUES(1,NULL);
785 INSERT INTO t3 VALUES(2,'');
786 INSERT INTO t3 VALUES(3,1);
787 INSERT INTO t3 VALUES(4,2.25);
788 INSERT INTO t3 VALUES(5,'hello');
789 INSERT INTO t3 VALUES(6,X'807f');
795 # The --preserve-rowids option to .dump
797 do_test shell1-4.1.1 {
798 catchcmd test.db {.dump --preserve-rowids}
799 } {0 {PRAGMA foreign_keys=OFF;
802 INSERT INTO t1(rowid,x) VALUES(1,NULL);
803 INSERT INTO t1(rowid,x) VALUES(2,'');
804 INSERT INTO t1(rowid,x) VALUES(3,1);
805 INSERT INTO t1(rowid,x) VALUES(4,2.25);
806 INSERT INTO t1(rowid,x) VALUES(5,'hello');
807 INSERT INTO t1(rowid,x) VALUES(6,X'807f');
808 CREATE TABLE t3(x,y);
809 INSERT INTO t3(rowid,x,y) VALUES(1,1,NULL);
810 INSERT INTO t3(rowid,x,y) VALUES(2,2,'');
811 INSERT INTO t3(rowid,x,y) VALUES(3,3,1);
812 INSERT INTO t3(rowid,x,y) VALUES(4,4,2.25);
813 INSERT INTO t3(rowid,x,y) VALUES(5,5,'hello');
814 INSERT INTO t3(rowid,x,y) VALUES(6,6,X'807f');
817 # If the table contains an INTEGER PRIMARY KEY, do not record a separate
818 # rowid column in the output.
820 do_test shell1-4.1.2 {
825 CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
826 INSERT INTO t1 VALUES(1,null), (2,''), (3,1),
827 (4,2.25), (5,'hello'), (6,x'807f');
829 catchcmd test2.db {.dump --preserve-rowids}
830 } {0 {PRAGMA foreign_keys=OFF;
832 CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
833 INSERT INTO t1 VALUES(1,NULL);
834 INSERT INTO t1 VALUES(2,'');
835 INSERT INTO t1 VALUES(3,1);
836 INSERT INTO t1 VALUES(4,2.25);
837 INSERT INTO t1 VALUES(5,'hello');
838 INSERT INTO t1 VALUES(6,X'807f');
841 # Verify that the table named [table] is correctly quoted and that
842 # an INTEGER PRIMARY KEY DESC is not an alias for the rowid.
844 do_test shell1-4.1.3 {
849 CREATE TABLE [table](x INTEGER PRIMARY KEY DESC, y);
850 INSERT INTO [table] VALUES(1,null), (12,''), (23,1),
851 (34,2.25), (45,'hello'), (56,x'807f');
853 catchcmd test2.db {.dump --preserve-rowids}
854 } {0 {PRAGMA foreign_keys=OFF;
856 CREATE TABLE [table](x INTEGER PRIMARY KEY DESC, y);
857 INSERT INTO "table"(rowid,x,y) VALUES(1,1,NULL);
858 INSERT INTO "table"(rowid,x,y) VALUES(2,12,'');
859 INSERT INTO "table"(rowid,x,y) VALUES(3,23,1);
860 INSERT INTO "table"(rowid,x,y) VALUES(4,34,2.25);
861 INSERT INTO "table"(rowid,x,y) VALUES(5,45,'hello');
862 INSERT INTO "table"(rowid,x,y) VALUES(6,56,X'807f');
865 # Do not record rowids for a WITHOUT ROWID table. Also check correct quoting
866 # of table names that contain odd characters.
868 do_test shell1-4.1.4 {
873 CREATE TABLE [ta<>ble](x INTEGER PRIMARY KEY, y) WITHOUT ROWID;
874 INSERT INTO [ta<>ble] VALUES(1,null), (12,''), (23,1),
875 (34,2.25), (45,'hello'), (56,x'807f');
877 catchcmd test2.db {.dump --preserve-rowids}
878 } {0 {PRAGMA foreign_keys=OFF;
880 CREATE TABLE [ta<>ble](x INTEGER PRIMARY KEY, y) WITHOUT ROWID;
881 INSERT INTO "ta<>ble" VALUES(1,NULL);
882 INSERT INTO "ta<>ble" VALUES(12,'');
883 INSERT INTO "ta<>ble" VALUES(23,1);
884 INSERT INTO "ta<>ble" VALUES(34,2.25);
885 INSERT INTO "ta<>ble" VALUES(45,'hello');
886 INSERT INTO "ta<>ble" VALUES(56,X'807f');
889 # Do not record rowids if the rowid is inaccessible
891 do_test shell1-4.1.5 {
896 CREATE TABLE t1(_ROWID_,rowid,oid);
897 INSERT INTO t1 VALUES(1,null,'alpha'), (12,'',99), (23,1,x'b0b1b2');
899 catchcmd test2.db {.dump --preserve-rowids}
900 } {0 {PRAGMA foreign_keys=OFF;
902 CREATE TABLE t1(_ROWID_,rowid,oid);
903 INSERT INTO t1 VALUES(1,NULL,'alpha');
904 INSERT INTO t1 VALUES(12,'',99);
905 INSERT INTO t1 VALUES(23,1,X'b0b1b2');
910 do_test shell1-4.1.6 {
915 CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
916 INSERT INTO t1 VALUES(1,null), (2,''), (3,1),
917 (4,2.25), (5,'hello'), (6,x'807f');
919 catchcmd test2.db {.dump --preserve-rowids}
920 } {1 {The --preserve-rowids option is not compatible with SQLITE_OMIT_VIRTUALTABLE}}
925 # Test the output of ".mode insert"
927 do_test shell1-4.2.1 {
928 catchcmd test.db ".mode insert t1\nselect * from t1;"
929 } {0 {INSERT INTO t1 VALUES(NULL);
930 INSERT INTO t1 VALUES('');
931 INSERT INTO t1 VALUES(1);
932 INSERT INTO t1 VALUES(2.25);
933 INSERT INTO t1 VALUES('hello');
934 INSERT INTO t1 VALUES(X'807f');}}
936 # Test the output of ".mode insert" with headers
938 do_test shell1-4.2.2 {
939 catchcmd test.db ".mode insert t1\n.headers on\nselect * from t1;"
940 } {0 {INSERT INTO t1(x) VALUES(NULL);
941 INSERT INTO t1(x) VALUES('');
942 INSERT INTO t1(x) VALUES(1);
943 INSERT INTO t1(x) VALUES(2.25);
944 INSERT INTO t1(x) VALUES('hello');
945 INSERT INTO t1(x) VALUES(X'807f');}}
947 # Test the output of ".mode insert"
949 do_test shell1-4.2.3 {
950 catchcmd test.db ".mode insert t3\nselect * from t3;"
951 } {0 {INSERT INTO t3 VALUES(1,NULL);
952 INSERT INTO t3 VALUES(2,'');
953 INSERT INTO t3 VALUES(3,1);
954 INSERT INTO t3 VALUES(4,2.25);
955 INSERT INTO t3 VALUES(5,'hello');
956 INSERT INTO t3 VALUES(6,X'807f');}}
958 # Test the output of ".mode insert" with headers
960 do_test shell1-4.2.4 {
961 catchcmd test.db ".mode insert t3\n.headers on\nselect * from t3;"
962 } {0 {INSERT INTO t3(x,y) VALUES(1,NULL);
963 INSERT INTO t3(x,y) VALUES(2,'');
964 INSERT INTO t3(x,y) VALUES(3,1);
965 INSERT INTO t3(x,y) VALUES(4,2.25);
966 INSERT INTO t3(x,y) VALUES(5,'hello');
967 INSERT INTO t3(x,y) VALUES(6,X'807f');}}
969 # Test the output of ".mode tcl"
976 PRAGMA encoding=UTF8;
978 INSERT INTO t1 VALUES(null), (''), (1), (2.25), ('hello'), (x'807f');
980 catchcmd test.db ".mode tcl\nselect * from t1;"
988 # Test the output of ".mode tcl" with multiple columns
992 CREATE TABLE t2(x,y);
993 INSERT INTO t2 VALUES(null, ''), (1, 2.25), ('hello', x'807f');
995 catchcmd test.db ".mode tcl\nselect * from t2;"
1000 # Test the output of ".mode tcl" with ".nullvalue"
1002 do_test shell1-4.5 {
1003 catchcmd test.db ".mode tcl\n.nullvalue NULL\nselect * from t2;"
1006 "hello" "\200\177"}}
1008 # Test the output of ".mode tcl" with Tcl reserved characters
1010 do_test shell1-4.6 {
1012 CREATE TABLE tcl1(x);
1013 INSERT INTO tcl1 VALUES('"'), ('['), (']'), ('\{'), ('\}'), (';'), ('$');
1015 foreach {x y} [catchcmd test.db ".mode tcl\nselect * from tcl1;"] break
1016 list $x $y [llength $y]
1025 # Test using arbitrary byte data with the shell via standard input/output.
1027 do_test shell1-5.0 {
1029 # NOTE: Skip NUL byte because it appears to be incompatible with command
1030 # shell argument parsing.
1032 for {set i 1} {$i < 256} {incr i} {
1034 # NOTE: Due to how the Tcl [exec] command works (i.e. where it treats
1035 # command channels opened for it as textual ones), the carriage
1036 # return character (and on Windows, the end-of-file character)
1037 # cannot be used here.
1039 if {$i==0x0D || ($tcl_platform(platform)=="windows" && $i==0x1A)} {
1042 # Tcl 8.7 maps 0x80 through 0x9f into valid UTF8. So skip those tests.
1043 if {$i>=0x80 && $i<=0x9f} continue
1044 if {$i>=0xE0 && $tcl_platform(os)=="OpenBSD"} continue
1045 if {$i>=0xE0 && $i<=0xEF && $tcl_platform(os)=="Linux"} continue
1046 set hex [format %02X $i]
1047 set char [subst \\x$hex]; set oldChar $char
1049 if {$tcl_platform(platform)=="windows"} {
1051 # NOTE: On Windows, we need to escape all the whitespace characters,
1052 # the alarm (\a) character, and those with special meaning to
1053 # the SQLite shell itself.
1056 \a \\a \b \\b \t \\t \n \\n \v \\v \f \\f \r \\r \
1057 " " "\" \"" \" \\\" ' \"'\" \\ \\\\]
1060 # NOTE: On Unix, we need to escape most of the whitespace characters
1061 # and those with special meaning to the SQLite shell itself.
1062 # The alarm (\a), backspace (\b), and carriage-return (\r)
1063 # characters do not appear to require escaping on Unix. For
1064 # the alarm and backspace characters, this is probably due to
1065 # differences in the command shell. For the carriage-return,
1066 # it is probably due to differences in how Tcl handles command
1067 # channel end-of-line translations.
1070 \t \\t \n \\n \v \\v \f \\f \
1071 " " "\" \"" \" \\\" ' \"'\" \\ \\\\]
1073 set char [string map $escapes $char]
1074 set x [catchcmdex test.db ".print $char\n"]
1075 set code [lindex $x 0]
1076 set res [lindex $x 1]
1078 error "failed with error: $res"
1080 if {$res ne "$oldChar\n"} {
1081 if {[llength $res] > 0} {
1082 set got [format %02X [scan $res %c]]
1086 error "failed with byte $hex mismatch, got $got"
1091 # These test cases do not work on MinGW
1094 # The string used here is the word "test" in Chinese.
1095 # In UTF-8, it is encoded as: \xE6\xB5\x8B\xE8\xAF\x95
1096 set test \u6D4B\u8BD5
1098 do_test shell1-6.0 {
1099 set fileName $test; append fileName .db
1100 catch {forcedelete $fileName}
1101 set x [catchcmdex $fileName "CREATE TABLE t1(x);\n.schema\n"]
1102 set code [lindex $x 0]
1103 set res [string trim [lindex $x 1]]
1105 error "failed with error: $res"
1107 if {$res ne "CREATE TABLE t1(x);"} {
1108 error "failed with mismatch: $res"
1110 if {![file exists $fileName]} {
1111 error "file \"$fileName\" (Unicode) does not exist"
1113 forcedelete $fileName
1116 do_test shell1-6.1 {
1117 catch {forcedelete test3.db}
1118 set x [catchcmdex test3.db \
1119 "CREATE TABLE [encoding convertto utf-8 $test](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 ${test}(x);"} {
1126 error "failed with mismatch: $res"
1128 forcedelete test3.db
1133 forcedelete test.db test.db-journal test.db-wal
1136 # The shell tool ".schema" command uses virtual table "pragma_database_list"
1140 do_test shell1-7.1.1 {
1142 CREATE TABLE Z (x TEXT PRIMARY KEY);
1143 CREATE TABLE _ (x TEXT PRIMARY KEY);
1144 CREATE TABLE YY (x TEXT PRIMARY KEY);
1145 CREATE TABLE __ (x TEXT PRIMARY KEY);
1146 CREATE TABLE WWW (x TEXT PRIMARY KEY);
1147 CREATE TABLE ___ (x TEXT PRIMARY KEY);
1150 do_test shell1-7.1.2 {
1151 catchcmd "test.db" ".schema _"
1152 } {0 {CREATE TABLE Z (x TEXT PRIMARY KEY);
1153 CREATE TABLE _ (x TEXT PRIMARY KEY);}}
1154 do_test shell1-7.1.3 {
1155 catchcmd "test.db" ".schema \\\\_"
1156 } {0 {CREATE TABLE _ (x TEXT PRIMARY KEY);}}
1157 do_test shell1-7.1.4 {
1158 catchcmd "test.db" ".schema __"
1159 } {0 {CREATE TABLE YY (x TEXT PRIMARY KEY);
1160 CREATE TABLE __ (x TEXT PRIMARY KEY);}}
1161 do_test shell1-7.1.5 {
1162 catchcmd "test.db" ".schema \\\\_\\\\_"
1163 } {0 {CREATE TABLE __ (x TEXT PRIMARY KEY);}}
1164 do_test shell1-7.1.6 {
1165 catchcmd "test.db" ".schema ___"
1166 } {0 {CREATE TABLE WWW (x TEXT PRIMARY KEY);
1167 CREATE TABLE ___ (x TEXT PRIMARY KEY);}}
1168 do_test shell1-7.1.7 {
1169 catchcmd "test.db" ".schema \\\\_\\\\_\\\\_"
1170 } {0 {CREATE TABLE ___ (x TEXT PRIMARY KEY);}}
1174 # Test case for the ieee754 and decimal extensions in the shell.
1175 # See the "floatingpoint.html" file in the documentation for more
1178 do_test shell1-8.1 {
1179 catchcmd ":memory:" {
1180 -- The pow2 table will hold all the necessary powers of two.
1181 CREATE TABLE pow2(x INTEGER PRIMARY KEY, v TEXT);
1182 WITH RECURSIVE c(x,v) AS (
1185 SELECT x+1, decimal_mul(v,'2') FROM c WHERE x+1<=971
1186 ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
1187 WITH RECURSIVE c(x,v) AS (
1190 SELECT x-1, decimal_mul(v,'0.5') FROM c WHERE x-1>=-1075
1191 ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
1193 -- This query finds the decimal representation of each value in the "c" table.
1194 WITH c(n) AS (VALUES(47.49))
1195 ----XXXXX----------- Replace with whatever you want
1196 SELECT decimal_mul(ieee754_mantissa(c.n),pow2.v)
1197 FROM pow2, c WHERE pow2.x=ieee754_exponent(c.n);
1199 } {0 47.49000000000000198951966012828052043914794921875}
1200 do_test shell1-8.2 {
1203 SELECT ieee754(47.49) AS x;
1205 } {0 {┌───────────────────────────────┐
1207 ├───────────────────────────────┤
1208 │ ieee754(6683623321994527,-47) │
1209 └───────────────────────────────┘}}
1210 do_test shell1-8.3 {
1211 catchcmd ":memory: --box" {
1212 select ieee754(6683623321994527,-47) as x;
1219 do_test shell1-8.4 {
1220 catchcmd ":memory: --table" {SELECT ieee754_mantissa(47.49) AS M, ieee754_exponent(47.49) AS E;}
1221 } {0 {+------------------+-----+
1223 +------------------+-----+
1224 | 6683623321994527 | -47 |
1225 +------------------+-----+}}