Browse Source

fix: handling of negative intervals for DATEADD

re #1638

Signed-off-by: mertmit <mertmit99@gmail.com>
pull/1816/head
mertmit 3 years ago
parent
commit
f658d56467
  1. 17
      packages/nocodb/src/lib/dataMapper/lib/sql/formulav2/formulaQueryBuilderv2.ts
  2. 23
      packages/nocodb/src/lib/dataMapper/lib/sql/functionMappings/mssql.ts
  3. 16
      packages/nocodb/src/lib/dataMapper/lib/sql/functionMappings/mysql.ts
  4. 21
      packages/nocodb/src/lib/dataMapper/lib/sql/functionMappings/pg.ts
  5. 25
      packages/nocodb/src/lib/dataMapper/lib/sql/functionMappings/sqlite.ts

17
packages/nocodb/src/lib/dataMapper/lib/sql/formulav2/formulaQueryBuilderv2.ts

@ -564,15 +564,6 @@ export default async function formulaQueryBuilderv2(
} }
} }
break; break;
case 'DATEADD':
if (pt.arguments[1].value) {
pt.callee.name = 'DATE_ADD';
return fn(pt, alias, prevBinaryOp);
} else if (pt.arguments[1].operator == '-') {
pt.callee.name = 'DATE_SUB';
return fn(pt, alias, prevBinaryOp);
}
break;
case 'URL': case 'URL':
return fn( return fn(
{ {
@ -654,6 +645,14 @@ export default async function formulaQueryBuilderv2(
query.wrap('(', ')'); query.wrap('(', ')');
} }
return query; return query;
} else if (pt.type === 'UnaryExpression') {
const query = knex.raw(
`${pt.operator}${fn(pt.argument, null, pt.operator).toQuery()}${colAlias}`
);
if (prevBinaryOp && pt.operator !== prevBinaryOp) {
query.wrap('(', ')');
}
return query;
} }
}; };
return { builder: fn(tree, alias) }; return { builder: fn(tree, alias) };

23
packages/nocodb/src/lib/dataMapper/lib/sql/functionMappings/mssql.ts

@ -89,31 +89,16 @@ const mssql = {
.raw(`CAST(${args.fn(args.pt.arguments[0])} as FLOAT)${args.colAlias}`) .raw(`CAST(${args.fn(args.pt.arguments[0])} as FLOAT)${args.colAlias}`)
.wrap('(', ')'); .wrap('(', ')');
}, },
DATE_ADD: ({ fn, knex, pt, colAlias }: MapFnArgs) => { DATEADD: ({ fn, knex, pt, colAlias }: MapFnArgs) => {
const dateIN = fn(pt.arguments[1]);
return knex.raw( return knex.raw(
`CASE `CASE
WHEN ${fn(pt.arguments[0])} LIKE '%:%' THEN WHEN ${fn(pt.arguments[0])} LIKE '%:%' THEN
FORMAT(DATEADD(${String(fn(pt.arguments[2])).replace(/["']/g, '')}, FORMAT(DATEADD(${String(fn(pt.arguments[2])).replace(/["']/g, '')},
+${fn(pt.arguments[1])}, ${fn(pt.arguments[0])}), 'yyyy-MM-dd HH:mm') ${dateIN > 0 ? '+' : ''}${fn(pt.arguments[1])}, ${fn(pt.arguments[0])}), 'yyyy-MM-dd HH:mm')
ELSE ELSE
FORMAT(DATEADD(${String(fn(pt.arguments[2])).replace(/["']/g, '')}, FORMAT(DATEADD(${String(fn(pt.arguments[2])).replace(/["']/g, '')},
+${fn(pt.arguments[1])}, ${fn(pt.arguments[0])}), 'yyyy-MM-dd') ${dateIN > 0 ? '+' : ''}${fn(pt.arguments[1])}, ${fn(pt.arguments[0])}), 'yyyy-MM-dd')
END${colAlias}`
);
},
DATE_SUB: ({ fn, knex, pt, colAlias }: MapFnArgs) => {
return knex.raw(
`CASE
WHEN ${fn(pt.arguments[0])} LIKE '%:%' THEN
FORMAT(DATEADD(${String(fn(pt.arguments[2])).replace(/["']/g, '')},
-${fn(pt.arguments[1])}.argument.value, ${fn(
pt.arguments[0]
)}), 'yyyy-MM-dd HH:mm')
ELSE
FORMAT(DATEADD(${String(fn(pt.arguments[2])).replace(/["']/g, '')},
-${fn(pt.arguments[1])}.argument.value, ${fn(
pt.arguments[0]
)}), 'yyyy-MM-dd')
END${colAlias}` END${colAlias}`
); );
} }

16
packages/nocodb/src/lib/dataMapper/lib/sql/functionMappings/mysql.ts

@ -37,7 +37,7 @@ const mysql2 = {
.raw(`CAST(${args.fn(args.pt.arguments[0])} as DOUBLE)${args.colAlias}`) .raw(`CAST(${args.fn(args.pt.arguments[0])} as DOUBLE)${args.colAlias}`)
.wrap('(', ')'); .wrap('(', ')');
}, },
DATE_ADD: ({ fn, knex, pt, colAlias }: MapFnArgs) => { DATEADD: ({ fn, knex, pt, colAlias }: MapFnArgs) => {
return knex.raw( return knex.raw(
`CASE `CASE
WHEN ${fn(pt.arguments[0])} LIKE '%:%' THEN WHEN ${fn(pt.arguments[0])} LIKE '%:%' THEN
@ -54,20 +54,6 @@ const mysql2 = {
)})) )}))
END${colAlias}` END${colAlias}`
); );
},
DATE_SUB: ({ fn, knex, pt, colAlias }: MapFnArgs) => {
return knex.raw(
`CASE
WHEN ${fn(pt.arguments[0])} LIKE '%:%' THEN
DATE_FORMAT(DATE_ADD(${fn(pt.arguments[0])}, INTERVAL
${fn(pt.arguments[1])}.argument.value
${String(fn(pt.arguments[2])).replace(/["']/g, '')}), '%Y-%m-%d %H:%i')
ELSE
DATE(DATE_ADD(${fn(pt.arguments[0])}, INTERVAL
${fn(pt.arguments[1])}.argument.value
${String(fn(pt.arguments[2])).replace(/["']/g, '')}))
END${colAlias}`
);
} }
}; };

