relq 1.0.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 +862 -0
- package/dist/addons/buffer.js +1869 -0
- package/dist/addons/pg-cursor.js +1425 -0
- package/dist/addons/pg-format.js +2248 -0
- package/dist/addons/pg.js +4790 -0
- package/dist/bin/relq.js +2 -0
- package/dist/cjs/cache/index.cjs +9 -0
- package/dist/cjs/cache/query-cache.cjs +311 -0
- package/dist/cjs/cli/commands/add.cjs +82 -0
- package/dist/cjs/cli/commands/commit.cjs +145 -0
- package/dist/cjs/cli/commands/diff.cjs +84 -0
- package/dist/cjs/cli/commands/export.cjs +333 -0
- package/dist/cjs/cli/commands/fetch.cjs +59 -0
- package/dist/cjs/cli/commands/generate.cjs +242 -0
- package/dist/cjs/cli/commands/history.cjs +165 -0
- package/dist/cjs/cli/commands/import.cjs +524 -0
- package/dist/cjs/cli/commands/init.cjs +437 -0
- package/dist/cjs/cli/commands/introspect.cjs +142 -0
- package/dist/cjs/cli/commands/log.cjs +62 -0
- package/dist/cjs/cli/commands/migrate.cjs +167 -0
- package/dist/cjs/cli/commands/pull.cjs +410 -0
- package/dist/cjs/cli/commands/push.cjs +165 -0
- package/dist/cjs/cli/commands/rollback.cjs +169 -0
- package/dist/cjs/cli/commands/status.cjs +110 -0
- package/dist/cjs/cli/commands/sync.cjs +79 -0
- package/dist/cjs/cli/index.cjs +275 -0
- package/dist/cjs/cli/utils/change-tracker.cjs +446 -0
- package/dist/cjs/cli/utils/commit-manager.cjs +239 -0
- package/dist/cjs/cli/utils/config-loader.cjs +127 -0
- package/dist/cjs/cli/utils/env-loader.cjs +62 -0
- package/dist/cjs/cli/utils/fast-introspect.cjs +398 -0
- package/dist/cjs/cli/utils/git-utils.cjs +404 -0
- package/dist/cjs/cli/utils/migration-generator.cjs +269 -0
- package/dist/cjs/cli/utils/relqignore.cjs +114 -0
- package/dist/cjs/cli/utils/repo-manager.cjs +515 -0
- package/dist/cjs/cli/utils/schema-comparator.cjs +313 -0
- package/dist/cjs/cli/utils/schema-diff.cjs +284 -0
- package/dist/cjs/cli/utils/schema-hash.cjs +108 -0
- package/dist/cjs/cli/utils/schema-introspect.cjs +455 -0
- package/dist/cjs/cli/utils/snapshot-manager.cjs +223 -0
- package/dist/cjs/cli/utils/spinner.cjs +108 -0
- package/dist/cjs/cli/utils/sql-generator.cjs +520 -0
- package/dist/cjs/cli/utils/sql-parser.cjs +999 -0
- package/dist/cjs/cli/utils/type-generator.cjs +2061 -0
- package/dist/cjs/condition/array-condition-builder.cjs +503 -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 +284 -0
- package/dist/cjs/condition/fulltext-condition-builder.cjs +61 -0
- package/dist/cjs/condition/geometric-condition-builder.cjs +208 -0
- package/dist/cjs/condition/index.cjs +25 -0
- package/dist/cjs/condition/jsonb-condition-builder.cjs +160 -0
- package/dist/cjs/condition/network-condition-builder.cjs +230 -0
- package/dist/cjs/condition/range-condition-builder.cjs +82 -0
- package/dist/cjs/config/config.cjs +190 -0
- package/dist/cjs/config/index.cjs +9 -0
- package/dist/cjs/constants/pg-values.cjs +68 -0
- package/dist/cjs/copy/copy-builder.cjs +316 -0
- package/dist/cjs/copy/index.cjs +6 -0
- package/dist/cjs/core/query-builder.cjs +440 -0
- package/dist/cjs/core/relq-client.cjs +1831 -0
- package/dist/cjs/core/typed-kuery-client.cjs +2 -0
- package/dist/cjs/count/count-builder.cjs +88 -0
- package/dist/cjs/count/index.cjs +5 -0
- package/dist/cjs/cte/cte-builder.cjs +89 -0
- package/dist/cjs/cte/index.cjs +5 -0
- package/dist/cjs/ddl/function.cjs +48 -0
- package/dist/cjs/ddl/index.cjs +7 -0
- package/dist/cjs/ddl/sql.cjs +54 -0
- package/dist/cjs/delete/delete-builder.cjs +135 -0
- package/dist/cjs/delete/index.cjs +5 -0
- package/dist/cjs/errors/relq-errors.cjs +329 -0
- package/dist/cjs/examples/fulltext-search-test.cjs +122 -0
- package/dist/cjs/explain/explain-builder.cjs +99 -0
- package/dist/cjs/explain/index.cjs +5 -0
- package/dist/cjs/function/create-function-builder.cjs +196 -0
- package/dist/cjs/function/index.cjs +6 -0
- package/dist/cjs/functions/advanced-functions.cjs +241 -0
- package/dist/cjs/functions/case-builder.cjs +66 -0
- package/dist/cjs/functions/geometric-functions.cjs +104 -0
- package/dist/cjs/functions/index.cjs +184 -0
- package/dist/cjs/functions/network-functions.cjs +86 -0
- package/dist/cjs/functions/sql-functions.cjs +431 -0
- package/dist/cjs/index.cjs +164 -0
- package/dist/cjs/indexing/create-index-builder.cjs +187 -0
- package/dist/cjs/indexing/drop-index-builder.cjs +89 -0
- package/dist/cjs/indexing/index-types.cjs +2 -0
- package/dist/cjs/indexing/index.cjs +8 -0
- package/dist/cjs/insert/conflict-builder.cjs +173 -0
- package/dist/cjs/insert/index.cjs +5 -0
- package/dist/cjs/insert/insert-builder.cjs +254 -0
- package/dist/cjs/introspect/index.cjs +229 -0
- package/dist/cjs/maintenance/index.cjs +6 -0
- package/dist/cjs/maintenance/vacuum-builder.cjs +166 -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 +5 -0
- package/dist/cjs/raw/raw-query-builder.cjs +27 -0
- package/dist/cjs/schema/index.cjs +15 -0
- package/dist/cjs/schema/schema-builder.cjs +1167 -0
- package/dist/cjs/schema-builder.cjs +21 -0
- package/dist/cjs/schema-definition/column-types.cjs +829 -0
- package/dist/cjs/schema-definition/index.cjs +62 -0
- package/dist/cjs/schema-definition/introspection.cjs +620 -0
- package/dist/cjs/schema-definition/partitions.cjs +129 -0
- package/dist/cjs/schema-definition/pg-enum.cjs +76 -0
- package/dist/cjs/schema-definition/pg-function.cjs +91 -0
- package/dist/cjs/schema-definition/pg-sequence.cjs +56 -0
- package/dist/cjs/schema-definition/pg-trigger.cjs +108 -0
- package/dist/cjs/schema-definition/relations.cjs +98 -0
- package/dist/cjs/schema-definition/sql-expressions.cjs +202 -0
- package/dist/cjs/schema-definition/table-definition.cjs +636 -0
- package/dist/cjs/select/aggregate-builder.cjs +179 -0
- package/dist/cjs/select/index.cjs +5 -0
- package/dist/cjs/select/select-builder.cjs +233 -0
- package/dist/cjs/sequence/index.cjs +7 -0
- package/dist/cjs/sequence/sequence-builder.cjs +264 -0
- package/dist/cjs/table/alter-table-builder.cjs +146 -0
- package/dist/cjs/table/constraint-builder.cjs +102 -0
- package/dist/cjs/table/create-table-builder.cjs +248 -0
- package/dist/cjs/table/index.cjs +17 -0
- package/dist/cjs/table/partition-builder.cjs +131 -0
- package/dist/cjs/table/truncate-builder.cjs +70 -0
- package/dist/cjs/transaction/index.cjs +6 -0
- package/dist/cjs/transaction/transaction-builder.cjs +78 -0
- package/dist/cjs/trigger/create-trigger-builder.cjs +174 -0
- package/dist/cjs/trigger/index.cjs +6 -0
- package/dist/cjs/types/aggregate-types.cjs +2 -0
- package/dist/cjs/types/config-types.cjs +40 -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/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 +205 -0
- package/dist/cjs/update/index.cjs +13 -0
- package/dist/cjs/update/update-builder.cjs +195 -0
- package/dist/cjs/utils/case-converter.cjs +58 -0
- package/dist/cjs/utils/environment-detection.cjs +120 -0
- package/dist/cjs/utils/index.cjs +10 -0
- package/dist/cjs/utils/pool-defaults.cjs +106 -0
- package/dist/cjs/utils/type-coercion.cjs +118 -0
- package/dist/cjs/view/create-view-builder.cjs +180 -0
- package/dist/cjs/view/index.cjs +7 -0
- package/dist/cjs/window/index.cjs +5 -0
- package/dist/cjs/window/window-builder.cjs +80 -0
- package/dist/config.cjs +1 -0
- package/dist/config.d.ts +655 -0
- package/dist/config.js +1 -0
- package/dist/esm/cache/index.js +1 -0
- package/dist/esm/cache/query-cache.js +303 -0
- package/dist/esm/cli/commands/add.js +78 -0
- package/dist/esm/cli/commands/commit.js +109 -0
- package/dist/esm/cli/commands/diff.js +81 -0
- package/dist/esm/cli/commands/export.js +297 -0
- package/dist/esm/cli/commands/fetch.js +56 -0
- package/dist/esm/cli/commands/generate.js +206 -0
- package/dist/esm/cli/commands/history.js +129 -0
- package/dist/esm/cli/commands/import.js +488 -0
- package/dist/esm/cli/commands/init.js +401 -0
- package/dist/esm/cli/commands/introspect.js +106 -0
- package/dist/esm/cli/commands/log.js +59 -0
- package/dist/esm/cli/commands/migrate.js +131 -0
- package/dist/esm/cli/commands/pull.js +374 -0
- package/dist/esm/cli/commands/push.js +129 -0
- package/dist/esm/cli/commands/rollback.js +133 -0
- package/dist/esm/cli/commands/status.js +107 -0
- package/dist/esm/cli/commands/sync.js +76 -0
- package/dist/esm/cli/index.js +240 -0
- package/dist/esm/cli/utils/change-tracker.js +405 -0
- package/dist/esm/cli/utils/commit-manager.js +191 -0
- package/dist/esm/cli/utils/config-loader.js +86 -0
- package/dist/esm/cli/utils/env-loader.js +57 -0
- package/dist/esm/cli/utils/fast-introspect.js +362 -0
- package/dist/esm/cli/utils/git-utils.js +347 -0
- package/dist/esm/cli/utils/migration-generator.js +263 -0
- package/dist/esm/cli/utils/relqignore.js +74 -0
- package/dist/esm/cli/utils/repo-manager.js +444 -0
- package/dist/esm/cli/utils/schema-comparator.js +307 -0
- package/dist/esm/cli/utils/schema-diff.js +276 -0
- package/dist/esm/cli/utils/schema-hash.js +69 -0
- package/dist/esm/cli/utils/schema-introspect.js +418 -0
- package/dist/esm/cli/utils/snapshot-manager.js +179 -0
- package/dist/esm/cli/utils/spinner.js +101 -0
- package/dist/esm/cli/utils/sql-generator.js +504 -0
- package/dist/esm/cli/utils/sql-parser.js +992 -0
- package/dist/esm/cli/utils/type-generator.js +2058 -0
- package/dist/esm/condition/array-condition-builder.js +495 -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 +275 -0
- package/dist/esm/condition/fulltext-condition-builder.js +53 -0
- package/dist/esm/condition/geometric-condition-builder.js +200 -0
- package/dist/esm/condition/index.js +7 -0
- package/dist/esm/condition/jsonb-condition-builder.js +152 -0
- package/dist/esm/condition/network-condition-builder.js +222 -0
- package/dist/esm/condition/range-condition-builder.js +74 -0
- package/dist/esm/config/config.js +150 -0
- package/dist/esm/config/index.js +1 -0
- package/dist/esm/constants/pg-values.js +63 -0
- package/dist/esm/copy/copy-builder.js +308 -0
- package/dist/esm/copy/index.js +1 -0
- package/dist/esm/core/query-builder.js +426 -0
- package/dist/esm/core/relq-client.js +1791 -0
- package/dist/esm/core/typed-kuery-client.js +1 -0
- package/dist/esm/count/count-builder.js +81 -0
- package/dist/esm/count/index.js +1 -0
- package/dist/esm/cte/cte-builder.js +82 -0
- package/dist/esm/cte/index.js +1 -0
- package/dist/esm/ddl/function.js +45 -0
- package/dist/esm/ddl/index.js +2 -0
- package/dist/esm/ddl/sql.js +51 -0
- package/dist/esm/delete/delete-builder.js +128 -0
- package/dist/esm/delete/index.js +1 -0
- package/dist/esm/errors/relq-errors.js +310 -0
- package/dist/esm/examples/fulltext-search-test.js +117 -0
- package/dist/esm/explain/explain-builder.js +95 -0
- package/dist/esm/explain/index.js +1 -0
- package/dist/esm/function/create-function-builder.js +188 -0
- package/dist/esm/function/index.js +1 -0
- package/dist/esm/functions/advanced-functions.js +231 -0
- package/dist/esm/functions/case-builder.js +58 -0
- package/dist/esm/functions/geometric-functions.js +97 -0
- package/dist/esm/functions/index.js +171 -0
- package/dist/esm/functions/network-functions.js +79 -0
- package/dist/esm/functions/sql-functions.js +421 -0
- package/dist/esm/index.js +34 -0
- package/dist/esm/indexing/create-index-builder.js +180 -0
- package/dist/esm/indexing/drop-index-builder.js +81 -0
- package/dist/esm/indexing/index-types.js +1 -0
- package/dist/esm/indexing/index.js +2 -0
- package/dist/esm/insert/conflict-builder.js +162 -0
- package/dist/esm/insert/index.js +1 -0
- package/dist/esm/insert/insert-builder.js +247 -0
- package/dist/esm/introspect/index.js +224 -0
- package/dist/esm/maintenance/index.js +1 -0
- package/dist/esm/maintenance/vacuum-builder.js +158 -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 +1 -0
- package/dist/esm/raw/raw-query-builder.js +20 -0
- package/dist/esm/schema/index.js +1 -0
- package/dist/esm/schema/schema-builder.js +1150 -0
- package/dist/esm/schema-builder.js +2 -0
- package/dist/esm/schema-definition/column-types.js +738 -0
- package/dist/esm/schema-definition/index.js +10 -0
- package/dist/esm/schema-definition/introspection.js +614 -0
- package/dist/esm/schema-definition/partitions.js +123 -0
- package/dist/esm/schema-definition/pg-enum.js +70 -0
- package/dist/esm/schema-definition/pg-function.js +85 -0
- package/dist/esm/schema-definition/pg-sequence.js +50 -0
- package/dist/esm/schema-definition/pg-trigger.js +102 -0
- package/dist/esm/schema-definition/relations.js +90 -0
- package/dist/esm/schema-definition/sql-expressions.js +193 -0
- package/dist/esm/schema-definition/table-definition.js +630 -0
- package/dist/esm/select/aggregate-builder.js +172 -0
- package/dist/esm/select/index.js +1 -0
- package/dist/esm/select/select-builder.js +226 -0
- package/dist/esm/sequence/index.js +1 -0
- package/dist/esm/sequence/sequence-builder.js +255 -0
- package/dist/esm/table/alter-table-builder.js +138 -0
- package/dist/esm/table/constraint-builder.js +95 -0
- package/dist/esm/table/create-table-builder.js +241 -0
- package/dist/esm/table/index.js +5 -0
- package/dist/esm/table/partition-builder.js +121 -0
- package/dist/esm/table/truncate-builder.js +63 -0
- package/dist/esm/transaction/index.js +1 -0
- package/dist/esm/transaction/transaction-builder.js +70 -0
- package/dist/esm/trigger/create-trigger-builder.js +166 -0
- package/dist/esm/trigger/index.js +1 -0
- package/dist/esm/types/aggregate-types.js +1 -0
- package/dist/esm/types/config-types.js +36 -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/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 +192 -0
- package/dist/esm/update/index.js +2 -0
- package/dist/esm/update/update-builder.js +188 -0
- package/dist/esm/utils/case-converter.js +55 -0
- package/dist/esm/utils/environment-detection.js +113 -0
- package/dist/esm/utils/index.js +2 -0
- package/dist/esm/utils/pool-defaults.js +100 -0
- package/dist/esm/utils/type-coercion.js +110 -0
- package/dist/esm/view/create-view-builder.js +171 -0
- package/dist/esm/view/index.js +1 -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 +10341 -0
- package/dist/index.js +1 -0
- package/dist/schema-builder.cjs +1 -0
- package/dist/schema-builder.d.ts +2272 -0
- package/dist/schema-builder.js +1 -0
- package/package.json +55 -0
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import format from "../../addons/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 @@
|
|
|
1
|
+
export { SelectBuilder } from "./select-builder.js";
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import format from "../../addons/pg-format.js";
|
|
2
|
+
import { ConditionCollector, buildConditionsSQL } from "../condition/condition-collector.js";
|
|
3
|
+
export class SelectBuilder {
|
|
4
|
+
tableName;
|
|
5
|
+
selectColumns = ['*'];
|
|
6
|
+
whereConditions = [];
|
|
7
|
+
havingConditions = [];
|
|
8
|
+
joinClauses = [];
|
|
9
|
+
groupByColumns = [];
|
|
10
|
+
orderByColumns = [];
|
|
11
|
+
limitValue;
|
|
12
|
+
offsetValue;
|
|
13
|
+
distinctOnColumns = [];
|
|
14
|
+
lockingClause;
|
|
15
|
+
unionQueries = [];
|
|
16
|
+
constructor(tableName, columns) {
|
|
17
|
+
this.tableName = tableName;
|
|
18
|
+
if (columns) {
|
|
19
|
+
this.selectColumns = Array.isArray(columns) ? columns : [columns];
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
distinct() {
|
|
23
|
+
this.selectColumns = this.selectColumns.map(col => {
|
|
24
|
+
if (typeof col === 'string') {
|
|
25
|
+
return col.startsWith('DISTINCT') ? col : `DISTINCT ${col}`;
|
|
26
|
+
}
|
|
27
|
+
return col;
|
|
28
|
+
});
|
|
29
|
+
return this;
|
|
30
|
+
}
|
|
31
|
+
where(callback) {
|
|
32
|
+
const conditionBuilder = new ConditionCollector();
|
|
33
|
+
callback(conditionBuilder);
|
|
34
|
+
this.whereConditions.push(...conditionBuilder.getConditions());
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
join(table, condition) {
|
|
38
|
+
this.joinClauses.push(`JOIN ${format.ident(table)} ON ${condition}`);
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
leftJoin(table, condition) {
|
|
42
|
+
this.joinClauses.push(`LEFT JOIN ${format.ident(table)} ON ${condition}`);
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
|
+
rightJoin(table, condition) {
|
|
46
|
+
this.joinClauses.push(`RIGHT JOIN ${format.ident(table)} ON ${condition}`);
|
|
47
|
+
return this;
|
|
48
|
+
}
|
|
49
|
+
innerJoin(table, condition) {
|
|
50
|
+
this.joinClauses.push(`INNER JOIN ${format.ident(table)} ON ${condition}`);
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
groupBy(...columns) {
|
|
54
|
+
this.groupByColumns.push(...columns);
|
|
55
|
+
return this;
|
|
56
|
+
}
|
|
57
|
+
having(callback) {
|
|
58
|
+
const conditionBuilder = new ConditionCollector();
|
|
59
|
+
callback(conditionBuilder);
|
|
60
|
+
this.havingConditions.push(...conditionBuilder.getConditions());
|
|
61
|
+
return this;
|
|
62
|
+
}
|
|
63
|
+
orderBy(column, direction = 'ASC') {
|
|
64
|
+
this.orderByColumns.push({ column, direction });
|
|
65
|
+
return this;
|
|
66
|
+
}
|
|
67
|
+
orderAsc(column) {
|
|
68
|
+
return this.orderBy(column, 'ASC');
|
|
69
|
+
}
|
|
70
|
+
orderDesc(column) {
|
|
71
|
+
return this.orderBy(column, 'DESC');
|
|
72
|
+
}
|
|
73
|
+
limit(count) {
|
|
74
|
+
this.limitValue = count;
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
offset(count) {
|
|
78
|
+
this.offsetValue = count;
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
distinctOn(...columns) {
|
|
82
|
+
this.distinctOnColumns.push(...columns);
|
|
83
|
+
return this;
|
|
84
|
+
}
|
|
85
|
+
forUpdate() {
|
|
86
|
+
this.lockingClause = 'FOR UPDATE';
|
|
87
|
+
return this;
|
|
88
|
+
}
|
|
89
|
+
forUpdateNoWait() {
|
|
90
|
+
this.lockingClause = 'FOR UPDATE NOWAIT';
|
|
91
|
+
return this;
|
|
92
|
+
}
|
|
93
|
+
forUpdateSkipLocked() {
|
|
94
|
+
this.lockingClause = 'FOR UPDATE SKIP LOCKED';
|
|
95
|
+
return this;
|
|
96
|
+
}
|
|
97
|
+
forShare() {
|
|
98
|
+
this.lockingClause = 'FOR SHARE';
|
|
99
|
+
return this;
|
|
100
|
+
}
|
|
101
|
+
forShareNoWait() {
|
|
102
|
+
this.lockingClause = 'FOR SHARE NOWAIT';
|
|
103
|
+
return this;
|
|
104
|
+
}
|
|
105
|
+
forShareSkipLocked() {
|
|
106
|
+
this.lockingClause = 'FOR SHARE SKIP LOCKED';
|
|
107
|
+
return this;
|
|
108
|
+
}
|
|
109
|
+
union(query) {
|
|
110
|
+
this.unionQueries.push({
|
|
111
|
+
query: typeof query === 'string' ? query : query.toString(),
|
|
112
|
+
type: 'UNION'
|
|
113
|
+
});
|
|
114
|
+
return this;
|
|
115
|
+
}
|
|
116
|
+
unionAll(query) {
|
|
117
|
+
this.unionQueries.push({
|
|
118
|
+
query: typeof query === 'string' ? query : query.toString(),
|
|
119
|
+
type: 'UNION ALL'
|
|
120
|
+
});
|
|
121
|
+
return this;
|
|
122
|
+
}
|
|
123
|
+
intersect(query) {
|
|
124
|
+
this.unionQueries.push({
|
|
125
|
+
query: typeof query === 'string' ? query : query.toString(),
|
|
126
|
+
type: 'INTERSECT'
|
|
127
|
+
});
|
|
128
|
+
return this;
|
|
129
|
+
}
|
|
130
|
+
except(query) {
|
|
131
|
+
this.unionQueries.push({
|
|
132
|
+
query: typeof query === 'string' ? query : query.toString(),
|
|
133
|
+
type: 'EXCEPT'
|
|
134
|
+
});
|
|
135
|
+
return this;
|
|
136
|
+
}
|
|
137
|
+
fullOuterJoin(table, condition) {
|
|
138
|
+
this.joinClauses.push(`FULL OUTER JOIN ${format.ident(table)} ON ${condition}`);
|
|
139
|
+
return this;
|
|
140
|
+
}
|
|
141
|
+
crossJoin(table) {
|
|
142
|
+
this.joinClauses.push(`CROSS JOIN ${format.ident(table)}`);
|
|
143
|
+
return this;
|
|
144
|
+
}
|
|
145
|
+
lateralJoin(subquery, left = false) {
|
|
146
|
+
const joinType = left ? 'LEFT JOIN LATERAL' : 'JOIN LATERAL';
|
|
147
|
+
this.joinClauses.push(`${joinType} ${subquery}`);
|
|
148
|
+
return this;
|
|
149
|
+
}
|
|
150
|
+
toString() {
|
|
151
|
+
const columns = this.selectColumns.map(col => {
|
|
152
|
+
if (col === '*')
|
|
153
|
+
return '*';
|
|
154
|
+
if (Array.isArray(col)) {
|
|
155
|
+
const [column, alias] = col;
|
|
156
|
+
return format('%I AS %I', column, alias);
|
|
157
|
+
}
|
|
158
|
+
if (col.includes('(') || col.includes('DISTINCT') || col.includes(' AS ')) {
|
|
159
|
+
return col;
|
|
160
|
+
}
|
|
161
|
+
return format.ident(col);
|
|
162
|
+
}).join(', ');
|
|
163
|
+
let query = 'SELECT';
|
|
164
|
+
if (this.distinctOnColumns.length > 0) {
|
|
165
|
+
const distinctCols = this.distinctOnColumns.map(col => format.ident(col)).join(', ');
|
|
166
|
+
query += format(' DISTINCT ON (%s)', distinctCols);
|
|
167
|
+
}
|
|
168
|
+
query += format(' %s FROM %I', columns, this.tableName);
|
|
169
|
+
if (this.joinClauses.length > 0) {
|
|
170
|
+
query += ' ' + this.joinClauses.join(' ');
|
|
171
|
+
}
|
|
172
|
+
if (this.whereConditions.length > 0) {
|
|
173
|
+
query += ' WHERE ' + buildConditionsSQL(this.whereConditions);
|
|
174
|
+
}
|
|
175
|
+
if (this.groupByColumns.length > 0) {
|
|
176
|
+
query += ' GROUP BY ' + this.groupByColumns.map(col => format.ident(col)).join(', ');
|
|
177
|
+
}
|
|
178
|
+
if (this.havingConditions.length > 0) {
|
|
179
|
+
query += ' HAVING ' + buildConditionsSQL(this.havingConditions);
|
|
180
|
+
}
|
|
181
|
+
if (this.orderByColumns.length > 0) {
|
|
182
|
+
query += ' ORDER BY ' + this.orderByColumns
|
|
183
|
+
.map(order => format('%I %s', order.column, order.direction))
|
|
184
|
+
.join(', ');
|
|
185
|
+
}
|
|
186
|
+
if (this.limitValue !== undefined) {
|
|
187
|
+
query += format(' LIMIT %s', this.limitValue);
|
|
188
|
+
}
|
|
189
|
+
if (this.offsetValue !== undefined) {
|
|
190
|
+
query += format(' OFFSET %s', this.offsetValue);
|
|
191
|
+
}
|
|
192
|
+
if (this.lockingClause) {
|
|
193
|
+
query += ' ' + this.lockingClause;
|
|
194
|
+
}
|
|
195
|
+
if (this.unionQueries.length > 0) {
|
|
196
|
+
for (const unionQuery of this.unionQueries) {
|
|
197
|
+
query += ` ${unionQuery.type} ${unionQuery.query}`;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return query;
|
|
201
|
+
}
|
|
202
|
+
whereRaw(condition) {
|
|
203
|
+
this.whereConditions.push({
|
|
204
|
+
method: 'raw',
|
|
205
|
+
column: condition,
|
|
206
|
+
values: undefined
|
|
207
|
+
});
|
|
208
|
+
return this;
|
|
209
|
+
}
|
|
210
|
+
toCountSQL() {
|
|
211
|
+
let query = `SELECT COUNT(*) as count FROM ${format.ident(this.tableName)}`;
|
|
212
|
+
if (this.joinClauses.length > 0) {
|
|
213
|
+
query += ' ' + this.joinClauses.join(' ');
|
|
214
|
+
}
|
|
215
|
+
if (this.whereConditions.length > 0) {
|
|
216
|
+
query += ' WHERE ' + buildConditionsSQL(this.whereConditions);
|
|
217
|
+
}
|
|
218
|
+
if (this.groupByColumns.length > 0) {
|
|
219
|
+
query += ' GROUP BY ' + this.groupByColumns.map(col => format.ident(col)).join(', ');
|
|
220
|
+
}
|
|
221
|
+
if (this.havingConditions.length > 0) {
|
|
222
|
+
query += ' HAVING ' + buildConditionsSQL(this.havingConditions);
|
|
223
|
+
}
|
|
224
|
+
return query;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { CreateSequenceBuilder, AlterSequenceBuilder, DropSequenceBuilder } from "./sequence-builder.js";
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import format from "../../addons/pg-format.js";
|
|
2
|
+
export class CreateSequenceBuilder {
|
|
3
|
+
sequenceName;
|
|
4
|
+
options = {};
|
|
5
|
+
ifNotExistsFlag = false;
|
|
6
|
+
temporaryFlag = false;
|
|
7
|
+
unloggedFlag = false;
|
|
8
|
+
constructor(sequenceName) {
|
|
9
|
+
this.sequenceName = sequenceName;
|
|
10
|
+
}
|
|
11
|
+
ifNotExists() {
|
|
12
|
+
this.ifNotExistsFlag = true;
|
|
13
|
+
return this;
|
|
14
|
+
}
|
|
15
|
+
temporary() {
|
|
16
|
+
this.temporaryFlag = true;
|
|
17
|
+
return this;
|
|
18
|
+
}
|
|
19
|
+
unlogged() {
|
|
20
|
+
this.unloggedFlag = true;
|
|
21
|
+
return this;
|
|
22
|
+
}
|
|
23
|
+
as(type) {
|
|
24
|
+
this.options.as = type;
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
start(value) {
|
|
28
|
+
this.options.start = value;
|
|
29
|
+
return this;
|
|
30
|
+
}
|
|
31
|
+
increment(value) {
|
|
32
|
+
this.options.increment = value;
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
minValue(value) {
|
|
36
|
+
this.options.minValue = value;
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
maxValue(value) {
|
|
40
|
+
this.options.maxValue = value;
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
noMinValue() {
|
|
44
|
+
this.options.minValue = undefined;
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
noMaxValue() {
|
|
48
|
+
this.options.maxValue = undefined;
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
cache(count) {
|
|
52
|
+
this.options.cache = count;
|
|
53
|
+
return this;
|
|
54
|
+
}
|
|
55
|
+
cycle() {
|
|
56
|
+
this.options.cycle = true;
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
59
|
+
noCycle() {
|
|
60
|
+
this.options.cycle = false;
|
|
61
|
+
return this;
|
|
62
|
+
}
|
|
63
|
+
ownedBy(tableColumn) {
|
|
64
|
+
this.options.ownedBy = tableColumn;
|
|
65
|
+
return this;
|
|
66
|
+
}
|
|
67
|
+
ownedByNone() {
|
|
68
|
+
this.options.ownedBy = 'NONE';
|
|
69
|
+
return this;
|
|
70
|
+
}
|
|
71
|
+
toString() {
|
|
72
|
+
let sql = 'CREATE';
|
|
73
|
+
if (this.temporaryFlag) {
|
|
74
|
+
sql += ' TEMPORARY';
|
|
75
|
+
}
|
|
76
|
+
if (this.unloggedFlag) {
|
|
77
|
+
sql += ' UNLOGGED';
|
|
78
|
+
}
|
|
79
|
+
sql += ' SEQUENCE';
|
|
80
|
+
if (this.ifNotExistsFlag) {
|
|
81
|
+
sql += ' IF NOT EXISTS';
|
|
82
|
+
}
|
|
83
|
+
sql += ' ' + format.ident(this.sequenceName);
|
|
84
|
+
if (this.options.as) {
|
|
85
|
+
sql += ` AS ${this.options.as}`;
|
|
86
|
+
}
|
|
87
|
+
if (this.options.increment !== undefined) {
|
|
88
|
+
sql += ` INCREMENT BY ${this.options.increment}`;
|
|
89
|
+
}
|
|
90
|
+
if (this.options.minValue !== undefined) {
|
|
91
|
+
sql += ` MINVALUE ${this.options.minValue}`;
|
|
92
|
+
}
|
|
93
|
+
if (this.options.maxValue !== undefined) {
|
|
94
|
+
sql += ` MAXVALUE ${this.options.maxValue}`;
|
|
95
|
+
}
|
|
96
|
+
if (this.options.start !== undefined) {
|
|
97
|
+
sql += ` START WITH ${this.options.start}`;
|
|
98
|
+
}
|
|
99
|
+
if (this.options.cache !== undefined) {
|
|
100
|
+
sql += ` CACHE ${this.options.cache}`;
|
|
101
|
+
}
|
|
102
|
+
if (this.options.cycle === true) {
|
|
103
|
+
sql += ' CYCLE';
|
|
104
|
+
}
|
|
105
|
+
else if (this.options.cycle === false) {
|
|
106
|
+
sql += ' NO CYCLE';
|
|
107
|
+
}
|
|
108
|
+
if (this.options.ownedBy) {
|
|
109
|
+
sql += ` OWNED BY ${this.options.ownedBy}`;
|
|
110
|
+
}
|
|
111
|
+
return sql;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
export class AlterSequenceBuilder {
|
|
115
|
+
sequenceName;
|
|
116
|
+
options = {};
|
|
117
|
+
ifExistsFlag = false;
|
|
118
|
+
constructor(sequenceName) {
|
|
119
|
+
this.sequenceName = sequenceName;
|
|
120
|
+
}
|
|
121
|
+
ifExists() {
|
|
122
|
+
this.ifExistsFlag = true;
|
|
123
|
+
return this;
|
|
124
|
+
}
|
|
125
|
+
restart(value) {
|
|
126
|
+
this.options.restart = value ?? true;
|
|
127
|
+
return this;
|
|
128
|
+
}
|
|
129
|
+
as(type) {
|
|
130
|
+
this.options.as = type;
|
|
131
|
+
return this;
|
|
132
|
+
}
|
|
133
|
+
start(value) {
|
|
134
|
+
this.options.start = value;
|
|
135
|
+
return this;
|
|
136
|
+
}
|
|
137
|
+
increment(value) {
|
|
138
|
+
this.options.increment = value;
|
|
139
|
+
return this;
|
|
140
|
+
}
|
|
141
|
+
minValue(value) {
|
|
142
|
+
this.options.minValue = value;
|
|
143
|
+
return this;
|
|
144
|
+
}
|
|
145
|
+
maxValue(value) {
|
|
146
|
+
this.options.maxValue = value;
|
|
147
|
+
return this;
|
|
148
|
+
}
|
|
149
|
+
noMinValue() {
|
|
150
|
+
this.options.minValue = undefined;
|
|
151
|
+
return this;
|
|
152
|
+
}
|
|
153
|
+
noMaxValue() {
|
|
154
|
+
this.options.maxValue = undefined;
|
|
155
|
+
return this;
|
|
156
|
+
}
|
|
157
|
+
cache(count) {
|
|
158
|
+
this.options.cache = count;
|
|
159
|
+
return this;
|
|
160
|
+
}
|
|
161
|
+
cycle() {
|
|
162
|
+
this.options.cycle = true;
|
|
163
|
+
return this;
|
|
164
|
+
}
|
|
165
|
+
noCycle() {
|
|
166
|
+
this.options.cycle = false;
|
|
167
|
+
return this;
|
|
168
|
+
}
|
|
169
|
+
ownedBy(tableColumn) {
|
|
170
|
+
this.options.ownedBy = tableColumn;
|
|
171
|
+
return this;
|
|
172
|
+
}
|
|
173
|
+
ownedByNone() {
|
|
174
|
+
this.options.ownedBy = 'NONE';
|
|
175
|
+
return this;
|
|
176
|
+
}
|
|
177
|
+
toString() {
|
|
178
|
+
let sql = 'ALTER SEQUENCE';
|
|
179
|
+
if (this.ifExistsFlag) {
|
|
180
|
+
sql += ' IF EXISTS';
|
|
181
|
+
}
|
|
182
|
+
sql += ' ' + format.ident(this.sequenceName);
|
|
183
|
+
if (this.options.as) {
|
|
184
|
+
sql += ` AS ${this.options.as}`;
|
|
185
|
+
}
|
|
186
|
+
if (this.options.increment !== undefined) {
|
|
187
|
+
sql += ` INCREMENT BY ${this.options.increment}`;
|
|
188
|
+
}
|
|
189
|
+
if (this.options.minValue !== undefined) {
|
|
190
|
+
sql += ` MINVALUE ${this.options.minValue}`;
|
|
191
|
+
}
|
|
192
|
+
if (this.options.maxValue !== undefined) {
|
|
193
|
+
sql += ` MAXVALUE ${this.options.maxValue}`;
|
|
194
|
+
}
|
|
195
|
+
if (this.options.start !== undefined) {
|
|
196
|
+
sql += ` START WITH ${this.options.start}`;
|
|
197
|
+
}
|
|
198
|
+
if (this.options.cache !== undefined) {
|
|
199
|
+
sql += ` CACHE ${this.options.cache}`;
|
|
200
|
+
}
|
|
201
|
+
if (this.options.cycle === true) {
|
|
202
|
+
sql += ' CYCLE';
|
|
203
|
+
}
|
|
204
|
+
else if (this.options.cycle === false) {
|
|
205
|
+
sql += ' NO CYCLE';
|
|
206
|
+
}
|
|
207
|
+
if (this.options.ownedBy) {
|
|
208
|
+
sql += ` OWNED BY ${this.options.ownedBy}`;
|
|
209
|
+
}
|
|
210
|
+
if (this.options.restart === true) {
|
|
211
|
+
sql += ' RESTART';
|
|
212
|
+
}
|
|
213
|
+
else if (typeof this.options.restart === 'number') {
|
|
214
|
+
sql += ` RESTART WITH ${this.options.restart}`;
|
|
215
|
+
}
|
|
216
|
+
return sql;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
export class DropSequenceBuilder {
|
|
220
|
+
sequenceNames;
|
|
221
|
+
ifExistsFlag = false;
|
|
222
|
+
cascadeFlag = false;
|
|
223
|
+
restrictFlag = false;
|
|
224
|
+
constructor(sequenceNames) {
|
|
225
|
+
this.sequenceNames = Array.isArray(sequenceNames) ? sequenceNames : [sequenceNames];
|
|
226
|
+
}
|
|
227
|
+
ifExists() {
|
|
228
|
+
this.ifExistsFlag = true;
|
|
229
|
+
return this;
|
|
230
|
+
}
|
|
231
|
+
cascade() {
|
|
232
|
+
this.cascadeFlag = true;
|
|
233
|
+
this.restrictFlag = false;
|
|
234
|
+
return this;
|
|
235
|
+
}
|
|
236
|
+
restrict() {
|
|
237
|
+
this.restrictFlag = true;
|
|
238
|
+
this.cascadeFlag = false;
|
|
239
|
+
return this;
|
|
240
|
+
}
|
|
241
|
+
toString() {
|
|
242
|
+
let sql = 'DROP SEQUENCE';
|
|
243
|
+
if (this.ifExistsFlag) {
|
|
244
|
+
sql += ' IF EXISTS';
|
|
245
|
+
}
|
|
246
|
+
sql += ' ' + this.sequenceNames.map(name => format.ident(name)).join(', ');
|
|
247
|
+
if (this.cascadeFlag) {
|
|
248
|
+
sql += ' CASCADE';
|
|
249
|
+
}
|
|
250
|
+
else if (this.restrictFlag) {
|
|
251
|
+
sql += ' RESTRICT';
|
|
252
|
+
}
|
|
253
|
+
return sql;
|
|
254
|
+
}
|
|
255
|
+
}
|