sphinxql
Version:
SphinxQL query builder for Node.JS. Supports Sphinx search(2.x and 3.x) and Manticore search
59 lines • 2.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var sqlstring_1 = require("sqlstring");
var BaseStatement_1 = require("./BaseStatement");
var template = require('es6-template-strings');
var InsertStatement = (function (_super) {
tslib_1.__extends(InsertStatement, _super);
function InsertStatement(connection, index, values, insertType) {
if (insertType === void 0) { insertType = 'INSERT'; }
var _this = _super.call(this, connection) || this;
if (!index.length) {
throw Error('real-time index must be valid but empty name provided');
}
if (!(values instanceof Array) && (typeof values !== 'object')) {
throw Error("Provide an array or an object (key-value pair) values");
}
if ((values instanceof Array) && !values.length) {
throw Error("No document to insert");
}
_this.connection = connection;
_this.index = index;
_this.values = values;
_this.type = insertType;
return _this;
}
InsertStatement.prototype.renderValues = function (values, columns) {
var tmpl = columns.map(function (column) { return '${' + column + '}'; }).join(', ');
for (var i = 0; i < columns.length; i++) {
if (typeof values[columns[i]] === 'string') {
values[columns[i]] = sqlstring_1.escape(values[columns[i]]);
}
}
var compiledTemplate = template(tmpl, values);
return "(" + compiledTemplate + ")";
};
InsertStatement.prototype.generate = function () {
var _this = this;
var valuesFields = [];
if (this.values instanceof Array) {
var columns_1 = Object.keys(this.values[0]);
var expression = this.type + " INTO " + this.index + " ";
expression += this.renderColumnList(columns_1) + " VALUES ";
valuesFields = this.values.map(function (values) {
return _this.renderValues(values, columns_1);
});
return "" + expression + valuesFields.join(', ');
}
var columns = Object.keys(this.values);
return this.type + " INTO " + this.index + " " + this.renderColumnList(columns) + " " +
("VALUES " + this.renderValues(this.values, columns));
};
InsertStatement.prototype.renderColumnList = function (columns) {
return "(" + columns.join(', ') + ")";
};
return InsertStatement;
}(BaseStatement_1.default));
exports.default = InsertStatement;
//# sourceMappingURL=InsertReplaceStatement.js.map