Browse Source

fix: switch function BLANK function and proper null support

pull/7299/head
Pranav C 10 months ago
parent
commit
ad35bc2a86
  1. 6
      packages/nocodb/src/db/formulav2/formulaQueryBuilderv2.ts
  2. 43
      packages/nocodb/src/db/functionMappings/commonFns.ts

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

@ -877,10 +877,12 @@ async function _formulaQueryBuilder(
if (pt.operator === '==' || pt.operator === '!=') { if (pt.operator === '==' || pt.operator === '!=') {
if (pt.left.callee?.name !== pt.right.callee?.name) { if (pt.left.callee?.name !== pt.right.callee?.name) {
// if left/right is BLANK, accept both NULL and empty string // if left/right is BLANK, accept both NULL and empty string
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
) { ) {
return fn( return fn(
{ {

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

@ -33,7 +33,6 @@ async function treatArgAsConditionalExp(
} }
export default { export default {
// todo: handle default case
SWITCH: async (args: MapFnArgs) => { SWITCH: async (args: MapFnArgs) => {
const count = Math.floor((args.pt.arguments.length - 1) / 2); const count = Math.floor((args.pt.arguments.length - 1) / 2);
let query = ''; let query = '';
@ -55,6 +54,9 @@ export default {
const switchVal = (await args.fn(args.pt.arguments[0])).builder.toQuery(); const switchVal = (await args.fn(args.pt.arguments[0])).builder.toQuery();
// used it for null value check
let elseValPrefix = '';
for (let i = 0; i < count; i++) { for (let i = 0; i < count; i++) {
let val; let val;
// cast to string if the return value types are different // cast to string if the return value types are different
@ -73,13 +75,29 @@ export default {
val = (await args.fn(args.pt.arguments[i * 2 + 2])).builder.toQuery(); val = (await args.fn(args.pt.arguments[i * 2 + 2])).builder.toQuery();
} }
query += args.knex if (
.raw( args.pt.arguments[i * 2 + 1].type === 'CallExpression' &&
`\n\tWHEN ${( args.pt.arguments[i * 2 + 1].callee?.name === 'BLANK' &&
await args.fn(args.pt.arguments[i * 2 + 1]) args.pt.arguments[i * 2 + 1].dataType === FormulaDataTypes.STRING
).builder.toQuery()} THEN ${val}`, ) {
) elseValPrefix += args.knex
.toQuery(); .raw(`\n\tWHEN ${switchVal} IS NULL OR ${switchVal} = '' THEN ${val}`)
.toQuery();
} else if (
args.pt.arguments[i * 2 + 1].dataType === FormulaDataTypes.NULL
) {
elseValPrefix += args.knex
.raw(`\n\tWHEN ${switchVal} IS NULL THEN ${val}`)
.toQuery();
} else {
query += args.knex
.raw(
`\n\tWHEN ${(
await args.fn(args.pt.arguments[i * 2 + 1])
).builder.toQuery()} THEN ${val}`,
)
.toQuery();
}
} }
if (args.pt.arguments.length % 2 === 0) { if (args.pt.arguments.length % 2 === 0) {
let val; let val;
@ -100,8 +118,13 @@ export default {
await args.fn(args.pt.arguments[args.pt.arguments.length - 1]) await args.fn(args.pt.arguments[args.pt.arguments.length - 1])
).builder.toQuery(); ).builder.toQuery();
} }
if (elseValPrefix) {
query += `\n\tELSE ${val}`; query += `\n\tELSE (CASE ${elseValPrefix} ELSE ${val} END)`;
} else {
query += `\n\tELSE ${val}`;
}
} else if (elseValPrefix) {
query += `\n\tELSE (CASE ${elseValPrefix} END)`;
} }
return { return {
builder: args.knex.raw( builder: args.knex.raw(

Loading…
Cancel
Save