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>
65 lines
2.8 KiB
C++
65 lines
2.8 KiB
C++
#ifndef __SqlBind_h__
|
|
#define __SqlBind_h__
|
|
|
|
#include <functional>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
// Bound parameters for <sql> - see docs/design-sql-bound-parameters.md.
|
|
//
|
|
// Everything here is plain string logic with no MySQL or Tag dependency,
|
|
// so it can be unit tested on its own (tests/sqlbind_test.cpp). __bind
|
|
// builds BoundValues with makeBoundValue(); __sql substitutes them into
|
|
// the rendered query with bindPlaceholders(), passing in the connection's
|
|
// own escaping function.
|
|
|
|
namespace jet {
|
|
|
|
struct BoundValue {
|
|
enum Kind {
|
|
SQLNULL, // emitted as NULL
|
|
BARE, // already validated (int/decimal/bool/ASC/DESC/`ident`) - emitted as is
|
|
QUOTED // escaped with the connection's escaper and wrapped in '...'
|
|
};
|
|
Kind kind = QUOTED;
|
|
std::string text;
|
|
};
|
|
|
|
// Validates value against type and returns how it must be emitted.
|
|
// position is the 1-based bind number, used only in error messages.
|
|
// Error messages never contain the value itself (it may be a password).
|
|
// type: string (default if empty), int, decimal, bool, date, datetime, ident
|
|
// nullMode: "" (bind '' for an empty value) or "empty" (bind NULL)
|
|
// allow: comma list of permitted identifiers - required for ident,
|
|
// not allowed on any other type
|
|
BoundValue makeBoundValue(const std::string &value,
|
|
const std::string &type,
|
|
const std::string &nullMode,
|
|
bool hasAllow,
|
|
const std::string &allow,
|
|
int position);
|
|
|
|
// Number of '?' placeholders in query that are outside string literals,
|
|
// backtick identifiers and comments.
|
|
size_t countPlaceholders(const std::string &query, bool noBackslashEscapes);
|
|
|
|
// Replaces each placeholder (as counted above) with the matching bound
|
|
// value, in order. Throws if the placeholder and value counts differ.
|
|
// quoteEscape escapes the body of a single-quoted string literal (no
|
|
// surrounding quotes) for the actual connection.
|
|
std::string bindPlaceholders(const std::string &query,
|
|
const std::vector<BoundValue> &values,
|
|
bool noBackslashEscapes,
|
|
const std::function<std::string(const std::string &)> "eEscape);
|
|
|
|
// True for a SET statement that changes the connection character set
|
|
// (SET NAMES, SET CHARACTER SET, SET CHARSET, or an assignment to
|
|
// character_set_client / character_set_connection / collation_connection).
|
|
// Changing it after connect makes client-side escaping and server-side
|
|
// parsing disagree, so <sql> refuses these.
|
|
bool isCharsetChange(const std::string &query);
|
|
|
|
}
|
|
|
|
#endif
|