21
packages/nocodb/src/lib/dataMapper/lib/sql/functionMappings/pg.ts

@ -33,7 +33,7 @@ const pg = {
.raw(`CAST(${fn(pt.arguments[0])} as DOUBLE PRECISION)${colAlias}`) .raw(`CAST(${fn(pt.arguments[0])} as DOUBLE PRECISION)${colAlias}`)
.wrap('(', ')'); .wrap('(', ')');
}, },
DATE_ADD: ({ fn, knex, pt, colAlias }: MapFnArgs) => { DATEADD: ({ fn, knex, pt, colAlias }: MapFnArgs) => {
return knex.raw( return knex.raw(
`CASE `CASE
WHEN CAST(${fn(pt.arguments[0])} AS text) LIKE '%:%' THEN WHEN CAST(${fn(pt.arguments[0])} AS text) LIKE '%:%' THEN
@ -47,25 +47,6 @@ const pg = {
${String(fn(pt.arguments[2])).replace(/["']/g, '')}', 'YYYY-MM-DD') ${String(fn(pt.arguments[2])).replace(/["']/g, '')}', 'YYYY-MM-DD')
END${colAlias}` END${colAlias}`
); );
},
DATE_SUB: ({ fn, knex, pt, colAlias }: MapFnArgs) => {
return knex.raw(
`CASE
WHEN CAST(${fn(pt.arguments[0])} AS text) LIKE '%:%' THEN
to_char(${fn(pt.arguments[0])} - INTERVAL '${
fn(pt.arguments[1]).argument.value
}
${String(fn(pt.arguments[2])).replace(
/["']/g,
''
)}', 'YYYY-MM-DD HH24:MI')
ELSE
to_char(${fn(pt.arguments[0])} - INTERVAL '${
fn(pt.arguments[1]).argument.value
}
${String(fn(pt.arguments[2])).replace(/["']/g, '')}', 'YYYY-MM-DD')
END${colAlias}`
);
} }
}; };

25
packages/nocodb/src/lib/dataMapper/lib/sql/functionMappings/sqlite.ts

@ -56,43 +56,26 @@ const sqlite3 = {
.raw(`CAST(${args.fn(args.pt.arguments[0])} as FLOAT)${args.colAlias}`) .raw(`CAST(${args.fn(args.pt.arguments[0])} as FLOAT)${args.colAlias}`)
.wrap('(', ')'); .wrap('(', ')');
}, },
DATE_ADD: ({ fn, knex, pt, colAlias }: MapFnArgs) => { DATEADD: ({ fn, knex, pt, colAlias }: MapFnArgs) => {
const dateIN = fn(pt.arguments[1]);
return knex.raw( return knex.raw(
`CASE `CASE
WHEN ${fn(pt.arguments[0])} LIKE '%:%' THEN WHEN ${fn(pt.arguments[0])} LIKE '%:%' THEN
STRFTIME('%Y-%m-%d %H:%M', DATETIME(DATETIME(${fn( STRFTIME('%Y-%m-%d %H:%M', DATETIME(DATETIME(${fn(
pt.arguments[0] pt.arguments[0]
)}, 'localtime'), )}, 'localtime'),
'+${fn(pt.arguments[1])} ${String(fn(pt.arguments[2])).replace( '${dateIN > 0 ? '+' : ''}${fn(pt.arguments[1])} ${String(fn(pt.arguments[2])).replace(
/["']/g, /["']/g,
'' ''
)}')) )}'))
ELSE ELSE
DATE(DATETIME(${fn(pt.arguments[0])}, 'localtime'), DATE(DATETIME(${fn(pt.arguments[0])}, 'localtime'),
'+${fn(pt.arguments[1])} ${String(fn(pt.arguments[2])).replace( '${dateIN > 0 ? '+' : ''}${fn(pt.arguments[1])} ${String(fn(pt.arguments[2])).replace(
/["']/g, /["']/g,
'' ''
)}') )}')
END${colAlias}` END${colAlias}`
); );
},
DATE_SUB: ({ fn, knex, pt, colAlias }: MapFnArgs) => {
return knex.raw(
`CASE
WHEN ${fn(pt.arguments[0])} LIKE '%:%' THEN
STRFTIME('%Y-%m-%d %H:%M', DATETIME(DATETIME(${fn(
pt.arguments[0]
)}, 'localtime'),
'-${fn(pt.arguments[1]).argument.value} ${String(
fn(pt.arguments[2])
).replace(/["']/g, '')}'))
ELSE
DATE(DATETIME(${fn(pt.arguments[0])}, 'localtime'),
'-${fn(pt.arguments[1]).argument.value} ${String(
fn(pt.arguments[2])
).replace(/["']/g, '')}')
END${colAlias}`
);
} }
}; };

Loading…
Cancel
Save