Files
JetCore/__sql.cpp
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

47 lines
2.1 KiB
C++

#include "__sql.h"
#include "Exception.h"
#include "MString.h"
#include "__mysql.h"
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
namespace jet {
__sql::__sql(coreutils::ZString &in, coreutils::MString &parentOut, Global &global, Tag *parent, Tag *local) : Tag(in, parentOut, global, parent, local) {
output = false;
if(!hasContainer)
throw coreutils::Exception("sql tag must have a container.");
// resolveKeyword("sessionid") already returns the resolved VALUE
// (e.g. "projman") - the query call below used to wrap that value
// back into keywords[...] as if it were a MAP KEY, auto-vivifying
// an empty string and silently running every <sql sessionid="X">
// query (X != "") against the DEFAULT session instead of the one
// actually named - same double-indirection bug already fixed in
// __mysql.cpp's own constructor (see its own comment on this).
// Resolved once into a local so both the existence check and the
// query call agree on the real, resolved session id.
coreutils::MString sessionId = resolveKeyword("sessionid");
if(!global.sessionExists(sessionId))
throw coreutils::Exception("sessionid does not exist.");
processContainer(container);
__mysql *session = global.getSession(sessionId);
// No <bind> inside: the query runs exactly as rendered, with no '?'
// replacement at all, so existing queries containing a literal '?'
// are unaffected. See docs/design-sql-bound-parameters.md.
if(binds.empty()) {
if(isCharsetChange(out.str()))
throw coreutils::Exception("sql: changing the connection character set is not allowed.");
session->query(out);
return;
}
std::string bound = bindPlaceholders(out.str(), binds, session->noBackslashEscapes(),
[session](const std::string &s) { return session->escapeString(s); });
if(isCharsetChange(bound))
throw coreutils::Exception("sql: changing the connection character set is not allowed.");
session->query(coreutils::MString(bound));
}
}