@hypequery/clickhouse 1.6.2 → 2.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/README-CLI.md +43 -88
- package/README.md +84 -253
- package/dist/cli/bin.js +16 -8
- package/dist/core/adapters/clickhouse-adapter.d.ts.map +1 -1
- package/dist/core/adapters/clickhouse-adapter.js +3 -2
- package/dist/core/cache/cache-manager.d.ts.map +1 -1
- package/dist/core/cache/cache-manager.js +5 -3
- package/dist/core/connection.d.ts +6 -6
- package/dist/core/connection.js +9 -9
- package/dist/core/cross-filter.js +1 -1
- package/dist/core/dialects/clickhouse-dialect.d.ts +2 -2
- package/dist/core/dialects/clickhouse-dialect.d.ts.map +1 -1
- package/dist/core/dialects/clickhouse-dialect.js +39 -22
- package/dist/core/dialects/sql-dialect.d.ts +2 -2
- package/dist/core/dialects/sql-dialect.d.ts.map +1 -1
- package/dist/core/env/auto-client.d.ts.map +1 -1
- package/dist/core/env/auto-client.js +1 -1
- package/dist/core/features/aggregations.d.ts +7 -90
- package/dist/core/features/aggregations.d.ts.map +1 -1
- package/dist/core/features/aggregations.js +19 -7
- package/dist/core/features/analytics.d.ts +5 -870
- package/dist/core/features/analytics.d.ts.map +1 -1
- package/dist/core/features/analytics.js +15 -13
- package/dist/core/features/cross-filtering.d.ts +1 -1
- package/dist/core/features/cross-filtering.d.ts.map +1 -1
- package/dist/core/features/cross-filtering.js +28 -73
- package/dist/core/features/executor.d.ts +1 -1
- package/dist/core/features/executor.d.ts.map +1 -1
- package/dist/core/features/executor.js +9 -11
- package/dist/core/features/filtering.d.ts +5 -91
- package/dist/core/features/filtering.d.ts.map +1 -1
- package/dist/core/features/filtering.js +63 -77
- package/dist/core/features/joins.d.ts +2 -19
- package/dist/core/features/joins.d.ts.map +1 -1
- package/dist/core/features/joins.js +16 -5
- package/dist/core/features/query-modifiers.d.ts +10 -109
- package/dist/core/features/query-modifiers.d.ts.map +1 -1
- package/dist/core/features/query-modifiers.js +64 -18
- package/dist/core/formatters/sql-formatter.d.ts +16 -5
- package/dist/core/formatters/sql-formatter.d.ts.map +1 -1
- package/dist/core/formatters/sql-formatter.js +197 -93
- package/dist/core/join-relationships.d.ts +22 -5
- package/dist/core/join-relationships.d.ts.map +1 -1
- package/dist/core/join-relationships.js +1 -1
- package/dist/core/query-builder.d.ts +63 -12
- package/dist/core/query-builder.d.ts.map +1 -1
- package/dist/core/query-builder.js +210 -153
- package/dist/core/query-node.d.ts +7 -0
- package/dist/core/query-node.d.ts.map +1 -0
- package/dist/core/query-node.js +80 -0
- package/dist/core/tests/integration/setup.d.ts +1 -0
- package/dist/core/tests/integration/setup.d.ts.map +1 -1
- package/dist/core/tests/integration/setup.js +4 -2
- package/dist/core/types/select-types.d.ts +3 -0
- package/dist/core/types/select-types.d.ts.map +1 -1
- package/dist/core/utils/connection-endpoint.d.ts +3 -0
- package/dist/core/utils/connection-endpoint.d.ts.map +1 -0
- package/dist/core/utils/connection-endpoint.js +9 -0
- package/dist/core/utils/filter-application.d.ts +15 -0
- package/dist/core/utils/filter-application.d.ts.map +1 -0
- package/dist/core/utils/filter-application.js +32 -0
- package/dist/core/utils/query-config-compat.d.ts +48 -0
- package/dist/core/utils/query-config-compat.d.ts.map +1 -0
- package/dist/core/utils/query-config-compat.js +137 -0
- package/dist/core/utils/relation-application.d.ts +9 -0
- package/dist/core/utils/relation-application.d.ts.map +1 -0
- package/dist/core/utils/relation-application.js +19 -0
- package/dist/core/utils/relation-validation.d.ts +6 -0
- package/dist/core/utils/relation-validation.d.ts.map +1 -0
- package/dist/core/utils/relation-validation.js +29 -0
- package/dist/core/utils/sql-expressions.d.ts +14 -0
- package/dist/core/utils/sql-expressions.d.ts.map +1 -1
- package/dist/core/utils/sql-expressions.js +40 -0
- package/dist/core/utils/tuple-filter-validation.d.ts +3 -0
- package/dist/core/utils/tuple-filter-validation.d.ts.map +1 -0
- package/dist/core/utils/tuple-filter-validation.js +16 -0
- package/dist/index.d.ts +2 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -8
- package/dist/types/base.d.ts +88 -22
- package/dist/types/base.d.ts.map +1 -1
- package/dist/types/filters.d.ts +9 -5
- package/dist/types/filters.d.ts.map +1 -1
- package/package.json +5 -5
- package/dist/migrations/config/index.d.ts +0 -3
- package/dist/migrations/config/index.d.ts.map +0 -1
- package/dist/migrations/config/index.js +0 -1
- package/dist/migrations/config/types.d.ts +0 -45
- package/dist/migrations/config/types.d.ts.map +0 -1
- package/dist/migrations/config/types.js +0 -28
- package/dist/migrations/diff/diff.d.ts +0 -11
- package/dist/migrations/diff/diff.d.ts.map +0 -1
- package/dist/migrations/diff/diff.js +0 -240
- package/dist/migrations/diff/index.d.ts +0 -3
- package/dist/migrations/diff/index.d.ts.map +0 -1
- package/dist/migrations/diff/index.js +0 -1
- package/dist/migrations/diff/types.d.ts +0 -74
- package/dist/migrations/diff/types.d.ts.map +0 -1
- package/dist/migrations/diff/types.js +0 -1
- package/dist/migrations/plan/index.d.ts +0 -3
- package/dist/migrations/plan/index.d.ts.map +0 -1
- package/dist/migrations/plan/index.js +0 -1
- package/dist/migrations/plan/plan.d.ts +0 -12
- package/dist/migrations/plan/plan.d.ts.map +0 -1
- package/dist/migrations/plan/plan.js +0 -416
- package/dist/migrations/plan/types.d.ts +0 -93
- package/dist/migrations/plan/types.d.ts.map +0 -1
- package/dist/migrations/plan/types.js +0 -1
- package/dist/migrations/schema/column.d.ts +0 -71
- package/dist/migrations/schema/column.d.ts.map +0 -1
- package/dist/migrations/schema/column.js +0 -123
- package/dist/migrations/schema/define.d.ts +0 -24
- package/dist/migrations/schema/define.d.ts.map +0 -1
- package/dist/migrations/schema/define.js +0 -47
- package/dist/migrations/schema/index.d.ts +0 -4
- package/dist/migrations/schema/index.d.ts.map +0 -1
- package/dist/migrations/schema/index.js +0 -2
- package/dist/migrations/schema/types.d.ts +0 -74
- package/dist/migrations/schema/types.d.ts.map +0 -1
- package/dist/migrations/schema/types.js +0 -1
- package/dist/migrations/snapshot/index.d.ts +0 -3
- package/dist/migrations/snapshot/index.d.ts.map +0 -1
- package/dist/migrations/snapshot/index.js +0 -1
- package/dist/migrations/snapshot/serialize.d.ts +0 -21
- package/dist/migrations/snapshot/serialize.d.ts.map +0 -1
- package/dist/migrations/snapshot/serialize.js +0 -127
- package/dist/migrations/snapshot/types.d.ts +0 -47
- package/dist/migrations/snapshot/types.d.ts.map +0 -1
- package/dist/migrations/snapshot/types.js +0 -1
- package/dist/migrations/sql/index.d.ts +0 -4
- package/dist/migrations/sql/index.d.ts.map +0 -1
- package/dist/migrations/sql/index.js +0 -2
- package/dist/migrations/sql/render.d.ts +0 -10
- package/dist/migrations/sql/render.d.ts.map +0 -1
- package/dist/migrations/sql/render.js +0 -347
- package/dist/migrations/sql/types.d.ts +0 -53
- package/dist/migrations/sql/types.d.ts.map +0 -1
- package/dist/migrations/sql/types.js +0 -1
- package/dist/migrations/sql/write.d.ts +0 -10
- package/dist/migrations/sql/write.d.ts.map +0 -1
- package/dist/migrations/sql/write.js +0 -35
|
@@ -1,116 +1,17 @@
|
|
|
1
1
|
import type { BuilderState, SchemaDefinition } from '../types/builder-state.js';
|
|
2
2
|
import { QueryBuilder } from '../query-builder.js';
|
|
3
|
-
import { OrderDirection } from '../../types/index.js';
|
|
3
|
+
import { OrderDirection, type SelectQueryNode } from '../../types/index.js';
|
|
4
4
|
export declare class QueryModifiersFeature<Schema extends SchemaDefinition<Schema>, State extends BuilderState<Schema, string, any, keyof Schema, Partial<Record<string, keyof Schema>>>> {
|
|
5
5
|
private builder;
|
|
6
6
|
constructor(builder: QueryBuilder<Schema, State>);
|
|
7
|
-
addGroupBy(columns: string | string[]):
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
column: keyof State["output"] | import("../../types/schema.js").TableColumn<Schema>;
|
|
17
|
-
direction: OrderDirection;
|
|
18
|
-
}[] | undefined;
|
|
19
|
-
joins?: import("../../types/base.js").JoinClause[];
|
|
20
|
-
parameters?: any[];
|
|
21
|
-
ctes?: string[];
|
|
22
|
-
unionQueries?: string[];
|
|
23
|
-
settings?: import("@clickhouse/client-common").ClickHouseSettings;
|
|
24
|
-
};
|
|
25
|
-
addLimit(count: number): {
|
|
26
|
-
limit: number;
|
|
27
|
-
select?: (string | keyof State["output"])[] | undefined;
|
|
28
|
-
where?: import("../../types/base.js").WhereCondition[];
|
|
29
|
-
groupBy?: string[];
|
|
30
|
-
having?: string[];
|
|
31
|
-
offset?: number;
|
|
32
|
-
distinct?: boolean;
|
|
33
|
-
orderBy?: {
|
|
34
|
-
column: keyof State["output"] | import("../../types/schema.js").TableColumn<Schema>;
|
|
35
|
-
direction: OrderDirection;
|
|
36
|
-
}[] | undefined;
|
|
37
|
-
joins?: import("../../types/base.js").JoinClause[];
|
|
38
|
-
parameters?: any[];
|
|
39
|
-
ctes?: string[];
|
|
40
|
-
unionQueries?: string[];
|
|
41
|
-
settings?: import("@clickhouse/client-common").ClickHouseSettings;
|
|
42
|
-
};
|
|
43
|
-
addOffset(count: number): {
|
|
44
|
-
offset: number;
|
|
45
|
-
select?: (string | keyof State["output"])[] | undefined;
|
|
46
|
-
where?: import("../../types/base.js").WhereCondition[];
|
|
47
|
-
groupBy?: string[];
|
|
48
|
-
having?: string[];
|
|
49
|
-
limit?: number;
|
|
50
|
-
distinct?: boolean;
|
|
51
|
-
orderBy?: {
|
|
52
|
-
column: keyof State["output"] | import("../../types/schema.js").TableColumn<Schema>;
|
|
53
|
-
direction: OrderDirection;
|
|
54
|
-
}[] | undefined;
|
|
55
|
-
joins?: import("../../types/base.js").JoinClause[];
|
|
56
|
-
parameters?: any[];
|
|
57
|
-
ctes?: string[];
|
|
58
|
-
unionQueries?: string[];
|
|
59
|
-
settings?: import("@clickhouse/client-common").ClickHouseSettings;
|
|
60
|
-
};
|
|
61
|
-
addOrderBy(column: string, direction?: OrderDirection): {
|
|
62
|
-
orderBy: {
|
|
63
|
-
column: keyof State["output"] | import("../../types/schema.js").TableColumn<Schema>;
|
|
64
|
-
direction: OrderDirection;
|
|
65
|
-
}[];
|
|
66
|
-
select?: (string | keyof State["output"])[] | undefined;
|
|
67
|
-
where?: import("../../types/base.js").WhereCondition[];
|
|
68
|
-
groupBy?: string[];
|
|
69
|
-
having?: string[];
|
|
70
|
-
limit?: number;
|
|
71
|
-
offset?: number;
|
|
72
|
-
distinct?: boolean;
|
|
73
|
-
joins?: import("../../types/base.js").JoinClause[];
|
|
74
|
-
parameters?: any[];
|
|
75
|
-
ctes?: string[];
|
|
76
|
-
unionQueries?: string[];
|
|
77
|
-
settings?: import("@clickhouse/client-common").ClickHouseSettings;
|
|
78
|
-
};
|
|
79
|
-
addHaving(condition: string, parameters?: any[]): {
|
|
80
|
-
having: string[];
|
|
81
|
-
parameters: any[] | undefined;
|
|
82
|
-
select?: (string | keyof State["output"])[] | undefined;
|
|
83
|
-
where?: import("../../types/base.js").WhereCondition[];
|
|
84
|
-
groupBy?: string[];
|
|
85
|
-
limit?: number;
|
|
86
|
-
offset?: number;
|
|
87
|
-
distinct?: boolean;
|
|
88
|
-
orderBy?: {
|
|
89
|
-
column: keyof State["output"] | import("../../types/schema.js").TableColumn<Schema>;
|
|
90
|
-
direction: OrderDirection;
|
|
91
|
-
}[] | undefined;
|
|
92
|
-
joins?: import("../../types/base.js").JoinClause[];
|
|
93
|
-
ctes?: string[];
|
|
94
|
-
unionQueries?: string[];
|
|
95
|
-
settings?: import("@clickhouse/client-common").ClickHouseSettings;
|
|
96
|
-
};
|
|
97
|
-
setDistinct(): {
|
|
98
|
-
distinct: boolean;
|
|
99
|
-
select?: (string | keyof State["output"])[] | undefined;
|
|
100
|
-
where?: import("../../types/base.js").WhereCondition[];
|
|
101
|
-
groupBy?: string[];
|
|
102
|
-
having?: string[];
|
|
103
|
-
limit?: number;
|
|
104
|
-
offset?: number;
|
|
105
|
-
orderBy?: {
|
|
106
|
-
column: keyof State["output"] | import("../../types/schema.js").TableColumn<Schema>;
|
|
107
|
-
direction: OrderDirection;
|
|
108
|
-
}[] | undefined;
|
|
109
|
-
joins?: import("../../types/base.js").JoinClause[];
|
|
110
|
-
parameters?: any[];
|
|
111
|
-
ctes?: string[];
|
|
112
|
-
unionQueries?: string[];
|
|
113
|
-
settings?: import("@clickhouse/client-common").ClickHouseSettings;
|
|
114
|
-
};
|
|
7
|
+
addGroupBy(columns: string | string[]): SelectQueryNode<State['output'], Schema>;
|
|
8
|
+
addArrayJoin(type: 'ARRAY' | 'LEFT ARRAY', expression: string): SelectQueryNode<State['output'], Schema>;
|
|
9
|
+
addLimit(count: number): SelectQueryNode<State['output'], Schema>;
|
|
10
|
+
addLimitBy(limit: number, by: string | string[]): SelectQueryNode<State['output'], Schema>;
|
|
11
|
+
addOffset(count: number): SelectQueryNode<State['output'], Schema>;
|
|
12
|
+
addOrderBy(column: string, direction?: OrderDirection): SelectQueryNode<State['output'], Schema>;
|
|
13
|
+
addHaving(condition: string, parameters?: any[]): SelectQueryNode<State['output'], Schema>;
|
|
14
|
+
setDistinct(): SelectQueryNode<State['output'], Schema>;
|
|
15
|
+
setWithTotals(): SelectQueryNode<State['output'], Schema>;
|
|
115
16
|
}
|
|
116
17
|
//# sourceMappingURL=query-modifiers.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"query-modifiers.d.ts","sourceRoot":"","sources":["../../../src/core/features/query-modifiers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAChF,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;
|
|
1
|
+
{"version":3,"file":"query-modifiers.d.ts","sourceRoot":"","sources":["../../../src/core/features/query-modifiers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,gBAAgB,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AG,eAAe,CAAC,KAAK,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAiB1F,WAAW,IAAI,eAAe,CAAC,KAAK,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAQvD,aAAa,IAAI,eAAe,CAAC,KAAK,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;CAO1D"}
|
|
@@ -4,48 +4,94 @@ export class QueryModifiersFeature {
|
|
|
4
4
|
this.builder = builder;
|
|
5
5
|
}
|
|
6
6
|
addGroupBy(columns) {
|
|
7
|
-
const
|
|
7
|
+
const query = this.builder.getQueryNode();
|
|
8
|
+
const existingExpressions = new Set((query.groupBy || []).map(item => item.expression));
|
|
9
|
+
const groupByItems = (Array.isArray(columns) ? columns.map(String) : [String(columns)])
|
|
10
|
+
.filter(expression => {
|
|
11
|
+
if (existingExpressions.has(expression))
|
|
12
|
+
return false;
|
|
13
|
+
existingExpressions.add(expression);
|
|
14
|
+
return true;
|
|
15
|
+
})
|
|
16
|
+
.map(expression => ({ kind: 'group-by-item', expression }));
|
|
8
17
|
return {
|
|
9
|
-
...
|
|
10
|
-
groupBy:
|
|
18
|
+
...query,
|
|
19
|
+
groupBy: [...(query.groupBy || []), ...groupByItems]
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
addArrayJoin(type, expression) {
|
|
23
|
+
const query = this.builder.getQueryNode();
|
|
24
|
+
return {
|
|
25
|
+
...query,
|
|
26
|
+
arrayJoins: [
|
|
27
|
+
...(query.arrayJoins || []),
|
|
28
|
+
{
|
|
29
|
+
kind: 'array-join',
|
|
30
|
+
type,
|
|
31
|
+
expression,
|
|
32
|
+
},
|
|
33
|
+
],
|
|
11
34
|
};
|
|
12
35
|
}
|
|
13
36
|
addLimit(count) {
|
|
14
|
-
const
|
|
37
|
+
const query = this.builder.getQueryNode();
|
|
15
38
|
return {
|
|
16
|
-
...
|
|
39
|
+
...query,
|
|
17
40
|
limit: count
|
|
18
41
|
};
|
|
19
42
|
}
|
|
43
|
+
addLimitBy(limit, by) {
|
|
44
|
+
const query = this.builder.getQueryNode();
|
|
45
|
+
return {
|
|
46
|
+
...query,
|
|
47
|
+
limitBy: {
|
|
48
|
+
kind: 'limit-by',
|
|
49
|
+
limit,
|
|
50
|
+
by: Array.isArray(by) ? by.map(String) : [String(by)],
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
}
|
|
20
54
|
addOffset(count) {
|
|
21
|
-
const
|
|
55
|
+
const query = this.builder.getQueryNode();
|
|
22
56
|
return {
|
|
23
|
-
...
|
|
57
|
+
...query,
|
|
24
58
|
offset: count
|
|
25
59
|
};
|
|
26
60
|
}
|
|
27
61
|
addOrderBy(column, direction = 'ASC') {
|
|
28
|
-
const
|
|
62
|
+
const query = this.builder.getQueryNode();
|
|
29
63
|
return {
|
|
30
|
-
...
|
|
31
|
-
orderBy: [...(
|
|
64
|
+
...query,
|
|
65
|
+
orderBy: [...(query.orderBy || []), { kind: 'order-by-item', column, direction }]
|
|
32
66
|
};
|
|
33
67
|
}
|
|
34
68
|
addHaving(condition, parameters) {
|
|
35
|
-
const
|
|
36
|
-
const having = [
|
|
37
|
-
|
|
69
|
+
const query = this.builder.getQueryNode();
|
|
70
|
+
const having = [
|
|
71
|
+
...(query.having || []),
|
|
72
|
+
{
|
|
73
|
+
kind: 'having',
|
|
74
|
+
expression: condition,
|
|
75
|
+
parameters: parameters?.map(value => ({ kind: 'value', value })),
|
|
76
|
+
}
|
|
77
|
+
];
|
|
38
78
|
return {
|
|
39
|
-
...
|
|
40
|
-
having
|
|
41
|
-
parameters: newParams
|
|
79
|
+
...query,
|
|
80
|
+
having
|
|
42
81
|
};
|
|
43
82
|
}
|
|
44
83
|
setDistinct() {
|
|
45
|
-
const
|
|
84
|
+
const query = this.builder.getQueryNode();
|
|
46
85
|
return {
|
|
47
|
-
...
|
|
86
|
+
...query,
|
|
48
87
|
distinct: true
|
|
49
88
|
};
|
|
50
89
|
}
|
|
90
|
+
setWithTotals() {
|
|
91
|
+
const query = this.builder.getQueryNode();
|
|
92
|
+
return {
|
|
93
|
+
...query,
|
|
94
|
+
withTotals: true,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
51
97
|
}
|
|
@@ -1,9 +1,20 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type CompiledQuery, type ExprNode, type SelectQueryNode, type SourceNode } from '../../types/index.js';
|
|
2
2
|
export declare class SQLFormatter {
|
|
3
|
-
formatSelect(
|
|
4
|
-
formatGroupBy(
|
|
5
|
-
|
|
3
|
+
formatSelect(query: SelectQueryNode<any, any>): string;
|
|
4
|
+
formatGroupBy(query: SelectQueryNode<any, any>): string;
|
|
5
|
+
formatArrayJoins(query: SelectQueryNode<any, any>): string;
|
|
6
|
+
formatPrewhere(query: SelectQueryNode<any, any>): string;
|
|
7
|
+
formatWhere(query: SelectQueryNode<any, any>): string;
|
|
8
|
+
formatFrom(source?: SourceNode): string;
|
|
9
|
+
compileExpr(expr?: ExprNode, nested?: boolean): CompiledQuery;
|
|
10
|
+
private compileCondition;
|
|
11
|
+
compileHaving(query: SelectQueryNode<any, any>): CompiledQuery;
|
|
6
12
|
private getSqlOperator;
|
|
7
|
-
formatJoins(
|
|
13
|
+
formatJoins(query: SelectQueryNode<any, any>): string;
|
|
14
|
+
formatCtes(query: SelectQueryNode<any, any>): string;
|
|
15
|
+
formatOrderBy(query: SelectQueryNode<any, any>): string;
|
|
16
|
+
formatLimitBy(query: SelectQueryNode<any, any>): string;
|
|
17
|
+
private combineCompiled;
|
|
18
|
+
private combineCompiledWithSeparator;
|
|
8
19
|
}
|
|
9
20
|
//# sourceMappingURL=sql-formatter.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sql-formatter.d.ts","sourceRoot":"","sources":["../../../src/core/formatters/sql-formatter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"sql-formatter.d.ts","sourceRoot":"","sources":["../../../src/core/formatters/sql-formatter.ts"],"names":[],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atter {
|
|
2
|
-
formatSelect(
|
|
3
|
-
const distinctClause =
|
|
4
|
-
if (!
|
|
2
|
+
formatSelect(query) {
|
|
3
|
+
const distinctClause = query.distinct ? 'DISTINCT ' : '';
|
|
4
|
+
if (!query.select?.length)
|
|
5
5
|
return distinctClause + '*';
|
|
6
|
-
return distinctClause +
|
|
6
|
+
return distinctClause + query.select.map(item => item.selection).join(', ');
|
|
7
7
|
}
|
|
8
|
-
formatGroupBy(
|
|
9
|
-
const groupBy =
|
|
8
|
+
formatGroupBy(query) {
|
|
9
|
+
const groupBy = query.groupBy;
|
|
10
10
|
if (!groupBy?.length)
|
|
11
11
|
return '';
|
|
12
|
-
|
|
13
|
-
return groupBy.join(', ');
|
|
14
|
-
}
|
|
15
|
-
return String(groupBy);
|
|
12
|
+
return groupBy.map(item => item.expression).join(', ');
|
|
16
13
|
}
|
|
17
|
-
|
|
18
|
-
if (!
|
|
14
|
+
formatArrayJoins(query) {
|
|
15
|
+
if (!query.arrayJoins?.length)
|
|
19
16
|
return '';
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
17
|
+
return query.arrayJoins
|
|
18
|
+
.map(item => `${item.type} JOIN ${item.expression}`)
|
|
19
|
+
.join(' ');
|
|
20
|
+
}
|
|
21
|
+
formatPrewhere(query) {
|
|
22
|
+
return this.compileExpr(query.prewhere).query;
|
|
23
|
+
}
|
|
24
|
+
formatWhere(query) {
|
|
25
|
+
return this.compileExpr(query.where).query;
|
|
26
|
+
}
|
|
27
|
+
formatFrom(source) {
|
|
28
|
+
if (!source)
|
|
29
|
+
return '';
|
|
30
|
+
switch (source.kind) {
|
|
31
|
+
case 'table':
|
|
32
|
+
return `${source.name}${source.final ? ' FINAL' : ''}`;
|
|
33
|
+
default:
|
|
34
|
+
throw new Error(`Unsupported source kind: ${String(source.kind)}`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
compileExpr(expr, nested = false) {
|
|
38
|
+
if (!expr)
|
|
39
|
+
return { query: '', parameters: [] };
|
|
40
|
+
switch (expr.kind) {
|
|
41
|
+
case 'raw':
|
|
42
|
+
return {
|
|
43
|
+
query: expr.expression,
|
|
44
|
+
parameters: expr.parameters.map(parameter => parameter.value),
|
|
45
|
+
};
|
|
46
|
+
case 'group':
|
|
47
|
+
if (!expr.expression) {
|
|
48
|
+
return { query: '', parameters: [] };
|
|
49
49
|
}
|
|
50
|
-
|
|
51
|
-
|
|
50
|
+
const compiledGroup = this.compileExpr(expr.expression);
|
|
51
|
+
if (!compiledGroup.query) {
|
|
52
|
+
return { query: '', parameters: [] };
|
|
52
53
|
}
|
|
53
|
-
|
|
54
|
-
|
|
54
|
+
return {
|
|
55
|
+
query: `(${compiledGroup.query})`,
|
|
56
|
+
parameters: compiledGroup.parameters,
|
|
57
|
+
};
|
|
58
|
+
case 'sequence':
|
|
59
|
+
return this.combineCompiled(expr.items
|
|
60
|
+
.map((item, index) => {
|
|
61
|
+
const rendered = this.compileExpr(item.expression, true);
|
|
62
|
+
return {
|
|
63
|
+
query: index === 0 ? rendered.query : ` ${item.conjunction} ${rendered.query}`,
|
|
64
|
+
parameters: rendered.parameters,
|
|
65
|
+
};
|
|
66
|
+
})
|
|
67
|
+
.filter(part => part.query.length > 0));
|
|
68
|
+
case 'logical': {
|
|
69
|
+
const rendered = expr.conditions
|
|
70
|
+
.map(condition => this.compileExpr(condition, true))
|
|
71
|
+
.filter(part => part.query.length > 0);
|
|
72
|
+
if (rendered.length === 0)
|
|
73
|
+
return { query: '', parameters: [] };
|
|
74
|
+
const combined = this.combineCompiledWithSeparator(rendered, ` ${expr.operator} `);
|
|
75
|
+
return {
|
|
76
|
+
query: nested ? `(${combined.query})` : combined.query,
|
|
77
|
+
parameters: combined.parameters,
|
|
78
|
+
};
|
|
55
79
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
80
|
+
case 'condition':
|
|
81
|
+
return this.compileCondition(expr);
|
|
82
|
+
default:
|
|
83
|
+
throw new Error(`Unsupported expression kind: ${String(expr.kind)}`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
compileCondition({ column, operator, value }) {
|
|
87
|
+
if (operator === 'isNull' || operator === 'isNotNull') {
|
|
88
|
+
return {
|
|
89
|
+
query: `${column} IS ${operator === 'isNull' ? '' : 'NOT '}NULL`.trim(),
|
|
90
|
+
parameters: [],
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
if (operator === 'in' || operator === 'notIn') {
|
|
94
|
+
if (!Array.isArray(value)) {
|
|
95
|
+
throw new Error(`Expected an array for ${operator} operator, but got ${typeof value}`);
|
|
66
96
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
if (typeof value !== 'string') {
|
|
70
|
-
throw new Error(`Expected a string (subquery) for ${operator} operator, but got ${typeof value}`);
|
|
71
|
-
}
|
|
72
|
-
return `${prefix}${column} ${operator === 'inSubquery' ? 'IN' : 'GLOBAL IN'} (${value})`.trim();
|
|
97
|
+
if (value.length === 0) {
|
|
98
|
+
return { query: operator === 'in' ? '1 = 0' : '1 = 1', parameters: [] };
|
|
73
99
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
100
|
+
return {
|
|
101
|
+
query: `${column} ${operator === 'in' ? 'IN' : 'NOT IN'} (${value.map(() => '?').join(', ')})`,
|
|
102
|
+
parameters: value.map(item => item.value),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
if (operator === 'globalIn' || operator === 'globalNotIn') {
|
|
106
|
+
if (!Array.isArray(value)) {
|
|
107
|
+
throw new Error(`Expected an array for ${operator} operator, but got ${typeof value}`);
|
|
80
108
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
109
|
+
if (value.length === 0) {
|
|
110
|
+
return { query: operator === 'globalIn' ? '1 = 0' : '1 = 1', parameters: [] };
|
|
111
|
+
}
|
|
112
|
+
return {
|
|
113
|
+
query: `${column} ${operator === 'globalIn' ? 'GLOBAL IN' : 'GLOBAL NOT IN'} (${value.map(() => '?').join(', ')})`,
|
|
114
|
+
parameters: value.map(item => item.value),
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
if (operator === 'inSubquery' || operator === 'globalInSubquery') {
|
|
118
|
+
if (typeof value !== 'string') {
|
|
119
|
+
throw new Error(`Expected a string (subquery) for ${operator} operator, but got ${typeof value}`);
|
|
120
|
+
}
|
|
121
|
+
return {
|
|
122
|
+
query: `${column} ${operator === 'inSubquery' ? 'IN' : 'GLOBAL IN'} (${value})`,
|
|
123
|
+
parameters: [],
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
if (operator === 'inTable' || operator === 'globalInTable') {
|
|
127
|
+
if (typeof value !== 'string') {
|
|
128
|
+
throw new Error(`Expected a string (table name) for ${operator} operator, but got ${typeof value}`);
|
|
91
129
|
}
|
|
92
|
-
|
|
93
|
-
|
|
130
|
+
return {
|
|
131
|
+
query: `${column} ${operator === 'inTable' ? 'IN' : 'GLOBAL IN'} ${value}`,
|
|
132
|
+
parameters: [],
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
if (operator === 'inTuple' || operator === 'globalInTuple') {
|
|
136
|
+
if (!Array.isArray(value)) {
|
|
137
|
+
throw new Error(`Expected an array of tuples for ${operator} operator, but got ${typeof value}`);
|
|
94
138
|
}
|
|
95
|
-
|
|
96
|
-
return
|
|
139
|
+
if (value.length === 0) {
|
|
140
|
+
return { query: '1 = 0', parameters: [] };
|
|
97
141
|
}
|
|
98
|
-
|
|
99
|
-
|
|
142
|
+
const tupleWidth = value[0].length;
|
|
143
|
+
if (tupleWidth === 0) {
|
|
144
|
+
throw new Error(`Expected non-empty tuples for ${operator} operator`);
|
|
100
145
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
146
|
+
const tuplePlaceholder = `(${Array.from({ length: tupleWidth }, () => '?').join(', ')})`;
|
|
147
|
+
return {
|
|
148
|
+
query: `${column} ${operator === 'inTuple' ? 'IN' : 'GLOBAL IN'} (${value.map(() => tuplePlaceholder).join(', ')})`,
|
|
149
|
+
parameters: value.flatMap(tuple => tuple.map(item => item.value)),
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
if (operator === 'between') {
|
|
153
|
+
const range = value;
|
|
154
|
+
return {
|
|
155
|
+
query: `${column} BETWEEN ? AND ?`,
|
|
156
|
+
parameters: [range[0].value, range[1].value],
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
if (operator === 'like') {
|
|
160
|
+
const parameter = value;
|
|
161
|
+
return {
|
|
162
|
+
query: `${column} LIKE ?`,
|
|
163
|
+
parameters: [parameter.value],
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
const parameter = value;
|
|
167
|
+
return {
|
|
168
|
+
query: `${column} ${this.getSqlOperator(operator)} ?`,
|
|
169
|
+
parameters: [parameter.value],
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
compileHaving(query) {
|
|
173
|
+
if (!query.having?.length)
|
|
174
|
+
return { query: '', parameters: [] };
|
|
175
|
+
return this.combineCompiledWithSeparator(query.having.map(item => ({
|
|
176
|
+
query: item.expression,
|
|
177
|
+
parameters: item.parameters?.map(parameter => parameter.value) || [],
|
|
178
|
+
})), ' AND ');
|
|
107
179
|
}
|
|
108
180
|
getSqlOperator(operator) {
|
|
109
181
|
switch (operator) {
|
|
@@ -118,14 +190,46 @@ export class SQLFormatter {
|
|
|
118
190
|
throw new Error(`Unsupported operator: ${operator}`);
|
|
119
191
|
}
|
|
120
192
|
}
|
|
121
|
-
formatJoins(
|
|
122
|
-
if (!
|
|
193
|
+
formatJoins(query) {
|
|
194
|
+
if (!query.joins?.length)
|
|
123
195
|
return '';
|
|
124
|
-
return
|
|
196
|
+
return query.joins.map(join => {
|
|
125
197
|
const tableClause = join.alias
|
|
126
198
|
? `${join.table} AS ${join.alias}`
|
|
127
199
|
: join.table;
|
|
128
|
-
|
|
200
|
+
const leftColumn = join.leftSource && !join.leftColumn.includes('.')
|
|
201
|
+
? `${join.leftSource}.${join.leftColumn}`
|
|
202
|
+
: join.leftColumn;
|
|
203
|
+
return `${join.type} JOIN ${tableClause} ON ${leftColumn} = ${join.rightColumn}`;
|
|
129
204
|
}).join(' ');
|
|
130
205
|
}
|
|
206
|
+
formatCtes(query) {
|
|
207
|
+
if (!query.ctes?.length)
|
|
208
|
+
return '';
|
|
209
|
+
return query.ctes.map(item => item.expression).join(', ');
|
|
210
|
+
}
|
|
211
|
+
formatOrderBy(query) {
|
|
212
|
+
if (!query.orderBy?.length)
|
|
213
|
+
return '';
|
|
214
|
+
return query.orderBy
|
|
215
|
+
.map(({ column, direction }) => `${String(column)} ${direction}`.trim())
|
|
216
|
+
.join(', ');
|
|
217
|
+
}
|
|
218
|
+
formatLimitBy(query) {
|
|
219
|
+
if (!query.limitBy)
|
|
220
|
+
return '';
|
|
221
|
+
return `${query.limitBy.limit} BY ${query.limitBy.by.join(', ')}`;
|
|
222
|
+
}
|
|
223
|
+
combineCompiled(parts) {
|
|
224
|
+
return {
|
|
225
|
+
query: parts.map(part => part.query).join(''),
|
|
226
|
+
parameters: parts.flatMap(part => part.parameters),
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
combineCompiledWithSeparator(parts, separator) {
|
|
230
|
+
return {
|
|
231
|
+
query: parts.map(part => part.query).join(separator),
|
|
232
|
+
parameters: parts.flatMap(part => part.parameters),
|
|
233
|
+
};
|
|
234
|
+
}
|
|
131
235
|
}
|