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>
This commit is contained in:
brad ArantandClaude Opus 5.5 committed 2026-11-20 19:55:23 -08:00
1 parent 9fae14eb96
commit a18d1358b0
18 files changed
+1103 -9

No files matched your search

+128
View File
@@ -0,0 +1,128 @@
# Generates tests/testbind.jet and tests/testbind.expected (the .jet holds raw
# 0xBF and NUL bytes, so edit this rather than the .jet). Run from tests/:
# python3 gentestbind.py testbind.jet testbind.expected
import sys
out_jet, out_exp = sys.argv[1], sys.argv[2]
# (label, raw bytes, attribute quote)
values = [
("plain", b"hello", '"'),
("or-injection", b"x' or '1'='1", '"'),
("backslash-quote", b"\\'", '"'),
# A jet attribute can't end in a backslash (CoreUtils getTokenExclude
# reads it as escaping the closing quote), so this one is built in SQL.
("trailing-backslash", b"a\\", "concat('a', char(92))"),
("dashdash", b"-- ", '"'),
("semicolon", b"; drop table t; --", '"'),
("gbk-bf27", b"\xbf' or 1=1 -- ", '"'),
("double-quote", b'say "hi"', "'"),
("question", b"what?", '"'),
("nul", b"a\x00b", '"'),
("utf8", "héllo – ✓ 🎹".encode(), '"'),
]
J = []
E = []
w = J.append
w(b'#!../jet-2.0\n')
w(b'<jet filterblanklines="true" trimlines="true">\n')
w(b'<comment>\n'
b' End-to-end tests for <sql>/<bind> - see docs/design-sql-bound-parameters.md.\n'
b' Run by tests/runbindtests.sh against a real MySQL server; only TEMPORARY\n'
b' tables are used, so nothing outlives the connection. Compared byte-for-byte\n'
b' with tests/testbind.expected. Generated - contains raw 0xBF and NUL bytes.\n'
b'</comment>\n')
w(b'<mysql host="$[&JETTEST_DB_HOST]" database="$[&JETTEST_DB_NAME]" user="$[&JETTEST_DB_USER]" password="$[&JETTEST_DB_PASSWORD]">\n')
w(b'<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>\n')
def section(name, mode):
for label, raw, q in values:
if len(q) > 1:
w(b'<sql>select ' + q.encode() + b' as v</sql><ifrow><set name="v" value="$[.v]"></ifrow>\n')
else:
qb = q.encode()
w(b'<set name="v" value=' + qb + raw + qb + b'>\n')
w(b'<sql>insert into bindtest (s) values (?)<bind value="$[v]"></sql>\n')
w(b'<sql>select hex(s) as h from bindtest where id=last_insert_id()</sql>\n')
w(('%s %s: <ifrow>$[.h]</ifrow>\n' % (mode, label)).encode())
E.append('%s %s: %s' % (mode, label, raw.hex().upper()))
w(b'<comment> Injection strings and odd bytes come back byte-for-byte. </comment>\n')
section("roundtrip", "default")
w(b'<sql>set session sql_mode = concat(@@sql_mode, \',NO_BACKSLASH_ESCAPES\')</sql>\n')
section("roundtrip", "nbe")
w(b'<sql>set session sql_mode = replace(@@sql_mode, \'NO_BACKSLASH_ESCAPES\', \'\')</sql>\n')
tail = r'''<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>
'''
w(tail.encode())
E += [
"types: 42 -12.50 2026-10-23 2026-10-23 14:05:00 1 1",
"literals: ? ? bound",
"nobind: what?",
"nested: from-if",
"ident: 9 5 3",
"rows: 3 columns: 2 first column: n second column: s",
"mismatch: sql: 3 placeholders but 2 bind values",
"badint: bind 1: value is not a valid int.",
"baddate: bind 1: value is not a valid date (YYYY-MM-DD).",
"badident: bind 1: value is not in the allow list.",
"outside: bind must be inside sql.",
"novalue: bind must have a value.",
'badtype: bind 1: unknown type "blob".',
"setnames: sql: changing the connection character set is not allowed.",
"setcharset: sql: changing the connection character set is not allowed.",
"charset: utf8mb4",
]
open(out_jet, "wb").write(b"".join(J))
open(out_exp, "w").write("\n".join(E) + "\n")