Repository navigation
Support named parameters in prepared statements - #261
Merged
Merged
Conversation
MySQL's prepared statements take positional ? markers only. DBInterface.prepare now rewrites each :name outside string literals, quoted identifiers, and comments to ? and keeps the names. DBInterface.execute binds a NamedTuple or AbstractDict by name; a tuple or vector binds one value per marker in order. executemany binds each row of a NamedTuple batch by name. SQL without :name is prepared byte-for-byte as before. When the server does not read every :name as a parameter (MariaDB stored-program labels such as lbl:LOOP), the SQL is prepared as written. Fixes #207 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
quinnj
approved these changes
Nov 11, 2026
quinnj
left a comment
Member
There was a problem hiding this comment.
[review generated via AI]
Approving. An independent check ran the new tests against main, where they fail, and against this head, where the full suite passes 2411/2411 with the live MySQL 8.4 and MariaDB 11.4 lanes and the trimmed build. Positional statements behaved exactly as on main on both servers. The MariaDB sql_mode=ORACLE changes listed under Blast Radius are accepted.
Merged
quinnj
referenced
this pull request
Nov 11, 2026
Co-authored-by: Jacob Quinn <quinn.jacobd@gmail.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
DBInterface.prepare(conn, "INSERT INTO t VALUES (:id, :name)")fails with a 1064 syntax error (#207): MySQL's prepared statements take positional?markers only. Users want to bind by name and reuse one value in several places. MySQL.jl now rewrites:nameto?on the client and binds values by name.What changed
DBInterface.preparesends each:nameoutside string literals, quoted identifiers, and comments as?and keeps the names. SQL without:nameis prepared byte-for-byte as before.DBInterface.executetakes aNamedTuple, or anAbstractDictkeyed bySymbolorString, for a statement with names.DBInterface.executemanybinds each row of aNamedTuplebatch by name.:nameas a parameter, the SQL is prepared as written. MariaDB prepares stored-program DDL, and a label such aslbl:LOOPlooks like a name.Fixes #207
Behavior
?markersNamedTuple, row, scalar:namemarkersNamedTuple,AbstractDict(SymbolorStringkeys):namemarkers:namemarkersTables.AbstractRowMySQLInterfaceError: its column order need not match the markers; useNamedTuple(row):idused twice(id=1,):userId(userid=1,)MySQLInterfaceError: names are case-sensitiveMySQLInterfaceErrorbefore anything is sent?and:namein one statementMySQLInterfaceErrorat prepareexecutemany,NamedTuplebatchDesign
:costs one memchr and no allocation. The scanner skips'...'and"..."literals (backslash escapes follow the session'sNO_BACKSLASH_ESCAPESstatus flag), backtick identifiers, and#,--, and/* */comments. I checked each rule against the parameter counts of mysql:8.4.13 and mariadb:11.4.13./*! */,/*M! */) stay untouched. The server runs or skips them by version (/*!99999 */is skipped, and MySQL skips/*M! */), so a rewrite inside one could break a statement that works today.:wordwas not a parameter. The client then prepares the SQL as written. If that also fails, it reports the rewrite's problem, such as the "cannot mix?and:name" error. MariaDB preparesCREATE PROCEDURE ... lbl:LOOP(alsolbl :LOOPand`lbl`:LOOP), so a lexical rule alone would break those statements.Statementgains one field,named::Union{Nothing, NamedSQL}(the rewritten SQL plus the name of each?).stmt.sqlkeeps the text as written. Execution of a named statement wraps the values in an allocation-free by-name view that the existing positional encoder iterates.Scope
prepare,execute, the one-shotexecute(conn, sql, params),executemultiple, andexecutemany. A tuple or vector still binds to:namemarkers in marker order, so MariaDBsql_mode=ORACLEstatements, where:xis a native positional marker, keep working.execute(stmt; id=1)), which MySQL.jl rejects for every statement today, and by-name binding ofTables.AbstractRow.:nameinside an executable comment is not a parameter.Tradeoffs
:begin,:loop) was rejected:BETWEEN :begin AND :endis a plausible query.lbl :LOOP.Blast Radius
Every
DBInterface.preparenow scans its SQL, and every prepared execute checks one field. Positional statements send identical bytes. On MariaDB withsql_mode=ORACLE, where:xis a native positional marker, positional values behave as before, but aNamedTuplenow binds by name instead of in field order. A Tables.jl row passed as the parameters there now raises an error, where main bound it positionally; pass a tuple or vector to keep binding by position.Verification
mainand pass here: the rewrite table, fake-peer wire checks (rewritten SQL, values in marker order,NO_BACKSLASH_ESCAPES, re-prepare after 1615, the fallbacks,executemany), and a live workflow on the mysql:8.4 and mariadb:11.4 lanes (NamedTuple,Dict, reused and mixed-case names, missing and extra names, the MariaDB label). The trim workload now prepares and executes a named statement, and the scripted server rejects any:idthat reaches it.🤖 Generated with Claude Code