UNPKG

sphinxql

Version:

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

95 lines 3.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); require('es7-object-polyfill'); var MatchStatement = (function () { function MatchStatement() { this.escapeChars = { '\\': '\\\\', '(': '\(', ')': '\)', '!': '\!', '@': '\@', '~': '\~', '&': '\&', '/': '\/', '^': '\^', '$': '\$', '=': '\=', '<': '\<' }; this.parts = []; } MatchStatement.prototype.getParts = function () { return this.parts; }; MatchStatement.prototype.escapeSpecialChars = function (value) { var newEscapedValue = value; for (var _i = 0, _a = Object.entries(this.escapeChars); _i < _a.length; _i++) { var _b = _a[_i], key = _b[0], value_1 = _b[1]; newEscapedValue = newEscapedValue.replace(key, value_1); } return newEscapedValue; }; MatchStatement.prototype.match = function (fields, value, escapeValue) { if (escapeValue === void 0) { escapeValue = true; } var part = { logicalLeftRelation: ' ', fields: fields, value: value, escapeValue: escapeValue }; this.parts = tslib_1.__spreadArrays(this.parts, [part]); }; MatchStatement.prototype.orMatch = function (fields, value, escapeValue) { if (escapeValue === void 0) { escapeValue = true; } if (!this.parts.length) { throw Error('OR statement can\'t be used at the beginning of a MATCH expression'); } var part = { logicalLeftRelation: ' | ', fields: fields, value: value, escapeValue: escapeValue }; this.parts = tslib_1.__spreadArrays(this.parts, [part]); }; MatchStatement.prototype.build = function () { var expression = '\''; for (var i = 0, length_1 = this.parts.length; i < length_1; i++) { var _a = this.parts[i], logicalLeftRelation = _a.logicalLeftRelation, fields = _a.fields, value = _a.value, escapeValue = _a.escapeValue; if (i === 0) { expression += "(" + this.buildFields(fields) + this.buildValues(value, escapeValue) + ")"; } else { expression += logicalLeftRelation; expression += "(" + this.buildFields(fields) + this.buildValues(value, escapeValue) + ")"; } } expression += '\''; return expression; }; MatchStatement.prototype.buildFields = function (fields) { var expression = ''; if (fields !== undefined) { if (typeof fields === 'string') { expression += "@" + fields + " "; } else { if (fields[0] === '!') { expression += "@!(" + fields.slice(1).join(',') + ") "; } else { expression += "@(" + fields.join(',') + ") "; } } } return expression; }; MatchStatement.prototype.buildValues = function (value, escapeValue) { return escapeValue ? this.escapeSpecialChars(value) : value; }; return MatchStatement; }()); exports.default = MatchStatement; //# sourceMappingURL=MatchStatement.js.map