-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ignore filtering values that do not have the right type
- Loading branch information
Showing
4 changed files
with
56 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ import { Schema } from '../types/Schema'; | |
|
||
type Values = Record<string, number | string>; | ||
|
||
interface BuildQueryCriteriaOptions { | ||
interface AppendSQLForCriteriaOptions { | ||
defaultFields?: string[]; | ||
fieldsAliases?: Record<string, string[]>; | ||
} | ||
|
@@ -14,10 +14,10 @@ interface BuildQueryCriteriaOptions { | |
* @param schema | ||
Check warning on line 14 in src/utils/appendSQLForCriteria.ts GitHub Actions / nodejs / lint-eslint
|
||
* @param options | ||
Check warning on line 15 in src/utils/appendSQLForCriteria.ts GitHub Actions / nodejs / lint-eslint
|
||
*/ | ||
export function buildQueryCriteria( | ||
export function appendSQLForCriteria( | ||
criteria: QueryCriterium[], | ||
schema: Schema, | ||
options: BuildQueryCriteriaOptions = {}, | ||
options: AppendSQLForCriteriaOptions = {}, | ||
): Record<string, number | string> { | ||
const { defaultFields = Object.keys(schema), fieldsAliases = {} } = options; | ||
const values: Values = {}; | ||
|
@@ -54,15 +54,30 @@ function buildSQL(criterium: QueryCriterium, schema: Schema, values: Values) { | |
|
||
switch (column.type) { | ||
case 'TEXT': | ||
sql.push(processText(field, criterium, values)); | ||
{ | ||
const newSQL = processText(field, criterium, values); | ||
if (newSQL) { | ||
sql.push(newSQL); | ||
} | ||
} | ||
break; | ||
case 'REAL': | ||
case 'INT': | ||
case 'INTEGER': | ||
sql.push(processNumber(field, criterium, values)); | ||
{ | ||
const newSQL = processNumber(field, criterium, values); | ||
if (newSQL) { | ||
sql.push(newSQL); | ||
} | ||
} | ||
break; | ||
case 'BOOLEAN': | ||
sql.push(processBoolean(field, criterium, values)); | ||
{ | ||
const newSQL = processBoolean(field, criterium, values); | ||
if (newSQL) { | ||
sql.push(newSQL); | ||
} | ||
} | ||
break; | ||
case 'BLOB': | ||
case 'NULL': | ||
|
@@ -73,6 +88,9 @@ function buildSQL(criterium: QueryCriterium, schema: Schema, values: Values) { | |
); | ||
} | ||
} | ||
if (sql.length === 0) { | ||
return ''; | ||
} | ||
return `(${sql.join(' OR ')})`; | ||
} | ||
|
||
|
@@ -119,7 +137,10 @@ function processNumber( | |
let joinOperator = 'OR'; | ||
for (let valueIndex = 0; valueIndex < criterium.values.length; valueIndex++) { | ||
const value = criterium.values[valueIndex]; | ||
|
||
// is it really a number ? We should skip otherwise | ||
if (operator !== '..' && Number.isNaN(Number(value))) { | ||
continue; | ||
} | ||
const valueFieldName = `${field}_${criterium.index}_${valueIndex}`; | ||
|
||
switch (operator) { | ||
|
@@ -159,6 +180,9 @@ function processNumber( | |
); | ||
} | ||
} | ||
if (sqls.length === 0) { | ||
return ''; | ||
} | ||
return `(${sqls.join(` ${joinOperator} `)})`; | ||
} | ||
|
||
|
@@ -170,7 +194,10 @@ function processBoolean( | |
if (criterium.values.length > 1) { | ||
throw new Error('Boolean does not support multiple values'); | ||
} | ||
const value = criterium.values[0]; | ||
const value = getBooleanValue(criterium.values[0]); | ||
if (value === null) { | ||
return ''; | ||
} | ||
const operator = criterium.operator || '='; | ||
switch (operator) { | ||
case '=': | ||
|
@@ -182,3 +209,14 @@ function processBoolean( | |
); | ||
} | ||
} | ||
|
||
function getBooleanValue(value: string): boolean | null { | ||
value = value.toLowerCase(); | ||
if (value === 'true' || value === '1' || value === 'yes') { | ||
return true; | ||
} | ||
if (value === 'false' || value === '0' || value === 'no') { | ||
return false; | ||
} | ||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters