Skip to content

Commit

Permalink
fix: allow positional value placeholders
Browse files Browse the repository at this point in the history
This check is unsafe: it will break if query contains “?” as a member of a string, e.g. SELECT “?”.
  • Loading branch information
gajus committed Apr 14, 2017
1 parent c33f531 commit 2843bcf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/utilities/normalizeAnonymousValuePlaceholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ export default (
sql: string,
values: AnonymouseValuePlaceholderValuesType = []
): NormalizedQueryType => {
if (!anonymousePlaceholdersRegex.test(sql)) {
return {
sql,
values
};
}

anonymousePlaceholdersRegex.lastIndex = 0;

let chunkIndex = 0;
let result = '';
let match;
Expand Down
14 changes: 14 additions & 0 deletions test/utilities/normalizeAnonymousValuePlaceholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ test('does not error when placeholders are absent', (t) => {
t.deepEqual(values, []);
});

test('does not error when passed values without anonymous placeholders', (t) => {
const {
sql,
values
} = normalizeAnonymousValuePlaceholders('SELECT $1', [
'foo'
]);

t.true(sql === 'SELECT $1');
t.deepEqual(values, [
'foo'
]);
});

test('interpolates a value placeholder', (t) => {
const {
sql,
Expand Down

0 comments on commit 2843bcf

Please sign in to comment.