From 9f286cca66f6a65de055f4e761211fd0bfd61419 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Tue, 26 Dec 2023 18:40:17 +0000 Subject: [PATCH] fix: handle non-string types in formula(switch/if with blank function) --- .../src/db/formulav2/formulaQueryBuilderv2.ts | 17 ++++++++++--- .../src/db/functionMappings/commonFns.ts | 25 ++++++++++++++++--- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/packages/nocodb/src/db/formulav2/formulaQueryBuilderv2.ts b/packages/nocodb/src/db/formulav2/formulaQueryBuilderv2.ts index de58f864b1..665f6bd6d9 100644 --- a/packages/nocodb/src/db/formulav2/formulaQueryBuilderv2.ts +++ b/packages/nocodb/src/db/formulav2/formulaQueryBuilderv2.ts @@ -880,17 +880,26 @@ async function _formulaQueryBuilder( for (const operand of ['left', 'right']) { if ( pt[operand].type === 'CallExpression' && - pt[operand].callee.name === 'BLANK' && - pt[operand === 'left' ?'right' : 'left'].dataType === - FormulaDataTypes.STRING + pt[operand].callee.name === 'BLANK' ) { + const isString = + pt[operand === 'left' ? 'right' : 'left'].dataType === + FormulaDataTypes.STRING; + let calleeName; + + if (pt.operator === '==') { + calleeName = isString ? 'ISBLANK' : 'ISNULL'; + } else { + calleeName = isString ? 'ISNOTBLANK' : 'ISNOTNULL'; + } + return fn( { type: 'CallExpression', arguments: [operand === 'left' ? pt.right : pt.left], callee: { type: 'Identifier', - name: pt.operator === '==' ? 'ISBLANK' : 'NOTBLANK', + name: calleeName, }, }, alias, diff --git a/packages/nocodb/src/db/functionMappings/commonFns.ts b/packages/nocodb/src/db/functionMappings/commonFns.ts index c26bf9889b..376086d65c 100644 --- a/packages/nocodb/src/db/functionMappings/commonFns.ts +++ b/packages/nocodb/src/db/functionMappings/commonFns.ts @@ -77,11 +77,16 @@ export default { if ( args.pt.arguments[i * 2 + 1].type === 'CallExpression' && - args.pt.arguments[i * 2 + 1].callee?.name === 'BLANK' && - args.pt.arguments[i * 2 + 1].dataType === FormulaDataTypes.STRING + args.pt.arguments[i * 2 + 1].callee?.name === 'BLANK' ) { elseValPrefix += args.knex - .raw(`\n\tWHEN ${switchVal} IS NULL OR ${switchVal} = '' THEN ${val}`) + .raw( + `\n\tWHEN ${switchVal} IS NULL ${ + args.pt.arguments[i * 2 + 1].dataType === FormulaDataTypes.STRING + ? `OR ${switchVal} = ''` + : '' + } THEN ${val}`, + ) .toQuery(); } else if ( args.pt.arguments[i * 2 + 1].dataType === FormulaDataTypes.NULL @@ -353,6 +358,13 @@ export default { ), }; }, + ISNULL: async ({ fn, knex, pt, colAlias }: MapFnArgs) => { + const { builder: valueBuilder } = await fn(pt.arguments[0]); + + return { + builder: knex.raw(`(${valueBuilder} IS NULL)${colAlias}`), + }; + }, ISNOTBLANK: async ({ fn, knex, pt, colAlias }: MapFnArgs) => { const { builder: valueBuilder } = await fn(pt.arguments[0]); @@ -362,4 +374,11 @@ export default { ), }; }, + ISNOTNULL: async ({ fn, knex, pt, colAlias }: MapFnArgs) => { + const { builder: valueBuilder } = await fn(pt.arguments[0]); + + return { + builder: knex.raw(`(${valueBuilder} IS NOT NULL)${colAlias}`), + }; + }, };