# 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'\n') w(b'\n' b' End-to-end tests for / - 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'\n') w(b'\n') w(b'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)\n') def section(name, mode): for label, raw, q in values: if len(q) > 1: w(b'select ' + q.encode() + b' as v\n') else: qb = q.encode() w(b'\n') w(b'insert into bindtest (s) values (?)\n') w(b'select hex(s) as h from bindtest where id=last_insert_id()\n') w(('%s %s: $[.h]\n' % (mode, label)).encode()) E.append('%s %s: %s' % (mode, label, raw.hex().upper())) w(b' Injection strings and odd bytes come back byte-for-byte. \n') section("roundtrip", "default") w(b'set session sql_mode = concat(@@sql_mode, \',NO_BACKSLASH_ESCAPES\')\n') section("roundtrip", "nbe") w(b'set session sql_mode = replace(@@sql_mode, \'NO_BACKSLASH_ESCAPES\', \'\')\n') tail = r''' Every type. insert into bindtest (n, dec_v, d, dt, b, t) values (?, ?, ?, ?, ?, ?) select n, dec_v, d, dt, b, t is null as tnull from bindtest where id=last_insert_id() types: $[.n] $[.dec_v] $[.d] $[.dt] $[.b] $[.tnull] ? inside literals, identifiers and comments is left alone. select '?' as a, "?" as `b?`, ? as c /* ? */ -- ? literals: $[.a] $[.b?] $[.c] A query with no bind runs unchanged, even with a literal ?. select concat('what', '?') as q nobind: $[.q] bind inside if inside sql. select ? as x nested: $[.x] ident with allow, order by. insert into bindtest (n) values (5), (3), (9) select n from bindtest where n in (?, ?, ?) order by ? ? ident: $[.n] Result access on a bound query: whilerow, column names and counts. select n, s from bindtest where n > ? order by n rows: $[.#] columns: $[.?#] first column: $[.?1] second column: $[.?2] Errors - all catchable, none echo the value. select ?, ?, ?mismatch: $[err] select ?badint: $[err] select ?baddate: $[err] select ? from bindtestbadident: $[err] outside: $[err] select 1novalue: $[err] select ?badtype: $[err] SET NAMES latin1setnames: $[err] set character_set_client = gbksetcharset: $[err] select @@character_set_client as cs charset: $[.cs] ''' 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")