Browse Source

refactor(nocodb): use convertDateFormatForConcat & fix sqlite case

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

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

@ -3,11 +3,13 @@ import { jsepCurlyHook, UITypes } from 'nocodb-sdk';
import mapFunctionName from '../mapFunctionName';
import genRollupSelectv2 from '../genRollupSelectv2';
import FormulaColumn from '../../../../../models/FormulaColumn';
import { validateDateWithUnknownFormat } from '../helpers/formulaFnHelper';
import {
convertDateFormatForConcat,
validateDateWithUnknownFormat,
} from '../helpers/formulaFnHelper';
import { CacheGetType, CacheScope } from '../../../../../utils/globals';
import NocoCache from '../../../../../cache/NocoCache';
import Column from '../../../../../models/Column';
import { convertDateFormat } from '../helpers/convertDateFormat';
import type Column from '../../../../../models/Column';
import type Model from '../../../../../models/Model';
import type RollupColumn from '../../../../../models/RollupColumn';
import type { XKnex } from '../../../index';
@ -636,38 +638,15 @@ async function _formulaQueryBuilder(
pt.arguments.map(async (arg) => {
let query = (await fn(arg)).builder.toQuery();
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() !== 'sqlite3') {
query = await convertDateFormatForConcat(
arg,
columnIdToUidt,
query,
knex.clientType()
);
} else {
// sqlite3: special handling - See BinaryExpression
}
if (knex.clientType() === 'mysql2') {
@ -714,8 +693,8 @@ async function _formulaQueryBuilder(
pt.left.fnName = pt.left.fnName || 'ARITH';
pt.right.fnName = pt.right.fnName || 'ARITH';
const left = (await fn(pt.left, null, pt.operator)).builder.toQuery();
const right = (await fn(pt.right, null, pt.operator)).builder.toQuery();
let left = (await fn(pt.left, null, pt.operator)).builder.toQuery();
let right = (await fn(pt.right, null, pt.operator)).builder.toQuery();
let sql = `${left} ${pt.operator} ${right}${colAlias}`;
// comparing a date with empty string would throw
@ -759,8 +738,22 @@ async function _formulaQueryBuilder(
}
}
// handle NULL values when calling CONCAT for sqlite3
if (pt.left.fnName === 'CONCAT' && knex.clientType() === 'sqlite3') {
// handle date format
left = await convertDateFormatForConcat(
pt.left?.arguments?.[0],
columnIdToUidt,
left,
knex.clientType()
);
right = await convertDateFormatForConcat(
pt.right?.arguments?.[0],
columnIdToUidt,
right,
knex.clientType()
);
// handle NULL values when calling CONCAT for sqlite3
sql = `COALESCE(${left}, '') ${pt.operator} COALESCE(${right},'')${colAlias}`;
}

Loading…
Cancel
Save