Browse Source

feat: cast values to string if there are different types values in IF

pull/7268/head
Pranav C 11 months ago
parent
commit
19be58a420
  1. 54
      packages/nocodb/src/db/functionMappings/commonFns.ts
  2. 9
      packages/nocodb/src/db/functionMappings/mysql.ts
  3. 9
      packages/nocodb/src/db/functionMappings/pg.ts

54
packages/nocodb/src/db/functionMappings/commonFns.ts

@ -67,24 +67,52 @@ export default {
}; };
}, },
IF: async (args: MapFnArgs) => { IF: async (args: MapFnArgs) => {
const cond = await treatArgAsConditionalExp(args); const cond = (await treatArgAsConditionalExp(args)).builder;
let thenArg;
let elseArg;
const returnArgsType = new Set(
[args.pt.arguments[1].dataType, args.pt.arguments[2].dataType].filter(
(type) => type !== FormulaDataTypes.NULL,
),
);
// cast to string if the return value types are different
if (returnArgsType.size > 1) {
thenArg = (
await args.fn({
type: 'CallExpression',
arguments: [args.pt.arguments[1]],
callee: {
type: 'Identifier',
name: 'STRING',
},
} as any)
).builder;
elseArg = (
await args.fn({
type: 'CallExpression',
arguments: [args.pt.arguments[2]],
callee: {
type: 'Identifier',
name: 'STRING',
},
} as any)
).builder;
} else {
thenArg = (await args.fn(args.pt.arguments[1])).builder.toQuery();
elseArg = (await args.fn(args.pt.arguments[1])).builder.toQuery();
}
let query = args.knex let query = args.knex.raw(`\n\tWHEN ${cond} THEN ${thenArg}`).toQuery();
.raw(
`\n\tWHEN ${cond} THEN ${(
await args.fn(args.pt.arguments[1])
).builder.toQuery()}`,
)
.toQuery();
if (args.pt.arguments[2]) { if (args.pt.arguments[2]) {
query += args.knex query += args.knex.raw(`\n\tELSE ${elseArg}`).toQuery();
.raw(
`\n\tELSE ${(await args.fn(args.pt.arguments[2])).builder.toQuery()}`,
)
.toQuery();
} }
return { builder: args.knex.raw(`CASE ${query}\n END${args.colAlias}`) }; return { builder: args.knex.raw(`CASE ${query}\n END${args.colAlias}`) };
}, },
// used only for casting to string internally, this one is dummy function
// and will work as fallback for dbs which don't support/implemented CAST
STRING(args: MapFnArgs) {
return args.fn(args.pt?.arguments?.[0]);
},
AND: async (args: MapFnArgs) => { AND: async (args: MapFnArgs) => {
return { return {
builder: args.knex.raw( builder: args.knex.raw(

9
packages/nocodb/src/db/functionMappings/mysql.ts

@ -160,6 +160,15 @@ END) ${colAlias}`,
), ),
}; };
}, },
STRING: async (args: MapFnArgs) => {
return {
builder: args.knex.raw(
`CAST(${(await args.fn(args.pt.arguments[0])).builder} AS CHAR) ${
args.colAlias
}`,
),
};
},
}; };
export default mysql2; export default mysql2;

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

@ -299,6 +299,15 @@ END) ${colAlias}`,
), ),
}; };
}, },
STRING: async (args: MapFnArgs) => {
return {
builder: args.knex.raw(
`(${(await args.fn(args.pt.arguments[0])).builder})::text ${
args.colAlias
}`,
),
};
},
}; };
export default pg; export default pg;

Loading…
Cancel
Save