db0 icon indicating copy to clipboard operation
db0 copied to clipboard

fix(postgresql): only replace `?` param placeholders outside quoted strings

Open guoyangzhen opened this issue 8 months ago β€’ 1 comments

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.

guoyangzhen avatar Mar 16 '26 06:03 guoyangzhen

πŸ“ Walkthrough

Walkthrough

The 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.ts
Replaces 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

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related issues

  • unjs/db0#216: Directly addresses the bug where normalizeParams incorrectly replaced literal question marks within SQL constructs by implementing a syntax-aware parser that distinguishes between placeholders and literal characters.

Poem

🐰 A parser's tale, with quotes and signs,
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
πŸ§ͺ Generate unit tests (beta)
  • [ ] Create PR with unit tests
  • [ ] Post copyable unit tests in a comment
πŸ“ Coding Plan
  • [ ] Generate coding plan for human review comments

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.

❀️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Mar 16 '26 06:03 coderabbitai[bot]