kysely 0.21.5 → 0.22.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.md +3 -2
- package/dist/cjs/dialect/sqlite/sqlite-query-compiler.d.ts +2 -0
- package/dist/cjs/dialect/sqlite/sqlite-query-compiler.js +4 -0
- package/dist/cjs/index.d.ts +10 -4
- package/dist/cjs/index.js +8 -2
- package/dist/cjs/kysely.d.ts +3 -3
- package/dist/cjs/kysely.js +3 -3
- package/dist/cjs/migration/file-migration-provider.js +7 -2
- package/dist/cjs/operation-node/aggregate-function-node.d.ts +22 -0
- package/dist/cjs/operation-node/aggregate-function-node.js +31 -0
- package/dist/cjs/operation-node/alias-node.d.ts +2 -1
- package/dist/cjs/operation-node/default-insert-value-node.d.ts +11 -0
- package/dist/cjs/operation-node/{union-node.js → default-insert-value-node.js} +5 -7
- package/dist/cjs/operation-node/group-by-node.d.ts +1 -1
- package/dist/cjs/operation-node/group-by-node.js +3 -3
- package/dist/cjs/operation-node/insert-query-node.d.ts +2 -1
- package/dist/cjs/operation-node/operation-node-transformer.d.ts +12 -2
- package/dist/cjs/operation-node/operation-node-transformer.js +43 -5
- package/dist/cjs/operation-node/operation-node-utils.d.ts +2 -1
- package/dist/cjs/operation-node/operation-node-visitor.d.ts +12 -2
- package/dist/cjs/operation-node/operation-node-visitor.js +6 -1
- package/dist/cjs/operation-node/operation-node.d.ts +1 -1
- package/dist/cjs/operation-node/over-node.d.ts +19 -0
- package/dist/cjs/operation-node/over-node.js +35 -0
- package/dist/cjs/operation-node/partition-by-item-node.d.ts +14 -0
- package/dist/cjs/operation-node/partition-by-item-node.js +18 -0
- package/dist/cjs/operation-node/partition-by-node.d.ts +14 -0
- package/dist/cjs/operation-node/partition-by-node.js +24 -0
- package/dist/cjs/operation-node/select-query-node.d.ts +3 -3
- package/dist/cjs/operation-node/select-query-node.js +4 -4
- package/dist/cjs/operation-node/set-operation-node.d.ts +18 -0
- package/dist/cjs/operation-node/set-operation-node.js +20 -0
- package/dist/cjs/operation-node/value-list-node.d.ts +3 -1
- package/dist/cjs/parser/complex-expression-parser.d.ts +6 -4
- package/dist/cjs/parser/group-by-parser.d.ts +1 -1
- package/dist/cjs/parser/group-by-parser.js +2 -2
- package/dist/cjs/parser/insert-values-parser.js +13 -4
- package/dist/cjs/parser/parse-utils.d.ts +2 -0
- package/dist/cjs/parser/parse-utils.js +9 -1
- package/dist/cjs/parser/partition-by-parser.d.ts +6 -0
- package/dist/cjs/parser/partition-by-parser.js +9 -0
- package/dist/cjs/parser/reference-parser.d.ts +2 -1
- package/dist/cjs/parser/select-parser.d.ts +4 -3
- package/dist/cjs/parser/set-operation-parser.d.ts +5 -0
- package/dist/cjs/parser/set-operation-parser.js +8 -0
- package/dist/cjs/parser/value-parser.d.ts +2 -1
- package/dist/cjs/query-builder/aggregate-function-builder.d.ts +117 -0
- package/dist/cjs/query-builder/aggregate-function-builder.js +146 -0
- package/dist/cjs/query-builder/expression-builder.d.ts +3 -3
- package/dist/cjs/query-builder/expression-builder.js +3 -3
- package/dist/cjs/query-builder/{function-builder.d.ts → function-module.d.ts} +49 -42
- package/dist/cjs/query-builder/{function-builder.js → function-module.js} +70 -59
- package/dist/cjs/query-builder/over-builder.d.ts +59 -0
- package/dist/cjs/query-builder/over-builder.js +50 -0
- package/dist/cjs/query-builder/select-query-builder.d.ts +63 -3
- package/dist/cjs/query-builder/select-query-builder.js +83 -3
- package/dist/cjs/query-compiler/default-query-compiler.d.ts +12 -2
- package/dist/cjs/query-compiler/default-query-compiler.js +44 -14
- package/dist/cjs/raw-builder/sql.d.ts +13 -5
- package/dist/cjs/raw-builder/sql.js +6 -0
- package/dist/cjs/util/object-utils.d.ts +1 -0
- package/dist/cjs/util/object-utils.js +5 -1
- package/dist/cjs/util/type-utils.d.ts +5 -1
- package/dist/esm/dialect/sqlite/sqlite-query-compiler.d.ts +2 -0
- package/dist/esm/dialect/sqlite/sqlite-query-compiler.js +4 -0
- package/dist/esm/index.d.ts +10 -4
- package/dist/esm/index.js +8 -2
- package/dist/esm/kysely.d.ts +3 -3
- package/dist/esm/kysely.js +3 -3
- package/dist/esm/migration/file-migration-provider.js +7 -2
- package/dist/esm/operation-node/aggregate-function-node.d.ts +22 -0
- package/dist/esm/operation-node/aggregate-function-node.js +29 -0
- package/dist/esm/operation-node/alias-node.d.ts +2 -1
- package/dist/esm/operation-node/default-insert-value-node.d.ts +11 -0
- package/dist/esm/operation-node/default-insert-value-node.js +15 -0
- package/dist/esm/operation-node/group-by-node.d.ts +1 -1
- package/dist/esm/operation-node/group-by-node.js +3 -3
- package/dist/esm/operation-node/insert-query-node.d.ts +2 -1
- package/dist/esm/operation-node/operation-node-transformer.d.ts +12 -2
- package/dist/esm/operation-node/operation-node-transformer.js +43 -5
- package/dist/esm/operation-node/operation-node-utils.d.ts +2 -1
- package/dist/esm/operation-node/operation-node-visitor.d.ts +12 -2
- package/dist/esm/operation-node/operation-node-visitor.js +6 -1
- package/dist/esm/operation-node/operation-node.d.ts +1 -1
- package/dist/esm/operation-node/over-node.d.ts +19 -0
- package/dist/esm/operation-node/over-node.js +33 -0
- package/dist/esm/operation-node/partition-by-item-node.d.ts +14 -0
- package/dist/esm/operation-node/partition-by-item-node.js +16 -0
- package/dist/esm/operation-node/partition-by-node.d.ts +14 -0
- package/dist/esm/operation-node/partition-by-node.js +22 -0
- package/dist/esm/operation-node/select-query-node.d.ts +3 -3
- package/dist/esm/operation-node/select-query-node.js +4 -4
- package/dist/esm/operation-node/set-operation-node.d.ts +18 -0
- package/dist/esm/operation-node/set-operation-node.js +18 -0
- package/dist/esm/operation-node/value-list-node.d.ts +3 -1
- package/dist/esm/parser/complex-expression-parser.d.ts +6 -4
- package/dist/esm/parser/group-by-parser.d.ts +1 -1
- package/dist/esm/parser/group-by-parser.js +2 -2
- package/dist/esm/parser/insert-values-parser.js +14 -5
- package/dist/esm/parser/parse-utils.d.ts +2 -0
- package/dist/esm/parser/parse-utils.js +7 -0
- package/dist/esm/parser/partition-by-parser.d.ts +6 -0
- package/dist/esm/parser/partition-by-parser.js +6 -0
- package/dist/esm/parser/reference-parser.d.ts +2 -1
- package/dist/esm/parser/select-parser.d.ts +4 -3
- package/dist/esm/parser/set-operation-parser.d.ts +5 -0
- package/dist/esm/parser/set-operation-parser.js +5 -0
- package/dist/esm/parser/value-parser.d.ts +2 -1
- package/dist/esm/query-builder/aggregate-function-builder.d.ts +117 -0
- package/dist/esm/query-builder/aggregate-function-builder.js +142 -0
- package/dist/esm/query-builder/expression-builder.d.ts +3 -3
- package/dist/esm/query-builder/expression-builder.js +3 -3
- package/dist/esm/query-builder/{function-builder.d.ts → function-module.d.ts} +49 -42
- package/dist/esm/query-builder/{function-builder.js → function-module.js} +69 -58
- package/dist/esm/query-builder/over-builder.d.ts +59 -0
- package/dist/esm/query-builder/over-builder.js +47 -0
- package/dist/esm/query-builder/select-query-builder.d.ts +63 -3
- package/dist/esm/query-builder/select-query-builder.js +83 -3
- package/dist/esm/query-compiler/default-query-compiler.d.ts +12 -2
- package/dist/esm/query-compiler/default-query-compiler.js +44 -14
- package/dist/esm/raw-builder/sql.d.ts +13 -5
- package/dist/esm/raw-builder/sql.js +6 -0
- package/dist/esm/util/object-utils.d.ts +1 -0
- package/dist/esm/util/object-utils.js +3 -0
- package/dist/esm/util/type-utils.d.ts +5 -1
- package/package.json +4 -1
- package/dist/cjs/operation-node/union-node.d.ts +0 -16
- package/dist/cjs/parser/union-parser.d.ts +0 -5
- package/dist/cjs/parser/union-parser.js +0 -8
- package/dist/esm/operation-node/union-node.d.ts +0 -16
- package/dist/esm/operation-node/union-node.js +0 -17
- package/dist/esm/parser/union-parser.d.ts +0 -5
- package/dist/esm/parser/union-parser.js +0 -5
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
/// <reference types="./function-
|
|
2
|
-
import {
|
|
1
|
+
/// <reference types="./function-module.d.ts" />
|
|
2
|
+
import { AggregateFunctionNode } from '../operation-node/aggregate-function-node.js';
|
|
3
3
|
import { parseStringReference, } from '../parser/reference-parser.js';
|
|
4
|
-
import {
|
|
5
|
-
import { createQueryId } from '../util/query-id.js';
|
|
4
|
+
import { AggregateFunctionBuilder } from './aggregate-function-builder.js';
|
|
6
5
|
/**
|
|
7
6
|
* Helpers for type safe SQL function calls.
|
|
8
7
|
*
|
|
@@ -35,46 +34,13 @@ import { createQueryId } from '../util/query-id.js';
|
|
|
35
34
|
* having count("pet"."id") > $1
|
|
36
35
|
* ```
|
|
37
36
|
*/
|
|
38
|
-
export class
|
|
37
|
+
export class FunctionModule {
|
|
39
38
|
constructor() {
|
|
40
|
-
this.min = this.min.bind(this);
|
|
41
|
-
this.max = this.max.bind(this);
|
|
42
39
|
this.avg = this.avg.bind(this);
|
|
43
|
-
this.sum = this.sum.bind(this);
|
|
44
40
|
this.count = this.count.bind(this);
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
*
|
|
49
|
-
* ### Examples
|
|
50
|
-
*
|
|
51
|
-
* ```ts
|
|
52
|
-
* const { max } = db.fn
|
|
53
|
-
*
|
|
54
|
-
* db.selectFrom('toy')
|
|
55
|
-
* .select(max('price').as('max_price'))
|
|
56
|
-
* .execute()
|
|
57
|
-
* ```
|
|
58
|
-
*/
|
|
59
|
-
max(column) {
|
|
60
|
-
return this.#oneArgFunction('max', column);
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Calls the `min` function for the column given as the argument.
|
|
64
|
-
*
|
|
65
|
-
* ### Examples
|
|
66
|
-
*
|
|
67
|
-
* ```ts
|
|
68
|
-
* const { min } = db.fn
|
|
69
|
-
*
|
|
70
|
-
* db.selectFrom('toy')
|
|
71
|
-
* .select(min('price').as('min_price'))
|
|
72
|
-
* .execute()
|
|
73
|
-
* ```
|
|
74
|
-
*
|
|
75
|
-
*/
|
|
76
|
-
min(column) {
|
|
77
|
-
return this.#oneArgFunction('min', column);
|
|
41
|
+
this.max = this.max.bind(this);
|
|
42
|
+
this.min = this.min.bind(this);
|
|
43
|
+
this.sum = this.sum.bind(this);
|
|
78
44
|
}
|
|
79
45
|
/**
|
|
80
46
|
* Calls the `avg` function for the column given as the argument.
|
|
@@ -98,10 +64,12 @@ export class FunctionBuilder {
|
|
|
98
64
|
* ```
|
|
99
65
|
*/
|
|
100
66
|
avg(column) {
|
|
101
|
-
return
|
|
67
|
+
return new AggregateFunctionBuilder({
|
|
68
|
+
aggregateFunctionNode: AggregateFunctionNode.create('avg', parseStringReference(column)),
|
|
69
|
+
});
|
|
102
70
|
}
|
|
103
71
|
/**
|
|
104
|
-
* Calls the `
|
|
72
|
+
* Calls the `count` function for the column given as the argument.
|
|
105
73
|
*
|
|
106
74
|
* If this is used in a `select` statement the type of the selected expression
|
|
107
75
|
* will be `number | string | bigint` by default. This is because Kysely can't
|
|
@@ -114,21 +82,68 @@ export class FunctionBuilder {
|
|
|
114
82
|
* the type as the first type argument:
|
|
115
83
|
*
|
|
116
84
|
* ```ts
|
|
117
|
-
* const {
|
|
85
|
+
* const { count } = db.fn
|
|
118
86
|
*
|
|
119
87
|
* db.selectFrom('toy')
|
|
120
|
-
* .select(
|
|
88
|
+
* .select(count<number>('id').as('num_toys'))
|
|
89
|
+
* .execute()
|
|
90
|
+
* ```
|
|
91
|
+
*
|
|
92
|
+
* You can limit column range:
|
|
93
|
+
*
|
|
94
|
+
* ```ts
|
|
95
|
+
* db.selectFrom('toy')
|
|
96
|
+
* .select(qb => qb.fn.count<number>('id').as('num_toys'))
|
|
121
97
|
* .execute()
|
|
122
98
|
* ```
|
|
123
99
|
*/
|
|
124
|
-
|
|
125
|
-
return
|
|
100
|
+
count(column) {
|
|
101
|
+
return new AggregateFunctionBuilder({
|
|
102
|
+
aggregateFunctionNode: AggregateFunctionNode.create('count', parseStringReference(column)),
|
|
103
|
+
});
|
|
126
104
|
}
|
|
127
105
|
/**
|
|
128
|
-
* Calls the `
|
|
106
|
+
* Calls the `max` function for the column given as the argument.
|
|
107
|
+
*
|
|
108
|
+
* ### Examples
|
|
109
|
+
*
|
|
110
|
+
* ```ts
|
|
111
|
+
* const { max } = db.fn
|
|
112
|
+
*
|
|
113
|
+
* db.selectFrom('toy')
|
|
114
|
+
* .select(max('price').as('max_price'))
|
|
115
|
+
* .execute()
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
max(column) {
|
|
119
|
+
return new AggregateFunctionBuilder({
|
|
120
|
+
aggregateFunctionNode: AggregateFunctionNode.create('max', parseStringReference(column)),
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Calls the `min` function for the column given as the argument.
|
|
125
|
+
*
|
|
126
|
+
* ### Examples
|
|
127
|
+
*
|
|
128
|
+
* ```ts
|
|
129
|
+
* const { min } = db.fn
|
|
130
|
+
*
|
|
131
|
+
* db.selectFrom('toy')
|
|
132
|
+
* .select(min('price').as('min_price'))
|
|
133
|
+
* .execute()
|
|
134
|
+
* ```
|
|
135
|
+
*
|
|
136
|
+
*/
|
|
137
|
+
min(column) {
|
|
138
|
+
return new AggregateFunctionBuilder({
|
|
139
|
+
aggregateFunctionNode: AggregateFunctionNode.create('min', parseStringReference(column)),
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Calls the `sum` function for the column given as the argument.
|
|
129
144
|
*
|
|
130
145
|
* If this is used in a `select` statement the type of the selected expression
|
|
131
|
-
* will be `number | string |bigint` by default. This is because Kysely can't
|
|
146
|
+
* will be `number | string | bigint` by default. This is because Kysely can't
|
|
132
147
|
* know the type the db driver outputs. Sometimes the output can be larger than
|
|
133
148
|
* the largest javascript number and a string is returned instead. Most drivers
|
|
134
149
|
* allow you to configure the output type of large numbers and Kysely can't
|
|
@@ -138,20 +153,16 @@ export class FunctionBuilder {
|
|
|
138
153
|
* the type as the first type argument:
|
|
139
154
|
*
|
|
140
155
|
* ```ts
|
|
141
|
-
* const {
|
|
156
|
+
* const { sum } = db.fn
|
|
142
157
|
*
|
|
143
158
|
* db.selectFrom('toy')
|
|
144
|
-
* .select(
|
|
159
|
+
* .select(sum<number>('price').as('total_price'))
|
|
145
160
|
* .execute()
|
|
146
161
|
* ```
|
|
147
162
|
*/
|
|
148
|
-
|
|
149
|
-
return
|
|
150
|
-
|
|
151
|
-
#oneArgFunction(fn, column) {
|
|
152
|
-
return new RawBuilder({
|
|
153
|
-
queryId: createQueryId(),
|
|
154
|
-
rawNode: RawNode.create([`${fn}(`, ')'], [parseStringReference(column)]),
|
|
163
|
+
sum(column) {
|
|
164
|
+
return new AggregateFunctionBuilder({
|
|
165
|
+
aggregateFunctionNode: AggregateFunctionNode.create('sum', parseStringReference(column)),
|
|
155
166
|
});
|
|
156
167
|
}
|
|
157
168
|
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { DynamicReferenceBuilder } from '../dynamic/dynamic-reference-builder.js';
|
|
2
|
+
import { OperationNodeSource } from '../operation-node/operation-node-source.js';
|
|
3
|
+
import { OverNode } from '../operation-node/over-node.js';
|
|
4
|
+
import { OrderByDirectionExpression } from '../parser/order-by-parser.js';
|
|
5
|
+
import { PartitionByExpression } from '../parser/partition-by-parser.js';
|
|
6
|
+
import { StringReference } from '../parser/reference-parser.js';
|
|
7
|
+
export declare class OverBuilder<DB, TB extends keyof DB> implements OperationNodeSource {
|
|
8
|
+
#private;
|
|
9
|
+
constructor(props: OverBuilderProps);
|
|
10
|
+
/**
|
|
11
|
+
* Adds an order by clause item inside the over function.
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* const result = await db
|
|
15
|
+
* .selectFrom('person')
|
|
16
|
+
* .select(
|
|
17
|
+
* eb => eb.fn.avg<number>('age').over(
|
|
18
|
+
* ob => ob.orderBy('first_name', 'asc').orderBy('last_name', 'asc')
|
|
19
|
+
* ).as('average_age')
|
|
20
|
+
* )
|
|
21
|
+
* .execute()
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* The generated SQL (PostgreSQL):
|
|
25
|
+
*
|
|
26
|
+
* ```sql
|
|
27
|
+
* select avg("age") over(order by "first_name" asc, "last_name" asc) as "average_age"
|
|
28
|
+
* from "person"
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
orderBy(orderBy: StringReference<DB, TB> | DynamicReferenceBuilder<any>, direction?: OrderByDirectionExpression): OverBuilder<DB, TB>;
|
|
32
|
+
/**
|
|
33
|
+
* Adds partition by clause item/s inside the over function.
|
|
34
|
+
*
|
|
35
|
+
* ```ts
|
|
36
|
+
* const result = await db
|
|
37
|
+
* .selectFrom('person')
|
|
38
|
+
* .select(
|
|
39
|
+
* eb => eb.fn.avg<number>('age').over(
|
|
40
|
+
* ob => ob.partitionBy(['last_name', 'first_name'])
|
|
41
|
+
* ).as('average_age')
|
|
42
|
+
* )
|
|
43
|
+
* .execute()
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* The generated SQL (PostgreSQL):
|
|
47
|
+
*
|
|
48
|
+
* ```sql
|
|
49
|
+
* select avg("age") over(partition by "last_name", "first_name") as "average_age"
|
|
50
|
+
* from "person"
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
partitionBy(partitionBy: ReadonlyArray<PartitionByExpression<DB, TB>>): OverBuilder<DB, TB>;
|
|
54
|
+
partitionBy(partitionBy: PartitionByExpression<DB, TB>): OverBuilder<DB, TB>;
|
|
55
|
+
toOperationNode(): OverNode;
|
|
56
|
+
}
|
|
57
|
+
export interface OverBuilderProps {
|
|
58
|
+
readonly overNode: OverNode;
|
|
59
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/// <reference types="./over-builder.d.ts" />
|
|
2
|
+
import { OverNode } from '../operation-node/over-node.js';
|
|
3
|
+
import { parseOrderBy, } from '../parser/order-by-parser.js';
|
|
4
|
+
import { parsePartitionBy, } from '../parser/partition-by-parser.js';
|
|
5
|
+
import { freeze } from '../util/object-utils.js';
|
|
6
|
+
import { preventAwait } from '../util/prevent-await.js';
|
|
7
|
+
export class OverBuilder {
|
|
8
|
+
#props;
|
|
9
|
+
constructor(props) {
|
|
10
|
+
this.#props = freeze(props);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Adds an order by clause item inside the over function.
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* const result = await db
|
|
17
|
+
* .selectFrom('person')
|
|
18
|
+
* .select(
|
|
19
|
+
* eb => eb.fn.avg<number>('age').over(
|
|
20
|
+
* ob => ob.orderBy('first_name', 'asc').orderBy('last_name', 'asc')
|
|
21
|
+
* ).as('average_age')
|
|
22
|
+
* )
|
|
23
|
+
* .execute()
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* The generated SQL (PostgreSQL):
|
|
27
|
+
*
|
|
28
|
+
* ```sql
|
|
29
|
+
* select avg("age") over(order by "first_name" asc, "last_name" asc) as "average_age"
|
|
30
|
+
* from "person"
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
orderBy(orderBy, direction) {
|
|
34
|
+
return new OverBuilder({
|
|
35
|
+
overNode: OverNode.cloneWithOrderByItem(this.#props.overNode, parseOrderBy(orderBy, direction)),
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
partitionBy(partitionBy) {
|
|
39
|
+
return new OverBuilder({
|
|
40
|
+
overNode: OverNode.cloneWithPartitionByItems(this.#props.overNode, parsePartitionBy(partitionBy)),
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
toOperationNode() {
|
|
44
|
+
return this.#props.overNode;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
preventAwait(OverBuilder, "don't await OverBuilder instances. They are never executed directly and are always just a part of a query.");
|
|
@@ -13,13 +13,13 @@ import { Compilable } from '../util/compilable.js';
|
|
|
13
13
|
import { QueryExecutor } from '../query-executor/query-executor.js';
|
|
14
14
|
import { QueryId } from '../util/query-id.js';
|
|
15
15
|
import { GroupByExpression } from '../parser/group-by-parser.js';
|
|
16
|
-
import { UnionExpression } from '../parser/union-parser.js';
|
|
17
16
|
import { KyselyPlugin } from '../plugin/kysely-plugin.js';
|
|
18
17
|
import { WhereInterface } from './where-interface.js';
|
|
19
18
|
import { NoResultErrorConstructor } from './no-result-error.js';
|
|
20
19
|
import { HavingInterface } from './having-interface.js';
|
|
21
20
|
import { AliasedRawBuilder } from '../raw-builder/raw-builder.js';
|
|
22
21
|
import { Explainable, ExplainFormat } from '../util/explainable.js';
|
|
22
|
+
import { SetOperationExpression } from '../parser/set-operation-parser.js';
|
|
23
23
|
export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements WhereInterface<DB, TB>, HavingInterface<DB, TB>, OperationNodeSource, Compilable, Explainable {
|
|
24
24
|
#private;
|
|
25
25
|
constructor(props: SelectQueryBuilderProps);
|
|
@@ -1202,7 +1202,7 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
1202
1202
|
* .orderBy('name')
|
|
1203
1203
|
* ```
|
|
1204
1204
|
*/
|
|
1205
|
-
union(expression:
|
|
1205
|
+
union(expression: SetOperationExpression<DB, O>): SelectQueryBuilder<DB, TB, O>;
|
|
1206
1206
|
/**
|
|
1207
1207
|
* Combines another select query or raw expression to this query using `union all`.
|
|
1208
1208
|
*
|
|
@@ -1217,7 +1217,67 @@ export declare class SelectQueryBuilder<DB, TB extends keyof DB, O> implements W
|
|
|
1217
1217
|
* .orderBy('name')
|
|
1218
1218
|
* ```
|
|
1219
1219
|
*/
|
|
1220
|
-
unionAll(expression:
|
|
1220
|
+
unionAll(expression: SetOperationExpression<DB, O>): SelectQueryBuilder<DB, TB, O>;
|
|
1221
|
+
/**
|
|
1222
|
+
* Combines another select query or raw expression to this query using `intersect`.
|
|
1223
|
+
*
|
|
1224
|
+
* The output row type of the combined query must match `this` query.
|
|
1225
|
+
*
|
|
1226
|
+
* ### Examples
|
|
1227
|
+
*
|
|
1228
|
+
* ```ts
|
|
1229
|
+
* db.selectFrom('person')
|
|
1230
|
+
* .select(['id', 'first_name as name'])
|
|
1231
|
+
* .intersect(db.selectFrom('pet').select(['id', 'name']))
|
|
1232
|
+
* .orderBy('name')
|
|
1233
|
+
* ```
|
|
1234
|
+
*/
|
|
1235
|
+
intersect(expression: SetOperationExpression<DB, O>): SelectQueryBuilder<DB, TB, O>;
|
|
1236
|
+
/**
|
|
1237
|
+
* Combines another select query or raw expression to this query using `intersect all`.
|
|
1238
|
+
*
|
|
1239
|
+
* The output row type of the combined query must match `this` query.
|
|
1240
|
+
*
|
|
1241
|
+
* ### Examples
|
|
1242
|
+
*
|
|
1243
|
+
* ```ts
|
|
1244
|
+
* db.selectFrom('person')
|
|
1245
|
+
* .select(['id', 'first_name as name'])
|
|
1246
|
+
* .intersectAll(db.selectFrom('pet').select(['id', 'name']))
|
|
1247
|
+
* .orderBy('name')
|
|
1248
|
+
* ```
|
|
1249
|
+
*/
|
|
1250
|
+
intersectAll(expression: SetOperationExpression<DB, O>): SelectQueryBuilder<DB, TB, O>;
|
|
1251
|
+
/**
|
|
1252
|
+
* Combines another select query or raw expression to this query using `except`.
|
|
1253
|
+
*
|
|
1254
|
+
* The output row type of the combined query must match `this` query.
|
|
1255
|
+
*
|
|
1256
|
+
* ### Examples
|
|
1257
|
+
*
|
|
1258
|
+
* ```ts
|
|
1259
|
+
* db.selectFrom('person')
|
|
1260
|
+
* .select(['id', 'first_name as name'])
|
|
1261
|
+
* .except(db.selectFrom('pet').select(['id', 'name']))
|
|
1262
|
+
* .orderBy('name')
|
|
1263
|
+
* ```
|
|
1264
|
+
*/
|
|
1265
|
+
except(expression: SetOperationExpression<DB, O>): SelectQueryBuilder<DB, TB, O>;
|
|
1266
|
+
/**
|
|
1267
|
+
* Combines another select query or raw expression to this query using `except all`.
|
|
1268
|
+
*
|
|
1269
|
+
* The output row type of the combined query must match `this` query.
|
|
1270
|
+
*
|
|
1271
|
+
* ### Examples
|
|
1272
|
+
*
|
|
1273
|
+
* ```ts
|
|
1274
|
+
* db.selectFrom('person')
|
|
1275
|
+
* .select(['id', 'first_name as name'])
|
|
1276
|
+
* .exceptAll(db.selectFrom('pet').select(['id', 'name']))
|
|
1277
|
+
* .orderBy('name')
|
|
1278
|
+
* ```
|
|
1279
|
+
*/
|
|
1280
|
+
exceptAll(expression: SetOperationExpression<DB, O>): SelectQueryBuilder<DB, TB, O>;
|
|
1221
1281
|
/**
|
|
1222
1282
|
* Simply calls the given function passing `this` as the only argument.
|
|
1223
1283
|
*
|
|
@@ -12,10 +12,10 @@ import { LimitNode } from '../operation-node/limit-node.js';
|
|
|
12
12
|
import { OffsetNode } from '../operation-node/offset-node.js';
|
|
13
13
|
import { freeze } from '../util/object-utils.js';
|
|
14
14
|
import { parseGroupBy, } from '../parser/group-by-parser.js';
|
|
15
|
-
import { parseUnion } from '../parser/union-parser.js';
|
|
16
15
|
import { NoResultError } from './no-result-error.js';
|
|
17
16
|
import { IdentifierNode } from '../operation-node/identifier-node.js';
|
|
18
17
|
import { ExplainNode } from '../operation-node/explain-node.js';
|
|
18
|
+
import { parseSetOperation, } from '../parser/set-operation-parser.js';
|
|
19
19
|
export class SelectQueryBuilder {
|
|
20
20
|
#props;
|
|
21
21
|
constructor(props) {
|
|
@@ -470,7 +470,7 @@ export class SelectQueryBuilder {
|
|
|
470
470
|
union(expression) {
|
|
471
471
|
return new SelectQueryBuilder({
|
|
472
472
|
...this.#props,
|
|
473
|
-
queryNode: SelectQueryNode.
|
|
473
|
+
queryNode: SelectQueryNode.cloneWithSetOperation(this.#props.queryNode, parseSetOperation('union', expression, false)),
|
|
474
474
|
});
|
|
475
475
|
}
|
|
476
476
|
/**
|
|
@@ -490,7 +490,87 @@ export class SelectQueryBuilder {
|
|
|
490
490
|
unionAll(expression) {
|
|
491
491
|
return new SelectQueryBuilder({
|
|
492
492
|
...this.#props,
|
|
493
|
-
queryNode: SelectQueryNode.
|
|
493
|
+
queryNode: SelectQueryNode.cloneWithSetOperation(this.#props.queryNode, parseSetOperation('union', expression, true)),
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
/**
|
|
497
|
+
* Combines another select query or raw expression to this query using `intersect`.
|
|
498
|
+
*
|
|
499
|
+
* The output row type of the combined query must match `this` query.
|
|
500
|
+
*
|
|
501
|
+
* ### Examples
|
|
502
|
+
*
|
|
503
|
+
* ```ts
|
|
504
|
+
* db.selectFrom('person')
|
|
505
|
+
* .select(['id', 'first_name as name'])
|
|
506
|
+
* .intersect(db.selectFrom('pet').select(['id', 'name']))
|
|
507
|
+
* .orderBy('name')
|
|
508
|
+
* ```
|
|
509
|
+
*/
|
|
510
|
+
intersect(expression) {
|
|
511
|
+
return new SelectQueryBuilder({
|
|
512
|
+
...this.#props,
|
|
513
|
+
queryNode: SelectQueryNode.cloneWithSetOperation(this.#props.queryNode, parseSetOperation('intersect', expression, false)),
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
/**
|
|
517
|
+
* Combines another select query or raw expression to this query using `intersect all`.
|
|
518
|
+
*
|
|
519
|
+
* The output row type of the combined query must match `this` query.
|
|
520
|
+
*
|
|
521
|
+
* ### Examples
|
|
522
|
+
*
|
|
523
|
+
* ```ts
|
|
524
|
+
* db.selectFrom('person')
|
|
525
|
+
* .select(['id', 'first_name as name'])
|
|
526
|
+
* .intersectAll(db.selectFrom('pet').select(['id', 'name']))
|
|
527
|
+
* .orderBy('name')
|
|
528
|
+
* ```
|
|
529
|
+
*/
|
|
530
|
+
intersectAll(expression) {
|
|
531
|
+
return new SelectQueryBuilder({
|
|
532
|
+
...this.#props,
|
|
533
|
+
queryNode: SelectQueryNode.cloneWithSetOperation(this.#props.queryNode, parseSetOperation('intersect', expression, true)),
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
/**
|
|
537
|
+
* Combines another select query or raw expression to this query using `except`.
|
|
538
|
+
*
|
|
539
|
+
* The output row type of the combined query must match `this` query.
|
|
540
|
+
*
|
|
541
|
+
* ### Examples
|
|
542
|
+
*
|
|
543
|
+
* ```ts
|
|
544
|
+
* db.selectFrom('person')
|
|
545
|
+
* .select(['id', 'first_name as name'])
|
|
546
|
+
* .except(db.selectFrom('pet').select(['id', 'name']))
|
|
547
|
+
* .orderBy('name')
|
|
548
|
+
* ```
|
|
549
|
+
*/
|
|
550
|
+
except(expression) {
|
|
551
|
+
return new SelectQueryBuilder({
|
|
552
|
+
...this.#props,
|
|
553
|
+
queryNode: SelectQueryNode.cloneWithSetOperation(this.#props.queryNode, parseSetOperation('except', expression, false)),
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
/**
|
|
557
|
+
* Combines another select query or raw expression to this query using `except all`.
|
|
558
|
+
*
|
|
559
|
+
* The output row type of the combined query must match `this` query.
|
|
560
|
+
*
|
|
561
|
+
* ### Examples
|
|
562
|
+
*
|
|
563
|
+
* ```ts
|
|
564
|
+
* db.selectFrom('person')
|
|
565
|
+
* .select(['id', 'first_name as name'])
|
|
566
|
+
* .exceptAll(db.selectFrom('pet').select(['id', 'name']))
|
|
567
|
+
* .orderBy('name')
|
|
568
|
+
* ```
|
|
569
|
+
*/
|
|
570
|
+
exceptAll(expression) {
|
|
571
|
+
return new SelectQueryBuilder({
|
|
572
|
+
...this.#props,
|
|
573
|
+
queryNode: SelectQueryNode.cloneWithSetOperation(this.#props.queryNode, parseSetOperation('except', expression, true)),
|
|
494
574
|
});
|
|
495
575
|
}
|
|
496
576
|
/**
|
|
@@ -60,7 +60,6 @@ import { ColumnDefinitionNode } from '../operation-node/column-definition-node.j
|
|
|
60
60
|
import { ModifyColumnNode } from '../operation-node/modify-column-node.js';
|
|
61
61
|
import { OnDuplicateKeyNode } from '../operation-node/on-duplicate-key-node.js';
|
|
62
62
|
import { ColumnNode } from '../operation-node/column-node.js';
|
|
63
|
-
import { UnionNode } from '../operation-node/union-node.js';
|
|
64
63
|
import { CreateViewNode } from '../operation-node/create-view-node.js';
|
|
65
64
|
import { DropViewNode } from '../operation-node/drop-view-node.js';
|
|
66
65
|
import { GeneratedNode } from '../operation-node/generated-node.js';
|
|
@@ -73,6 +72,12 @@ import { CreateTypeNode } from '../operation-node/create-type-node.js';
|
|
|
73
72
|
import { DropTypeNode } from '../operation-node/drop-type-node.js';
|
|
74
73
|
import { ExplainNode } from '../operation-node/explain-node.js';
|
|
75
74
|
import { SchemableIdentifierNode } from '../operation-node/schemable-identifier-node.js';
|
|
75
|
+
import { DefaultInsertValueNode } from '../operation-node/default-insert-value-node.js';
|
|
76
|
+
import { AggregateFunctionNode } from '../operation-node/aggregate-function-node.js';
|
|
77
|
+
import { OverNode } from '../operation-node/over-node.js';
|
|
78
|
+
import { PartitionByNode } from '../operation-node/partition-by-node.js';
|
|
79
|
+
import { PartitionByItemNode } from '../operation-node/partition-by-item-node.js';
|
|
80
|
+
import { SetOperationNode } from '../operation-node/set-operation-node.js';
|
|
76
81
|
export declare class DefaultQueryCompiler extends OperationNodeVisitor implements QueryCompiler {
|
|
77
82
|
#private;
|
|
78
83
|
protected get numParameters(): number;
|
|
@@ -144,7 +149,7 @@ export declare class DefaultQueryCompiler extends OperationNodeVisitor implement
|
|
|
144
149
|
protected visitModifyColumn(node: ModifyColumnNode): void;
|
|
145
150
|
protected visitAddConstraint(node: AddConstraintNode): void;
|
|
146
151
|
protected visitDropConstraint(node: DropConstraintNode): void;
|
|
147
|
-
protected
|
|
152
|
+
protected visitSetOperation(node: SetOperationNode): void;
|
|
148
153
|
protected visitCreateView(node: CreateViewNode): void;
|
|
149
154
|
protected visitDropView(node: DropViewNode): void;
|
|
150
155
|
protected visitGenerated(node: GeneratedNode): void;
|
|
@@ -153,6 +158,11 @@ export declare class DefaultQueryCompiler extends OperationNodeVisitor implement
|
|
|
153
158
|
protected visitCreateType(node: CreateTypeNode): void;
|
|
154
159
|
protected visitDropType(node: DropTypeNode): void;
|
|
155
160
|
protected visitExplain(node: ExplainNode): void;
|
|
161
|
+
protected visitDefaultInsertValue(_: DefaultInsertValueNode): void;
|
|
162
|
+
protected visitAggregateFunction(node: AggregateFunctionNode): void;
|
|
163
|
+
protected visitOver(node: OverNode): void;
|
|
164
|
+
protected visitPartitionBy(node: PartitionByNode): void;
|
|
165
|
+
protected visitPartitionByItem(node: PartitionByItemNode): void;
|
|
156
166
|
protected append(str: string): void;
|
|
157
167
|
protected appendValue(parameter: unknown): void;
|
|
158
168
|
protected getLeftIdentifierWrapper(): string;
|
|
@@ -2,10 +2,9 @@
|
|
|
2
2
|
import { InsertQueryNode } from '../operation-node/insert-query-node.js';
|
|
3
3
|
import { OperationNodeVisitor } from '../operation-node/operation-node-visitor.js';
|
|
4
4
|
import { QueryNode } from '../operation-node/query-node.js';
|
|
5
|
-
import { RawNode } from '../operation-node/raw-node.js';
|
|
6
5
|
import { getLast, freeze, isString, isNumber, isBoolean, isNull, isDate, isBigInt, } from '../util/object-utils.js';
|
|
7
|
-
import { UnionNode } from '../operation-node/union-node.js';
|
|
8
6
|
import { CreateViewNode } from '../operation-node/create-view-node.js';
|
|
7
|
+
import { SetOperationNode } from '../operation-node/set-operation-node.js';
|
|
9
8
|
export class DefaultQueryCompiler extends OperationNodeVisitor {
|
|
10
9
|
#sql = '';
|
|
11
10
|
#parameters = [];
|
|
@@ -29,7 +28,7 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
|
|
|
29
28
|
const wrapInParens = this.parentNode !== undefined &&
|
|
30
29
|
!InsertQueryNode.is(this.parentNode) &&
|
|
31
30
|
!CreateViewNode.is(this.parentNode) &&
|
|
32
|
-
!
|
|
31
|
+
!SetOperationNode.is(this.parentNode);
|
|
33
32
|
if (this.parentNode === undefined && node.explain) {
|
|
34
33
|
this.visitNode(node.explain);
|
|
35
34
|
this.append(' ');
|
|
@@ -71,9 +70,9 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
|
|
|
71
70
|
this.append(' ');
|
|
72
71
|
this.visitNode(node.having);
|
|
73
72
|
}
|
|
74
|
-
if (node.
|
|
73
|
+
if (node.setOperations) {
|
|
75
74
|
this.append(' ');
|
|
76
|
-
this.compileList(node.
|
|
75
|
+
this.compileList(node.setOperations, ' ');
|
|
77
76
|
}
|
|
78
77
|
if (node.orderBy) {
|
|
79
78
|
this.append(' ');
|
|
@@ -542,13 +541,7 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
|
|
|
542
541
|
}
|
|
543
542
|
if (node.expression) {
|
|
544
543
|
this.append(' (');
|
|
545
|
-
if (RawNode.is(node.expression)) {
|
|
546
|
-
this.append('(');
|
|
547
|
-
}
|
|
548
544
|
this.visitNode(node.expression);
|
|
549
|
-
if (RawNode.is(node.expression)) {
|
|
550
|
-
this.append(')');
|
|
551
|
-
}
|
|
552
545
|
this.append(')');
|
|
553
546
|
}
|
|
554
547
|
}
|
|
@@ -750,12 +743,13 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
|
|
|
750
743
|
this.append(' restrict');
|
|
751
744
|
}
|
|
752
745
|
}
|
|
753
|
-
|
|
754
|
-
this.append(
|
|
746
|
+
visitSetOperation(node) {
|
|
747
|
+
this.append(node.operator);
|
|
748
|
+
this.append(' ');
|
|
755
749
|
if (node.all) {
|
|
756
750
|
this.append('all ');
|
|
757
751
|
}
|
|
758
|
-
this.visitNode(node.
|
|
752
|
+
this.visitNode(node.expression);
|
|
759
753
|
}
|
|
760
754
|
visitCreateView(node) {
|
|
761
755
|
this.append('create ');
|
|
@@ -865,6 +859,42 @@ export class DefaultQueryCompiler extends OperationNodeVisitor {
|
|
|
865
859
|
this.append(this.getRightExplainOptionsWrapper());
|
|
866
860
|
}
|
|
867
861
|
}
|
|
862
|
+
visitDefaultInsertValue(_) {
|
|
863
|
+
this.append('default');
|
|
864
|
+
}
|
|
865
|
+
visitAggregateFunction(node) {
|
|
866
|
+
this.append(node.func);
|
|
867
|
+
this.append('(');
|
|
868
|
+
if (node.distinct) {
|
|
869
|
+
this.append('distinct ');
|
|
870
|
+
}
|
|
871
|
+
this.visitNode(node.column);
|
|
872
|
+
this.append(')');
|
|
873
|
+
if (node.over) {
|
|
874
|
+
this.append(' ');
|
|
875
|
+
this.visitNode(node.over);
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
visitOver(node) {
|
|
879
|
+
this.append('over(');
|
|
880
|
+
if (node.partitionBy) {
|
|
881
|
+
this.visitNode(node.partitionBy);
|
|
882
|
+
if (node.orderBy) {
|
|
883
|
+
this.append(' ');
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
if (node.orderBy) {
|
|
887
|
+
this.visitNode(node.orderBy);
|
|
888
|
+
}
|
|
889
|
+
this.append(')');
|
|
890
|
+
}
|
|
891
|
+
visitPartitionBy(node) {
|
|
892
|
+
this.append('partition by ');
|
|
893
|
+
this.compileList(node.items);
|
|
894
|
+
}
|
|
895
|
+
visitPartitionByItem(node) {
|
|
896
|
+
this.visitNode(node.partitionBy);
|
|
897
|
+
}
|
|
868
898
|
append(str) {
|
|
869
899
|
this.#sql += str;
|
|
870
900
|
}
|
|
@@ -7,13 +7,14 @@ export interface Sql {
|
|
|
7
7
|
* import { sql } from 'kysely'
|
|
8
8
|
*
|
|
9
9
|
* const id = 123
|
|
10
|
-
*
|
|
11
10
|
* const snippet = sql<Person[]>`select * from person where id = ${id}`
|
|
12
11
|
* ```
|
|
13
12
|
*
|
|
14
13
|
* Substitutions (the things inside `${}`) are automatically passed to the database
|
|
15
|
-
* as parameters and are
|
|
16
|
-
* about SQL injection vulnerabilities.
|
|
14
|
+
* as parameters and are never interpolated to the SQL string. There's no need to worry
|
|
15
|
+
* about SQL injection vulnerabilities. Substitutions can be values, other `sql`
|
|
16
|
+
* expressions, queries and almost anything else Kysely can produce and they get
|
|
17
|
+
* handled correctly. See the examples below.
|
|
17
18
|
*
|
|
18
19
|
* If you need your substitutions to be interpreted as identifiers, value literals or
|
|
19
20
|
* lists of things, see the {@link Sql.ref}, {@link Sql.table}, {@link Sql.id},
|
|
@@ -29,8 +30,7 @@ export interface Sql {
|
|
|
29
30
|
* .selectFrom('person')
|
|
30
31
|
* .select(
|
|
31
32
|
* // If you use `sql` in a select statement, remember to call the `as`
|
|
32
|
-
* // method to give it an alias.
|
|
33
|
-
* // the column to the result type.
|
|
33
|
+
* // method to give it an alias.
|
|
34
34
|
* sql<string>`concat(first_name, ' ', last_name)`.as('full_name')
|
|
35
35
|
* )
|
|
36
36
|
* .where(sql`birthdate between ${date1} and ${date2}`)
|
|
@@ -97,6 +97,14 @@ export interface Sql {
|
|
|
97
97
|
* type-safety though... But it's possible.
|
|
98
98
|
*/
|
|
99
99
|
<T = unknown>(sqlFragments: TemplateStringsArray, ...parameters: unknown[]): RawBuilder<T>;
|
|
100
|
+
/**
|
|
101
|
+
* `sql.value(value)` is a shortcut for:
|
|
102
|
+
*
|
|
103
|
+
* ```ts
|
|
104
|
+
* sql`${value}`
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
value(value: unknown): RawBuilder<unknown>;
|
|
100
108
|
/**
|
|
101
109
|
* This can be used to add runtime column references to SQL snippets.
|
|
102
110
|
*
|