176 lines
23 KiB
JavaScript
176 lines
23 KiB
JavaScript
|
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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiUXVlcnkuanMiLCJzb3VyY2VSb290IjoiLi8iLCJzb3VyY2VzIjpbImRiL1F1ZXJ5LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7OztBQUFBLE9BQU8sRUFBQyxLQUFLLEVBQWMsTUFBTSwwQkFBMEIsQ0FBQztBQUc1RCxNQUFNLENBQUMsT0FBTyxPQUFPLEtBQUs7SUErQnRCLFlBQW9CLElBQWUsRUFBRSxLQUFhLEVBQUUsTUFBeUQ7UUFQckcsV0FBTSxHQUFzQixFQUFFLENBQUM7UUFLL0IsZUFBVSxHQUFZLEtBQUssQ0FBQztRQUdoQyxJQUFJLENBQUMsSUFBSSxHQUFHLElBQUksQ0FBQztRQUNqQixJQUFJLENBQUMsS0FBSyxHQUFHLEtBQUssQ0FBQztRQUNuQixJQUFJLENBQUMsTUFBTSxHQUFHLE1BQU0sSUFBSSxFQUFFLENBQUM7SUFDL0IsQ0FBQztJQWxDTSxNQUFNLENBQUMsTUFBTSxDQUFDLEtBQWEsRUFBRSxHQUFHLE1BQWdCO1FBQ25ELE9BQU8sSUFBSSxLQUFLLENBQUMsU0FBUyxDQUFDLE1BQU0sRUFBRSxLQUFLLEVBQUUsTUFBTSxDQUFDLE1BQU0sR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