@qrvey/formula-lang
Version:
QFormula support for qrvey projects
59 lines • 1.79 kB
JavaScript
import { AST_PRIMITIVES } from '../constants';
import { dateDif as ESdateDif } from '../utils/elasticsearch/scripts';
import { dateDif as SQLdateDif, dateDifPostgres } from '../utils/sql/scripts';
import { isDateParam, isStringParam, removeQuotes } from '../utils';
import { isAnAllowedValue } from '../utils/isAnAllowedValue';
/**
* Returns the number of the DATEDIF in a Date.
*/
export const DATEDIF = {
identifier: 'DATEDIF',
parameters: [
{
identifier: 'START_DATE',
optional: false,
expectedPrimitive: AST_PRIMITIVES.DATE,
validator: [isDateParam],
},
{
identifier: 'END_DATE',
optional: false,
expectedPrimitive: AST_PRIMITIVES.DATE,
validator: [isDateParam],
},
{
identifier: 'UNIT',
optional: false,
expectedPrimitive: AST_PRIMITIVES.STRING,
validator: [isStringParam, isAnAllowedValue(['Y', 'M', 'D'])],
},
],
transpiler: {
elasticsearch,
snowflake,
redshift,
postgresql,
},
primitiveResult: AST_PRIMITIVES.NUMBER,
};
function elasticsearch(start, end, unit) {
unit = removeQuotes(unit);
return ESdateDif(start, end, unit);
}
function SQL(start, end, unit) {
unit = removeQuotes(unit);
return SQLdateDif(start, end, unit);
}
function snowflake(start, end, unit) {
return SQL(start, end, unit);
}
function redshift(start, end, unit) {
const result = SQL(start, end, unit);
return `CAST(${result} AS INT)`;
}
function postgresql(start, end, unit) {
unit = removeQuotes(unit);
const result = dateDifPostgres(start, end, unit);
return `CAST(${result} AS INT)`;
}
//# sourceMappingURL=datedif.js.map