UNPKG

@qrvey/formula-lang

Version:

QFormula support for qrvey projects

71 lines 2.3 kB
export function toDate(value) { return `CAST('${value}' AS TIMESTAMP)`; } 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 }); } export 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); } export 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 }); } export 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); } //# sourceMappingURL=scripts.js.map