#ifndef __SqlBind_h__ #define __SqlBind_h__ #include #include #include // Bound parameters for - 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 &values, bool noBackslashEscapes, const std::function "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 refuses these. bool isCharsetChange(const std::string &query); } #endif