UNPKG

sedk-mysql

Version:
177 lines 8.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UpdateCondition = exports.Condition = void 0; const binder_1 = require("../binder"); const errors_1 = require("../errors"); const operators_1 = require("../operators"); const SelectItemInfo_1 = require("../SelectItemInfo"); const Expression_1 = require("./Expression"); const Operand_1 = require("./Operand"); const types_1 = require("./types"); class Condition { constructor(con) { this.leftOperand = new Operand_1.ConditionOperand(con.leftExpression, con.notLeft); this.operator = con.operator; this.rightOperand = con.rightExpression !== undefined ? new Operand_1.ConditionOperand(con.rightExpression, con.notRight) : undefined; this.type = Condition.getResultExpressionType(con.leftExpression, con.operator, con.rightExpression); } getStmt(data, artifacts, binderStore) { if (this.operator !== undefined && this.rightOperand !== undefined) if (this.leftOperand.value instanceof Condition) { return `(${this.leftOperand.getStmt(data, artifacts, binderStore)}) ${this.operator} ${this.rightOperand.getStmt(data, artifacts, binderStore)}`; } else { return `${this.leftOperand.getStmt(data, artifacts, binderStore)} ${this.operator} ${this.rightOperand.getStmt(data, artifacts, binderStore)}`; } else return this.leftOperand.getStmt(data, artifacts, binderStore); } // Implement Expression, We don't really need it as(alias) { return new SelectItemInfo_1.SelectItemInfo(this, alias); } eq(value) { return new Condition({ leftExpression: this, operator: operators_1.ComparisonOperator.Equal, rightExpression: Expression_1.Expression.getSimpleExp(value), }); } eq$(value) { const binder = new binder_1.Binder(value); return new Condition({ leftExpression: this, operator: operators_1.ComparisonOperator.Equal, rightExpression: Expression_1.Expression.getSimpleExp(binder), }); } ne(value) { return new Condition({ leftExpression: this, operator: operators_1.ComparisonOperator.NotEqual, rightExpression: Expression_1.Expression.getSimpleExp(value), }); } ne$(value) { const binder = new binder_1.Binder(value); return new Condition({ leftExpression: this, operator: operators_1.ComparisonOperator.NotEqual, rightExpression: Expression_1.Expression.getSimpleExp(binder), }); } isEq(value) { const qualifier = value === null ? operators_1.NullOperator.Is : operators_1.ComparisonOperator.Equal; return new Condition({ leftExpression: this, operator: qualifier, rightExpression: Expression_1.Expression.getSimpleExp(value), }); } isEq$(value) { const qualifier = value === null ? operators_1.NullOperator.Is : operators_1.ComparisonOperator.Equal; const binder = new binder_1.Binder(value); return new Condition({ leftExpression: this, operator: qualifier, rightExpression: Expression_1.Expression.getSimpleExp(binder), }); } isNe(value) { const qualifier = value === null ? operators_1.NullOperator.IsNot : operators_1.ComparisonOperator.NotEqual; return new Condition({ leftExpression: this, operator: qualifier, rightExpression: Expression_1.Expression.getSimpleExp(value), }); } isNe$(value) { const qualifier = value === null ? operators_1.NullOperator.IsNot : operators_1.ComparisonOperator.NotEqual; const binder = new binder_1.Binder(value); return new Condition({ leftExpression: this, operator: qualifier, rightExpression: Expression_1.Expression.getSimpleExp(binder), }); } get NOT() { return new Condition({ leftExpression: this, notLeft: true }); } // Implement Expression, but still good to keep it getColumns() { var _a; const columns = []; columns.push(...this.leftOperand.value.getColumns()); if (((_a = this.rightOperand) === null || _a === void 0 ? void 0 : _a.value) !== undefined) columns.push(...this.rightOperand.value.getColumns()); return columns; } static getResultExpressionType(leftExpression, operator, rightExpression) { if (operator === undefined || rightExpression === undefined) { if (leftExpression.type === Expression_1.ExpressionType.NULL || leftExpression.type === Expression_1.ExpressionType.BOOLEAN) { return leftExpression.type; } Condition.throwInvalidConditionError(leftExpression.type); } if (leftExpression.type === rightExpression.type) { return Expression_1.ExpressionType.BOOLEAN; } if ((0, operators_1.isNullOperator)(operator)) { if (rightExpression.type === Expression_1.ExpressionType.NULL) { return Expression_1.ExpressionType.BOOLEAN; } else if (leftExpression.type === Expression_1.ExpressionType.NULL && rightExpression.type === Expression_1.ExpressionType.BOOLEAN) { return Expression_1.ExpressionType.BOOLEAN; } else if (leftExpression.type === Expression_1.ExpressionType.BOOLEAN || rightExpression.type === Expression_1.ExpressionType.BOOLEAN) { Condition.throwInvalidConditionError(leftExpression.type, operator, rightExpression.type); } else if (leftExpression.type === Expression_1.ExpressionType.NULL) { Condition.throwInvalidConditionError(leftExpression.type, operator, rightExpression.type); } Condition.throwInvalidConditionError(leftExpression.type, operator, rightExpression.type); } else if ((0, operators_1.isComparisonOperator)(operator)) { if (Condition.isListComparisonOperator(operator)) { // TODO: Check if leftExpression list values are comparable with rightExpression if (rightExpression.type === Expression_1.ExpressionType.ARRAY) { return Expression_1.ExpressionType.BOOLEAN; } Condition.throwInvalidConditionError(leftExpression.type, operator, rightExpression.type); } else { if (leftExpression.type === Expression_1.ExpressionType.NULL || rightExpression.type === Expression_1.ExpressionType.NULL) { return Expression_1.ExpressionType.NULL; } else if (leftExpression.type === Expression_1.ExpressionType.BOOLEAN && rightExpression.type === Expression_1.ExpressionType.TEXT && (0, types_1.isTextBoolean)(rightExpression.leftOperand.value)) { return Expression_1.ExpressionType.BOOLEAN; } } Condition.throwInvalidConditionError(leftExpression.type, operator, rightExpression.type); } Condition.throwInvalidConditionError(leftExpression.type, operator, rightExpression.type); } static isListComparisonOperator(operator) { return [operators_1.ComparisonOperator.In, operators_1.ComparisonOperator.NotIn].includes(operator); } static throwInvalidConditionError(leftType, operator, rightType) { if (operator === undefined || rightType === undefined) { throw new errors_1.InvalidConditionError(`Condition can not created with only "${Expression_1.ExpressionType[leftType]}"`); } throw new errors_1.InvalidConditionError(`Condition can not created with "${Expression_1.ExpressionType[leftType]}" "${operator}" "${Expression_1.ExpressionType[rightType]}"`); } } exports.Condition = Condition; class UpdateCondition extends Condition { constructor(column, rightExpression) { super({ leftExpression: Expression_1.Expression.getSimpleExp(column), operator: operators_1.ComparisonOperator.Equal, rightExpression, }); this.operand = new Operand_1.Operand(rightExpression); this.column = column; } } exports.UpdateCondition = UpdateCondition; //# sourceMappingURL=Condition.js.map