Browse Source

fix: XOR - formula function - WIP

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

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

@ -486,6 +486,17 @@ const formulas: Record<string, any> = {
syntax: 'BLANK()',
examples: ['BLANK()'],
},
XOR: {
type: formulaTypes.NUMERIC,
validation: {
args: {
min: 1,
},
},
description: 'Returns true if an odd number of arguments are true.',
syntax: 'XOR(expression, [exp2, ...])',
examples: ['XOR(TRUE(), FALSE(), TRUE())'],
},
}
const formulaList = Object.keys(formulas)

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

@ -193,7 +193,7 @@ const pg = {
const source = (await fn(pt.arguments[0])).builder;
const pattern = (await fn(pt.arguments[1])).builder;
return {
builder: knex.raw(`(${source}::text ~ ${pattern}::text) ${colAlias}`),
builder: knex.raw(`(${source}::TEXT ~ ${pattern}::TEXT) ${colAlias}`),
};
},
REGEX_EXTRACT: async ({ fn, knex, pt, colAlias }: MapFnArgs) => {
@ -202,8 +202,8 @@ const pg = {
return {
builder: knex.raw(
// use `SUBSTRING` since REGEXP_MATCH returns array value
// `REGEXP_MATCH(${source}::text, ${pattern}::text) ${colAlias}`,
`SUBSTRING(${source}::text from ${pattern}::text) ${colAlias}`,
// `REGEXP_MATCH(${source}::TEXT, ${pattern}::TEXT) ${colAlias}`,
`SUBSTRING(${source}::TEXT from ${pattern}::TEXT) ${colAlias}`,
),
};
},
@ -213,10 +213,22 @@ const pg = {
const replacement = (await fn(pt.arguments[2])).builder;
return {
builder: knex.raw(
`REGEXP_REPLACE(${source}::text, ${pattern}::text, ${replacement}::text, 'g') ${colAlias}`,
`REGEXP_REPLACE(${source}::TEXT, ${pattern}::TEXT, ${replacement}::TEXT, 'g') ${colAlias}`,
),
};
},
XOR: async ({ fn, knex, pt, colAlias }: MapFnArgs) => {
const args = await Promise.all(
pt.arguments.map(async (arg) => {
return knex.raw(
`(${(await fn(pt.arguments[0])).builder} IS NOT NULL)::INTEGER`,
);
}),
);
return {
builder: knex.raw(`check (${args.join(' + ')} = 1)`),
};
},
};
export default pg;

Loading…
Cancel
Save