Browse Source

feat: formula - pg - value function support

pull/7019/head
Pranav C 1 year ago
parent
commit
ebc01a59d4
  1. 10
      packages/nc-gui/utils/formulaUtils.ts
  2. 13
      packages/nocodb/src/db/functionMappings/pg.ts

10
packages/nc-gui/utils/formulaUtils.ts

@ -583,6 +583,16 @@ const formulas: Record<string, any> = {
syntax: 'ROUNDUP()', syntax: 'ROUNDUP()',
examples: ['ROUNDUP()'], examples: ['ROUNDUP()'],
}, },
VALUE: {
validation: {
args: {
rqd: 1,
},
},
description: '',
syntax: 'VALUE()',
examples: ['VALUE()'],
},
} }
const formulaList = Object.keys(formulas) const formulaList = Object.keys(formulas)

13
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; export default pg;

Loading…
Cancel
Save