Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle unescaped ? when calling toString() on query #6

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions __tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,18 @@ describe('onDuplicateUpdate', () => {
email: 'updated-email',
}));
});

it('should correctly escape field with ? character', async () => {
await db
.insert({ id: 7, name: 'other value' })
.into('persons');
await db
.insert({ id: 7, name: '?' })
.into('persons')
.onDuplicateUpdate('name');
const person = await getById(7);

expect(person.name).toBe('?');
});
});
});
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ module.exports.attachOnDuplicateUpdate = function attachOnDuplicateUpdate() {

return result;
}, { placeholders: [], bindings: [] });

const escapeKnexString = input => input.replace(/\?/g, '\\?');
return this.client.raw(
`${this.toString()} on duplicate key update ${placeholders.join(', ')}`,
`${escapeKnexString(this.toString())} on duplicate key update ${placeholders.join(', ')}`,
CFitzsimons marked this conversation as resolved.
Show resolved Hide resolved
bindings
);
};
Expand Down