rado 0.2.24 → 0.2.26
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/define/Expr.d.ts +5 -4
- package/dist/define/Expr.js +3 -2
- package/dist/define/Fields.d.ts +2 -2
- package/dist/define/Ops.d.ts +13 -7
- package/dist/define/Ops.js +17 -7
- package/dist/define/Query.d.ts +4 -104
- package/dist/define/Query.js +8 -388
- package/dist/define/Selection.d.ts +2 -2
- package/dist/define/Table.d.ts +10 -16
- package/dist/define/Table.js +3 -41
- package/dist/define/Target.d.ts +6 -6
- package/dist/define/Target.js +8 -8
- package/dist/define/VirtualTable.d.ts +33 -0
- package/dist/define/VirtualTable.js +41 -0
- package/dist/define/query/Batch.d.ts +7 -0
- package/dist/define/query/Batch.js +18 -0
- package/dist/define/query/CreateTable.d.ts +6 -0
- package/dist/define/query/CreateTable.js +12 -0
- package/dist/define/query/Delete.d.ts +11 -0
- package/dist/define/query/Delete.js +19 -0
- package/dist/define/query/Insert.d.ts +19 -0
- package/dist/define/query/Insert.js +36 -0
- package/dist/define/query/Select.d.ts +40 -0
- package/dist/define/query/Select.js +173 -0
- package/dist/define/query/TableSelect.d.ts +21 -0
- package/dist/define/query/TableSelect.js +71 -0
- package/dist/define/query/Union.d.ts +17 -0
- package/dist/define/query/Union.js +117 -0
- package/dist/define/query/Update.d.ts +12 -0
- package/dist/define/query/Update.js +19 -0
- package/dist/driver/bun-sqlite.js +3 -2
- package/dist/driver/sql.js.d.ts +2 -2
- package/dist/driver/sql.js.js +2 -2
- package/dist/lib/Driver.d.ts +3 -16
- package/dist/lib/Driver.js +0 -28
- package/dist/lib/Formatter.d.ts +3 -1
- package/dist/lib/Formatter.js +28 -9
- package/dist/sqlite/SqliteSchema.js +3 -3
- package/dist/util/Alias.d.ts +1 -0
- package/dist/util/Alias.js +7 -0
- package/dist/util/Callable.d.ts +8 -0
- package/package.json +1 -1
- package/dist/define/CTE.d.ts +0 -2
- package/dist/define/CTE.js +0 -51
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// src/define/query/Batch.ts
|
|
2
|
+
import { Query, QueryData } from "../Query.js";
|
|
3
|
+
var Batch = class extends Query {
|
|
4
|
+
constructor(queries) {
|
|
5
|
+
super(new QueryData.Batch({ queries }));
|
|
6
|
+
this.queries = queries;
|
|
7
|
+
}
|
|
8
|
+
next(cursor) {
|
|
9
|
+
return new Query(
|
|
10
|
+
new QueryData.Batch({
|
|
11
|
+
queries: [...this[Query.Data].queries, cursor[Query.Data]]
|
|
12
|
+
})
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
export {
|
|
17
|
+
Batch
|
|
18
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// src/define/query/CreateTable.ts
|
|
2
|
+
import { Query } from "../Query.js";
|
|
3
|
+
import { Schema } from "../Schema.js";
|
|
4
|
+
var CreateTable = class extends Query {
|
|
5
|
+
constructor(table) {
|
|
6
|
+
super(Schema.create(table));
|
|
7
|
+
this.table = table;
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
export {
|
|
11
|
+
CreateTable
|
|
12
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { EV } from '../Expr';
|
|
2
|
+
import { Query, QueryData } from '../Query';
|
|
3
|
+
export declare class Delete extends Query<{
|
|
4
|
+
rowsAffected: number;
|
|
5
|
+
}> {
|
|
6
|
+
[Query.Data]: QueryData.Delete;
|
|
7
|
+
constructor(query: QueryData.Delete);
|
|
8
|
+
where(...where: Array<EV<boolean>>): Delete;
|
|
9
|
+
take(limit: number | undefined): Delete;
|
|
10
|
+
skip(offset: number | undefined): Delete;
|
|
11
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// src/define/query/Delete.ts
|
|
2
|
+
import { Query } from "../Query.js";
|
|
3
|
+
var Delete = class extends Query {
|
|
4
|
+
constructor(query) {
|
|
5
|
+
super(query);
|
|
6
|
+
}
|
|
7
|
+
where(...where) {
|
|
8
|
+
return new Delete(this.addWhere(where));
|
|
9
|
+
}
|
|
10
|
+
take(limit) {
|
|
11
|
+
return new Delete(this[Query.Data].with({ limit }));
|
|
12
|
+
}
|
|
13
|
+
skip(offset) {
|
|
14
|
+
return new Delete(this[Query.Data].with({ offset }));
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
export {
|
|
18
|
+
Delete
|
|
19
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Query, QueryData } from '../Query';
|
|
2
|
+
import { Selection } from '../Selection';
|
|
3
|
+
import { Table, TableData } from '../Table';
|
|
4
|
+
import { SelectMultiple } from './Select';
|
|
5
|
+
export declare class InsertValuesReturning<T> extends Query<T> {
|
|
6
|
+
}
|
|
7
|
+
export declare class Inserted extends Query<{
|
|
8
|
+
rowsAffected: number;
|
|
9
|
+
}> {
|
|
10
|
+
[Query.Data]: QueryData.Insert;
|
|
11
|
+
constructor(query: QueryData.Insert);
|
|
12
|
+
returning<X extends Selection>(selection: X): InsertValuesReturning<Selection.Infer<X>>;
|
|
13
|
+
}
|
|
14
|
+
export declare class Insert<Definition> {
|
|
15
|
+
protected into: TableData;
|
|
16
|
+
constructor(into: TableData);
|
|
17
|
+
selection(query: SelectMultiple<Table.Select<Definition>>): Inserted;
|
|
18
|
+
values(...data: Array<Table.Insert<Definition>>): Inserted;
|
|
19
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// src/define/query/Insert.ts
|
|
2
|
+
import { ExprData } from "../Expr.js";
|
|
3
|
+
import { Query, QueryData } from "../Query.js";
|
|
4
|
+
var InsertValuesReturning = class extends Query {
|
|
5
|
+
};
|
|
6
|
+
var Inserted = class extends Query {
|
|
7
|
+
constructor(query) {
|
|
8
|
+
super(query);
|
|
9
|
+
}
|
|
10
|
+
returning(selection) {
|
|
11
|
+
return new InsertValuesReturning(
|
|
12
|
+
new QueryData.Insert({
|
|
13
|
+
...this[Query.Data],
|
|
14
|
+
selection: ExprData.create(selection)
|
|
15
|
+
})
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
var Insert = class {
|
|
20
|
+
constructor(into) {
|
|
21
|
+
this.into = into;
|
|
22
|
+
}
|
|
23
|
+
selection(query) {
|
|
24
|
+
return new Inserted(
|
|
25
|
+
new QueryData.Insert({ into: this.into, select: query[Query.Data] })
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
values(...data) {
|
|
29
|
+
return new Inserted(new QueryData.Insert({ into: this.into, data }));
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
export {
|
|
33
|
+
Insert,
|
|
34
|
+
InsertValuesReturning,
|
|
35
|
+
Inserted
|
|
36
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { EV, Expr } from '../Expr';
|
|
2
|
+
import { OrderBy } from '../OrderBy';
|
|
3
|
+
import { Query, QueryData } from '../Query';
|
|
4
|
+
import { Selection } from '../Selection';
|
|
5
|
+
import { Table } from '../Table';
|
|
6
|
+
import { VirtualTable } from '../VirtualTable';
|
|
7
|
+
import { TableSelect } from './TableSelect';
|
|
8
|
+
import { Union } from './Union';
|
|
9
|
+
export declare class SelectMultiple<Row> extends Union<Row> {
|
|
10
|
+
[Query.Data]: QueryData.Select;
|
|
11
|
+
constructor(query: QueryData.Select);
|
|
12
|
+
from(table: Table<any> | VirtualTable<any>): SelectMultiple<Row>;
|
|
13
|
+
leftJoin<C>(that: Table<C> | TableSelect<C>, ...on: Array<EV<boolean>>): SelectMultiple<Row>;
|
|
14
|
+
innerJoin<C>(that: Table<C> | TableSelect<C>, ...on: Array<EV<boolean>>): SelectMultiple<Row>;
|
|
15
|
+
select<X extends Selection>(selection: X): SelectMultiple<Selection.Infer<X>>;
|
|
16
|
+
count(): SelectSingle<number>;
|
|
17
|
+
orderBy(...orderBy: Array<Expr<any> | OrderBy>): SelectMultiple<Row>;
|
|
18
|
+
groupBy(...groupBy: Array<Expr<any>>): SelectMultiple<Row>;
|
|
19
|
+
maybeFirst(): SelectSingle<Row | undefined>;
|
|
20
|
+
first(): SelectSingle<Row>;
|
|
21
|
+
where(...where: Array<EV<boolean>>): SelectMultiple<Row>;
|
|
22
|
+
take(limit: number | undefined): SelectMultiple<Row>;
|
|
23
|
+
skip(offset: number | undefined): SelectMultiple<Row>;
|
|
24
|
+
[Expr.ToExpr](): Expr<Row>;
|
|
25
|
+
}
|
|
26
|
+
export declare class SelectSingle<Row> extends Query<Row> {
|
|
27
|
+
[Query.Data]: QueryData.Select;
|
|
28
|
+
constructor(query: QueryData.Select);
|
|
29
|
+
from(table: Table<any>): SelectMultiple<Row>;
|
|
30
|
+
leftJoin<C>(that: Table<C> | TableSelect<C>, ...on: Array<EV<boolean>>): SelectSingle<Row>;
|
|
31
|
+
innerJoin<C>(that: Table<C> | TableSelect<C>, ...on: Array<EV<boolean>>): SelectSingle<Row>;
|
|
32
|
+
select<X extends Selection>(selection: X): SelectSingle<Selection.Infer<X>>;
|
|
33
|
+
orderBy(...orderBy: Array<OrderBy>): SelectSingle<Row>;
|
|
34
|
+
groupBy(...groupBy: Array<Expr<any>>): SelectSingle<Row>;
|
|
35
|
+
where(...where: Array<EV<boolean>>): SelectSingle<Row>;
|
|
36
|
+
take(limit: number | undefined): SelectSingle<Row>;
|
|
37
|
+
skip(offset: number | undefined): SelectSingle<Row>;
|
|
38
|
+
all(): SelectMultiple<Row>;
|
|
39
|
+
[Expr.ToExpr](): Expr<Row>;
|
|
40
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
// src/define/query/Select.ts
|
|
2
|
+
import { Expr, ExprData } from "../Expr.js";
|
|
3
|
+
import { Functions } from "../Functions.js";
|
|
4
|
+
import { Query } from "../Query.js";
|
|
5
|
+
import { Table } from "../Table.js";
|
|
6
|
+
import { Target } from "../Target.js";
|
|
7
|
+
import { VirtualTable } from "../VirtualTable.js";
|
|
8
|
+
import { Union } from "./Union.js";
|
|
9
|
+
function joinTarget(joinType, query, to, on) {
|
|
10
|
+
const toQuery = Query.isQuery(to) ? to[Query.Data] : void 0;
|
|
11
|
+
const target = toQuery ? toQuery.from : new Target.Table(to[Table.Data]);
|
|
12
|
+
if (!query.from || !target)
|
|
13
|
+
throw new Error("No from clause");
|
|
14
|
+
const conditions = [...on];
|
|
15
|
+
if (toQuery) {
|
|
16
|
+
const where = toQuery.where;
|
|
17
|
+
if (where)
|
|
18
|
+
conditions.push(new Expr(where));
|
|
19
|
+
}
|
|
20
|
+
return new Target.Join(
|
|
21
|
+
query.from,
|
|
22
|
+
target,
|
|
23
|
+
joinType,
|
|
24
|
+
Expr.and(...conditions)[Expr.Data]
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
var SelectMultiple = class extends Union {
|
|
28
|
+
constructor(query) {
|
|
29
|
+
super(query);
|
|
30
|
+
}
|
|
31
|
+
from(table) {
|
|
32
|
+
const virtual = table[VirtualTable.Data];
|
|
33
|
+
return new SelectMultiple(
|
|
34
|
+
this[Query.Data].with({ from: virtual?.target || new Target.Table(table) })
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
leftJoin(that, ...on) {
|
|
38
|
+
const query = this[Query.Data];
|
|
39
|
+
return new SelectMultiple(
|
|
40
|
+
this[Query.Data].with({
|
|
41
|
+
from: joinTarget("left", query, that, on)
|
|
42
|
+
})
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
innerJoin(that, ...on) {
|
|
46
|
+
const query = this[Query.Data];
|
|
47
|
+
return new SelectMultiple(
|
|
48
|
+
this[Query.Data].with({
|
|
49
|
+
from: joinTarget("inner", query, that, on)
|
|
50
|
+
})
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
select(selection) {
|
|
54
|
+
return new SelectMultiple(
|
|
55
|
+
this[Query.Data].with({
|
|
56
|
+
selection: ExprData.create(selection)
|
|
57
|
+
})
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
count() {
|
|
61
|
+
return new SelectSingle(
|
|
62
|
+
this[Query.Data].with({
|
|
63
|
+
selection: Functions.count()[Expr.Data],
|
|
64
|
+
singleResult: true
|
|
65
|
+
})
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
orderBy(...orderBy) {
|
|
69
|
+
return new SelectMultiple(
|
|
70
|
+
this[Query.Data].with({
|
|
71
|
+
orderBy: orderBy.map((e) => {
|
|
72
|
+
return Expr.isExpr(e) ? e.asc() : e;
|
|
73
|
+
})
|
|
74
|
+
})
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
groupBy(...groupBy) {
|
|
78
|
+
return new SelectMultiple(
|
|
79
|
+
this[Query.Data].with({
|
|
80
|
+
groupBy: groupBy.map(ExprData.create)
|
|
81
|
+
})
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
maybeFirst() {
|
|
85
|
+
return new SelectSingle(this[Query.Data].with({ singleResult: true }));
|
|
86
|
+
}
|
|
87
|
+
first() {
|
|
88
|
+
return new SelectSingle(
|
|
89
|
+
this[Query.Data].with({
|
|
90
|
+
singleResult: true,
|
|
91
|
+
validate: true
|
|
92
|
+
})
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
where(...where) {
|
|
96
|
+
return new SelectMultiple(this.addWhere(where));
|
|
97
|
+
}
|
|
98
|
+
take(limit) {
|
|
99
|
+
return new SelectMultiple(this[Query.Data].with({ limit }));
|
|
100
|
+
}
|
|
101
|
+
skip(offset) {
|
|
102
|
+
return new SelectMultiple(this[Query.Data].with({ offset }));
|
|
103
|
+
}
|
|
104
|
+
[Expr.ToExpr]() {
|
|
105
|
+
return new Expr(new ExprData.Query(this[Query.Data]));
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
var SelectSingle = class extends Query {
|
|
109
|
+
constructor(query) {
|
|
110
|
+
super(query);
|
|
111
|
+
}
|
|
112
|
+
from(table) {
|
|
113
|
+
return new SelectMultiple(
|
|
114
|
+
this[Query.Data].with({ from: new Target.Table(table) })
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
leftJoin(that, ...on) {
|
|
118
|
+
const query = this[Query.Data];
|
|
119
|
+
return new SelectSingle(
|
|
120
|
+
this[Query.Data].with({
|
|
121
|
+
from: joinTarget("left", query, that, on)
|
|
122
|
+
})
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
innerJoin(that, ...on) {
|
|
126
|
+
const query = this[Query.Data];
|
|
127
|
+
return new SelectSingle(
|
|
128
|
+
this[Query.Data].with({
|
|
129
|
+
from: joinTarget("inner", query, that, on)
|
|
130
|
+
})
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
select(selection) {
|
|
134
|
+
return new SelectSingle(
|
|
135
|
+
this[Query.Data].with({
|
|
136
|
+
selection: ExprData.create(selection)
|
|
137
|
+
})
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
orderBy(...orderBy) {
|
|
141
|
+
return new SelectSingle(
|
|
142
|
+
this[Query.Data].with({
|
|
143
|
+
orderBy
|
|
144
|
+
})
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
groupBy(...groupBy) {
|
|
148
|
+
return new SelectSingle(
|
|
149
|
+
this[Query.Data].with({
|
|
150
|
+
groupBy: groupBy.map(ExprData.create)
|
|
151
|
+
})
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
where(...where) {
|
|
155
|
+
return new SelectSingle(this.addWhere(where));
|
|
156
|
+
}
|
|
157
|
+
take(limit) {
|
|
158
|
+
return new SelectSingle(this[Query.Data].with({ limit }));
|
|
159
|
+
}
|
|
160
|
+
skip(offset) {
|
|
161
|
+
return new SelectSingle(this[Query.Data].with({ offset }));
|
|
162
|
+
}
|
|
163
|
+
all() {
|
|
164
|
+
return new SelectMultiple(this[Query.Data].with({ singleResult: false }));
|
|
165
|
+
}
|
|
166
|
+
[Expr.ToExpr]() {
|
|
167
|
+
return new Expr(new ExprData.Query(this[Query.Data]));
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
export {
|
|
171
|
+
SelectMultiple,
|
|
172
|
+
SelectSingle
|
|
173
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { EV, Expr } from '../Expr';
|
|
2
|
+
import { Query, QueryData } from '../Query';
|
|
3
|
+
import { Table, TableData } from '../Table';
|
|
4
|
+
import { CreateTable } from './CreateTable';
|
|
5
|
+
import { Delete } from './Delete';
|
|
6
|
+
import { Inserted } from './Insert';
|
|
7
|
+
import { SelectMultiple } from './Select';
|
|
8
|
+
import { Update } from './Update';
|
|
9
|
+
export declare class TableSelect<Definition> extends SelectMultiple<Table.Select<Definition>> {
|
|
10
|
+
protected table: TableData;
|
|
11
|
+
[Query.Data]: QueryData.Select;
|
|
12
|
+
constructor(table: TableData, conditions?: Array<EV<boolean>>);
|
|
13
|
+
as(alias: string): Table<Definition>;
|
|
14
|
+
create(): CreateTable;
|
|
15
|
+
insertSelect(query: SelectMultiple<Table.Insert<Definition>>): Inserted;
|
|
16
|
+
insertOne(record: Table.Insert<Definition>): Query<Table.Select<Definition>>;
|
|
17
|
+
insertAll(data: Array<Table.Insert<Definition>>): Inserted;
|
|
18
|
+
set(data: Table.Update<Definition>): Update<Definition>;
|
|
19
|
+
delete(): Delete;
|
|
20
|
+
get(name: string): Expr<any>;
|
|
21
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// src/define/query/TableSelect.ts
|
|
2
|
+
import { Expr, ExprData } from "../Expr.js";
|
|
3
|
+
import { Query, QueryData } from "../Query.js";
|
|
4
|
+
import { createTable } from "../Table.js";
|
|
5
|
+
import { Target } from "../Target.js";
|
|
6
|
+
import { CreateTable } from "./CreateTable.js";
|
|
7
|
+
import { Delete } from "./Delete.js";
|
|
8
|
+
import { Insert, Inserted } from "./Insert.js";
|
|
9
|
+
import { SelectMultiple } from "./Select.js";
|
|
10
|
+
import { Update } from "./Update.js";
|
|
11
|
+
var TableSelect = class extends SelectMultiple {
|
|
12
|
+
constructor(table, conditions = []) {
|
|
13
|
+
const target = new Target.Table(table);
|
|
14
|
+
super(
|
|
15
|
+
new QueryData.Select({
|
|
16
|
+
from: target,
|
|
17
|
+
selection: new ExprData.Row(target),
|
|
18
|
+
where: Expr.and(...conditions)[Expr.Data]
|
|
19
|
+
})
|
|
20
|
+
);
|
|
21
|
+
this.table = table;
|
|
22
|
+
}
|
|
23
|
+
as(alias) {
|
|
24
|
+
return createTable({ ...this.table, alias });
|
|
25
|
+
}
|
|
26
|
+
create() {
|
|
27
|
+
return new CreateTable(this.table);
|
|
28
|
+
}
|
|
29
|
+
insertSelect(query) {
|
|
30
|
+
return new Inserted(
|
|
31
|
+
new QueryData.Insert({ into: this.table, select: query[Query.Data] })
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
insertOne(record) {
|
|
35
|
+
return new Query(
|
|
36
|
+
new QueryData.Insert({
|
|
37
|
+
into: this.table,
|
|
38
|
+
data: [record],
|
|
39
|
+
selection: new ExprData.Row(new Target.Table(this.table)),
|
|
40
|
+
singleResult: true
|
|
41
|
+
})
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
insertAll(data) {
|
|
45
|
+
return new Insert(this.table).values(...data);
|
|
46
|
+
}
|
|
47
|
+
set(data) {
|
|
48
|
+
return new Update(
|
|
49
|
+
new QueryData.Update({
|
|
50
|
+
table: this.table,
|
|
51
|
+
where: this[Query.Data].where
|
|
52
|
+
})
|
|
53
|
+
).set(data);
|
|
54
|
+
}
|
|
55
|
+
delete() {
|
|
56
|
+
return new Delete(
|
|
57
|
+
new QueryData.Delete({
|
|
58
|
+
table: this.table,
|
|
59
|
+
where: this[Query.Data].where
|
|
60
|
+
})
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
get(name) {
|
|
64
|
+
return new Expr(
|
|
65
|
+
new ExprData.Field(new ExprData.Row(new Target.Table(this.table)), name)
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
export {
|
|
70
|
+
TableSelect
|
|
71
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Query, QueryData } from '../Query';
|
|
2
|
+
import { VirtualTable } from '../VirtualTable';
|
|
3
|
+
import { SelectMultiple } from './Select';
|
|
4
|
+
export declare class RecursiveUnion<Row> {
|
|
5
|
+
initialSelect: QueryData.Select;
|
|
6
|
+
constructor(initialSelect: QueryData.Select);
|
|
7
|
+
union(create: () => SelectMultiple<Row> | Union<Row>): VirtualTable.Of<Row>;
|
|
8
|
+
unionAll(create: () => SelectMultiple<Row> | Union<Row>): VirtualTable.Of<Row>;
|
|
9
|
+
}
|
|
10
|
+
export declare class Union<Row> extends Query<Array<Row>> {
|
|
11
|
+
[Query.Data]: QueryData.Union | QueryData.Select;
|
|
12
|
+
constructor(query: QueryData.Union | QueryData.Select);
|
|
13
|
+
union(query: SelectMultiple<Row> | Union<Row>): Union<Row>;
|
|
14
|
+
unionAll(query: SelectMultiple<Row> | Union<Row>): Union<Row>;
|
|
15
|
+
except(query: SelectMultiple<Row> | Union<Row>): Union<Row>;
|
|
16
|
+
intersect(query: SelectMultiple<Row> | Union<Row>): Union<Row>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// src/define/query/Union.ts
|
|
2
|
+
import { randomAlias } from "../../util/Alias.js";
|
|
3
|
+
import { Expr, ExprData, ExprType } from "../Expr.js";
|
|
4
|
+
import { Query, QueryData } from "../Query.js";
|
|
5
|
+
import { Target, TargetType } from "../Target.js";
|
|
6
|
+
import { createVirtualTable } from "../VirtualTable.js";
|
|
7
|
+
import { SelectMultiple } from "./Select.js";
|
|
8
|
+
var { keys, create, fromEntries } = Object;
|
|
9
|
+
function columnsOf(expr) {
|
|
10
|
+
switch (expr.type) {
|
|
11
|
+
case ExprType.Record:
|
|
12
|
+
return keys(expr.fields);
|
|
13
|
+
case ExprType.Row:
|
|
14
|
+
switch (expr.target.type) {
|
|
15
|
+
case TargetType.Table:
|
|
16
|
+
return keys(expr.target.table.columns);
|
|
17
|
+
default:
|
|
18
|
+
throw new Error("Could not retrieve CTE columns");
|
|
19
|
+
}
|
|
20
|
+
default:
|
|
21
|
+
throw new Error("Could not retrieve CTE columns");
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function makeRecursiveUnion(initial, createUnion, operator) {
|
|
25
|
+
const name = randomAlias();
|
|
26
|
+
const cols = columnsOf(initial.selection);
|
|
27
|
+
const union = new QueryData.Union({
|
|
28
|
+
a: initial,
|
|
29
|
+
operator,
|
|
30
|
+
b: () => createUnion()[Query.Data]
|
|
31
|
+
});
|
|
32
|
+
const target = new Target.CTE(name, union);
|
|
33
|
+
const row = new ExprData.Row(target);
|
|
34
|
+
const selection = new ExprData.Record(
|
|
35
|
+
fromEntries(cols.map((col) => [col, new ExprData.Field(row, col)]))
|
|
36
|
+
);
|
|
37
|
+
const select = (conditions) => {
|
|
38
|
+
const where = Expr.and(...conditions)[Expr.Data];
|
|
39
|
+
return new SelectMultiple(
|
|
40
|
+
new QueryData.Select({
|
|
41
|
+
selection,
|
|
42
|
+
from: target,
|
|
43
|
+
where
|
|
44
|
+
})
|
|
45
|
+
);
|
|
46
|
+
};
|
|
47
|
+
const cte = createVirtualTable({
|
|
48
|
+
name,
|
|
49
|
+
target,
|
|
50
|
+
select
|
|
51
|
+
});
|
|
52
|
+
return cte;
|
|
53
|
+
}
|
|
54
|
+
var RecursiveUnion = class {
|
|
55
|
+
constructor(initialSelect) {
|
|
56
|
+
this.initialSelect = initialSelect;
|
|
57
|
+
}
|
|
58
|
+
union(create2) {
|
|
59
|
+
return makeRecursiveUnion(
|
|
60
|
+
this.initialSelect,
|
|
61
|
+
create2,
|
|
62
|
+
QueryData.UnionOperation.Union
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
unionAll(create2) {
|
|
66
|
+
return makeRecursiveUnion(
|
|
67
|
+
this.initialSelect,
|
|
68
|
+
create2,
|
|
69
|
+
QueryData.UnionOperation.UnionAll
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
var Union = class extends Query {
|
|
74
|
+
constructor(query) {
|
|
75
|
+
super(query);
|
|
76
|
+
}
|
|
77
|
+
union(query) {
|
|
78
|
+
return new Union(
|
|
79
|
+
new QueryData.Union({
|
|
80
|
+
a: this[Query.Data],
|
|
81
|
+
operator: QueryData.UnionOperation.Union,
|
|
82
|
+
b: query[Query.Data]
|
|
83
|
+
})
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
unionAll(query) {
|
|
87
|
+
return new Union(
|
|
88
|
+
new QueryData.Union({
|
|
89
|
+
a: this[Query.Data],
|
|
90
|
+
operator: QueryData.UnionOperation.UnionAll,
|
|
91
|
+
b: query[Query.Data]
|
|
92
|
+
})
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
except(query) {
|
|
96
|
+
return new Union(
|
|
97
|
+
new QueryData.Union({
|
|
98
|
+
a: this[Query.Data],
|
|
99
|
+
operator: QueryData.UnionOperation.Except,
|
|
100
|
+
b: query[Query.Data]
|
|
101
|
+
})
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
intersect(query) {
|
|
105
|
+
return new Union(
|
|
106
|
+
new QueryData.Union({
|
|
107
|
+
a: this[Query.Data],
|
|
108
|
+
operator: QueryData.UnionOperation.Intersect,
|
|
109
|
+
b: query[Query.Data]
|
|
110
|
+
})
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
export {
|
|
115
|
+
RecursiveUnion,
|
|
116
|
+
Union
|
|
117
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { EV } from '../Expr';
|
|
2
|
+
import { Query, QueryData } from '../Query';
|
|
3
|
+
import { Table } from '../Table';
|
|
4
|
+
export declare class Update<Definition> extends Query<{
|
|
5
|
+
rowsAffected: number;
|
|
6
|
+
}> {
|
|
7
|
+
[Query.Data]: QueryData.Update;
|
|
8
|
+
set(set: Table.Update<Definition>): Update<Definition>;
|
|
9
|
+
where(...where: Array<EV<boolean>>): Update<Definition>;
|
|
10
|
+
take(limit: number | undefined): Update<Definition>;
|
|
11
|
+
skip(offset: number | undefined): Update<Definition>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// src/define/query/Update.ts
|
|
2
|
+
import { Query } from "../Query.js";
|
|
3
|
+
var Update = class extends Query {
|
|
4
|
+
set(set) {
|
|
5
|
+
return new Update(this[Query.Data].with({ set }));
|
|
6
|
+
}
|
|
7
|
+
where(...where) {
|
|
8
|
+
return new Update(this.addWhere(where));
|
|
9
|
+
}
|
|
10
|
+
take(limit) {
|
|
11
|
+
return new Update(this[Query.Data].with({ limit }));
|
|
12
|
+
}
|
|
13
|
+
skip(offset) {
|
|
14
|
+
return new Update(this[Query.Data].with({ offset }));
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
export {
|
|
18
|
+
Update
|
|
19
|
+
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// src/driver/bun-sqlite.ts
|
|
2
|
-
import {
|
|
2
|
+
import { QueryData } from "../define/Query.js";
|
|
3
|
+
import { SelectSingle } from "../define/query/Select.js";
|
|
3
4
|
import { Driver } from "../lib/Driver.js";
|
|
4
5
|
import { SqlError } from "../lib/SqlError.js";
|
|
5
6
|
import { SqliteFormatter } from "../sqlite/SqliteFormatter.js";
|
|
@@ -34,7 +35,7 @@ var BunSqliteDriver = class extends Driver.Sync {
|
|
|
34
35
|
this.tableData = this.prepare(SqliteSchema.tableData);
|
|
35
36
|
this.indexData = this.prepare(SqliteSchema.indexData);
|
|
36
37
|
this.lastChanges = this.prepare(() => {
|
|
37
|
-
return new
|
|
38
|
+
return new SelectSingle(
|
|
38
39
|
new QueryData.Raw({
|
|
39
40
|
expectedReturn: "row",
|
|
40
41
|
strings: ["SELECT changes() as rowsAffected"],
|
package/dist/driver/sql.js.d.ts
CHANGED
|
@@ -5,8 +5,8 @@ import { Statement } from '../lib/Statement';
|
|
|
5
5
|
import { SqliteSchema } from '../sqlite/SqliteSchema';
|
|
6
6
|
export declare class SqlJsDriver extends Driver.Sync {
|
|
7
7
|
db: Database;
|
|
8
|
-
tableData
|
|
9
|
-
indexData
|
|
8
|
+
tableData?: (tableName: string) => Array<SqliteSchema.Column>;
|
|
9
|
+
indexData?: (tableName: string) => Array<SqliteSchema.Index>;
|
|
10
10
|
constructor(db: Database);
|
|
11
11
|
prepareStatement(stmt: Statement, discardAfter: boolean): Driver.Sync.PreparedStatement;
|
|
12
12
|
schemaInstructions(tableName: string): SchemaInstructions | undefined;
|
package/dist/driver/sql.js.js
CHANGED
|
@@ -38,8 +38,6 @@ var SqlJsDriver = class extends Driver.Sync {
|
|
|
38
38
|
constructor(db) {
|
|
39
39
|
super(new SqliteFormatter());
|
|
40
40
|
this.db = db;
|
|
41
|
-
this.tableData = this.prepare(SqliteSchema.tableData);
|
|
42
|
-
this.indexData = this.prepare(SqliteSchema.indexData);
|
|
43
41
|
}
|
|
44
42
|
tableData;
|
|
45
43
|
indexData;
|
|
@@ -55,6 +53,8 @@ var SqlJsDriver = class extends Driver.Sync {
|
|
|
55
53
|
}
|
|
56
54
|
}
|
|
57
55
|
schemaInstructions(tableName) {
|
|
56
|
+
this.tableData = this.tableData || (this.tableData = this.prepare(SqliteSchema.tableData));
|
|
57
|
+
this.indexData = this.indexData || (this.indexData = this.prepare(SqliteSchema.indexData));
|
|
58
58
|
const columnData = this.tableData(tableName);
|
|
59
59
|
const indexData = this.indexData(tableName);
|
|
60
60
|
return SqliteSchema.createInstructions(columnData, indexData);
|