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:
1 parent
9fae14eb96
commit
a18d1358b0
18 files changed
+1103
-9
No files matched your search
Executable
+74
@@ -0,0 +1,74 @@
|
||||
#!/bin/bash
|
||||
# Tests for <sql>/<bind> (docs/design-sql-bound-parameters.md, phase 1):
|
||||
# 1. tests/sqlbind_test.cpp - unit tests for SqlBind.cpp (no MySQL needed)
|
||||
# 2. tests/testbind.jet - end to end through jet-2.0 against a real MySQL
|
||||
# server (TEMPORARY tables only), compared with
|
||||
# tests/testbind.expected
|
||||
# 3. jetc - <bind> is void in the compiler too, and a
|
||||
# compiled page runs the same <sql>/<bind> code
|
||||
#
|
||||
# Build jet-2.0 first (./compile). The MySQL part needs:
|
||||
# JETTEST_DB_USER, JETTEST_DB_PASSWORD (required, or it is skipped)
|
||||
# JETTEST_DB_HOST (default localhost), JETTEST_DB_NAME (default test)
|
||||
set -u
|
||||
T="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
JC="$(cd "$T/.." && pwd)"
|
||||
CU="$JC/../CoreUtils"
|
||||
WORK="$(mktemp -d)"
|
||||
trap 'rm -rf "$WORK"' EXIT
|
||||
fail=0
|
||||
|
||||
echo "== unit tests (SqlBind) =="
|
||||
g++ -g -std=c++23 -I"$CU" -I"$JC" "$T/sqlbind_test.cpp" "$JC/SqlBind.cpp" -L"$CU" -lCoreUtils -o "$WORK/sqlbind_test" || exit 1
|
||||
"$WORK/sqlbind_test" || fail=1
|
||||
|
||||
echo "== end to end (jet-2.0 + MySQL) =="
|
||||
if [ -z "${JETTEST_DB_USER:-}" ] || [ -z "${JETTEST_DB_PASSWORD:-}" ]; then
|
||||
echo "SKIPPED: set JETTEST_DB_USER and JETTEST_DB_PASSWORD"
|
||||
else
|
||||
export JETTEST_DB_HOST="${JETTEST_DB_HOST:-localhost}" JETTEST_DB_NAME="${JETTEST_DB_NAME:-test}"
|
||||
(cd "$T" && "$JC/jet-2.0" testbind.jet) > "$WORK/testbind.out" 2>&1
|
||||
if diff -u "$T/testbind.expected" "$WORK/testbind.out"; then
|
||||
echo "PASS ($(wc -l < "$T/testbind.expected") lines match)"
|
||||
else
|
||||
echo "FAIL"; fail=1
|
||||
fi
|
||||
fi
|
||||
|
||||
echo "== jetc =="
|
||||
g++ -g -std=c++23 -o "$WORK/jetc" "$JC/jetc/jetc.cpp" "$JC/jetc/JetParser.cpp" "$JC/jetc/JetCodegen.cpp" || exit 1
|
||||
# A bare <bind> must not swallow the content after it looking for </bind>.
|
||||
printf 'A<sql>select ?<bind value="$[x]"></sql>B<bind value="y">C\n' > "$WORK/void.jet"
|
||||
"$WORK/jetc" "$WORK/void.jet" "$WORK/void.cpp" >/dev/null || fail=1
|
||||
if grep -q '<sql>select ?<bind value=\\"$\[x\]\\"></sql>' "$WORK/void.cpp" &&
|
||||
grep -q '"<bind value=\\"y\\">"' "$WORK/void.cpp" &&
|
||||
grep -q '"C\\n"' "$WORK/void.cpp"; then
|
||||
echo "PASS (bind is void; sql span intact)"
|
||||
else
|
||||
echo "FAIL: unexpected codegen for bind"; grep -n 'interpretSpan\|out.write' "$WORK/void.cpp"; fail=1
|
||||
fi
|
||||
if [ -n "${JETTEST_DB_USER:-}" ] && [ -n "${JETTEST_DB_PASSWORD:-}" ]; then
|
||||
# The whole end-to-end test again, compiled by jetc instead of interpreted.
|
||||
# Drop the #! line, and the newline after </jet> (the interpreter stops
|
||||
# at </jet>; a compiled page would emit it as text).
|
||||
tail -n +2 "$T/testbind.jet" | head -c -1 > "$WORK/compiled.jet"
|
||||
"$WORK/jetc" "$WORK/compiled.jet" "$WORK/compiled.cpp" >/dev/null || fail=1
|
||||
objs=""
|
||||
for f in "$JC"/*.cpp; do
|
||||
[ "$(basename "$f")" = "jet-2.0.cpp" ] && continue
|
||||
o="$WORK/$(basename "${f%.cpp}").o"
|
||||
g++ -g -c -std=c++23 -I"$CU" -I"$JC" "$f" -o "$o" &
|
||||
objs="$objs $o"
|
||||
done
|
||||
wait
|
||||
g++ -g -std=c++23 -I"$CU" -I"$JC" "$WORK/compiled.cpp" $objs -L"$CU" -lCoreUtils -lmysqlclient -lssl -lcrypto -o "$WORK/compiled" || exit 1
|
||||
(cd "$T" && "$WORK/compiled") > "$WORK/compiled.out" 2>&1
|
||||
if diff -u "$T/testbind.expected" "$WORK/compiled.out"; then
|
||||
echo "PASS (compiled page output matches)"
|
||||
else
|
||||
echo "FAIL (compiled page)"; fail=1
|
||||
fi
|
||||
fi
|
||||
|
||||
[ $fail = 0 ] && echo "ALL PASSED" || echo "FAILURES"
|
||||
exit $fail
|
||||
Reference in new issue
Block a user