@relq/orm 0.1.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/LICENSE +21 -0
- package/README.md +236 -0
- package/dist/cjs/__test-types.cjs +17 -0
- package/dist/cjs/addon/cursor.cjs +1473 -0
- package/dist/cjs/addon/pg.cjs +4969 -0
- package/dist/cjs/cache/index.cjs +9 -0
- package/dist/cjs/cache/query-cache.cjs +311 -0
- package/dist/cjs/condition/array-condition-builder.cjs +527 -0
- package/dist/cjs/condition/array-numeric-condition-builder.cjs +186 -0
- package/dist/cjs/condition/array-specialized-condition-builder.cjs +206 -0
- package/dist/cjs/condition/array-string-condition-builder.cjs +146 -0
- package/dist/cjs/condition/base-condition-builder.cjs +2 -0
- package/dist/cjs/condition/condition-collector.cjs +461 -0
- package/dist/cjs/condition/fulltext-condition-builder.cjs +61 -0
- package/dist/cjs/condition/geometric-condition-builder.cjs +228 -0
- package/dist/cjs/condition/index.cjs +29 -0
- package/dist/cjs/condition/jsonb-condition-builder.cjs +448 -0
- package/dist/cjs/condition/network-condition-builder.cjs +237 -0
- package/dist/cjs/condition/postgis-condition-builder.cjs +188 -0
- package/dist/cjs/condition/range-condition-builder.cjs +98 -0
- package/dist/cjs/core/helpers/ConnectedAggregateBuilder.cjs +132 -0
- package/dist/cjs/core/helpers/ConnectedCTEBuilder.cjs +53 -0
- package/dist/cjs/core/helpers/ConnectedCountBuilder.cjs +73 -0
- package/dist/cjs/core/helpers/ConnectedDeleteBuilder.cjs +65 -0
- package/dist/cjs/core/helpers/ConnectedInsertBuilder.cjs +112 -0
- package/dist/cjs/core/helpers/ConnectedInsertFromSelectBuilder.cjs +66 -0
- package/dist/cjs/core/helpers/ConnectedQueryBuilder.cjs +146 -0
- package/dist/cjs/core/helpers/ConnectedRawQueryBuilder.cjs +46 -0
- package/dist/cjs/core/helpers/ConnectedSelectBuilder.cjs +331 -0
- package/dist/cjs/core/helpers/ConnectedTransactionBuilder.cjs +105 -0
- package/dist/cjs/core/helpers/ConnectedUpdateBuilder.cjs +79 -0
- package/dist/cjs/core/helpers/PaginateBuilder.cjs +178 -0
- package/dist/cjs/core/helpers/ReturningExecutor.cjs +70 -0
- package/dist/cjs/core/helpers/capability-guard.cjs +10 -0
- package/dist/cjs/core/helpers/index.cjs +31 -0
- package/dist/cjs/core/helpers/methods.cjs +10 -0
- package/dist/cjs/core/helpers/query-convenience.cjs +238 -0
- package/dist/cjs/core/helpers/select-joins.cjs +251 -0
- package/dist/cjs/core/helpers/select-pagination.cjs +233 -0
- package/dist/cjs/core/helpers/select-types.cjs +2 -0
- package/dist/cjs/core/pg-family/cockroachdb-client/capabilities.cjs +31 -0
- package/dist/cjs/core/pg-family/cockroachdb-client/index.cjs +7 -0
- package/dist/cjs/core/pg-family/cockroachdb-client/relq-cockroach.cjs +16 -0
- package/dist/cjs/core/pg-family/dsql-client/capabilities.cjs +31 -0
- package/dist/cjs/core/pg-family/dsql-client/index.cjs +7 -0
- package/dist/cjs/core/pg-family/dsql-client/relq-dsql.cjs +16 -0
- package/dist/cjs/core/pg-family/index.cjs +19 -0
- package/dist/cjs/core/pg-family/nile-client/capabilities.cjs +31 -0
- package/dist/cjs/core/pg-family/nile-client/index.cjs +7 -0
- package/dist/cjs/core/pg-family/nile-client/relq-nile.cjs +36 -0
- package/dist/cjs/core/pg-family/nile-client/tenant-context.cjs +44 -0
- package/dist/cjs/core/pg-family/pg-client/capabilities.cjs +31 -0
- package/dist/cjs/core/pg-family/pg-client/index.cjs +7 -0
- package/dist/cjs/core/pg-family/pg-client/relq-postgres.cjs +43 -0
- package/dist/cjs/core/pg-family/shared/pg-base.cjs +385 -0
- package/dist/cjs/core/pg-family/shared/pg-dialect.cjs +67 -0
- package/dist/cjs/core/pg-family/shared/pg-error-parser.cjs +34 -0
- package/dist/cjs/core/pg-family/shared/pg-type-coercion.cjs +14 -0
- package/dist/cjs/core/relq-base.cjs +307 -0
- package/dist/cjs/core/relq-client.cjs +56 -0
- package/dist/cjs/core/shared/cleanup.cjs +36 -0
- package/dist/cjs/core/shared/column-mapping.cjs +97 -0
- package/dist/cjs/core/shared/errors.cjs +17 -0
- package/dist/cjs/core/shared/index.cjs +24 -0
- package/dist/cjs/core/shared/table-accessor.cjs +22 -0
- package/dist/cjs/core/shared/transform.cjs +35 -0
- package/dist/cjs/core/shared/types.cjs +2 -0
- package/dist/cjs/core/shared/validation.cjs +140 -0
- package/dist/cjs/core/types/core.types.cjs +2 -0
- package/dist/cjs/count/count-builder.cjs +88 -0
- package/dist/cjs/count/index.cjs +5 -0
- package/dist/cjs/delete/delete-builder.cjs +176 -0
- package/dist/cjs/delete/index.cjs +5 -0
- package/dist/cjs/explain/explain-builder.cjs +99 -0
- package/dist/cjs/explain/index.cjs +5 -0
- package/dist/cjs/index.cjs +26 -0
- package/dist/cjs/insert/conflict-builder.cjs +213 -0
- package/dist/cjs/insert/index.cjs +5 -0
- package/dist/cjs/insert/insert-builder.cjs +320 -0
- package/dist/cjs/insert/insert-from-select-builder.cjs +86 -0
- package/dist/cjs/pubsub/index.cjs +7 -0
- package/dist/cjs/pubsub/listen-notify-builder.cjs +57 -0
- package/dist/cjs/pubsub/listener-connection.cjs +180 -0
- package/dist/cjs/raw/index.cjs +8 -0
- package/dist/cjs/raw/raw-query-builder.cjs +27 -0
- package/dist/cjs/raw/sql-template.cjs +73 -0
- package/dist/cjs/select/aggregate-builder.cjs +179 -0
- package/dist/cjs/select/index.cjs +16 -0
- package/dist/cjs/select/join-builder.cjs +192 -0
- package/dist/cjs/select/join-condition-builder.cjs +189 -0
- package/dist/cjs/select/join-internals.cjs +5 -0
- package/dist/cjs/select/join-many-condition-builder.cjs +159 -0
- package/dist/cjs/select/scalar-query-builder.cjs +134 -0
- package/dist/cjs/select/scalar-select-builder.cjs +78 -0
- package/dist/cjs/select/select-builder.cjs +426 -0
- package/dist/cjs/select/sql-expression.cjs +38 -0
- package/dist/cjs/select/table-proxy.cjs +99 -0
- package/dist/cjs/shared/aws-dsql.cjs +181 -0
- package/dist/cjs/shared/errors/relq-errors.cjs +361 -0
- package/dist/cjs/shared/pg-format.cjs +383 -0
- package/dist/cjs/shared/types/config-types.cjs +51 -0
- package/dist/cjs/transaction/index.cjs +6 -0
- package/dist/cjs/transaction/transaction-builder.cjs +78 -0
- package/dist/cjs/types/aggregate-types.cjs +2 -0
- package/dist/cjs/types/inference-types.cjs +18 -0
- package/dist/cjs/types/pagination-types.cjs +7 -0
- package/dist/cjs/types/result-types.cjs +2 -0
- package/dist/cjs/types/scalar-types.cjs +2 -0
- package/dist/cjs/types/schema-types.cjs +2 -0
- package/dist/cjs/types/subscription-types.cjs +2 -0
- package/dist/cjs/types.cjs +2 -0
- package/dist/cjs/update/array-update-builder.cjs +232 -0
- package/dist/cjs/update/index.cjs +16 -0
- package/dist/cjs/update/jsonb-update-builder.cjs +219 -0
- package/dist/cjs/update/update-builder.cjs +274 -0
- package/dist/cjs/utils/addon/pg/cursor.cjs +8 -0
- package/dist/cjs/utils/addon/pg/pg.cjs +23 -0
- package/dist/cjs/utils/case-converter.cjs +58 -0
- package/dist/cjs/utils/env-resolver.cjs +226 -0
- package/dist/cjs/utils/environment-detection.cjs +124 -0
- package/dist/cjs/utils/fk-resolver.cjs +186 -0
- package/dist/cjs/utils/index.cjs +25 -0
- package/dist/cjs/utils/pool-defaults.cjs +91 -0
- package/dist/cjs/utils/type-coercion.cjs +120 -0
- package/dist/cjs/window/index.cjs +5 -0
- package/dist/cjs/window/window-builder.cjs +80 -0
- package/dist/esm/__test-types.js +15 -0
- package/dist/esm/addon/cursor.js +1440 -0
- package/dist/esm/addon/pg.js +4931 -0
- package/dist/esm/cache/index.js +1 -0
- package/dist/esm/cache/query-cache.js +303 -0
- package/dist/esm/condition/array-condition-builder.js +519 -0
- package/dist/esm/condition/array-numeric-condition-builder.js +182 -0
- package/dist/esm/condition/array-specialized-condition-builder.js +200 -0
- package/dist/esm/condition/array-string-condition-builder.js +142 -0
- package/dist/esm/condition/base-condition-builder.js +1 -0
- package/dist/esm/condition/condition-collector.js +452 -0
- package/dist/esm/condition/fulltext-condition-builder.js +53 -0
- package/dist/esm/condition/geometric-condition-builder.js +220 -0
- package/dist/esm/condition/index.js +8 -0
- package/dist/esm/condition/jsonb-condition-builder.js +439 -0
- package/dist/esm/condition/network-condition-builder.js +229 -0
- package/dist/esm/condition/postgis-condition-builder.js +180 -0
- package/dist/esm/condition/range-condition-builder.js +90 -0
- package/dist/esm/core/helpers/ConnectedAggregateBuilder.js +128 -0
- package/dist/esm/core/helpers/ConnectedCTEBuilder.js +49 -0
- package/dist/esm/core/helpers/ConnectedCountBuilder.js +69 -0
- package/dist/esm/core/helpers/ConnectedDeleteBuilder.js +61 -0
- package/dist/esm/core/helpers/ConnectedInsertBuilder.js +108 -0
- package/dist/esm/core/helpers/ConnectedInsertFromSelectBuilder.js +62 -0
- package/dist/esm/core/helpers/ConnectedQueryBuilder.js +142 -0
- package/dist/esm/core/helpers/ConnectedRawQueryBuilder.js +42 -0
- package/dist/esm/core/helpers/ConnectedSelectBuilder.js +327 -0
- package/dist/esm/core/helpers/ConnectedTransactionBuilder.js +100 -0
- package/dist/esm/core/helpers/ConnectedUpdateBuilder.js +75 -0
- package/dist/esm/core/helpers/PaginateBuilder.js +174 -0
- package/dist/esm/core/helpers/ReturningExecutor.js +66 -0
- package/dist/esm/core/helpers/capability-guard.js +7 -0
- package/dist/esm/core/helpers/index.js +13 -0
- package/dist/esm/core/helpers/methods.js +6 -0
- package/dist/esm/core/helpers/query-convenience.js +194 -0
- package/dist/esm/core/helpers/select-joins.js +246 -0
- package/dist/esm/core/helpers/select-pagination.js +226 -0
- package/dist/esm/core/helpers/select-types.js +1 -0
- package/dist/esm/core/pg-family/cockroachdb-client/capabilities.js +28 -0
- package/dist/esm/core/pg-family/cockroachdb-client/index.js +2 -0
- package/dist/esm/core/pg-family/cockroachdb-client/relq-cockroach.js +12 -0
- package/dist/esm/core/pg-family/dsql-client/capabilities.js +28 -0
- package/dist/esm/core/pg-family/dsql-client/index.js +2 -0
- package/dist/esm/core/pg-family/dsql-client/relq-dsql.js +12 -0
- package/dist/esm/core/pg-family/index.js +6 -0
- package/dist/esm/core/pg-family/nile-client/capabilities.js +28 -0
- package/dist/esm/core/pg-family/nile-client/index.js +2 -0
- package/dist/esm/core/pg-family/nile-client/relq-nile.js +32 -0
- package/dist/esm/core/pg-family/nile-client/tenant-context.js +40 -0
- package/dist/esm/core/pg-family/pg-client/capabilities.js +28 -0
- package/dist/esm/core/pg-family/pg-client/index.js +2 -0
- package/dist/esm/core/pg-family/pg-client/relq-postgres.js +39 -0
- package/dist/esm/core/pg-family/shared/pg-base.js +347 -0
- package/dist/esm/core/pg-family/shared/pg-dialect.js +63 -0
- package/dist/esm/core/pg-family/shared/pg-error-parser.js +29 -0
- package/dist/esm/core/pg-family/shared/pg-type-coercion.js +6 -0
- package/dist/esm/core/relq-base.js +270 -0
- package/dist/esm/core/relq-client.js +48 -0
- package/dist/esm/core/shared/cleanup.js +27 -0
- package/dist/esm/core/shared/column-mapping.js +90 -0
- package/dist/esm/core/shared/errors.js +13 -0
- package/dist/esm/core/shared/index.js +6 -0
- package/dist/esm/core/shared/table-accessor.js +19 -0
- package/dist/esm/core/shared/transform.js +30 -0
- package/dist/esm/core/shared/types.js +1 -0
- package/dist/esm/core/shared/validation.js +136 -0
- package/dist/esm/core/types/core.types.js +1 -0
- package/dist/esm/count/count-builder.js +81 -0
- package/dist/esm/count/index.js +1 -0
- package/dist/esm/delete/delete-builder.js +169 -0
- package/dist/esm/delete/index.js +1 -0
- package/dist/esm/explain/explain-builder.js +95 -0
- package/dist/esm/explain/index.js +1 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/insert/conflict-builder.js +202 -0
- package/dist/esm/insert/index.js +1 -0
- package/dist/esm/insert/insert-builder.js +313 -0
- package/dist/esm/insert/insert-from-select-builder.js +79 -0
- package/dist/esm/pubsub/index.js +1 -0
- package/dist/esm/pubsub/listen-notify-builder.js +48 -0
- package/dist/esm/pubsub/listener-connection.js +173 -0
- package/dist/esm/raw/index.js +2 -0
- package/dist/esm/raw/raw-query-builder.js +20 -0
- package/dist/esm/raw/sql-template.js +66 -0
- package/dist/esm/select/aggregate-builder.js +172 -0
- package/dist/esm/select/index.js +4 -0
- package/dist/esm/select/join-builder.js +184 -0
- package/dist/esm/select/join-condition-builder.js +181 -0
- package/dist/esm/select/join-internals.js +2 -0
- package/dist/esm/select/join-many-condition-builder.js +151 -0
- package/dist/esm/select/scalar-query-builder.js +126 -0
- package/dist/esm/select/scalar-select-builder.js +70 -0
- package/dist/esm/select/select-builder.js +419 -0
- package/dist/esm/select/sql-expression.js +33 -0
- package/dist/esm/select/table-proxy.js +91 -0
- package/dist/esm/shared/aws-dsql.js +140 -0
- package/dist/esm/shared/errors/relq-errors.js +339 -0
- package/dist/esm/shared/pg-format.js +375 -0
- package/dist/esm/shared/types/config-types.js +46 -0
- package/dist/esm/transaction/index.js +1 -0
- package/dist/esm/transaction/transaction-builder.js +70 -0
- package/dist/esm/types/aggregate-types.js +1 -0
- package/dist/esm/types/inference-types.js +12 -0
- package/dist/esm/types/pagination-types.js +4 -0
- package/dist/esm/types/result-types.js +1 -0
- package/dist/esm/types/scalar-types.js +1 -0
- package/dist/esm/types/schema-types.js +1 -0
- package/dist/esm/types/subscription-types.js +1 -0
- package/dist/esm/types.js +1 -0
- package/dist/esm/update/array-update-builder.js +219 -0
- package/dist/esm/update/index.js +3 -0
- package/dist/esm/update/jsonb-update-builder.js +211 -0
- package/dist/esm/update/update-builder.js +267 -0
- package/dist/esm/utils/addon/pg/cursor.js +1 -0
- package/dist/esm/utils/addon/pg/pg.js +2 -0
- package/dist/esm/utils/case-converter.js +55 -0
- package/dist/esm/utils/env-resolver.js +213 -0
- package/dist/esm/utils/environment-detection.js +114 -0
- package/dist/esm/utils/fk-resolver.js +178 -0
- package/dist/esm/utils/index.js +4 -0
- package/dist/esm/utils/pool-defaults.js +85 -0
- package/dist/esm/utils/type-coercion.js +112 -0
- package/dist/esm/window/index.js +1 -0
- package/dist/esm/window/window-builder.js +73 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +7281 -0
- package/dist/index.js +1 -0
- package/package.json +52 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import format from "../shared/pg-format.js";
|
|
2
|
+
export class SqlFragment {
|
|
3
|
+
text;
|
|
4
|
+
_isSqlFragment = true;
|
|
5
|
+
constructor(text) {
|
|
6
|
+
this.text = text;
|
|
7
|
+
}
|
|
8
|
+
toString() {
|
|
9
|
+
return this.text;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
function sqlIdentifier(name) {
|
|
13
|
+
return new SqlFragment(format.ident(name));
|
|
14
|
+
}
|
|
15
|
+
function sqlRaw(text) {
|
|
16
|
+
return new SqlFragment(text);
|
|
17
|
+
}
|
|
18
|
+
function sql(strings, ...values) {
|
|
19
|
+
let result = '';
|
|
20
|
+
for (let i = 0; i < strings.length; i++) {
|
|
21
|
+
result += strings[i];
|
|
22
|
+
if (i < values.length) {
|
|
23
|
+
const value = values[i];
|
|
24
|
+
result += formatValue(value);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return new SqlFragment(result);
|
|
28
|
+
}
|
|
29
|
+
function formatValue(value) {
|
|
30
|
+
if (value && value._isSqlFragment === true) {
|
|
31
|
+
return value.text;
|
|
32
|
+
}
|
|
33
|
+
if (value === null || value === undefined) {
|
|
34
|
+
return 'NULL';
|
|
35
|
+
}
|
|
36
|
+
if (value instanceof Date) {
|
|
37
|
+
return format('%L', value.toISOString());
|
|
38
|
+
}
|
|
39
|
+
if (Array.isArray(value)) {
|
|
40
|
+
if (value.length === 0) {
|
|
41
|
+
return 'ARRAY[]';
|
|
42
|
+
}
|
|
43
|
+
const escaped = value.map(v => formatValue(v)).join(', ');
|
|
44
|
+
return `(${escaped})`;
|
|
45
|
+
}
|
|
46
|
+
if (typeof value === 'boolean') {
|
|
47
|
+
return value ? 'true' : 'false';
|
|
48
|
+
}
|
|
49
|
+
if (typeof value === 'number') {
|
|
50
|
+
return String(value);
|
|
51
|
+
}
|
|
52
|
+
if (typeof value === 'bigint') {
|
|
53
|
+
return String(value);
|
|
54
|
+
}
|
|
55
|
+
if (typeof value === 'string') {
|
|
56
|
+
return format('%L', value);
|
|
57
|
+
}
|
|
58
|
+
if (typeof value === 'object') {
|
|
59
|
+
return format('%L', JSON.stringify(value)) + '::jsonb';
|
|
60
|
+
}
|
|
61
|
+
return format('%L', String(value));
|
|
62
|
+
}
|
|
63
|
+
sql.id = sqlIdentifier;
|
|
64
|
+
sql.raw = sqlRaw;
|
|
65
|
+
sql.fragment = sqlRaw;
|
|
66
|
+
export { sql };
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import format from "../shared/pg-format.js";
|
|
2
|
+
import { ConditionCollector, buildConditionsSQL } from "../condition/condition-collector.js";
|
|
3
|
+
const NUMERIC_FUNCS = new Set(['COUNT', 'SUM', 'AVG', 'MIN', 'MAX']);
|
|
4
|
+
export class AggregateQueryBuilder {
|
|
5
|
+
tableName;
|
|
6
|
+
groupByColumns = [];
|
|
7
|
+
entries = [];
|
|
8
|
+
whereConditions = [];
|
|
9
|
+
havingConditions = [];
|
|
10
|
+
orderByColumns = [];
|
|
11
|
+
limitValue;
|
|
12
|
+
offsetValue;
|
|
13
|
+
constructor(tableName) {
|
|
14
|
+
this.tableName = tableName;
|
|
15
|
+
}
|
|
16
|
+
groupBy(...columns) {
|
|
17
|
+
this.groupByColumns.push(...columns);
|
|
18
|
+
return this;
|
|
19
|
+
}
|
|
20
|
+
count(alias = 'count') {
|
|
21
|
+
this.entries.push({ func: 'COUNT', column: '*', alias });
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
countColumn(column, alias) {
|
|
25
|
+
this.entries.push({ func: 'COUNT', column, alias: alias || column });
|
|
26
|
+
return this;
|
|
27
|
+
}
|
|
28
|
+
countDistinct(column, alias) {
|
|
29
|
+
this.entries.push({ func: 'COUNT', column, alias: alias || column, distinct: true });
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
sum(column, alias, filter) {
|
|
33
|
+
const entry = { func: 'SUM', column, alias: alias || column };
|
|
34
|
+
if (filter) {
|
|
35
|
+
const collector = new ConditionCollector();
|
|
36
|
+
filter(collector);
|
|
37
|
+
entry.filterConditions = collector.getConditions();
|
|
38
|
+
}
|
|
39
|
+
this.entries.push(entry);
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
avg(column, alias, filter) {
|
|
43
|
+
const entry = { func: 'AVG', column, alias: alias || column };
|
|
44
|
+
if (filter) {
|
|
45
|
+
const collector = new ConditionCollector();
|
|
46
|
+
filter(collector);
|
|
47
|
+
entry.filterConditions = collector.getConditions();
|
|
48
|
+
}
|
|
49
|
+
this.entries.push(entry);
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
min(column, alias) {
|
|
53
|
+
this.entries.push({ func: 'MIN', column, alias: alias || column });
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
max(column, alias) {
|
|
57
|
+
this.entries.push({ func: 'MAX', column, alias: alias || column });
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
arrayAgg(column, alias) {
|
|
61
|
+
this.entries.push({ func: 'ARRAY_AGG', column, alias: alias || column });
|
|
62
|
+
return this;
|
|
63
|
+
}
|
|
64
|
+
stringAgg(column, delimiter = ', ', alias) {
|
|
65
|
+
this.entries.push({ func: 'STRING_AGG', column, alias: alias || column, delimiter });
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
jsonAgg(column, alias) {
|
|
69
|
+
this.entries.push({ func: 'JSON_AGG', column, alias: alias || column });
|
|
70
|
+
return this;
|
|
71
|
+
}
|
|
72
|
+
jsonbAgg(column, alias) {
|
|
73
|
+
this.entries.push({ func: 'JSONB_AGG', column, alias: alias || column });
|
|
74
|
+
return this;
|
|
75
|
+
}
|
|
76
|
+
boolAnd(column, alias) {
|
|
77
|
+
this.entries.push({ func: 'BOOL_AND', column, alias: alias || column });
|
|
78
|
+
return this;
|
|
79
|
+
}
|
|
80
|
+
boolOr(column, alias) {
|
|
81
|
+
this.entries.push({ func: 'BOOL_OR', column, alias: alias || column });
|
|
82
|
+
return this;
|
|
83
|
+
}
|
|
84
|
+
where(callback) {
|
|
85
|
+
const collector = new ConditionCollector();
|
|
86
|
+
callback(collector);
|
|
87
|
+
this.whereConditions.push(...collector.getConditions());
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
having(callback) {
|
|
91
|
+
const collector = new ConditionCollector();
|
|
92
|
+
callback(collector);
|
|
93
|
+
this.havingConditions.push(...collector.getConditions());
|
|
94
|
+
return this;
|
|
95
|
+
}
|
|
96
|
+
orderBy(column, direction = 'ASC') {
|
|
97
|
+
this.orderByColumns.push({ column, direction });
|
|
98
|
+
return this;
|
|
99
|
+
}
|
|
100
|
+
limit(count) {
|
|
101
|
+
this.limitValue = count;
|
|
102
|
+
return this;
|
|
103
|
+
}
|
|
104
|
+
offset(count) {
|
|
105
|
+
this.offsetValue = count;
|
|
106
|
+
return this;
|
|
107
|
+
}
|
|
108
|
+
getNumericAliases() {
|
|
109
|
+
return this.entries
|
|
110
|
+
.filter(e => NUMERIC_FUNCS.has(e.func))
|
|
111
|
+
.map(e => e.alias);
|
|
112
|
+
}
|
|
113
|
+
getGroupByColumns() {
|
|
114
|
+
return this.groupByColumns;
|
|
115
|
+
}
|
|
116
|
+
toString() {
|
|
117
|
+
const selectParts = [];
|
|
118
|
+
for (const col of this.groupByColumns) {
|
|
119
|
+
selectParts.push(format.ident(col));
|
|
120
|
+
}
|
|
121
|
+
for (const entry of this.entries) {
|
|
122
|
+
selectParts.push(this.buildEntrySQL(entry));
|
|
123
|
+
}
|
|
124
|
+
if (selectParts.length === 0) {
|
|
125
|
+
selectParts.push('COUNT(*) AS count');
|
|
126
|
+
}
|
|
127
|
+
let query = format('SELECT %s FROM %I', selectParts.join(', '), this.tableName);
|
|
128
|
+
if (this.whereConditions.length > 0) {
|
|
129
|
+
query += ' WHERE ' + buildConditionsSQL(this.whereConditions);
|
|
130
|
+
}
|
|
131
|
+
if (this.groupByColumns.length > 0) {
|
|
132
|
+
query += ' GROUP BY ' + this.groupByColumns.map(col => format.ident(col)).join(', ');
|
|
133
|
+
}
|
|
134
|
+
if (this.havingConditions.length > 0) {
|
|
135
|
+
query += ' HAVING ' + buildConditionsSQL(this.havingConditions);
|
|
136
|
+
}
|
|
137
|
+
if (this.orderByColumns.length > 0) {
|
|
138
|
+
query += ' ORDER BY ' + this.orderByColumns
|
|
139
|
+
.map(o => `${format.ident(o.column)} ${o.direction}`)
|
|
140
|
+
.join(', ');
|
|
141
|
+
}
|
|
142
|
+
if (this.limitValue !== undefined) {
|
|
143
|
+
query += format(' LIMIT %s', this.limitValue);
|
|
144
|
+
}
|
|
145
|
+
if (this.offsetValue !== undefined) {
|
|
146
|
+
query += format(' OFFSET %s', this.offsetValue);
|
|
147
|
+
}
|
|
148
|
+
return query;
|
|
149
|
+
}
|
|
150
|
+
buildEntrySQL(entry) {
|
|
151
|
+
let columnExpr;
|
|
152
|
+
if (entry.column === '*') {
|
|
153
|
+
columnExpr = '*';
|
|
154
|
+
}
|
|
155
|
+
else if (entry.distinct) {
|
|
156
|
+
columnExpr = `DISTINCT ${format.ident(entry.column)}`;
|
|
157
|
+
}
|
|
158
|
+
else if (entry.func === 'STRING_AGG' && entry.delimiter) {
|
|
159
|
+
columnExpr = `${format.ident(entry.column)}, ${format.literal(entry.delimiter)}`;
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
columnExpr = format.ident(entry.column);
|
|
163
|
+
}
|
|
164
|
+
let sql = `${entry.func}(${columnExpr})`;
|
|
165
|
+
if (entry.filterConditions && entry.filterConditions.length > 0) {
|
|
166
|
+
const filterSQL = buildConditionsSQL(entry.filterConditions);
|
|
167
|
+
sql += ` FILTER (WHERE ${filterSQL})`;
|
|
168
|
+
}
|
|
169
|
+
sql += ` AS ${format.ident(entry.alias)}`;
|
|
170
|
+
return sql;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { SelectBuilder } from "./select-builder.js";
|
|
2
|
+
export { createTableProxy, columnRefToSQL, columnRefToSQLUnqualified, isColumnRef } from "./table-proxy.js";
|
|
3
|
+
export { JoinConditionBuilder, createJoinConditionBuilder } from "./join-condition-builder.js";
|
|
4
|
+
export { JoinManyBuilder, createJoinManyBuilder } from "./join-builder.js";
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { JoinConditionBuilder } from "./join-condition-builder.js";
|
|
2
|
+
import { columnRefToSQL, columnRefToSQLUnqualified } from "./table-proxy.js";
|
|
3
|
+
import { ConditionCollector, buildConditionsSQL } from "../condition/condition-collector.js";
|
|
4
|
+
import format from "../shared/pg-format.js";
|
|
5
|
+
export class JoinManyBuilder extends JoinConditionBuilder {
|
|
6
|
+
orderBySpecs = [];
|
|
7
|
+
selectColumns = [];
|
|
8
|
+
limitValue;
|
|
9
|
+
offsetValue;
|
|
10
|
+
groupByColumns = [];
|
|
11
|
+
havingConditions = [];
|
|
12
|
+
selectRefs(columns) {
|
|
13
|
+
this.selectColumns = columns;
|
|
14
|
+
return this;
|
|
15
|
+
}
|
|
16
|
+
orderBy(column, direction = 'ASC') {
|
|
17
|
+
this.orderBySpecs.push({ column, direction });
|
|
18
|
+
return this;
|
|
19
|
+
}
|
|
20
|
+
orderByNulls(column, direction, nulls) {
|
|
21
|
+
this.orderBySpecs.push({ column, direction, nulls });
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
limit(count) {
|
|
25
|
+
this.limitValue = count;
|
|
26
|
+
return this;
|
|
27
|
+
}
|
|
28
|
+
offset(count) {
|
|
29
|
+
this.offsetValue = count;
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
groupBy(...columns) {
|
|
33
|
+
this.groupByColumns.push(...columns);
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
having(callback) {
|
|
37
|
+
const collector = new ConditionCollector();
|
|
38
|
+
callback(collector);
|
|
39
|
+
this.havingConditions.push(...collector.getConditions());
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
hasQueryModifiers() {
|
|
43
|
+
return (this.orderBySpecs.length > 0 ||
|
|
44
|
+
this.selectColumns.length > 0 ||
|
|
45
|
+
this.limitValue !== undefined ||
|
|
46
|
+
this.offsetValue !== undefined ||
|
|
47
|
+
this.groupByColumns.length > 0 ||
|
|
48
|
+
this.havingConditions.length > 0);
|
|
49
|
+
}
|
|
50
|
+
getSelectColumns() {
|
|
51
|
+
return [...this.selectColumns];
|
|
52
|
+
}
|
|
53
|
+
getOrderBySpecs() {
|
|
54
|
+
return [...this.orderBySpecs];
|
|
55
|
+
}
|
|
56
|
+
getLimit() {
|
|
57
|
+
return this.limitValue;
|
|
58
|
+
}
|
|
59
|
+
getOffset() {
|
|
60
|
+
return this.offsetValue;
|
|
61
|
+
}
|
|
62
|
+
toLateralSQL(rightTable, rightAlias, leftAlias) {
|
|
63
|
+
const selectPart = this.buildSelectPart();
|
|
64
|
+
const fromPart = format.ident(rightTable);
|
|
65
|
+
const wherePart = this.buildWherePart(leftAlias);
|
|
66
|
+
const groupByPart = this.buildGroupByPart();
|
|
67
|
+
const havingPart = this.buildHavingPart();
|
|
68
|
+
const orderByPart = this.buildOrderByPart();
|
|
69
|
+
const limitPart = this.buildLimitPart();
|
|
70
|
+
let innerQuery = `SELECT ${selectPart} FROM ${fromPart}`;
|
|
71
|
+
if (wherePart) {
|
|
72
|
+
innerQuery += ` WHERE ${wherePart}`;
|
|
73
|
+
}
|
|
74
|
+
if (groupByPart) {
|
|
75
|
+
innerQuery += ` GROUP BY ${groupByPart}`;
|
|
76
|
+
}
|
|
77
|
+
if (havingPart) {
|
|
78
|
+
innerQuery += ` HAVING ${havingPart}`;
|
|
79
|
+
}
|
|
80
|
+
if (orderByPart) {
|
|
81
|
+
innerQuery += ` ORDER BY ${orderByPart}`;
|
|
82
|
+
}
|
|
83
|
+
if (limitPart) {
|
|
84
|
+
innerQuery += limitPart;
|
|
85
|
+
}
|
|
86
|
+
const wrappedQuery = `SELECT COALESCE(json_agg(sub.*), '[]'::json) as ${format.ident(rightAlias)} FROM (${innerQuery}) sub`;
|
|
87
|
+
return `(${wrappedQuery})`;
|
|
88
|
+
}
|
|
89
|
+
toSubquerySQL(rightTable, leftAlias) {
|
|
90
|
+
const selectPart = this.buildSelectPart();
|
|
91
|
+
const fromPart = format.ident(rightTable);
|
|
92
|
+
const wherePart = this.buildWherePart(leftAlias);
|
|
93
|
+
const groupByPart = this.buildGroupByPart();
|
|
94
|
+
const havingPart = this.buildHavingPart();
|
|
95
|
+
const orderByPart = this.buildOrderByPart();
|
|
96
|
+
const limitPart = this.buildLimitPart();
|
|
97
|
+
let query = `SELECT ${selectPart} FROM ${fromPart}`;
|
|
98
|
+
if (wherePart) {
|
|
99
|
+
query += ` WHERE ${wherePart}`;
|
|
100
|
+
}
|
|
101
|
+
if (groupByPart) {
|
|
102
|
+
query += ` GROUP BY ${groupByPart}`;
|
|
103
|
+
}
|
|
104
|
+
if (havingPart) {
|
|
105
|
+
query += ` HAVING ${havingPart}`;
|
|
106
|
+
}
|
|
107
|
+
if (orderByPart) {
|
|
108
|
+
query += ` ORDER BY ${orderByPart}`;
|
|
109
|
+
}
|
|
110
|
+
if (limitPart) {
|
|
111
|
+
query += limitPart;
|
|
112
|
+
}
|
|
113
|
+
return query;
|
|
114
|
+
}
|
|
115
|
+
buildSelectPart() {
|
|
116
|
+
if (this.selectColumns.length === 0) {
|
|
117
|
+
return '*';
|
|
118
|
+
}
|
|
119
|
+
return this.selectColumns
|
|
120
|
+
.map(col => columnRefToSQLUnqualified(col))
|
|
121
|
+
.join(', ');
|
|
122
|
+
}
|
|
123
|
+
buildWherePart(leftAlias) {
|
|
124
|
+
const parts = [];
|
|
125
|
+
for (const cond of this.conditions) {
|
|
126
|
+
if (cond.type === 'using' || cond.type === 'raw') {
|
|
127
|
+
if (cond.raw)
|
|
128
|
+
parts.push(cond.raw);
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
const leftSQL = columnRefToSQL(cond.left);
|
|
132
|
+
const rightSQL = this.formatRightSide(cond.right);
|
|
133
|
+
parts.push(`${leftSQL} ${cond.operator} ${rightSQL}`);
|
|
134
|
+
}
|
|
135
|
+
if (this.whereConditions.length > 0) {
|
|
136
|
+
const whereSQL = buildConditionsSQL(this.whereConditions);
|
|
137
|
+
if (whereSQL) {
|
|
138
|
+
parts.push(whereSQL);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return parts.length > 0 ? parts.join(' AND ') : null;
|
|
142
|
+
}
|
|
143
|
+
buildGroupByPart() {
|
|
144
|
+
if (this.groupByColumns.length === 0) {
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
return this.groupByColumns
|
|
148
|
+
.map(col => columnRefToSQLUnqualified(col))
|
|
149
|
+
.join(', ');
|
|
150
|
+
}
|
|
151
|
+
buildHavingPart() {
|
|
152
|
+
if (this.havingConditions.length === 0) {
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
return buildConditionsSQL(this.havingConditions);
|
|
156
|
+
}
|
|
157
|
+
buildOrderByPart() {
|
|
158
|
+
if (this.orderBySpecs.length === 0) {
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
return this.orderBySpecs
|
|
162
|
+
.map(spec => {
|
|
163
|
+
let sql = `${columnRefToSQLUnqualified(spec.column)} ${spec.direction}`;
|
|
164
|
+
if (spec.nulls) {
|
|
165
|
+
sql += ` NULLS ${spec.nulls}`;
|
|
166
|
+
}
|
|
167
|
+
return sql;
|
|
168
|
+
})
|
|
169
|
+
.join(', ');
|
|
170
|
+
}
|
|
171
|
+
buildLimitPart() {
|
|
172
|
+
let sql = '';
|
|
173
|
+
if (this.limitValue !== undefined) {
|
|
174
|
+
sql += ` LIMIT ${this.limitValue}`;
|
|
175
|
+
}
|
|
176
|
+
if (this.offsetValue !== undefined) {
|
|
177
|
+
sql += ` OFFSET ${this.offsetValue}`;
|
|
178
|
+
}
|
|
179
|
+
return sql;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
export function createJoinManyBuilder() {
|
|
183
|
+
return new JoinManyBuilder();
|
|
184
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { columnRefToSQL, isColumnRef } from "./table-proxy.js";
|
|
2
|
+
import { ConditionCollector, buildConditionsSQL } from "../condition/condition-collector.js";
|
|
3
|
+
import format from "../shared/pg-format.js";
|
|
4
|
+
import { JOIN_INTERNAL } from "./join-internals.js";
|
|
5
|
+
export class JoinConditionBuilder {
|
|
6
|
+
conditions = [];
|
|
7
|
+
whereConditions = [];
|
|
8
|
+
selectedColumns;
|
|
9
|
+
equal(left, right) {
|
|
10
|
+
this.conditions.push({
|
|
11
|
+
type: 'equal',
|
|
12
|
+
left,
|
|
13
|
+
right,
|
|
14
|
+
operator: '='
|
|
15
|
+
});
|
|
16
|
+
return this;
|
|
17
|
+
}
|
|
18
|
+
notEqual(left, right) {
|
|
19
|
+
this.conditions.push({
|
|
20
|
+
type: 'notEqual',
|
|
21
|
+
left,
|
|
22
|
+
right,
|
|
23
|
+
operator: '!='
|
|
24
|
+
});
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
greaterThan(left, right) {
|
|
28
|
+
this.conditions.push({
|
|
29
|
+
type: 'greaterThan',
|
|
30
|
+
left,
|
|
31
|
+
right,
|
|
32
|
+
operator: '>'
|
|
33
|
+
});
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
lessThan(left, right) {
|
|
37
|
+
this.conditions.push({
|
|
38
|
+
type: 'lessThan',
|
|
39
|
+
left,
|
|
40
|
+
right,
|
|
41
|
+
operator: '<'
|
|
42
|
+
});
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
|
+
greaterThanOrEqual(left, right) {
|
|
46
|
+
this.conditions.push({
|
|
47
|
+
type: 'greaterThanOrEqual',
|
|
48
|
+
left,
|
|
49
|
+
right,
|
|
50
|
+
operator: '>='
|
|
51
|
+
});
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
lessThanOrEqual(left, right) {
|
|
55
|
+
this.conditions.push({
|
|
56
|
+
type: 'lessThanOrEqual',
|
|
57
|
+
left,
|
|
58
|
+
right,
|
|
59
|
+
operator: '<='
|
|
60
|
+
});
|
|
61
|
+
return this;
|
|
62
|
+
}
|
|
63
|
+
like(left, right) {
|
|
64
|
+
this.conditions.push({
|
|
65
|
+
type: 'like',
|
|
66
|
+
left,
|
|
67
|
+
right,
|
|
68
|
+
operator: 'LIKE'
|
|
69
|
+
});
|
|
70
|
+
return this;
|
|
71
|
+
}
|
|
72
|
+
ilike(left, right) {
|
|
73
|
+
this.conditions.push({
|
|
74
|
+
type: 'ilike',
|
|
75
|
+
left,
|
|
76
|
+
right,
|
|
77
|
+
operator: 'ILIKE'
|
|
78
|
+
});
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
using(...columns) {
|
|
82
|
+
this.conditions.push({
|
|
83
|
+
type: 'using',
|
|
84
|
+
columns: columns
|
|
85
|
+
});
|
|
86
|
+
return this;
|
|
87
|
+
}
|
|
88
|
+
where(callback) {
|
|
89
|
+
const collector = new ConditionCollector();
|
|
90
|
+
callback(collector);
|
|
91
|
+
this.whereConditions.push(...collector.getConditions());
|
|
92
|
+
return this;
|
|
93
|
+
}
|
|
94
|
+
select(...args) {
|
|
95
|
+
if (args.length === 1 && Array.isArray(args[0])) {
|
|
96
|
+
this.selectedColumns = args[0];
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
this.selectedColumns = args;
|
|
100
|
+
}
|
|
101
|
+
return this;
|
|
102
|
+
}
|
|
103
|
+
get [JOIN_INTERNAL]() {
|
|
104
|
+
return {
|
|
105
|
+
toSQL: () => this.internalToSQL(),
|
|
106
|
+
isUsingJoin: () => this.internalIsUsingJoin(),
|
|
107
|
+
getUsingColumns: () => this.internalGetUsingColumns(),
|
|
108
|
+
toUsingSQL: () => this.internalToUsingSQL(),
|
|
109
|
+
getConditions: () => [...this.conditions],
|
|
110
|
+
getWhereConditions: () => [...this.whereConditions],
|
|
111
|
+
hasConditions: () => this.conditions.length > 0,
|
|
112
|
+
hasWhereConditions: () => this.whereConditions.length > 0,
|
|
113
|
+
getSelectedColumns: () => this.selectedColumns ?? null,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
internalToSQL() {
|
|
117
|
+
const parts = [];
|
|
118
|
+
for (const cond of this.conditions) {
|
|
119
|
+
switch (cond.type) {
|
|
120
|
+
case 'using':
|
|
121
|
+
break;
|
|
122
|
+
case 'raw':
|
|
123
|
+
parts.push(cond.raw);
|
|
124
|
+
break;
|
|
125
|
+
default:
|
|
126
|
+
const leftSQL = columnRefToSQL(cond.left);
|
|
127
|
+
const rightSQL = this.formatRightSide(cond.right);
|
|
128
|
+
parts.push(`${leftSQL} ${cond.operator} ${rightSQL}`);
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (this.whereConditions.length > 0) {
|
|
133
|
+
const whereSQL = buildConditionsSQL(this.whereConditions);
|
|
134
|
+
if (whereSQL) {
|
|
135
|
+
parts.push(whereSQL);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return parts.join(' AND ');
|
|
139
|
+
}
|
|
140
|
+
internalIsUsingJoin() {
|
|
141
|
+
return this.conditions.length === 1 && this.conditions[0].type === 'using';
|
|
142
|
+
}
|
|
143
|
+
internalGetUsingColumns() {
|
|
144
|
+
if (!this.internalIsUsingJoin())
|
|
145
|
+
return null;
|
|
146
|
+
return this.conditions[0].columns || null;
|
|
147
|
+
}
|
|
148
|
+
internalToUsingSQL() {
|
|
149
|
+
const columns = this.internalGetUsingColumns();
|
|
150
|
+
if (!columns)
|
|
151
|
+
return null;
|
|
152
|
+
return `USING (${columns.map(c => format.ident(c)).join(', ')})`;
|
|
153
|
+
}
|
|
154
|
+
formatRightSide(right) {
|
|
155
|
+
if (isColumnRef(right)) {
|
|
156
|
+
return columnRefToSQL(right);
|
|
157
|
+
}
|
|
158
|
+
if (right === null) {
|
|
159
|
+
return 'NULL';
|
|
160
|
+
}
|
|
161
|
+
if (typeof right === 'boolean') {
|
|
162
|
+
return right ? 'true' : 'false';
|
|
163
|
+
}
|
|
164
|
+
if (typeof right === 'number') {
|
|
165
|
+
return String(right);
|
|
166
|
+
}
|
|
167
|
+
if (typeof right === 'string') {
|
|
168
|
+
return format.literal(right);
|
|
169
|
+
}
|
|
170
|
+
if (right instanceof Date) {
|
|
171
|
+
return format.literal(right.toISOString());
|
|
172
|
+
}
|
|
173
|
+
if (Array.isArray(right)) {
|
|
174
|
+
return `ARRAY[${right.map(v => this.formatRightSide(v)).join(', ')}]`;
|
|
175
|
+
}
|
|
176
|
+
return format.literal(JSON.stringify(right));
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
export function createJoinConditionBuilder() {
|
|
180
|
+
return new JoinConditionBuilder();
|
|
181
|
+
}
|