Skip to content

Fix placeholder parsing: literals, comments and multiple %% (4.0.12) - #13

Merged
ellisgl merged 2 commits into
releasefrom
fix-placeholders-in-literals
Nov 25, 2026
Merged

ellisgl merged 2 commits into
releasefrom
fix-placeholders-in-literals

Conversation

@ellisgl

@ellisgl ellisgl commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Statement::sql() turned every ? and %% into a placeholder, even inside quoted strings in your own SQL:

$Statement->sql("SELECT 'Why?' AS q, ? AS v")->bStr('x');
// before: the ? in 'Why?' became a placeholder, so the query failed
// after:  returns ['q' => 'Why?', 'v' => 'x']

How it works: sql() now tracks where it is in the statement, character by character, and only replaces placeholders in SQL code.

Context ? %%
SQL code placeholder placeholder
'...' and "..." strings (incl. '' and \' escapes) left alone left alone
`...` identifiers left alone still a placeholder, so `%%` with bRaw() can name a table, as the tests and README do
-- , # and /* */ comments left alone left alone

The state carries across sql() calls, so a string split over two calls is handled. reset() clears it.

Also fixed: execute() joined the sql() calls with spaces, while getComputed() used newlines. With spaces, a -- comment in one sql() call silently commented out every call after it. execute() now uses newlines too.

Tests

  • New tests/StatementPlaceholderTest.php: 19 cases covering strings, escapes, identifiers, comments, -- without a space (subtraction, not a comment), strings spanning sql() calls, and reset(). No database needed.
  • 2 new MySQL tests that execute a query with ? and %% inside literals, and one with a -- comment. Both fail on the old code.
  • The full suite passes against MySQL 8.4 (93 tests), along with PHPStan level 8 and PSR-12.

🤖 Generated with Claude Code

ellisgl and others added 2 commits November 25, 2026 09:00
Statement::sql() turned every ? and %% into a placeholder, so SQL such
as WHERE note = 'Why?' broke. sql() now tracks quoted strings,
backtick identifiers and comments, across sql() calls, and only
replaces placeholders in SQL code. Inside backticks %% is still a
placeholder, so `%%` with bRaw() can still name a table.

execute() now joins sql() calls with newlines, as getComputed()
already did, so a -- comment in one call no longer comments out the
calls after it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sql() named every %% with the current raw bind position without
advancing it, so two %% in one sql() call got the same name and both
were filled with the first raw value: sql('VALUES (%%, %%)')
->bFloat(1)->bFloat(2) produced VALUES (1.000, 1.000). %% now has its
own counter, matched in order to the raw binds, as ? already is.

Also sets the CHANGELOG and README "Latest" entries to 4.0.12.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ellisgl ellisgl changed the title Leave ? and %% alone inside SQL string literals and comments Fix placeholder parsing: literals, comments and multiple %% (4.0.12) Nov 25, 2026
@ellisgl

ellisgl commented Nov 25, 2026

Copy link
Copy Markdown
Owner Author

Added one more fix: two %% placeholders in one sql() call both got the first value.

$s->sql('VALUES (%%, %%)')->bFloat(1)->bFloat(2);
// before: VALUES (1.000, 1.000)
// after:  VALUES (1.000, 2.000)

sql() named each %% with the current raw bind position but never advanced it. %% now has its own counter, matched in order to the raw bindings, the same way ? already works.

Also added 3 tests (two %% in one call, ? and %% mixed in one call, and a MySQL execution test) and set the CHANGELOG and README entries to 4.0.12. The full suite passes against MySQL 8.4 (96 tests), along with PHPStan level 8 and PSR-12.

🤖 Generated with Claude Code

@ellisgl
ellisgl merged commit 5ff91bd into release Nov 25, 2026
7 checks passed
@ellisgl
ellisgl deleted the fix-placeholders-in-literals branch November 25, 2026 15:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant