UNPKG

sphinxql

Version:

SphinxQL query builder for Node.JS. Supports Sphinx search(2.x and 3.x) and Manticore search

96 lines 4.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); require('es7-object-polyfill'); var MatchStatement_1 = require("./statement_expressions/MatchStatement"); var WhereStatement_1 = require("./statement_expressions/WhereStatement"); var OptionExprStatement_1 = require("./statement_expressions/OptionExprStatement"); var utils = require("../utils"); var BaseStatement_1 = require("./BaseStatement"); var UpdateStatement = (function (_super) { tslib_1.__extends(UpdateStatement, _super); function UpdateStatement(connection, index) { var _this = _super.call(this, connection) || this; _this.setParams = {}; _this.matchStatement = new MatchStatement_1.default(); _this.whereConditions = []; _this.optionExprs = []; _this.index = index; return _this; } UpdateStatement.prototype.set = function (newValues) { this.setParams = newValues; return this; }; UpdateStatement.prototype.where = function (columnExpr, operator, value) { if (value === undefined) { value = operator; operator = '='; } var condition = new WhereStatement_1.default(columnExpr, operator, value); this.whereConditions = tslib_1.__spreadArrays(this.whereConditions, [condition]); return this; }; UpdateStatement.prototype.whereIn = function (column, values) { var condition = new WhereStatement_1.default(column, 'IN', values); this.whereConditions = tslib_1.__spreadArrays(this.whereConditions, [condition]); return this; }; UpdateStatement.prototype.whereNotIn = function (column, values) { var condition = new WhereStatement_1.default(column, 'NOT IN', values); this.whereConditions = tslib_1.__spreadArrays(this.whereConditions, [condition]); return this; }; UpdateStatement.prototype.between = function (column, value1, value2) { var condtion = new WhereStatement_1.default(column, 'BETWEEN', [value1, value2]); this.whereConditions = tslib_1.__spreadArrays(this.whereConditions, [condtion]); return this; }; UpdateStatement.prototype.match = function (fields, value, escapeValue) { if (escapeValue === void 0) { escapeValue = true; } this.matchStatement.match(fields.length ? fields : undefined, value, escapeValue); return this; }; UpdateStatement.prototype.orMatch = function (fields, value, escapeValue) { if (escapeValue === void 0) { escapeValue = true; } this.matchStatement.orMatch(fields.length ? fields : undefined, value, escapeValue); return this; }; UpdateStatement.prototype.option = function (option, value) { this.optionExprs = tslib_1.__spreadArrays(this.optionExprs, [new OptionExprStatement_1.default(option, value)]); return this; }; UpdateStatement.prototype.generate = function () { var statement = 'UPDATE '; statement += this.index; statement += ' SET '; statement += this.generateSet(); var hasMatchStatement = this.matchStatement.getParts().length > 0; var hasWhereStatements = this.whereConditions.length > 0; if (hasWhereStatements || hasMatchStatement) { statement += ' WHERE '; if (hasMatchStatement) { statement += "MATCH(" + this.matchStatement.build() + ")"; if (hasWhereStatements) { statement += ' AND '; } } var stringStatements = void 0; stringStatements = this.whereConditions.map(function (condition) { return condition.build(); }); statement += stringStatements.join(' AND '); } if (this.optionExprs.length) { statement += " OPTION " + this.optionExprs.map(function (option) { return option.build(); }).join(','); } return statement; }; UpdateStatement.prototype.generateSet = function () { return Object.entries(this.setParams).map(function (_a) { var key = _a[0], value = _a[1]; return key + "=" + utils.getExpressionCompare(value); }).join(', '); }; return UpdateStatement; }(BaseStatement_1.default)); exports.default = UpdateStatement; //# sourceMappingURL=UpdateStatement.js.map