Skip to content

Commit

Permalink
Handle unescaped ? when calling toString() on query (#6)
Browse files Browse the repository at this point in the history
* Fixed unescaped ? and added a test

* Tweaked test to confirm update occurs

* Changed split / join with regex replace
  • Loading branch information
CFitzsimons authored and felixmosh committed Sep 4, 2019
1 parent f592113 commit c1713b1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
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(', ')}`,
bindings
);
};
Expand Down

0 comments on commit c1713b1

Please sign in to comment.