Browse Source

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

pull/7299/head
Pranav C 9 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']) {
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,

25
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}`),
};
},
};

Loading…
Cancel
Save