Files
JetCore/tests/testbind.jet
T
brad ArantandClaude Opus 5.5 a18d1358b0 Add <bind> bound parameters for <sql> (phase 1)
Implements phase 1 of docs/design-sql-bound-parameters.md: <bind> supplies
the value for the next ? placeholder of the enclosing <sql>, with typed
validation (int, decimal, date, datetime, bool, ident with allow list),
null="empty", and refusal to change the connection character set.

- SqlBind.*: MySQL-free placeholder scanning and binding logic
- __bind.*, __sql, __mysql: runtime wiring
- Tag.cpp, jetc/JetParser.cpp: bind is a void tag in both
- jetc/JetCodegen: emit sized ZString literals for text holding a NUL,
  which previously truncated the compiled page
- tests: unit tests, end-to-end test against MySQL (interpreted and
  jetc-compiled), and the generator for the binary test page

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 19:55:23 -08:00

155 lines
8.2 KiB
Plaintext

#!../jet-2.0
<jet filterblanklines="true" trimlines="true">
<comment>
End-to-end tests for <sql>/<bind> - see docs/design-sql-bound-parameters.md.
Run by tests/runbindtests.sh against a real MySQL server; only TEMPORARY
tables are used, so nothing outlives the connection. Compared byte-for-byte
with tests/testbind.expected. Generated - contains raw 0xBF and NUL bytes.
</comment>
<mysql host="$[&JETTEST_DB_HOST]" database="$[&JETTEST_DB_NAME]" user="$[&JETTEST_DB_USER]" password="$[&JETTEST_DB_PASSWORD]">
<sql>create temporary table bindtest (id int auto_increment primary key, s varbinary(255), n int, dec_v decimal(10,2), d date, dt datetime, b tinyint, t varchar(100) character set utf8mb4)</sql>
<comment> Injection strings and odd bytes come back byte-for-byte. </comment>
<set name="v" value="hello">
<sql>insert into bindtest (s) values (?)<bind value="$[v]"></sql>
<sql>select hex(s) as h from bindtest where id=last_insert_id()</sql>
default plain: <ifrow>$[.h]</ifrow>
<set name="v" value="x' or '1'='1">
<sql>insert into bindtest (s) values (?)<bind value="$[v]"></sql>
<sql>select hex(s) as h from bindtest where id=last_insert_id()</sql>
default or-injection: <ifrow>$[.h]</ifrow>
<set name="v" value="\'">
<sql>insert into bindtest (s) values (?)<bind value="$[v]"></sql>
<sql>select hex(s) as h from bindtest where id=last_insert_id()</sql>
default backslash-quote: <ifrow>$[.h]</ifrow>
<sql>select concat('a', char(92)) as v</sql><ifrow><set name="v" value="$[.v]"></ifrow>
<sql>insert into bindtest (s) values (?)<bind value="$[v]"></sql>
<sql>select hex(s) as h from bindtest where id=last_insert_id()</sql>
default trailing-backslash: <ifrow>$[.h]</ifrow>
<set name="v" value="-- ">
<sql>insert into bindtest (s) values (?)<bind value="$[v]"></sql>
<sql>select hex(s) as h from bindtest where id=last_insert_id()</sql>
default dashdash: <ifrow>$[.h]</ifrow>
<set name="v" value="; drop table t; --">
<sql>insert into bindtest (s) values (?)<bind value="$[v]"></sql>
<sql>select hex(s) as h from bindtest where id=last_insert_id()</sql>
default semicolon: <ifrow>$[.h]</ifrow>
<set name="v" value="¿' or 1=1 -- ">
<sql>insert into bindtest (s) values (?)<bind value="$[v]"></sql>
<sql>select hex(s) as h from bindtest where id=last_insert_id()</sql>
default gbk-bf27: <ifrow>$[.h]</ifrow>
<set name="v" value='say "hi"'>
<sql>insert into bindtest (s) values (?)<bind value="$[v]"></sql>
<sql>select hex(s) as h from bindtest where id=last_insert_id()</sql>
default double-quote: <ifrow>$[.h]</ifrow>
<set name="v" value="what?">
<sql>insert into bindtest (s) values (?)<bind value="$[v]"></sql>
<sql>select hex(s) as h from bindtest where id=last_insert_id()</sql>
default question: <ifrow>$[.h]</ifrow>
<set name="v" value="ab">
<sql>insert into bindtest (s) values (?)<bind value="$[v]"></sql>
<sql>select hex(s) as h from bindtest where id=last_insert_id()</sql>
default nul: <ifrow>$[.h]</ifrow>
<set name="v" value="héllo – ✓ 🎹">
<sql>insert into bindtest (s) values (?)<bind value="$[v]"></sql>
<sql>select hex(s) as h from bindtest where id=last_insert_id()</sql>
default utf8: <ifrow>$[.h]</ifrow>
<sql>set session sql_mode = concat(@@sql_mode, ',NO_BACKSLASH_ESCAPES')</sql>
<set name="v" value="hello">
<sql>insert into bindtest (s) values (?)<bind value="$[v]"></sql>
<sql>select hex(s) as h from bindtest where id=last_insert_id()</sql>
nbe plain: <ifrow>$[.h]</ifrow>
<set name="v" value="x' or '1'='1">
<sql>insert into bindtest (s) values (?)<bind value="$[v]"></sql>
<sql>select hex(s) as h from bindtest where id=last_insert_id()</sql>
nbe or-injection: <ifrow>$[.h]</ifrow>
<set name="v" value="\'">
<sql>insert into bindtest (s) values (?)<bind value="$[v]"></sql>
<sql>select hex(s) as h from bindtest where id=last_insert_id()</sql>
nbe backslash-quote: <ifrow>$[.h]</ifrow>
<sql>select concat('a', char(92)) as v</sql><ifrow><set name="v" value="$[.v]"></ifrow>
<sql>insert into bindtest (s) values (?)<bind value="$[v]"></sql>
<sql>select hex(s) as h from bindtest where id=last_insert_id()</sql>
nbe trailing-backslash: <ifrow>$[.h]</ifrow>
<set name="v" value="-- ">
<sql>insert into bindtest (s) values (?)<bind value="$[v]"></sql>
<sql>select hex(s) as h from bindtest where id=last_insert_id()</sql>
nbe dashdash: <ifrow>$[.h]</ifrow>
<set name="v" value="; drop table t; --">
<sql>insert into bindtest (s) values (?)<bind value="$[v]"></sql>
<sql>select hex(s) as h from bindtest where id=last_insert_id()</sql>
nbe semicolon: <ifrow>$[.h]</ifrow>
<set name="v" value="¿' or 1=1 -- ">
<sql>insert into bindtest (s) values (?)<bind value="$[v]"></sql>
<sql>select hex(s) as h from bindtest where id=last_insert_id()</sql>
nbe gbk-bf27: <ifrow>$[.h]</ifrow>
<set name="v" value='say "hi"'>
<sql>insert into bindtest (s) values (?)<bind value="$[v]"></sql>
<sql>select hex(s) as h from bindtest where id=last_insert_id()</sql>
nbe double-quote: <ifrow>$[.h]</ifrow>
<set name="v" value="what?">
<sql>insert into bindtest (s) values (?)<bind value="$[v]"></sql>
<sql>select hex(s) as h from bindtest where id=last_insert_id()</sql>
nbe question: <ifrow>$[.h]</ifrow>
<set name="v" value="ab">
<sql>insert into bindtest (s) values (?)<bind value="$[v]"></sql>
<sql>select hex(s) as h from bindtest where id=last_insert_id()</sql>
nbe nul: <ifrow>$[.h]</ifrow>
<set name="v" value="héllo – ✓ 🎹">
<sql>insert into bindtest (s) values (?)<bind value="$[v]"></sql>
<sql>select hex(s) as h from bindtest where id=last_insert_id()</sql>
nbe utf8: <ifrow>$[.h]</ifrow>
<sql>set session sql_mode = replace(@@sql_mode, 'NO_BACKSLASH_ESCAPES', '')</sql>
<comment> Every type. </comment>
<set name="num" value="42">
<sql>insert into bindtest (n, dec_v, d, dt, b, t) values (?, ?, ?, ?, ?, ?)
<bind value="$[num]" type="int">
<bind value="-12.50" type="decimal">
<bind value="2026-10-23" type="date">
<bind value="2026-10-23 14:05" type="datetime">
<bind value="yes" type="bool">
<bind value="" null="empty">
</sql>
<sql>select n, dec_v, d, dt, b, t is null as tnull from bindtest where id=last_insert_id()</sql>
types: <ifrow>$[.n] $[.dec_v] $[.d] $[.dt] $[.b] $[.tnull]</ifrow>
<comment> ? inside literals, identifiers and comments is left alone. </comment>
<sql>select '?' as a, "?" as `b?`, ? as c /* ? */ -- ?
<bind value="bound">
</sql>
literals: <ifrow>$[.a] $[.b?] $[.c]</ifrow>
<comment> A query with no bind runs unchanged, even with a literal ?. </comment>
<sql>select concat('what', '?') as q</sql>
nobind: <ifrow>$[.q]</ifrow>
<comment> bind inside if inside sql. </comment>
<set name="flag" value="1">
<sql>select ? as x
<if value1="$[flag]" value2="1" type="eq"><bind value="from-if"></if>
</sql>
nested: <ifrow>$[.x]</ifrow>
<comment> ident with allow, order by. </comment>
<sql>insert into bindtest (n) values (5), (3), (9)</sql>
<set name="sort" value="N">
<set name="dir" value="desc">
<sql>select n from bindtest where n in (?, ?, ?) order by ? ?
<bind value="3" type="int"><bind value="5" type="int"><bind value="9" type="int">
<bind value="$[sort]" type="ident" allow="n,s">
<bind value="$[dir]" type="ident" allow="ASC,DESC">
</sql>
ident: <whilerow name="r">$[.n] </whilerow>
<comment> Result access on a bound query: whilerow, column names and counts. </comment>
<sql>select n, s from bindtest where n > ? order by n<bind value="4" type="int"></sql>
rows: $[.#] columns: $[.?#] first column: $[.?1] second column: $[.?2]
<comment> Errors - all catchable, none echo the value. </comment>
<try><sql>select ?, ?, ?<bind value="a"><bind value="b"></sql><catch name="err">mismatch: $[err]</try>
<try><sql>select ?<bind value="1 or 1=1" type="int"></sql><catch name="err">badint: $[err]</try>
<try><sql>select ?<bind value="s3cret" type="date"></sql><catch name="err">baddate: $[err]</try>
<try><sql>select ? from bindtest<bind value="password" type="ident" allow="n,s"></sql><catch name="err">badident: $[err]</try>
<try><bind value="x"><catch name="err">outside: $[err]</try>
<try><sql>select 1<bind></sql><catch name="err">novalue: $[err]</try>
<try><sql>select ?<bind value="x" type="blob"></sql><catch name="err">badtype: $[err]</try>
<try><sql>SET NAMES latin1</sql><catch name="err">setnames: $[err]</try>
<try><sql>set character_set_client = gbk</sql><catch name="err">setcharset: $[err]</try>
<sql>select @@character_set_client as cs</sql>
charset: <ifrow>$[.cs]</ifrow>
</mysql>
</jet>