sumak 0.0.7 → 0.0.8
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 +134 -121
- package/dist/builder/eb.d.mts +0 -1
- package/dist/builder/eb.mjs +1 -5
- package/dist/builder/typed-delete.d.mts +2 -0
- package/dist/builder/typed-delete.mjs +11 -2
- package/dist/builder/typed-insert.d.mts +3 -6
- package/dist/builder/typed-insert.mjs +30 -29
- package/dist/builder/typed-merge.d.mts +1 -2
- package/dist/builder/typed-merge.mjs +9 -22
- package/dist/builder/typed-select.d.mts +5 -2
- package/dist/builder/typed-select.mjs +58 -52
- package/dist/builder/typed-update.d.mts +3 -2
- package/dist/builder/typed-update.mjs +22 -18
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +1 -1
- package/dist/sumak.mjs +10 -4
- package/package.json +1 -1
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { star } from "../ast/expression.mjs";
|
|
2
2
|
import { unwrap } from "../ast/typed-expression.mjs";
|
|
3
3
|
import { DeleteBuilder } from "./delete.mjs";
|
|
4
|
-
import { createColumnProxies
|
|
4
|
+
import { createColumnProxies } from "./eb.mjs";
|
|
5
5
|
|
|
6
6
|
export class TypedDeleteBuilder {
|
|
7
7
|
|
|
8
8
|
_builder;
|
|
9
|
+
|
|
10
|
+
_printer;
|
|
9
11
|
constructor(table) {
|
|
10
12
|
this._builder = new DeleteBuilder().from(table);
|
|
11
13
|
}
|
|
@@ -13,12 +15,12 @@ export class TypedDeleteBuilder {
|
|
|
13
15
|
_with(builder) {
|
|
14
16
|
const t = new TypedDeleteBuilder("");
|
|
15
17
|
t._builder = builder;
|
|
18
|
+
t._printer = this._printer;
|
|
16
19
|
return t;
|
|
17
20
|
}
|
|
18
21
|
|
|
19
22
|
where(exprOrCallback) {
|
|
20
23
|
if (typeof exprOrCallback === "function") {
|
|
21
|
-
resetParams();
|
|
22
24
|
const table = this._builder.build().table.name;
|
|
23
25
|
const cols = createColumnProxies(table);
|
|
24
26
|
const result = exprOrCallback(cols);
|
|
@@ -74,6 +76,13 @@ export class TypedDeleteBuilder {
|
|
|
74
76
|
return printer.print(this.build());
|
|
75
77
|
}
|
|
76
78
|
|
|
79
|
+
toSQL() {
|
|
80
|
+
if (!this._printer) {
|
|
81
|
+
throw new Error("toSQL() requires a printer. Use db.deleteFrom() or pass a printer to compile().");
|
|
82
|
+
}
|
|
83
|
+
return this._printer.print(this.build());
|
|
84
|
+
}
|
|
85
|
+
|
|
77
86
|
explain(options) {
|
|
78
87
|
const node = this.build();
|
|
79
88
|
const explainNode = {
|
|
@@ -11,8 +11,7 @@ export declare class TypedInsertBuilder<
|
|
|
11
11
|
DB,
|
|
12
12
|
TB extends keyof DB
|
|
13
13
|
> {
|
|
14
|
-
|
|
15
|
-
constructor(table: TB & string, paramIdx?: number);
|
|
14
|
+
constructor(table: TB & string);
|
|
16
15
|
/**
|
|
17
16
|
* Insert a single row. Columns/values inferred from Insertable<DB[TB]>.
|
|
18
17
|
*/
|
|
@@ -46,10 +45,6 @@ export declare class TypedInsertBuilder<
|
|
|
46
45
|
}[]): TypedInsertBuilder<DB, TB>;
|
|
47
46
|
/**
|
|
48
47
|
* ON CONFLICT DO UPDATE — with plain object (auto-parameterized).
|
|
49
|
-
*
|
|
50
|
-
* ```ts
|
|
51
|
-
* .onConflictDoUpdateSet(["email"], { name: "Alice Updated" })
|
|
52
|
-
* ```
|
|
53
48
|
*/
|
|
54
49
|
onConflictDoUpdateSet(columns: (keyof DB[TB] & string)[], values: Partial<Insertable<DB[TB]>>): TypedInsertBuilder<DB, TB>;
|
|
55
50
|
/** ON CONFLICT ON CONSTRAINT name DO NOTHING */
|
|
@@ -78,6 +73,8 @@ export declare class TypedInsertBuilder<
|
|
|
78
73
|
$if(condition: boolean, fn: (qb: TypedInsertBuilder<DB, TB>) => TypedInsertBuilder<DB, TB>): TypedInsertBuilder<DB, TB>;
|
|
79
74
|
build(): InsertNode;
|
|
80
75
|
compile(printer: Printer): CompiledQuery;
|
|
76
|
+
/** Compile to SQL using the dialect's printer. */
|
|
77
|
+
toSQL(): CompiledQuery;
|
|
81
78
|
/** EXPLAIN this query. */
|
|
82
79
|
explain(options?: {
|
|
83
80
|
analyze?: boolean;
|
|
@@ -6,27 +6,23 @@ import { InsertBuilder } from "./insert.mjs";
|
|
|
6
6
|
export class TypedInsertBuilder {
|
|
7
7
|
|
|
8
8
|
_builder;
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
|
|
10
|
+
_printer;
|
|
11
|
+
constructor(table) {
|
|
11
12
|
this._builder = new InsertBuilder().into(table);
|
|
12
|
-
this._paramIdx = paramIdx;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
_withBuilder(builder
|
|
15
|
+
_withBuilder(builder) {
|
|
16
16
|
const t = new TypedInsertBuilder("");
|
|
17
17
|
t._builder = builder;
|
|
18
|
-
t.
|
|
18
|
+
t._printer = this._printer;
|
|
19
19
|
return t;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
values(row) {
|
|
23
23
|
const entries = Object.entries(row);
|
|
24
24
|
const cols = entries.map(([k]) => k);
|
|
25
|
-
const vals = entries.map(([_, v]) =>
|
|
26
|
-
const p = param(this._paramIdx, v);
|
|
27
|
-
this._paramIdx++;
|
|
28
|
-
return p;
|
|
29
|
-
});
|
|
25
|
+
const vals = entries.map(([_, v]) => param(0, v));
|
|
30
26
|
let builder = this._builder;
|
|
31
27
|
|
|
32
28
|
if (builder.build().columns.length === 0) {
|
|
@@ -35,8 +31,8 @@ export class TypedInsertBuilder {
|
|
|
35
31
|
builder = new InsertBuilder({
|
|
36
32
|
...builder.build(),
|
|
37
33
|
values: [...builder.build().values, vals]
|
|
38
|
-
}
|
|
39
|
-
return this._withBuilder(builder
|
|
34
|
+
});
|
|
35
|
+
return this._withBuilder(builder);
|
|
40
36
|
}
|
|
41
37
|
|
|
42
38
|
valuesMany(rows) {
|
|
@@ -55,7 +51,7 @@ export class TypedInsertBuilder {
|
|
|
55
51
|
const builder = new InsertBuilder({
|
|
56
52
|
...this._builder.build(),
|
|
57
53
|
returning: exprs
|
|
58
|
-
}
|
|
54
|
+
});
|
|
59
55
|
return new TypedInsertReturningBuilder(builder);
|
|
60
56
|
}
|
|
61
57
|
|
|
@@ -69,7 +65,7 @@ export class TypedInsertBuilder {
|
|
|
69
65
|
const builder = new InsertBuilder({
|
|
70
66
|
...this._builder.build(),
|
|
71
67
|
returning: [...this._builder.build().returning, aliased]
|
|
72
|
-
}
|
|
68
|
+
});
|
|
73
69
|
return new TypedInsertReturningBuilder(builder);
|
|
74
70
|
}
|
|
75
71
|
|
|
@@ -77,72 +73,70 @@ export class TypedInsertBuilder {
|
|
|
77
73
|
const builder = new InsertBuilder({
|
|
78
74
|
...this._builder.build(),
|
|
79
75
|
returning: [star()]
|
|
80
|
-
}
|
|
76
|
+
});
|
|
81
77
|
return new TypedInsertReturningBuilder(builder);
|
|
82
78
|
}
|
|
83
79
|
|
|
84
80
|
onConflictDoNothing(...columns) {
|
|
85
|
-
return this._withBuilder(this._builder.onConflictDoNothing(...columns)
|
|
81
|
+
return this._withBuilder(this._builder.onConflictDoNothing(...columns));
|
|
86
82
|
}
|
|
87
83
|
|
|
88
84
|
onConflictDoUpdate(columns, set) {
|
|
89
85
|
return this._withBuilder(this._builder.onConflictDoUpdate(columns, set.map((s) => ({
|
|
90
86
|
column: s.column,
|
|
91
87
|
value: unwrap(s.value)
|
|
92
|
-
})))
|
|
88
|
+
}))));
|
|
93
89
|
}
|
|
94
90
|
|
|
95
91
|
onConflictDoUpdateSet(columns, values) {
|
|
96
92
|
const set = [];
|
|
97
|
-
let idx = this._paramIdx;
|
|
98
93
|
for (const [col, val] of Object.entries(values)) {
|
|
99
94
|
if (val !== undefined) {
|
|
100
95
|
set.push({
|
|
101
96
|
column: col,
|
|
102
|
-
value: param(
|
|
97
|
+
value: param(0, val)
|
|
103
98
|
});
|
|
104
|
-
idx++;
|
|
105
99
|
}
|
|
106
100
|
}
|
|
107
|
-
return this._withBuilder(this._builder.onConflictDoUpdate(columns, set)
|
|
101
|
+
return this._withBuilder(this._builder.onConflictDoUpdate(columns, set));
|
|
108
102
|
}
|
|
109
103
|
|
|
110
104
|
onConflictConstraintDoNothing(constraint) {
|
|
111
|
-
return this._withBuilder(this._builder.onConflictConstraintDoNothing(constraint)
|
|
105
|
+
return this._withBuilder(this._builder.onConflictConstraintDoNothing(constraint));
|
|
112
106
|
}
|
|
113
107
|
|
|
114
108
|
onConflictConstraintDoUpdate(constraint, set) {
|
|
115
109
|
return this._withBuilder(this._builder.onConflictConstraintDoUpdate(constraint, set.map((s) => ({
|
|
116
110
|
column: s.column,
|
|
117
111
|
value: unwrap(s.value)
|
|
118
|
-
})))
|
|
112
|
+
}))));
|
|
119
113
|
}
|
|
120
114
|
|
|
121
115
|
orIgnore() {
|
|
122
|
-
return this._withBuilder(this._builder.orIgnore()
|
|
116
|
+
return this._withBuilder(this._builder.orIgnore());
|
|
123
117
|
}
|
|
124
118
|
|
|
125
119
|
orReplace() {
|
|
126
|
-
return this._withBuilder(this._builder.orReplace()
|
|
120
|
+
return this._withBuilder(this._builder.orReplace());
|
|
127
121
|
}
|
|
128
122
|
|
|
129
123
|
onDuplicateKeyUpdate(set) {
|
|
130
124
|
return this._withBuilder(this._builder.onDuplicateKeyUpdate(set.map((s) => ({
|
|
131
125
|
column: s.column,
|
|
132
126
|
value: unwrap(s.value)
|
|
133
|
-
})))
|
|
127
|
+
}))));
|
|
134
128
|
}
|
|
135
129
|
|
|
136
130
|
fromSelect(query) {
|
|
137
|
-
return this._withBuilder(this._builder.fromSelect(query)
|
|
131
|
+
return this._withBuilder(this._builder.fromSelect(query));
|
|
138
132
|
}
|
|
139
133
|
|
|
140
134
|
defaultValues() {
|
|
141
|
-
return this._withBuilder(this._builder.defaultValues()
|
|
135
|
+
return this._withBuilder(this._builder.defaultValues());
|
|
142
136
|
}
|
|
143
137
|
|
|
144
138
|
with(name, query, recursive = false) {
|
|
145
|
-
return this._withBuilder(this._builder.with(name, query, recursive)
|
|
139
|
+
return this._withBuilder(this._builder.with(name, query, recursive));
|
|
146
140
|
}
|
|
147
141
|
|
|
148
142
|
$if(condition, fn) {
|
|
@@ -158,6 +152,13 @@ export class TypedInsertBuilder {
|
|
|
158
152
|
return printer.print(this.build());
|
|
159
153
|
}
|
|
160
154
|
|
|
155
|
+
toSQL() {
|
|
156
|
+
if (!this._printer) {
|
|
157
|
+
throw new Error("toSQL() requires a printer. Use db.insertInto() or pass a printer to compile().");
|
|
158
|
+
}
|
|
159
|
+
return this._printer.print(this.build());
|
|
160
|
+
}
|
|
161
|
+
|
|
161
162
|
explain(options) {
|
|
162
163
|
const node = this.build();
|
|
163
164
|
const explainNode = {
|
|
@@ -19,8 +19,7 @@ export declare class TypedMergeBuilder<
|
|
|
19
19
|
> {
|
|
20
20
|
private _targetTable;
|
|
21
21
|
private _sourceAlias;
|
|
22
|
-
|
|
23
|
-
constructor(targetTable: Target & string, sourceTable: Source & string, sourceAlias: string, on: Expression<boolean>, paramIdx?: number);
|
|
22
|
+
constructor(targetTable: Target & string, sourceTable: Source & string, sourceAlias: string, on: Expression<boolean>);
|
|
24
23
|
whenMatchedThenUpdate(values: Updateable<DB[Target]>, condition?: (proxies: MergeProxies<DB, Target, Source>) => Expression<boolean>): TypedMergeBuilder<DB, Target, Source>;
|
|
25
24
|
whenMatchedThenDelete(condition?: (proxies: MergeProxies<DB, Target, Source>) => Expression<boolean>): TypedMergeBuilder<DB, Target, Source>;
|
|
26
25
|
whenNotMatchedThenInsert(row: Insertable<DB[Target]>, condition?: (proxies: MergeProxies<DB, Target, Source>) => Expression<boolean>): TypedMergeBuilder<DB, Target, Source>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { param } from "../ast/expression.mjs";
|
|
2
2
|
import { unwrap } from "../ast/typed-expression.mjs";
|
|
3
|
-
import { Col
|
|
3
|
+
import { Col } from "./eb.mjs";
|
|
4
4
|
import { MergeBuilder } from "./merge.mjs";
|
|
5
5
|
function createMergeProxies(targetTable, sourceAlias) {
|
|
6
6
|
const makeProxy = (prefix) => new Proxy({}, { get(_t, colName) {
|
|
@@ -16,15 +16,13 @@ export class TypedMergeBuilder {
|
|
|
16
16
|
_builder;
|
|
17
17
|
_targetTable;
|
|
18
18
|
_sourceAlias;
|
|
19
|
-
|
|
20
|
-
constructor(targetTable, sourceTable, sourceAlias, on, paramIdx = 0) {
|
|
19
|
+
constructor(targetTable, sourceTable, sourceAlias, on) {
|
|
21
20
|
this._targetTable = targetTable;
|
|
22
21
|
this._sourceAlias = sourceAlias;
|
|
23
|
-
this._paramIdx = paramIdx;
|
|
24
22
|
this._builder = new MergeBuilder().into(targetTable).using(sourceTable, sourceAlias).on(unwrap(on));
|
|
25
23
|
}
|
|
26
24
|
|
|
27
|
-
_with(builder
|
|
25
|
+
_with(builder) {
|
|
28
26
|
const t = new TypedMergeBuilder("", "", "", { node: {
|
|
29
27
|
type: "literal",
|
|
30
28
|
value: true
|
|
@@ -32,57 +30,46 @@ export class TypedMergeBuilder {
|
|
|
32
30
|
t._builder = builder;
|
|
33
31
|
t._targetTable = this._targetTable;
|
|
34
32
|
t._sourceAlias = this._sourceAlias;
|
|
35
|
-
t._paramIdx = paramIdx;
|
|
36
33
|
return t;
|
|
37
34
|
}
|
|
38
35
|
whenMatchedThenUpdate(values, condition) {
|
|
39
|
-
let idx = this._paramIdx;
|
|
40
36
|
const set = [];
|
|
41
37
|
for (const [col, val] of Object.entries(values)) {
|
|
42
38
|
if (val !== undefined) {
|
|
43
39
|
set.push({
|
|
44
40
|
column: col,
|
|
45
|
-
value: param(
|
|
41
|
+
value: param(0, val)
|
|
46
42
|
});
|
|
47
|
-
idx++;
|
|
48
43
|
}
|
|
49
44
|
}
|
|
50
45
|
let condExpr;
|
|
51
46
|
if (condition) {
|
|
52
|
-
resetParams();
|
|
53
47
|
const proxies = createMergeProxies(this._targetTable, this._sourceAlias);
|
|
54
48
|
condExpr = unwrap(condition(proxies));
|
|
55
49
|
}
|
|
56
|
-
return this._with(this._builder.whenMatchedUpdate(set, condExpr)
|
|
50
|
+
return this._with(this._builder.whenMatchedUpdate(set, condExpr));
|
|
57
51
|
}
|
|
58
52
|
whenMatchedThenDelete(condition) {
|
|
59
53
|
let condExpr;
|
|
60
54
|
if (condition) {
|
|
61
|
-
resetParams();
|
|
62
55
|
const proxies = createMergeProxies(this._targetTable, this._sourceAlias);
|
|
63
56
|
condExpr = unwrap(condition(proxies));
|
|
64
57
|
}
|
|
65
|
-
return this._with(this._builder.whenMatchedDelete(condExpr)
|
|
58
|
+
return this._with(this._builder.whenMatchedDelete(condExpr));
|
|
66
59
|
}
|
|
67
60
|
whenNotMatchedThenInsert(row, condition) {
|
|
68
|
-
let idx = this._paramIdx;
|
|
69
61
|
const entries = Object.entries(row);
|
|
70
62
|
const columns = entries.map(([k]) => k);
|
|
71
|
-
const values = entries.map(([_, v]) =>
|
|
72
|
-
const p = param(idx, v);
|
|
73
|
-
idx++;
|
|
74
|
-
return p;
|
|
75
|
-
});
|
|
63
|
+
const values = entries.map(([_, v]) => param(0, v));
|
|
76
64
|
let condExpr;
|
|
77
65
|
if (condition) {
|
|
78
|
-
resetParams();
|
|
79
66
|
const proxies = createMergeProxies(this._targetTable, this._sourceAlias);
|
|
80
67
|
condExpr = unwrap(condition(proxies));
|
|
81
68
|
}
|
|
82
|
-
return this._with(this._builder.whenNotMatchedInsert(columns, values, condExpr)
|
|
69
|
+
return this._with(this._builder.whenNotMatchedInsert(columns, values, condExpr));
|
|
83
70
|
}
|
|
84
71
|
with(name, query, recursive = false) {
|
|
85
|
-
return this._with(this._builder.with(name, query, recursive)
|
|
72
|
+
return this._with(this._builder.with(name, query, recursive));
|
|
86
73
|
}
|
|
87
74
|
build() {
|
|
88
75
|
return this._builder.build();
|
|
@@ -18,7 +18,8 @@ export declare class TypedSelectBuilder<
|
|
|
18
18
|
O
|
|
19
19
|
> {
|
|
20
20
|
private _table;
|
|
21
|
-
|
|
21
|
+
private _printer?;
|
|
22
|
+
constructor(builder: SelectBuilder, table?: string, printer?: Printer);
|
|
22
23
|
/** Select specific columns. Narrows O. */
|
|
23
24
|
select<K extends keyof O & string>(...cols: K[]): TypedSelectBuilder<DB, TB, Pick<O, K>>;
|
|
24
25
|
/** Select all columns. */
|
|
@@ -166,8 +167,10 @@ export declare class TypedSelectBuilder<
|
|
|
166
167
|
$if<O2>(condition: boolean, fn: (qb: TypedSelectBuilder<DB, TB, O>) => TypedSelectBuilder<DB, TB, O2>): TypedSelectBuilder<DB, TB, O | O2>;
|
|
167
168
|
/** Build the AST node. */
|
|
168
169
|
build(): SelectNode;
|
|
169
|
-
/** Compile to SQL. */
|
|
170
|
+
/** Compile to SQL with explicit printer. */
|
|
170
171
|
compile(printer: Printer): CompiledQuery;
|
|
172
|
+
/** Compile to SQL using the dialect's printer. */
|
|
173
|
+
toSQL(): CompiledQuery;
|
|
171
174
|
/** EXPLAIN this query. */
|
|
172
175
|
explain(options?: {
|
|
173
176
|
analyze?: boolean;
|
|
@@ -1,28 +1,30 @@
|
|
|
1
1
|
import { unwrap } from "../ast/typed-expression.mjs";
|
|
2
|
-
import { createColumnProxies
|
|
2
|
+
import { createColumnProxies } from "./eb.mjs";
|
|
3
3
|
import { SelectBuilder } from "./select.mjs";
|
|
4
4
|
|
|
5
5
|
export class TypedSelectBuilder {
|
|
6
6
|
|
|
7
7
|
_builder;
|
|
8
8
|
_table;
|
|
9
|
-
|
|
9
|
+
_printer;
|
|
10
|
+
constructor(builder, table, printer) {
|
|
10
11
|
this._builder = builder;
|
|
11
12
|
this._table = table ?? "";
|
|
13
|
+
this._printer = printer;
|
|
12
14
|
}
|
|
13
15
|
|
|
14
16
|
select(...cols) {
|
|
15
|
-
return new TypedSelectBuilder(this._builder.columns(...cols), this._table);
|
|
17
|
+
return new TypedSelectBuilder(this._builder.columns(...cols), this._table, this._printer);
|
|
16
18
|
}
|
|
17
19
|
|
|
18
20
|
selectAll() {
|
|
19
|
-
return new TypedSelectBuilder(this._builder.allColumns(), this._table);
|
|
21
|
+
return new TypedSelectBuilder(this._builder.allColumns(), this._table, this._printer);
|
|
20
22
|
}
|
|
21
23
|
|
|
22
24
|
selectExpr(expr, alias) {
|
|
23
25
|
const node = unwrap(expr);
|
|
24
26
|
const aliased = aliasExpr(node, alias);
|
|
25
|
-
return new TypedSelectBuilder(this._builder.columns(aliased), this._table);
|
|
27
|
+
return new TypedSelectBuilder(this._builder.columns(aliased), this._table, this._printer);
|
|
26
28
|
}
|
|
27
29
|
|
|
28
30
|
selectExprs(exprs) {
|
|
@@ -32,145 +34,142 @@ export class TypedSelectBuilder {
|
|
|
32
34
|
const aliased = aliasExpr(node, alias);
|
|
33
35
|
builder = builder.columns(aliased);
|
|
34
36
|
}
|
|
35
|
-
return new TypedSelectBuilder(builder, this._table);
|
|
37
|
+
return new TypedSelectBuilder(builder, this._table, this._printer);
|
|
36
38
|
}
|
|
37
39
|
|
|
38
40
|
distinct() {
|
|
39
|
-
return new TypedSelectBuilder(this._builder.distinct(), this._table);
|
|
41
|
+
return new TypedSelectBuilder(this._builder.distinct(), this._table, this._printer);
|
|
40
42
|
}
|
|
41
43
|
|
|
42
44
|
distinctOn(...cols) {
|
|
43
|
-
return new TypedSelectBuilder(this._builder.distinctOn(...cols), this._table);
|
|
45
|
+
return new TypedSelectBuilder(this._builder.distinctOn(...cols), this._table, this._printer);
|
|
44
46
|
}
|
|
45
47
|
|
|
46
48
|
where(exprOrCallback) {
|
|
47
49
|
if (typeof exprOrCallback === "function") {
|
|
48
|
-
resetParams();
|
|
49
50
|
const cols = createColumnProxies(this._table);
|
|
50
51
|
const result = exprOrCallback(cols);
|
|
51
|
-
return new TypedSelectBuilder(this._builder.where(unwrap(result)), this._table);
|
|
52
|
+
return new TypedSelectBuilder(this._builder.where(unwrap(result)), this._table, this._printer);
|
|
52
53
|
}
|
|
53
|
-
return new TypedSelectBuilder(this._builder.where(unwrap(exprOrCallback)), this._table);
|
|
54
|
+
return new TypedSelectBuilder(this._builder.where(unwrap(exprOrCallback)), this._table, this._printer);
|
|
54
55
|
}
|
|
55
56
|
|
|
56
57
|
orWhere(exprOrCallback) {
|
|
57
58
|
if (typeof exprOrCallback === "function") {
|
|
58
|
-
resetParams();
|
|
59
59
|
const cols = createColumnProxies(this._table);
|
|
60
60
|
const result = exprOrCallback(cols);
|
|
61
|
-
return new TypedSelectBuilder(this._builder.orWhere(unwrap(result)), this._table);
|
|
61
|
+
return new TypedSelectBuilder(this._builder.orWhere(unwrap(result)), this._table, this._printer);
|
|
62
62
|
}
|
|
63
|
-
return new TypedSelectBuilder(this._builder.orWhere(unwrap(exprOrCallback)), this._table);
|
|
63
|
+
return new TypedSelectBuilder(this._builder.orWhere(unwrap(exprOrCallback)), this._table, this._printer);
|
|
64
64
|
}
|
|
65
65
|
|
|
66
66
|
innerJoin(table, onOrCallback) {
|
|
67
67
|
const on = resolveJoinOn(onOrCallback, this._table, table);
|
|
68
|
-
return new TypedSelectBuilder(this._builder.innerJoin(table, unwrap(on)), this._table);
|
|
68
|
+
return new TypedSelectBuilder(this._builder.innerJoin(table, unwrap(on)), this._table, this._printer);
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
leftJoin(table, onOrCallback) {
|
|
72
72
|
const on = resolveJoinOn(onOrCallback, this._table, table);
|
|
73
|
-
return new TypedSelectBuilder(this._builder.leftJoin(table, unwrap(on)), this._table);
|
|
73
|
+
return new TypedSelectBuilder(this._builder.leftJoin(table, unwrap(on)), this._table, this._printer);
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
rightJoin(table, onOrCallback) {
|
|
77
77
|
const on = resolveJoinOn(onOrCallback, this._table, table);
|
|
78
|
-
return new TypedSelectBuilder(this._builder.rightJoin(table, unwrap(on)), this._table);
|
|
78
|
+
return new TypedSelectBuilder(this._builder.rightJoin(table, unwrap(on)), this._table, this._printer);
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
groupBy(...cols) {
|
|
82
82
|
const resolved = cols.map((c) => typeof c === "string" ? c : unwrap(c));
|
|
83
|
-
return new TypedSelectBuilder(this._builder.groupBy(...resolved), this._table);
|
|
83
|
+
return new TypedSelectBuilder(this._builder.groupBy(...resolved), this._table, this._printer);
|
|
84
84
|
}
|
|
85
85
|
|
|
86
86
|
having(exprOrCallback) {
|
|
87
87
|
if (typeof exprOrCallback === "function") {
|
|
88
|
-
resetParams();
|
|
89
88
|
const cols = createColumnProxies(this._table);
|
|
90
89
|
const result = exprOrCallback(cols);
|
|
91
|
-
return new TypedSelectBuilder(this._builder.having(unwrap(result)), this._table);
|
|
90
|
+
return new TypedSelectBuilder(this._builder.having(unwrap(result)), this._table, this._printer);
|
|
92
91
|
}
|
|
93
|
-
return new TypedSelectBuilder(this._builder.having(unwrap(exprOrCallback)), this._table);
|
|
92
|
+
return new TypedSelectBuilder(this._builder.having(unwrap(exprOrCallback)), this._table, this._printer);
|
|
94
93
|
}
|
|
95
94
|
|
|
96
95
|
orderBy(col, direction = "ASC", nulls) {
|
|
97
96
|
const expr = typeof col === "string" ? col : unwrap(col);
|
|
98
|
-
return new TypedSelectBuilder(this._builder.orderBy(expr, direction, nulls), this._table);
|
|
97
|
+
return new TypedSelectBuilder(this._builder.orderBy(expr, direction, nulls), this._table, this._printer);
|
|
99
98
|
}
|
|
100
99
|
|
|
101
100
|
limit(n) {
|
|
102
101
|
return new TypedSelectBuilder(this._builder.limit({
|
|
103
102
|
type: "literal",
|
|
104
103
|
value: n
|
|
105
|
-
}), this._table);
|
|
104
|
+
}), this._table, this._printer);
|
|
106
105
|
}
|
|
107
106
|
|
|
108
107
|
offset(n) {
|
|
109
108
|
return new TypedSelectBuilder(this._builder.offset({
|
|
110
109
|
type: "literal",
|
|
111
110
|
value: n
|
|
112
|
-
}), this._table);
|
|
111
|
+
}), this._table, this._printer);
|
|
113
112
|
}
|
|
114
113
|
|
|
115
114
|
forSystemTime(clause) {
|
|
116
|
-
return new TypedSelectBuilder(this._builder.forSystemTime(clause), this._table);
|
|
115
|
+
return new TypedSelectBuilder(this._builder.forSystemTime(clause), this._table, this._printer);
|
|
117
116
|
}
|
|
118
117
|
|
|
119
118
|
forUpdate() {
|
|
120
|
-
return new TypedSelectBuilder(this._builder.forUpdate(), this._table);
|
|
119
|
+
return new TypedSelectBuilder(this._builder.forUpdate(), this._table, this._printer);
|
|
121
120
|
}
|
|
122
121
|
|
|
123
122
|
forShare() {
|
|
124
|
-
return new TypedSelectBuilder(this._builder.forShare(), this._table);
|
|
123
|
+
return new TypedSelectBuilder(this._builder.forShare(), this._table, this._printer);
|
|
125
124
|
}
|
|
126
125
|
|
|
127
126
|
forNoKeyUpdate() {
|
|
128
|
-
return new TypedSelectBuilder(this._builder.forNoKeyUpdate(), this._table);
|
|
127
|
+
return new TypedSelectBuilder(this._builder.forNoKeyUpdate(), this._table, this._printer);
|
|
129
128
|
}
|
|
130
129
|
|
|
131
130
|
forKeyShare() {
|
|
132
|
-
return new TypedSelectBuilder(this._builder.forKeyShare(), this._table);
|
|
131
|
+
return new TypedSelectBuilder(this._builder.forKeyShare(), this._table, this._printer);
|
|
133
132
|
}
|
|
134
133
|
|
|
135
134
|
skipLocked() {
|
|
136
|
-
return new TypedSelectBuilder(this._builder.skipLocked(), this._table);
|
|
135
|
+
return new TypedSelectBuilder(this._builder.skipLocked(), this._table, this._printer);
|
|
137
136
|
}
|
|
138
137
|
|
|
139
138
|
noWait() {
|
|
140
|
-
return new TypedSelectBuilder(this._builder.noWait(), this._table);
|
|
139
|
+
return new TypedSelectBuilder(this._builder.noWait(), this._table, this._printer);
|
|
141
140
|
}
|
|
142
141
|
|
|
143
142
|
with(name, query, recursive = false) {
|
|
144
|
-
return new TypedSelectBuilder(this._builder.with(name, query, recursive), this._table);
|
|
143
|
+
return new TypedSelectBuilder(this._builder.with(name, query, recursive), this._table, this._printer);
|
|
145
144
|
}
|
|
146
145
|
|
|
147
146
|
union(query) {
|
|
148
|
-
return new TypedSelectBuilder(this._builder.union(query.build()), this._table);
|
|
147
|
+
return new TypedSelectBuilder(this._builder.union(query.build()), this._table, this._printer);
|
|
149
148
|
}
|
|
150
149
|
|
|
151
150
|
unionAll(query) {
|
|
152
|
-
return new TypedSelectBuilder(this._builder.unionAll(query.build()), this._table);
|
|
151
|
+
return new TypedSelectBuilder(this._builder.unionAll(query.build()), this._table, this._printer);
|
|
153
152
|
}
|
|
154
153
|
|
|
155
154
|
intersect(query) {
|
|
156
|
-
return new TypedSelectBuilder(this._builder.intersect(query.build()), this._table);
|
|
155
|
+
return new TypedSelectBuilder(this._builder.intersect(query.build()), this._table, this._printer);
|
|
157
156
|
}
|
|
158
157
|
|
|
159
158
|
intersectAll(query) {
|
|
160
|
-
return new TypedSelectBuilder(this._builder.intersectAll(query.build()), this._table);
|
|
159
|
+
return new TypedSelectBuilder(this._builder.intersectAll(query.build()), this._table, this._printer);
|
|
161
160
|
}
|
|
162
161
|
|
|
163
162
|
except(query) {
|
|
164
|
-
return new TypedSelectBuilder(this._builder.except(query.build()), this._table);
|
|
163
|
+
return new TypedSelectBuilder(this._builder.except(query.build()), this._table, this._printer);
|
|
165
164
|
}
|
|
166
165
|
|
|
167
166
|
exceptAll(query) {
|
|
168
|
-
return new TypedSelectBuilder(this._builder.exceptAll(query.build()), this._table);
|
|
167
|
+
return new TypedSelectBuilder(this._builder.exceptAll(query.build()), this._table, this._printer);
|
|
169
168
|
}
|
|
170
169
|
|
|
171
170
|
fullJoin(table, onOrCallback) {
|
|
172
171
|
const on = resolveJoinOn(onOrCallback, this._table, table);
|
|
173
|
-
return new TypedSelectBuilder(this._builder.join("FULL", table, unwrap(on)), this._table);
|
|
172
|
+
return new TypedSelectBuilder(this._builder.join("FULL", table, unwrap(on)), this._table, this._printer);
|
|
174
173
|
}
|
|
175
174
|
|
|
176
175
|
innerJoinLateral(subquery, alias, on) {
|
|
@@ -179,7 +178,7 @@ export class TypedSelectBuilder {
|
|
|
179
178
|
query: subquery.build(),
|
|
180
179
|
alias
|
|
181
180
|
};
|
|
182
|
-
return new TypedSelectBuilder(this._builder.innerJoinLateral(sub, unwrap(on)), this._table);
|
|
181
|
+
return new TypedSelectBuilder(this._builder.innerJoinLateral(sub, unwrap(on)), this._table, this._printer);
|
|
183
182
|
}
|
|
184
183
|
|
|
185
184
|
leftJoinLateral(subquery, alias, on) {
|
|
@@ -188,7 +187,7 @@ export class TypedSelectBuilder {
|
|
|
188
187
|
query: subquery.build(),
|
|
189
188
|
alias
|
|
190
189
|
};
|
|
191
|
-
return new TypedSelectBuilder(this._builder.leftJoinLateral(sub, unwrap(on)), this._table);
|
|
190
|
+
return new TypedSelectBuilder(this._builder.leftJoinLateral(sub, unwrap(on)), this._table, this._printer);
|
|
192
191
|
}
|
|
193
192
|
|
|
194
193
|
innerJoinAs(table, alias, onCallback) {
|
|
@@ -202,7 +201,7 @@ export class TypedSelectBuilder {
|
|
|
202
201
|
type: "table_ref",
|
|
203
202
|
name: table,
|
|
204
203
|
alias
|
|
205
|
-
}, unwrap(on)), this._table);
|
|
204
|
+
}, unwrap(on)), this._table, this._printer);
|
|
206
205
|
}
|
|
207
206
|
|
|
208
207
|
leftJoinAs(table, alias, onCallback) {
|
|
@@ -216,11 +215,11 @@ export class TypedSelectBuilder {
|
|
|
216
215
|
type: "table_ref",
|
|
217
216
|
name: table,
|
|
218
217
|
alias
|
|
219
|
-
}, unwrap(on)), this._table);
|
|
218
|
+
}, unwrap(on)), this._table, this._printer);
|
|
220
219
|
}
|
|
221
220
|
|
|
222
221
|
crossJoin(table) {
|
|
223
|
-
return new TypedSelectBuilder(this._builder.join("CROSS", table), this._table);
|
|
222
|
+
return new TypedSelectBuilder(this._builder.join("CROSS", table), this._table, this._printer);
|
|
224
223
|
}
|
|
225
224
|
|
|
226
225
|
crossJoinLateral(subquery, alias) {
|
|
@@ -229,56 +228,56 @@ export class TypedSelectBuilder {
|
|
|
229
228
|
query: subquery.build(),
|
|
230
229
|
alias
|
|
231
230
|
};
|
|
232
|
-
return new TypedSelectBuilder(this._builder.crossJoinLateral(sub), this._table);
|
|
231
|
+
return new TypedSelectBuilder(this._builder.crossJoinLateral(sub), this._table, this._printer);
|
|
233
232
|
}
|
|
234
233
|
|
|
235
234
|
clearWhere() {
|
|
236
235
|
return new TypedSelectBuilder(new SelectBuilder({
|
|
237
236
|
...this._builder.build(),
|
|
238
237
|
where: undefined
|
|
239
|
-
}), this._table);
|
|
238
|
+
}), this._table, this._printer);
|
|
240
239
|
}
|
|
241
240
|
|
|
242
241
|
clearOrderBy() {
|
|
243
242
|
return new TypedSelectBuilder(new SelectBuilder({
|
|
244
243
|
...this._builder.build(),
|
|
245
244
|
orderBy: []
|
|
246
|
-
}), this._table);
|
|
245
|
+
}), this._table, this._printer);
|
|
247
246
|
}
|
|
248
247
|
|
|
249
248
|
clearLimit() {
|
|
250
249
|
return new TypedSelectBuilder(new SelectBuilder({
|
|
251
250
|
...this._builder.build(),
|
|
252
251
|
limit: undefined
|
|
253
|
-
}), this._table);
|
|
252
|
+
}), this._table, this._printer);
|
|
254
253
|
}
|
|
255
254
|
|
|
256
255
|
clearOffset() {
|
|
257
256
|
return new TypedSelectBuilder(new SelectBuilder({
|
|
258
257
|
...this._builder.build(),
|
|
259
258
|
offset: undefined
|
|
260
|
-
}), this._table);
|
|
259
|
+
}), this._table, this._printer);
|
|
261
260
|
}
|
|
262
261
|
|
|
263
262
|
clearGroupBy() {
|
|
264
263
|
return new TypedSelectBuilder(new SelectBuilder({
|
|
265
264
|
...this._builder.build(),
|
|
266
265
|
groupBy: []
|
|
267
|
-
}), this._table);
|
|
266
|
+
}), this._table, this._printer);
|
|
268
267
|
}
|
|
269
268
|
|
|
270
269
|
clearHaving() {
|
|
271
270
|
return new TypedSelectBuilder(new SelectBuilder({
|
|
272
271
|
...this._builder.build(),
|
|
273
272
|
having: undefined
|
|
274
|
-
}), this._table);
|
|
273
|
+
}), this._table, this._printer);
|
|
275
274
|
}
|
|
276
275
|
|
|
277
276
|
clearSelect() {
|
|
278
277
|
return new TypedSelectBuilder(new SelectBuilder({
|
|
279
278
|
...this._builder.build(),
|
|
280
279
|
columns: []
|
|
281
|
-
}), this._table);
|
|
280
|
+
}), this._table, this._printer);
|
|
282
281
|
}
|
|
283
282
|
|
|
284
283
|
$call(fn) {
|
|
@@ -300,6 +299,13 @@ export class TypedSelectBuilder {
|
|
|
300
299
|
return printer.print(this.build());
|
|
301
300
|
}
|
|
302
301
|
|
|
302
|
+
toSQL() {
|
|
303
|
+
if (!this._printer) {
|
|
304
|
+
throw new Error("toSQL() requires a printer. Use db.selectFrom() or pass a printer to compile().");
|
|
305
|
+
}
|
|
306
|
+
return this._printer.print(this.build());
|
|
307
|
+
}
|
|
308
|
+
|
|
303
309
|
explain(options) {
|
|
304
310
|
const node = this.build();
|
|
305
311
|
const explainNode = {
|