Browse Source

feat(nocodb): add DATETIME_DIFF logic for mysql

pull/4629/head
Wing-Kam Wong 2 years ago
parent
commit
9d104016bd
  1. 21
      packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/functionMappings/mysql.ts

21
packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/functionMappings/mysql.ts

@ -1,6 +1,7 @@
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import { MapFnArgs } from '../mapFunctionName'; import { MapFnArgs } from '../mapFunctionName';
import commonFns from './commonFns'; import commonFns from './commonFns';
import { convertUnits } from '../helpers/convertUnits';
import { getWeekdayByText } from '../helpers/formulaFnHelper'; import { getWeekdayByText } from '../helpers/formulaFnHelper';
const mysql2 = { const mysql2 = {
@ -61,6 +62,26 @@ const mysql2 = {
END${colAlias}` END${colAlias}`
); );
}, },
DATETIME_DIFF: ({ fn, knex, pt, colAlias }: MapFnArgs) => {
const datetime_expr1 = fn(pt.arguments[0]);
const datetime_expr2 = fn(pt.arguments[1]);
const unit = convertUnits(
pt.arguments[2] ? fn(pt.arguments[2]).bindings[0] : 'seconds',
'mysql'
);
if (unit === 'MICROSECOND') {
// MySQL doesn't support millisecond
// hence change from MICROSECOND to millisecond manually
return knex.raw(
`TIMESTAMPDIFF(${unit}, ${datetime_expr2}, ${datetime_expr1}) div 1000 ${colAlias}`
);
}
return knex.raw(
`TIMESTAMPDIFF(${unit}, ${datetime_expr2}, ${datetime_expr1}) ${colAlias}`
);
},
WEEKDAY: ({ fn, knex, pt, colAlias }: MapFnArgs) => { WEEKDAY: ({ fn, knex, pt, colAlias }: MapFnArgs) => {
// WEEKDAY() returns an index from 0 to 6 for Monday to Sunday // WEEKDAY() returns an index from 0 to 6 for Monday to Sunday
return knex.raw( return knex.raw(

Loading…
Cancel
Save