Browse Source

refactor(nocodb): update formula functions to async and change return statement

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5392/head
Pranav C 2 years ago
parent
commit
88349195aa
  1. 200
      packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/functionMappings/mysql.ts
  2. 40
      packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/functionMappings/pg.ts
  3. 412
      packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/functionMappings/sqlite.ts

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

@ -1,99 +1,115 @@
// import dayjs from 'dayjs'; import dayjs from 'dayjs';
// import { convertUnits } from '../helpers/convertUnits'; import { convertUnits } from '../helpers/convertUnits';
// import { getWeekdayByText } from '../helpers/formulaFnHelper'; import { getWeekdayByText } from '../helpers/formulaFnHelper';
import commonFns from './commonFns'; import commonFns from './commonFns';
// import type { MapFnArgs } from '../mapFunctionName'; import type { MapFnArgs } from '../mapFunctionName';
const mysql2 = { const mysql2 = {
...commonFns, ...commonFns,
// LEN: 'CHAR_LENGTH', LEN: 'CHAR_LENGTH',
// MIN: 'LEAST', MIN: 'LEAST',
// MAX: 'GREATEST', MAX: 'GREATEST',
// SEARCH: (args: MapFnArgs) => { SEARCH: async (args: MapFnArgs) => {
// args.pt.callee.name = 'LOCATE'; args.pt.callee.name = 'LOCATE';
// const temp = args.pt.arguments[0]; const temp = args.pt.arguments[0];
// args.pt.arguments[0] = args.pt.arguments[1]; args.pt.arguments[0] = args.pt.arguments[1];
// args.pt.arguments[1] = temp; args.pt.arguments[1] = temp;
// }, },
// INT: (args: MapFnArgs) => { INT: async (args: MapFnArgs) => {
// return args.knex.raw( return {
// `CAST(${args.fn(args.pt.arguments[0])} as SIGNED)${args.colAlias}` builder: args.knex.raw(
// ); `CAST(${(await args.fn(args.pt.arguments[0])).builder} as SIGNED)${args.colAlias}`,
// }, ),
// LEFT: (args: MapFnArgs) => { };
// return args.knex.raw( },
// `SUBSTR(${args.fn(args.pt.arguments[0])},1,${args.fn( LEFT: async (args: MapFnArgs) => {
// args.pt.arguments[1] return {
// )})${args.colAlias}` builder: args.knex.raw(
// ); `SUBSTR(${(await args.fn(args.pt.arguments[0])).builder},1,${(await args.fn(
// }, args.pt.arguments[1],
// RIGHT: (args: MapFnArgs) => { )).builder})${args.colAlias}`,
// return args.knex.raw( ),
// `SUBSTR(${args.fn(args.pt.arguments[0])}, -(${args.fn( };
// args.pt.arguments[1] },
// )}))${args.colAlias}` RIGHT: async (args: MapFnArgs) => {
// ); return {
// }, builder: args.knex.raw(
// MID: 'SUBSTR', `SUBSTR(${(await args.fn(args.pt.arguments[0])).builder}, -(${(await args.fn(
// FLOAT: (args: MapFnArgs) => { args.pt.arguments[1],
// return args.knex )).builder}))${args.colAlias}`,
// .raw( ),
// `CAST(CAST(${args.fn(args.pt.arguments[0])} as CHAR) AS DOUBLE)${ };
// args.colAlias },
// }` MID: 'SUBSTR',
// ) FLOAT: async (args: MapFnArgs) => {
// .wrap('(', ')'); return {
// }, builder: args.knex
// DATEADD: ({ fn, knex, pt, colAlias }: MapFnArgs) => { .raw(
// return knex.raw( `CAST(CAST(${(await args.fn(args.pt.arguments[0])).builder} as CHAR) AS DOUBLE)${
// `CASE args.colAlias
// WHEN ${fn(pt.arguments[0])} LIKE '%:%' THEN }`,
// DATE_FORMAT(DATE_ADD(${fn(pt.arguments[0])}, INTERVAL )
// ${fn(pt.arguments[1])} ${String(fn(pt.arguments[2])).replace( .wrap('(', ')'),
// /["']/g, };
// '' },
// )}), '%Y-%m-%d %H:%i') DATEADD: async ({ fn, knex, pt, colAlias }: MapFnArgs) => {
// ELSE return {
// DATE(DATE_ADD(${fn(pt.arguments[0])}, INTERVAL builder: knex.raw(
// ${fn(pt.arguments[1])} ${String(fn(pt.arguments[2])).replace( `CASE
// /["']/g, WHEN ${(await fn(pt.arguments[0])).builder} LIKE '%:%' THEN
// '' DATE_FORMAT(DATE_ADD(${(await fn(pt.arguments[0])).builder}, INTERVAL
// )})) ${(await fn(pt.arguments[1])).builder} ${String((await fn(pt.arguments[2])).builder).replace(
// END${colAlias}` /["']/g,
// ); '',
// }, )}), '%Y-%m-%d %H:%i')
// DATETIME_DIFF: ({ fn, knex, pt, colAlias }: MapFnArgs) => { ELSE
// const datetime_expr1 = fn(pt.arguments[0]); DATE(DATE_ADD(${(await fn(pt.arguments[0])).builder}, INTERVAL
// const datetime_expr2 = fn(pt.arguments[1]); ${(await fn(pt.arguments[1])).builder} ${String((await fn(pt.arguments[2])).builder).replace(
// /["']/g,
// const unit = convertUnits( '',
// pt.arguments[2] ? fn(pt.arguments[2]).bindings[0] : 'seconds', )}))
// 'mysql' END${colAlias}`,
// ); ),
// };
// if (unit === 'MICROSECOND') { },
// // MySQL doesn't support millisecond DATETIME_DIFF: async ({ fn, knex, pt, colAlias }: MapFnArgs) => {
// // hence change from MICROSECOND to millisecond manually const datetime_expr1 = (await fn(pt.arguments[0])).builder;
// return knex.raw( const datetime_expr2 = (await fn(pt.arguments[1])).builder;
// `TIMESTAMPDIFF(${unit}, ${datetime_expr2}, ${datetime_expr1}) div 1000 ${colAlias}`
// ); const unit = convertUnits(
// } pt.arguments[2] ? (await fn(pt.arguments[2])).builder.bindings[0] : 'seconds',
// return knex.raw( 'mysql'
// `TIMESTAMPDIFF(${unit}, ${datetime_expr2}, ${datetime_expr1}) ${colAlias}` );
// );
// }, if (unit === 'MICROSECOND') {
// WEEKDAY: ({ fn, knex, pt, colAlias }: MapFnArgs) => { // MySQL doesn't support millisecond
// // WEEKDAY() returns an index from 0 to 6 for Monday to Sunday // hence change from MICROSECOND to millisecond manually
// return knex.raw( return {
// `(WEEKDAY(${ builder: knex.raw(
// pt.arguments[0].type === 'Literal' `TIMESTAMPDIFF(${unit}, ${datetime_expr2}, ${datetime_expr1}) div 1000 ${colAlias}`,
// ? `'${dayjs(fn(pt.arguments[0])).format('YYYY-MM-DD')}'` ),
// : fn(pt.arguments[0]) };
// }) - ${getWeekdayByText( }
// pt?.arguments[1]?.value return {
// )} % 7 + 7) % 7 ${colAlias}` builder: knex.raw(
// ); `TIMESTAMPDIFF(${unit}, ${datetime_expr2}, ${datetime_expr1}) ${colAlias}`,
// }, ),
};
},
WEEKDAY: async ({ fn, knex, pt, colAlias }: MapFnArgs) => {
// WEEKDAY() returns an index from 0 to 6 for Monday to Sunday
return {
builder: knex.raw(
`(WEEKDAY(${
pt.arguments[0].type === 'Literal'
? `'${dayjs((await fn(pt.arguments[0])).builder).format('YYYY-MM-DD')}'`
: (await fn(pt.arguments[0])).builder
}) - ${getWeekdayByText(
pt?.arguments[1]?.value,
)} % 7 + 7) % 7 ${colAlias}`,
),
};
},
}; };
export default mysql2; export default mysql2;

