Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/test/shell1.test # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers 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 # 25 set testdir [file dirname $argv0] 26 source $testdir/tester.tcl 27 set CLI [test_cli_invocation] 28 db close 29 forcedelete test.db test.db-journal test.db-wal 30 sqlite3 db test.db 31 32 #---------------------------------------------------------------------------- 33 # Test cases shell1-1.*: Basic command line option handling. 34 # 35 36 # invalid option 37 do_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} 43 do_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 50 do_test shell1-1.1.2 { 51 catchcmd "test.db \"select+3\" \"select+4\"" "" 52 } {0 {3 53 4}} 54 # error on extra options 55 do_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 60 do_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 70 forcedelete FOO 71 set out [open FOO w] 72 puts $out "" 73 close $out 74 do_test shell1-1.3.1 { 75 catchcmd "-init FOO test.db" "" 76 } {0 {}} 77 do_test shell1-1.3.2 { 78 catchcmd "-init FOO test.db .quit BAD" "" 79 } {0 {}} 80 do_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 85 do_test shell1-1.4.1 { 86 catchcmd "-echo test.db" "" 87 } {0 {}} 88 89 # -[no]header turn headers on or off 90 do_test shell1-1.5.1 { 91 catchcmd "-header test.db" "" 92 } {0 {}} 93 do_test shell1-1.5.2 { 94 catchcmd "-noheader test.db" "" 95 } {0 {}} 96 97 # -bail stop after hitting an error 98 do_test shell1-1.6.1 { 99 catchcmd "-bail test.db" "" 100 } {0 {}} 101 102 # -interactive force interactive I/O 103 do_test shell1-1.7.1 { 104 set res [catchcmd "-interactive test.db" ".quit"] 105 set rc [lindex $res 0] 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 112 do_test shell1-1.8.1 { 113 catchcmd "-batch test.db" "" 114 } {0 {}} 115 116 # -column set output mode to 'column' 117 do_test shell1-1.9.1 { 118 catchcmd "-column test.db" "" 119 } {0 {}} 120 121 # -csv set output mode to 'csv' 122 do_test shell1-1.10.1 { 123 catchcmd "-csv test.db" "" 124 } {0 {}} 125 do_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 129 4,5,6}} 130 do_test shell1-1.10.3 { 131 catchcmd "-header -csv test.db" ".mode --rowsep \"\\012\"\nSELECT * FROM t1" 132 } {0 {a,b,c 133 1,2,3 134 4,5,6}} 135 do_test shell1-1.10.4 { 136 catchcmd "-csv -header test.db" ".mode --rowsep \"\\012\"\nSELECT * FROM t1" 137 } {0 {a,b,c 138 1,2,3 139 4,5,6}} 140 141 # -html set output mode to HTML 142 do_test shell1-1.11.1 { 143 catchcmd "-html test.db" "" 144 } {0 {}} 145 146 # -line set output mode to 'line' 147 do_test shell1-1.12.1 { 148 catchcmd "-line test.db" "" 149 } {0 {}} 150 151 # -list set output mode to 'list' 152 do_test shell1-1.13.1 { 153 catchcmd "-list test.db" "" 154 } {0 {}} 155 156 # -separator 'x' set output field separator (|) 157 do_test shell1-1.14.1 { 158 catchcmd "-separator 'x' test.db" "" 159 } {0 {}} 160 do_test shell1-1.14.2 { 161 catchcmd "-separator x test.db" "" 162 } {0 {}} 163 do_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 171 do_test shell1-1.14b.1 { 172 catchcmd "-stats test.db" "" 173 } {0 {}} 174 175 # -nullvalue 'text' set text string for NULL values 176 do_test shell1-1.15.1 { 177 catchcmd "-nullvalue 'x' test.db" "" 178 } {0 {}} 179 do_test shell1-1.15.2 { 180 catchcmd "-nullvalue x test.db" "" 181 } {0 {}} 182 do_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 190 do_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 195 forcedelete ./--db 196 do_test shell1-1.17.1 { 197 catchcmd {-- --db "CREATE TABLE T(c1);"} 198 } {0 {}} 199 do_test shell1-1.17.2 { 200 catchcmd {-- --db "SELECT name from sqlite_schema;"} 201 } {0 T} 202 forcedelete ./--db 203 204 #---------------------------------------------------------------------------- 205 # Test cases shell1-2.*: Basic "dot" command token parsing. 206 # 207 208 # check first token handling 209 do_test shell1-2.1.1 { 210 catchcmd "test.db" ".foo" 211 } {1 {Error: unknown command or invalid arguments: "foo". Enter ".help" for help}} 212 do_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}} 215 do_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 220 do_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}} 223 do_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}} 226 do_test shell1-2.2.3 { 227 catchcmd "test.db" ".explain \"OFF" 228 } {0 {}} 229 do_test shell1-2.2.4 { 230 catchcmd "test.db" ".explain \'OFF" 231 } {0 {}} 232 do_test shell1-2.2.5 { 233 catchcmd "test.db" ".mode \"insert FOO" 234 } {1 {line 1: .mode "insert FOO 235 line 1: ^--- unknown mode 236 line 1: Use ".help .mode" for more info}} 237 do_test shell1-2.2.6 { 238 catchcmd "test.db" ".mode \'insert FOO" 239 } {1 {line 1: .mode 'insert FOO 240 line 1: ^--- unknown mode 241 line 1: Use ".help .mode" for more info}} 242 243 # check multiple tokens, and quoted tokens 244 do_test shell1-2.3.1 { 245 catchcmd "test.db" ".explain 1" 246 } {0 {}} 247 do_test shell1-2.3.2 { 248 catchcmd "test.db" ".explain on" 249 } {0 {}} 250 do_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".}} 253 do_test shell1-2.3.4 { 254 catchcmd "test.db" ".explain \"OFF\"" 255 } {0 {}} 256 do_test shell1-2.3.5 { 257 catchcmd "test.db" ".\'explain\' \'OFF\'" 258 } {0 {}} 259 do_test shell1-2.3.6 { 260 catchcmd "test.db" ".explain \'OFF\'" 261 } {0 {}} 262 do_test shell1-2.3.7 { 263 catchcmd "test.db" ".\'explain\' \'OFF\'" 264 } {0 {}} 265 266 # check quoted args are unquoted 267 do_test shell1-2.4.1 { 268 catchcmd "test.db" ".mode FOO" 269 } {1 {line 1: .mode FOO 270 line 1: ^--- unknown mode 271 line 1: Use ".help .mode" for more info}} 272 do_test shell1-2.4.2 { 273 catchcmd "test.db" ".mode csv" 274 } {0 {}} 275 do_test shell1-2.4.2 { 276 catchcmd "test.db" ".mode \"csv\"" 277 } {0 {}} 278 279 # check that certain quoted arg escapes work 280 do_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 290 do_test shell1-3.1.1 { 291 catchcmd "test.db" ".backup" 292 } {1 {missing FILENAME argument on .backup}} 293 forcedelete FOO 294 do_test shell1-3.1.2 { 295 catchcmd "test.db" ".backup FOO" 296 } {0 {}} 297 do_test shell1-3.1.3 { 298 catchcmd "test.db" ".backup FOO BAR" 299 } {1 {Error: unknown database FOO}} 300 do_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 306 do_test shell1-3.2.1 { 307 catchcmd "test.db" ".bail" 308 } {1 {Usage: .bail on|off}} 309 do_test shell1-3.2.2 { 310 catchcmd "test.db" ".bail ON" 311 } {0 {}} 312 do_test shell1-3.2.3 { 313 catchcmd "test.db" ".bail OFF" 314 } {0 {}} 315 do_test shell1-3.2.4 { 316 # too many arguments 317 catchcmd "test.db" ".bail OFF BAD" 318 } {1 {Usage: .bail on|off}} 319 320 ifcapable vtab { 321 # .databases List names and files of attached databases 322 do_test shell1-3.3.1 { 323 catchcmd "-csv test.db" ".databases" 324 } "/0.+main.+[string map {/ ".{1,2}"} [string range [get_pwd] 0 10]].*/" 325 do_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. 334 do_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} 339 do_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 351 do_test shell1-3.5.1 { 352 catchcmd "test.db" ".echo" 353 } {1 {Usage: .echo on|off}} 354 do_test shell1-3.5.2 { 355 catchcmd "test.db" ".echo ON" 356 } {0 {}} 357 do_test shell1-3.5.3 { 358 catchcmd "test.db" ".echo OFF" 359 } {0 {}} 360 do_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 366 do_test shell1-3.6.1 { 367 catchcmd "test.db" ".exit" 368 } {0 {}} 369 do_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. 374 do_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 {}} 378 do_test shell1-3.7.2 { 379 catchcmd "test.db" ".explain ON" 380 } {0 {}} 381 do_test shell1-3.7.3 { 382 catchcmd "test.db" ".explain OFF" 383 } {0 {}} 384 do_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 390 do_test shell1-3.9.1 { 391 catchcmd "test.db" ".header" 392 } {1 {Usage: .headers off|on|always}} 393 do_test shell1-3.9.2 { 394 catchcmd "test.db" ".header ON" 395 } {0 {}} 396 do_test shell1-3.9.3 { 397 catchcmd "test.db" ".header OFF" 398 } {0 {}} 399 do_test shell1-3.9.4 { 400 # too many arguments 401 catchcmd "test.db" ".header OFF BAD" 402 } {1 {Usage: .headers off|on|always}} 403 404 do_test shell1-3.9.5 { 405 catchcmd "test.db" ".headers" 406 } {1 {Usage: .headers off|on|always}} 407 do_test shell1-3.9.6 { 408 catchcmd "test.db" ".headers ON" 409 } {0 {}} 410 do_test shell1-3.9.7 { 411 catchcmd "test.db" ".headers OFF" 412 } {0 {}} 413 do_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 419 do_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} 426 do_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 436 do_test shell1-3.11.1 { 437 catchcmd "test.db" ".import" 438 } {/1 .line 1: Missing FILE argument.*/} 439 do_test shell1-3.11.2 { 440 catchcmd "test.db" ".import FOO" 441 } {/1 .line 1: Missing TABLE argument.*/} 442 do_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 446 line 1: ^--- unknown argument}} 447 do_test shell1-3.11.4 { 448 catchcmd "test.db" ".import < 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 479 do_test shell1-3.13.1 { 480 catchcmd "test.db" ".mode batch\n.mode" 481 } {0 {.mode list}} 482 do_test shell1-3.13.2 { 483 catchcmd "test.db" ".mode FOO" 484 } {1 {line 1: .mode FOO 485 line 1: ^--- unknown mode 486 line 1: Use ".help .mode" for more info}} 487 do_test shell1-3.13.3 { 488 catchcmd "test.db" ".mode csv" 489 } {0 {}} 490 do_test shell1-3.13.4 { 491 catchcmd "test.db" ".mode column" 492 } {0 {}} 493 do_test shell1-3.13.5 { 494 catchcmd "test.db" ".mode html" 495 } {0 {}} 496 do_test shell1-3.13.6 { 497 catchcmd "test.db" ".mode insert" 498 } {0 {}} 499 do_test shell1-3.13.7 { 500 catchcmd "test.db" ".mode line" 501 } {0 {}} 502 do_test shell1-3.13.8 { 503 catchcmd "test.db" ".mode list" 504 } {0 {}} 505 do_test shell1-3.13.9 { 506 catchcmd "test.db" ".mode tabs" 507 } {0 {}} 508 do_test shell1-3.13.10 { 509 catchcmd "test.db" ".mode tcl" 510 } {0 {}} 511 do_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 517 do_test shell1-3.14.1 { 518 catchcmd "test.db" ".nullvalue" 519 } {1 {Usage: .nullvalue STRING}} 520 do_test shell1-3.14.2 { 521 catchcmd "test.db" ".nullvalue FOO" 522 } {0 {}} 523 do_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 529 do_test shell1-3.15.1 { 530 catchcmd "test.db" ".output 531 .print x" 532 } {0 x} 533 do_test shell1-3.15.2 { 534 catchcmd "test.db" ".mode batch\n.output FOO 535 .print x 536 .output 537 SELECT readfile('FOO');" 538 } {0 {x 539 }} 540 do_test shell1-3.15.3 { 541 # too many arguments 542 catchcmd "test.db" ".output FOO BAD" 543 } {1 {line 1: .output FOO BAD 544 line 1: ^--- surplus argument}} 545 546 # .output stdout Send output to the screen 547 do_test shell1-3.16.1 { 548 catchcmd "test.db" ".output stdout" 549 } {0 {}} 550 do_test shell1-3.16.2 { 551 # too many arguments 552 catchcmd "test.db" ".output stdout BAD" 553 } {1 {line 1: .output stdout BAD 554 line 1: ^--- surplus argument}} 555 556 # .prompt MAIN CONTINUE Replace the standard prompts 557 do_test shell1-3.17.1 { 558 catchcmd "test.db" ".prompt" 559 } {0 {}} 560 do_test shell1-3.17.2 { 561 catchcmd "test.db" ".prompt FOO" 562 } {0 {}} 563 do_test shell1-3.17.3 { 564 catchcmd "test.db" ".prompt FOO BAR" 565 } {0 {}} 566 do_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 570 line 1: ^--- extra argument}} 571 572 # .quit Exit this program 573 do_test shell1-3.18.1 { 574 catchcmd "test.db" ".quit" 575 } {0 {}} 576 do_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 582 do_test shell1-3.19.1 { 583 catchcmd "test.db" ".read" 584 } {1 {Usage: .read FILE}} 585 do_test shell1-3.19.2 { 586 forcedelete FOO 587 catchcmd "test.db" ".read FOO" 588 } {1 {Error: cannot open "FOO"}} 589 do_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 595 do_test shell1-3.20.1 { 596 catchcmd "test.db" ".restore" 597 } {1 {Usage: .restore ?DB? FILE}} 598 do_test shell1-3.20.2 { 599 catchcmd "test.db" ".restore FOO" 600 } {0 {}} 601 do_test shell1-3.20.3 { 602 catchcmd "test.db" ".restore FOO BAR" 603 } {1 {Error: unknown database FOO}} 604 do_test shell1-3.20.4 { 605 # too many arguments 606 catchcmd "test.db" ".restore FOO BAR BAD" 607 } {1 {Usage: .restore ?DB? FILE}} 608 609 ifcapable vtab { 610 # .schema ?TABLE? Show the CREATE statements 611 # If TABLE specified, only show tables matching 612 # LIKE pattern TABLE. 613 do_test shell1-3.21.1 { 614 catchcmd "test.db" ".schema" 615 } {0 {}} 616 do_test shell1-3.21.2 { 617 catchcmd "test.db" ".schema FOO" 618 } {0 {}} 619 do_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 624 do_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); 632 CREATE VIEW v2 AS SELECT x+1 AS y FROM t1; 633 /* v2(y) */ 634 CREATE 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 } 639 do_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;} 648 do_test shell1-3.21.6 { 649 exec {*}$CLI -noinit test.db \ 650 {DROP TABLE t2;} \ 651 {.schema -indent t2} 652 } {} 653 do_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); 660 CREATE VIEW v7("a*/;SELECT 12345;/*") AS SELECT 234; 661 /* v7("a**;SELECT 12345;/*") */}} 662 forcedelete test7.db 663 664 # .separator STRING Change column separator used by output and .import 665 do_test shell1-3.22.1 { 666 catchcmd "test.db" ".separator" 667 } {1 {Usage: .separator COL ?ROW?}} 668 do_test shell1-3.22.2 { 669 catchcmd "test.db" ".separator FOO" 670 } {0 {}} 671 do_test shell1-3.22.3 { 672 catchcmd "test.db" ".separator ABC XYZ" 673 } {0 {}} 674 do_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 680 do_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} 693 do_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}} 702 do_test shell1-3.23b.2 { 703 catchcmd "test.db" ".stats ON" 704 } {0 {}} 705 do_test shell1-3.23b.3 { 706 catchcmd "test.db" ".stats OFF" 707 } {0 {}} 708 do_test shell1-3.23b.4 { 709 # too many arguments 710 catchcmd "test.db" ".stats OFF BAD" 711 } {1 {line 1: .stats OFF BAD 712 line 1: ^--- unknown argument}} 713 714 # Ticket `7be932dfa60a8a6b3b26bcf7623ec46e0a403ddb` 2018-06-07 715 # Adverse interaction between .stats and .eqp 716 # 717 do_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. 730 do_test shell1-3.24.1 { 731 catchcmd "test.db" ".tables" 732 } {0 {}} 733 do_test shell1-3.24.2 { 734 catchcmd "test.db" ".tables FOO" 735 } {0 {}} 736 do_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 742 do_test shell1-3.25.1 { 743 catchcmd "test.db" ".timeout" 744 } {0 {}} 745 do_test shell1-3.25.2 { 746 catchcmd "test.db" ".timeout zzz" 747 # this should be treated the same as a '0' timeout 748 } {0 {}} 749 do_test shell1-3.25.3 { 750 catchcmd "test.db" ".timeout 1" 751 } {0 {}} 752 do_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 758 do_test shell1-3.26.1 { 759 catchcmd "test.db" ".width" 760 } {0 {}} 761 do_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 {}} 765 do_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 {}} 769 do_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 {}} 773 do_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}} 777 do_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 784 do_test shell1-3.27.1 { 785 catchcmd "test.db" ".timer" 786 } {1 {Usage: .timer on|off|once}} 787 do_test shell1-3.27.2 { 788 catchcmd "test.db" ".timer ON" 789 } {0 {}} 790 do_test shell1-3.27.3 { 791 catchcmd "test.db" ".timer OFF" 792 } {0 {}} 793 do_test shell1-3.27.4 { 794 # too many arguments 795 catchcmd "test.db" ".timer OFF BAD" 796 } {1 {Usage: .timer on|off|once}} 797 798 do_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 803 do_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 808 do_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}} 811 do_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 # 818 do_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; 832 BEGIN TRANSACTION; 833 CREATE TABLE t1(x); 834 INSERT INTO t1 VALUES(NULL); 835 INSERT INTO t1 VALUES(''); 836 INSERT INTO t1 VALUES(1); 837 INSERT INTO t1 VALUES(2.25); 838 INSERT INTO t1 VALUES('hello'); 839 INSERT INTO t1 VALUES(x'807f'); 840 CREATE TABLE t3(x,y); 841 INSERT INTO t3 VALUES(1,NULL); 842 INSERT INTO t3 VALUES(2,''); 843 INSERT INTO t3 VALUES(3,1); 844 INSERT INTO t3 VALUES(4,2.25); 845 INSERT INTO t3 VALUES(5,'hello'); 846 INSERT INTO t3 VALUES(6,x'807f'); 847 COMMIT;}} 848 849 850 ifcapable vtab { 851 852 # The --preserve-rowids option to .dump 853 # 854 do_test shell1-4.1.1 { 855 catchcmd test.db {.dump --preserve-rowids} 856 } {0 {PRAGMA foreign_keys=OFF; 857 BEGIN TRANSACTION; 858 CREATE TABLE t1(x); 859 INSERT INTO t1(rowid,x) VALUES(1,NULL); 860 INSERT INTO t1(rowid,x) VALUES(2,''); 861 INSERT INTO t1(rowid,x) VALUES(3,1); 862 INSERT INTO t1(rowid,x) VALUES(4,2.25); 863 INSERT INTO t1(rowid,x) VALUES(5,'hello'); 864 INSERT INTO t1(rowid,x) VALUES(6,x'807f'); 865 CREATE TABLE t3(x,y); 866 INSERT INTO t3(rowid,x,y) VALUES(1,1,NULL); 867 INSERT INTO t3(rowid,x,y) VALUES(2,2,''); 868 INSERT INTO t3(rowid,x,y) VALUES(3,3,1); 869 INSERT INTO t3(rowid,x,y) VALUES(4,4,2.25); 870 INSERT INTO t3(rowid,x,y) VALUES(5,5,'hello'); 871 INSERT INTO t3(rowid,x,y) VALUES(6,6,x'807f'); 872 COMMIT;}} 873 874 # If the table contains an INTEGER PRIMARY KEY, do not record a separate 875 # rowid column in the output. 876 # 877 do_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; 888 BEGIN TRANSACTION; 889 CREATE TABLE t1(x INTEGER PRIMARY KEY, y); 890 INSERT INTO t1(x,y) VALUES(1,NULL); 891 INSERT INTO t1(x,y) VALUES(2,''); 892 INSERT INTO t1(x,y) VALUES(3,1); 893 INSERT INTO t1(x,y) VALUES(4,2.25); 894 INSERT INTO t1(x,y) VALUES(5,'hello'); 895 INSERT INTO t1(x,y) VALUES(6,x'807f'); 896 COMMIT;}} 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 # 901 do_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; 912 BEGIN TRANSACTION; 913 CREATE TABLE [table](x INTEGER PRIMARY KEY DESC, y); 914 INSERT INTO "table"(rowid,x,y) VALUES(1,1,NULL); 915 INSERT INTO "table"(rowid,x,y) VALUES(2,12,''); 916 INSERT INTO "table"(rowid,x,y) VALUES(3,23,1); 917 INSERT INTO "table"(rowid,x,y) VALUES(4,34,2.25); 918 INSERT INTO "table"(rowid,x,y) VALUES(5,45,'hello'); 919 INSERT INTO "table"(rowid,x,y) VALUES(6,56,x'807f'); 920 COMMIT;}} 921 922 # Do not record rowids for a WITHOUT ROWID table. Also check correct quoting 923 # of table names that contain odd characters. 924 # 925 do_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; 936 BEGIN TRANSACTION; 937 CREATE TABLE [ta<>ble](x INTEGER PRIMARY KEY, y) WITHOUT ROWID; 938 INSERT INTO "ta<>ble"(x,y) VALUES(1,NULL); 939 INSERT INTO "ta<>ble"(x,y) VALUES(12,''); 940 INSERT INTO "ta<>ble"(x,y) VALUES(23,1); 941 INSERT INTO "ta<>ble"(x,y) VALUES(34,2.25); 942 INSERT INTO "ta<>ble"(x,y) VALUES(45,'hello'); 943 INSERT INTO "ta<>ble"(x,y) VALUES(56,x'807f'); 944 COMMIT;}} 945 946 # Do not record rowids if the rowid is inaccessible 947 # 948 do_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; 958 BEGIN TRANSACTION; 959 CREATE TABLE t1(_ROWID_,rowid,oid); 960 INSERT INTO t1(_ROWID_,rowid,oid) VALUES(1,NULL,'alpha'); 961 INSERT INTO t1(_ROWID_,rowid,oid) VALUES(12,'',99); 962 INSERT INTO t1(_ROWID_,rowid,oid) VALUES(23,1,x'b0b1b2'); 963 COMMIT;}} 964 965 } else { 966 967 do_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 # 985 do_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; 995 BEGIN TRANSACTION; 996 CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT, b); 997 INSERT INTO t1 VALUES(1,2); 998 INSERT INTO t1 VALUES(15,16); 999 INSERT INTO t1 VALUES(20,21); 1000 PRAGMA writable_schema=ON; 1001 CREATE TABLE IF NOT EXISTS sqlite_sequence(name,seq); 1002 DELETE FROM sqlite_sequence; 1003 INSERT INTO sqlite_sequence VALUES('t1',20); 1004 PRAGMA writable_schema=OFF; 1005 COMMIT;}} 1006 do_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; 1019 BEGIN TRANSACTION; 1020 CREATE TABLE t2(x,y); 1021 INSERT INTO t2 VALUES(99,88); 1022 COMMIT;}} 1023 do_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; 1037 BEGIN TRANSACTION; 1038 CREATE TABLE t2(x,y); 1039 INSERT INTO t2 VALUES(99,88); 1040 PRAGMA writable_schema=ON; 1041 CREATE TABLE IF NOT EXISTS sqlite_sequence(name,seq); 1042 DELETE FROM sqlite_sequence; 1043 INSERT INTO sqlite_sequence VALUES('extra',999); 1044 PRAGMA writable_schema=OFF; 1045 COMMIT;}} 1046 1047 # Test the output of ".mode insert" 1048 # 1049 do_test shell1-4.2.1 { 1050 catchcmd test.db ".mode insert t1\nselect * from t1;" 1051 } {0 {INSERT INTO t1 VALUES(NULL); 1052 INSERT INTO t1 VALUES(''); 1053 INSERT INTO t1 VALUES(1); 1054 INSERT INTO t1 VALUES(2.25); 1055 INSERT INTO t1 VALUES('hello'); 1056 INSERT INTO t1 VALUES(x'807f');}} 1057 1058 # Test the output of ".mode insert" with headers 1059 # 1060 do_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); 1063 INSERT INTO t1(x) VALUES(''); 1064 INSERT INTO t1(x) VALUES(1); 1065 INSERT INTO t1(x) VALUES(2.25); 1066 INSERT INTO t1(x) VALUES('hello'); 1067 INSERT INTO t1(x) VALUES(x'807f');}} 1068 1069 # Test the output of ".mode insert" 1070 # 1071 do_test shell1-4.2.3 { 1072 catchcmd test.db ".mode insert t3\nselect * from t3;" 1073 } {0 {INSERT INTO t3 VALUES(1,NULL); 1074 INSERT INTO t3 VALUES(2,''); 1075 INSERT INTO t3 VALUES(3,1); 1076 INSERT INTO t3 VALUES(4,2.25); 1077 INSERT INTO t3 VALUES(5,'hello'); 1078 INSERT INTO t3 VALUES(6,x'807f');}} 1079 1080 # Test the output of ".mode insert" with headers 1081 # 1082 do_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); 1085 INSERT INTO t3(x,y) VALUES(2,''); 1086 INSERT INTO t3(x,y) VALUES(3,1); 1087 INSERT INTO t3(x,y) VALUES(4,2.25); 1088 INSERT INTO t3(x,y) VALUES(5,'hello'); 1089 INSERT INTO t3(x,y) VALUES(6,x'807f');}} 1090 1091 # Test the output of ".mode tcl" 1092 # 1093 do_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 "" 1105 1 1106 2.25 1107 "hello" 1108 "\200\177"}} 1109 1110 # Test the output of ".mode tcl" with multiple columns 1111 # 1112 do_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 {"" "" 1119 1 2.25 1120 "hello" "\200\177"}} 1121 1122 # Test the output of ".mode tcl" with ".nullvalue" 1123 # 1124 do_test shell1-4.5 { 1125 catchcmd test.db ".mode tcl\n.nullvalue NULL\nselect * from t2;" 1126 } {0 {NULL "" 1127 1 2.25 1128 "hello" "\200\177"}} 1129 1130 # Test the output of ".mode tcl" with Tcl reserved characters 1131 # 1132 do_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 # 1149 do_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 # 1155 if 0 { # Causes a valgrind error in TCL. Seems to be a TCL problem. 1156 do_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 1217 } 1218 error "failed with byte $hex mismatch, got $got" 1219 } 1220 } 1221 } {} 1222 } 1223 1224 # These test cases do not work on MinGW 1225 if 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 1229 set test \u6D4B\u8BD5 1230 1231 do_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 1249 do_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 1265 db close 1266 forcedelete test.db test.db-journal test.db-wal 1267 sqlite3 db test.db 1268 1269 # The shell tool ".schema" command uses virtual table "pragma_database_list" 1270 # 1271 ifcapable vtab { 1272 1273 do_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 } {} 1283 do_test shell1-7.1.2 { 1284 catchcmd "test.db" ".schema _" 1285 } {0 {CREATE TABLE Z (x TEXT PRIMARY KEY); 1286 CREATE TABLE _ (x TEXT PRIMARY KEY);}} 1287 do_test shell1-7.1.3 { 1288 catchcmd "test.db" ".schema \"\\\\_\"" 1289 } {0 {CREATE TABLE _ (x TEXT PRIMARY KEY);}} 1290 do_test shell1-7.1.4 { 1291 catchcmd "test.db" ".schema __" 1292 } {0 {CREATE TABLE YY (x TEXT PRIMARY KEY); 1293 CREATE TABLE __ (x TEXT PRIMARY KEY);}} 1294 do_test shell1-7.1.5 { 1295 catchcmd "test.db" ".schema \"\\\\_\\\\_\"" 1296 } {0 {CREATE TABLE __ (x TEXT PRIMARY KEY);}} 1297 do_test shell1-7.1.6 { 1298 catchcmd "test.db" ".schema ___" 1299 } {0 {CREATE TABLE WWW (x TEXT PRIMARY KEY); 1300 CREATE TABLE ___ (x TEXT PRIMARY KEY);}} 1301 do_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 # 1311 do_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`} 1333 do_test_with_ansi_output shell1-8.2 { 1334 catchcmd :memory: { 1335 .mode box 1336 SELECT ieee754(47.49) AS x; 1337 } 1338 } {0 {╭───────────────────────────────╮ 1339 │ x │ 1340 ╞═══════════════════════════════╡ 1341 │ ieee754(`6683623321994527`,-47) │ 1342 ╰───────────────────────────────╯}} 1343 do_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 ╰───────╯}} 1352 do_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 +------------------+-----+}} 1359 do_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)}} 1366 do_test_with_ansi_output shell1-8.5 { 1367 catchcmd ":memory: --box" { 1368 create table t(a text, b int); 1369 insert 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 1374 select * 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 # 1388 do_test shell1-9.1 { 1389 catchcmd :memory: { 1390 .mode csv --rowsep "\n" 1391 /* 1392 x */ select 1,2; --x 1393 -- .nada 1394 ; 1395 .mode csv --rowsep "\n" 1396 --x 1397 select 2,1; select 3,4; 1398 } 1399 } {0 {1,2 1400 2,1 1401 3,4}} 1402 1403 #---------------------------------------------------------------------------- 1404 # Test cases shell1-10.*: Test that certain static extensions are there. 1405 # 1406 do_test shell1-10.1 { 1407 catchcmd :memory: { 1408 .mode list 1409 .header off 1410 select 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 # 1418 do_test shell1-11.1 { 1419 catchcmd :memory: { 1420 .mode list 1421 .header off 1422 select base64(zeroblob(`2000000000`)); 1423 } 1424 } {/1.*too big.*/} 1425 do_test shell1-11.2 { 1426 catchcmd :memory: { 1427 .mode list 1428 .header off 1429 select 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 # 1438 do_test shell1-12.1 { 1439 catchcmd :memory: {.mode tty -quote sql 1440 .print 1441 SELECT jsonb(1234) AS x;} 1442 } {0 { 1443 ╭───────────────╮ 1444 │ x │ 1445 ╞═══════════════╡ 1446 │ jsonb('1234') │ 1447 ╰───────────────╯}} 1448 do_test shell1-12.2 { 1449 catchcmd :memory: {.mode box --textjsonb on 1450 .print 1451 SELECT 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 # 1463 do_test shell1-13.1 { 1464 forcedelete empty.db 1465 close [open empty.db w] 1466 catchcmd {-deserialize empty.db} {SELECT 'ok';} 1467 } {0 ok} 1468 forcedelete empty.db 1469 1470 finish_test