Browse Source

fix: handle non-string types in formula(switch/if with blank function)

pull/7299/head
Pranav C 10 months ago
parent
commit
9f286cca66
  1. 17
      packages/nocodb/src/db/formulav2/formulaQueryBuilderv2.ts
  2. 25
      packages/nocodb/src/db/functionMappings/commonFns.ts

17
packages/nocodb/src/db/formulav2/formulaQueryBuilderv2.ts

@ -880,17 +880,26 @@ async function _formulaQueryBuilder(
for (const operand of ['left', 'right']) { for (const operand of ['left', 'right']) {
if ( if (
pt[operand].type === 'CallExpression' && pt[operand].type === 'CallExpression' &&
pt[operand].callee.name === 'BLANK' && pt[operand].callee.name === 'BLANK'
pt[operand === 'left' ?'right' : 'left'].dataType ===
FormulaDataTypes.STRING
) { ) {
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( return fn(
{ {
type: 'CallExpression', type: 'CallExpression',
arguments: [operand === 'left' ? pt.right : pt.left], arguments: [operand === 'left' ? pt.right : pt.left],
callee: { callee: {
type: 'Identifier', type: 'Identifier',
name: pt.operator === '==' ? 'ISBLANK' : 'NOTBLANK', name: calleeName,
}, },
}, },
alias, alias,

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

@ -77,11 +77,16 @@ export default {
if ( if (
args.pt.arguments[i * 2 + 1].type === 'CallExpression' && args.pt.arguments[i * 2 + 1].type === 'CallExpression' &&
args.pt.arguments[i * 2 + 1].callee?.name === 'BLANK' && args.pt.arguments[i * 2 + 1].callee?.name === 'BLANK'
args.pt.arguments[i * 2 + 1].dataType === FormulaDataTypes.STRING
) { ) {
elseValPrefix += args.knex 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(); .toQuery();
} else if ( } else if (
args.pt.arguments[i * 2 + 1].dataType === FormulaDataTypes.NULL 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) => { ISNOTBLANK: async ({ fn, knex, pt, colAlias }: MapFnArgs) => {
const { builder: valueBuilder } = await fn(pt.arguments[0]); 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}`),
};
},
}; };

Loading…
Cancel
Save