fix(postgresql): only replace `?` param placeholders outside quoted strings
Fixes #216 | Fixes nuxt/content#3682
Problem
normalizeParams uses sql.replace(/\?/g, ...) that replaces ALL question marks β including literal ones inside quoted strings, comments, and identifiers. This causes data corruption when content containing ? is stored via db0 PostgreSQL connector.
Fix
Replace the regex with a proper character-by-character parser that tracks quoted/comment context and only replaces standalone ? outside these regions.
Handles: single-quoted strings (with '' escapes), dollar-quoted strings, double-quoted identifiers, line/block comments.
Summary by CodeRabbit
- Bug Fixes
- Improved PostgreSQL query parameter substitution to correctly handle queries containing quoted strings, comments, and special SQL constructs. Previously, parameter placeholders within these elements were incorrectly replaced, leading to malformed queries. The system now accurately distinguishes between actual parameters and embedded placeholders.
The π― 4 (Complex) | β±οΈ ~45 minutes π° A parser's tale, with quotes and signs, βοΈ Tip: You can configure your own custom pre-merge checks in the settings.π Walkthrough
Walkthrough
normalizeParams function in the PostgreSQL connector is refactored from a simple global regex replacement to a robust character-level parser. The new implementation preserves question mark placeholders within SQL string literals, dollar-quoted strings, identifiers, and comments while correctly converting standalone ? to PostgreSQL-style $N parameters.Changes
Cohort / File(s)
Summary
PostgreSQL Parameter Normalization
src/connectors/postgresql.tsReplaces simplistic global regex replacement with character-level parser that correctly handles SQL syntax (string literals, comments, dollar-quoting) to avoid replacing literal
? characters while normalizing parameter placeholders to $N format.Estimated code review effort
Possibly related issues
normalizeParams incorrectly replaced literal question marks within SQL constructs by implementing a syntax-aware parser that distinguishes between placeholders and literal characters.Poem
Where question marks hide in clever lines,
No more a blanket replace in sight,
Character by character, getting it right,
Dollar signs dance where placeholders belong! π«π₯ Pre-merge checks | β
2 | β 1
β Failed checks (1 warning)
Check name
Status
Explanation
Resolution
Docstring Coverage
β οΈ Warning
Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%.
Write docstrings for the functions missing them to satisfy the coverage threshold.
β
Passed checks (2 passed)
Check name
Status
Explanation
Description Check
β
Passed
Check skipped - CodeRabbitβs high-level summary is enabled.
Title check
β
Passed
The title clearly and specifically describes the main change: fixing the PostgreSQL parameter placeholder replacement to only target placeholders outside quoted strings and comments, which is exactly what the changeset implements.
β¨ Finishing Touches
π§ͺ Generate unit tests (beta)
π Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands and usage tips.