From 50a0f244a976596ecf050062357c46c3e8e0f452 Mon Sep 17 00:00:00 2001 From: Hari Narayan Sharma Date: Tue, 14 Oct 2025 22:31:07 +0530 Subject: [PATCH] Improve handling of mixed/nested quotes in query parsing fix: improve handling of mixed/nested quotes in query parsing --- index.js | 2 +- test/example.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 9e3d57f..4f58f09 100644 --- a/index.js +++ b/index.js @@ -40,7 +40,7 @@ function parse(query) { continue; } inQuote = false; - } else if (chr === DQUOTE || chr === SQUOTE) { + } else if (!inQuote && (chr === DQUOTE || chr === SQUOTE)) { inQuote = true; qchr = chr; } diff --git a/test/example.js b/test/example.js index 6e6cad7..1553f40 100644 --- a/test/example.js +++ b/test/example.js @@ -61,6 +61,40 @@ describe('given input query with named parameters', () => { assert.deepEqual(compile(query, { id: 123 }), expected); assert.deepEqual(compile(query, { id: 123 }), expected); }); + + it('should handle queries with mixed/nested quotes correctly', () => { + // Bug fix test: queries with single and double quotes mixed should parse all parameters + const query = `SELECT * FROM items WHERE name = 'foo "bar"' AND id = :id AND status = :status`; + const result = compile(query, { id: 123, status: 'active' }); + + assert.strictEqual(result[1].length, 2, 'Should extract both parameters'); + assert.deepEqual(result[1], [123, 'active']); + }); + + it('should handle large queries with multiple parameters after quoted strings', () => { + // Real-world scenario: complex query with nested quotes and multiple parameters + const query = `Select users.json, + CASE + WHEN users.role = 'admin' + THEN CONCAT('"', users.name, '"') + ELSE users.name + END AS label + from users + where users.id = :id + and users.created = :created + and users.updated = :updated + and users.status = :status`; + + const result = compile(query, { + id: 100, + created: '2024-01-01', + updated: '2024-12-31', + status: 'active' + }); + + assert.strictEqual(result[1].length, 4, 'Should extract all 4 parameters'); + assert.deepEqual(result[1], [100, '2024-01-01', '2024-12-31', 'active']); + }); }); describe('postgres-style toNumbered conversion', () => {