Browse Source

fix: treat values in AND/OR based on type

pull/7268/head
Pranav C 11 months ago
parent
commit
9e8871cf40
  1. 48
      packages/nocodb/src/db/functionMappings/commonFns.ts

48
packages/nocodb/src/db/functionMappings/commonFns.ts

@ -2,6 +2,31 @@ import { FormulaDataTypes } from 'nocodb-sdk';
import type { MapFnArgs } from '../mapFunctionName'; import type { MapFnArgs } from '../mapFunctionName';
import { NcError } from '~/helpers/catchError'; import { NcError } from '~/helpers/catchError';
async function treatArgAsConditionalExp(
args: MapFnArgs,
argument = args.pt?.arguments?.[0],
) {
const condArg = (await args.fn(argument)).builder.toQuery();
let cond = condArg;
switch (argument.dataType as FormulaDataTypes) {
case FormulaDataTypes.NUMERIC:
cond = `(${condArg}) IS NOT NULL AND (${condArg}) != 0`;
break;
case FormulaDataTypes.STRING:
cond = `(${condArg}) IS NOT NULL AND (${condArg}) != ''`;
break;
case FormulaDataTypes.BOOLEAN:
cond = `(${condArg}) IS NOT NULL AND (${condArg}) != false`;
break;
case FormulaDataTypes.DATE:
cond = `(${condArg}) IS NOT NULL`;
break;
}
return { builder: args.knex.raw(cond) };
}
export default { export default {
// todo: handle default case // todo: handle default case
SWITCH: async (args: MapFnArgs) => { SWITCH: async (args: MapFnArgs) => {
@ -37,24 +62,7 @@ export default {
}; };
}, },
IF: async (args: MapFnArgs) => { IF: async (args: MapFnArgs) => {
const condArg = (await args.fn(args.pt.arguments[0])).builder.toQuery(); const cond = await treatArgAsConditionalExp(args);
let cond = condArg;
switch (args.pt.arguments[0].dataType as FormulaDataTypes) {
case FormulaDataTypes.NUMERIC:
cond = `(${condArg}) IS NOT NULL AND (${condArg}) != 0`;
break;
case FormulaDataTypes.STRING:
cond = `(${condArg}) IS NOT NULL AND (${condArg}) != ''`;
break;
case FormulaDataTypes.BOOLEAN:
cond = `(${condArg}) IS NOT NULL AND (${condArg}) != false`;
break;
case FormulaDataTypes.DATE:
cond = `(${condArg}) IS NOT NULL`;
break;
}
let query = args.knex let query = args.knex
.raw( .raw(
@ -80,7 +88,7 @@ export default {
`${( `${(
await Promise.all( await Promise.all(
args.pt.arguments.map(async (ar) => args.pt.arguments.map(async (ar) =>
(await args.fn(ar)).builder.toQuery(), (await treatArgAsConditionalExp(args, ar)).builder.toQuery(),
), ),
) )
).join(' AND ')}`, ).join(' AND ')}`,
@ -98,7 +106,7 @@ export default {
`${( `${(
await Promise.all( await Promise.all(
args.pt.arguments.map(async (ar) => args.pt.arguments.map(async (ar) =>
(await args.fn(ar)).builder.toQuery(), (await treatArgAsConditionalExp(args, ar)).builder.toQuery(),
), ),
) )
).join(' OR ')}`, ).join(' OR ')}`,

Loading…
Cancel
Save