UNPKG

sedk-mysql

Version:
121 lines 5.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConditionOperand = exports.Operand = void 0; const binder_1 = require("../binder"); const Expression_1 = require("./Expression"); const AggregateFunction_1 = require("../AggregateFunction"); const database_1 = require("../database"); const types_1 = require("./types"); const util_1 = require("../util"); const Condition_1 = require("./Condition"); class Operand { constructor(value, isNot = false) { this.value = value; this.isNot = isNot; this.value = value; this.type = Operand.getExpressionType(value); Operand.throwIfInvalidUseOfNot(this.type, isNot); } getStmt(data, artifacts, binderStore) { return Operand.getStmtOfValue(this.value, this.isNot, data, artifacts, binderStore); } /** * written as static in separate function to be able to call it recursively * when the value is an array */ static getStmtOfValue(value, isNot, data, artifacts, binderStore) { if (value === null) { return (0, util_1.getStmtNull)(); } else if (value instanceof binder_1.Binder) { if (value.no === undefined) { binderStore.add(value); } return `${value.getStmt()}`; } else if (value instanceof binder_1.BinderArray) { value.binders.forEach(it => { if (it.no === undefined) { binderStore.add(it); } }); return `${value.getStmt()}`; } else if (typeof value === 'boolean') { return `${isNot ? 'NOT ' : ''}${(0, util_1.getStmtBoolean)(value)}`; } else if ((0, types_1.isNumber)(value)) { // TODO: why NOT needed for numbers? return `${isNot ? 'NOT ' : ''}${value}`; } else if (typeof value === 'string') { return (0, util_1.getStmtString)(value); } else if (value instanceof Date) { // TODO: why NOT needed for date? return `${isNot ? 'NOT ' : ''}${(0, util_1.getStmtDate)(value)}`; } else if (value instanceof AggregateFunction_1.AggregateFunction) { // TODO: why NOT needed for aggregate function? return `${isNot ? 'NOT ' : ''}${value.getStmt(data, artifacts, binderStore)}`; } else if (value instanceof Expression_1.Expression) { // TODO: why NOT needed for expression? return `${isNot ? 'NOT ' : ''}${value.getStmt(data, artifacts, binderStore)}`; } else if (value instanceof Condition_1.Condition) { /** ignore IDE warning, "value" can be an instance of Condition */ return `${isNot ? 'NOT ' : ''}${value.getStmt(data, artifacts, binderStore)}`; } else if (Array.isArray(value)) { // TODO: why NOT needed for Array? return `${isNot ? 'NOT ' : ''}(${value.map(it => Operand.getStmtOfValue(it, isNot, data, artifacts, binderStore)).join(', ')})`; } else if (value instanceof database_1.Column) { return `${isNot ? 'NOT ' : ''}${value.getStmt(data, artifacts)}`; } throw new Error(`Operand type of value: ${value} is not supported`); } static getExpressionType(operand) { if (operand === null) { return Expression_1.ExpressionType.NULL; } else if (typeof operand === 'boolean' || operand instanceof database_1.BooleanColumn) { return Expression_1.ExpressionType.BOOLEAN; } else if ((0, types_1.isNumber)(operand) || operand instanceof database_1.NumberColumn) { return Expression_1.ExpressionType.NUMBER; } else if (typeof operand === 'string' || operand instanceof database_1.TextColumn) { return Expression_1.ExpressionType.TEXT; } else if (operand instanceof Date || operand instanceof database_1.DateColumn) { return Expression_1.ExpressionType.DATE; } else if (operand instanceof AggregateFunction_1.AggregateFunction) { return Expression_1.ExpressionType.NUMBER; /** ignore IDE warning, operand can be an instance of Condition */ } else if (operand instanceof Expression_1.Expression || operand instanceof binder_1.Binder || operand instanceof binder_1.BinderArray || operand instanceof Condition_1.Condition) { return operand.type; } else if (Array.isArray(operand)) { return Expression_1.ExpressionType.ARRAY; } throw new Error(`Operand type of: ${operand} is not supported`); } static throwIfInvalidUseOfNot(expressionType, notValue) { if (notValue === true && expressionType !== Expression_1.ExpressionType.BOOLEAN) { throw new Error('You can not use "NOT" modifier unless expression type is boolean'); } } } exports.Operand = Operand; class ConditionOperand extends Operand { constructor(value, isNot = false) { super(value, isNot); this.value = value; this.isNot = isNot; } } exports.ConditionOperand = ConditionOperand; //# sourceMappingURL=Operand.js.map