From ebc01a59d48725f8015df753629a0e70a8bc1055 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Wed, 22 Nov 2023 07:17:57 +0000 Subject: [PATCH] feat: formula - pg - value function support --- packages/nc-gui/utils/formulaUtils.ts | 10 ++++++++++ packages/nocodb/src/db/functionMappings/pg.ts | 13 +++++++++++++ 2 files changed, 23 insertions(+) diff --git a/packages/nc-gui/utils/formulaUtils.ts b/packages/nc-gui/utils/formulaUtils.ts index 0ece3b6bec..c216e47399 100644 --- a/packages/nc-gui/utils/formulaUtils.ts +++ b/packages/nc-gui/utils/formulaUtils.ts @@ -583,6 +583,16 @@ const formulas: Record = { syntax: 'ROUNDUP()', examples: ['ROUNDUP()'], }, + VALUE: { + validation: { + args: { + rqd: 1, + }, + }, + description: '', + syntax: 'VALUE()', + examples: ['VALUE()'], + }, } const formulaList = Object.keys(formulas) diff --git a/packages/nocodb/src/db/functionMappings/pg.ts b/packages/nocodb/src/db/functionMappings/pg.ts index 13386176f5..7fea4f8ca7 100644 --- a/packages/nocodb/src/db/functionMappings/pg.ts +++ b/packages/nocodb/src/db/functionMappings/pg.ts @@ -283,6 +283,19 @@ const pg = { ), }; }, + VALUE: async ({ fn, knex, pt, colAlias }: MapFnArgs) => { + const value = (await fn(pt.arguments[0])).builder.toString(); + + return { + builder: knex.raw( + `ROUND(CASE + WHEN ${value} IS NULL OR REGEXP_REPLACE(${value}::TEXT, '[^\\d.]+', '', 'g') IN ('.', '') OR LENGTH(REGEXP_REPLACE(${value}::TEXT, '[^.]+', '', 'g')) > 1 THEN NULL + WHEN LENGTH(REGEXP_REPLACE(${value}::TEXT, '[^%]', '','g')) > 0 THEN POW(-1, LENGTH(REGEXP_REPLACE(${value}::TEXT, '[^-]','', 'g'))) * (REGEXP_REPLACE(${value}::TEXT, '[^\\d.]+', '', 'g'))::NUMERIC / 100 + ELSE POW(-1, LENGTH(REGEXP_REPLACE(${value}::TEXT, '[^-]', '', 'g'))) * (REGEXP_REPLACE(${value}::TEXT, '[^\\d.]+', '', 'g'))::NUMERIC +END) ${colAlias}`, + ), + }; + }, }; export default pg;