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