bind.test 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. # 2003 September 6
  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 sqlite_bind API.
  13. #
  14. # $Id: bind.test,v 1.48 2009/07/22 07:27:57 danielk1977 Exp $
  15. #
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. proc sqlite_step {stmt N VALS COLS} {
  19. upvar VALS vals
  20. upvar COLS cols
  21. set vals [list]
  22. set cols [list]
  23. set rc [sqlite3_step $stmt]
  24. for {set i 0} {$i < [sqlite3_column_count $stmt]} {incr i} {
  25. lappend cols [sqlite3_column_name $stmt $i]
  26. }
  27. for {set i 0} {$i < [sqlite3_data_count $stmt]} {incr i} {
  28. lappend vals [sqlite3_column_text $stmt $i]
  29. }
  30. return $rc
  31. }
  32. do_test bind-1.1 {
  33. set DB [sqlite3_connection_pointer db]
  34. execsql {CREATE TABLE t1(a,b,c);}
  35. set VM [sqlite3_prepare $DB {INSERT INTO t1 VALUES(:1,?,:abc)} -1 TAIL]
  36. set TAIL
  37. } {}
  38. do_test bind-1.1.1 {
  39. sqlite3_bind_parameter_count $VM
  40. } 3
  41. do_test bind-1.1.2 {
  42. sqlite3_bind_parameter_name $VM 1
  43. } {:1}
  44. do_test bind-1.1.3 {
  45. sqlite3_bind_parameter_name $VM 2
  46. } {}
  47. do_test bind-1.1.4 {
  48. sqlite3_bind_parameter_name $VM 3
  49. } {:abc}
  50. do_test bind-1.2 {
  51. sqlite_step $VM N VALUES COLNAMES
  52. } {SQLITE_DONE}
  53. do_test bind-1.3 {
  54. execsql {SELECT rowid, * FROM t1}
  55. } {1 {} {} {}}
  56. do_test bind-1.4 {
  57. sqlite3_reset $VM
  58. sqlite_bind $VM 1 {test value 1} normal
  59. sqlite_step $VM N VALUES COLNAMES
  60. } SQLITE_DONE
  61. do_test bind-1.5 {
  62. execsql {SELECT rowid, * FROM t1}
  63. } {1 {} {} {} 2 {test value 1} {} {}}
  64. do_test bind-1.6 {
  65. sqlite3_reset $VM
  66. sqlite_bind $VM 3 {'test value 2'} normal
  67. sqlite_step $VM N VALUES COLNAMES
  68. } SQLITE_DONE
  69. do_test bind-1.7 {
  70. execsql {SELECT rowid, * FROM t1}
  71. } {1 {} {} {} 2 {test value 1} {} {} 3 {test value 1} {} {'test value 2'}}
  72. do_test bind-1.8 {
  73. sqlite3_reset $VM
  74. set sqlite_static_bind_value 123
  75. sqlite_bind $VM 1 {} static
  76. sqlite_bind $VM 2 {abcdefg} normal
  77. sqlite_bind $VM 3 {} null
  78. execsql {DELETE FROM t1}
  79. sqlite_step $VM N VALUES COLNAMES
  80. execsql {SELECT rowid, * FROM t1}
  81. } {1 123 abcdefg {}}
  82. do_test bind-1.9 {
  83. sqlite3_reset $VM
  84. sqlite_bind $VM 1 {456} normal
  85. sqlite_step $VM N VALUES COLNAMES
  86. execsql {SELECT rowid, * FROM t1}
  87. } {1 123 abcdefg {} 2 456 abcdefg {}}
  88. do_test bind-1.10 {
  89. set rc [catch {
  90. sqlite3_prepare db {INSERT INTO t1 VALUES($abc:123,?,:abc)} -1 TAIL
  91. } msg]
  92. lappend rc $msg
  93. } {1 {(1) near ":123": syntax error}}
  94. do_test bind-1.11 {
  95. set rc [catch {
  96. sqlite3_prepare db {INSERT INTO t1 VALUES(@abc:xyz,?,:abc)} -1 TAIL
  97. } msg]
  98. lappend rc $msg
  99. } {1 {(1) near ":xyz": syntax error}}
  100. do_test bind-1.99 {
  101. sqlite3_finalize $VM
  102. } SQLITE_OK
  103. # Prepare the statement in different ways depending on whether or not
  104. # the $var processing is compiled into the library.
  105. #
  106. ifcapable {tclvar} {
  107. do_test bind-2.1 {
  108. execsql {
  109. DELETE FROM t1;
  110. }
  111. set VM [sqlite3_prepare $DB {INSERT INTO t1 VALUES($one,$::two,$x(-z-))}\
  112. -1 TX]
  113. set TX
  114. } {}
  115. set v1 {$one}
  116. set v2 {$::two}
  117. set v3 {$x(-z-)}
  118. }
  119. ifcapable {!tclvar} {
  120. do_test bind-2.1 {
  121. execsql {
  122. DELETE FROM t1;
  123. }
  124. set VM [sqlite3_prepare $DB {INSERT INTO t1 VALUES(:one,:two,:_)} -1 TX]
  125. set TX
  126. } {}
  127. set v1 {:one}
  128. set v2 {:two}
  129. set v3 {:_}
  130. }
  131. do_test bind-2.1.1 {
  132. sqlite3_bind_parameter_count $VM
  133. } 3
  134. do_test bind-2.1.2 {
  135. sqlite3_bind_parameter_name $VM 1
  136. } $v1
  137. do_test bind-2.1.3 {
  138. sqlite3_bind_parameter_name $VM 2
  139. } $v2
  140. do_test bind-2.1.4 {
  141. sqlite3_bind_parameter_name $VM 3
  142. } $v3
  143. do_test bind-2.1.5 {
  144. sqlite3_bind_parameter_index $VM $v1
  145. } 1
  146. do_test bind-2.1.6 {
  147. sqlite3_bind_parameter_index $VM $v2
  148. } 2
  149. do_test bind-2.1.7 {
  150. sqlite3_bind_parameter_index $VM $v3
  151. } 3
  152. do_test bind-2.1.8 {
  153. sqlite3_bind_parameter_index $VM {:hi}
  154. } 0
  155. # 32 bit Integers
  156. do_test bind-2.2 {
  157. sqlite3_bind_int $VM 1 123
  158. sqlite3_bind_int $VM 2 456
  159. sqlite3_bind_int $VM 3 789
  160. sqlite_step $VM N VALUES COLNAMES
  161. sqlite3_reset $VM
  162. execsql {SELECT rowid, * FROM t1}
  163. } {1 123 456 789}
  164. do_test bind-2.3 {
  165. sqlite3_bind_int $VM 2 -2000000000
  166. sqlite3_bind_int $VM 3 2000000000
  167. sqlite_step $VM N VALUES COLNAMES
  168. sqlite3_reset $VM
  169. execsql {SELECT rowid, * FROM t1}
  170. } {1 123 456 789 2 123 -2000000000 2000000000}
  171. do_test bind-2.4 {
  172. execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
  173. } {integer integer integer integer integer integer}
  174. do_test bind-2.5 {
  175. execsql {
  176. DELETE FROM t1;
  177. }
  178. } {}
  179. # 64 bit Integers
  180. do_test bind-3.1 {
  181. sqlite3_bind_int64 $VM 1 32
  182. sqlite3_bind_int64 $VM 2 -2000000000000
  183. sqlite3_bind_int64 $VM 3 2000000000000
  184. sqlite_step $VM N VALUES COLNAMES
  185. sqlite3_reset $VM
  186. execsql {SELECT rowid, * FROM t1}
  187. } {1 32 -2000000000000 2000000000000}
  188. do_test bind-3.2 {
  189. execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
  190. } {integer integer integer}
  191. do_test bind-3.3 {
  192. execsql {
  193. DELETE FROM t1;
  194. }
  195. } {}
  196. # Doubles
  197. do_test bind-4.1 {
  198. sqlite3_bind_double $VM 1 1234.1234
  199. sqlite3_bind_double $VM 2 0.00001
  200. sqlite3_bind_double $VM 3 123456789
  201. sqlite_step $VM N VALUES COLNAMES
  202. sqlite3_reset $VM
  203. set x [execsql {SELECT rowid, * FROM t1}]
  204. regsub {1e-005} $x {1e-05} y
  205. set y
  206. } {1 1234.1234 1e-05 123456789.0}
  207. do_test bind-4.2 {
  208. execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
  209. } {real real real}
  210. do_test bind-4.3 {
  211. execsql {
  212. DELETE FROM t1;
  213. }
  214. } {}
  215. do_test bind-4.4 {
  216. sqlite3_bind_double $VM 1 NaN
  217. sqlite3_bind_double $VM 2 1e300
  218. sqlite3_bind_double $VM 3 -1e-300
  219. sqlite_step $VM N VALUES COLNAMES
  220. sqlite3_reset $VM
  221. set x [execsql {SELECT rowid, * FROM t1}]
  222. regsub {1e-005} $x {1e-05} y
  223. set y
  224. } {1 {} 1e+300 -1e-300}
  225. do_test bind-4.5 {
  226. execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
  227. } {null real real}
  228. do_test bind-4.6 {
  229. execsql {
  230. DELETE FROM t1;
  231. }
  232. } {}
  233. # NULL
  234. do_test bind-5.1 {
  235. sqlite3_bind_null $VM 1
  236. sqlite3_bind_null $VM 2
  237. sqlite3_bind_null $VM 3
  238. sqlite_step $VM N VALUES COLNAMES
  239. sqlite3_reset $VM
  240. execsql {SELECT rowid, * FROM t1}
  241. } {1 {} {} {}}
  242. do_test bind-5.2 {
  243. execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
  244. } {null null null}
  245. do_test bind-5.3 {
  246. execsql {
  247. DELETE FROM t1;
  248. }
  249. } {}
  250. # UTF-8 text
  251. do_test bind-6.1 {
  252. sqlite3_bind_text $VM 1 hellothere 5
  253. sqlite3_bind_text $VM 2 ".." 1
  254. sqlite3_bind_text $VM 3 world\000 -1
  255. sqlite_step $VM N VALUES COLNAMES
  256. sqlite3_reset $VM
  257. execsql {SELECT rowid, * FROM t1}
  258. } {1 hello . world}
  259. do_test bind-6.2 {
  260. execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
  261. } {text text text}
  262. do_test bind-6.3 {
  263. execsql {
  264. DELETE FROM t1;
  265. }
  266. } {}
  267. # Make sure zeros in a string work.
  268. #
  269. do_test bind-6.4 {
  270. db eval {DELETE FROM t1}
  271. sqlite3_bind_text $VM 1 hello\000there\000 12
  272. sqlite3_bind_text $VM 2 hello\000there\000 11
  273. sqlite3_bind_text $VM 3 hello\000there\000 -1
  274. sqlite_step $VM N VALUES COLNAMES
  275. sqlite3_reset $VM
  276. execsql {SELECT * FROM t1}
  277. } {hello hello hello}
  278. set enc [db eval {PRAGMA encoding}]
  279. if {$enc=="UTF-8" || $enc==""} {
  280. do_test bind-6.5 {
  281. execsql {SELECT hex(a), hex(b), hex(c) FROM t1}
  282. } {68656C6C6F00746865726500 68656C6C6F007468657265 68656C6C6F}
  283. } elseif {$enc=="UTF-16le"} {
  284. do_test bind-6.5 {
  285. execsql {SELECT hex(a), hex(b), hex(c) FROM t1}
  286. } {680065006C006C006F000000740068006500720065000000 680065006C006C006F00000074006800650072006500 680065006C006C006F00}
  287. } elseif {$enc=="UTF-16be"} {
  288. do_test bind-6.5 {
  289. execsql {SELECT hex(a), hex(b), hex(c) FROM t1}
  290. } {00680065006C006C006F0000007400680065007200650000 00680065006C006C006F000000740068006500720065 00680065006C006C006F}
  291. } else {
  292. do_test bind-6.5 {
  293. set "Unknown database encoding: $::enc"
  294. } {}
  295. }
  296. do_test bind-6.6 {
  297. execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
  298. } {text text text}
  299. do_test bind-6.7 {
  300. execsql {
  301. DELETE FROM t1;
  302. }
  303. } {}
  304. # UTF-16 text
  305. ifcapable {utf16} {
  306. do_test bind-7.1 {
  307. sqlite3_bind_text16 $VM 1 [encoding convertto unicode hellothere] 10
  308. sqlite3_bind_text16 $VM 2 [encoding convertto unicode ""] 0
  309. sqlite3_bind_text16 $VM 3 [encoding convertto unicode world] 10
  310. sqlite_step $VM N VALUES COLNAMES
  311. sqlite3_reset $VM
  312. execsql {SELECT rowid, * FROM t1}
  313. } {1 hello {} world}
  314. do_test bind-7.2 {
  315. execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
  316. } {text text text}
  317. do_test bind-7.3 {
  318. db eval {DELETE FROM t1}
  319. sqlite3_bind_text16 $VM 1 [encoding convertto unicode hi\000yall\000] 16
  320. sqlite3_bind_text16 $VM 2 [encoding convertto unicode hi\000yall\000] 14
  321. sqlite3_bind_text16 $VM 3 [encoding convertto unicode hi\000yall\000] -1
  322. sqlite_step $VM N VALUES COLNAMES
  323. sqlite3_reset $VM
  324. execsql {SELECT * FROM t1}
  325. } {hi hi hi}
  326. if {$enc=="UTF-8"} {
  327. do_test bind-7.4 {
  328. execsql {SELECT hex(a), hex(b), hex(c) FROM t1}
  329. } {68690079616C6C00 68690079616C6C 6869}
  330. } elseif {$enc=="UTF-16le"} {
  331. do_test bind-7.4 {
  332. execsql {SELECT hex(a), hex(b), hex(c) FROM t1}
  333. } {680069000000790061006C006C000000 680069000000790061006C006C00 68006900}
  334. } elseif {$enc=="UTF-16be"} {
  335. do_test bind-7.4 {
  336. execsql {SELECT hex(a), hex(b), hex(c) FROM t1}
  337. } {00680069000000790061006C006C0000 00680069000000790061006C006C 00680069}
  338. }
  339. do_test bind-7.5 {
  340. execsql {SELECT typeof(a), typeof(b), typeof(c) FROM t1}
  341. } {text text text}
  342. }
  343. do_test bind-7.99 {
  344. execsql {DELETE FROM t1;}
  345. } {}
  346. # Test that the 'out of range' error works.
  347. do_test bind-8.1 {
  348. catch { sqlite3_bind_null $VM 0 }
  349. } {1}
  350. do_test bind-8.2 {
  351. sqlite3_errmsg $DB
  352. } {bind or column index out of range}
  353. ifcapable {utf16} {
  354. do_test bind-8.3 {
  355. encoding convertfrom unicode [sqlite3_errmsg16 $DB]
  356. } {bind or column index out of range}
  357. }
  358. do_test bind-8.4 {
  359. sqlite3_bind_null $VM 1
  360. sqlite3_errmsg $DB
  361. } {not an error}
  362. do_test bind-8.5 {
  363. catch { sqlite3_bind_null $VM 4 }
  364. } {1}
  365. do_test bind-8.6 {
  366. sqlite3_errmsg $DB
  367. } {bind or column index out of range}
  368. ifcapable {utf16} {
  369. do_test bind-8.7 {
  370. encoding convertfrom unicode [sqlite3_errmsg16 $DB]
  371. } {bind or column index out of range}
  372. }
  373. do_test bind-8.8 {
  374. catch { sqlite3_bind_blob $VM 0 "abc" 3 }
  375. } {1}
  376. do_test bind-8.9 {
  377. catch { sqlite3_bind_blob $VM 4 "abc" 3 }
  378. } {1}
  379. do_test bind-8.10 {
  380. catch { sqlite3_bind_text $VM 0 "abc" 3 }
  381. } {1}
  382. ifcapable {utf16} {
  383. do_test bind-8.11 {
  384. catch { sqlite3_bind_text16 $VM 4 "abc" 2 }
  385. } {1}
  386. }
  387. do_test bind-8.12 {
  388. catch { sqlite3_bind_int $VM 0 5 }
  389. } {1}
  390. do_test bind-8.13 {
  391. catch { sqlite3_bind_int $VM 4 5 }
  392. } {1}
  393. do_test bind-8.14 {
  394. catch { sqlite3_bind_double $VM 0 5.0 }
  395. } {1}
  396. do_test bind-8.15 {
  397. catch { sqlite3_bind_double $VM 4 6.0 }
  398. } {1}
  399. do_test bind-8.99 {
  400. sqlite3_finalize $VM
  401. } SQLITE_OK
  402. set iMaxVar $SQLITE_MAX_VARIABLE_NUMBER
  403. set zError "(1) variable number must be between ?1 and ?$iMaxVar"
  404. do_test bind-9.1 {
  405. execsql {
  406. CREATE TABLE t2(a,b,c,d,e,f);
  407. }
  408. set rc [catch {
  409. sqlite3_prepare $DB {
  410. INSERT INTO t2(a) VALUES(?0)
  411. } -1 TAIL
  412. } msg]
  413. lappend rc $msg
  414. } [list 1 $zError]
  415. do_test bind-9.2 {
  416. set rc [catch {
  417. sqlite3_prepare $DB "INSERT INTO t2(a) VALUES(?[expr $iMaxVar+1])" -1 TAIL
  418. } msg]
  419. lappend rc $msg
  420. } [list 1 $zError]
  421. do_test bind-9.3.1 {
  422. set VM [
  423. sqlite3_prepare $DB "
  424. INSERT INTO t2(a,b) VALUES(?1,?$iMaxVar)
  425. " -1 TAIL
  426. ]
  427. sqlite3_bind_parameter_count $VM
  428. } $iMaxVar
  429. catch {sqlite3_finalize $VM}
  430. do_test bind-9.3.2 {
  431. set VM [
  432. sqlite3_prepare $DB "
  433. INSERT INTO t2(a,b) VALUES(?2,?[expr $iMaxVar - 1])
  434. " -1 TAIL
  435. ]
  436. sqlite3_bind_parameter_count $VM
  437. } [expr {$iMaxVar - 1}]
  438. catch {sqlite3_finalize $VM}
  439. do_test bind-9.4 {
  440. set VM [
  441. sqlite3_prepare $DB "
  442. INSERT INTO t2(a,b,c,d) VALUES(?1,?[expr $iMaxVar - 2],?,?)
  443. " -1 TAIL
  444. ]
  445. sqlite3_bind_parameter_count $VM
  446. } $iMaxVar
  447. do_test bind-9.5 {
  448. sqlite3_bind_int $VM 1 1
  449. sqlite3_bind_int $VM [expr $iMaxVar - 2] 999
  450. sqlite3_bind_int $VM [expr $iMaxVar - 1] 1000
  451. sqlite3_bind_int $VM $iMaxVar 1001
  452. sqlite3_step $VM
  453. } SQLITE_DONE
  454. do_test bind-9.6 {
  455. sqlite3_finalize $VM
  456. } SQLITE_OK
  457. do_test bind-9.7 {
  458. execsql {SELECT * FROM t2}
  459. } {1 999 1000 1001 {} {}}
  460. ifcapable {tclvar} {
  461. do_test bind-10.1 {
  462. set VM [
  463. sqlite3_prepare $DB {
  464. INSERT INTO t2(a,b,c,d,e,f) VALUES(:abc,$abc,:abc,$ab,$abc,:abc)
  465. } -1 TAIL
  466. ]
  467. sqlite3_bind_parameter_count $VM
  468. } 3
  469. set v1 {$abc}
  470. set v2 {$ab}
  471. }
  472. ifcapable {!tclvar} {
  473. do_test bind-10.1 {
  474. set VM [
  475. sqlite3_prepare $DB {
  476. INSERT INTO t2(a,b,c,d,e,f) VALUES(:abc,:xyz,:abc,:xy,:xyz,:abc)
  477. } -1 TAIL
  478. ]
  479. sqlite3_bind_parameter_count $VM
  480. } 3
  481. set v1 {:xyz}
  482. set v2 {:xy}
  483. }
  484. do_test bind-10.2 {
  485. sqlite3_bind_parameter_index $VM :abc
  486. } 1
  487. do_test bind-10.3 {
  488. sqlite3_bind_parameter_index $VM $v1
  489. } 2
  490. do_test bind-10.4 {
  491. sqlite3_bind_parameter_index $VM $v2
  492. } 3
  493. do_test bind-10.5 {
  494. sqlite3_bind_parameter_name $VM 1
  495. } :abc
  496. do_test bind-10.6 {
  497. sqlite3_bind_parameter_name $VM 2
  498. } $v1
  499. do_test bind-10.7 {
  500. sqlite3_bind_parameter_name $VM 3
  501. } $v2
  502. do_test bind-10.7.1 {
  503. sqlite3_bind_parameter_name 0 1 ;# Ignore if VM is NULL
  504. } {}
  505. do_test bind-10.7.2 {
  506. sqlite3_bind_parameter_name $VM 0 ;# Ignore if index too small
  507. } {}
  508. do_test bind-10.7.3 {
  509. sqlite3_bind_parameter_name $VM 4 ;# Ignore if index is too big
  510. } {}
  511. do_test bind-10.8 {
  512. sqlite3_bind_int $VM 1 1
  513. sqlite3_bind_int $VM 2 2
  514. sqlite3_bind_int $VM 3 3
  515. sqlite3_step $VM
  516. } SQLITE_DONE
  517. do_test bind-10.8.1 {
  518. # Binding attempts after program start should fail
  519. set rc [catch {
  520. sqlite3_bind_int $VM 1 1
  521. } msg]
  522. lappend rc $msg
  523. } {1 {}}
  524. do_test bind-10.9 {
  525. sqlite3_finalize $VM
  526. } SQLITE_OK
  527. do_test bind-10.10 {
  528. execsql {SELECT * FROM t2}
  529. } {1 999 1000 1001 {} {} 1 2 1 3 2 1}
  530. # Ticket #918
  531. #
  532. do_test bind-10.11 {
  533. # catch {sqlite3_finalize $VM}
  534. set VM [
  535. sqlite3_prepare $DB {
  536. INSERT INTO t2(a,b,c,d,e,f) VALUES(:abc,?,?4,:pqr,:abc,?4)
  537. } -1 TAIL
  538. ]
  539. sqlite3_bind_parameter_count $VM
  540. } 5
  541. do_test bind-10.11.1 {
  542. sqlite3_bind_parameter_index 0 :xyz ;# ignore NULL VM arguments
  543. } 0
  544. do_test bind-10.12 {
  545. sqlite3_bind_parameter_index $VM :xyz
  546. } 0
  547. do_test bind-10.13 {
  548. sqlite3_bind_parameter_index $VM {}
  549. } 0
  550. do_test bind-10.14 {
  551. sqlite3_bind_parameter_index $VM :pqr
  552. } 5
  553. do_test bind-10.15 {
  554. sqlite3_bind_parameter_index $VM ?4
  555. } 4
  556. do_test bind-10.16 {
  557. sqlite3_bind_parameter_name $VM 1
  558. } :abc
  559. do_test bind-10.17 {
  560. sqlite3_bind_parameter_name $VM 2
  561. } {}
  562. do_test bind-10.18 {
  563. sqlite3_bind_parameter_name $VM 3
  564. } {}
  565. do_test bind-10.19 {
  566. sqlite3_bind_parameter_name $VM 4
  567. } {?4}
  568. do_test bind-10.20 {
  569. sqlite3_bind_parameter_name $VM 5
  570. } :pqr
  571. catch {sqlite3_finalize $VM}
  572. # Make sure we catch an unterminated "(" in a Tcl-style variable name
  573. #
  574. ifcapable tclvar {
  575. do_test bind-11.1 {
  576. catchsql {SELECT * FROM sqlite_master WHERE name=$abc(123 and sql NOT NULL;}
  577. } {1 {unrecognized token: "$abc(123"}}
  578. }
  579. if {[execsql {pragma encoding}]=="UTF-8"} {
  580. # Test the ability to bind text that contains embedded '\000' characters.
  581. # Make sure we can recover the entire input string.
  582. #
  583. do_test bind-12.1 {
  584. execsql {
  585. CREATE TABLE t3(x BLOB);
  586. }
  587. set VM [sqlite3_prepare $DB {INSERT INTO t3 VALUES(?)} -1 TAIL]
  588. sqlite_bind $VM 1 not-used blob10
  589. sqlite3_step $VM
  590. sqlite3_finalize $VM
  591. execsql {
  592. SELECT typeof(x), length(x), quote(x),
  593. length(cast(x AS BLOB)), quote(cast(x AS BLOB)) FROM t3
  594. }
  595. } {text 3 'abc' 10 X'6162630078797A007071'}
  596. do_test bind-12.2 {
  597. sqlite3_create_function $DB
  598. execsql {
  599. SELECT quote(cast(x_coalesce(x) AS blob)) FROM t3
  600. }
  601. } {X'6162630078797A007071'}
  602. }
  603. # Test the operation of sqlite3_clear_bindings
  604. #
  605. do_test bind-13.1 {
  606. set VM [sqlite3_prepare $DB {SELECT ?,?,?} -1 TAIL]
  607. sqlite3_step $VM
  608. list [sqlite3_column_type $VM 0] [sqlite3_column_type $VM 1] \
  609. [sqlite3_column_type $VM 2]
  610. } {NULL NULL NULL}
  611. do_test bind-13.2 {
  612. sqlite3_reset $VM
  613. sqlite3_bind_int $VM 1 1
  614. sqlite3_bind_int $VM 2 2
  615. sqlite3_bind_int $VM 3 3
  616. sqlite3_step $VM
  617. list [sqlite3_column_type $VM 0] [sqlite3_column_type $VM 1] \
  618. [sqlite3_column_type $VM 2]
  619. } {INTEGER INTEGER INTEGER}
  620. do_test bind-13.3 {
  621. sqlite3_reset $VM
  622. sqlite3_step $VM
  623. list [sqlite3_column_type $VM 0] [sqlite3_column_type $VM 1] \
  624. [sqlite3_column_type $VM 2]
  625. } {INTEGER INTEGER INTEGER}
  626. do_test bind-13.4 {
  627. sqlite3_reset $VM
  628. sqlite3_clear_bindings $VM
  629. sqlite3_step $VM
  630. list [sqlite3_column_type $VM 0] [sqlite3_column_type $VM 1] \
  631. [sqlite3_column_type $VM 2]
  632. } {NULL NULL NULL}
  633. sqlite3_finalize $VM
  634. #--------------------------------------------------------------------
  635. # These tests attempt to reproduce bug #3463.
  636. #
  637. proc param_names {db zSql} {
  638. set ret [list]
  639. set VM [sqlite3_prepare db $zSql -1 TAIL]
  640. for {set ii 1} {$ii <= [sqlite3_bind_parameter_count $VM]} {incr ii} {
  641. lappend ret [sqlite3_bind_parameter_name $VM $ii]
  642. }
  643. sqlite3_finalize $VM
  644. set ret
  645. }
  646. do_test bind-14.1 {
  647. param_names db { SELECT @a, @b }
  648. } {@a @b}
  649. do_test bind-14.2 {
  650. param_names db { SELECT NULL FROM (SELECT NULL) WHERE @a = @b }
  651. } {@a @b}
  652. do_test bind-14.3 {
  653. param_names db { SELECT @a FROM (SELECT NULL) WHERE 1 = @b }
  654. } {@a @b}
  655. do_test bind-14.4 {
  656. param_names db { SELECT @a, @b FROM (SELECT NULL) }
  657. } {@a @b}
  658. #--------------------------------------------------------------------------
  659. # Tests of the OP_Variable opcode where P3>1
  660. #
  661. do_test bind-15.1 {
  662. db eval {CREATE TABLE t4(a,b,c,d,e,f,g,h);}
  663. set VM [sqlite3_prepare db {
  664. INSERT INTO t4(a,b,c,d,f,g,h,e) VALUES(?,?,?,?,?,?,?,?)
  665. } -1 TAIL]
  666. sqlite3_bind_int $VM 1 1
  667. sqlite3_bind_int $VM 2 2
  668. sqlite3_bind_int $VM 3 3
  669. sqlite3_bind_int $VM 4 4
  670. sqlite3_bind_int $VM 5 5
  671. sqlite3_bind_int $VM 6 6
  672. sqlite3_bind_int $VM 7 7
  673. sqlite3_bind_int $VM 8 8
  674. sqlite3_step $VM
  675. sqlite3_finalize $VM
  676. db eval {SELECT * FROM t4}
  677. } {1 2 3 4 8 5 6 7}
  678. do_test bind-15.2 {
  679. db eval {DELETE FROM t4}
  680. set VM [sqlite3_prepare db {
  681. INSERT INTO t4(a,b,c,d,e,f,g,h) VALUES(?,?,?,?,?,?,?,?)
  682. } -1 TAIL]
  683. sqlite3_bind_int $VM 1 1
  684. sqlite3_bind_int $VM 2 2
  685. sqlite3_bind_int $VM 3 3
  686. sqlite3_bind_int $VM 4 4
  687. sqlite3_bind_int $VM 5 5
  688. sqlite3_bind_int $VM 6 6
  689. sqlite3_bind_int $VM 7 7
  690. sqlite3_bind_int $VM 8 8
  691. sqlite3_step $VM
  692. sqlite3_finalize $VM
  693. db eval {SELECT * FROM t4}
  694. } {1 2 3 4 5 6 7 8}
  695. do_test bind-15.3 {
  696. db eval {DELETE FROM t4}
  697. set VM [sqlite3_prepare db {
  698. INSERT INTO t4(h,g,f,e,d,c,b,a) VALUES(?,?,?,?,?,?,?,?)
  699. } -1 TAIL]
  700. sqlite3_bind_int $VM 1 1
  701. sqlite3_bind_int $VM 2 2
  702. sqlite3_bind_int $VM 3 3
  703. sqlite3_bind_int $VM 4 4
  704. sqlite3_bind_int $VM 5 5
  705. sqlite3_bind_int $VM 6 6
  706. sqlite3_bind_int $VM 7 7
  707. sqlite3_bind_int $VM 8 8
  708. sqlite3_step $VM
  709. sqlite3_finalize $VM
  710. db eval {SELECT * FROM t4}
  711. } {8 7 6 5 4 3 2 1}
  712. do_test bind-15.4 {
  713. db eval {DELETE FROM t4}
  714. set VM [sqlite3_prepare db {
  715. INSERT INTO t4(a,b,c,d,e,f,g,h) VALUES(?,?,?,?4,?,?6,?,?)
  716. } -1 TAIL]
  717. sqlite3_bind_int $VM 1 1
  718. sqlite3_bind_int $VM 2 2
  719. sqlite3_bind_int $VM 3 3
  720. sqlite3_bind_int $VM 4 4
  721. sqlite3_bind_int $VM 5 5
  722. sqlite3_bind_int $VM 6 6
  723. sqlite3_bind_int $VM 7 7
  724. sqlite3_bind_int $VM 8 8
  725. sqlite3_step $VM
  726. sqlite3_finalize $VM
  727. db eval {SELECT * FROM t4}
  728. } {1 2 3 4 5 6 7 8}
  729. finish_test