Browse Source

fix: switch function BLANK function and proper null support

pull/7299/head
Pranav C 1 year ago
parent
commit
ad35bc2a86
  1. 4
      packages/nocodb/src/db/formulav2/formulaQueryBuilderv2.ts
  2. 27
      packages/nocodb/src/db/functionMappings/commonFns.ts

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

@ -880,7 +880,9 @@ 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
) { ) {
return fn( return fn(
{ {

27
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,6 +75,21 @@ 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();
} }
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
) {
elseValPrefix += args.knex
.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 query += args.knex
.raw( .raw(
`\n\tWHEN ${( `\n\tWHEN ${(
@ -81,6 +98,7 @@ export default {
) )
.toQuery(); .toQuery();
} }
}
if (args.pt.arguments.length % 2 === 0) { if (args.pt.arguments.length % 2 === 0) {
let val; let val;
// cast to string if the return value types are different // cast to string if the return value types are different
@ -100,9 +118,14 @@ 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 (CASE ${elseValPrefix} ELSE ${val} END)`;
} else {
query += `\n\tELSE ${val}`; query += `\n\tELSE ${val}`;
} }
} else if (elseValPrefix) {
query += `\n\tELSE (CASE ${elseValPrefix} END)`;
}
return { return {
builder: args.knex.raw( builder: args.knex.raw(
`CASE ${switchVal} ${query}\n END${args.colAlias}`, `CASE ${switchVal} ${query}\n END${args.colAlias}`,

Loading…
Cancel
Save