@iamkirbki/database-handler-core 4.3.1 → 4.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/abstract/Controller.d.ts +17 -0
- package/dist/abstract/Controller.d.ts.map +1 -0
- package/dist/abstract/Controller.js +47 -0
- package/dist/abstract/Model.d.ts +33 -19
- package/dist/abstract/Model.d.ts.map +1 -1
- package/dist/abstract/Model.js +177 -119
- package/dist/abstract/SchemaTableBuilder.d.ts +1 -1
- package/dist/abstract/SchemaTableBuilder.d.ts.map +1 -1
- package/dist/abstract/model/ModelRelation.d.ts +23 -0
- package/dist/abstract/model/ModelRelation.d.ts.map +1 -0
- package/dist/abstract/model/ModelRelation.js +130 -0
- package/dist/base/Query.d.ts +6 -4
- package/dist/base/Query.d.ts.map +1 -1
- package/dist/base/Query.js +62 -21
- package/dist/base/Record.d.ts.map +1 -1
- package/dist/base/Record.js +7 -5
- package/dist/base/Table.d.ts +5 -4
- package/dist/base/Table.d.ts.map +1 -1
- package/dist/base/Table.js +56 -31
- package/dist/helpers/Errors/AdapterErrors/AdapterNotFoundError.d.ts +4 -0
- package/dist/helpers/Errors/AdapterErrors/AdapterNotFoundError.d.ts.map +1 -0
- package/dist/helpers/Errors/AdapterErrors/AdapterNotFoundError.js +6 -0
- package/dist/helpers/Errors/AdapterErrors/ConnectionFailedError.d.ts +4 -0
- package/dist/helpers/Errors/AdapterErrors/ConnectionFailedError.d.ts.map +1 -0
- package/dist/helpers/Errors/AdapterErrors/ConnectionFailedError.js +6 -0
- package/dist/helpers/Errors/AdapterErrors/NoDefaultAdapterError.d.ts +4 -0
- package/dist/helpers/Errors/AdapterErrors/NoDefaultAdapterError.d.ts.map +1 -0
- package/dist/helpers/Errors/AdapterErrors/NoDefaultAdapterError.js +6 -0
- package/dist/helpers/Errors/ExpressionErrors/InvalidExpressionParametersError.d.ts +4 -0
- package/dist/helpers/Errors/ExpressionErrors/InvalidExpressionParametersError.d.ts.map +1 -0
- package/dist/helpers/Errors/ExpressionErrors/InvalidExpressionParametersError.js +6 -0
- package/dist/helpers/Errors/ExpressionErrors/UnknownExpressionTypeError.d.ts +4 -0
- package/dist/helpers/Errors/ExpressionErrors/UnknownExpressionTypeError.d.ts.map +1 -0
- package/dist/helpers/Errors/ExpressionErrors/UnknownExpressionTypeError.js +6 -0
- package/dist/helpers/Errors/ExpressionErrors/UnsupportedQueryPhaseError.d.ts +4 -0
- package/dist/helpers/Errors/ExpressionErrors/UnsupportedQueryPhaseError.d.ts.map +1 -0
- package/dist/helpers/Errors/ExpressionErrors/UnsupportedQueryPhaseError.js +6 -0
- package/dist/helpers/Errors/QueryErrors/UnexpectedEmptyQueryError.d.ts +4 -0
- package/dist/helpers/Errors/QueryErrors/UnexpectedEmptyQueryError.d.ts.map +1 -0
- package/dist/helpers/Errors/QueryErrors/UnexpectedEmptyQueryError.js +6 -0
- package/dist/helpers/Errors/TableErrors/UnknownTableError.d.ts +4 -0
- package/dist/helpers/Errors/TableErrors/UnknownTableError.d.ts.map +1 -0
- package/dist/helpers/Errors/TableErrors/UnknownTableError.js +6 -0
- package/dist/helpers/QueryBuilders/BaseQueryBuilders/BaseDeleteQueryBuilder.d.ts +1 -0
- package/dist/helpers/QueryBuilders/BaseQueryBuilders/BaseDeleteQueryBuilder.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/BaseQueryBuilders/BaseDeleteQueryBuilder.js +1 -0
- package/dist/helpers/QueryBuilders/BaseQueryBuilders/BaseInsertQueryBuilder.d.ts +1 -0
- package/dist/helpers/QueryBuilders/BaseQueryBuilders/BaseInsertQueryBuilder.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/BaseQueryBuilders/BaseInsertQueryBuilder.js +11 -0
- package/dist/helpers/QueryBuilders/BaseQueryBuilders/BaseSelectQueryBuilder.d.ts +11 -0
- package/dist/helpers/QueryBuilders/BaseQueryBuilders/BaseSelectQueryBuilder.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/BaseQueryBuilders/BaseSelectQueryBuilder.js +27 -0
- package/dist/helpers/QueryBuilders/BaseQueryBuilders/BaseUpdateQueryBuilder.d.ts +1 -0
- package/dist/helpers/QueryBuilders/BaseQueryBuilders/BaseUpdateQueryBuilder.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/BaseQueryBuilders/BaseUpdateQueryBuilder.js +1 -0
- package/dist/helpers/QueryBuilders/ExpressionBuilders/JsonAggregateExpression.d.ts +9 -0
- package/dist/helpers/QueryBuilders/ExpressionBuilders/JsonAggregateExpression.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/ExpressionBuilders/JsonAggregateExpression.js +102 -0
- package/dist/helpers/QueryBuilders/ExpressionBuilders/SpatialDistanceExpression.d.ts +8 -0
- package/dist/helpers/QueryBuilders/ExpressionBuilders/SpatialDistanceExpression.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/ExpressionBuilders/SpatialDistanceExpression.js +63 -0
- package/dist/helpers/QueryBuilders/ExpressionBuilders/TextRelevanceExpression.d.ts +8 -0
- package/dist/helpers/QueryBuilders/ExpressionBuilders/TextRelevanceExpression.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/ExpressionBuilders/TextRelevanceExpression.js +49 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/ExpressionDecorator.d.ts +19 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/ExpressionDecorator.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/ExpressionDecorator.js +60 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/GroupByDecorator.d.ts +9 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/GroupByDecorator.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/GroupByDecorator.js +27 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/JoinDecorator.d.ts +15 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/JoinDecorator.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/JoinDecorator.js +74 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/LimitDecorator.d.ts +10 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/LimitDecorator.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/LimitDecorator.js +29 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/OrderByDecorator.d.ts +9 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/OrderByDecorator.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/OrderByDecorator.js +27 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/QueryDecorator.d.ts +8 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/QueryDecorator.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/QueryDecorator.js +5 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/ValuesDecorator.d.ts +1 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/ValuesDecorator.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/ValuesDecorator.js +17 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/WhereDecorator.d.ts +9 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/WhereDecorator.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/WhereDecorator.js +29 -0
- package/dist/helpers/QueryBuilders/QueryExpressionBuilder.d.ts +54 -0
- package/dist/helpers/QueryBuilders/QueryExpressionBuilder.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/QueryExpressionBuilder.js +102 -0
- package/dist/helpers/QueryBuilders/QueryStatementBuilder.d.ts +16 -0
- package/dist/helpers/QueryBuilders/QueryStatementBuilder.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/QueryStatementBuilder.js +169 -0
- package/dist/helpers/QueryBuilders/SqlRenderer.d.ts +15 -0
- package/dist/helpers/QueryBuilders/SqlRenderer.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/SqlRenderer.js +79 -0
- package/dist/helpers/QueryBuilders/oldQueryStatementBuilder.d.ts +160 -0
- package/dist/helpers/QueryBuilders/oldQueryStatementBuilder.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/oldQueryStatementBuilder.js +217 -0
- package/dist/interfaces/IExpressionBuilder.d.ts +7 -0
- package/dist/interfaces/IExpressionBuilder.d.ts.map +1 -0
- package/dist/interfaces/IExpressionBuilder.js +1 -0
- package/dist/interfaces/IQueryBuilder.d.ts +5 -0
- package/dist/interfaces/IQueryBuilder.d.ts.map +1 -0
- package/dist/interfaces/IQueryBuilder.js +1 -0
- package/dist/runtime/Container.d.ts +1 -0
- package/dist/runtime/Container.d.ts.map +1 -1
- package/dist/runtime/Container.js +5 -2
- package/dist/runtime/QueryCache.d.ts +12 -0
- package/dist/runtime/QueryCache.d.ts.map +1 -0
- package/dist/runtime/QueryCache.js +26 -0
- package/dist/runtime/Repository.d.ts +6 -4
- package/dist/runtime/Repository.d.ts.map +1 -1
- package/dist/runtime/Repository.js +95 -60
- package/dist/types/expressions.d.ts +154 -0
- package/dist/types/expressions.d.ts.map +1 -0
- package/dist/types/expressions.js +6 -0
- package/dist/types/factories.d.ts +3 -2
- package/dist/types/factories.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +1 -0
- package/dist/types/model.d.ts +8 -8
- package/dist/types/model.d.ts.map +1 -1
- package/dist/types/query.d.ts +56 -2
- package/dist/types/query.d.ts.map +1 -1
- package/package.json +11 -3
- package/dist/helpers/QueryStatementBuilder.d.ts +0 -305
- package/dist/helpers/QueryStatementBuilder.d.ts.map +0 -1
- package/dist/helpers/QueryStatementBuilder.js +0 -416
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import QueryDecorator from "./QueryDecorator.js";
|
|
11
|
+
export default class JoinDecorator extends QueryDecorator {
|
|
12
|
+
constructor(builder, layer, tableColumnInformation) {
|
|
13
|
+
var _a, _b, _c, _d, _e;
|
|
14
|
+
if (!layer.base.from) {
|
|
15
|
+
throw new Error("Base layer must specify 'from' table name for JoinDecorator.");
|
|
16
|
+
}
|
|
17
|
+
super(builder);
|
|
18
|
+
this.fromTableName = layer.base.from;
|
|
19
|
+
this.joins = layer.base.joins || [];
|
|
20
|
+
this.tableColumnsCache = tableColumnInformation;
|
|
21
|
+
this.options = {
|
|
22
|
+
orderBy: (_a = layer.final) === null || _a === void 0 ? void 0 : _a.orderBy,
|
|
23
|
+
limit: (_b = layer.final) === null || _b === void 0 ? void 0 : _b.limit,
|
|
24
|
+
offset: (_c = layer.final) === null || _c === void 0 ? void 0 : _c.offset,
|
|
25
|
+
groupBy: (_d = layer.pretty) === null || _d === void 0 ? void 0 : _d.groupBy,
|
|
26
|
+
blacklistTables: (_e = layer.final) === null || _e === void 0 ? void 0 : _e.blacklistTables,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
build() {
|
|
30
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
31
|
+
var _a;
|
|
32
|
+
const context = yield this.component.build();
|
|
33
|
+
const selectExtensions = yield this.buildJoinSelect();
|
|
34
|
+
const joinPart = this.buildJoinPart();
|
|
35
|
+
context.joinsSelect = selectExtensions;
|
|
36
|
+
(_a = context.joins) !== null && _a !== void 0 ? _a : (context.joins = []);
|
|
37
|
+
context.joins.push(...joinPart);
|
|
38
|
+
return context;
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
buildJoinSelect() {
|
|
42
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
43
|
+
var _a;
|
|
44
|
+
const blacklist = ((_a = this.options) === null || _a === void 0 ? void 0 : _a.blacklistTables) || [];
|
|
45
|
+
const joinArray = Array.isArray(this.joins) ? this.joins : [this.joins];
|
|
46
|
+
const mainCols = this.tableColumnsCache.get(this.fromTableName) || [];
|
|
47
|
+
const mainSelect = mainCols
|
|
48
|
+
.filter(() => !blacklist.includes(this.fromTableName))
|
|
49
|
+
.map(col => `"${this.fromTableName}"."${col.name}" AS "${this.fromTableName}__${col.name}"`);
|
|
50
|
+
const joinedSelects = yield Promise.all(joinArray.map((join) => __awaiter(this, void 0, void 0, function* () {
|
|
51
|
+
if (blacklist.includes(join.fromTable))
|
|
52
|
+
return "";
|
|
53
|
+
const cols = this.tableColumnsCache.get(join.fromTable) || [];
|
|
54
|
+
return cols
|
|
55
|
+
.map(col => `"${join.fromTable}"."${col.name}" AS "${join.fromTable}__${col.name}"`)
|
|
56
|
+
.filter(col => col.trim() !== "");
|
|
57
|
+
})));
|
|
58
|
+
return [...mainSelect, ...joinedSelects.flat()].filter(s => s !== "").filter(Boolean);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
buildJoinPart() {
|
|
62
|
+
const joinArray = Array.isArray(this.joins) ? this.joins : [this.joins];
|
|
63
|
+
return joinArray.map(join => {
|
|
64
|
+
const baseTable = join.baseTable || this.fromTableName;
|
|
65
|
+
const onConditions = Array.isArray(join.on) ? join.on : [join.on];
|
|
66
|
+
const onClause = onConditions.map(part => {
|
|
67
|
+
const targetCol = Object.keys(part)[0];
|
|
68
|
+
const sourceCol = Object.values(part)[0];
|
|
69
|
+
return `${baseTable}.${sourceCol} = ${join.fromTable}.${targetCol}`;
|
|
70
|
+
}).join(" AND ");
|
|
71
|
+
return `${join.joinType} JOIN "${join.fromTable}" ON ${onClause}`;
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import IQueryBuilder from "../../../interfaces/IQueryBuilder.js";
|
|
2
|
+
import QueryDecorator from "./QueryDecorator.js";
|
|
3
|
+
import { QueryContext } from "../../../types/query.js";
|
|
4
|
+
export default class LimitDecorator extends QueryDecorator {
|
|
5
|
+
private limitCount;
|
|
6
|
+
private offsetCount?;
|
|
7
|
+
constructor(component: IQueryBuilder, limitCount: number, offsetCount?: number);
|
|
8
|
+
build(): Promise<QueryContext>;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=LimitDecorator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LimitDecorator.d.ts","sourceRoot":"","sources":["../../../../src/helpers/QueryBuilders/QueryDecorators/LimitDecorator.ts"],"names":[],"mappings":"AAAA,OAAO,aAAa,MAAM,mCAAmC,CAAC;AAC9D,OAAO,cAAc,MAAM,qBAAqB,CAAC;AACjD,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAEpD,MAAM,CAAC,OAAO,OAAO,cAAe,SAAQ,cAAc;IACtD,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,WAAW,CAAC,CAAS;gBAEjB,SAAS,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,MAAM;IAMxE,KAAK,IAAI,OAAO,CAAC,YAAY,CAAC;CAYvC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import QueryDecorator from "./QueryDecorator.js";
|
|
11
|
+
export default class LimitDecorator extends QueryDecorator {
|
|
12
|
+
constructor(component, limitCount, offsetCount) {
|
|
13
|
+
super(component);
|
|
14
|
+
this.limitCount = limitCount;
|
|
15
|
+
this.offsetCount = offsetCount;
|
|
16
|
+
}
|
|
17
|
+
build() {
|
|
18
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
19
|
+
const context = yield this.component.build();
|
|
20
|
+
if (this.limitCount) {
|
|
21
|
+
context.limit = this.limitCount;
|
|
22
|
+
}
|
|
23
|
+
if (this.offsetCount) {
|
|
24
|
+
context.offset = this.offsetCount;
|
|
25
|
+
}
|
|
26
|
+
return context;
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import IQueryBuilder from "../../../interfaces/IQueryBuilder.js";
|
|
2
|
+
import QueryDecorator from "./QueryDecorator.js";
|
|
3
|
+
import { OrderByDefinition, QueryContext } from "../../../types/query.js";
|
|
4
|
+
export default class OrderByDecorator extends QueryDecorator {
|
|
5
|
+
private orderByColumns?;
|
|
6
|
+
constructor(component: IQueryBuilder, orderByColumns?: OrderByDefinition[]);
|
|
7
|
+
build(): Promise<QueryContext>;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=OrderByDecorator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OrderByDecorator.d.ts","sourceRoot":"","sources":["../../../../src/helpers/QueryBuilders/QueryDecorators/OrderByDecorator.ts"],"names":[],"mappings":"AAAA,OAAO,aAAa,MAAM,mCAAmC,CAAC;AAC9D,OAAO,cAAc,MAAM,qBAAqB,CAAC;AACjD,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAEvE,MAAM,CAAC,OAAO,OAAO,gBAAiB,SAAQ,cAAc;IACxD,OAAO,CAAC,cAAc,CAAC,CAAsB;gBAEjC,SAAS,EAAE,aAAa,EAAE,cAAc,CAAC,EAAE,iBAAiB,EAAE;IAKpE,KAAK,IAAI,OAAO,CAAC,YAAY,CAAC;CASvC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import QueryDecorator from "./QueryDecorator.js";
|
|
11
|
+
export default class OrderByDecorator extends QueryDecorator {
|
|
12
|
+
constructor(component, orderByColumns) {
|
|
13
|
+
super(component);
|
|
14
|
+
this.orderByColumns = orderByColumns;
|
|
15
|
+
}
|
|
16
|
+
build() {
|
|
17
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
18
|
+
var _a;
|
|
19
|
+
const context = yield this.component.build();
|
|
20
|
+
if (this.orderByColumns) {
|
|
21
|
+
(_a = context.orderBy) !== null && _a !== void 0 ? _a : (context.orderBy = []);
|
|
22
|
+
context.orderBy.push(...this.orderByColumns);
|
|
23
|
+
}
|
|
24
|
+
return context;
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import IQueryBuilder from "../../../interfaces/IQueryBuilder";
|
|
2
|
+
import { QueryContext } from "../../../types/query";
|
|
3
|
+
export default abstract class QueryDecorator implements IQueryBuilder {
|
|
4
|
+
protected component: IQueryBuilder;
|
|
5
|
+
constructor(component: IQueryBuilder);
|
|
6
|
+
abstract build(): Promise<QueryContext>;
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=QueryDecorator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"QueryDecorator.d.ts","sourceRoot":"","sources":["../../../../src/helpers/QueryBuilders/QueryDecorators/QueryDecorator.ts"],"names":[],"mappings":"AAAA,OAAO,aAAa,MAAM,gCAAgC,CAAC;AAC3D,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjD,MAAM,CAAC,OAAO,CAAC,QAAQ,OAAO,cAAe,YAAW,aAAa;IACjE,SAAS,CAAC,SAAS,EAAE,aAAa,CAAC;gBAEvB,SAAS,EAAE,aAAa;IAIpC,QAAQ,CAAC,KAAK,IAAI,OAAO,CAAC,YAAY,CAAC;CAC1C"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=ValuesDecorator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ValuesDecorator.d.ts","sourceRoot":"","sources":["../../../../src/helpers/QueryBuilders/QueryDecorators/ValuesDecorator.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// import IQueryBuilder from "../../../interfaces/IQueryBuilder.js";
|
|
3
|
+
// import { columnType } from "../../../types/index.js";
|
|
4
|
+
// import QueryDecorator from "./QueryDecorator";
|
|
5
|
+
// export default class ValuesDecorator extends QueryDecorator {
|
|
6
|
+
// private values: columnType[];
|
|
7
|
+
// constructor(component: IQueryBuilder, values: columnType[]) {
|
|
8
|
+
// super(component);
|
|
9
|
+
// this.values = values;
|
|
10
|
+
// }
|
|
11
|
+
// async build(): Promise<string> {
|
|
12
|
+
// const baseQuery = await this.component.build();
|
|
13
|
+
// const columns = Object.keys(this.values);
|
|
14
|
+
// const placeholders = columns.map((col) => `@${col}`);
|
|
15
|
+
// return `${baseQuery} ${columns.map((c) => `"${c}"`).join(", ")} VALUES (${placeholders.join(", ")})`;
|
|
16
|
+
// }
|
|
17
|
+
// }
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import IQueryBuilder from "../../../interfaces/IQueryBuilder.js";
|
|
2
|
+
import { QueryContext, QueryWhereCondition } from "../../../types/query.js";
|
|
3
|
+
import QueryDecorator from "./QueryDecorator.js";
|
|
4
|
+
export default class WhereDecorator extends QueryDecorator {
|
|
5
|
+
private conditions;
|
|
6
|
+
constructor(component: IQueryBuilder, conditions: QueryWhereCondition);
|
|
7
|
+
build(): Promise<QueryContext>;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=WhereDecorator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WhereDecorator.d.ts","sourceRoot":"","sources":["../../../../src/helpers/QueryBuilders/QueryDecorators/WhereDecorator.ts"],"names":[],"mappings":"AAAA,OAAO,aAAa,MAAM,mCAAmC,CAAC;AAC9D,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AACzE,OAAO,cAAc,MAAM,qBAAqB,CAAC;AAGjD,MAAM,CAAC,OAAO,OAAO,cAAe,SAAQ,cAAc;IACtD,OAAO,CAAC,UAAU,CAAsB;gBAGpC,SAAS,EAAE,aAAa,EACxB,UAAU,EAAE,mBAAmB;IAM7B,KAAK,IAAI,OAAO,CAAC,YAAY,CAAC;CAUvC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import QueryDecorator from "./QueryDecorator.js";
|
|
11
|
+
import QueryStatementBuilder from "../QueryStatementBuilder.js";
|
|
12
|
+
export default class WhereDecorator extends QueryDecorator {
|
|
13
|
+
constructor(component, conditions) {
|
|
14
|
+
super(component);
|
|
15
|
+
this.conditions = conditions;
|
|
16
|
+
}
|
|
17
|
+
build() {
|
|
18
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
19
|
+
var _a, _b;
|
|
20
|
+
var _c;
|
|
21
|
+
const context = yield this.component.build();
|
|
22
|
+
const combinedConditions = [...QueryStatementBuilder.normalizeQueryConditions(this.conditions)];
|
|
23
|
+
(_a = context.conditions) !== null && _a !== void 0 ? _a : (context.conditions = {});
|
|
24
|
+
(_b = (_c = context.conditions).where) !== null && _b !== void 0 ? _b : (_c.where = []);
|
|
25
|
+
context.conditions.where.push(...combinedConditions);
|
|
26
|
+
return context;
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { PossibleExpressions, ExpressionBuilderFunction, expressionClause } from "../../types/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* A normalized, intermediate representation of a query expression.
|
|
4
|
+
*
|
|
5
|
+
* Expressions are NOT SQL strings directly — they are metadata-rich
|
|
6
|
+
* building blocks that later stages of the query builder can reason about.
|
|
7
|
+
*
|
|
8
|
+
* This is what allows:
|
|
9
|
+
* - base vs projection expressions
|
|
10
|
+
* - conditional query wrapping
|
|
11
|
+
* - expression-driven WHERE / ORDER BY injection
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Central factory and orchestration layer for query expressions.
|
|
15
|
+
*
|
|
16
|
+
* This class is intentionally:
|
|
17
|
+
* - static
|
|
18
|
+
* - registry-driven
|
|
19
|
+
* - dumb about SQL execution
|
|
20
|
+
*
|
|
21
|
+
* Its ONLY job is to translate abstract expressions
|
|
22
|
+
* into structured SQL fragments with metadata.
|
|
23
|
+
*/
|
|
24
|
+
export default class QueryExpressionBuilder {
|
|
25
|
+
/**
|
|
26
|
+
* Registry mapping expression "type" → builder function.
|
|
27
|
+
*
|
|
28
|
+
* This allows:
|
|
29
|
+
* - zero switch statements
|
|
30
|
+
* - easy plugin-style extension
|
|
31
|
+
* - no touching core logic to add new expressions
|
|
32
|
+
*/
|
|
33
|
+
private static expressionBuilders;
|
|
34
|
+
private static expressionDefaults;
|
|
35
|
+
static getExpressionDefaultRequirements(type: string): PossibleExpressions['requirements'] | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Registers a new expression builder at runtime.
|
|
38
|
+
*
|
|
39
|
+
* This is your “escape hatch” when core expressions
|
|
40
|
+
* are not enough.
|
|
41
|
+
*
|
|
42
|
+
* @param type - expression.type value
|
|
43
|
+
* @param builder - function that converts expression → expressionClause
|
|
44
|
+
*/
|
|
45
|
+
static registerExpressionBuilder<T extends PossibleExpressions>(type: string, builder: ExpressionBuilderFunction<T>): void;
|
|
46
|
+
/**
|
|
47
|
+
* Converts high-level expression definitions into normalized clauses.
|
|
48
|
+
*
|
|
49
|
+
* Any expression without a registered builder is ignored
|
|
50
|
+
* (with a error, because silence is how bugs breed).
|
|
51
|
+
*/
|
|
52
|
+
static buildExpressionsPart(expressions: PossibleExpressions[]): expressionClause[];
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=QueryExpressionBuilder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"QueryExpressionBuilder.d.ts","sourceRoot":"","sources":["../../../src/helpers/QueryBuilders/QueryExpressionBuilder.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,mBAAmB,EAEnB,yBAAyB,EACzB,gBAAgB,EAGnB,MAAM,sBAAsB,CAAC;AAM9B;;;;;;;;;;GAUG;AAEH;;;;;;;;;;GAUG;AACH,MAAM,CAAC,OAAO,OAAO,sBAAsB;IAEvC;;;;;;;OAOG;IACH,OAAO,CAAC,MAAM,CAAC,kBAAkB,CA0B9B;IAEH,OAAO,CAAC,MAAM,CAAC,kBAAkB,CAa9B;WAEW,gCAAgC,CAC1C,IAAI,EAAE,MAAM,GACb,mBAAmB,CAAC,cAAc,CAAC,GAAG,SAAS;IAIlD;;;;;;;;OAQG;WACW,yBAAyB,CAAC,CAAC,SAAS,mBAAmB,EACjE,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,yBAAyB,CAAC,CAAC,CAAC,GACtC,IAAI;IAIP;;;;;OAKG;WACW,oBAAoB,CAC9B,WAAW,EAAE,mBAAmB,EAAE,GACnC,gBAAgB,EAAE;CAexB"}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import SpatialDistanceExpression from "./ExpressionBuilders/SpatialDistanceExpression.js";
|
|
2
|
+
import { UnknownExpressionTypeError } from "../Errors/ExpressionErrors/UnknownExpressionTypeError.js";
|
|
3
|
+
import TextRelevanceExpression from "./ExpressionBuilders/TextRelevanceExpression.js";
|
|
4
|
+
import JsonAggregateExpression from "./ExpressionBuilders/JsonAggregateExpression.js";
|
|
5
|
+
/**
|
|
6
|
+
* A normalized, intermediate representation of a query expression.
|
|
7
|
+
*
|
|
8
|
+
* Expressions are NOT SQL strings directly — they are metadata-rich
|
|
9
|
+
* building blocks that later stages of the query builder can reason about.
|
|
10
|
+
*
|
|
11
|
+
* This is what allows:
|
|
12
|
+
* - base vs projection expressions
|
|
13
|
+
* - conditional query wrapping
|
|
14
|
+
* - expression-driven WHERE / ORDER BY injection
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Central factory and orchestration layer for query expressions.
|
|
18
|
+
*
|
|
19
|
+
* This class is intentionally:
|
|
20
|
+
* - static
|
|
21
|
+
* - registry-driven
|
|
22
|
+
* - dumb about SQL execution
|
|
23
|
+
*
|
|
24
|
+
* Its ONLY job is to translate abstract expressions
|
|
25
|
+
* into structured SQL fragments with metadata.
|
|
26
|
+
*/
|
|
27
|
+
class QueryExpressionBuilder {
|
|
28
|
+
static getExpressionDefaultRequirements(type) {
|
|
29
|
+
return this.expressionDefaults.get(type);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Registers a new expression builder at runtime.
|
|
33
|
+
*
|
|
34
|
+
* This is your “escape hatch” when core expressions
|
|
35
|
+
* are not enough.
|
|
36
|
+
*
|
|
37
|
+
* @param type - expression.type value
|
|
38
|
+
* @param builder - function that converts expression → expressionClause
|
|
39
|
+
*/
|
|
40
|
+
static registerExpressionBuilder(type, builder) {
|
|
41
|
+
this.expressionBuilders.set(type, builder);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Converts high-level expression definitions into normalized clauses.
|
|
45
|
+
*
|
|
46
|
+
* Any expression without a registered builder is ignored
|
|
47
|
+
* (with a error, because silence is how bugs breed).
|
|
48
|
+
*/
|
|
49
|
+
static buildExpressionsPart(expressions) {
|
|
50
|
+
const queryParts = [];
|
|
51
|
+
expressions.forEach(expression => {
|
|
52
|
+
const builder = this.expressionBuilders.get(expression.type);
|
|
53
|
+
if (builder) {
|
|
54
|
+
queryParts.push(builder(expression));
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
throw new UnknownExpressionTypeError(expression.type);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
return queryParts;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Registry mapping expression "type" → builder function.
|
|
65
|
+
*
|
|
66
|
+
* This allows:
|
|
67
|
+
* - zero switch statements
|
|
68
|
+
* - easy plugin-style extension
|
|
69
|
+
* - no touching core logic to add new expressions
|
|
70
|
+
*/
|
|
71
|
+
QueryExpressionBuilder.expressionBuilders = new Map([
|
|
72
|
+
[
|
|
73
|
+
'spatialDistance',
|
|
74
|
+
(expr) => new SpatialDistanceExpression().build(expr)
|
|
75
|
+
],
|
|
76
|
+
[
|
|
77
|
+
'textRelevance',
|
|
78
|
+
(expr) => new TextRelevanceExpression().build(expr)
|
|
79
|
+
],
|
|
80
|
+
[
|
|
81
|
+
'jsonAggregate',
|
|
82
|
+
(expr) => new JsonAggregateExpression().build(expr)
|
|
83
|
+
]
|
|
84
|
+
// Future examples:
|
|
85
|
+
// ['jsonAggregation', (expr) => QueryExpressionBuilder.BuildJsonAggregation(expr)]
|
|
86
|
+
// ['windowFunction', (expr) => QueryExpressionBuilder.BuildWindowFunction(expr)]
|
|
87
|
+
]);
|
|
88
|
+
QueryExpressionBuilder.expressionDefaults = new Map([
|
|
89
|
+
[
|
|
90
|
+
'spatialDistance',
|
|
91
|
+
new SpatialDistanceExpression().defaultRequirements
|
|
92
|
+
],
|
|
93
|
+
[
|
|
94
|
+
'textRelevance',
|
|
95
|
+
new TextRelevanceExpression().defaultRequirements
|
|
96
|
+
],
|
|
97
|
+
[
|
|
98
|
+
'jsonAggregate',
|
|
99
|
+
new JsonAggregateExpression().defaultRequirements
|
|
100
|
+
]
|
|
101
|
+
]);
|
|
102
|
+
export default QueryExpressionBuilder;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { QueryWhereCondition, QueryComparisonParameters, QueryLayers, TableColumnInfo } from "../../types/index.js";
|
|
2
|
+
export default class QueryStatementBuilder {
|
|
3
|
+
private _layers;
|
|
4
|
+
private _contexts;
|
|
5
|
+
private _tableColumnsCache;
|
|
6
|
+
private _valueClauseKeywords;
|
|
7
|
+
constructor(queryLayers: QueryLayers, tableColumnInformation?: Map<string, TableColumnInfo[]>);
|
|
8
|
+
build(): Promise<string>;
|
|
9
|
+
private buildBaseLayer;
|
|
10
|
+
private buildPrettyLayer;
|
|
11
|
+
private buildFinalLayer;
|
|
12
|
+
static normalizeAndQualifyConditions(where: QueryWhereCondition, tableName: string, normalizeBlacklist?: string[], valueClauseKeywords?: Set<string>): QueryComparisonParameters[];
|
|
13
|
+
static normalizeQueryConditions(where: QueryWhereCondition): QueryComparisonParameters[];
|
|
14
|
+
private addUnique;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=QueryStatementBuilder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"QueryStatementBuilder.d.ts","sourceRoot":"","sources":["../../../src/helpers/QueryBuilders/QueryStatementBuilder.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,mBAAmB,EACnB,yBAAyB,EACzB,WAAW,EAEX,eAAe,EAClB,MAAM,sBAAsB,CAAC;AAY9B,MAAM,CAAC,OAAO,OAAO,qBAAqB;IACtC,OAAO,CAAC,OAAO,CAAc;IAC7B,OAAO,CAAC,SAAS,CAIV;IAEP,OAAO,CAAC,kBAAkB,CAA6C;IACvE,OAAO,CAAC,oBAAoB,CAA0B;gBAE1C,WAAW,EAAE,WAAW,EAAE,sBAAsB,CAAC,EAAE,GAAG,CAAC,MAAM,EAAE,eAAe,EAAE,CAAC;IAQhF,KAAK,IAAI,OAAO,CAAC,MAAM,CAAC;YAcvB,cAAc;YAyCd,gBAAgB;YAgDhB,eAAe;WAiBf,6BAA6B,CACvC,KAAK,EAAE,mBAAmB,EAC1B,SAAS,EAAE,MAAM,EACjB,kBAAkB,GAAE,MAAM,EAAO,EACjC,mBAAmB,GAAE,GAAG,CAAC,MAAM,CAAa,GAC7C,yBAAyB,EAAE;WAqBhB,wBAAwB,CAClC,KAAK,EAAE,mBAAmB,GAC3B,yBAAyB,EAAE;IAa9B,OAAO,CAAC,SAAS;CAWpB"}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import BaseSelectQueryBuilder from "./BaseQueryBuilders/BaseSelectQueryBuilder.js";
|
|
11
|
+
import ExpressionDecorator from "./QueryDecorators/ExpressionDecorator.js";
|
|
12
|
+
import QueryExpressionBuilder from "./QueryExpressionBuilder.js";
|
|
13
|
+
import WhereDecorator from "./QueryDecorators/WhereDecorator.js";
|
|
14
|
+
import JoinDecorator from "./QueryDecorators/JoinDecorator.js";
|
|
15
|
+
import GroupByDecorator from "./QueryDecorators/GroupByDecorator.js";
|
|
16
|
+
import OrderByDecorator from "./QueryDecorators/OrderByDecorator.js";
|
|
17
|
+
import LimitDecorator from "./QueryDecorators/LimitDecorator.js";
|
|
18
|
+
import SqlRenderer from "./SqlRenderer.js";
|
|
19
|
+
export default class QueryStatementBuilder {
|
|
20
|
+
constructor(queryLayers, tableColumnInformation) {
|
|
21
|
+
this._contexts = {};
|
|
22
|
+
this._tableColumnsCache = new Map();
|
|
23
|
+
this._valueClauseKeywords = new Set();
|
|
24
|
+
this._layers = queryLayers;
|
|
25
|
+
if (tableColumnInformation) {
|
|
26
|
+
this._tableColumnsCache = tableColumnInformation;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
build() {
|
|
30
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
31
|
+
let sql = yield this.buildBaseLayer();
|
|
32
|
+
if (this._layers.pretty) {
|
|
33
|
+
sql = yield this.buildPrettyLayer(sql);
|
|
34
|
+
}
|
|
35
|
+
if (this._layers.final) {
|
|
36
|
+
sql = yield this.buildFinalLayer(sql);
|
|
37
|
+
}
|
|
38
|
+
return sql;
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
buildBaseLayer() {
|
|
42
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
43
|
+
var _a, _b, _c;
|
|
44
|
+
var _d, _e;
|
|
45
|
+
if (!this._layers.base.from) {
|
|
46
|
+
throw new Error("Base layer must specify 'from' table name.");
|
|
47
|
+
}
|
|
48
|
+
let builder = new BaseSelectQueryBuilder(this._layers.base.from, this._layers.base.select || [], this._layers.base.joinsSelect || [], this._layers.base.expressionsSelect || []);
|
|
49
|
+
if (this._layers.base.joins && this._layers.base.joins.length > 0) {
|
|
50
|
+
builder = new JoinDecorator(builder, { base: this._layers.base, pretty: this._layers.pretty, final: this._layers.final }, this._tableColumnsCache);
|
|
51
|
+
}
|
|
52
|
+
if (this._layers.base.expressions && this._layers.base.expressions.length > 0) {
|
|
53
|
+
const expressions = QueryExpressionBuilder.buildExpressionsPart(this._layers.base.expressions || []);
|
|
54
|
+
builder = new ExpressionDecorator(builder, expressions || []);
|
|
55
|
+
if (builder instanceof ExpressionDecorator) {
|
|
56
|
+
this._valueClauseKeywords = new Set([...this._valueClauseKeywords, ...builder.valueClauseKeywords]);
|
|
57
|
+
(_a = (_d = this._layers).pretty) !== null && _a !== void 0 ? _a : (_d.pretty = {});
|
|
58
|
+
(_b = (_e = this._layers).final) !== null && _b !== void 0 ? _b : (_e.final = {});
|
|
59
|
+
this._layers.pretty.where = this.addUnique(this._layers.pretty.where, builder.whereClauses);
|
|
60
|
+
this._layers.pretty.groupBy = this.addUnique(this._layers.pretty.groupBy, builder.groupByClauses);
|
|
61
|
+
this._layers.pretty.having = this.addUnique(this._layers.pretty.having, builder.havingClauses);
|
|
62
|
+
this._layers.base.orderBy = this.addUnique((_c = this._layers.base.orderBy) === null || _c === void 0 ? void 0 : _c.map(ob => ({ column: `${ob.column}`, direction: ob.direction })), builder.orderByClauses);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (this._layers.base.where) {
|
|
66
|
+
builder = new WhereDecorator(builder, this._layers.base.joins ? QueryStatementBuilder.normalizeAndQualifyConditions(this._layers.base.where, this._layers.base.from) : this._layers.base.where);
|
|
67
|
+
}
|
|
68
|
+
if (this._layers.base.orderBy) {
|
|
69
|
+
builder = new OrderByDecorator(builder, this._layers.base.orderBy);
|
|
70
|
+
}
|
|
71
|
+
this._contexts.base = yield builder.build();
|
|
72
|
+
const renderer = new SqlRenderer(this._contexts.base);
|
|
73
|
+
return renderer.build();
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
buildPrettyLayer(sql) {
|
|
77
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
78
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
79
|
+
var _h;
|
|
80
|
+
let builder = new BaseSelectQueryBuilder(`( ${sql} ) AS BASE_QUERY`, [...((_a = this._contexts.base) === null || _a === void 0 ? void 0 : _a.select) || [], ...((_b = this._layers.pretty) === null || _b === void 0 ? void 0 : _b.select) || []], ((_d = (_c = this._contexts.base) === null || _c === void 0 ? void 0 : _c.joinsSelect) === null || _d === void 0 ? void 0 : _d.map(j => j.split("AS")[1].trim())) || []);
|
|
81
|
+
if (this._layers.pretty) {
|
|
82
|
+
const expressions = ((_e = this._layers.pretty.expressions) === null || _e === void 0 ? void 0 : _e.length)
|
|
83
|
+
? QueryExpressionBuilder.buildExpressionsPart(this._layers.pretty.expressions)
|
|
84
|
+
: [];
|
|
85
|
+
if (expressions.length > 0) {
|
|
86
|
+
builder = new ExpressionDecorator(builder, expressions);
|
|
87
|
+
if (builder instanceof ExpressionDecorator) {
|
|
88
|
+
this._layers.pretty.where = this.addUnique(this._layers.pretty.where, builder.whereClauses);
|
|
89
|
+
this._layers.pretty.groupBy = this.addUnique(this._layers.pretty.groupBy, builder.groupByClauses);
|
|
90
|
+
this._layers.pretty.having = this.addUnique(this._layers.pretty.having, builder.havingClauses);
|
|
91
|
+
(_f = (_h = this._layers).final) !== null && _f !== void 0 ? _f : (_h.final = {});
|
|
92
|
+
this._layers.final.orderBy = this.addUnique((_g = this._layers.final.orderBy) === null || _g === void 0 ? void 0 : _g.map(ob => ({ column: `BASE_QUERY.${ob.column}`, direction: ob.direction })), builder.orderByClauses);
|
|
93
|
+
}
|
|
94
|
+
if (this._layers.pretty.where) {
|
|
95
|
+
builder = new WhereDecorator(builder, QueryStatementBuilder.normalizeAndQualifyConditions(this._layers.pretty.where, "BASE_QUERY", [], this._valueClauseKeywords));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
else if (this._layers.pretty.where) {
|
|
99
|
+
builder = new WhereDecorator(builder, QueryStatementBuilder.normalizeAndQualifyConditions(this._layers.pretty.where, "BASE_QUERY", [], this._valueClauseKeywords));
|
|
100
|
+
}
|
|
101
|
+
if (this._layers.pretty.groupBy) {
|
|
102
|
+
builder = new GroupByDecorator(builder, this._layers.pretty.groupBy);
|
|
103
|
+
}
|
|
104
|
+
if (this._layers.pretty.having) {
|
|
105
|
+
builder = new WhereDecorator(builder, QueryStatementBuilder.normalizeAndQualifyConditions(this._layers.pretty.having, "BASE_QUERY"));
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
this._contexts.pretty = yield builder.build();
|
|
109
|
+
const renderer = new SqlRenderer(this._contexts.pretty);
|
|
110
|
+
return renderer.build();
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
buildFinalLayer(sql) {
|
|
114
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
115
|
+
var _a;
|
|
116
|
+
let builder = new BaseSelectQueryBuilder(`( ${sql} ) AS PRETTY_QUERY`, ((_a = this._layers.final) === null || _a === void 0 ? void 0 : _a.select) || []);
|
|
117
|
+
if (this._layers.final) {
|
|
118
|
+
if (this._layers.final.orderBy) {
|
|
119
|
+
builder = new OrderByDecorator(builder, this._layers.final.orderBy);
|
|
120
|
+
}
|
|
121
|
+
if (this._layers.final.limit) {
|
|
122
|
+
builder = new LimitDecorator(builder, this._layers.final.limit, this._layers.final.offset);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
this._contexts.final = yield builder.build();
|
|
126
|
+
const renderer = new SqlRenderer(this._contexts.final);
|
|
127
|
+
return renderer.build();
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
static normalizeAndQualifyConditions(where, tableName, normalizeBlacklist = [], valueClauseKeywords = new Set()) {
|
|
131
|
+
const conditions = this.normalizeQueryConditions(where);
|
|
132
|
+
return conditions.map((condition) => {
|
|
133
|
+
const shouldSkipQualification = normalizeBlacklist.some((blk) => condition.column.includes(blk)) ||
|
|
134
|
+
condition.column.includes(".");
|
|
135
|
+
const isValueClauseKeyword = valueClauseKeywords.has(condition.column);
|
|
136
|
+
if (isValueClauseKeyword) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
return Object.assign(Object.assign({}, condition), { column: shouldSkipQualification
|
|
140
|
+
? condition.column
|
|
141
|
+
: `${tableName}.${condition.column}` });
|
|
142
|
+
}).filter(cond => cond !== undefined);
|
|
143
|
+
}
|
|
144
|
+
static normalizeQueryConditions(where) {
|
|
145
|
+
if (Array.isArray(where)) {
|
|
146
|
+
return where;
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
return Object.entries(where).map(([column, value]) => ({
|
|
150
|
+
column,
|
|
151
|
+
operator: "=",
|
|
152
|
+
value,
|
|
153
|
+
}));
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
// Copilot moment maybe refactor this later
|
|
157
|
+
addUnique(target, values) {
|
|
158
|
+
if (!(values === null || values === void 0 ? void 0 : values.length))
|
|
159
|
+
return target !== null && target !== void 0 ? target : [];
|
|
160
|
+
const set = new Set((target !== null && target !== void 0 ? target : []).map(v => JSON.stringify(v)));
|
|
161
|
+
for (const v of values) {
|
|
162
|
+
const key = JSON.stringify(v);
|
|
163
|
+
if (!set.has(key))
|
|
164
|
+
set.add(key);
|
|
165
|
+
}
|
|
166
|
+
return Array.from(set).map(s => JSON.parse(s));
|
|
167
|
+
}
|
|
168
|
+
;
|
|
169
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { QueryContext } from "../../types/query.js";
|
|
2
|
+
export default class SqlRenderer {
|
|
3
|
+
private _context;
|
|
4
|
+
constructor(context: QueryContext);
|
|
5
|
+
build(): string;
|
|
6
|
+
private renderSelect;
|
|
7
|
+
private renderFrom;
|
|
8
|
+
private renderWhere;
|
|
9
|
+
private buildWhereWithOperators;
|
|
10
|
+
private renderOrderBy;
|
|
11
|
+
private renderGroupBy;
|
|
12
|
+
private renderLimit;
|
|
13
|
+
private renderOffset;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=SqlRenderer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SqlRenderer.d.ts","sourceRoot":"","sources":["../../../src/helpers/QueryBuilders/SqlRenderer.ts"],"names":[],"mappings":"AAAA,OAAO,EAA6B,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAE/E,MAAM,CAAC,OAAO,OAAO,WAAW;IAC5B,OAAO,CAAC,QAAQ,CAAe;gBAEnB,OAAO,EAAE,YAAY;IAI1B,KAAK,IAAI,MAAM;IAatB,OAAO,CAAC,YAAY;IAapB,OAAO,CAAC,UAAU;IAIlB,OAAO,CAAC,WAAW;IAWnB,OAAO,CAAC,uBAAuB;IAW/B,OAAO,CAAC,aAAa;IAYrB,OAAO,CAAC,aAAa;IAQrB,OAAO,CAAC,WAAW;IAQnB,OAAO,CAAC,YAAY;CAOvB"}
|