40
packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/functionMappings/pg.ts

@ -34,27 +34,27 @@ const pg = {
}; };
}, },
MID: 'SUBSTR', MID: 'SUBSTR',
FLOAT: ({ fn, knex, pt, colAlias }: MapFnArgs) => { FLOAT: async ({ fn, knex, pt, colAlias }: MapFnArgs) => {
return { return {
builder: knex builder: knex
.raw(`CAST(${fn(pt.arguments[0])} as DOUBLE PRECISION)${colAlias}`) .raw(`CAST(${(await fn(pt.arguments[0])).builder} as DOUBLE PRECISION)${colAlias}`)
.wrap('(', ')'), .wrap('(', ')'),
}; };
}, },
ROUND: ({ fn, knex, pt, colAlias }: MapFnArgs) => { ROUND: async ({ fn, knex, pt, colAlias }: MapFnArgs) => {
return { return {
builder: knex.raw( builder: knex.raw(
`ROUND((${fn(pt.arguments[0])})::numeric, ${ `ROUND((${(await fn(pt.arguments[0])).builder})::numeric, ${
pt?.arguments[1] ? fn(pt.arguments[1]) : 0 pt?.arguments[1] ? (await fn(pt.arguments[1])).builder : 0
}) ${colAlias}` }) ${colAlias}`
), ),
}; };
}, },
DATEADD: ({ fn, knex, pt, colAlias }: MapFnArgs) => { DATEADD: async ({ fn, knex, pt, colAlias }: MapFnArgs) => {
return { return {
builder: knex.raw( builder: knex.raw(
`${fn(pt.arguments[0])} + (${fn(pt.arguments[1])} || `${(await fn(pt.arguments[0])).builder} + (${(await fn(pt.arguments[1])).builder} ||
'${String(fn(pt.arguments[2])).replace( '${String((await fn(pt.arguments[2])).builder).replace(
/["']/g, /["']/g,
'' ''
)}')::interval${colAlias}` )}')::interval${colAlias}`
@ -62,8 +62,8 @@ const pg = {
}; };
}, },
DATETIME_DIFF: async ({ fn, knex, pt, colAlias }: MapFnArgs) => { DATETIME_DIFF: async ({ fn, knex, pt, colAlias }: MapFnArgs) => {
const datetime_expr1 = fn(pt.arguments[0]); const datetime_expr1 = (await fn(pt.arguments[0])).builder;
const datetime_expr2 = fn(pt.arguments[1]); const datetime_expr2 = (await fn(pt.arguments[1])).builder;
const rawUnit = pt.arguments[2] const rawUnit = pt.arguments[2]
? (await fn(pt.arguments[2])).builder.bindings[0] ? (await fn(pt.arguments[2])).builder.bindings[0]
: 'seconds'; : 'seconds';
@ -121,7 +121,7 @@ const pg = {
? `date '${dayjs((await fn(pt.arguments[0])).builder).format( ? `date '${dayjs((await fn(pt.arguments[0])).builder).format(
'YYYY-MM-DD' 'YYYY-MM-DD'
)}'` )}'`
: fn(pt.arguments[0]) : (await fn(pt.arguments[0])).builder
}) - 1 - ${getWeekdayByText( }) - 1 - ${getWeekdayByText(
pt?.arguments[1]?.value pt?.arguments[1]?.value
)} % 7 + 7) ::INTEGER % 7 ${colAlias}` )} % 7 + 7) ::INTEGER % 7 ${colAlias}`
@ -133,10 +133,10 @@ const pg = {
builder: args.knex.raw( builder: args.knex.raw(
`CASE WHEN ${args.knex `CASE WHEN ${args.knex
.raw( .raw(
`${args.pt.arguments `${(await Promise.all(args.pt.arguments
.map(async (ar) => .map(async (ar) =>
(await args.fn(ar, '', 'AND')).builder.toQuery() (await args.fn(ar, '', 'AND')).builder.toQuery()
) )))
.join(' AND ')}` .join(' AND ')}`
) )
.wrap('(', ')') .wrap('(', ')')
@ -160,10 +160,10 @@ const pg = {
), ),
}; };
}, },
SUBSTR: ({ fn, knex, pt, colAlias }: MapFnArgs) => { SUBSTR:async ({ fn, knex, pt, colAlias }: MapFnArgs) => {
const str = fn(pt.arguments[0]); const str = (await fn(pt.arguments[0])).builder;
const positionFrom = fn(pt.arguments[1] ?? 1); const positionFrom = (await fn(pt.arguments[1] ?? 1)).builder;
const numberOfCharacters = fn(pt.arguments[2] ?? ''); const numberOfCharacters = (await fn(pt.arguments[2] ?? '')).builder;
return { return {
builder: knex.raw( builder: knex.raw(
`SUBSTR(${str}::TEXT, ${positionFrom}${ `SUBSTR(${str}::TEXT, ${positionFrom}${
@ -172,9 +172,9 @@ const pg = {
), ),
}; };
}, },
MOD: ({ fn, knex, pt, colAlias }: MapFnArgs) => { MOD: async ({ fn, knex, pt, colAlias }: MapFnArgs) => {
const x = fn(pt.arguments[0]); const x = (await fn(pt.arguments[0])).builder;
const y = fn(pt.arguments[1]); const y = (await fn(pt.arguments[1])).builder;
return { return {
builder: knex.raw(`MOD((${x})::NUMERIC, (${y})::NUMERIC) ${colAlias}`), builder: knex.raw(`MOD((${x})::NUMERIC, (${y})::NUMERIC) ${colAlias}`),
}; };

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

@ -1,198 +1,228 @@
// import dayjs from 'dayjs'; import dayjs from 'dayjs';
// import { convertUnits } from '../helpers/convertUnits'; import { convertUnits } from '../helpers/convertUnits';
// import { getWeekdayByText } from '../helpers/formulaFnHelper'; import { getWeekdayByText } from '../helpers/formulaFnHelper';
// import { import {
// convertToTargetFormat, convertToTargetFormat,
// getDateFormat, getDateFormat,
// } from '../../../../../utils/dateTimeUtils'; } from '../../../../../utils/dateTimeUtils';
import commonFns from './commonFns'; import commonFns from './commonFns';
// import type { MapFnArgs } from '../mapFunctionName'; import type { MapFnArgs } from '../mapFunctionName';
const sqlite3 = { const sqlite3 = {
...commonFns, ...commonFns,
// LEN: 'LENGTH', LEN: 'LENGTH',
// CEILING(args) { async CEILING(args) {
// return args.knex.raw( return args.knex.raw(
// `round(${args.fn(args.pt.arguments[0])} + 0.5)${args.colAlias}` `round(${(await args.fn(args.pt.arguments[0])).builder} + 0.5)${
// ); args.colAlias
// }, }`
// FLOOR(args) { );
// return args.knex.raw( },
// `round(${args.fn(args.pt.arguments[0])} - 0.5)${args.colAlias}` async FLOOR(args) {
// ); return args.knex.raw(
// }, `round(${(await args.fn(args.pt.arguments[0])).builder} - 0.5)${
// MOD: (args: MapFnArgs) => { args.colAlias
// return args.fn({ }`
// type: 'BinaryExpression', );
// operator: '%', },
// left: args.pt.arguments[0], MOD: async (args: MapFnArgs) => {
// right: args.pt.arguments[1], return (
// }); await args.fn({
// }, type: 'BinaryExpression',
// REPEAT(args: MapFnArgs) { operator: '%',
// return args.knex.raw( left: args.pt.arguments[0],
// `replace(printf('%.' || ${args.fn( right: args.pt.arguments[1],
// args.pt.arguments[1] })
// )} || 'c', '/'),'/',${args.fn(args.pt.arguments[0])})${args.colAlias}` ).builder;
// ); },
// }, async REPEAT(args: MapFnArgs) {
// NOW: 'DATE', return args.knex.raw(
// SEARCH: 'INSTR', `replace(printf('%.' || ${
// INT(args: MapFnArgs) { (await args.fn(args.pt.arguments[1])).builder
// return args.knex.raw( } || 'c', '/'),'/',${(await args.fn(args.pt.arguments[0])).builder})${
// `CAST(${args.fn(args.pt.arguments[0])} as INTEGER)${args.colAlias}` args.colAlias
// ); }`
// }, );
// LEFT: (args: MapFnArgs) => { },
// return args.knex.raw( NOW: 'DATE',
// `SUBSTR(${args.fn(args.pt.arguments[0])},1,${args.fn( SEARCH: 'INSTR',
// args.pt.arguments[1] async INT(args: MapFnArgs) {
// )})${args.colAlias}` return args.knex.raw(
// ); `CAST(${(await args.fn(args.pt.arguments[0])).builder} as INTEGER)${
// }, args.colAlias
// RIGHT: (args: MapFnArgs) => { }`
// return args.knex.raw( );
// `SUBSTR(${args.fn(args.pt.arguments[0])},-(${args.fn( },
// args.pt.arguments[1] LEFT: async (args: MapFnArgs) => {
// )}))${args.colAlias}` return args.knex.raw(
// ); `SUBSTR(${(await args.fn(args.pt.arguments[0])).builder},1,${
// }, (await args.fn(args.pt.arguments[1])).builder
// MID: 'SUBSTR', })${args.colAlias}`
// FLOAT: (args: MapFnArgs) => { );
// return args.knex },
// .raw(`CAST(${args.fn(args.pt.arguments[0])} as FLOAT)${args.colAlias}`) RIGHT: async (args: MapFnArgs) => {
// .wrap('(', ')'); return args.knex.raw(
// }, `SUBSTR(${(await args.fn(args.pt.arguments[0])).builder},-(${
// DATEADD: ({ fn, knex, pt, colAlias }: MapFnArgs) => { (await args.fn(args.pt.arguments[1])).builder
// const dateIN = fn(pt.arguments[1]); }))${args.colAlias}`
// return knex.raw( );
// `CASE },
// WHEN ${fn(pt.arguments[0])} LIKE '%:%' THEN MID: 'SUBSTR',
// STRFTIME('%Y-%m-%d %H:%M', DATETIME(DATETIME(${fn( FLOAT: async (args: MapFnArgs) => {
// pt.arguments[0] return args.knex
// )}, 'localtime'), .raw(
// ${dateIN > 0 ? '+' : ''}${fn(pt.arguments[1])} || ' ${String( `CAST(${(await args.fn(args.pt.arguments[0])).builder} as FLOAT)${
// fn(pt.arguments[2]) args.colAlias
// ).replace(/["']/g, '')}')) }`
// ELSE )
// DATE(DATETIME(${fn(pt.arguments[0])}, 'localtime'), .wrap('(', ')');
// ${dateIN > 0 ? '+' : ''}${fn(pt.arguments[1])} || ' ${String( },
// fn(pt.arguments[2]) DATEADD: async ({ fn, knex, pt, colAlias }: MapFnArgs) => {
// ).replace(/["']/g, '')}') const dateIN = (await fn(pt.arguments[1])).builder;
// END${colAlias}` return knex.raw(
// ); `CASE
// }, WHEN ${(await fn(pt.arguments[0])).builder} LIKE '%:%' THEN
// DATETIME_DIFF: ({ fn, knex, pt, colAlias }: MapFnArgs) => { STRFTIME('%Y-%m-%d %H:%M', DATETIME(DATETIME(${fn(
// let datetime_expr1 = fn(pt.arguments[0]); pt.arguments[0]
// let datetime_expr2 = fn(pt.arguments[1]); )}, 'localtime'),
// // JULIANDAY takes YYYY-MM-DD ${dateIN > 0 ? '+' : ''}${
// if (datetime_expr1.sql === '?' && datetime_expr1.bindings?.[0]) { (await fn(pt.arguments[1])).builder
// datetime_expr1 = `'${convertToTargetFormat( } || ' ${String((await fn(pt.arguments[2])).builder).replace(
// datetime_expr1.bindings[0], /["']/g,
// getDateFormat(datetime_expr1.bindings[0]), ''
// 'YYYY-MM-DD' )}'))
// )}'`; ELSE
// } DATE(DATETIME(${(await fn(pt.arguments[0])).builder}, 'localtime'),
// ${dateIN > 0 ? '+' : ''}${
// if (datetime_expr2.sql === '?' && datetime_expr2.bindings?.[0]) { (await fn(pt.arguments[1])).builder
// datetime_expr2 = `'${convertToTargetFormat( } || ' ${String((await fn(pt.arguments[2])).builder).replace(
// datetime_expr2.bindings[0], /["']/g,
// getDateFormat(datetime_expr2.bindings[0]), ''
// 'YYYY-MM-DD' )}')
// )}'`; END${colAlias}`
// } );
// },
// const rawUnit = pt.arguments[2] DATETIME_DIFF: async ({ fn, knex, pt, colAlias }: MapFnArgs) => {
// ? fn(pt.arguments[2]).bindings[0] let datetime_expr1 = (await fn(pt.arguments[0])).builder;
// : 'seconds'; let datetime_expr2 = (await fn(pt.arguments[1])).builder;
// let sql; // JULIANDAY takes YYYY-MM-DD
// const unit = convertUnits(rawUnit, 'sqlite'); if (datetime_expr1.sql === '?' && datetime_expr1.bindings?.[0]) {
// switch (unit) { datetime_expr1 = `'${convertToTargetFormat(
// case 'seconds': datetime_expr1.bindings[0],
// sql = `(strftime('%s', ${datetime_expr1}) - strftime('%s', ${datetime_expr2}))`; getDateFormat(datetime_expr1.bindings[0]),
// break; 'YYYY-MM-DD'
// case 'minutes': )}'`;
// sql = `(strftime('%s', ${datetime_expr1}) - strftime('%s', ${datetime_expr2})) / 60`; }
// break;
// case 'hours': if (datetime_expr2.sql === '?' && datetime_expr2.bindings?.[0]) {
// sql = `(strftime('%s', ${datetime_expr1}) - strftime('%s', ${datetime_expr2})) / 3600`; datetime_expr2 = `'${convertToTargetFormat(
// break; datetime_expr2.bindings[0],
// case 'milliseconds': getDateFormat(datetime_expr2.bindings[0]),
// sql = `(strftime('%s', ${datetime_expr1}) - strftime('%s', ${datetime_expr2})) * 1000`; 'YYYY-MM-DD'
// break; )}'`;
// case 'weeks': }
// sql = `ROUND((JULIANDAY(${datetime_expr1}) - JULIANDAY(${datetime_expr2})) / 7)`;
// break; const rawUnit = pt.arguments[2]
// case 'months': ? (await fn(pt.arguments[2])).builder.bindings[0]
// sql = `(strftime('%Y', ${datetime_expr1}) - strftime('%Y', ${datetime_expr2})) * 12 + (strftime('%m', ${datetime_expr1}) - strftime('%m', ${datetime_expr2})) `; : 'seconds';
// break; let sql;
// case 'quarters': const unit = convertUnits(rawUnit, 'sqlite');
// sql = `(strftime('%Y', ${datetime_expr1}) - strftime('%Y', ${datetime_expr2})) * 4 + (strftime('%m', ${datetime_expr1}) - strftime('%m', ${datetime_expr2})) / 3`; switch (unit) {
// break; case 'seconds':
// case 'years': sql = `(strftime('%s', ${datetime_expr1}) - strftime('%s', ${datetime_expr2}))`;
// sql = `CASE break;
// WHEN (${datetime_expr2} < ${datetime_expr1}) THEN case 'minutes':
// ( sql = `(strftime('%s', ${datetime_expr1}) - strftime('%s', ${datetime_expr2})) / 60`;
// (strftime('%Y', ${datetime_expr1}) - strftime('%Y', ${datetime_expr2})) break;
// - (strftime('%m', ${datetime_expr1}) < strftime('%m', ${datetime_expr2}) case 'hours':
// OR (strftime('%m', ${datetime_expr1}) = strftime('%m', ${datetime_expr2}) sql = `(strftime('%s', ${datetime_expr1}) - strftime('%s', ${datetime_expr2})) / 3600`;
// AND strftime('%d', ${datetime_expr1}) < strftime('%d', ${datetime_expr2}))) break;
// ) case 'milliseconds':
// WHEN (${datetime_expr2} > ${datetime_expr1}) THEN sql = `(strftime('%s', ${datetime_expr1}) - strftime('%s', ${datetime_expr2})) * 1000`;
// -1 * ( break;
// (strftime('%Y', ${datetime_expr2}) - strftime('%Y', ${datetime_expr1})) case 'weeks':
// - (strftime('%m', ${datetime_expr2}) < strftime('%m', ${datetime_expr1}) sql = `ROUND((JULIANDAY(${datetime_expr1}) - JULIANDAY(${datetime_expr2})) / 7)`;
// OR (strftime('%m', ${datetime_expr2}) = strftime('%m', ${datetime_expr1}) break;
// AND strftime('%d', ${datetime_expr2}) < strftime('%d', ${datetime_expr1}))) case 'months':
// ) sql = `(strftime('%Y', ${datetime_expr1}) - strftime('%Y', ${datetime_expr2})) * 12 + (strftime('%m', ${datetime_expr1}) - strftime('%m', ${datetime_expr2})) `;
// ELSE 0 break;
// END`; case 'quarters':
// break; sql = `(strftime('%Y', ${datetime_expr1}) - strftime('%Y', ${datetime_expr2})) * 4 + (strftime('%m', ${datetime_expr1}) - strftime('%m', ${datetime_expr2})) / 3`;
// case 'days': break;
// sql = `JULIANDAY(${datetime_expr1}) - JULIANDAY(${datetime_expr2})`; case 'years':
// break; sql = `CASE
// default: WHEN (${datetime_expr2} < ${datetime_expr1}) THEN
// sql = ''; (
// } (strftime('%Y', ${datetime_expr1}) - strftime('%Y', ${datetime_expr2}))
// return knex.raw(`${sql} ${colAlias}`); - (strftime('%m', ${datetime_expr1}) < strftime('%m', ${datetime_expr2})
// }, OR (strftime('%m', ${datetime_expr1}) = strftime('%m', ${datetime_expr2})
// WEEKDAY: ({ fn, knex, pt, colAlias }: MapFnArgs) => { AND strftime('%d', ${datetime_expr1}) < strftime('%d', ${datetime_expr2})))
// // strftime('%w', date) - day of week 0 - 6 with Sunday == 0 )
// // WEEKDAY() returns an index from 0 to 6 for Monday to Sunday WHEN (${datetime_expr2} > ${datetime_expr1}) THEN
// return knex.raw( -1 * (
// `(strftime('%w', ${ (strftime('%Y', ${datetime_expr2}) - strftime('%Y', ${datetime_expr1}))
// pt.arguments[0].type === 'Literal' - (strftime('%m', ${datetime_expr2}) < strftime('%m', ${datetime_expr1})
// ? `'${dayjs(fn(pt.arguments[0])).format('YYYY-MM-DD')}'` OR (strftime('%m', ${datetime_expr2}) = strftime('%m', ${datetime_expr1})
// : fn(pt.arguments[0]) AND strftime('%d', ${datetime_expr2}) < strftime('%d', ${datetime_expr1})))
// }) - 1 - ${getWeekdayByText( )
// pt?.arguments[1]?.value ELSE 0
// )} % 7 + 7) % 7 ${colAlias}` END`;
// ); break;
// }, case 'days':
// AND: (args: MapFnArgs) => { sql = `JULIANDAY(${datetime_expr1}) - JULIANDAY(${datetime_expr2})`;
// return args.knex.raw( break;
// `CASE WHEN ${args.knex default:
// .raw( sql = '';
// `${args.pt.arguments }
// .map((ar) => args.fn(ar, '', 'AND').toQuery()) return knex.raw(`${sql} ${colAlias}`);
// .join(' AND ')}` },
// ) WEEKDAY: async ({ fn, knex, pt, colAlias }: MapFnArgs) => {
// .wrap('(', ')') // strftime('%w', date) - day of week 0 - 6 with Sunday == 0
// .toQuery()} THEN 1 ELSE 0 END ${args.colAlias}` // WEEKDAY() returns an index from 0 to 6 for Monday to Sunday
// ); return knex.raw(
// }, `(strftime('%w', ${
// OR: (args: MapFnArgs) => { pt.arguments[0].type === 'Literal'
// return args.knex.raw( ? `'${dayjs((await fn(pt.arguments[0])).builder).format(
// `CASE WHEN ${args.knex 'YYYY-MM-DD'
// .raw( )}'`
// `${args.pt.arguments : (await fn(pt.arguments[0])).builder
// .map((ar) => args.fn(ar, '', 'OR').toQuery()) }) - 1 - ${getWeekdayByText(
// .join(' OR ')}` pt?.arguments[1]?.value
// ) )} % 7 + 7) % 7 ${colAlias}`
// .wrap('(', ')') );
// .toQuery()} THEN 1 ELSE 0 END ${args.colAlias}` },
// ); AND: async (args: MapFnArgs) => {
// }, return args.knex.raw(
`CASE WHEN ${args.knex
.raw(
`${(
await Promise.all(
args.pt.arguments.map(async (ar) =>
(await args.fn(ar, '', 'AND')).builder.toQuery()
)
)
).join(' AND ')}`
)
.wrap('(', ')')
.toQuery()} THEN 1 ELSE 0 END ${args.colAlias}`
);
},
OR: async (args: MapFnArgs) => {
return args.knex.raw(
`CASE WHEN ${args.knex
.raw(
`${(
await Promise.all(
args.pt.arguments.map(async (ar) =>
(await args.fn(ar, '', 'OR')).builder.toQuery()
)
)
).join(' OR ')}`
)
.wrap('(', ')')
.toQuery()} THEN 1 ELSE 0 END ${args.colAlias}`
);
},
}; };
export default sqlite3; export default sqlite3;

Loading…
Cancel
Save