Browse Source

feat: formula - add rounddown and roundup support

pull/7019/head
Pranav C 1 year ago
parent
commit
86702eda64
  1. 26
      packages/nc-gui/utils/formulaUtils.ts
  2. 30
      packages/nocodb/src/db/functionMappings/pg.ts

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

@ -532,7 +532,7 @@ const formulas: Record<string, any> = {
COUNTA: {
validation: {
args: {
min:1,
min: 1,
},
},
description: '',
@ -559,6 +559,30 @@ const formulas: Record<string, any> = {
syntax: 'COUNTALL()',
examples: ['COUNTALL()'],
},
ROUNDDOWN: {
type: formulaTypes.NUMERIC,
validation: {
args: {
min: 1,
max: 2,
},
},
description: '',
syntax: 'ROUNDDOWN()',
examples: ['ROUNDDOWN()'],
},
ROUNDUP: {
type: formulaTypes.NUMERIC,
validation: {
args: {
min: 1,
max: 2,
},
},
description: '',
syntax: 'ROUNDUP()',
examples: ['ROUNDUP()'],
},
}
const formulaList = Object.keys(formulas)

30
packages/nocodb/src/db/functionMappings/pg.ts

@ -253,6 +253,36 @@ const pg = {
),
};
},
// todo: move to common and use existing functions
ROUNDDOWN: async ({ fn, knex, pt, colAlias }: MapFnArgs) => {
const { builder: valueBuilder } = await fn(pt.arguments[0]);
let precisionBuilder = knex.raw('0');
if (pt.arguments[1]) {
const { builder } = await fn(pt.arguments[1]);
precisionBuilder = builder;
}
return {
builder: knex.raw(
`ROUND(FLOOR(${valueBuilder} * POWER(10, ${precisionBuilder})) / POWER(10, ${precisionBuilder}))${colAlias}`,
),
};
},
ROUNDUP: async ({ fn, knex, pt, colAlias }: MapFnArgs) => {
const { builder: valueBuilder } = await fn(pt.arguments[0]);
let precisionBuilder = knex.raw('0');
if (pt.arguments[1]) {
const { builder } = await fn(pt.arguments[1]);
precisionBuilder = builder;
}
return {
builder: knex.raw(
`ROUND(CEIL(${valueBuilder} * POWER(10, ${precisionBuilder})) / POWER(10, ${precisionBuilder}))${colAlias}`,
),
};
},
};
export default pg;

Loading…
Cancel
Save