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