UNPKG

@qrvey/formula-lang

Version:

QFormula support for qrvey projects

78 lines 2.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.dateDifDatabricks = exports.dateDifPostgres = exports.dateDif = exports.toDate = void 0; function toDate(value) { return `CAST('${value}' AS TIMESTAMP)`; } exports.toDate = toDate; function generalDateDif(start, end, unit, omitQuotesForUnit = false) { const units = { Y: 'year', M: 'month', D: 'day', H: 'hour', MI: 'minute', S: 'second', }; const currentUnit = units[unit]; return omitQuotesForUnit ? `DATEDIFF(${currentUnit}, ${start}, ${end})` : `DATEDIFF('${currentUnit}', ${start}, ${end})`; } function customDateDifYear({ start, end, yearDif }) { const customFun = (date) => { return `EXTRACT(MONTH FROM ${date}) * 100 + EXTRACT(DAY FROM ${date})`; }; const startComparison = customFun(start); const endComparison = customFun(end); const result = `CASE WHEN ${startComparison} <= ${endComparison} THEN ${yearDif} ELSE ${yearDif} - 1 END`; return result; } function commonSQLDifYear(start, end) { const yearDif = generalDateDif(start, end, 'Y'); return customDateDifYear({ start, end, yearDif }); } function dateDif(start, end, unit) { const units = { Y: commonSQLDifYear, M: generalDateDif, D: generalDateDif, H: generalDateDif, MI: generalDateDif, S: generalDateDif, }; return units[unit](start, end, unit); } exports.dateDif = dateDif; function dateDifPostgres(start, end, unit) { if (unit === 'D') return `DATE_PART('day', ${end} - ${start})`; const yearDif = `DATE_PART('year', ${end}) - DATE_PART('year', ${start})`; if (unit === 'M') { return `(${yearDif}) * 12 + (DATE_PART('month', ${end}) - DATE_PART('month', ${start}))`; } if (unit === 'H') { return `EXTRACT(EPOCH FROM (${end} - ${start})) / 3600`; } if (unit === 'MI') { return `EXTRACT(EPOCH FROM (${end} - ${start})) / 60`; } if (unit === 'S') { return `EXTRACT(EPOCH FROM (${end} - ${start}))`; } return customDateDifYear({ start, end, yearDif }); } exports.dateDifPostgres = dateDifPostgres; function dateDifDatabricks(start, end, unit) { if (unit === 'Y') { const yearDif = generalDateDif(start, end, 'Y', true); return customDateDifYear({ start, end, yearDif }); } return generalDateDif(start, end, unit, true); } exports.dateDifDatabricks = dateDifDatabricks; //# sourceMappingURL=scripts.js.map