metal-orm 1.0.39 → 1.0.41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1466 -189
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +723 -51
- package/dist/index.d.ts +723 -51
- package/dist/index.js +1457 -189
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/codegen/typescript.ts +66 -5
- package/src/core/ast/aggregate-functions.ts +15 -15
- package/src/core/ast/expression-builders.ts +378 -316
- package/src/core/ast/expression-nodes.ts +210 -186
- package/src/core/ast/expression-visitor.ts +40 -30
- package/src/core/ast/query.ts +164 -132
- package/src/core/ast/window-functions.ts +86 -86
- package/src/core/dialect/abstract.ts +509 -479
- package/src/core/dialect/base/groupby-compiler.ts +6 -6
- package/src/core/dialect/base/join-compiler.ts +9 -12
- package/src/core/dialect/base/orderby-compiler.ts +20 -6
- package/src/core/dialect/base/sql-dialect.ts +237 -138
- package/src/core/dialect/mssql/index.ts +164 -185
- package/src/core/dialect/sqlite/index.ts +39 -34
- package/src/core/execution/db-executor.ts +46 -6
- package/src/core/execution/executors/mssql-executor.ts +39 -22
- package/src/core/execution/executors/mysql-executor.ts +23 -6
- package/src/core/execution/executors/sqlite-executor.ts +29 -3
- package/src/core/execution/pooling/pool-types.ts +30 -0
- package/src/core/execution/pooling/pool.ts +268 -0
- package/src/core/functions/standard-strategy.ts +46 -37
- package/src/decorators/bootstrap.ts +7 -7
- package/src/index.ts +6 -0
- package/src/orm/domain-event-bus.ts +49 -0
- package/src/orm/entity-metadata.ts +9 -9
- package/src/orm/entity.ts +58 -0
- package/src/orm/orm-session.ts +465 -270
- package/src/orm/orm.ts +61 -11
- package/src/orm/pooled-executor-factory.ts +131 -0
- package/src/orm/query-logger.ts +6 -12
- package/src/orm/relation-change-processor.ts +75 -0
- package/src/orm/relations/many-to-many.ts +4 -2
- package/src/orm/save-graph.ts +303 -0
- package/src/orm/transaction-runner.ts +3 -3
- package/src/orm/unit-of-work.ts +128 -0
- package/src/query-builder/delete-query-state.ts +67 -38
- package/src/query-builder/delete.ts +37 -1
- package/src/query-builder/hydration-manager.ts +93 -79
- package/src/query-builder/insert-query-state.ts +131 -61
- package/src/query-builder/insert.ts +27 -1
- package/src/query-builder/query-ast-service.ts +207 -170
- package/src/query-builder/select-query-state.ts +169 -162
- package/src/query-builder/select.ts +15 -23
- package/src/query-builder/update-query-state.ts +114 -77
- package/src/query-builder/update.ts +38 -1
package/src/core/ast/query.ts
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ColumnNode,
|
|
3
|
-
FunctionNode,
|
|
4
|
-
ExpressionNode,
|
|
5
|
-
ScalarSubqueryNode,
|
|
6
|
-
CaseExpressionNode,
|
|
7
|
-
WindowFunctionNode,
|
|
8
|
-
OperandNode
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
import {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
import {
|
|
2
|
+
ColumnNode,
|
|
3
|
+
FunctionNode,
|
|
4
|
+
ExpressionNode,
|
|
5
|
+
ScalarSubqueryNode,
|
|
6
|
+
CaseExpressionNode,
|
|
7
|
+
WindowFunctionNode,
|
|
8
|
+
OperandNode,
|
|
9
|
+
AliasRefNode
|
|
10
|
+
} from './expression.js';
|
|
11
|
+
import { JoinNode } from './join.js';
|
|
12
|
+
import { OrderDirection } from '../sql/sql.js';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* AST node representing a table reference in a query
|
|
16
|
+
*/
|
|
16
17
|
export interface TableNode {
|
|
17
18
|
type: 'Table';
|
|
18
19
|
/** Table name */
|
|
@@ -25,18 +26,18 @@ export interface TableNode {
|
|
|
25
26
|
|
|
26
27
|
/**
|
|
27
28
|
* AST node representing a function used as a table source (table-valued function)
|
|
28
|
-
*/
|
|
29
|
-
export interface FunctionTableNode {
|
|
30
|
-
type: 'FunctionTable';
|
|
31
|
-
/** Function name */
|
|
32
|
-
name: string;
|
|
33
|
-
/** Optional schema for the function (some dialects) */
|
|
34
|
-
schema?: string;
|
|
35
|
-
/** Function arguments as operand nodes */
|
|
36
|
-
args?: any[]; // use any to avoid circular import here; caller should supply OperandNode
|
|
37
|
-
/** Optional alias for the function table */
|
|
38
|
-
alias?: string;
|
|
39
|
-
/** LATERAL flag */
|
|
29
|
+
*/
|
|
30
|
+
export interface FunctionTableNode {
|
|
31
|
+
type: 'FunctionTable';
|
|
32
|
+
/** Function name */
|
|
33
|
+
name: string;
|
|
34
|
+
/** Optional schema for the function (some dialects) */
|
|
35
|
+
schema?: string;
|
|
36
|
+
/** Function arguments as operand nodes */
|
|
37
|
+
args?: any[]; // use any to avoid circular import here; caller should supply OperandNode
|
|
38
|
+
/** Optional alias for the function table */
|
|
39
|
+
alias?: string;
|
|
40
|
+
/** LATERAL flag */
|
|
40
41
|
lateral?: boolean;
|
|
41
42
|
/** WITH ORDINALITY flag */
|
|
42
43
|
withOrdinality?: boolean;
|
|
@@ -59,118 +60,149 @@ export interface DerivedTableNode {
|
|
|
59
60
|
|
|
60
61
|
export type TableSourceNode = TableNode | FunctionTableNode | DerivedTableNode;
|
|
61
62
|
|
|
63
|
+
/**
|
|
64
|
+
* Any expression that can appear in ORDER BY / GROUP BY terms.
|
|
65
|
+
*/
|
|
66
|
+
export type OrderingTerm = OperandNode | ExpressionNode | AliasRefNode;
|
|
67
|
+
|
|
62
68
|
/**
|
|
63
69
|
* AST node representing an ORDER BY clause
|
|
64
70
|
*/
|
|
65
71
|
export interface OrderByNode {
|
|
66
|
-
type: 'OrderBy';
|
|
67
|
-
/**
|
|
68
|
-
|
|
69
|
-
/** Order direction (ASC or DESC) */
|
|
70
|
-
direction: OrderDirection;
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
/** CTE
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
|
|
106
|
-
|
|
72
|
+
type: 'OrderBy';
|
|
73
|
+
/** Expression/operand/alias to order by */
|
|
74
|
+
term: OrderingTerm;
|
|
75
|
+
/** Order direction (ASC or DESC) */
|
|
76
|
+
direction: OrderDirection;
|
|
77
|
+
/** Optional nulls ordering (NULLS FIRST/LAST) */
|
|
78
|
+
nulls?: 'FIRST' | 'LAST';
|
|
79
|
+
/** Optional collation */
|
|
80
|
+
collation?: string;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* AST node representing a Common Table Expression (CTE)
|
|
85
|
+
*/
|
|
86
|
+
export interface CommonTableExpressionNode {
|
|
87
|
+
type: 'CommonTableExpression';
|
|
88
|
+
/** CTE name */
|
|
89
|
+
name: string;
|
|
90
|
+
/** Optional column names */
|
|
91
|
+
columns?: string[];
|
|
92
|
+
/** CTE query */
|
|
93
|
+
query: SelectQueryNode;
|
|
94
|
+
/** Whether the CTE is recursive */
|
|
95
|
+
recursive: boolean;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Supported set operation kinds for compound SELECT queries
|
|
100
|
+
*/
|
|
101
|
+
export type SetOperationKind = 'UNION' | 'UNION ALL' | 'INTERSECT' | 'EXCEPT';
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* AST node representing a set operation (UNION, INTERSECT, etc.)
|
|
105
|
+
*/
|
|
106
|
+
export interface SetOperationNode {
|
|
107
|
+
type: 'SetOperation';
|
|
108
|
+
/** Operator to combine queries */
|
|
109
|
+
operator: SetOperationKind;
|
|
110
|
+
/** Right-hand query in the compound expression */
|
|
111
|
+
query: SelectQueryNode;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* AST node representing a complete SELECT query
|
|
116
|
+
*/
|
|
107
117
|
export interface SelectQueryNode {
|
|
108
118
|
type: 'SelectQuery';
|
|
109
119
|
/** Optional CTEs (WITH clauses) */
|
|
110
120
|
ctes?: CommonTableExpressionNode[];
|
|
111
121
|
/** FROM clause table (either a Table or a FunctionTable) */
|
|
112
122
|
from: TableSourceNode;
|
|
113
|
-
/** SELECT clause columns */
|
|
114
|
-
columns: (ColumnNode | FunctionNode | ScalarSubqueryNode | CaseExpressionNode | WindowFunctionNode)[];
|
|
115
|
-
/** JOIN clauses */
|
|
116
|
-
joins: JoinNode[];
|
|
117
|
-
/** Optional WHERE clause */
|
|
118
|
-
where?: ExpressionNode;
|
|
119
|
-
/** Optional GROUP BY clause */
|
|
120
|
-
groupBy?:
|
|
121
|
-
/** Optional HAVING clause */
|
|
122
|
-
having?: ExpressionNode;
|
|
123
|
-
/** Optional ORDER BY clause */
|
|
124
|
-
orderBy?: OrderByNode[];
|
|
125
|
-
/** Optional LIMIT clause */
|
|
126
|
-
limit?: number;
|
|
127
|
-
/** Optional OFFSET clause */
|
|
128
|
-
offset?: number;
|
|
129
|
-
/** Optional query metadata */
|
|
130
|
-
meta?: Record<string, unknown>;
|
|
131
|
-
/** Optional DISTINCT clause */
|
|
132
|
-
distinct?: ColumnNode[];
|
|
133
|
-
/** Optional set operations chaining this query with others */
|
|
134
|
-
setOps?: SetOperationNode[];
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
export interface
|
|
138
|
-
type: '
|
|
139
|
-
/**
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
export
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
|
|
174
|
-
/** Optional
|
|
175
|
-
|
|
176
|
-
|
|
123
|
+
/** SELECT clause columns */
|
|
124
|
+
columns: (ColumnNode | FunctionNode | ScalarSubqueryNode | CaseExpressionNode | WindowFunctionNode)[];
|
|
125
|
+
/** JOIN clauses */
|
|
126
|
+
joins: JoinNode[];
|
|
127
|
+
/** Optional WHERE clause */
|
|
128
|
+
where?: ExpressionNode;
|
|
129
|
+
/** Optional GROUP BY clause */
|
|
130
|
+
groupBy?: OrderingTerm[];
|
|
131
|
+
/** Optional HAVING clause */
|
|
132
|
+
having?: ExpressionNode;
|
|
133
|
+
/** Optional ORDER BY clause */
|
|
134
|
+
orderBy?: OrderByNode[];
|
|
135
|
+
/** Optional LIMIT clause */
|
|
136
|
+
limit?: number;
|
|
137
|
+
/** Optional OFFSET clause */
|
|
138
|
+
offset?: number;
|
|
139
|
+
/** Optional query metadata */
|
|
140
|
+
meta?: Record<string, unknown>;
|
|
141
|
+
/** Optional DISTINCT clause */
|
|
142
|
+
distinct?: ColumnNode[];
|
|
143
|
+
/** Optional set operations chaining this query with others */
|
|
144
|
+
setOps?: SetOperationNode[];
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export interface InsertValuesSourceNode {
|
|
148
|
+
type: 'InsertValues';
|
|
149
|
+
/** Rows of values for INSERT rows */
|
|
150
|
+
rows: OperandNode[][];
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export interface InsertSelectSourceNode {
|
|
154
|
+
type: 'InsertSelect';
|
|
155
|
+
/** SELECT query providing rows */
|
|
156
|
+
query: SelectQueryNode;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export type InsertSourceNode = InsertValuesSourceNode | InsertSelectSourceNode;
|
|
160
|
+
|
|
161
|
+
export interface InsertQueryNode {
|
|
162
|
+
type: 'InsertQuery';
|
|
163
|
+
/** Target table */
|
|
164
|
+
into: TableNode;
|
|
165
|
+
/** Column order for inserted values */
|
|
166
|
+
columns: ColumnNode[];
|
|
167
|
+
/** Source of inserted rows (either literal values or a SELECT query) */
|
|
168
|
+
source: InsertSourceNode;
|
|
169
|
+
/** Optional RETURNING clause */
|
|
170
|
+
returning?: ColumnNode[];
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export interface UpdateAssignmentNode {
|
|
174
|
+
/** Column to update */
|
|
175
|
+
column: ColumnNode;
|
|
176
|
+
/** Value to set */
|
|
177
|
+
value: OperandNode;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export interface UpdateQueryNode {
|
|
181
|
+
type: 'UpdateQuery';
|
|
182
|
+
/** Table being updated */
|
|
183
|
+
table: TableNode;
|
|
184
|
+
/** Optional FROM clause for multi-table updates */
|
|
185
|
+
from?: TableSourceNode;
|
|
186
|
+
/** Optional joins applied to the FROM/USING tables */
|
|
187
|
+
joins?: JoinNode[];
|
|
188
|
+
/** Assignments for SET clause */
|
|
189
|
+
set: UpdateAssignmentNode[];
|
|
190
|
+
/** Optional WHERE clause */
|
|
191
|
+
where?: ExpressionNode;
|
|
192
|
+
/** Optional RETURNING clause */
|
|
193
|
+
returning?: ColumnNode[];
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export interface DeleteQueryNode {
|
|
197
|
+
type: 'DeleteQuery';
|
|
198
|
+
/** Table to delete from */
|
|
199
|
+
from: TableNode;
|
|
200
|
+
/** Optional USING clause for multi-table deletes */
|
|
201
|
+
using?: TableSourceNode;
|
|
202
|
+
/** Optional joins applied to the USING clause */
|
|
203
|
+
joins?: JoinNode[];
|
|
204
|
+
/** Optional WHERE clause */
|
|
205
|
+
where?: ExpressionNode;
|
|
206
|
+
/** Optional RETURNING clause */
|
|
207
|
+
returning?: ColumnNode[];
|
|
208
|
+
}
|
|
@@ -3,60 +3,60 @@ import { columnOperand } from './expression-builders.js';
|
|
|
3
3
|
import { OrderDirection } from '../sql/sql.js';
|
|
4
4
|
import { OrderByNode } from './query.js';
|
|
5
5
|
import { ColumnRef } from './types.js';
|
|
6
|
-
|
|
7
|
-
const buildWindowFunction = (
|
|
8
|
-
name: string,
|
|
9
|
-
args: (ColumnNode | LiteralNode | JsonPathNode)[] = [],
|
|
10
|
-
partitionBy?: ColumnNode[],
|
|
11
|
-
orderBy?: OrderByNode[]
|
|
12
|
-
): WindowFunctionNode => {
|
|
13
|
-
const node: WindowFunctionNode = {
|
|
14
|
-
type: 'WindowFunction',
|
|
15
|
-
name,
|
|
16
|
-
args
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
if (partitionBy && partitionBy.length) {
|
|
20
|
-
node.partitionBy = partitionBy;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
if (orderBy && orderBy.length) {
|
|
24
|
-
node.orderBy = orderBy;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return node;
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Creates a ROW_NUMBER window function
|
|
32
|
-
* @returns Window function node for ROW_NUMBER
|
|
33
|
-
*/
|
|
34
|
-
export const rowNumber = (): WindowFunctionNode => buildWindowFunction('ROW_NUMBER');
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Creates a RANK window function
|
|
38
|
-
* @returns Window function node for RANK
|
|
39
|
-
*/
|
|
40
|
-
export const rank = (): WindowFunctionNode => buildWindowFunction('RANK');
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Creates a DENSE_RANK window function
|
|
44
|
-
* @returns Window function node for DENSE_RANK
|
|
45
|
-
*/
|
|
46
|
-
export const denseRank = (): WindowFunctionNode => buildWindowFunction('DENSE_RANK');
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Creates an NTILE window function
|
|
50
|
-
* @param n - Number of buckets
|
|
51
|
-
* @returns Window function node for NTILE
|
|
52
|
-
*/
|
|
6
|
+
|
|
7
|
+
const buildWindowFunction = (
|
|
8
|
+
name: string,
|
|
9
|
+
args: (ColumnNode | LiteralNode | JsonPathNode)[] = [],
|
|
10
|
+
partitionBy?: ColumnNode[],
|
|
11
|
+
orderBy?: OrderByNode[]
|
|
12
|
+
): WindowFunctionNode => {
|
|
13
|
+
const node: WindowFunctionNode = {
|
|
14
|
+
type: 'WindowFunction',
|
|
15
|
+
name,
|
|
16
|
+
args
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
if (partitionBy && partitionBy.length) {
|
|
20
|
+
node.partitionBy = partitionBy;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (orderBy && orderBy.length) {
|
|
24
|
+
node.orderBy = orderBy;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return node;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Creates a ROW_NUMBER window function
|
|
32
|
+
* @returns Window function node for ROW_NUMBER
|
|
33
|
+
*/
|
|
34
|
+
export const rowNumber = (): WindowFunctionNode => buildWindowFunction('ROW_NUMBER');
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Creates a RANK window function
|
|
38
|
+
* @returns Window function node for RANK
|
|
39
|
+
*/
|
|
40
|
+
export const rank = (): WindowFunctionNode => buildWindowFunction('RANK');
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Creates a DENSE_RANK window function
|
|
44
|
+
* @returns Window function node for DENSE_RANK
|
|
45
|
+
*/
|
|
46
|
+
export const denseRank = (): WindowFunctionNode => buildWindowFunction('DENSE_RANK');
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Creates an NTILE window function
|
|
50
|
+
* @param n - Number of buckets
|
|
51
|
+
* @returns Window function node for NTILE
|
|
52
|
+
*/
|
|
53
53
|
export const ntile = (n: number): WindowFunctionNode =>
|
|
54
54
|
buildWindowFunction('NTILE', [{ type: 'Literal', value: n }]);
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Creates a LAG window function
|
|
58
|
-
* @param col - Column to lag
|
|
59
|
-
* @param offset - Offset (defaults to 1)
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Creates a LAG window function
|
|
58
|
+
* @param col - Column to lag
|
|
59
|
+
* @param offset - Offset (defaults to 1)
|
|
60
60
|
* @param defaultValue - Default value if no row exists
|
|
61
61
|
* @returns Window function node for LAG
|
|
62
62
|
*/
|
|
@@ -65,16 +65,16 @@ export const lag = (col: ColumnRef | ColumnNode, offset: number = 1, defaultValu
|
|
|
65
65
|
columnOperand(col),
|
|
66
66
|
{ type: 'Literal', value: offset }
|
|
67
67
|
];
|
|
68
|
-
if (defaultValue !== undefined) {
|
|
69
|
-
args.push({ type: 'Literal', value: defaultValue });
|
|
70
|
-
}
|
|
71
|
-
return buildWindowFunction('LAG', args);
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Creates a LEAD window function
|
|
76
|
-
* @param col - Column to lead
|
|
77
|
-
* @param offset - Offset (defaults to 1)
|
|
68
|
+
if (defaultValue !== undefined) {
|
|
69
|
+
args.push({ type: 'Literal', value: defaultValue });
|
|
70
|
+
}
|
|
71
|
+
return buildWindowFunction('LAG', args);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Creates a LEAD window function
|
|
76
|
+
* @param col - Column to lead
|
|
77
|
+
* @param offset - Offset (defaults to 1)
|
|
78
78
|
* @param defaultValue - Default value if no row exists
|
|
79
79
|
* @returns Window function node for LEAD
|
|
80
80
|
*/
|
|
@@ -83,36 +83,36 @@ export const lead = (col: ColumnRef | ColumnNode, offset: number = 1, defaultVal
|
|
|
83
83
|
columnOperand(col),
|
|
84
84
|
{ type: 'Literal', value: offset }
|
|
85
85
|
];
|
|
86
|
-
if (defaultValue !== undefined) {
|
|
87
|
-
args.push({ type: 'Literal', value: defaultValue });
|
|
88
|
-
}
|
|
89
|
-
return buildWindowFunction('LEAD', args);
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Creates a FIRST_VALUE window function
|
|
86
|
+
if (defaultValue !== undefined) {
|
|
87
|
+
args.push({ type: 'Literal', value: defaultValue });
|
|
88
|
+
}
|
|
89
|
+
return buildWindowFunction('LEAD', args);
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Creates a FIRST_VALUE window function
|
|
94
94
|
* @param col - Column to get first value from
|
|
95
95
|
* @returns Window function node for FIRST_VALUE
|
|
96
96
|
*/
|
|
97
97
|
export const firstValue = (col: ColumnRef | ColumnNode): WindowFunctionNode =>
|
|
98
98
|
buildWindowFunction('FIRST_VALUE', [columnOperand(col)]);
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Creates a LAST_VALUE window function
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Creates a LAST_VALUE window function
|
|
102
102
|
* @param col - Column to get last value from
|
|
103
103
|
* @returns Window function node for LAST_VALUE
|
|
104
104
|
*/
|
|
105
105
|
export const lastValue = (col: ColumnRef | ColumnNode): WindowFunctionNode =>
|
|
106
106
|
buildWindowFunction('LAST_VALUE', [columnOperand(col)]);
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Creates a custom window function
|
|
110
|
-
* @param name - Window function name
|
|
111
|
-
* @param args - Function arguments
|
|
112
|
-
* @param partitionBy - Optional PARTITION BY columns
|
|
113
|
-
* @param orderBy - Optional ORDER BY clauses
|
|
114
|
-
* @returns Window function node
|
|
115
|
-
*/
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Creates a custom window function
|
|
110
|
+
* @param name - Window function name
|
|
111
|
+
* @param args - Function arguments
|
|
112
|
+
* @param partitionBy - Optional PARTITION BY columns
|
|
113
|
+
* @param orderBy - Optional ORDER BY clauses
|
|
114
|
+
* @returns Window function node
|
|
115
|
+
*/
|
|
116
116
|
export const windowFunction = (
|
|
117
117
|
name: string,
|
|
118
118
|
args: (ColumnRef | ColumnNode | LiteralNode | JsonPathNode)[] = [],
|
|
@@ -132,9 +132,9 @@ export const windowFunction = (
|
|
|
132
132
|
const partitionNodes = partitionBy?.map(col => columnOperand(col)) ?? undefined;
|
|
133
133
|
const orderNodes: OrderByNode[] | undefined = orderBy?.map(o => ({
|
|
134
134
|
type: 'OrderBy',
|
|
135
|
-
|
|
135
|
+
term: columnOperand(o.column),
|
|
136
136
|
direction: o.direction
|
|
137
137
|
}));
|
|
138
|
-
|
|
139
|
-
return buildWindowFunction(name, nodeArgs, partitionNodes, orderNodes);
|
|
140
|
-
};
|
|
138
|
+
|
|
139
|
+
return buildWindowFunction(name, nodeArgs, partitionNodes, orderNodes);
|
|
140
|
+
};
|