Browse Source

fix(nocodb): convert date format based on meta in CONCAT

pull/5463/head
Wing-Kam Wong 2 years ago
parent
commit
64e24b1215
  1. 39
      packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/formulav2/formulaQueryBuilderv2.ts

39
packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/formulav2/formulaQueryBuilderv2.ts

@ -6,8 +6,9 @@ import FormulaColumn from '../../../../../models/FormulaColumn';
import { validateDateWithUnknownFormat } from '../helpers/formulaFnHelper'; import { validateDateWithUnknownFormat } from '../helpers/formulaFnHelper';
import { CacheGetType, CacheScope } from '../../../../../utils/globals'; import { CacheGetType, CacheScope } from '../../../../../utils/globals';
import NocoCache from '../../../../../cache/NocoCache'; import NocoCache from '../../../../../cache/NocoCache';
import Column from '../../../../../models/Column';
import { convertDateFormat } from '../helpers/convertDateFormat';
import type Model from '../../../../../models/Model'; import type Model from '../../../../../models/Model';
import type Column from '../../../../../models/Column';
import type RollupColumn from '../../../../../models/RollupColumn'; import type RollupColumn from '../../../../../models/RollupColumn';
import type { XKnex } from '../../../index'; import type { XKnex } from '../../../index';
import type LinkToAnotherRecordColumn from '../../../../../models/LinkToAnotherRecordColumn'; import type LinkToAnotherRecordColumn from '../../../../../models/LinkToAnotherRecordColumn';
@ -633,8 +634,42 @@ async function _formulaQueryBuilder(
`${pt.callee.name}(${( `${pt.callee.name}(${(
await Promise.all( await Promise.all(
pt.arguments.map(async (arg) => { pt.arguments.map(async (arg) => {
const query = (await fn(arg)).builder.toQuery(); let query = (await fn(arg)).builder.toQuery();
if (pt.callee.name === 'CONCAT') { if (pt.callee.name === 'CONCAT') {
if (
arg.type === 'Identifier' &&
arg.name in columnIdToUidt &&
columnIdToUidt[arg.name] === UITypes.Date
) {
const meta = (
await Column.get({
colId: arg.name,
})
).meta;
if (knex.clientType() === 'mysql2') {
query = `DATE_FORMAT(${query}, '${convertDateFormat(
meta.date_format,
knex.clientType()
)}')`;
} else if (knex.clientType() === 'pg') {
query = `TO_CHAR(${query}, '${convertDateFormat(
meta.date_format,
knex.clientType()
)}')`;
} else if (knex.clientType() === 'sqlite') {
query = `strftime('${convertDateFormat(
meta.date_format,
knex.clientType()
)}', ${query})`;
} else if (knex.clientType() === 'mssql') {
query = `FORMAT(${query}, '${convertDateFormat(
meta.date_format,
knex.clientType()
)}')`;
}
}
if (knex.clientType() === 'mysql2') { if (knex.clientType() === 'mysql2') {
// mysql2: CONCAT() returns NULL if any argument is NULL. // mysql2: CONCAT() returns NULL if any argument is NULL.
// adding IFNULL to convert NULL values to empty strings // adding IFNULL to convert NULL values to empty strings

Loading…
Cancel
Save