shell1.test 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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. #
  15. # Test plan:
  16. #
  17. # shell1-1.*: Basic command line option handling.
  18. # shell1-2.*: Basic "dot" command token parsing.
  19. # shell1-3.*: Basic test that "dot" command can be called.
  20. #
  21. set testdir [file dirname $argv0]
  22. source $testdir/tester.tcl
  23. if {$tcl_platform(platform)=="windows"} {
  24. set CLI "sqlite3.exe"
  25. } else {
  26. set CLI "./sqlite3"
  27. }
  28. if {![file executable $CLI]} {
  29. finish_test
  30. return
  31. }
  32. db close
  33. forcedelete test.db test.db-journal test.db-wal
  34. sqlite3 db test.db
  35. #----------------------------------------------------------------------------
  36. # Test cases shell1-1.*: Basic command line option handling.
  37. #
  38. # invalid option
  39. do_test shell1-1.1.1 {
  40. set res [catchcmd "-bad test.db" ""]
  41. set rc [lindex $res 0]
  42. list $rc \
  43. [regexp {Error: unknown option: -bad} $res]
  44. } {1 1}
  45. # error on extra options
  46. do_test shell1-1.1.2 {
  47. set res [catchcmd "-bad test.db \"select 3\" \"select 4\"" ""]
  48. set rc [lindex $res 0]
  49. list $rc \
  50. [regexp {Error: too many options: "select 4"} $res]
  51. } {1 1}
  52. # error on extra options
  53. do_test shell1-1.1.3 {
  54. set res [catchcmd "-bad FOO test.db BAD" ".quit"]
  55. set rc [lindex $res 0]
  56. list $rc \
  57. [regexp {Error: too many options: "BAD"} $res]
  58. } {1 1}
  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. # -init filename read/process named file
  69. do_test shell1-1.3.1 {
  70. catchcmd "-init FOO test.db" ""
  71. } {0 {}}
  72. do_test shell1-1.3.2 {
  73. set res [catchcmd "-init FOO test.db .quit BAD" ""]
  74. set rc [lindex $res 0]
  75. list $rc \
  76. [regexp {Error: too many options: "BAD"} $res]
  77. } {1 1}
  78. # -echo print commands before execution
  79. do_test shell1-1.4.1 {
  80. catchcmd "-echo test.db" ""
  81. } {0 {}}
  82. # -[no]header turn headers on or off
  83. do_test shell1-1.5.1 {
  84. catchcmd "-header test.db" ""
  85. } {0 {}}
  86. do_test shell1-1.5.2 {
  87. catchcmd "-noheader test.db" ""
  88. } {0 {}}
  89. # -bail stop after hitting an error
  90. do_test shell1-1.6.1 {
  91. catchcmd "-bail test.db" ""
  92. } {0 {}}
  93. # -interactive force interactive I/O
  94. do_test shell1-1.7.1 {
  95. set res [catchcmd "-interactive test.db" ".quit"]
  96. set rc [lindex $res 0]
  97. list $rc \
  98. [regexp {SQLite version} $res] \
  99. [regexp {Enter SQL statements} $res]
  100. } {0 1 1}
  101. # -batch force batch I/O
  102. do_test shell1-1.8.1 {
  103. catchcmd "-batch test.db" ""
  104. } {0 {}}
  105. # -column set output mode to 'column'
  106. do_test shell1-1.9.1 {
  107. catchcmd "-column test.db" ""
  108. } {0 {}}
  109. # -csv set output mode to 'csv'
  110. do_test shell1-1.10.1 {
  111. catchcmd "-csv test.db" ""
  112. } {0 {}}
  113. # -html set output mode to HTML
  114. do_test shell1-1.11.1 {
  115. catchcmd "-html test.db" ""
  116. } {0 {}}
  117. # -line set output mode to 'line'
  118. do_test shell1-1.12.1 {
  119. catchcmd "-line test.db" ""
  120. } {0 {}}
  121. # -list set output mode to 'list'
  122. do_test shell1-1.13.1 {
  123. catchcmd "-list test.db" ""
  124. } {0 {}}
  125. # -separator 'x' set output field separator (|)
  126. do_test shell1-1.14.1 {
  127. catchcmd "-separator 'x' test.db" ""
  128. } {0 {}}
  129. do_test shell1-1.14.2 {
  130. catchcmd "-separator x test.db" ""
  131. } {0 {}}
  132. do_test shell1-1.14.3 {
  133. set res [catchcmd "-separator" ""]
  134. set rc [lindex $res 0]
  135. list $rc \
  136. [regexp {Error: missing argument to -separator} $res]
  137. } {1 1}
  138. # -stats print memory stats before each finalize
  139. do_test shell1-1.14b.1 {
  140. catchcmd "-stats test.db" ""
  141. } {0 {}}
  142. # -nullvalue 'text' set text string for NULL values
  143. do_test shell1-1.15.1 {
  144. catchcmd "-nullvalue 'x' test.db" ""
  145. } {0 {}}
  146. do_test shell1-1.15.2 {
  147. catchcmd "-nullvalue x test.db" ""
  148. } {0 {}}
  149. do_test shell1-1.15.3 {
  150. set res [catchcmd "-nullvalue" ""]
  151. set rc [lindex $res 0]
  152. list $rc \
  153. [regexp {Error: missing argument to -nullvalue} $res]
  154. } {1 1}
  155. # -version show SQLite version
  156. do_test shell1-1.16.1 {
  157. set x [catchcmd "-version test.db" ""]
  158. } {/3.[0-9.]+ 20\d\d-[01]\d-\d\d \d\d:\d\d:\d\d [0-9a-f]+/}
  159. #----------------------------------------------------------------------------
  160. # Test cases shell1-2.*: Basic "dot" command token parsing.
  161. #
  162. # check first token handling
  163. do_test shell1-2.1.1 {
  164. catchcmd "test.db" ".foo"
  165. } {1 {Error: unknown command or invalid arguments: "foo". Enter ".help" for help}}
  166. do_test shell1-2.1.2 {
  167. catchcmd "test.db" ".\"foo OFF\""
  168. } {1 {Error: unknown command or invalid arguments: "foo OFF". Enter ".help" for help}}
  169. do_test shell1-2.1.3 {
  170. catchcmd "test.db" ".\'foo OFF\'"
  171. } {1 {Error: unknown command or invalid arguments: "foo OFF". Enter ".help" for help}}
  172. # unbalanced quotes
  173. do_test shell1-2.2.1 {
  174. catchcmd "test.db" ".\"foo OFF"
  175. } {1 {Error: unknown command or invalid arguments: "foo OFF". Enter ".help" for help}}
  176. do_test shell1-2.2.2 {
  177. catchcmd "test.db" ".\'foo OFF"
  178. } {1 {Error: unknown command or invalid arguments: "foo OFF". Enter ".help" for help}}
  179. do_test shell1-2.2.3 {
  180. catchcmd "test.db" ".explain \"OFF"
  181. } {0 {}}
  182. do_test shell1-2.2.4 {
  183. catchcmd "test.db" ".explain \'OFF"
  184. } {0 {}}
  185. do_test shell1-2.2.5 {
  186. catchcmd "test.db" ".mode \"insert FOO"
  187. } {1 {Error: mode should be one of: column csv html insert line list tabs tcl}}
  188. do_test shell1-2.2.6 {
  189. catchcmd "test.db" ".mode \'insert FOO"
  190. } {1 {Error: mode should be one of: column csv html insert line list tabs tcl}}
  191. # check multiple tokens, and quoted tokens
  192. do_test shell1-2.3.1 {
  193. catchcmd "test.db" ".explain 1"
  194. } {0 {}}
  195. do_test shell1-2.3.2 {
  196. catchcmd "test.db" ".explain on"
  197. } {0 {}}
  198. do_test shell1-2.3.3 {
  199. catchcmd "test.db" ".explain \"1 2 3\""
  200. } {1 {ERROR: Not a boolean value: "1 2 3". Assuming "no".}}
  201. do_test shell1-2.3.4 {
  202. catchcmd "test.db" ".explain \"OFF\""
  203. } {0 {}}
  204. do_test shell1-2.3.5 {
  205. catchcmd "test.db" ".\'explain\' \'OFF\'"
  206. } {0 {}}
  207. do_test shell1-2.3.6 {
  208. catchcmd "test.db" ".explain \'OFF\'"
  209. } {0 {}}
  210. do_test shell1-2.3.7 {
  211. catchcmd "test.db" ".\'explain\' \'OFF\'"
  212. } {0 {}}
  213. # check quoted args are unquoted
  214. do_test shell1-2.4.1 {
  215. catchcmd "test.db" ".mode FOO"
  216. } {1 {Error: mode should be one of: column csv html insert line list tabs tcl}}
  217. do_test shell1-2.4.2 {
  218. catchcmd "test.db" ".mode csv"
  219. } {0 {}}
  220. do_test shell1-2.4.2 {
  221. catchcmd "test.db" ".mode \"csv\""
  222. } {0 {}}
  223. #----------------------------------------------------------------------------
  224. # Test cases shell1-3.*: Basic test that "dot" command can be called.
  225. #
  226. # .backup ?DB? FILE Backup DB (default "main") to FILE
  227. do_test shell1-3.1.1 {
  228. catchcmd "test.db" ".backup"
  229. } {1 {missing FILENAME argument on .backup}}
  230. do_test shell1-3.1.2 {
  231. catchcmd "test.db" ".backup FOO"
  232. } {0 {}}
  233. do_test shell1-3.1.3 {
  234. catchcmd "test.db" ".backup FOO BAR"
  235. } {1 {Error: unknown database FOO}}
  236. do_test shell1-3.1.4 {
  237. # too many arguments
  238. catchcmd "test.db" ".backup FOO BAR BAD"
  239. } {1 {too many arguments to .backup}}
  240. # .bail ON|OFF Stop after hitting an error. Default OFF
  241. do_test shell1-3.2.1 {
  242. catchcmd "test.db" ".bail"
  243. } {1 {Error: unknown command or invalid arguments: "bail". Enter ".help" for help}}
  244. do_test shell1-3.2.2 {
  245. catchcmd "test.db" ".bail ON"
  246. } {0 {}}
  247. do_test shell1-3.2.3 {
  248. catchcmd "test.db" ".bail OFF"
  249. } {0 {}}
  250. do_test shell1-3.2.4 {
  251. # too many arguments
  252. catchcmd "test.db" ".bail OFF BAD"
  253. } {1 {Error: unknown command or invalid arguments: "bail". Enter ".help" for help}}
  254. # .databases List names and files of attached databases
  255. do_test shell1-3.3.1 {
  256. catchcmd "-csv test.db" ".databases"
  257. } "/0 +.*main +[string map {/ .} [string range [get_pwd] 0 10]].*/"
  258. do_test shell1-3.3.2 {
  259. # too many arguments
  260. catchcmd "test.db" ".databases BAD"
  261. } {1 {Error: unknown command or invalid arguments: "databases". Enter ".help" for help}}
  262. # .dump ?TABLE? ... Dump the database in an SQL text format
  263. # If TABLE specified, only dump tables matching
  264. # LIKE pattern TABLE.
  265. do_test shell1-3.4.1 {
  266. set res [catchcmd "test.db" ".dump"]
  267. list [regexp {BEGIN TRANSACTION;} $res] \
  268. [regexp {COMMIT;} $res]
  269. } {1 1}
  270. do_test shell1-3.4.2 {
  271. set res [catchcmd "test.db" ".dump FOO"]
  272. list [regexp {BEGIN TRANSACTION;} $res] \
  273. [regexp {COMMIT;} $res]
  274. } {1 1}
  275. do_test shell1-3.4.3 {
  276. # too many arguments
  277. catchcmd "test.db" ".dump FOO BAD"
  278. } {1 {Error: unknown command or invalid arguments: "dump". Enter ".help" for help}}
  279. # .echo ON|OFF Turn command echo on or off
  280. do_test shell1-3.5.1 {
  281. catchcmd "test.db" ".echo"
  282. } {1 {Error: unknown command or invalid arguments: "echo". Enter ".help" for help}}
  283. do_test shell1-3.5.2 {
  284. catchcmd "test.db" ".echo ON"
  285. } {0 {}}
  286. do_test shell1-3.5.3 {
  287. catchcmd "test.db" ".echo OFF"
  288. } {0 {}}
  289. do_test shell1-3.5.4 {
  290. # too many arguments
  291. catchcmd "test.db" ".echo OFF BAD"
  292. } {1 {Error: unknown command or invalid arguments: "echo". Enter ".help" for help}}
  293. # .exit Exit this program
  294. do_test shell1-3.6.1 {
  295. catchcmd "test.db" ".exit"
  296. } {0 {}}
  297. # .explain ON|OFF Turn output mode suitable for EXPLAIN on or off.
  298. do_test shell1-3.7.1 {
  299. catchcmd "test.db" ".explain"
  300. # explain is the exception to the booleans. without an option, it turns it on.
  301. } {0 {}}
  302. do_test shell1-3.7.2 {
  303. catchcmd "test.db" ".explain ON"
  304. } {0 {}}
  305. do_test shell1-3.7.3 {
  306. catchcmd "test.db" ".explain OFF"
  307. } {0 {}}
  308. do_test shell1-3.7.4 {
  309. # too many arguments
  310. catchcmd "test.db" ".explain OFF BAD"
  311. } {1 {Error: unknown command or invalid arguments: "explain". Enter ".help" for help}}
  312. # .header(s) ON|OFF Turn display of headers on or off
  313. do_test shell1-3.9.1 {
  314. catchcmd "test.db" ".header"
  315. } {1 {Error: unknown command or invalid arguments: "header". Enter ".help" for help}}
  316. do_test shell1-3.9.2 {
  317. catchcmd "test.db" ".header ON"
  318. } {0 {}}
  319. do_test shell1-3.9.3 {
  320. catchcmd "test.db" ".header OFF"
  321. } {0 {}}
  322. do_test shell1-3.9.4 {
  323. # too many arguments
  324. catchcmd "test.db" ".header OFF BAD"
  325. } {1 {Error: unknown command or invalid arguments: "header". Enter ".help" for help}}
  326. do_test shell1-3.9.5 {
  327. catchcmd "test.db" ".headers"
  328. } {1 {Error: unknown command or invalid arguments: "headers". Enter ".help" for help}}
  329. do_test shell1-3.9.6 {
  330. catchcmd "test.db" ".headers ON"
  331. } {0 {}}
  332. do_test shell1-3.9.7 {
  333. catchcmd "test.db" ".headers OFF"
  334. } {0 {}}
  335. do_test shell1-3.9.8 {
  336. # too many arguments
  337. catchcmd "test.db" ".headers OFF BAD"
  338. } {1 {Error: unknown command or invalid arguments: "headers". Enter ".help" for help}}
  339. # .help Show this message
  340. do_test shell1-3.10.1 {
  341. set res [catchcmd "test.db" ".help"]
  342. # look for a few of the possible help commands
  343. list [regexp {.help} $res] \
  344. [regexp {.quit} $res] \
  345. [regexp {.show} $res]
  346. } {1 1 1}
  347. do_test shell1-3.10.2 {
  348. # we allow .help to take extra args (it is help after all)
  349. set res [catchcmd "test.db" ".help BAD"]
  350. # look for a few of the possible help commands
  351. list [regexp {.help} $res] \
  352. [regexp {.quit} $res] \
  353. [regexp {.show} $res]
  354. } {1 1 1}
  355. # .import FILE TABLE Import data from FILE into TABLE
  356. do_test shell1-3.11.1 {
  357. catchcmd "test.db" ".import"
  358. } {1 {Error: unknown command or invalid arguments: "import". Enter ".help" for help}}
  359. do_test shell1-3.11.2 {
  360. catchcmd "test.db" ".import FOO"
  361. } {1 {Error: unknown command or invalid arguments: "import". Enter ".help" for help}}
  362. #do_test shell1-3.11.2 {
  363. # catchcmd "test.db" ".import FOO BAR"
  364. #} {1 {Error: no such table: BAR}}
  365. do_test shell1-3.11.3 {
  366. # too many arguments
  367. catchcmd "test.db" ".import FOO BAR BAD"
  368. } {1 {Error: unknown command or invalid arguments: "import". Enter ".help" for help}}
  369. # .indices ?TABLE? Show names of all indices
  370. # If TABLE specified, only show indices for tables
  371. # matching LIKE pattern TABLE.
  372. do_test shell1-3.12.1 {
  373. catchcmd "test.db" ".indices"
  374. } {0 {}}
  375. do_test shell1-3.12.2 {
  376. catchcmd "test.db" ".indices FOO"
  377. } {0 {}}
  378. do_test shell1-3.12.3 {
  379. # too many arguments
  380. catchcmd "test.db" ".indices FOO BAD"
  381. } {1 {Error: unknown command or invalid arguments: "indices". Enter ".help" for help}}
  382. # .mode MODE ?TABLE? Set output mode where MODE is one of:
  383. # csv Comma-separated values
  384. # column Left-aligned columns. (See .width)
  385. # html HTML <table> code
  386. # insert SQL insert statements for TABLE
  387. # line One value per line
  388. # list Values delimited by .separator string
  389. # tabs Tab-separated values
  390. # tcl TCL list elements
  391. do_test shell1-3.13.1 {
  392. catchcmd "test.db" ".mode"
  393. } {1 {Error: unknown command or invalid arguments: "mode". Enter ".help" for help}}
  394. do_test shell1-3.13.2 {
  395. catchcmd "test.db" ".mode FOO"
  396. } {1 {Error: mode should be one of: column csv html insert line list tabs tcl}}
  397. do_test shell1-3.13.3 {
  398. catchcmd "test.db" ".mode csv"
  399. } {0 {}}
  400. do_test shell1-3.13.4 {
  401. catchcmd "test.db" ".mode column"
  402. } {0 {}}
  403. do_test shell1-3.13.5 {
  404. catchcmd "test.db" ".mode html"
  405. } {0 {}}
  406. do_test shell1-3.13.6 {
  407. catchcmd "test.db" ".mode insert"
  408. } {0 {}}
  409. do_test shell1-3.13.7 {
  410. catchcmd "test.db" ".mode line"
  411. } {0 {}}
  412. do_test shell1-3.13.8 {
  413. catchcmd "test.db" ".mode list"
  414. } {0 {}}
  415. do_test shell1-3.13.9 {
  416. catchcmd "test.db" ".mode tabs"
  417. } {0 {}}
  418. do_test shell1-3.13.10 {
  419. catchcmd "test.db" ".mode tcl"
  420. } {0 {}}
  421. do_test shell1-3.13.11 {
  422. # too many arguments
  423. catchcmd "test.db" ".mode tcl BAD"
  424. } {1 {Error: invalid arguments: "BAD". Enter ".help" for help}}
  425. # don't allow partial mode type matches
  426. do_test shell1-3.13.12 {
  427. catchcmd "test.db" ".mode l"
  428. } {1 {Error: mode should be one of: column csv html insert line list tabs tcl}}
  429. do_test shell1-3.13.13 {
  430. catchcmd "test.db" ".mode li"
  431. } {1 {Error: mode should be one of: column csv html insert line list tabs tcl}}
  432. do_test shell1-3.13.14 {
  433. catchcmd "test.db" ".mode lin"
  434. } {1 {Error: mode should be one of: column csv html insert line list tabs tcl}}
  435. # .nullvalue STRING Print STRING in place of NULL values
  436. do_test shell1-3.14.1 {
  437. catchcmd "test.db" ".nullvalue"
  438. } {1 {Error: unknown command or invalid arguments: "nullvalue". Enter ".help" for help}}
  439. do_test shell1-3.14.2 {
  440. catchcmd "test.db" ".nullvalue FOO"
  441. } {0 {}}
  442. do_test shell1-3.14.3 {
  443. # too many arguments
  444. catchcmd "test.db" ".nullvalue FOO BAD"
  445. } {1 {Error: unknown command or invalid arguments: "nullvalue". Enter ".help" for help}}
  446. # .output FILENAME Send output to FILENAME
  447. do_test shell1-3.15.1 {
  448. catchcmd "test.db" ".output"
  449. } {1 {Error: unknown command or invalid arguments: "output". Enter ".help" for help}}
  450. do_test shell1-3.15.2 {
  451. catchcmd "test.db" ".output FOO"
  452. } {0 {}}
  453. do_test shell1-3.15.3 {
  454. # too many arguments
  455. catchcmd "test.db" ".output FOO BAD"
  456. } {1 {Error: unknown command or invalid arguments: "output". Enter ".help" for help}}
  457. # .output stdout Send output to the screen
  458. do_test shell1-3.16.1 {
  459. catchcmd "test.db" ".output stdout"
  460. } {0 {}}
  461. do_test shell1-3.16.2 {
  462. # too many arguments
  463. catchcmd "test.db" ".output stdout BAD"
  464. } {1 {Error: unknown command or invalid arguments: "output". Enter ".help" for help}}
  465. # .prompt MAIN CONTINUE Replace the standard prompts
  466. do_test shell1-3.17.1 {
  467. catchcmd "test.db" ".prompt"
  468. } {1 {Error: unknown command or invalid arguments: "prompt". Enter ".help" for help}}
  469. do_test shell1-3.17.2 {
  470. catchcmd "test.db" ".prompt FOO"
  471. } {0 {}}
  472. do_test shell1-3.17.3 {
  473. catchcmd "test.db" ".prompt FOO BAR"
  474. } {0 {}}
  475. do_test shell1-3.17.4 {
  476. # too many arguments
  477. catchcmd "test.db" ".prompt FOO BAR BAD"
  478. } {1 {Error: unknown command or invalid arguments: "prompt". Enter ".help" for help}}
  479. # .quit Exit this program
  480. do_test shell1-3.18.1 {
  481. catchcmd "test.db" ".quit"
  482. } {0 {}}
  483. do_test shell1-3.18.2 {
  484. # too many arguments
  485. catchcmd "test.db" ".quit BAD"
  486. } {1 {Error: unknown command or invalid arguments: "quit". Enter ".help" for help}}
  487. # .read FILENAME Execute SQL in FILENAME
  488. do_test shell1-3.19.1 {
  489. catchcmd "test.db" ".read"
  490. } {1 {Error: unknown command or invalid arguments: "read". Enter ".help" for help}}
  491. do_test shell1-3.19.2 {
  492. forcedelete FOO
  493. catchcmd "test.db" ".read FOO"
  494. } {1 {Error: cannot open "FOO"}}
  495. do_test shell1-3.19.3 {
  496. # too many arguments
  497. catchcmd "test.db" ".read FOO BAD"
  498. } {1 {Error: unknown command or invalid arguments: "read". Enter ".help" for help}}
  499. # .restore ?DB? FILE Restore content of DB (default "main") from FILE
  500. do_test shell1-3.20.1 {
  501. catchcmd "test.db" ".restore"
  502. } {1 {Error: unknown command or invalid arguments: "restore". Enter ".help" for help}}
  503. do_test shell1-3.20.2 {
  504. catchcmd "test.db" ".restore FOO"
  505. } {0 {}}
  506. do_test shell1-3.20.3 {
  507. catchcmd "test.db" ".restore FOO BAR"
  508. } {1 {Error: unknown database FOO}}
  509. do_test shell1-3.20.4 {
  510. # too many arguments
  511. catchcmd "test.db" ".restore FOO BAR BAD"
  512. } {1 {Error: unknown command or invalid arguments: "restore". Enter ".help" for help}}
  513. # .schema ?TABLE? Show the CREATE statements
  514. # If TABLE specified, only show tables matching
  515. # LIKE pattern TABLE.
  516. do_test shell1-3.21.1 {
  517. catchcmd "test.db" ".schema"
  518. } {0 {}}
  519. do_test shell1-3.21.2 {
  520. catchcmd "test.db" ".schema FOO"
  521. } {0 {}}
  522. do_test shell1-3.21.3 {
  523. # too many arguments
  524. catchcmd "test.db" ".schema FOO BAD"
  525. } {1 {Error: unknown command or invalid arguments: "schema". Enter ".help" for help}}
  526. do_test shell1-3.21.4 {
  527. catchcmd "test.db" {
  528. CREATE TABLE t1(x);
  529. CREATE VIEW v2 AS SELECT x+1 AS y FROM t1;
  530. CREATE VIEW v1 AS SELECT y+1 FROM v2;
  531. }
  532. catchcmd "test.db" ".schema"
  533. } {0 {CREATE TABLE t1(x);
  534. CREATE VIEW v2 AS SELECT x+1 AS y FROM t1;
  535. CREATE VIEW v1 AS SELECT y+1 FROM v2;}}
  536. db eval {DROP VIEW v1; DROP VIEW v2; DROP TABLE t1;}
  537. # .separator STRING Change separator used by output mode and .import
  538. do_test shell1-3.22.1 {
  539. catchcmd "test.db" ".separator"
  540. } {1 {Error: unknown command or invalid arguments: "separator". Enter ".help" for help}}
  541. do_test shell1-3.22.2 {
  542. catchcmd "test.db" ".separator FOO"
  543. } {0 {}}
  544. do_test shell1-3.22.3 {
  545. # too many arguments
  546. catchcmd "test.db" ".separator FOO BAD"
  547. } {1 {Error: unknown command or invalid arguments: "separator". Enter ".help" for help}}
  548. # .show Show the current values for various settings
  549. do_test shell1-3.23.1 {
  550. set res [catchcmd "test.db" ".show"]
  551. list [regexp {echo:} $res] \
  552. [regexp {explain:} $res] \
  553. [regexp {headers:} $res] \
  554. [regexp {mode:} $res] \
  555. [regexp {nullvalue:} $res] \
  556. [regexp {output:} $res] \
  557. [regexp {separator:} $res] \
  558. [regexp {stats:} $res] \
  559. [regexp {width:} $res]
  560. } {1 1 1 1 1 1 1 1 1}
  561. do_test shell1-3.23.2 {
  562. # too many arguments
  563. catchcmd "test.db" ".show BAD"
  564. } {1 {Error: unknown command or invalid arguments: "show". Enter ".help" for help}}
  565. # .stats ON|OFF Turn stats on or off
  566. do_test shell1-3.23b.1 {
  567. catchcmd "test.db" ".stats"
  568. } {1 {Error: unknown command or invalid arguments: "stats". Enter ".help" for help}}
  569. do_test shell1-3.23b.2 {
  570. catchcmd "test.db" ".stats ON"
  571. } {0 {}}
  572. do_test shell1-3.23b.3 {
  573. catchcmd "test.db" ".stats OFF"
  574. } {0 {}}
  575. do_test shell1-3.23b.4 {
  576. # too many arguments
  577. catchcmd "test.db" ".stats OFF BAD"
  578. } {1 {Error: unknown command or invalid arguments: "stats". Enter ".help" for help}}
  579. # .tables ?TABLE? List names of tables
  580. # If TABLE specified, only list tables matching
  581. # LIKE pattern TABLE.
  582. do_test shell1-3.24.1 {
  583. catchcmd "test.db" ".tables"
  584. } {0 {}}
  585. do_test shell1-3.24.2 {
  586. catchcmd "test.db" ".tables FOO"
  587. } {0 {}}
  588. do_test shell1-3.24.3 {
  589. # too many arguments
  590. catchcmd "test.db" ".tables FOO BAD"
  591. } {1 {Error: unknown command or invalid arguments: "tables". Enter ".help" for help}}
  592. # .timeout MS Try opening locked tables for MS milliseconds
  593. do_test shell1-3.25.1 {
  594. catchcmd "test.db" ".timeout"
  595. } {1 {Error: unknown command or invalid arguments: "timeout". Enter ".help" for help}}
  596. do_test shell1-3.25.2 {
  597. catchcmd "test.db" ".timeout zzz"
  598. # this should be treated the same as a '0' timeout
  599. } {0 {}}
  600. do_test shell1-3.25.3 {
  601. catchcmd "test.db" ".timeout 1"
  602. } {0 {}}
  603. do_test shell1-3.25.4 {
  604. # too many arguments
  605. catchcmd "test.db" ".timeout 1 BAD"
  606. } {1 {Error: unknown command or invalid arguments: "timeout". Enter ".help" for help}}
  607. # .width NUM NUM ... Set column widths for "column" mode
  608. do_test shell1-3.26.1 {
  609. catchcmd "test.db" ".width"
  610. } {1 {Error: unknown command or invalid arguments: "width". Enter ".help" for help}}
  611. do_test shell1-3.26.2 {
  612. catchcmd "test.db" ".width xxx"
  613. # this should be treated the same as a '0' width for col 1
  614. } {0 {}}
  615. do_test shell1-3.26.3 {
  616. catchcmd "test.db" ".width xxx yyy"
  617. # this should be treated the same as a '0' width for col 1 and 2
  618. } {0 {}}
  619. do_test shell1-3.26.4 {
  620. catchcmd "test.db" ".width 1 1"
  621. # this should be treated the same as a '1' width for col 1 and 2
  622. } {0 {}}
  623. do_test shell1-3.26.5 {
  624. catchcmd "test.db" ".mode column\n.width 10 -10\nSELECT 'abcdefg', 123456;"
  625. # this should be treated the same as a '1' width for col 1 and 2
  626. } {0 {abcdefg 123456}}
  627. do_test shell1-3.26.6 {
  628. catchcmd "test.db" ".mode column\n.width -10 10\nSELECT 'abcdefg', 123456;"
  629. # this should be treated the same as a '1' width for col 1 and 2
  630. } {0 { abcdefg 123456 }}
  631. # .timer ON|OFF Turn the CPU timer measurement on or off
  632. do_test shell1-3.27.1 {
  633. catchcmd "test.db" ".timer"
  634. } {1 {Error: unknown command or invalid arguments: "timer". Enter ".help" for help}}
  635. do_test shell1-3.27.2 {
  636. catchcmd "test.db" ".timer ON"
  637. } {0 {}}
  638. do_test shell1-3.27.3 {
  639. catchcmd "test.db" ".timer OFF"
  640. } {0 {}}
  641. do_test shell1-3.27.4 {
  642. # too many arguments
  643. catchcmd "test.db" ".timer OFF BAD"
  644. } {1 {Error: unknown command or invalid arguments: "timer". Enter ".help" for help}}
  645. do_test shell1-3-28.1 {
  646. catchcmd test.db \
  647. ".log stdout\nSELECT coalesce(sqlite_log(123,'hello'),'456');"
  648. } "0 {(123) hello\n456}"
  649. do_test shell1-3-29.1 {
  650. catchcmd "test.db" ".print this is a test"
  651. } {0 {this is a test}}
  652. # dot-command argument quoting
  653. do_test shell1-3-30.1 {
  654. catchcmd {test.db} {.print "this\"is'a\055test" 'this\"is\\a\055test'}
  655. } {0 {this"is'a-test this\"is\\a\055test}}
  656. do_test shell1-3-31.1 {
  657. catchcmd {test.db} {.print "this\nis\ta\\test" 'this\nis\ta\\test'}
  658. } [list 0 "this\nis\ta\\test this\\nis\\ta\\\\test"]
  659. # Test the output of the ".dump" command
  660. #
  661. do_test shell1-4.1 {
  662. db close
  663. forcedelete test.db
  664. sqlite3 db test.db
  665. db eval {
  666. PRAGMA encoding=UTF16;
  667. CREATE TABLE t1(x);
  668. INSERT INTO t1 VALUES(null), (''), (1), (2.25), ('hello'), (x'807f');
  669. }
  670. catchcmd test.db {.dump}
  671. } {0 {PRAGMA foreign_keys=OFF;
  672. BEGIN TRANSACTION;
  673. CREATE TABLE t1(x);
  674. INSERT INTO "t1" VALUES(NULL);
  675. INSERT INTO "t1" VALUES('');
  676. INSERT INTO "t1" VALUES(1);
  677. INSERT INTO "t1" VALUES(2.25);
  678. INSERT INTO "t1" VALUES('hello');
  679. INSERT INTO "t1" VALUES(X'807F');
  680. COMMIT;}}
  681. # Test the output of ".mode insert"
  682. #
  683. do_test shell1-4.2 {
  684. catchcmd test.db ".mode insert t1\nselect * from t1;"
  685. } {0 {INSERT INTO t1 VALUES(NULL);
  686. INSERT INTO t1 VALUES('');
  687. INSERT INTO t1 VALUES(1);
  688. INSERT INTO t1 VALUES(2.25);
  689. INSERT INTO t1 VALUES('hello');
  690. INSERT INTO t1 VALUES(X'807f');}}
  691. # Test the output of ".mode tcl"
  692. #
  693. do_test shell1-4.3 {
  694. db close
  695. forcedelete test.db
  696. sqlite3 db test.db
  697. db eval {
  698. PRAGMA encoding=UTF8;
  699. CREATE TABLE t1(x);
  700. INSERT INTO t1 VALUES(null), (''), (1), (2.25), ('hello'), (x'807f');
  701. }
  702. catchcmd test.db ".mode tcl\nselect * from t1;"
  703. } {0 {""
  704. ""
  705. "1"
  706. "2.25"
  707. "hello"
  708. "\200\177"}}
  709. # Test the output of ".mode tcl" with multiple columns
  710. #
  711. do_test shell1-4.4 {
  712. db eval {
  713. CREATE TABLE t2(x,y);
  714. INSERT INTO t2 VALUES(null, ''), (1, 2.25), ('hello', x'807f');
  715. }
  716. catchcmd test.db ".mode tcl\nselect * from t2;"
  717. } {0 {"" ""
  718. "1" "2.25"
  719. "hello" "\200\177"}}
  720. # Test the output of ".mode tcl" with ".nullvalue"
  721. #
  722. do_test shell1-4.5 {
  723. catchcmd test.db ".mode tcl\n.nullvalue NULL\nselect * from t2;"
  724. } {0 {"NULL" ""
  725. "1" "2.25"
  726. "hello" "\200\177"}}
  727. # Test the output of ".mode tcl" with Tcl reserved characters
  728. #
  729. do_test shell1-4.6 {
  730. db eval {
  731. CREATE TABLE tcl1(x);
  732. INSERT INTO tcl1 VALUES('"'), ('['), (']'), ('\{'), ('\}'), (';'), ('$');
  733. }
  734. foreach {x y} [catchcmd test.db ".mode tcl\nselect * from tcl1;"] break
  735. list $x $y [llength $y]
  736. } {0 {"\""
  737. "["
  738. "]"
  739. "\\{"
  740. "\\}"
  741. ";"
  742. "$"} 7}
  743. finish_test