-
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.
- Loading branch information
Showing
6 changed files
with
77 additions
and
21 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
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 |
---|---|---|
@@ -1,11 +1,16 @@ | ||
import { Logger } from 'cheminfo-types'; | ||
|
||
import { QueryCriterium } from '../types/QueryCriterium'; | ||
import { Schema } from '../types/Schema'; | ||
|
||
import { getNumberRange } from './getNumberRange'; | ||
|
||
type Values = Record<string, number | string>; | ||
|
||
interface AppendSQLForCriteriaOptions { | ||
defaultFields?: string[]; | ||
fieldsAliases?: Record<string, string[]>; | ||
logger?: Logger; | ||
} | ||
|
||
/** | ||
Check warning on line 16 in src/utils/appendSQLForCriteria.ts GitHub Actions / nodejs / lint-eslint
Check warning on line 16 in src/utils/appendSQLForCriteria.ts GitHub Actions / nodejs / lint-eslint
|
||
|
@@ -44,12 +49,17 @@ export function appendSQLForCriteria( | |
} | ||
// build the corresponding sql part | ||
for (const criterium of criteria) { | ||
criterium.sql = buildSQL(criterium, schema, values); | ||
criterium.sql = buildSQL(criterium, schema, values, options); | ||
} | ||
return values; | ||
} | ||
|
||
function buildSQL(criterium: QueryCriterium, schema: Schema, values: Values) { | ||
function buildSQL( | ||
criterium: QueryCriterium, | ||
schema: Schema, | ||
values: Values, | ||
options: AppendSQLForCriteriaOptions, | ||
) { | ||
const sql = []; | ||
for (const field of criterium.fields) { | ||
const column = schema[field]; | ||
|
@@ -67,15 +77,15 @@ function buildSQL(criterium: QueryCriterium, schema: Schema, values: Values) { | |
case 'INT': | ||
case 'INTEGER': | ||
{ | ||
const newSQL = processNumber(field, criterium, values); | ||
const newSQL = processNumber(field, criterium, values, options); | ||
if (newSQL) { | ||
sql.push(newSQL); | ||
} | ||
} | ||
break; | ||
case 'BOOLEAN': | ||
{ | ||
const newSQL = processBoolean(field, criterium, values); | ||
const newSQL = processBoolean(field, criterium, values, options); | ||
if (newSQL) { | ||
sql.push(newSQL); | ||
} | ||
|
@@ -133,8 +143,10 @@ function processNumber( | |
field: string, | ||
criterium: QueryCriterium, | ||
values: Values, | ||
options: AppendSQLForCriteriaOptions, | ||
) { | ||
const operator = criterium.operator || '='; | ||
const { logger } = options; | ||
const operator = criterium.operator || '~'; | ||
const sqls = []; | ||
let joinOperator = 'OR'; | ||
for (let valueIndex = 0; valueIndex < criterium.values.length; valueIndex++) { | ||
|
@@ -146,13 +158,28 @@ function processNumber( | |
const valueFieldName = `${field}_${criterium.index}_${valueIndex}`; | ||
|
||
switch (operator) { | ||
case '~': | ||
{ | ||
// we consider by default that a number is not exact by default | ||
const { min, max } = getNumberRange(value); | ||
values[`${valueFieldName}_min`] = min; | ||
values[`${valueFieldName}_max`] = max; | ||
sqls.push( | ||
`${field} BETWEEN :${valueFieldName}_min AND :${valueFieldName}_max`, | ||
); | ||
} | ||
break; | ||
case '=': | ||
values[valueFieldName] = Number(value); | ||
sqls.push(`${field} = :${valueFieldName}`); | ||
break; | ||
case '..': | ||
{ | ||
const [min, max] = value.split('..').map(Number); | ||
if (Number.isNaN(min) || Number.isNaN(max)) { | ||
if (logger) logger.info(`Invalid range for ${field}: ${value}`); | ||
continue; | ||
} | ||
values[`${valueFieldName}_min`] = min; | ||
values[`${valueFieldName}_max`] = max; | ||
sqls.push( | ||
|
@@ -192,9 +219,12 @@ function processBoolean( | |
field: string, | ||
criterium: QueryCriterium, | ||
values: Values, | ||
options: AppendSQLForCriteriaOptions, | ||
) { | ||
const { logger } = options; | ||
if (criterium.values.length > 1) { | ||
throw new Error('Boolean does not support multiple values'); | ||
if (logger) logger.info('Boolean does not support multiple values'); | ||
return ''; | ||
} | ||
const value = getBooleanValue(criterium.values[0]); | ||
if (value === null) { | ||
|