Add Test Case for Single Quote and Fix Logic

Summary: Single quotes need to be double to be correctly recognized in queries. #thanks mweststrate for reminding me

Reviewed By: mweststrate

Differential Revision: D21908315

fbshipit-source-id: 6c13f9ddb527d1144cc3df90ba48bdb5f2ed4952
This commit is contained in:
Chaiwat Ekkaewnumchai
2020-06-08 08:57:33 -07:00
committed by Facebook GitHub Bot
parent a50c373631
commit 525d0e9925
2 changed files with 26 additions and 5 deletions

View File

@@ -51,13 +51,12 @@ export function constructQueryClause(
): string {
return Object.entries(values).reduce(
(clauses, [key, val]: [string, Value], idx) => {
const {type, value} = val;
const valueString =
type === 'null'
val.type === 'null'
? 'NULL'
: type === 'string' || type === 'blob'
? `'${value}'`
: `${value}`;
: val.type === 'string' || val.type === 'blob'
? `'${val.value.replace(/'/g, "''")}'`
: `${val.value}`;
if (idx <= 0) {
return `${key}=${valueString}`;
} else {

View File

@@ -248,6 +248,15 @@ test('constructQueryClause with exactly one null value', () => {
).toEqual(`key1=NULL`);
});
test("constructQueryClause with special character (single quote ('))", () => {
expect(
constructQueryClause(
{key1: {type: 'string', value: "this is a 'single quote'"}},
'connecter',
),
).toEqual(`key1='this is a ''single quote'''`);
});
test('constructQueryClause with multiple value', () => {
const values: {[key: string]: Value} = {
key1: {type: 'string', value: 'this is a string'},
@@ -260,6 +269,19 @@ test('constructQueryClause with multiple value', () => {
);
});
test('constructQueryClause with multiple value with single quotes mixed in string', () => {
const values: {[key: string]: Value} = {
key1: {type: 'string', value: `this is 'a' string`},
key2: {type: 'null', value: null},
key3: {type: 'float', value: 13.37},
key4: {type: 'string', value: `there are single quotes 'here' and 'there'`},
};
expect(constructQueryClause(values, 'connector')).toEqual(
`key1='this is ''a'' string' connector key2=NULL connector key3=13.37 connector key4='there are single quotes ''here'' and ''there'''`,
);
});
test('constructUpdateQuery', () => {
const setClause: {[key: string]: Value} = {
key1: {type: 'string', value: 'this is a string'},