Browse Source

Merge pull request #7279 from nocodb/nc-fix/formula-concat

fix: Formula followup bugs
pull/7243/head
Raju Udava 11 months ago committed by GitHub
parent
commit
3714cb0a79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      packages/nocodb-sdk/src/lib/formulaHelpers.ts
  2. 2
      packages/nocodb/src/db/BaseModelSqlv2.ts
  3. 33
      packages/nocodb/src/db/formulav2/formulaQueryBuilderv2.ts

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

@ -511,7 +511,6 @@ export const formulas: Record<string, FormulaMeta> = {
validation: { validation: {
args: { args: {
min: 1, min: 1,
type: FormulaDataTypes.STRING,
}, },
}, },
description: 'Concatenated string of input parameters', description: 'Concatenated string of input parameters',
@ -1428,7 +1427,10 @@ export async function validateFormulaAndExtractTreeWithType({
if (parsedTree.type === JSEPNode.CALL_EXP) { if (parsedTree.type === JSEPNode.CALL_EXP) {
const calleeName = parsedTree.callee.name.toUpperCase(); const calleeName = parsedTree.callee.name.toUpperCase();
// validate function name // validate function name
if (!formulas[calleeName] || sqlUI?.getUnsupportedFnList().includes(calleeName)) { if (
!formulas[calleeName] ||
sqlUI?.getUnsupportedFnList().includes(calleeName)
) {
throw new FormulaError( throw new FormulaError(
FormulaErrorType.INVALID_FUNCTION_NAME, FormulaErrorType.INVALID_FUNCTION_NAME,
{}, {},
@ -1505,15 +1507,20 @@ export async function validateFormulaAndExtractTreeWithType({
argPt.dataType !== FormulaDataTypes.UNKNOWN argPt.dataType !== FormulaDataTypes.UNKNOWN
) { ) {
if (argPt.type === JSEPNode.IDENTIFIER) { if (argPt.type === JSEPNode.IDENTIFIER) {
const name =
columns?.find(
(c) => c.id === argPt.name || c.title === argPt.name
)?.title || argPt.name;
throw new FormulaError( throw new FormulaError(
FormulaErrorType.INVALID_ARG, FormulaErrorType.INVALID_ARG,
{ {
key: 'msg.formula.columnWithTypeFoundButExpected', key: 'msg.formula.columnWithTypeFoundButExpected',
columnName: parsedTree.name, columnName: name,
columnType: parsedTree.dataType, columnType: argPt.dataType,
expectedType: expectedArgType, expectedType: expectedArgType,
}, },
`Field ${parsedTree.name} with ${parsedTree.dataType} type is found but ${expectedArgType} type is expected` `Field ${name} with ${argPt.dataType} type is found but ${expectedArgType} type is expected`
); );
} else { } else {
let key = '', let key = '',

2
packages/nocodb/src/db/BaseModelSqlv2.ts

@ -5909,7 +5909,7 @@ function getCompositePk(primaryKeys: Column[], row) {
return primaryKeys.map((c) => row[c.title]).join('___'); return primaryKeys.map((c) => row[c.title]).join('___');
} }
function haveFormulaColumn(columns: Column[]) { export function haveFormulaColumn(columns: Column[]) {
return columns.some((c) => c.uidt === UITypes.Formula); return columns.some((c) => c.uidt === UITypes.Formula);
} }

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

@ -1057,23 +1057,24 @@ export default async function formulaQueryBuilderv2(
const knex = baseModelSqlv2.dbDriver; const knex = baseModelSqlv2.dbDriver;
// register jsep curly hook once only // register jsep curly hook once only
jsep.plugins.register(jsepCurlyHook); jsep.plugins.register(jsepCurlyHook);
// generate qb let qb;
const qb = await _formulaQueryBuilder( try {
baseModelSqlv2, // generate qb
_tree, qb = await _formulaQueryBuilder(
alias, baseModelSqlv2,
model, _tree,
aliasToColumn, alias,
tableAlias, model,
parsedTree ?? aliasToColumn,
(await column tableAlias,
?.getColOptions<FormulaColumn>() parsedTree ??
.then((formula) => formula?.getParsedTree())), (await column
); ?.getColOptions<FormulaColumn>()
.then((formula) => formula?.getParsedTree())),
);
if (!validateFormula) return qb; if (!validateFormula) return qb;
try {
// dry run qb.builder to see if it will break the grid view or not // dry run qb.builder to see if it will break the grid view or not
// if so, set formula error and show empty selectQb instead // if so, set formula error and show empty selectQb instead
await baseModelSqlv2.execAndParse( await baseModelSqlv2.execAndParse(
@ -1095,6 +1096,8 @@ export default async function formulaQueryBuilderv2(
} }
} }
} catch (e) { } catch (e) {
if (!validateFormula) throw e;
console.error(e); console.error(e);
if (column) { if (column) {
const formula = await column.getColOptions<FormulaColumn>(); const formula = await column.getColOptions<FormulaColumn>();

Loading…
Cancel
Save