UNPKG

@qrvey/formula-lang

Version:

QFormula support for qrvey projects

69 lines 2.12 kB
import { AST_PRIMITIVES, OPERATION_SCOPE } from '../constants'; import { dateDif as ESdateDif } from '../utils/elasticsearch/scripts'; import { dateDif as SQLdateDif, dateDifDatabricks, 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', operationScope: OPERATION_SCOPE.RAW, functionScope: [OPERATION_SCOPE.RAW], 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', 'H', 'S', 'MI']), ], }, ], transpiler: { elasticsearch, snowflake, redshift, postgresql, databricks, }, 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)`; } function databricks(start, end, unit) { unit = removeQuotes(unit); return dateDifDatabricks(start, end, unit); } //# sourceMappingURL=datedif.js.map