Browse Source

fix(nocodb): revise DATETIME_DIFF logic for sqlite

pull/4809/head
Wing-Kam Wong 2 years ago
parent
commit
061d27e4ba
  1. 50
      packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/functionMappings/sqlite.ts

50
packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/functionMappings/sqlite.ts

@ -83,19 +83,25 @@ const sqlite3 = {
); );
}, },
DATETIME_DIFF: ({ fn, knex, pt, colAlias }: MapFnArgs) => { DATETIME_DIFF: ({ fn, knex, pt, colAlias }: MapFnArgs) => {
let datetime_expr1 = fn(pt.arguments[0]).bindings[0]; let datetime_expr1 = fn(pt.arguments[0]);
let datetime_expr2 = fn(pt.arguments[1]).bindings[0]; let datetime_expr2 = fn(pt.arguments[1]);
// JULIANDAY takes YYYY-MM-DD // JULIANDAY takes YYYY-MM-DD
datetime_expr1 = convertToTargetFormat( if (datetime_expr1.sql === '?' && datetime_expr1.bindings?.[0]) {
datetime_expr1, datetime_expr1 = `'${convertToTargetFormat(
getDateFormat(datetime_expr1), datetime_expr1.bindings[0],
getDateFormat(datetime_expr1.bindings[0]),
'YYYY-MM-DD' 'YYYY-MM-DD'
); )}'`;
datetime_expr2 = convertToTargetFormat( }
datetime_expr2,
getDateFormat(datetime_expr2), if (datetime_expr2.sql === '?' && datetime_expr2.bindings?.[0]) {
datetime_expr2 = `'${convertToTargetFormat(
datetime_expr2.bindings[0],
getDateFormat(datetime_expr2.bindings[0]),
'YYYY-MM-DD' 'YYYY-MM-DD'
); )}'`;
}
const rawUnit = pt.arguments[2] const rawUnit = pt.arguments[2]
? fn(pt.arguments[2]).bindings[0] ? fn(pt.arguments[2]).bindings[0]
: 'seconds'; : 'seconds';
@ -103,36 +109,36 @@ const sqlite3 = {
const unit = convertUnits(rawUnit, 'sqlite'); const unit = convertUnits(rawUnit, 'sqlite');
switch (unit) { switch (unit) {
case 'seconds': case 'seconds':
sql = `ROUND((JULIANDAY('${datetime_expr1}') - JULIANDAY('${datetime_expr2}')) * 86400)`; sql = `ROUND((JULIANDAY(${datetime_expr1}) - JULIANDAY(${datetime_expr2})) * 86400)`;
break; break;
case 'minutes': case 'minutes':
sql = `ROUND((JULIANDAY('${datetime_expr1}') - JULIANDAY('${datetime_expr2}')) * 1440)`; sql = `ROUND((JULIANDAY(${datetime_expr1}) - JULIANDAY(${datetime_expr2})) * 1440)`;
break; break;
case 'hours': case 'hours':
sql = `ROUND((JULIANDAY('${datetime_expr1}') - JULIANDAY('${datetime_expr2}')) * 24)`; sql = `ROUND((JULIANDAY(${datetime_expr1}) - JULIANDAY(${datetime_expr2})) * 24)`;
break; break;
case 'milliseconds': case 'milliseconds':
sql = `ROUND((JULIANDAY('${datetime_expr1}') - JULIANDAY('${datetime_expr2}')) * 86400000)`; sql = `ROUND((JULIANDAY(${datetime_expr1}) - JULIANDAY(${datetime_expr2})) * 86400000)`;
break; break;
case 'weeks': case 'weeks':
sql = `ROUND((JULIANDAY('${datetime_expr1}') - JULIANDAY('${datetime_expr2}')) / 7)`; sql = `ROUND((JULIANDAY(${datetime_expr1}) - JULIANDAY(${datetime_expr2})) / 7)`;
break; break;
case 'months': case 'months':
sql = `(ROUND((JULIANDAY('${datetime_expr1}') - JULIANDAY('${datetime_expr2}')) / 365)) sql = `(ROUND((JULIANDAY(${datetime_expr1}) - JULIANDAY(${datetime_expr2})) / 365))
* 12 + (ROUND((JULIANDAY('${datetime_expr1}') - JULIANDAY('${datetime_expr2}')) / 365 / 12))`; * 12 + (ROUND((JULIANDAY(${datetime_expr1}) - JULIANDAY(${datetime_expr2})) / 365 / 12))`;
break; break;
case 'quarters': case 'quarters':
sql = ` sql = `
ROUND((JULIANDAY('${datetime_expr1}')) / 365 / 4) - ROUND((JULIANDAY(${datetime_expr1})) / 365 / 4) -
ROUND((JULIANDAY('${datetime_expr2}')) / 365 / 4) + ROUND((JULIANDAY(${datetime_expr2})) / 365 / 4) +
(ROUND((JULIANDAY('${datetime_expr1}') - JULIANDAY('${datetime_expr2}')) / 365)) * 4 (ROUND((JULIANDAY(${datetime_expr1}) - JULIANDAY(${datetime_expr2})) / 365)) * 4
`; `;
break; break;
case 'years': case 'years':
sql = `ROUND((JULIANDAY('${datetime_expr1}') - JULIANDAY('${datetime_expr2}')) / 365)`; sql = `ROUND((JULIANDAY(${datetime_expr1}) - JULIANDAY(${datetime_expr2})) / 365)`;
break; break;
case 'days': case 'days':
sql = `JULIANDAY('${datetime_expr1}') - JULIANDAY('${datetime_expr2}')`; sql = `JULIANDAY(${datetime_expr1}) - JULIANDAY(${datetime_expr2})`;
break; break;
default: default:
sql = ''; sql = '';

Loading…
Cancel
Save