UNPKG

@qrvey/formula-lang

Version:

QFormula support for qrvey projects

76 lines 2.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MID = void 0; const constants_1 = require("../constants"); const utils_1 = require("../utils"); const greaterThanOrEqualToReference_1 = require("../utils/greaterThanOrEqualToReference"); const isInteger_1 = require("../utils/isInteger"); /** * `MID` Returns a segment of a string. */ exports.MID = { identifier: 'MID', parameters: [ { identifier: 'CURRENT', optional: false, expectedPrimitive: constants_1.AST_PRIMITIVES.STRING, validator: [utils_1.isStringParam], }, { identifier: 'STARTING_AT', optional: false, expectedPrimitive: constants_1.AST_PRIMITIVES.NUMBER, validator: [utils_1.isNumberParam, isInteger_1.isInteger], }, { identifier: 'EXTRACT_LENGTH', optional: false, expectedPrimitive: constants_1.AST_PRIMITIVES.NUMBER, validator: [ utils_1.isNumberParam, (0, greaterThanOrEqualToReference_1.greaterThanOrEqualToReference)(0), isInteger_1.isInteger, ], }, ], transpiler: { elasticsearch, snowflake, redshift, postgresql, }, primitiveResult: constants_1.AST_PRIMITIVES.STRING, }; function elasticsearch(current, start, length) { return `${constants_1.ELASTICSEARCH_SCRIPT_NAMES.subString}(${current}, ${start}, ${length})`; } function sqlRound(value) { return `(FLOOR(ABS(${value})) * (CASE WHEN ${value} < 0 THEN -1 ELSE 1 END))`; } function snowflake(current, start, length) { return `SUBSTRING(${current}, ${sqlRound(start)} , ${sqlRound(length)})`; } function SQL(current, start, length) { return ` (case when ${current} IS NULL OR ${start} IS NULL OR ${length} IS NULL then NULL when (${sqlRound(length)} < 0) OR (${sqlRound(start)} * -1) > LENGTH(${current}) then '' else SUBSTRING(${current}, (case when ${sqlRound(start)} < 0 then (LENGTH(${current}) + ${sqlRound(start)} + 1) when ${sqlRound(start)} = 0 then 1 else ${sqlRound(start)} end)::INTEGER, ${sqlRound(length)}::INTEGER) end )`.trim(); } function redshift(current, start, length) { return SQL(current, start, length); } function postgresql(current, start, length) { return SQL(current, start, length); } //# sourceMappingURL=mid.js.map