Browse Source

feat: support non conditional_expression in if formula

pull/7268/head
Pranav C 11 months ago
parent
commit
f4944a2dd7
  1. 2
      packages/nocodb-sdk/src/lib/formulaHelpers.ts
  2. 2
      packages/nocodb-sdk/tsconfig.json
  3. 15
      packages/nocodb/src/db/formulav2/formulaQueryBuilderv2.ts
  4. 21
      packages/nocodb/src/db/functionMappings/commonFns.ts

2
packages/nocodb-sdk/src/lib/formulaHelpers.ts

@ -1685,7 +1685,7 @@ export function validateFormulaAndExtractTreeWithType(
res.dataType = formulas[calleeName].returnType as FormulaDataTypes;
}
} else if (parsedTree.type === JSEPNode.IDENTIFIER) {
const col = columns.find[parsedTree.name] as Record<
const col = (colIdToColMap[parsedTree.name] || colAliasToColMap[parsedTree.name]) as Record<
string,
any
>;

2
packages/nocodb-sdk/tsconfig.json

@ -38,7 +38,7 @@
// "emitDecoratorMetadata": true /* Enables experimental support for emitting type metadata for decorators. */,
"lib": ["es2017","dom"],
"types": ["jest", "node"],
"types": ["jest"],
"typeRoots": ["node_modules/@types", "src/types"],
"baseUrl": "./src",
"paths": {

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

@ -2,6 +2,7 @@ import jsep from 'jsep';
import {
jsepCurlyHook,
UITypes,
validateFormulaAndExtractTreeWithType,
validateDateWithUnknownFormat,
} from 'nocodb-sdk';
import mapFunctionName from '../mapFunctionName';
@ -14,7 +15,10 @@ import type LookupColumn from '~/models/LookupColumn';
import type { BaseModelSqlv2 } from '~/db/BaseModelSqlv2';
import NocoCache from '~/cache/NocoCache';
import { CacheGetType, CacheScope } from '~/utils/globals';
import { convertDateFormatForConcat } from '~/helpers/formulaFnHelper';
import {
convertDateFormatForConcat,
validateDateWithUnknownFormat,
} from '~/helpers/formulaFnHelper';
import FormulaColumn from '~/models/FormulaColumn';
// todo: switch function based on database
@ -62,14 +66,19 @@ async function _formulaQueryBuilder(
) {
const knex = baseModelSqlv2.dbDriver;
const columns = await model.getColumns();
// formula may include double curly brackets in previous version
// convert to single curly bracket here for compatibility
const tree = jsep(_tree.replaceAll('{{', '{').replaceAll('}}', '}'));
// const _tree1 = jsep(_tree.replaceAll('{{', '{').replaceAll('}}', '}'));
const tree = validateFormulaAndExtractTreeWithType(
_tree.replaceAll('{{', '{').replaceAll('}}', '}'),
columns,
);
const columnIdToUidt = {};
// todo: improve - implement a common solution for filter, sort, formula, etc
for (const col of await model.getColumns()) {
for (const col of columns) {
columnIdToUidt[col.id] = col.uidt;
if (col.id in aliasToColumn) continue;
switch (col.uidt) {

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

@ -1,3 +1,4 @@
import { FormulaDataTypes } from 'nocodb-sdk';
import type { MapFnArgs } from '../mapFunctionName';
import { NcError } from '~/helpers/catchError';
@ -36,11 +37,25 @@ export default {
};
},
IF: async (args: MapFnArgs) => {
const condArg = (await args.fn(args.pt.arguments[0])).builder.toQuery();
let cond = condArg;
switch (args.pt.arguments[0].dataType as FormulaDataTypes) {
case FormulaDataTypes.NUMERIC:
cond = `(${condArg}) IS NOT NULL OR (${condArg}) != 0`;
break;
case FormulaDataTypes.STRING:
cond = `(${condArg}) IS NOT NULL OR (${condArg}) != ''`;
break;
case FormulaDataTypes.BOOLEAN:
cond = `(${condArg}) IS NOT NULL OR (${condArg}) != false`;
break;
}
let query = args.knex
.raw(
`\n\tWHEN ${(
await args.fn(args.pt.arguments[0])
).builder.toQuery()} THEN ${(
`\n\tWHEN ${cond} THEN ${(
await args.fn(args.pt.arguments[1])
).builder.toQuery()}`,
)

Loading…
Cancel
Save