UNPKG

snack-query-builder

Version:
77 lines 3.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SnackQueryBuilder = void 0; const from_query_builder_1 = require("./from-query-builder"); const groupby_query_builder_1 = require("./groupby-query-builder"); const having_query_builder_1 = require("./having-query-builder"); const select_query_builder_1 = require("./select-query-builder"); const where_query_builder_1 = require("./where-query-builder"); const colorful_enum_1 = require("./colorful.enum"); const keywords_sql_1 = require("./keywords-sql"); const console_cf_1 = require("./console-cf"); const paginate_query_builder_1 = require("./paginate-query-builder"); const orderby_query_builder_1 = require("./orderby-query-builder"); class SnackQueryBuilder { constructor() { this.$spaces = 8; this.$groupBy = new groupby_query_builder_1.GroupByQueryBuilder(this); this.$orderBy = new orderby_query_builder_1.OrderByQueryBuilder(this); this.$having = new having_query_builder_1.HavingQueryBuilder(this, this.$groupBy); this.$paginate = new paginate_query_builder_1.PaginateQueryBuilder(this); this.$conditions = new where_query_builder_1.WhereQueryBuilder(this, this.$groupBy, this.$having, this.$orderBy); this.$select = new select_query_builder_1.SelectQueryBuilder(this, this.$conditions); } from(table) { this.$from = new from_query_builder_1.FromQueryBuilder(this, table, this.$select); return this.$from.from(); } fromSubQuery(qb, name = 'sub_query') { const subQ = qb .build() .split('\n') .map(i => `${''.padStart(this.$spaces, ' ')}${i}\n`) .join(''); this.$from = new from_query_builder_1.FromQueryBuilder(this, `(\n${subQ}${')'.padStart(this.$spaces, ' ')} as ${name || 'sub_query'}`, this.$select); return this.$from.from(); } paginate(limit, page, countName) { this.$paginate = new paginate_query_builder_1.PaginateQueryBuilder(this); this.$paginate.paginate(limit, page, countName); this.$select.select(this.$paginate.getCounter()); return { build: () => this.build() }; } build(colorful = colorful_enum_1.Colorful.None) { var _a, _b, _c, _d, _e, _f, _g; let result = ''; if (this.$from) { result = `${(_a = this.$select) === null || _a === void 0 ? void 0 : _a.toString()}\n` + `${(_b = this.$from) === null || _b === void 0 ? void 0 : _b.toString()}\n` + `${(_c = this.$conditions) === null || _c === void 0 ? void 0 : _c.toString()}\n` + `${(_d = this.$groupBy) === null || _d === void 0 ? void 0 : _d.toString()}\n` + `${(_e = this.$having) === null || _e === void 0 ? void 0 : _e.toString()}\n` + `${(_f = this.$orderBy) === null || _f === void 0 ? void 0 : _f.toString()}\n` + `${(_g = this.$paginate) === null || _g === void 0 ? void 0 : _g.toString()}`; } else { throw `Define a from value`; } if (this[`cf_${colorful_enum_1.Colorful[colorful]}`]) result = this[`cf_${colorful_enum_1.Colorful[colorful]}`](result); result = result.split('\n').filter(i => i.length > 0).join('\n'); return result; } getSpaces() { return this.$spaces; } cf_Terminal(query) { Object.keys(keywords_sql_1.KeyWordsSQL).forEach(key => { query = query.replace(new RegExp(key, 'gi'), console_cf_1.ConsoleCF.colorfulText(keywords_sql_1.KeyWordsSQL[key], key.replace('\\', ''))); }); return query; } } exports.SnackQueryBuilder = SnackQueryBuilder; //# sourceMappingURL=snack-query-builder.js.map