Skip to content

Commit

Permalink
fix: fix parameters not being replaced when after $$ strings (#15307)
Browse files Browse the repository at this point in the history
  • Loading branch information
ephys committed Nov 19, 2022
1 parent a205765 commit bc39fd6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/utils/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function injectReplacements(
const remainingString = sqlString.slice(i, sqlString.length);

const dollarStringStartMatch = remainingString.match(/^\$(?<name>[a-z_][0-9a-z_])?(\$)/i);
const tagName = dollarStringStartMatch?.groups?.name;
const tagName = dollarStringStartMatch?.groups?.name || '';
if (currentDollarStringTagName === tagName) {
currentDollarStringTagName = null;
}
Expand Down
18 changes: 18 additions & 0 deletions test/unit/utils/sql.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ describe('injectReplacements (named replacements)', () => {
});
});

it('does consider the token to be a bind parameter if it is located after a $ quoted string', () => {
const sql = injectReplacements('SELECT $$ abc $$ AS string FROM users WHERE id = :id', dialect, {
id: 1
});

expectsql(sql, {
default: 'SELECT $$ abc $$ AS string FROM users WHERE id = 1'
});
});

it('does not consider the token to be a bind parameter if it is part of a string with a backslash escaped quote, in dialects that support backslash escape', () => {
expectPerDialect(
() =>
Expand Down Expand Up @@ -377,6 +387,14 @@ describe('injectReplacements (positional replacements)', () => {
});
});

it('does consider the token to be a bind parameter if it is located after a $ quoted string', () => {
const sql = injectReplacements('SELECT $$ abc $$ AS string FROM users WHERE id = ?', dialect, [1]);

expectsql(sql, {
default: 'SELECT $$ abc $$ AS string FROM users WHERE id = 1'
});
});

it('does not consider the token to be a replacement if it is part of a string with a backslash escaped quote', () => {
const test = () =>
injectReplacements(
Expand Down

0 comments on commit bc39fd6

Please sign in to comment.