interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / test / shell1.test

1470 lines · 43073 bytes Raw Blame History
1# 2009 Nov 11
2#
3# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
5#
6# May you do good and not evil.
7# May you find forgiveness for yourself and forgive others.
8# May you share freely, never taking more than you give.
9#
10#***********************************************************************
11#
12# The focus of this file is testing the CLI shell tool.
13#
14# TESTRUNNER: shell
15#
16
17# Test plan:
18#
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.
24#
25set testdir [file dirname $argv0]
26source $testdir/tester.tcl
27set CLI [test_cli_invocation]
28db close
29forcedelete test.db test.db-journal test.db-wal
30sqlite3 db test.db
31
32#----------------------------------------------------------------------------
33# Test cases shell1-1.*: Basic command line option handling.
34#
35
36# invalid option
37do_test shell1-1.1.1 {
38 set res [catchcmd "-bad test.db" ""]
39 set rc [lindex $res 0]
40 list $rc \
41 [regexp {Error: unknown option: -bad} $res]
42} {1 1}
43do_test shell1-1.1.1b {
44 set res [catchcmd "test.db -bad" ""]
45 set rc [lindex $res 0]
46 list $rc \
47 [regexp {Error: unknown option: -bad} $res]
48} {1 1}
49# error on extra options
50do_test shell1-1.1.2 {
51 catchcmd "test.db \"select+3\" \"select+4\"" ""
52} {0 {3
534}}
54# error on extra options
55do_test shell1-1.1.3 {
56 catchcmd "test.db FOO test.db BAD" ".quit"
57} {/1 .Parse error in 2nd command line argument: near "FOO": syntax error*/}
58
59# -help
60do_test shell1-1.2.1 {
61 set res [catchcmd "-help test.db" ""]
62 set rc [lindex $res 0]
63 list $rc \
64 [regexp {Usage} $res] \
65 [regexp {\-init} $res] \
66 [regexp {\-version} $res]
67} {1 1 1 1}
68
69# -init filename read/process named file
70forcedelete FOO
71set out [open FOO w]
72puts $out ""
73close $out
74do_test shell1-1.3.1 {
75 catchcmd "-init FOO test.db" ""
76} {0 {}}
77do_test shell1-1.3.2 {
78 catchcmd "-init FOO test.db .quit BAD" ""
79} {0 {}}
80do_test shell1-1.3.3 {
81 catchcmd "-init FOO test.db BAD .quit" ""
82} {/1 .Parse error in 4th command line argument: near "BAD": syntax error*/}
83
84# -echo print commands before execution
85do_test shell1-1.4.1 {
86 catchcmd "-echo test.db" ""
87} {0 {}}
88
89# -[no]header turn headers on or off
90do_test shell1-1.5.1 {
91 catchcmd "-header test.db" ""
92} {0 {}}
93do_test shell1-1.5.2 {
94 catchcmd "-noheader test.db" ""
95} {0 {}}
96
97# -bail stop after hitting an error
98do_test shell1-1.6.1 {
99 catchcmd "-bail test.db" ""
100} {0 {}}
101
102# -interactive force interactive I/O
103do_test shell1-1.7.1 {
104 set res [catchcmd "-interactive test.db" ".quit"]
105 set rc [lindex $res 0]
106 list $rc \
107 [regexp {SQLite version} $res] \
108 [regexp {Enter ".help" for usage hints} $res]
109} {0 1 1}
110
111# -batch force batch I/O
112do_test shell1-1.8.1 {
113 catchcmd "-batch test.db" ""
114} {0 {}}
115
116# -column set output mode to 'column'
117do_test shell1-1.9.1 {
118 catchcmd "-column test.db" ""
119} {0 {}}
120
121# -csv set output mode to 'csv'
122do_test shell1-1.10.1 {
123 catchcmd "-csv test.db" ""
124} {0 {}}
125do_test shell1-1.10.2 {
126 catchcmd test.db "CREATE TABLE t1(a,b,c);INSERT INTO t1 VALUES(1,2,3),(4,5,6);"
127 catchcmd "-csv test.db" ".mode --rowsep \"\\012\"\nSELECT * FROM t1"
128} {0 {1,2,3
1294,5,6}}
130do_test shell1-1.10.3 {
131 catchcmd "-header -csv test.db" ".mode --rowsep \"\\012\"\nSELECT * FROM t1"
132} {0 {a,b,c
1331,2,3
1344,5,6}}
135do_test shell1-1.10.4 {
136 catchcmd "-csv -header test.db" ".mode --rowsep \"\\012\"\nSELECT * FROM t1"
137} {0 {a,b,c
1381,2,3
1394,5,6}}
140
141# -html set output mode to HTML
142do_test shell1-1.11.1 {
143 catchcmd "-html test.db" ""
144} {0 {}}
145
146# -line set output mode to 'line'
147do_test shell1-1.12.1 {
148 catchcmd "-line test.db" ""
149} {0 {}}
150
151# -list set output mode to 'list'
152do_test shell1-1.13.1 {
153 catchcmd "-list test.db" ""
154} {0 {}}
155
156# -separator 'x' set output field separator (|)
157do_test shell1-1.14.1 {
158 catchcmd "-separator 'x' test.db" ""
159} {0 {}}
160do_test shell1-1.14.2 {
161 catchcmd "-separator x test.db" ""
162} {0 {}}
163do_test shell1-1.14.3 {
164 set res [catchcmd "-separator" ""]
165 set rc [lindex $res 0]
166 list $rc \
167 [regexp {Error: missing argument to -separator} $res]
168} {1 1}
169
170# -stats print memory stats before each finalize
171do_test shell1-1.14b.1 {
172 catchcmd "-stats test.db" ""
173} {0 {}}
174
175# -nullvalue 'text' set text string for NULL values
176do_test shell1-1.15.1 {
177 catchcmd "-nullvalue 'x' test.db" ""
178} {0 {}}
179do_test shell1-1.15.2 {
180 catchcmd "-nullvalue x test.db" ""
181} {0 {}}
182do_test shell1-1.15.3 {
183 set res [catchcmd "-nullvalue" ""]
184 set rc [lindex $res 0]
185 list $rc \
186 [regexp {Error: missing argument to -nullvalue} $res]
187} {1 1}
188
189# -version show SQLite version
190do_test shell1-1.16.1 {
191 set x [catchcmd "-version test.db" ""]
192} {/3.[0-9.]+ 20\d\d-[01]\d-\d\d \d\d:\d\d:\d\d [0-9a-f]+/}
193
194# Handle no-more-options option
195forcedelete ./--db
196do_test shell1-1.17.1 {
197 catchcmd {-- --db "CREATE TABLE T(c1);"}
198} {0 {}}
199do_test shell1-1.17.2 {
200 catchcmd {-- --db "SELECT name from sqlite_schema;"}
201} {0 T}
202forcedelete ./--db
203
204#----------------------------------------------------------------------------
205# Test cases shell1-2.*: Basic "dot" command token parsing.
206#
207
208# check first token handling
209do_test shell1-2.1.1 {
210 catchcmd "test.db" ".foo"
211} {1 {Error: unknown command or invalid arguments: "foo". Enter ".help" for help}}
212do_test shell1-2.1.2 {
213 catchcmd "test.db" ".\"foo OFF\""
214} {1 {Error: unknown command or invalid arguments: "foo OFF". Enter ".help" for help}}
215do_test shell1-2.1.3 {
216 catchcmd "test.db" ".\'foo OFF\'"
217} {1 {Error: unknown command or invalid arguments: "foo OFF". Enter ".help" for help}}
218
219# unbalanced quotes
220do_test shell1-2.2.1 {
221 catchcmd "test.db" ".\"foo OFF"
222} {1 {Error: unknown command or invalid arguments: "foo OFF". Enter ".help" for help}}
223do_test shell1-2.2.2 {
224 catchcmd "test.db" ".\'foo OFF"
225} {1 {Error: unknown command or invalid arguments: "foo OFF". Enter ".help" for help}}
226do_test shell1-2.2.3 {
227 catchcmd "test.db" ".explain \"OFF"
228} {0 {}}
229do_test shell1-2.2.4 {
230 catchcmd "test.db" ".explain \'OFF"
231} {0 {}}
232do_test shell1-2.2.5 {
233 catchcmd "test.db" ".mode \"insert FOO"
234} {1 {line 1: .mode "insert FOO
235line 1: ^--- unknown mode
236line 1: Use ".help .mode" for more info}}
237do_test shell1-2.2.6 {
238 catchcmd "test.db" ".mode \'insert FOO"
239} {1 {line 1: .mode 'insert FOO
240line 1: ^--- unknown mode
241line 1: Use ".help .mode" for more info}}
242
243# check multiple tokens, and quoted tokens
244do_test shell1-2.3.1 {
245 catchcmd "test.db" ".explain 1"
246} {0 {}}
247do_test shell1-2.3.2 {
248 catchcmd "test.db" ".explain on"
249} {0 {}}
250do_test shell1-2.3.3 {
251 catchcmd "test.db" ".explain \"1 2 3\""
252} {1 {ERROR: Not a boolean value: "1 2 3". Assuming "no".}}
253do_test shell1-2.3.4 {
254 catchcmd "test.db" ".explain \"OFF\""
255} {0 {}}
256do_test shell1-2.3.5 {
257 catchcmd "test.db" ".\'explain\' \'OFF\'"
258} {0 {}}
259do_test shell1-2.3.6 {
260 catchcmd "test.db" ".explain \'OFF\'"
261} {0 {}}
262do_test shell1-2.3.7 {
263 catchcmd "test.db" ".\'explain\' \'OFF\'"
264} {0 {}}
265
266# check quoted args are unquoted
267do_test shell1-2.4.1 {
268 catchcmd "test.db" ".mode FOO"
269} {1 {line 1: .mode FOO
270line 1: ^--- unknown mode
271line 1: Use ".help .mode" for more info}}
272do_test shell1-2.4.2 {
273 catchcmd "test.db" ".mode csv"
274} {0 {}}
275do_test shell1-2.4.2 {
276 catchcmd "test.db" ".mode \"csv\""
277} {0 {}}
278
279# check that certain quoted arg escapes work
280do_test shell1-2.5.1 {
281 catchcmd ":memory:" ".print \"\\060\\077 \\x3f\\x30 \\a\\t\""
282} [list 0 "0? ?0 \a\t"]
283
284
285#----------------------------------------------------------------------------
286# Test cases shell1-3.*: Basic test that "dot" command can be called.
287#
288
289# .backup ?DB? FILE Backup DB (default "main") to FILE
290do_test shell1-3.1.1 {
291 catchcmd "test.db" ".backup"
292} {1 {missing FILENAME argument on .backup}}
293forcedelete FOO
294do_test shell1-3.1.2 {
295 catchcmd "test.db" ".backup FOO"
296} {0 {}}
297do_test shell1-3.1.3 {
298 catchcmd "test.db" ".backup FOO BAR"
299} {1 {Error: unknown database FOO}}
300do_test shell1-3.1.4 {
301 # too many arguments
302 catchcmd "test.db" ".backup FOO BAR BAD"
303} {1 {Usage: .backup ?DB? ?OPTIONS? FILENAME}}
304
305# .bail ON|OFF Stop after hitting an error. Default OFF
306do_test shell1-3.2.1 {
307 catchcmd "test.db" ".bail"
308} {1 {Usage: .bail on|off}}
309do_test shell1-3.2.2 {
310 catchcmd "test.db" ".bail ON"
311} {0 {}}
312do_test shell1-3.2.3 {
313 catchcmd "test.db" ".bail OFF"
314} {0 {}}
315do_test shell1-3.2.4 {
316 # too many arguments
317 catchcmd "test.db" ".bail OFF BAD"
318} {1 {Usage: .bail on|off}}
319
320ifcapable vtab {
321# .databases List names and files of attached databases
322do_test shell1-3.3.1 {
323 catchcmd "-csv test.db" ".databases"
324} "/0.+main.+[string map {/ ".{1,2}"} [string range [get_pwd] 0 10]].*/"
325do_test shell1-3.3.2 {
326 # extra arguments ignored
327 catchcmd "test.db" ".databases BAD"
328} "/0.+main.+[string map {/ ".{1,2}"} [string range [get_pwd] 0 10]].*/"
329}
330
331# .dump ?TABLE? ... Dump the database in an SQL text format
332# If TABLE specified, only dump tables matching
333# LIKE pattern TABLE.
334do_test shell1-3.4.1 {
335 set res [catchcmd "test.db" ".dump"]
336 list [regexp {BEGIN TRANSACTION;} $res] \
337 [regexp {COMMIT;} $res]
338} {1 1}
339do_test shell1-3.4.2 {
340 set res [catchcmd "test.db" ".dump FOO"]
341 list [regexp {BEGIN TRANSACTION;} $res] \
342 [regexp {COMMIT;} $res]
343} {1 1}
344# The .dump command now accepts multiple arguments
345#do_test shell1-3.4.3 {
346# # too many arguments
347# catchcmd "test.db" ".dump FOO BAD"
348#} {1 {Usage: .dump ?--preserve-rowids? ?--newlines? ?LIKE-PATTERN?}}
349
350# .echo ON|OFF Turn command echo on or off
351do_test shell1-3.5.1 {
352 catchcmd "test.db" ".echo"
353} {1 {Usage: .echo on|off}}
354do_test shell1-3.5.2 {
355 catchcmd "test.db" ".echo ON"
356} {0 {}}
357do_test shell1-3.5.3 {
358 catchcmd "test.db" ".echo OFF"
359} {0 {}}
360do_test shell1-3.5.4 {
361 # too many arguments
362 catchcmd "test.db" ".echo OFF BAD"
363} {1 {Usage: .echo on|off}}
364
365# .exit Exit this program
366do_test shell1-3.6.1 {
367 catchcmd "test.db" ".exit"
368} {0 {}}
369do_test shell1-3.6.2 {
370 catchcmd test.db ".output memory\n.exit 7"
371} {1 {child process exited abnormally}}
372
373# .explain ON|OFF Turn output mode suitable for EXPLAIN on or off.
374do_test shell1-3.7.1 {
375 catchcmd "test.db" ".explain"
376 # explain is the exception to the booleans. without an option, it turns it on.
377} {0 {}}
378do_test shell1-3.7.2 {
379 catchcmd "test.db" ".explain ON"
380} {0 {}}
381do_test shell1-3.7.3 {
382 catchcmd "test.db" ".explain OFF"
383} {0 {}}
384do_test shell1-3.7.4 {
385 # extra arguments ignored
386 catchcmd "test.db" ".explain OFF BAD"
387} {0 {}}
388
389# .header(s) ON|OFF Turn display of headers on or off
390do_test shell1-3.9.1 {
391 catchcmd "test.db" ".header"
392} {1 {Usage: .headers off|on|always}}
393do_test shell1-3.9.2 {
394 catchcmd "test.db" ".header ON"
395} {0 {}}
396do_test shell1-3.9.3 {
397 catchcmd "test.db" ".header OFF"
398} {0 {}}
399do_test shell1-3.9.4 {
400 # too many arguments
401 catchcmd "test.db" ".header OFF BAD"
402} {1 {Usage: .headers off|on|always}}
403
404do_test shell1-3.9.5 {
405 catchcmd "test.db" ".headers"
406} {1 {Usage: .headers off|on|always}}
407do_test shell1-3.9.6 {
408 catchcmd "test.db" ".headers ON"
409} {0 {}}
410do_test shell1-3.9.7 {
411 catchcmd "test.db" ".headers OFF"
412} {0 {}}
413do_test shell1-3.9.8 {
414 # too many arguments
415 catchcmd "test.db" ".headers OFF BAD"
416} {1 {Usage: .headers off|on|always}}
417
418# .help Show this message
419do_test shell1-3.10.1 {
420 set res [catchcmd "test.db" ".help"]
421 # look for a few of the possible help commands
422 list [regexp {.help} $res] \
423 [regexp {.quit} $res] \
424 [regexp {.mode} $res]
425} {1 1 1}
426do_test shell1-3.10.2 {
427 # we allow .help to take extra args (it is help after all)
428 set res [catchcmd "test.db" ".help *"]
429 # look for a few of the possible help commands
430 list [regexp {.help} $res] \
431 [regexp {.quit} $res] \
432 [regexp {.mode} $res]
433} {1 1 1}
434
435# .import FILE TABLE Import data from FILE into TABLE
436do_test shell1-3.11.1 {
437 catchcmd "test.db" ".import"
438} {/1 .line 1: Missing FILE argument.*/}
439do_test shell1-3.11.2 {
440 catchcmd "test.db" ".import FOO"
441} {/1 .line 1: Missing TABLE argument.*/}
442do_test shell1-3.11.3 {
443 # too many arguments
444 catchcmd "test.db" ".import FOO BAR BAD"
445} {1 {line 1: .import FOO BAR BAD
446line 1: ^--- unknown argument}}
447do_test shell1-3.11.4 {
448 catchcmd "test.db" ".import <<END t1\na,b,c\n1,2,3"
449} {1 {line 1: Content terminator "END" not found.}}
450
451# .indexes ?TABLE? Show names of all indexes
452# If TABLE specified, only show indexes for tables
453# matching LIKE pattern TABLE.
454do_test shell1-3.12.1 {
455 catchcmd "test.db" ".indexes"
456} {0 {}}
457do_test shell1-3.12.2 {
458 catchcmd "test.db" ".indexes FOO"
459} {0 {}}
460do_test shell1-3.12.2-legacy {
461 catchcmd "test.db" ".indices FOO"
462} {0 {}}
463do_test shell1-3.12.3 {
464 # too many arguments
465 catchcmd "test.db" ".indexes FOO BAD"
466} {1 {line 1: .indexes FOO BAD
467line 1: ^--- unknown argument}}
468
469# .mode MODE ?TABLE? Set output mode where MODE is one of:
470# ascii Columns/rows delimited by 0x1F and 0x1E
471# csv Comma-separated values
472# column Left-aligned columns. (See .width)
473# html HTML <table> code
474# insert SQL insert statements for TABLE
475# line One value per line
476# list Values delimited by .separator strings
477# tabs Tab-separated values
478# tcl TCL list elements
479do_test shell1-3.13.1 {
480 catchcmd "test.db" ".mode batch\n.mode"
481} {0 {.mode list}}
482do_test shell1-3.13.2 {
483 catchcmd "test.db" ".mode FOO"
484} {1 {line 1: .mode FOO
485line 1: ^--- unknown mode
486line 1: Use ".help .mode" for more info}}
487do_test shell1-3.13.3 {
488 catchcmd "test.db" ".mode csv"
489} {0 {}}
490do_test shell1-3.13.4 {
491 catchcmd "test.db" ".mode column"
492} {0 {}}
493do_test shell1-3.13.5 {
494 catchcmd "test.db" ".mode html"
495} {0 {}}
496do_test shell1-3.13.6 {
497 catchcmd "test.db" ".mode insert"
498} {0 {}}
499do_test shell1-3.13.7 {
500 catchcmd "test.db" ".mode line"
501} {0 {}}
502do_test shell1-3.13.8 {
503 catchcmd "test.db" ".mode list"
504} {0 {}}
505do_test shell1-3.13.9 {
506 catchcmd "test.db" ".mode tabs"
507} {0 {}}
508do_test shell1-3.13.10 {
509 catchcmd "test.db" ".mode tcl"
510} {0 {}}
511do_test shell1-3.13.11 {
512 # extra arguments ignored
513 catchcmd "test.db" ".mode tcl BAD"
514} {0 {}}
515
516# .nullvalue STRING Print STRING in place of NULL values
517do_test shell1-3.14.1 {
518 catchcmd "test.db" ".nullvalue"
519} {1 {Usage: .nullvalue STRING}}
520do_test shell1-3.14.2 {
521 catchcmd "test.db" ".nullvalue FOO"
522} {0 {}}
523do_test shell1-3.14.3 {
524 # too many arguments
525 catchcmd "test.db" ".nullvalue FOO BAD"
526} {1 {Usage: .nullvalue STRING}}
527
528# .output FILENAME Send output to FILENAME
529do_test shell1-3.15.1 {
530 catchcmd "test.db" ".output
531.print x"
532} {0 x}
533do_test shell1-3.15.2 {
534 catchcmd "test.db" ".mode batch\n.output FOO
535.print x
536.output
537SELECT readfile('FOO');"
538} {0 {x
539}}
540do_test shell1-3.15.3 {
541 # too many arguments
542 catchcmd "test.db" ".output FOO BAD"
543} {1 {line 1: .output FOO BAD
544line 1: ^--- surplus argument}}
545
546# .output stdout Send output to the screen
547do_test shell1-3.16.1 {
548 catchcmd "test.db" ".output stdout"
549} {0 {}}
550do_test shell1-3.16.2 {
551 # too many arguments
552 catchcmd "test.db" ".output stdout BAD"
553} {1 {line 1: .output stdout BAD
554line 1: ^--- surplus argument}}
555
556# .prompt MAIN CONTINUE Replace the standard prompts
557do_test shell1-3.17.1 {
558 catchcmd "test.db" ".prompt"
559} {0 {}}
560do_test shell1-3.17.2 {
561 catchcmd "test.db" ".prompt FOO"
562} {0 {}}
563do_test shell1-3.17.3 {
564 catchcmd "test.db" ".prompt FOO BAR"
565} {0 {}}
566do_test shell1-3.17.4 {
567 # too many arguments
568 catchcmd "test.db" ".prompt FOO BAR BAD"
569} {1 {line 1: .prompt FOO BAR BAD
570line 1: ^--- extra argument}}
571
572# .quit Exit this program
573do_test shell1-3.18.1 {
574 catchcmd "test.db" ".quit"
575} {0 {}}
576do_test shell1-3.18.2 {
577 # too many arguments
578 catchcmd "test.db" ".quit BAD"
579} {0 {}}
580
581# .read FILENAME Execute SQL in FILENAME
582do_test shell1-3.19.1 {
583 catchcmd "test.db" ".read"
584} {1 {Usage: .read FILE}}
585do_test shell1-3.19.2 {
586 forcedelete FOO
587 catchcmd "test.db" ".read FOO"
588} {1 {Error: cannot open "FOO"}}
589do_test shell1-3.19.3 {
590 # too many arguments
591 catchcmd "test.db" ".read FOO BAD"
592} {1 {Usage: .read FILE}}
593
594# .restore ?DB? FILE Restore content of DB (default "main") from FILE
595do_test shell1-3.20.1 {
596 catchcmd "test.db" ".restore"
597} {1 {Usage: .restore ?DB? FILE}}
598do_test shell1-3.20.2 {
599 catchcmd "test.db" ".restore FOO"
600} {0 {}}
601do_test shell1-3.20.3 {
602 catchcmd "test.db" ".restore FOO BAR"
603} {1 {Error: unknown database FOO}}
604do_test shell1-3.20.4 {
605 # too many arguments
606 catchcmd "test.db" ".restore FOO BAR BAD"
607} {1 {Usage: .restore ?DB? FILE}}
608
609ifcapable vtab {
610# .schema ?TABLE? Show the CREATE statements
611# If TABLE specified, only show tables matching
612# LIKE pattern TABLE.
613do_test shell1-3.21.1 {
614 catchcmd "test.db" ".schema"
615} {0 {}}
616do_test shell1-3.21.2 {
617 catchcmd "test.db" ".schema FOO"
618} {0 {}}
619do_test shell1-3.21.3 {
620 # too many arguments
621 catchcmd "test.db" ".schema FOO BAD"
622} {1 {Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?}}
623
624do_test shell1-3.21.4 {
625 catchcmd "test.db" {
626 CREATE TABLE t1(x);
627 CREATE VIEW v2 AS SELECT x+1 AS y FROM t1;
628 CREATE VIEW v1 AS SELECT y+1 FROM v2;
629 }
630 catchcmd "test.db" ".schema"
631} {0 {CREATE TABLE t1(x);
632CREATE VIEW v2 AS SELECT x+1 AS y FROM t1;
633/* v2(y) */
634CREATE VIEW v1 AS SELECT y+1 FROM v2;
635/* v1("y+1") */}}
636
637 catch {db eval {DROP VIEW v1; DROP VIEW v2; DROP TABLE t1;}}
638}
639do_test shell1-3.21.5 {
640 exec {*}$CLI -noinit test.db \
641 {CREATE TABLE t2(a INTEGER PRIMARY KEY, b BLOB DEFAULT(jsonb('[]')),c TEXT NOT NULL)STRICT;} \
642 {.schema -indent t2}
643} {CREATE TABLE t2(
644 a INTEGER PRIMARY KEY,
645 b BLOB DEFAULT(jsonb('[]')),
646 c TEXT NOT NULL
647)STRICT;}
648do_test shell1-3.21.6 {
649 exec {*}$CLI -noinit test.db \
650 {DROP TABLE t2;} \
651 {.schema -indent t2}
652} {}
653do_test shell1-3.21.7 {
654 catchcmd "test7.db" {
655 CREATE TABLE t7(x);
656 CREATE VIEW v7("a*/;SELECT 12345;/*") AS SELECT 234;
657 }
658 catchcmd "test7.db" ".schema"
659} {0 {CREATE TABLE t7(x);
660CREATE VIEW v7("a*/;SELECT 12345;/*") AS SELECT 234;
661/* v7("a**;SELECT 12345;/*") */}}
662forcedelete test7.db
663
664# .separator STRING Change column separator used by output and .import
665do_test shell1-3.22.1 {
666 catchcmd "test.db" ".separator"
667} {1 {Usage: .separator COL ?ROW?}}
668do_test shell1-3.22.2 {
669 catchcmd "test.db" ".separator FOO"
670} {0 {}}
671do_test shell1-3.22.3 {
672 catchcmd "test.db" ".separator ABC XYZ"
673} {0 {}}
674do_test shell1-3.22.4 {
675 # too many arguments
676 catchcmd "test.db" ".separator FOO BAD BAD2"
677} {1 {Usage: .separator COL ?ROW?}}
678
679# .show Show the current values for various settings
680do_test shell1-3.23.1 {
681 set res [catchcmd "test.db" ".mode batch\n.show"]
682 list [regexp {echo:} $res] \
683 [regexp {explain:} $res] \
684 [regexp {headers:} $res] \
685 [regexp {mode:} $res] \
686 [regexp {nullvalue:} $res] \
687 [regexp {output:} $res] \
688 [regexp {colseparator:} $res] \
689 [regexp {rowseparator:} $res] \
690 [regexp {stats:} $res] \
691 [regexp {width:} $res]
692} {1 1 1 1 1 1 1 1 1 1}
693do_test shell1-3.23.2 {
694 # too many arguments
695 catchcmd "test.db" ".show BAD"
696} {1 {Usage: .show}}
697
698# .stats ON|OFF Turn stats on or off
699#do_test shell1-3.23b.1 {
700# catchcmd "test.db" ".stats"
701#} {1 {Usage: .stats on|off|stmt|vmstep}}
702do_test shell1-3.23b.2 {
703 catchcmd "test.db" ".stats ON"
704} {0 {}}
705do_test shell1-3.23b.3 {
706 catchcmd "test.db" ".stats OFF"
707} {0 {}}
708do_test shell1-3.23b.4 {
709 # too many arguments
710 catchcmd "test.db" ".stats OFF BAD"
711} {1 {line 1: .stats OFF BAD
712line 1: ^--- unknown argument}}
713
714# Ticket 7be932dfa60a8a6b3b26bcf7623ec46e0a403ddb 2018-06-07
715# Adverse interaction between .stats and .eqp
716#
717do_test shell1-3.23b.5 {
718 catchcmd "test.db" [string map {"\n " "\n"} {.mode batch
719 CREATE TEMP TABLE t1(x);
720 INSERT INTO t1 VALUES(1),(2);
721 .stats on
722 .eqp full
723 SELECT * FROM t1;
724 }]
725} {/1\n2\n/}
726
727# .tables ?TABLE? List names of tables
728# If TABLE specified, only list tables matching
729# LIKE pattern TABLE.
730do_test shell1-3.24.1 {
731 catchcmd "test.db" ".tables"
732} {0 {}}
733do_test shell1-3.24.2 {
734 catchcmd "test.db" ".tables FOO"
735} {0 {}}
736do_test shell1-3.24.3 {
737 # too many arguments
738 catchcmd "test.db" ".tables FOO BAD"
739} {0 {}}
740
741# .timeout MS Try opening locked tables for MS milliseconds
742do_test shell1-3.25.1 {
743 catchcmd "test.db" ".timeout"
744} {0 {}}
745do_test shell1-3.25.2 {
746 catchcmd "test.db" ".timeout zzz"
747 # this should be treated the same as a '0' timeout
748} {0 {}}
749do_test shell1-3.25.3 {
750 catchcmd "test.db" ".timeout 1"
751} {0 {}}
752do_test shell1-3.25.4 {
753 # too many arguments
754 catchcmd "test.db" ".timeout 1 BAD"
755} {0 {}}
756
757# .width NUM NUM ... Set column widths for "column" mode
758do_test shell1-3.26.1 {
759 catchcmd "test.db" ".width"
760} {0 {}}
761do_test shell1-3.26.2 {
762 catchcmd "test.db" ".width xxx"
763 # this should be treated the same as a '0' width for col 1
764} {0 {}}
765do_test shell1-3.26.3 {
766 catchcmd "test.db" ".width xxx yyy"
767 # this should be treated the same as a '0' width for col 1 and 2
768} {0 {}}
769do_test shell1-3.26.4 {
770 catchcmd "test.db" ".width 1 1"
771 # this should be treated the same as a '1' width for col 1 and 2
772} {0 {}}
773do_test shell1-3.26.5 {
774 catchcmd "test.db" ".mode column\n.header off\n.width 10 -10\nSELECT 'abcdefg', 123456;"
775 # this should be treated the same as a '1' width for col 1 and 2
776} {0 {abcdefg 123456}}
777do_test shell1-3.26.6 {
778 catchcmd "test.db" ".mode column\n.header off\n.width -10 10\nSELECT 'abcdefg', 123456;"
779 # this should be treated the same as a '1' width for col 1 and 2
780} {0 { abcdefg 123456}}
781
782
783# .timer ON|OFF Turn the CPU timer measurement on or off
784do_test shell1-3.27.1 {
785 catchcmd "test.db" ".timer"
786} {1 {Usage: .timer on|off|once}}
787do_test shell1-3.27.2 {
788 catchcmd "test.db" ".timer ON"
789} {0 {}}
790do_test shell1-3.27.3 {
791 catchcmd "test.db" ".timer OFF"
792} {0 {}}
793do_test shell1-3.27.4 {
794 # too many arguments
795 catchcmd "test.db" ".timer OFF BAD"
796} {1 {Usage: .timer on|off|once}}
797
798do_test shell1-3.28.1 {
799 catchcmd test.db \
800 ".mode batch\n.log stdout\nSELECT coalesce(sqlite_log(123,'hello'),'456');"
801} "0 {(123) hello\n456}"
802
803do_test shell1-3-29.1 {
804 catchcmd "test.db" ".print this is a test"
805} {0 {this is a test}}
806
807# dot-command argument quoting
808do_test shell1-3-30.1 {
809 catchcmd {test.db} {.print "this\"is'a\055test" 'this\"is\\a\055test'}
810} {0 {this"is'a-test this\"is\\a\055test}}
811do_test shell1-3-31.1 {
812 catchcmd {test.db} {.print "this\nis\ta\\test" 'this\nis\ta\\test'}
813} [list 0 "this\nis\ta\\test this\\nis\\ta\\\\test"]
814
815
816# Test the output of the ".dump" command
817#
818do_test shell1-4.1 {
819 db close
820 forcedelete test.db
821 sqlite3 db test.db
822 db eval {
823 PRAGMA encoding=UTF16;
824 CREATE TABLE t1(x);
825 INSERT INTO t1 VALUES(null), (''), (1), (2.25), ('hello'), (x'807f');
826 CREATE TABLE t3(x,y);
827 INSERT INTO t3 VALUES(1,null), (2,''), (3,1),
828 (4,2.25), (5,'hello'), (6,x'807f');
829 }
830 catchcmd test.db {.dump}
831} {0 {PRAGMA foreign_keys=OFF;
832BEGIN TRANSACTION;
833CREATE TABLE t1(x);
834INSERT INTO t1 VALUES(NULL);
835INSERT INTO t1 VALUES('');
836INSERT INTO t1 VALUES(1);
837INSERT INTO t1 VALUES(2.25);
838INSERT INTO t1 VALUES('hello');
839INSERT INTO t1 VALUES(x'807f');
840CREATE TABLE t3(x,y);
841INSERT INTO t3 VALUES(1,NULL);
842INSERT INTO t3 VALUES(2,'');
843INSERT INTO t3 VALUES(3,1);
844INSERT INTO t3 VALUES(4,2.25);
845INSERT INTO t3 VALUES(5,'hello');
846INSERT INTO t3 VALUES(6,x'807f');
847COMMIT;}}
848
849
850ifcapable vtab {
851
852# The --preserve-rowids option to .dump
853#
854do_test shell1-4.1.1 {
855 catchcmd test.db {.dump --preserve-rowids}
856} {0 {PRAGMA foreign_keys=OFF;
857BEGIN TRANSACTION;
858CREATE TABLE t1(x);
859INSERT INTO t1(rowid,x) VALUES(1,NULL);
860INSERT INTO t1(rowid,x) VALUES(2,'');
861INSERT INTO t1(rowid,x) VALUES(3,1);
862INSERT INTO t1(rowid,x) VALUES(4,2.25);
863INSERT INTO t1(rowid,x) VALUES(5,'hello');
864INSERT INTO t1(rowid,x) VALUES(6,x'807f');
865CREATE TABLE t3(x,y);
866INSERT INTO t3(rowid,x,y) VALUES(1,1,NULL);
867INSERT INTO t3(rowid,x,y) VALUES(2,2,'');
868INSERT INTO t3(rowid,x,y) VALUES(3,3,1);
869INSERT INTO t3(rowid,x,y) VALUES(4,4,2.25);
870INSERT INTO t3(rowid,x,y) VALUES(5,5,'hello');
871INSERT INTO t3(rowid,x,y) VALUES(6,6,x'807f');
872COMMIT;}}
873
874# If the table contains an INTEGER PRIMARY KEY, do not record a separate
875# rowid column in the output.
876#
877do_test shell1-4.1.2 {
878 db close
879 forcedelete test2.db
880 sqlite3 db test2.db
881 db eval {
882 CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
883 INSERT INTO t1 VALUES(1,null), (2,''), (3,1),
884 (4,2.25), (5,'hello'), (6,x'807f');
885 }
886 catchcmd test2.db {.dump --preserve-rowids}
887} {0 {PRAGMA foreign_keys=OFF;
888BEGIN TRANSACTION;
889CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
890INSERT INTO t1(x,y) VALUES(1,NULL);
891INSERT INTO t1(x,y) VALUES(2,'');
892INSERT INTO t1(x,y) VALUES(3,1);
893INSERT INTO t1(x,y) VALUES(4,2.25);
894INSERT INTO t1(x,y) VALUES(5,'hello');
895INSERT INTO t1(x,y) VALUES(6,x'807f');
896COMMIT;}}
897
898# Verify that the table named [table] is correctly quoted and that
899# an INTEGER PRIMARY KEY DESC is not an alias for the rowid.
900#
901do_test shell1-4.1.3 {
902 db close
903 forcedelete test2.db
904 sqlite3 db test2.db
905 db eval {
906 CREATE TABLE [table](x INTEGER PRIMARY KEY DESC, y);
907 INSERT INTO [table] VALUES(1,null), (12,''), (23,1),
908 (34,2.25), (45,'hello'), (56,x'807f');
909 }
910 catchcmd test2.db {.dump --preserve-rowids}
911} {0 {PRAGMA foreign_keys=OFF;
912BEGIN TRANSACTION;
913CREATE TABLE [table](x INTEGER PRIMARY KEY DESC, y);
914INSERT INTO "table"(rowid,x,y) VALUES(1,1,NULL);
915INSERT INTO "table"(rowid,x,y) VALUES(2,12,'');
916INSERT INTO "table"(rowid,x,y) VALUES(3,23,1);
917INSERT INTO "table"(rowid,x,y) VALUES(4,34,2.25);
918INSERT INTO "table"(rowid,x,y) VALUES(5,45,'hello');
919INSERT INTO "table"(rowid,x,y) VALUES(6,56,x'807f');
920COMMIT;}}
921
922# Do not record rowids for a WITHOUT ROWID table. Also check correct quoting
923# of table names that contain odd characters.
924#
925do_test shell1-4.1.4 {
926 db close
927 forcedelete test2.db
928 sqlite3 db test2.db
929 db eval {
930 CREATE TABLE [ta<>ble](x INTEGER PRIMARY KEY, y) WITHOUT ROWID;
931 INSERT INTO [ta<>ble] VALUES(1,null), (12,''), (23,1),
932 (34,2.25), (45,'hello'), (56,x'807f');
933 }
934 catchcmd test2.db {.dump --preserve-rowids}
935} {0 {PRAGMA foreign_keys=OFF;
936BEGIN TRANSACTION;
937CREATE TABLE [ta<>ble](x INTEGER PRIMARY KEY, y) WITHOUT ROWID;
938INSERT INTO "ta<>ble"(x,y) VALUES(1,NULL);
939INSERT INTO "ta<>ble"(x,y) VALUES(12,'');
940INSERT INTO "ta<>ble"(x,y) VALUES(23,1);
941INSERT INTO "ta<>ble"(x,y) VALUES(34,2.25);
942INSERT INTO "ta<>ble"(x,y) VALUES(45,'hello');
943INSERT INTO "ta<>ble"(x,y) VALUES(56,x'807f');
944COMMIT;}}
945
946# Do not record rowids if the rowid is inaccessible
947#
948do_test shell1-4.1.5 {
949 db close
950 forcedelete test2.db
951 sqlite3 db test2.db
952 db eval {
953 CREATE TABLE t1(_ROWID_,rowid,oid);
954 INSERT INTO t1 VALUES(1,null,'alpha'), (12,'',99), (23,1,x'b0b1b2');
955 }
956 catchcmd test2.db {.dump --preserve-rowids}
957} {0 {PRAGMA foreign_keys=OFF;
958BEGIN TRANSACTION;
959CREATE TABLE t1(_ROWID_,rowid,oid);
960INSERT INTO t1(_ROWID_,rowid,oid) VALUES(1,NULL,'alpha');
961INSERT INTO t1(_ROWID_,rowid,oid) VALUES(12,'',99);
962INSERT INTO t1(_ROWID_,rowid,oid) VALUES(23,1,x'b0b1b2');
963COMMIT;}}
964
965} else {
966
967do_test shell1-4.1.6 {
968 db close
969 forcedelete test2.db
970 sqlite3 db test2.db
971 db eval {
972 CREATE TABLE t1(x INTEGER PRIMARY KEY, y);
973 INSERT INTO t1 VALUES(1,null), (2,''), (3,1),
974 (4,2.25), (5,'hello'), (6,x'807f');
975 }
976 catchcmd test2.db {.dump --preserve-rowids}
977} {/.* --preserve-rowids option is not compatible with SQLITE_OMIT_VIRTUALTABLE/}
978
979}
980
981# DELETE content of sqlite_sequence prior to repopulating,
982# but only if the sqlite_sequence table is non-empty.
983# Forum: 2024-10-13T17:10:01z and 2025-10-29T19:38:43z
984#
985do_test shell1-4.1.7 {
986 db close
987 forcedelete test2.db
988 sqlite3 db test2.db
989 db eval {
990 CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT, b);
991 INSERT INTO t1 VALUES(1,2),(20,21),(15,16);
992 }
993 catchcmd test2.db {.dump}
994} {0 {PRAGMA foreign_keys=OFF;
995BEGIN TRANSACTION;
996CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT, b);
997INSERT INTO t1 VALUES(1,2);
998INSERT INTO t1 VALUES(15,16);
999INSERT INTO t1 VALUES(20,21);
1000PRAGMA writable_schema=ON;
1001CREATE TABLE IF NOT EXISTS sqlite_sequence(name,seq);
1002DELETE FROM sqlite_sequence;
1003INSERT INTO sqlite_sequence VALUES('t1',20);
1004PRAGMA writable_schema=OFF;
1005COMMIT;}}
1006do_test shell1-4.1.8 {
1007 db close
1008 forcedelete test2.db
1009 sqlite3 db test2.db
1010 db eval {
1011 CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT, b);
1012 INSERT INTO t1 VALUES(1,2),(20,21),(15,16);
1013 CREATE TABLE t2(x,y);
1014 INSERT INTO t2 VALUES(99,88);
1015 DROP TABLE t1;
1016 }
1017 catchcmd test2.db {.dump}
1018} {0 {PRAGMA foreign_keys=OFF;
1019BEGIN TRANSACTION;
1020CREATE TABLE t2(x,y);
1021INSERT INTO t2 VALUES(99,88);
1022COMMIT;}}
1023do_test shell1-4.1.9 {
1024 db close
1025 forcedelete test2.db
1026 sqlite3 db test2.db
1027 db eval {
1028 CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT, b);
1029 INSERT INTO t1 VALUES(1,2),(20,21),(15,16);
1030 CREATE TABLE t2(x,y);
1031 INSERT INTO t2 VALUES(99,88);
1032 INSERT INTO sqlite_sequence VALUES('extra',999);
1033 DROP TABLE t1;
1034 }
1035 catchcmd test2.db {.dump}
1036} {0 {PRAGMA foreign_keys=OFF;
1037BEGIN TRANSACTION;
1038CREATE TABLE t2(x,y);
1039INSERT INTO t2 VALUES(99,88);
1040PRAGMA writable_schema=ON;
1041CREATE TABLE IF NOT EXISTS sqlite_sequence(name,seq);
1042DELETE FROM sqlite_sequence;
1043INSERT INTO sqlite_sequence VALUES('extra',999);
1044PRAGMA writable_schema=OFF;
1045COMMIT;}}
1046
1047# Test the output of ".mode insert"
1048#
1049do_test shell1-4.2.1 {
1050 catchcmd test.db ".mode insert t1\nselect * from t1;"
1051} {0 {INSERT INTO t1 VALUES(NULL);
1052INSERT INTO t1 VALUES('');
1053INSERT INTO t1 VALUES(1);
1054INSERT INTO t1 VALUES(2.25);
1055INSERT INTO t1 VALUES('hello');
1056INSERT INTO t1 VALUES(x'807f');}}
1057
1058# Test the output of ".mode insert" with headers
1059#
1060do_test shell1-4.2.2 {
1061 catchcmd test.db ".mode insert t1\n.headers on\nselect * from t1;"
1062} {0 {INSERT INTO t1(x) VALUES(NULL);
1063INSERT INTO t1(x) VALUES('');
1064INSERT INTO t1(x) VALUES(1);
1065INSERT INTO t1(x) VALUES(2.25);
1066INSERT INTO t1(x) VALUES('hello');
1067INSERT INTO t1(x) VALUES(x'807f');}}
1068
1069# Test the output of ".mode insert"
1070#
1071do_test shell1-4.2.3 {
1072 catchcmd test.db ".mode insert t3\nselect * from t3;"
1073} {0 {INSERT INTO t3 VALUES(1,NULL);
1074INSERT INTO t3 VALUES(2,'');
1075INSERT INTO t3 VALUES(3,1);
1076INSERT INTO t3 VALUES(4,2.25);
1077INSERT INTO t3 VALUES(5,'hello');
1078INSERT INTO t3 VALUES(6,x'807f');}}
1079
1080# Test the output of ".mode insert" with headers
1081#
1082do_test shell1-4.2.4 {
1083 catchcmd test.db ".mode insert t3\n.headers on\nselect * from t3;"
1084} {0 {INSERT INTO t3(x,y) VALUES(1,NULL);
1085INSERT INTO t3(x,y) VALUES(2,'');
1086INSERT INTO t3(x,y) VALUES(3,1);
1087INSERT INTO t3(x,y) VALUES(4,2.25);
1088INSERT INTO t3(x,y) VALUES(5,'hello');
1089INSERT INTO t3(x,y) VALUES(6,x'807f');}}
1090
1091# Test the output of ".mode tcl"
1092#
1093do_test shell1-4.3 {
1094 db close
1095 forcedelete test.db
1096 sqlite3 db test.db
1097 db eval {
1098 PRAGMA encoding=UTF8;
1099 CREATE TABLE t1(x);
1100 INSERT INTO t1 VALUES(null), (''), (1), (2.25), ('hello'), (x'807f');
1101 }
1102 catchcmd test.db ".mode tcl\nselect * from t1;"
1103} {0 {""
1104""
11051
11062.25
1107"hello"
1108"\200\177"}}
1109
1110# Test the output of ".mode tcl" with multiple columns
1111#
1112do_test shell1-4.4 {
1113 db eval {
1114 CREATE TABLE t2(x,y);
1115 INSERT INTO t2 VALUES(null, ''), (1, 2.25), ('hello', x'807f');
1116 }
1117 catchcmd test.db ".mode tcl\nselect * from t2;"
1118} {0 {"" ""
11191 2.25
1120"hello" "\200\177"}}
1121
1122# Test the output of ".mode tcl" with ".nullvalue"
1123#
1124do_test shell1-4.5 {
1125 catchcmd test.db ".mode tcl\n.nullvalue NULL\nselect * from t2;"
1126} {0 {NULL ""
11271 2.25
1128"hello" "\200\177"}}
1129
1130# Test the output of ".mode tcl" with Tcl reserved characters
1131#
1132do_test shell1-4.6 {
1133 db eval {
1134 CREATE TABLE tcl1(x);
1135 INSERT INTO tcl1 VALUES('"'), ('['), (']'), ('\{'), ('\}'), (';'), ('$');
1136 }
1137 foreach {x y} [catchcmd test.db ".mode tcl\nselect * from tcl1;"] break
1138 list $x $y [llength $y]
1139} {0 {"\""
1140"["
1141"]"
1142"\\{"
1143"\\}"
1144";"
1145"$"} 7}
1146
1147# Test the output of ".mode quote"
1148#
1149do_test shell1-4.7 {
1150 catchcmd test.db ".mode quote\nselect x'0123456789ABCDEF';"
1151} {0 x'0123456789abcdef'}
1152
1153# Test using arbitrary byte data with the shell via standard input/output.
1154#
1155if 0 { # Causes a valgrind error in TCL. Seems to be a TCL problem.
1156do_test shell1-5.0 {
1157 #
1158 # NOTE: Skip NUL byte because it appears to be incompatible with command
1159 # shell argument parsing.
1160 #
1161 for {set i 1} {$i < 256} {incr i} {
1162 #
1163 # NOTE: Due to how the Tcl [exec] command works (i.e. where it treats
1164 # command channels opened for it as textual ones), the carriage
1165 # return character (and on Windows, the end-of-file character)
1166 # cannot be used here.
1167 #
1168 if {$i==0x0D || ($tcl_platform(platform) eq "windows" && $i==0x1A)} {
1169 continue
1170 }
1171 # Tcl 8.7 maps 0x80 through 0x9f into valid UTF8. So skip those tests.
1172 if {$i>=0x80} {
1173 if {$i<=0x9F || $tcl_version>=9.0} continue
1174 if {$tcl_platform(platform) eq "windows"} continue
1175 }
1176 if {$i>=0xE0 && $tcl_platform(os) eq "OpenBSD"} continue
1177 if {$i>=0xE0 && $i<=0xEF && $tcl_platform(os) eq "Linux"} continue
1178 set hex [format %02X $i]
1179 set char [subst \\x$hex]; set oldChar $char
1180 set escapes [list]
1181 if {$tcl_platform(platform) eq "windows"} {
1182 #
1183 # NOTE: On Windows, we need to escape all the whitespace characters,
1184 # the alarm (\a) character, and those with special meaning to
1185 # the SQLite shell itself.
1186 #
1187 set escapes [list \
1188 \a \\a \b \\b \t \\t \n \\n \v \\v \f \\f \r \\r \
1189 " " "\" \"" \" \\\" \\ \\\\]
1190 } else {
1191 #
1192 # NOTE: On Unix, we need to escape most of the whitespace characters
1193 # and those with special meaning to the SQLite shell itself.
1194 # The alarm (\a), backspace (\b), and carriage-return (\r)
1195 # characters do not appear to require escaping on Unix. For
1196 # the alarm and backspace characters, this is probably due to
1197 # differences in the command shell. For the carriage-return,
1198 # it is probably due to differences in how Tcl handles command
1199 # channel end-of-line translations.
1200 #
1201 set escapes [list \
1202 \t \\t \n \\n \v \\v \f \\f \
1203 " " "\" \"" \" \\\" \\ \\\\]
1204 }
1205 set char [string map $escapes $char]
1206 set x [catchcmdex test.db ".print \"$char\"\n"]
1207 set code [lindex $x 0]
1208 set res [lindex $x 1]
1209 if {$code ne "0"} {
1210 error "failed with error: $res"
1211 }
1212 if {$res ne "$oldChar\n"} {
1213 if {[llength $res] > 0} {
1214 set got [format %02X [scan $res %c]]
1215 } else {
1216 set got <empty>
1217 }
1218 error "failed with byte $hex mismatch, got $got"
1219 }
1220 }
1221} {}
1222}
1223
1224# These test cases do not work on MinGW
1225if 0 {
1226
1227# The string used here is the word "test" in Chinese.
1228# In UTF-8, it is encoded as: \xE6\xB5\x8B\xE8\xAF\x95
1229set test \u6D4B\u8BD5
1230
1231do_test shell1-6.0 {
1232 set fileName $test; append fileName .db
1233 catch {forcedelete $fileName}
1234 set x [catchcmdex $fileName "CREATE TABLE t1(x);\n.schema\n"]
1235 set code [lindex $x 0]
1236 set res [string trim [lindex $x 1]]
1237 if {$code ne "0"} {
1238 error "failed with error: $res"
1239 }
1240 if {$res ne "CREATE TABLE t1(x);"} {
1241 error "failed with mismatch: $res"
1242 }
1243 if {![file exists $fileName]} {
1244 error "file \"$fileName\" (Unicode) does not exist"
1245 }
1246 forcedelete $fileName
1247} {}
1248
1249do_test shell1-6.1 {
1250 catch {forcedelete test3.db}
1251 set x [catchcmdex test3.db \
1252 "CREATE TABLE [encoding convertto utf-8 $test](x);\n.schema\n"]
1253 set code [lindex $x 0]
1254 set res [string trim [lindex $x 1]]
1255 if {$code ne "0"} {
1256 error "failed with error: $res"
1257 }
1258 if {$res ne "CREATE TABLE ${test}(x);"} {
1259 error "failed with mismatch: $res"
1260 }
1261 forcedelete test3.db
1262} {}
1263}
1264
1265db close
1266forcedelete test.db test.db-journal test.db-wal
1267sqlite3 db test.db
1268
1269# The shell tool ".schema" command uses virtual table "pragma_database_list"
1270#
1271ifcapable vtab {
1272
1273do_test shell1-7.1.1 {
1274 db eval {
1275 CREATE TABLE Z (x TEXT PRIMARY KEY);
1276 CREATE TABLE _ (x TEXT PRIMARY KEY);
1277 CREATE TABLE YY (x TEXT PRIMARY KEY);
1278 CREATE TABLE __ (x TEXT PRIMARY KEY);
1279 CREATE TABLE WWW (x TEXT PRIMARY KEY);
1280 CREATE TABLE ___ (x TEXT PRIMARY KEY);
1281 }
1282} {}
1283do_test shell1-7.1.2 {
1284 catchcmd "test.db" ".schema _"
1285} {0 {CREATE TABLE Z (x TEXT PRIMARY KEY);
1286CREATE TABLE _ (x TEXT PRIMARY KEY);}}
1287do_test shell1-7.1.3 {
1288 catchcmd "test.db" ".schema \"\\\\_\""
1289} {0 {CREATE TABLE _ (x TEXT PRIMARY KEY);}}
1290do_test shell1-7.1.4 {
1291 catchcmd "test.db" ".schema __"
1292} {0 {CREATE TABLE YY (x TEXT PRIMARY KEY);
1293CREATE TABLE __ (x TEXT PRIMARY KEY);}}
1294do_test shell1-7.1.5 {
1295 catchcmd "test.db" ".schema \"\\\\_\\\\_\""
1296} {0 {CREATE TABLE __ (x TEXT PRIMARY KEY);}}
1297do_test shell1-7.1.6 {
1298 catchcmd "test.db" ".schema ___"
1299} {0 {CREATE TABLE WWW (x TEXT PRIMARY KEY);
1300CREATE TABLE ___ (x TEXT PRIMARY KEY);}}
1301do_test shell1-7.1.7 {
1302 catchcmd "test.db" ".schema \"\\\\_\\\\_\\\\_\""
1303} {0 {CREATE TABLE ___ (x TEXT PRIMARY KEY);}}
1304
1305}
1306
1307# Test case for the ieee754 and decimal extensions in the shell.
1308# See the "floatingpoint.html" file in the documentation for more
1309# information.
1310#
1311do_test shell1-8.1 {
1312 catchcmd ":memory:" {.mode batch
1313 -- The pow2 table will hold all the necessary powers of two.
1314 CREATE TABLE pow2(x INTEGER PRIMARY KEY, v TEXT);
1315 WITH RECURSIVE c(x,v) AS (
1316 VALUES(0,'1')
1317 UNION ALL
1318 SELECT x+1, decimal_mul(v,'2') FROM c WHERE x+1<=971
1319 ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
1320 WITH RECURSIVE c(x,v) AS (
1321 VALUES(-1,'0.5')
1322 UNION ALL
1323 SELECT x-1, decimal_mul(v,'0.5') FROM c WHERE x-1>=-1075
1324 ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
1325
1326 -- This query finds the decimal representation of each value in the "c" table.
1327 WITH c(n) AS (VALUES(47.49))
1328 ----XXXXX----------- Replace with whatever you want
1329 SELECT decimal_mul(ieee754_mantissa(c.n),pow2.v)
1330 FROM pow2, c WHERE pow2.x=ieee754_exponent(c.n);
1331 }
1332} {0 47.49000000000000198951966012828052043914794921875}
1333do_test_with_ansi_output shell1-8.2 {
1334 catchcmd :memory: {
1335.mode box
1336SELECT ieee754(47.49) AS x;
1337 }
1338} {0 {╭───────────────────────────────╮
1339│ x │
1340╞═══════════════════════════════╡
1341│ ieee754(6683623321994527,-47) │
1342╰───────────────────────────────╯}}
1343do_test_with_ansi_output shell1-8.3 {
1344 catchcmd ":memory: --box" {
1345 select ieee754(6683623321994527,-47) as x;
1346 }
1347} {0 {╭───────╮
1348│ x │
1349╞═══════╡
1350│ 47.49 │
1351╰───────╯}}
1352do_test shell1-8.4 {
1353 catchcmd ":memory: --table" {SELECT ieee754_mantissa(47.49) AS M, ieee754_exponent(47.49) AS E;}
1354} {0 {+------------------+-----+
1355| M | E |
1356+------------------+-----+
1357| 6683623321994527 | -47 |
1358+------------------+-----+}}
1359do_test shell1-8.4b {
1360 catchcmd ":memory: --psql" \
1361 {SELECT ieee754_mantissa(47.49) AS M, ieee754_exponent(47.49) AS E;}
1362} {0 { M | E
1363------------------+-----
1364 6683623321994527 | -47
1365(1 row)}}
1366do_test_with_ansi_output shell1-8.5 {
1367 catchcmd ":memory: --box" {
1368create table t(a text, b int);
1369insert into t values ('too long for one line', 1), ('shorter', NULL);
1370.header on
1371.mode box --wordwrap off
1372.width 10 10
1373.nullvalue NADA
1374select * from t;}
1375} {0 {╭────────────┬────────────╮
1376│ a │ b │
1377╞════════════╪════════════╡
1378│ too long f │ 1 │
1379│ or one lin │ │
1380│ e │ │
1381├────────────┼────────────┤
1382│ shorter │ NADA │
1383╰────────────┴────────────╯}}
1384
1385#----------------------------------------------------------------------------
1386# Test cases shell1-9.*: Basic test that "dot" commands and SQL intermix ok.
1387#
1388do_test shell1-9.1 {
1389 catchcmd :memory: {
1390.mode csv --rowsep "\n"
1391/*
1392x */ select 1,2; --x
1393 -- .nada
1394;
1395.mode csv --rowsep "\n"
1396--x
1397select 2,1; select 3,4;
1398}
1399} {0 {1,2
14002,1
14013,4}}
1402
1403#----------------------------------------------------------------------------
1404# Test cases shell1-10.*: Test that certain static extensions are there.
1405#
1406do_test shell1-10.1 {
1407 catchcmd :memory: {
1408.mode list
1409.header off
1410select base64(base64(cast('digity-doo' as blob))),
1411 base85(base85(cast('digity-doo' as blob)));
1412}
1413} {0 digity-doo|digity-doo}
1414
1415#----------------------------------------------------------------------------
1416# Test cases shell1-11.*:
1417#
1418do_test shell1-11.1 {
1419 catchcmd :memory: {
1420.mode list
1421.header off
1422select base64(zeroblob(2000000000));
1423}
1424} {/1.*too big.*/}
1425do_test shell1-11.2 {
1426 catchcmd :memory: {
1427.mode list
1428.header off
1429select base85(zeroblob(2000000000));
1430}
1431} {/1.*too big.*/}
1432
1433#----------------------------------------------------------------------------
1434# As of 2025-11-17, the default mode is:
1435#
1436# qbox --screenwidth auto --linelimit 5 --charlimit 300 --textjsonb on
1437#
1438do_test shell1-12.1 {
1439 catchcmd :memory: {.mode tty -quote sql
1440.print
1441SELECT jsonb(1234) AS x;}
1442} {0 {
1443╭───────────────╮
1444│ x │
1445╞═══════════════╡
1446│ jsonb('1234') │
1447╰───────────────╯}}
1448do_test shell1-12.2 {
1449 catchcmd :memory: {.mode box --textjsonb on
1450.print
1451SELECT jsonb(1234) AS x;}
1452} {0 {
1453╭──────╮
1454│ x │
1455╞══════╡
1456│ 1234 │
1457╰──────╯}}
1458
1459#----------------------------------------------------------------------------
1460# https://sqlite.org/bugs/info/23d77b5d47908a41
1461# The -deserialize option should accept a zero-length database file.
1462#
1463do_test shell1-13.1 {
1464 forcedelete empty.db
1465 close [open empty.db w]
1466 catchcmd {-deserialize empty.db} {SELECT 'ok';}
1467} {0 ok}
1468forcedelete empty.db
1469
1470finish_test