capi2.test 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. # 2003 January 29
  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. # This file implements regression tests for SQLite library. The
  12. # focus of this script testing the callback-free C/C++ API.
  13. #
  14. # $Id: capi2.test,v 1.37 2008/12/30 17:55:00 drh Exp $
  15. #
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. # Return the text values from the current row pointed at by STMT as a list.
  19. proc get_row_values {STMT} {
  20. set VALUES [list]
  21. for {set i 0} {$i < [sqlite3_data_count $STMT]} {incr i} {
  22. lappend VALUES [sqlite3_column_text $STMT $i]
  23. }
  24. return $VALUES
  25. }
  26. # Return the column names followed by declaration types for the result set
  27. # of the SQL statement STMT.
  28. #
  29. # i.e. for:
  30. # CREATE TABLE abc(a text, b integer);
  31. # SELECT * FROM abc;
  32. #
  33. # The result is {a b text integer}
  34. proc get_column_names {STMT} {
  35. set VALUES [list]
  36. for {set i 0} {$i < [sqlite3_column_count $STMT]} {incr i} {
  37. lappend VALUES [sqlite3_column_name $STMT $i]
  38. }
  39. for {set i 0} {$i < [sqlite3_column_count $STMT]} {incr i} {
  40. lappend VALUES [sqlite3_column_decltype $STMT $i]
  41. }
  42. return $VALUES
  43. }
  44. # Check basic functionality
  45. #
  46. do_test capi2-1.1 {
  47. set DB [sqlite3_connection_pointer db]
  48. execsql {CREATE TABLE t1(a,b,c)}
  49. set VM [sqlite3_prepare $DB {SELECT name, rowid FROM sqlite_master} -1 TAIL]
  50. set TAIL
  51. } {}
  52. do_test capi2-1.2 {
  53. sqlite3_step $VM
  54. } {SQLITE_ROW}
  55. do_test capi2-1.3 {
  56. sqlite3_data_count $VM
  57. } {2}
  58. do_test capi2-1.4 {
  59. get_row_values $VM
  60. } {t1 1}
  61. do_test capi2-1.5 {
  62. get_column_names $VM
  63. } {name rowid text INTEGER}
  64. do_test capi2-1.6 {
  65. sqlite3_step $VM
  66. } {SQLITE_DONE}
  67. do_test capi2-1.7 {
  68. list [sqlite3_column_count $VM] [get_row_values $VM] [get_column_names $VM]
  69. } {2 {} {name rowid text INTEGER}}
  70. # This used to be SQLITE_MISUSE. But now we automatically reset prepared
  71. # statements.
  72. ifcapable autoreset {
  73. do_test capi2-1.8 {
  74. sqlite3_step $VM
  75. } {SQLITE_ROW}
  76. } else {
  77. do_test capi2-1.8 {
  78. sqlite3_step $VM
  79. } {SQLITE_MISUSE}
  80. }
  81. # Update: In v2, once SQLITE_MISUSE is returned the statement handle cannot
  82. # be interrogated for more information. However in v3, since the column
  83. # count, names and types are determined at compile time, these are still
  84. # accessible after an SQLITE_MISUSE error.
  85. do_test capi2-1.9 {
  86. sqlite3_reset $VM
  87. list [sqlite3_column_count $VM] [get_row_values $VM] [get_column_names $VM]
  88. } {2 {} {name rowid text INTEGER}}
  89. do_test capi2-1.10 {
  90. sqlite3_data_count $VM
  91. } {0}
  92. do_test capi2-1.11 {
  93. sqlite3_finalize $VM
  94. } {SQLITE_OK}
  95. # Check to make sure that the "tail" of a multi-statement SQL script
  96. # is returned by sqlite3_prepare.
  97. #
  98. do_test capi2-2.1 {
  99. set SQL {
  100. SELECT name, rowid FROM sqlite_master;
  101. SELECT name, rowid FROM sqlite_master WHERE 0;
  102. -- A comment at the end
  103. }
  104. set VM [sqlite3_prepare $DB $SQL -1 SQL]
  105. set SQL
  106. } {
  107. SELECT name, rowid FROM sqlite_master WHERE 0;
  108. -- A comment at the end
  109. }
  110. do_test capi2-2.2 {
  111. set r [sqlite3_step $VM]
  112. lappend r [sqlite3_column_count $VM] \
  113. [get_row_values $VM] \
  114. [get_column_names $VM]
  115. } {SQLITE_ROW 2 {t1 1} {name rowid text INTEGER}}
  116. do_test capi2-2.3 {
  117. set r [sqlite3_step $VM]
  118. lappend r [sqlite3_column_count $VM] \
  119. [get_row_values $VM] \
  120. [get_column_names $VM]
  121. } {SQLITE_DONE 2 {} {name rowid text INTEGER}}
  122. do_test capi2-2.4 {
  123. sqlite3_finalize $VM
  124. } {SQLITE_OK}
  125. do_test capi2-2.5 {
  126. set VM [sqlite3_prepare $DB $SQL -1 SQL]
  127. set SQL
  128. } {
  129. -- A comment at the end
  130. }
  131. do_test capi2-2.6 {
  132. set r [sqlite3_step $VM]
  133. lappend r [sqlite3_column_count $VM] \
  134. [get_row_values $VM] \
  135. [get_column_names $VM]
  136. } {SQLITE_DONE 2 {} {name rowid text INTEGER}}
  137. do_test capi2-2.7 {
  138. sqlite3_finalize $VM
  139. } {SQLITE_OK}
  140. do_test capi2-2.8 {
  141. set VM [sqlite3_prepare $DB $SQL -1 SQL]
  142. list $SQL $VM
  143. } {{} {}}
  144. # Check the error handling.
  145. #
  146. do_test capi2-3.1 {
  147. set rc [catch {
  148. sqlite3_prepare $DB {select bogus from sqlite_master} -1 TAIL
  149. } msg]
  150. lappend rc $msg $TAIL
  151. } {1 {(1) no such column: bogus} {}}
  152. do_test capi2-3.2 {
  153. set rc [catch {
  154. sqlite3_prepare $DB {select bogus from } -1 TAIL
  155. } msg]
  156. lappend rc $msg $TAIL
  157. } {1 {(1) near " ": syntax error} {}}
  158. do_test capi2-3.3 {
  159. set rc [catch {
  160. sqlite3_prepare $DB {;;;;select bogus from sqlite_master} -1 TAIL
  161. } msg]
  162. lappend rc $msg $TAIL
  163. } {1 {(1) no such column: bogus} {}}
  164. do_test capi2-3.4 {
  165. set rc [catch {
  166. sqlite3_prepare $DB {select bogus from sqlite_master;x;} -1 TAIL
  167. } msg]
  168. lappend rc $msg $TAIL
  169. } {1 {(1) no such column: bogus} {x;}}
  170. do_test capi2-3.5 {
  171. set rc [catch {
  172. sqlite3_prepare $DB {select bogus from sqlite_master;;;x;} -1 TAIL
  173. } msg]
  174. lappend rc $msg $TAIL
  175. } {1 {(1) no such column: bogus} {;;x;}}
  176. do_test capi2-3.6 {
  177. set rc [catch {
  178. sqlite3_prepare $DB {select 5/0} -1 TAIL
  179. } VM]
  180. lappend rc $TAIL
  181. } {0 {}}
  182. do_test capi2-3.7 {
  183. list [sqlite3_step $VM] \
  184. [sqlite3_column_count $VM] \
  185. [get_row_values $VM] \
  186. [get_column_names $VM]
  187. } {SQLITE_ROW 1 {{}} {5/0 {}}}
  188. do_test capi2-3.8 {
  189. sqlite3_finalize $VM
  190. } {SQLITE_OK}
  191. do_test capi2-3.9 {
  192. execsql {CREATE UNIQUE INDEX i1 ON t1(a)}
  193. set VM [sqlite3_prepare $DB {INSERT INTO t1 VALUES(1,2,3)} -1 TAIL]
  194. set TAIL
  195. } {}
  196. do_test capi2-3.9b {db changes} {0}
  197. do_test capi2-3.10 {
  198. list [sqlite3_step $VM] \
  199. [sqlite3_column_count $VM] \
  200. [get_row_values $VM] \
  201. [get_column_names $VM]
  202. } {SQLITE_DONE 0 {} {}}
  203. # Update for v3 - the change has not actually happened until the query is
  204. # finalized. Is this going to cause trouble for anyone? Lee Nelson maybe?
  205. # (Later:) The change now happens just before SQLITE_DONE is returned.
  206. do_test capi2-3.10b {db changes} {1}
  207. do_test capi2-3.11 {
  208. sqlite3_finalize $VM
  209. } {SQLITE_OK}
  210. do_test capi2-3.11b {db changes} {1}
  211. #do_test capi2-3.12-misuse {
  212. # sqlite3_finalize $VM
  213. #} {SQLITE_MISUSE}
  214. do_test capi2-3.13 {
  215. set VM [sqlite3_prepare $DB {INSERT INTO t1 VALUES(1,3,4)} -1 TAIL]
  216. list [sqlite3_step $VM] \
  217. [sqlite3_column_count $VM] \
  218. [get_row_values $VM] \
  219. [get_column_names $VM]
  220. } {SQLITE_ERROR 0 {} {}}
  221. # Update for v3: Preparing a statement does not affect the change counter.
  222. # (Test result changes from 0 to 1). (Later:) change counter updates occur
  223. # when sqlite3_step returns, not at finalize time.
  224. do_test capi2-3.13b {db changes} {0}
  225. do_test capi2-3.14 {
  226. list [sqlite3_finalize $VM] [sqlite3_errmsg $DB] \
  227. [sqlite3_extended_errcode $DB]
  228. } {SQLITE_CONSTRAINT {column a is not unique} SQLITE_CONSTRAINT_UNIQUE}
  229. do_test capi2-3.15 {
  230. set VM [sqlite3_prepare $DB {CREATE TABLE t2(a NOT NULL, b)} -1 TAIL]
  231. set TAIL
  232. } {}
  233. do_test capi2-3.16 {
  234. list [sqlite3_step $VM] \
  235. [sqlite3_column_count $VM] \
  236. [get_row_values $VM] \
  237. [get_column_names $VM]
  238. } {SQLITE_DONE 0 {} {}}
  239. do_test capi2-3.17 {
  240. list [sqlite3_finalize $VM] [sqlite3_errmsg $DB]
  241. } {SQLITE_OK {not an error}}
  242. do_test capi2-3.18 {
  243. set VM [sqlite3_prepare $DB {INSERT INTO t2 VALUES(NULL,2)} -1 TAIL]
  244. list [sqlite3_step $VM] \
  245. [sqlite3_column_count $VM] \
  246. [get_row_values $VM] \
  247. [get_column_names $VM]
  248. } {SQLITE_ERROR 0 {} {}}
  249. do_test capi2-3.19 {
  250. list [sqlite3_finalize $VM] [sqlite3_errmsg $DB] \
  251. [sqlite3_extended_errcode $DB]
  252. } {SQLITE_CONSTRAINT {t2.a may not be NULL} SQLITE_CONSTRAINT_NOTNULL}
  253. do_test capi2-3.20 {
  254. execsql {
  255. CREATE TABLE a1(message_id, name , UNIQUE(message_id, name) );
  256. INSERT INTO a1 VALUES(1, 1);
  257. }
  258. } {}
  259. do_test capi2-3.21 {
  260. set VM [sqlite3_prepare $DB {INSERT INTO a1 VALUES(1, 1)} -1 TAIL]
  261. sqlite3_step $VM
  262. } {SQLITE_ERROR}
  263. do_test capi2-3.22 {
  264. sqlite3_errcode $DB
  265. } {SQLITE_ERROR}
  266. do_test capi2-3.23 {
  267. sqlite3_finalize $VM
  268. } {SQLITE_CONSTRAINT}
  269. do_test capi2-3.24 {
  270. list [sqlite3_errcode $DB] [sqlite3_extended_errcode $DB]
  271. } {SQLITE_CONSTRAINT SQLITE_CONSTRAINT_UNIQUE}
  272. # Two or more virtual machines exists at the same time.
  273. #
  274. do_test capi2-4.1 {
  275. set VM1 [sqlite3_prepare $DB {INSERT INTO t2 VALUES(1,2)} -1 TAIL]
  276. set TAIL
  277. } {}
  278. do_test capi2-4.2 {
  279. set VM2 [sqlite3_prepare $DB {INSERT INTO t2 VALUES(2,3)} -1 TAIL]
  280. set TAIL
  281. } {}
  282. do_test capi2-4.3 {
  283. set VM3 [sqlite3_prepare $DB {INSERT INTO t2 VALUES(3,4)} -1 TAIL]
  284. set TAIL
  285. } {}
  286. do_test capi2-4.4 {
  287. list [sqlite3_step $VM2] \
  288. [sqlite3_column_count $VM2] \
  289. [get_row_values $VM2] \
  290. [get_column_names $VM2]
  291. } {SQLITE_DONE 0 {} {}}
  292. do_test capi2-4.5 {
  293. execsql {SELECT * FROM t2 ORDER BY a}
  294. } {2 3}
  295. do_test capi2-4.6 {
  296. sqlite3_finalize $VM2
  297. } {SQLITE_OK}
  298. do_test capi2-4.7 {
  299. list [sqlite3_step $VM3] \
  300. [sqlite3_column_count $VM3] \
  301. [get_row_values $VM3] \
  302. [get_column_names $VM3]
  303. } {SQLITE_DONE 0 {} {}}
  304. do_test capi2-4.8 {
  305. execsql {SELECT * FROM t2 ORDER BY a}
  306. } {2 3 3 4}
  307. do_test capi2-4.9 {
  308. sqlite3_finalize $VM3
  309. } {SQLITE_OK}
  310. do_test capi2-4.10 {
  311. list [sqlite3_step $VM1] \
  312. [sqlite3_column_count $VM1] \
  313. [get_row_values $VM1] \
  314. [get_column_names $VM1]
  315. } {SQLITE_DONE 0 {} {}}
  316. do_test capi2-4.11 {
  317. execsql {SELECT * FROM t2 ORDER BY a}
  318. } {1 2 2 3 3 4}
  319. do_test capi2-4.12 {
  320. sqlite3_finalize $VM1
  321. } {SQLITE_OK}
  322. # Interleaved SELECTs
  323. #
  324. do_test capi2-5.1 {
  325. set VM1 [sqlite3_prepare $DB {SELECT * FROM t2} -1 TAIL]
  326. set VM2 [sqlite3_prepare $DB {SELECT * FROM t2} -1 TAIL]
  327. set VM3 [sqlite3_prepare $DB {SELECT * FROM t2} -1 TAIL]
  328. list [sqlite3_step $VM1] \
  329. [sqlite3_column_count $VM1] \
  330. [get_row_values $VM1] \
  331. [get_column_names $VM1]
  332. } {SQLITE_ROW 2 {2 3} {a b {} {}}}
  333. do_test capi2-5.2 {
  334. list [sqlite3_step $VM2] \
  335. [sqlite3_column_count $VM2] \
  336. [get_row_values $VM2] \
  337. [get_column_names $VM2]
  338. } {SQLITE_ROW 2 {2 3} {a b {} {}}}
  339. do_test capi2-5.3 {
  340. list [sqlite3_step $VM1] \
  341. [sqlite3_column_count $VM1] \
  342. [get_row_values $VM1] \
  343. [get_column_names $VM1]
  344. } {SQLITE_ROW 2 {3 4} {a b {} {}}}
  345. do_test capi2-5.4 {
  346. list [sqlite3_step $VM3] \
  347. [sqlite3_column_count $VM3] \
  348. [get_row_values $VM3] \
  349. [get_column_names $VM3]
  350. } {SQLITE_ROW 2 {2 3} {a b {} {}}}
  351. do_test capi2-5.5 {
  352. list [sqlite3_step $VM3] \
  353. [sqlite3_column_count $VM3] \
  354. [get_row_values $VM3] \
  355. [get_column_names $VM3]
  356. } {SQLITE_ROW 2 {3 4} {a b {} {}}}
  357. do_test capi2-5.6 {
  358. list [sqlite3_step $VM3] \
  359. [sqlite3_column_count $VM3] \
  360. [get_row_values $VM3] \
  361. [get_column_names $VM3]
  362. } {SQLITE_ROW 2 {1 2} {a b {} {}}}
  363. do_test capi2-5.7 {
  364. list [sqlite3_step $VM3] \
  365. [sqlite3_column_count $VM3] \
  366. [get_row_values $VM3] \
  367. [get_column_names $VM3]
  368. } {SQLITE_DONE 2 {} {a b {} {}}}
  369. do_test capi2-5.8 {
  370. sqlite3_finalize $VM3
  371. } {SQLITE_OK}
  372. do_test capi2-5.9 {
  373. list [sqlite3_step $VM1] \
  374. [sqlite3_column_count $VM1] \
  375. [get_row_values $VM1] \
  376. [get_column_names $VM1]
  377. } {SQLITE_ROW 2 {1 2} {a b {} {}}}
  378. do_test capi2-5.10 {
  379. sqlite3_finalize $VM1
  380. } {SQLITE_OK}
  381. do_test capi2-5.11 {
  382. list [sqlite3_step $VM2] \
  383. [sqlite3_column_count $VM2] \
  384. [get_row_values $VM2] \
  385. [get_column_names $VM2]
  386. } {SQLITE_ROW 2 {3 4} {a b {} {}}}
  387. do_test capi2-5.12 {
  388. list [sqlite3_step $VM2] \
  389. [sqlite3_column_count $VM2] \
  390. [get_row_values $VM2] \
  391. [get_column_names $VM2]
  392. } {SQLITE_ROW 2 {1 2} {a b {} {}}}
  393. do_test capi2-5.11 {
  394. sqlite3_finalize $VM2
  395. } {SQLITE_OK}
  396. # Check for proper SQLITE_BUSY returns.
  397. #
  398. do_test capi2-6.1 {
  399. execsql {
  400. BEGIN;
  401. CREATE TABLE t3(x counter);
  402. INSERT INTO t3 VALUES(1);
  403. INSERT INTO t3 VALUES(2);
  404. INSERT INTO t3 SELECT x+2 FROM t3;
  405. INSERT INTO t3 SELECT x+4 FROM t3;
  406. INSERT INTO t3 SELECT x+8 FROM t3;
  407. COMMIT;
  408. }
  409. set VM1 [sqlite3_prepare $DB {SELECT * FROM t3} -1 TAIL]
  410. sqlite3 db2 test.db
  411. execsql {BEGIN} db2
  412. } {}
  413. # Update for v3: BEGIN doesn't write-lock the database. It is quite
  414. # difficult to get v3 to write-lock the database, which causes a few
  415. # problems for test scripts.
  416. #
  417. # do_test capi2-6.2 {
  418. # list [sqlite3_step $VM1] \
  419. # [sqlite3_column_count $VM1] \
  420. # [get_row_values $VM1] \
  421. # [get_column_names $VM1]
  422. # } {SQLITE_BUSY 0 {} {}}
  423. do_test capi2-6.3 {
  424. execsql {COMMIT} db2
  425. } {}
  426. do_test capi2-6.4 {
  427. list [sqlite3_step $VM1] \
  428. [sqlite3_column_count $VM1] \
  429. [get_row_values $VM1] \
  430. [get_column_names $VM1]
  431. } {SQLITE_ROW 1 1 {x counter}}
  432. do_test capi2-6.5 {
  433. catchsql {INSERT INTO t3 VALUES(10);} db2
  434. } {1 {database is locked}}
  435. do_test capi2-6.6 {
  436. list [sqlite3_step $VM1] \
  437. [sqlite3_column_count $VM1] \
  438. [get_row_values $VM1] \
  439. [get_column_names $VM1]
  440. } {SQLITE_ROW 1 2 {x counter}}
  441. do_test capi2-6.7 {
  442. execsql {SELECT * FROM t2} db2
  443. } {2 3 3 4 1 2}
  444. do_test capi2-6.8 {
  445. list [sqlite3_step $VM1] \
  446. [sqlite3_column_count $VM1] \
  447. [get_row_values $VM1] \
  448. [get_column_names $VM1]
  449. } {SQLITE_ROW 1 3 {x counter}}
  450. do_test capi2-6.9 {
  451. execsql {SELECT * FROM t2}
  452. } {2 3 3 4 1 2}
  453. do_test capi2-6.10 {
  454. list [sqlite3_step $VM1] \
  455. [sqlite3_column_count $VM1] \
  456. [get_row_values $VM1] \
  457. [get_column_names $VM1]
  458. } {SQLITE_ROW 1 4 {x counter}}
  459. do_test capi2-6.11 {
  460. execsql {BEGIN}
  461. } {}
  462. do_test capi2-6.12 {
  463. list [sqlite3_step $VM1] \
  464. [sqlite3_column_count $VM1] \
  465. [get_row_values $VM1] \
  466. [get_column_names $VM1]
  467. } {SQLITE_ROW 1 5 {x counter}}
  468. # A read no longer blocks a write in the same connection.
  469. #do_test capi2-6.13 {
  470. # catchsql {UPDATE t3 SET x=x+1}
  471. #} {1 {database table is locked}}
  472. do_test capi2-6.14 {
  473. list [sqlite3_step $VM1] \
  474. [sqlite3_column_count $VM1] \
  475. [get_row_values $VM1] \
  476. [get_column_names $VM1]
  477. } {SQLITE_ROW 1 6 {x counter}}
  478. do_test capi2-6.15 {
  479. execsql {SELECT * FROM t1}
  480. } {1 2 3}
  481. do_test capi2-6.16 {
  482. list [sqlite3_step $VM1] \
  483. [sqlite3_column_count $VM1] \
  484. [get_row_values $VM1] \
  485. [get_column_names $VM1]
  486. } {SQLITE_ROW 1 7 {x counter}}
  487. do_test capi2-6.17 {
  488. catchsql {UPDATE t1 SET b=b+1}
  489. } {0 {}}
  490. do_test capi2-6.18 {
  491. list [sqlite3_step $VM1] \
  492. [sqlite3_column_count $VM1] \
  493. [get_row_values $VM1] \
  494. [get_column_names $VM1]
  495. } {SQLITE_ROW 1 8 {x counter}}
  496. do_test capi2-6.19 {
  497. execsql {SELECT * FROM t1}
  498. } {1 3 3}
  499. do_test capi2-6.20 {
  500. list [sqlite3_step $VM1] \
  501. [sqlite3_column_count $VM1] \
  502. [get_row_values $VM1] \
  503. [get_column_names $VM1]
  504. } {SQLITE_ROW 1 9 {x counter}}
  505. #do_test capi2-6.21 {
  506. # execsql {ROLLBACK; SELECT * FROM t1}
  507. #} {1 2 3}
  508. do_test capi2-6.22 {
  509. list [sqlite3_step $VM1] \
  510. [sqlite3_column_count $VM1] \
  511. [get_row_values $VM1] \
  512. [get_column_names $VM1]
  513. } {SQLITE_ROW 1 10 {x counter}}
  514. #do_test capi2-6.23 {
  515. # execsql {BEGIN TRANSACTION;}
  516. #} {}
  517. do_test capi2-6.24 {
  518. list [sqlite3_step $VM1] \
  519. [sqlite3_column_count $VM1] \
  520. [get_row_values $VM1] \
  521. [get_column_names $VM1]
  522. } {SQLITE_ROW 1 11 {x counter}}
  523. do_test capi2-6.25 {
  524. execsql {
  525. INSERT INTO t1 VALUES(2,3,4);
  526. SELECT * FROM t1;
  527. }
  528. } {1 3 3 2 3 4}
  529. do_test capi2-6.26 {
  530. list [sqlite3_step $VM1] \
  531. [sqlite3_column_count $VM1] \
  532. [get_row_values $VM1] \
  533. [get_column_names $VM1]
  534. } {SQLITE_ROW 1 12 {x counter}}
  535. do_test capi2-6.27 {
  536. catchsql {
  537. INSERT INTO t1 VALUES(2,4,5);
  538. SELECT * FROM t1;
  539. }
  540. } {1 {column a is not unique}}
  541. do_test capi2-6.28 {
  542. list [sqlite3_step $VM1] \
  543. [sqlite3_column_count $VM1] \
  544. [get_row_values $VM1] \
  545. [get_column_names $VM1]
  546. } {SQLITE_ROW 1 13 {x counter}}
  547. do_test capi2-6.99 {
  548. sqlite3_finalize $VM1
  549. } {SQLITE_OK}
  550. catchsql {ROLLBACK}
  551. do_test capi2-7.1 {
  552. stepsql $DB {
  553. SELECT * FROM t1
  554. }
  555. } {0 1 2 3}
  556. do_test capi2-7.2 {
  557. stepsql $DB {
  558. PRAGMA count_changes=on
  559. }
  560. } {0}
  561. do_test capi2-7.3 {
  562. stepsql $DB {
  563. UPDATE t1 SET a=a+10;
  564. }
  565. } {0 1}
  566. do_test capi2-7.4 {
  567. stepsql $DB {
  568. INSERT INTO t1 SELECT a+1,b+1,c+1 FROM t1;
  569. }
  570. } {0 1}
  571. do_test capi2-7.4b {sqlite3_changes $DB} {1}
  572. do_test capi2-7.5 {
  573. stepsql $DB {
  574. UPDATE t1 SET a=a+10;
  575. }
  576. } {0 2}
  577. do_test capi2-7.5b {sqlite3_changes $DB} {2}
  578. do_test capi2-7.6 {
  579. stepsql $DB {
  580. SELECT * FROM t1;
  581. }
  582. } {0 21 2 3 22 3 4}
  583. do_test capi2-7.7 {
  584. stepsql $DB {
  585. INSERT INTO t1 SELECT a+2,b+2,c+2 FROM t1;
  586. }
  587. } {0 2}
  588. do_test capi2-7.8 {
  589. sqlite3_changes $DB
  590. } {2}
  591. do_test capi2-7.9 {
  592. stepsql $DB {
  593. SELECT * FROM t1;
  594. }
  595. } {0 21 2 3 22 3 4 23 4 5 24 5 6}
  596. do_test capi2-7.10 {
  597. stepsql $DB {
  598. UPDATE t1 SET a=a-20;
  599. SELECT * FROM t1;
  600. }
  601. } {0 4 1 2 3 2 3 4 3 4 5 4 5 6}
  602. # Update for version 3: A SELECT statement no longer resets the change
  603. # counter (Test result changes from 0 to 4).
  604. do_test capi2-7.11 {
  605. sqlite3_changes $DB
  606. } {4}
  607. do_test capi2-7.11a {
  608. execsql {SELECT count(*) FROM t1}
  609. } {4}
  610. ifcapable {explain} {
  611. do_test capi2-7.12 {
  612. set x [stepsql $DB {EXPLAIN SELECT * FROM t1}]
  613. lindex $x 0
  614. } {0}
  615. }
  616. # Ticket #261 - make sure we can finalize before the end of a query.
  617. #
  618. do_test capi2-8.1 {
  619. set VM1 [sqlite3_prepare $DB {SELECT * FROM t2} -1 TAIL]
  620. sqlite3_finalize $VM1
  621. } {SQLITE_OK}
  622. # Tickets #384 and #385 - make sure the TAIL argument to sqlite3_prepare
  623. # and all of the return pointers in sqlite_step can be null.
  624. #
  625. do_test capi2-9.1 {
  626. set VM1 [sqlite3_prepare $DB {SELECT * FROM t2} -1 DUMMY]
  627. sqlite3_step $VM1
  628. sqlite3_finalize $VM1
  629. } {SQLITE_OK}
  630. # Test that passing a NULL pointer to sqlite3_finalize() or sqlite3_reset
  631. # does not cause an error.
  632. do_test capi2-10.1 {
  633. sqlite3_finalize 0
  634. } {SQLITE_OK}
  635. do_test capi2-10.2 {
  636. sqlite3_reset 0
  637. } {SQLITE_OK}
  638. #---------------------------------------------------------------------------
  639. # The following tests - capi2-11.* - test the "column origin" APIs.
  640. #
  641. # sqlite3_column_origin_name()
  642. # sqlite3_column_database_name()
  643. # sqlite3_column_table_name()
  644. #
  645. ifcapable columnmetadata {
  646. # This proc uses the database handle $::DB to compile the SQL statement passed
  647. # as a parameter. The return value of this procedure is a list with one
  648. # element for each column returned by the compiled statement. Each element of
  649. # this list is itself a list of length three, consisting of the origin
  650. # database, table and column for the corresponding returned column.
  651. proc check_origins {sql} {
  652. set ret [list]
  653. set ::STMT [sqlite3_prepare $::DB $sql -1 dummy]
  654. for {set i 0} {$i < [sqlite3_column_count $::STMT]} {incr i} {
  655. lappend ret [list \
  656. [sqlite3_column_database_name $::STMT $i] \
  657. [sqlite3_column_table_name $::STMT $i] \
  658. [sqlite3_column_origin_name $::STMT $i] \
  659. ]
  660. }
  661. sqlite3_finalize $::STMT
  662. return $ret
  663. }
  664. do_test capi2-11.1 {
  665. execsql {
  666. CREATE TABLE tab1(col1, col2);
  667. }
  668. } {}
  669. do_test capi2-11.2 {
  670. check_origins {SELECT col2, col1 FROM tab1}
  671. } [list {main tab1 col2} {main tab1 col1}]
  672. do_test capi2-11.3 {
  673. check_origins {SELECT col2 AS hello, col1 AS world FROM tab1}
  674. } [list {main tab1 col2} {main tab1 col1}]
  675. ifcapable subquery {
  676. do_test capi2-11.4 {
  677. check_origins {SELECT b, a FROM (SELECT col1 AS a, col2 AS b FROM tab1)}
  678. } [list {main tab1 col2} {main tab1 col1}]
  679. do_test capi2-11.5 {
  680. check_origins {SELECT (SELECT col2 FROM tab1), (SELECT col1 FROM tab1)}
  681. } [list {main tab1 col2} {main tab1 col1}]
  682. do_test capi2-11.6 {
  683. check_origins {SELECT (SELECT col2), (SELECT col1) FROM tab1}
  684. } [list {main tab1 col2} {main tab1 col1}]
  685. do_test capi2-11.7 {
  686. check_origins {SELECT * FROM tab1}
  687. } [list {main tab1 col1} {main tab1 col2}]
  688. do_test capi2-11.8 {
  689. check_origins {SELECT * FROM (SELECT * FROM tab1)}
  690. } [list {main tab1 col1} {main tab1 col2}]
  691. }
  692. ifcapable view&&subquery {
  693. do_test capi2-12.1 {
  694. execsql {
  695. CREATE VIEW view1 AS SELECT * FROM tab1;
  696. }
  697. } {}
  698. do_test capi2-12.2 {
  699. check_origins {SELECT col2, col1 FROM view1}
  700. } [list {main tab1 col2} {main tab1 col1}]
  701. do_test capi2-12.3 {
  702. check_origins {SELECT col2 AS hello, col1 AS world FROM view1}
  703. } [list {main tab1 col2} {main tab1 col1}]
  704. do_test capi2-12.4 {
  705. check_origins {SELECT b, a FROM (SELECT col1 AS a, col2 AS b FROM view1)}
  706. } [list {main tab1 col2} {main tab1 col1}]
  707. do_test capi2-12.5 {
  708. check_origins {SELECT (SELECT col2 FROM view1), (SELECT col1 FROM view1)}
  709. } [list {main tab1 col2} {main tab1 col1}]
  710. do_test capi2-12.6 {
  711. check_origins {SELECT (SELECT col2), (SELECT col1) FROM view1}
  712. } [list {main tab1 col2} {main tab1 col1}]
  713. do_test capi2-12.7 {
  714. check_origins {SELECT * FROM view1}
  715. } [list {main tab1 col1} {main tab1 col2}]
  716. do_test capi2-12.8 {
  717. check_origins {select * from (select * from view1)}
  718. } [list {main tab1 col1} {main tab1 col2}]
  719. do_test capi2-12.9 {
  720. check_origins {select * from (select * from (select * from view1))}
  721. } [list {main tab1 col1} {main tab1 col2}]
  722. do_test capi2-12.10 {
  723. db close
  724. sqlite3 db test.db
  725. set ::DB [sqlite3_connection_pointer db]
  726. check_origins {select * from (select * from (select * from view1))}
  727. } [list {main tab1 col1} {main tab1 col2}]
  728. # This view will thwart the flattening optimization.
  729. do_test capi2-13.1 {
  730. execsql {
  731. CREATE VIEW view2 AS SELECT * FROM tab1 limit 10 offset 10;
  732. }
  733. } {}
  734. do_test capi2-13.2 {
  735. check_origins {SELECT col2, col1 FROM view2}
  736. } [list {main tab1 col2} {main tab1 col1}]
  737. do_test capi2-13.3 {
  738. check_origins {SELECT col2 AS hello, col1 AS world FROM view2}
  739. } [list {main tab1 col2} {main tab1 col1}]
  740. do_test capi2-13.4 {
  741. check_origins {SELECT b, a FROM (SELECT col1 AS a, col2 AS b FROM view2)}
  742. } [list {main tab1 col2} {main tab1 col1}]
  743. do_test capi2-13.5 {
  744. check_origins {SELECT (SELECT col2 FROM view2), (SELECT col1 FROM view2)}
  745. } [list {main tab1 col2} {main tab1 col1}]
  746. do_test capi2-13.6 {
  747. check_origins {SELECT (SELECT col2), (SELECT col1) FROM view2}
  748. } [list {main tab1 col2} {main tab1 col1}]
  749. do_test capi2-13.7 {
  750. check_origins {SELECT * FROM view2}
  751. } [list {main tab1 col1} {main tab1 col2}]
  752. do_test capi2-13.8 {
  753. check_origins {select * from (select * from view2)}
  754. } [list {main tab1 col1} {main tab1 col2}]
  755. do_test capi2-13.9 {
  756. check_origins {select * from (select * from (select * from view2))}
  757. } [list {main tab1 col1} {main tab1 col2}]
  758. do_test capi2-13.10 {
  759. db close
  760. sqlite3 db test.db
  761. set ::DB [sqlite3_connection_pointer db]
  762. check_origins {select * from (select * from (select * from view2))}
  763. } [list {main tab1 col1} {main tab1 col2}]
  764. do_test capi2-13.11 {
  765. check_origins {select * from (select * from tab1 limit 10 offset 10)}
  766. } [list {main tab1 col1} {main tab1 col2}]
  767. }
  768. } ;# ifcapable columnmetadata
  769. db2 close
  770. finish_test