var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { query } from "./MysqlConnectionManager"; export default class Query { constructor(type, table, fields) { this._where = []; this._foundRows = false; this.type = type; this.table = table; this.fields = fields || []; } static select(table, ...fields) { return new Query(QueryType.SELECT, table, fields.length > 0 ? fields : ['*']); } static update(table, data) { const fields = []; for (let key in data) { if (data.hasOwnProperty(key)) { fields.push(new UpdateFieldValue(key, data[key])); } } return new Query(QueryType.UPDATE, table, fields); } static delete(table) { return new Query(QueryType.DELETE, table); } where(field, value, operator = WhereOperator.AND, test = WhereTest.EQUALS) { this._where.push(new WhereFieldValue(field, value, operator, test)); return this; } whereNot(field, value, operator = WhereOperator.AND) { return this.where(field, value, operator, WhereTest.DIFFERENT); } orWhere(field, value) { return this.where(field, value, WhereOperator.OR); } whereIn(field, value) { return this.where(field, value, WhereOperator.AND, WhereTest.IN); } limit(limit, offset = 0) { this._limit = limit; this._offset = offset; return this; } first() { return this.limit(1); } sortBy(field, direction = 'ASC') { this._sortBy = field; this._sortDirection = direction; return this; } withTotalRowCount() { this._foundRows = true; return this; } toString(final = false) { var _a; let query = ''; let fields = (_a = this.fields) === null || _a === void 0 ? void 0 : _a.join(','); let where = ''; if (this._where.length > 0) { where = `WHERE ${this._where[0]}`; for (let i = 1; i < this._where.length; i++) { where += this._where[i].toString(false); } } let limit = ''; if (typeof this._limit === 'number') { limit = `LIMIT ${this._limit}`; if (typeof this._offset === 'number' && this._offset !== 0) { limit += ` OFFSET ${this._offset}`; } } let orderBy = ''; if (typeof this._sortBy === 'string') { orderBy = `ORDER BY ${this._sortBy} ${this._sortDirection}`; } switch (this.type) { case QueryType.SELECT: query = `SELECT ${this._foundRows ? 'SQL_CALC_FOUND_ROWS' : ''} ${fields} FROM ${this.table} ${where} ${orderBy} ${limit}`; break; case QueryType.UPDATE: query = `UPDATE ${this.table} SET ${fields} ${where} ${orderBy} ${limit}`; break; case QueryType.DELETE: query = `DELETE FROM ${this.table} ${where} ${orderBy} ${limit}`; break; } return final ? query : `(${query})`; } build() { return this.toString(true); } get variables() { var _a; const variables = []; (_a = this.fields) === null || _a === void 0 ? void 0 : _a.filter(v => v instanceof FieldValue).flatMap(v => v.variables).forEach(v => variables.push(v)); this._where.flatMap(v => v.variables) .forEach(v => variables.push(v)); return variables; } isCacheable() { return this.type === QueryType.SELECT && this.fields.length === 1 && this.fields[0] === '*'; } execute(connection) { return __awaiter(this, void 0, void 0, function* () { const queryResult = yield query(this.build(), this.variables, connection); if (this._foundRows) { const foundRows = yield query('SELECT FOUND_ROWS() as r', undefined, connection); queryResult.foundRows = foundRows.results[0].r; } return queryResult; }); } } export var QueryType; (function (QueryType) { QueryType[QueryType["SELECT"] = 0] = "SELECT"; QueryType[QueryType["UPDATE"] = 1] = "UPDATE"; QueryType[QueryType["DELETE"] = 2] = "DELETE"; })(QueryType || (QueryType = {})); var WhereOperator; (function (WhereOperator) { WhereOperator["AND"] = "AND"; WhereOperator["OR"] = "OR"; })(WhereOperator || (WhereOperator = {})); var WhereTest; (function (WhereTest) { WhereTest["EQUALS"] = "="; WhereTest["DIFFERENT"] = "!="; WhereTest["IN"] = " IN "; })(WhereTest || (WhereTest = {})); class FieldValue { constructor(field, value) { this.field = field; this.value = value; } toString(first = true) { return `${!first ? ',' : ''}${this.field}${this.test}${this.value instanceof Query ? this.value : (Array.isArray(this.value) ? '(?)' : '?')}`; } get test() { return '='; } get variables() { return this.value instanceof Query ? this.value.variables : [this.value]; } } class SelectFieldValue extends FieldValue { toString(first = true) { return `(${this.value instanceof Query ? this.value : '?'}) AS ${this.field}`; } } class UpdateFieldValue extends FieldValue { } class WhereFieldValue extends FieldValue { constructor(field, value, operator, test) { super(field, value); this.operator = operator; this._test = test; } toString(first = true) { return (!first ? ` ${this.operator} ` : '') + super.toString(true); } get test() { return this._test; } } //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiUXVlcnkuanMiLCJzb3VyY2VSb290IjoiLi8iLCJzb3VyY2VzIjpbImRiL1F1ZXJ5LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7OztBQUFBLE9BQU8sRUFBQyxLQUFLLEVBQWMsTUFBTSwwQkFBMEIsQ0FBQztBQUc1RCxNQUFNLENBQUMsT0FBTyxPQUFPLEtBQUs7SUErQnRCLFlBQW9CLElBQWUsRUFBRSxLQUFhLEVBQUUsTUFBeUQ7UUFQckcsV0FBTSxHQUFzQixFQUFFLENBQUM7UUFLL0IsZUFBVSxHQUFZLEtBQUssQ0FBQztRQUdoQyxJQUFJLENBQUMsSUFBSSxHQUFHLElBQUksQ0FBQztRQUNqQixJQUFJLENBQUMsS0FBSyxHQUFHLEtBQUssQ0FBQztRQUNuQixJQUFJLENBQUMsTUFBTSxHQUFHLE1BQU0sSUFBSSxFQUFFLENBQUM7SUFDL0IsQ0FBQztJQWxDTSxNQUFNLENBQUMsTUFBTSxDQUFDLEtBQWEsRUFBRSxHQUFHLE1BQWdCO1FBQ25ELE9BQU8sSUFBSSxLQUFLLENBQUMsU0FBUyxDQUFDLE1BQU0sRUFBRSxLQUFLLEVBQUUsTUFBTSxDQUFDLE1BQU0sR0FBRyxDQUFDLENBQUMsQ0FBQyxDQUFDLE1BQU0sQ0FBQyxDQUFDLENBQUMsQ0FBQyxHQUFHLENBQUMsQ0FBQyxDQUFDO0lBQ2xGLENBQUM7SUFFTSxNQUFNLENBQUMsTUFBTSxDQUFDLEtBQWEsRUFBRSxJQUVuQztRQUNHLE1BQU0sTUFBTSxHQUFHLEVBQUUsQ0FBQztRQUNsQixLQUFLLElBQU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ZXR1cm4gdGhpcztcbiAgICB9XG5cbiAgICBwdWJsaWMgd2l0aFRvdGFsUm93Q291bnQoKTogUXVlcnkge1xuICAgICAgICB0aGlzLl9mb3VuZFJvd3MgPSB0cnVlO1xuICAgICAgICByZXR1cm4gdGhpcztcbiAgICB9XG5cbiAgICBwdWJsaWMgdG9TdHJpbmcoZmluYWw6IGJvb2xlYW4gPSBmYWxzZSk6IHN0cmluZyB7XG4gICAgICAgIGxldCBxdWVyeSA9ICcnO1xuXG4gICAgICAgIGxldCBmaWVsZHMgPSB0aGlzLmZpZWxkcz8uam9pbignLCcpO1xuXG4gICAgICAgIGxldCB3aGVyZSA9ICcnO1xuICAgICAgICBpZiAodGhpcy5fd2hlcmUubGVuZ3RoID4gMCkge1xuICAgICAgICAgICAgd2hlcmUgPSBgV0hFUkUgJHt0aGlzLl93aGVyZVswXX1gO1xuICAgICAgICAgICAgZm9yIChsZXQgaSA9IDE7IGkgPCB0aGlzLl93aGVyZS5sZW5ndGg7IGkrKykge1xuICAgICAgICAgICAgICAgIHdoZXJlICs9IHRoaXMuX3doZXJlW2ldLnRvU3RyaW5nKGZhbHNlKTtcbiAgICAgICAgICAgIH1cbiAgICAgICAgfVxuXG4gICAgICAgIGxldCBsaW1pdCA9ICcnO1xuICAgICAgICBpZiAodHlwZW9mIHRoaXMuX2xpbWl0ID09PSAnbnVtYmVyJykge1xuICAgICAgICAgICAgbGltaXQgPSBgTElNSVQgJHt0aGlzLl9saW1pdH1gO1xuICAgICAgICAgICAgaWYgKHR5cGVvZiB0aGlzLl9vZmZzZXQgPT09ICdudW1iZXInICYmIHRoaXMuX29mZnNldCAhPT0gMCkge1xuICAgICAgICAgICAgICAgIGxpbWl0ICs9IGAgT0ZGU0VUICR7dGhpcy5fb2Zmc2V0fWA7XG4gICAgICAgICAgICB9XG4gICAgICAgIH1cblxuICAgICAgICBsZXQgb3JkZXJCeSA9ICcnO1xuICAgICAgICBpZiAodHlwZW9mIHRoaXMuX3NvcnRCeSA9P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