Files
JetCore/__mysql.h
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

64 lines
2.2 KiB
C++

#ifndef ____mysql_h__
#define ____mysql_h__
#include "Tag.h"
#include "ZString.h"
#include "MString.h"
#include <sstream>
#include <string>
#include <unordered_map>
#include <mysql/mysql.h>
namespace jet {
class __mysql : public Tag {
public:
__mysql(coreutils::ZString &in, coreutils::MString &parentOut, Global &global, Tag *parent, Tag *local);
~__mysql();
void query(coreutils::MString query);
void nextRow();
bool hasRow();
coreutils::ZString getColumnValue(coreutils::ZString column);
// Body of a single-quoted SQL string literal for this connection
// (mysql_real_escape_string_quote: uses the connection's character
// set and honours NO_BACKSLASH_ESCAPES). Used by <bind>.
std::string escapeString(const std::string &value);
// True while the session's sql_mode includes NO_BACKSLASH_ESCAPES.
bool noBackslashEscapes();
private:
// Explicitly initialized - result/row/fieldLength were otherwise
// left as garbage stack values until query() first ran. A
// <mysql>...</mysql> block that connects successfully but never
// runs a <sql> query inside it (e.g. just a connectivity check)
// would previously reach ~__mysql()'s mysql_free_result(result)
// with an uninitialized pointer - real undefined behavior, found
// while root-causing a live crash in a related part of this same
// class's constructor.
MYSQL *mysql = NULL;
MYSQL_RES *result = NULL;
MYSQL_ROW row = NULL;
unsigned long *fieldLength = NULL;
unsigned int qFields = 0;
coreutils::MString sessionId;
// Column-name -> row[] index, built once per query() call (see its
// own comment) instead of getColumnValue() re-deriving it via a
// fresh mysql_fetch_field_direct() linear scan on every single
// named lookup - a template reading N columns off one row used to
// pay that scan N times over, even though the schema is fixed for
// the lifetime of one result set.
std::unordered_map<std::string, int> fieldIndex;
coreutils::MString nbrOfRows = "0";
coreutils::MString nbrOfColumns = "0";
};
}
#endif