rado 0.2.24 → 0.2.25
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/Fields.d.ts +2 -2
- package/dist/define/Ops.d.ts +11 -7
- package/dist/define/Ops.js +11 -6
- package/dist/define/Query.d.ts +3 -103
- package/dist/define/Query.js +8 -388
- package/dist/define/Selection.d.ts +2 -2
- package/dist/define/Table.d.ts +7 -7
- package/dist/define/Table.js +3 -3
- package/dist/define/Target.d.ts +6 -6
- package/dist/define/Target.js +8 -8
- 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 +41 -0
- package/dist/define/query/Select.js +190 -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 +10 -0
- package/dist/define/query/Union.js +46 -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/sqlite/SqliteSchema.js +3 -3
- package/package.json +1 -1
package/dist/define/Expr.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { Fields } from './Fields';
|
|
2
2
|
import { OrderBy } from './OrderBy';
|
|
3
3
|
import { ParamData } from './Param';
|
|
4
|
-
import {
|
|
4
|
+
import { QueryData } from './Query';
|
|
5
5
|
import { Selection } from './Selection';
|
|
6
6
|
import { Target } from './Target';
|
|
7
|
+
import { SelectMultiple, SelectSingle } from './query/Select';
|
|
7
8
|
declare const DATA: unique symbol;
|
|
8
9
|
declare const TO_EXPR: unique symbol;
|
|
9
10
|
declare const IS_EXPR: unique symbol;
|
|
@@ -127,13 +128,13 @@ export declare class Expr<T> {
|
|
|
127
128
|
not(): Expr<boolean>;
|
|
128
129
|
or(this: Expr<boolean>, that: EV<boolean>): Expr<boolean>;
|
|
129
130
|
and(this: Expr<boolean>, that: EV<boolean>): Expr<boolean>;
|
|
130
|
-
is(that: EV<T> |
|
|
131
|
+
is(that: EV<T> | SelectSingle<T>): Expr<boolean>;
|
|
131
132
|
isConstant(value: T): boolean;
|
|
132
133
|
isNot(that: EV<T>): Expr<boolean>;
|
|
133
134
|
isNull(): Expr<boolean>;
|
|
134
135
|
isNotNull(): Expr<boolean>;
|
|
135
|
-
isIn(that: EV<Array<T>> |
|
|
136
|
-
isNotIn(that: EV<Array<T>> |
|
|
136
|
+
isIn(that: EV<Array<T>> | SelectMultiple<T>): Expr<boolean>;
|
|
137
|
+
isNotIn(that: EV<Array<T>> | SelectMultiple<T>): Expr<boolean>;
|
|
137
138
|
isGreater(that: EV<any>): Expr<boolean>;
|
|
138
139
|
isGreaterOrEqual(that: EV<any>): Expr<boolean>;
|
|
139
140
|
isLess(that: EV<any>): Expr<boolean>;
|
package/dist/define/Fields.d.ts
CHANGED
|
@@ -4,9 +4,9 @@ type ObjectUnion<T> = {
|
|
|
4
4
|
};
|
|
5
5
|
type RecordField<T> = Expr<T> & FieldsOf<ObjectUnion<T>>;
|
|
6
6
|
type Field<T> = [T] extends [Array<any>] ? Expr<T> : [T] extends [number | string | boolean] ? Expr<T> : [T] extends [Record<string, any> | null] ? RecordField<T> : Expr<T>;
|
|
7
|
-
type FieldsOf<Row> = Row extends Record<string, any> ? {
|
|
7
|
+
type FieldsOf<Row> = [Row] extends [Record<string, any>] ? {
|
|
8
8
|
[K in keyof Row]-?: Field<Row[K]>;
|
|
9
9
|
} : never;
|
|
10
10
|
type IsStrictlyAny<T> = (T extends never ? true : false) extends false ? false : true;
|
|
11
|
-
export type Fields<T> = IsStrictlyAny<T> extends true ? any : T extends object ? FieldsOf<T> : Field<T>;
|
|
11
|
+
export type Fields<T> = IsStrictlyAny<T> extends true ? any : [T] extends [object] ? FieldsOf<T> : Field<T>;
|
|
12
12
|
export {};
|
package/dist/define/Ops.d.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import { Query } from './Query';
|
|
2
1
|
import { Selection } from './Selection';
|
|
3
2
|
import { Table } from './Table';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
export declare function
|
|
3
|
+
import { Batch } from './query/Batch';
|
|
4
|
+
import { Delete } from './query/Delete';
|
|
5
|
+
import { Insert } from './query/Insert';
|
|
6
|
+
import { SelectMultiple } from './query/Select';
|
|
7
|
+
import { Update } from './query/Update';
|
|
8
|
+
export declare function select<X extends Selection>(selection: X): SelectMultiple<Selection.Infer<X>>;
|
|
9
|
+
export declare function from<Row>(source: Table<Row> | SelectMultiple<Row>): SelectMultiple<Row>;
|
|
10
|
+
export declare function update<Row>(table: Table<Row>): Update<Row>;
|
|
11
|
+
export declare function insertInto<Row>(table: Table<Row>): Insert<Row>;
|
|
12
|
+
export declare function deleteFrom<Row>(table: Table<Row>): Delete;
|
|
13
|
+
export declare function create(...tables: Array<Table<any>>): Batch;
|
package/dist/define/Ops.js
CHANGED
|
@@ -4,8 +4,13 @@ import { Query, QueryData } from "./Query.js";
|
|
|
4
4
|
import { Schema } from "./Schema.js";
|
|
5
5
|
import { Table } from "./Table.js";
|
|
6
6
|
import { Target } from "./Target.js";
|
|
7
|
+
import { Batch } from "./query/Batch.js";
|
|
8
|
+
import { Delete } from "./query/Delete.js";
|
|
9
|
+
import { Insert } from "./query/Insert.js";
|
|
10
|
+
import { SelectMultiple } from "./query/Select.js";
|
|
11
|
+
import { Update } from "./query/Update.js";
|
|
7
12
|
function select(selection) {
|
|
8
|
-
return new
|
|
13
|
+
return new SelectMultiple(
|
|
9
14
|
new QueryData.Select({
|
|
10
15
|
selection: ExprData.create(selection)
|
|
11
16
|
})
|
|
@@ -13,7 +18,7 @@ function select(selection) {
|
|
|
13
18
|
}
|
|
14
19
|
function from(source) {
|
|
15
20
|
const target = Query.isQuery(source) ? new Target.Query(source[Query.Data]) : new Target.Table(source[Table.Data]);
|
|
16
|
-
return new
|
|
21
|
+
return new SelectMultiple(
|
|
17
22
|
new QueryData.Select({
|
|
18
23
|
from: target,
|
|
19
24
|
selection: new ExprData.Row(target)
|
|
@@ -21,16 +26,16 @@ function from(source) {
|
|
|
21
26
|
);
|
|
22
27
|
}
|
|
23
28
|
function update(table) {
|
|
24
|
-
return new
|
|
29
|
+
return new Update(new QueryData.Update({ table: table[Table.Data] }));
|
|
25
30
|
}
|
|
26
31
|
function insertInto(table) {
|
|
27
|
-
return new
|
|
32
|
+
return new Insert(table[Table.Data]);
|
|
28
33
|
}
|
|
29
34
|
function deleteFrom(table) {
|
|
30
|
-
return new
|
|
35
|
+
return new Delete(new QueryData.Delete({ table: table[Table.Data] }));
|
|
31
36
|
}
|
|
32
37
|
function create(...tables) {
|
|
33
|
-
return new
|
|
38
|
+
return new Batch(
|
|
34
39
|
tables.flatMap((table) => Schema.create(table[Table.Data]).queries)
|
|
35
40
|
);
|
|
36
41
|
}
|
package/dist/define/Query.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { Driver } from '../lib/Driver';
|
|
2
2
|
import { CompileOptions } from '../lib/Formatter';
|
|
3
3
|
import { ColumnData } from './Column';
|
|
4
|
-
import { EV,
|
|
4
|
+
import { EV, ExprData } from './Expr';
|
|
5
5
|
import { IndexData } from './Index';
|
|
6
6
|
import { OrderBy } from './OrderBy';
|
|
7
7
|
import { Selection } from './Selection';
|
|
8
|
-
import {
|
|
8
|
+
import { TableData } from './Table';
|
|
9
9
|
import { Target } from './Target';
|
|
10
10
|
declare const DATA: unique symbol;
|
|
11
11
|
export declare enum QueryType {
|
|
@@ -139,115 +139,15 @@ export declare class Query<T> {
|
|
|
139
139
|
[Selection.CursorType]: () => T;
|
|
140
140
|
[DATA]: QueryData;
|
|
141
141
|
constructor(query: QueryData);
|
|
142
|
-
static all(strings: TemplateStringsArray, ...params: Array<any>): Query<any>;
|
|
143
142
|
next<T>(cursor: Query<T>): Query<T>;
|
|
144
143
|
on(driver: Driver.Sync): T;
|
|
145
144
|
on(driver: Driver.Async): Promise<T>;
|
|
146
145
|
toSql(driver: Driver, options?: CompileOptions): string;
|
|
147
146
|
toJSON(): QueryData;
|
|
148
|
-
|
|
149
|
-
export declare namespace Query {
|
|
150
|
-
class Delete extends Query<{
|
|
151
|
-
rowsAffected: number;
|
|
152
|
-
}> {
|
|
153
|
-
[DATA]: QueryData.Delete;
|
|
154
|
-
where(...where: Array<EV<boolean>>): Delete;
|
|
155
|
-
take(limit: number | undefined): Delete;
|
|
156
|
-
skip(offset: number | undefined): Delete;
|
|
157
|
-
}
|
|
158
|
-
class Update<Definition> extends Query<{
|
|
159
|
-
rowsAffected: number;
|
|
160
|
-
}> {
|
|
161
|
-
[DATA]: QueryData.Update;
|
|
162
|
-
set(set: Table.Update<Definition>): Update<Definition>;
|
|
163
|
-
where(...where: Array<EV<boolean>>): Update<Definition>;
|
|
164
|
-
take(limit: number | undefined): Update<Definition>;
|
|
165
|
-
skip(offset: number | undefined): Update<Definition>;
|
|
166
|
-
}
|
|
167
|
-
class InsertValuesReturning<T> extends Query<T> {
|
|
168
|
-
}
|
|
169
|
-
class Inserted extends Query<{
|
|
170
|
-
rowsAffected: number;
|
|
171
|
-
}> {
|
|
172
|
-
[DATA]: QueryData.Insert;
|
|
173
|
-
constructor(query: QueryData.Insert);
|
|
174
|
-
returning<X extends Selection>(selection: X): InsertValuesReturning<Selection.Infer<X>>;
|
|
175
|
-
}
|
|
176
|
-
class Insert<Definition> {
|
|
177
|
-
protected into: TableData;
|
|
178
|
-
constructor(into: TableData);
|
|
179
|
-
selection(query: Query.SelectMultiple<Table.Select<Definition>>): Inserted;
|
|
180
|
-
values(...data: Array<Table.Insert<Definition>>): Inserted;
|
|
181
|
-
}
|
|
182
|
-
class CreateTable extends Query<void> {
|
|
183
|
-
protected table: TableData;
|
|
184
|
-
constructor(table: TableData);
|
|
185
|
-
}
|
|
186
|
-
class Batch<T = void> extends Query<T> {
|
|
187
|
-
protected queries: Array<QueryData>;
|
|
188
|
-
[DATA]: QueryData.Batch;
|
|
189
|
-
constructor(queries: Array<QueryData>);
|
|
190
|
-
next<T>(cursor: Query<T>): Query<T>;
|
|
191
|
-
}
|
|
192
|
-
class Union<Row> extends Query<Array<Row>> {
|
|
193
|
-
[DATA]: QueryData.Union | QueryData.Select;
|
|
194
|
-
union(query: SelectMultiple<Row> | Union<Row>): Union<Row>;
|
|
195
|
-
unionAll(query: SelectMultiple<Row> | Union<Row>): Union<Row>;
|
|
196
|
-
except(query: SelectMultiple<Row> | Union<Row>): Union<Row>;
|
|
197
|
-
intersect(query: SelectMultiple<Row> | Union<Row>): Union<Row>;
|
|
198
|
-
}
|
|
199
|
-
class SelectMultiple<Row> extends Union<Row> {
|
|
200
|
-
[DATA]: QueryData.Select;
|
|
201
|
-
constructor(query: QueryData.Select);
|
|
202
|
-
from(table: Table<any>): SelectMultiple<Row>;
|
|
203
|
-
leftJoin<C>(that: Table<C> | TableSelect<C>, ...on: Array<EV<boolean>>): SelectMultiple<Row>;
|
|
204
|
-
innerJoin<C>(that: Table<C> | TableSelect<C>, ...on: Array<EV<boolean>>): SelectMultiple<Row>;
|
|
205
|
-
select<X extends Selection>(selection: X): SelectMultiple<Selection.Infer<X>>;
|
|
206
|
-
count(): SelectSingle<number>;
|
|
207
|
-
orderBy(...orderBy: Array<Expr<any> | OrderBy>): SelectMultiple<Row>;
|
|
208
|
-
groupBy(...groupBy: Array<Expr<any>>): SelectMultiple<Row>;
|
|
209
|
-
maybeFirst(): SelectSingle<Row | undefined>;
|
|
210
|
-
first(): SelectSingle<Row>;
|
|
211
|
-
where(...where: Array<EV<boolean>>): SelectMultiple<Row>;
|
|
212
|
-
take(limit: number | undefined): SelectMultiple<Row>;
|
|
213
|
-
skip(offset: number | undefined): SelectMultiple<Row>;
|
|
214
|
-
recursiveUnion<T extends {}>(this: SelectMultiple<T>, create: (fields: Record<string, Table.Of<T>>) => SelectMultiple<T> | Union<T>): SelectMultiple<T>;
|
|
215
|
-
recursiveUnionAll<T extends {}>(this: SelectMultiple<T>, create: (fields: Record<string, Table.Of<T>>) => SelectMultiple<T> | Union<T>): SelectMultiple<T>;
|
|
216
|
-
[Expr.ToExpr](): Expr<Row>;
|
|
217
|
-
}
|
|
218
|
-
class TableSelect<Definition> extends SelectMultiple<Table.Select<Definition>> {
|
|
219
|
-
protected table: TableData;
|
|
220
|
-
[DATA]: QueryData.Select;
|
|
221
|
-
constructor(table: TableData, conditions?: Array<EV<boolean>>);
|
|
222
|
-
as(alias: string): Table<Definition>;
|
|
223
|
-
create(): CreateTable;
|
|
224
|
-
insertSelect(query: SelectMultiple<Table.Insert<Definition>>): Inserted;
|
|
225
|
-
insertOne(record: Table.Insert<Definition>): Query<Table.Select<Definition>>;
|
|
226
|
-
insertAll(data: Array<Table.Insert<Definition>>): Inserted;
|
|
227
|
-
set(data: Table.Update<Definition>): Update<Definition>;
|
|
228
|
-
delete(): Delete;
|
|
229
|
-
get(name: string): Expr<any>;
|
|
230
|
-
}
|
|
231
|
-
class SelectSingle<Row> extends Query<Row> {
|
|
232
|
-
[DATA]: QueryData.Select;
|
|
233
|
-
constructor(query: QueryData.Select);
|
|
234
|
-
from(table: Table<any>): SelectMultiple<Row>;
|
|
235
|
-
leftJoin<C>(that: Table<C> | TableSelect<C>, ...on: Array<EV<boolean>>): SelectSingle<Row>;
|
|
236
|
-
innerJoin<C>(that: Table<C> | TableSelect<C>, ...on: Array<EV<boolean>>): SelectSingle<Row>;
|
|
237
|
-
select<X extends Selection>(selection: X): SelectSingle<Selection.Infer<X>>;
|
|
238
|
-
orderBy(...orderBy: Array<OrderBy>): SelectSingle<Row>;
|
|
239
|
-
groupBy(...groupBy: Array<Expr<any>>): SelectSingle<Row>;
|
|
240
|
-
where(...where: Array<EV<boolean>>): SelectSingle<Row>;
|
|
241
|
-
take(limit: number | undefined): SelectSingle<Row>;
|
|
242
|
-
skip(offset: number | undefined): SelectSingle<Row>;
|
|
243
|
-
all(): SelectMultiple<Row>;
|
|
244
|
-
[Expr.ToExpr](): Expr<Row>;
|
|
245
|
-
}
|
|
147
|
+
protected addWhere(where: Array<EV<boolean>>): this[typeof DATA];
|
|
246
148
|
}
|
|
247
149
|
export declare namespace Query {
|
|
248
150
|
const Data: typeof DATA;
|
|
249
151
|
function isQuery<T>(input: any): input is Query<T>;
|
|
250
|
-
function isSingle<T>(input: any): input is Query.SelectSingle<T>;
|
|
251
|
-
function isMultiple<T>(input: any): input is Query.SelectMultiple<T>;
|
|
252
152
|
}
|
|
253
153
|
export {};
|
package/dist/define/Query.js
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
1
|
// src/define/Query.ts
|
|
2
|
-
import {
|
|
3
|
-
import { Expr, ExprData } from "./Expr.js";
|
|
4
|
-
import { Functions } from "./Functions.js";
|
|
5
|
-
import { Schema } from "./Schema.js";
|
|
6
|
-
import { createTable, Table } from "./Table.js";
|
|
7
|
-
import { Target } from "./Target.js";
|
|
2
|
+
import { Expr } from "./Expr.js";
|
|
8
3
|
var { assign } = Object;
|
|
9
4
|
var DATA = Symbol("Query.Data");
|
|
10
5
|
var QueryType = /* @__PURE__ */ ((QueryType2) => {
|
|
@@ -109,11 +104,6 @@ var Query = class {
|
|
|
109
104
|
constructor(query) {
|
|
110
105
|
this[DATA] = query;
|
|
111
106
|
}
|
|
112
|
-
static all(strings, ...params) {
|
|
113
|
-
return new Query(
|
|
114
|
-
new QueryData.Raw({ expectedReturn: "rows", strings, params })
|
|
115
|
-
);
|
|
116
|
-
}
|
|
117
107
|
next(cursor) {
|
|
118
108
|
return new Query(
|
|
119
109
|
new QueryData.Batch(
|
|
@@ -132,390 +122,20 @@ var Query = class {
|
|
|
132
122
|
toJSON() {
|
|
133
123
|
return this[DATA];
|
|
134
124
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
}
|
|
142
|
-
((Query2) => {
|
|
143
|
-
class Delete extends Query2 {
|
|
144
|
-
where(...where) {
|
|
145
|
-
return new Delete(this[DATA].with({ where: addWhere(this[DATA], where) }));
|
|
146
|
-
}
|
|
147
|
-
take(limit) {
|
|
148
|
-
return new Delete(this[DATA].with({ limit }));
|
|
149
|
-
}
|
|
150
|
-
skip(offset) {
|
|
151
|
-
return new Delete(this[DATA].with({ offset }));
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
Query2.Delete = Delete;
|
|
155
|
-
class Update extends Query2 {
|
|
156
|
-
set(set) {
|
|
157
|
-
return new Update(this[DATA].with({ set }));
|
|
158
|
-
}
|
|
159
|
-
where(...where) {
|
|
160
|
-
return new Update(this[DATA].with({ where: addWhere(this[DATA], where) }));
|
|
161
|
-
}
|
|
162
|
-
take(limit) {
|
|
163
|
-
return new Update(this[DATA].with({ limit }));
|
|
164
|
-
}
|
|
165
|
-
skip(offset) {
|
|
166
|
-
return new Update(this[DATA].with({ offset }));
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
Query2.Update = Update;
|
|
170
|
-
class InsertValuesReturning extends Query2 {
|
|
171
|
-
}
|
|
172
|
-
Query2.InsertValuesReturning = InsertValuesReturning;
|
|
173
|
-
class Inserted extends Query2 {
|
|
174
|
-
constructor(query) {
|
|
175
|
-
super(query);
|
|
176
|
-
}
|
|
177
|
-
returning(selection) {
|
|
178
|
-
return new InsertValuesReturning(
|
|
179
|
-
new QueryData.Insert({
|
|
180
|
-
...this[DATA],
|
|
181
|
-
selection: ExprData.create(selection)
|
|
182
|
-
})
|
|
183
|
-
);
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
Query2.Inserted = Inserted;
|
|
187
|
-
class Insert {
|
|
188
|
-
constructor(into) {
|
|
189
|
-
this.into = into;
|
|
190
|
-
}
|
|
191
|
-
selection(query) {
|
|
192
|
-
return new Inserted(
|
|
193
|
-
new QueryData.Insert({ into: this.into, select: query[DATA] })
|
|
194
|
-
);
|
|
195
|
-
}
|
|
196
|
-
values(...data) {
|
|
197
|
-
return new Inserted(new QueryData.Insert({ into: this.into, data }));
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
Query2.Insert = Insert;
|
|
201
|
-
class CreateTable extends Query2 {
|
|
202
|
-
constructor(table) {
|
|
203
|
-
super(Schema.create(table));
|
|
204
|
-
this.table = table;
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
Query2.CreateTable = CreateTable;
|
|
208
|
-
class Batch extends Query2 {
|
|
209
|
-
constructor(queries) {
|
|
210
|
-
super(new QueryData.Batch({ queries }));
|
|
211
|
-
this.queries = queries;
|
|
212
|
-
}
|
|
213
|
-
next(cursor) {
|
|
214
|
-
return new Query2(
|
|
215
|
-
new QueryData.Batch({
|
|
216
|
-
queries: [...this[DATA].queries, cursor[DATA]]
|
|
217
|
-
})
|
|
218
|
-
);
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
Query2.Batch = Batch;
|
|
222
|
-
class Union extends Query2 {
|
|
223
|
-
union(query) {
|
|
224
|
-
return new Union(
|
|
225
|
-
new QueryData.Union({
|
|
226
|
-
a: this[DATA],
|
|
227
|
-
operator: "Union" /* Union */,
|
|
228
|
-
b: query[DATA]
|
|
229
|
-
})
|
|
230
|
-
);
|
|
231
|
-
}
|
|
232
|
-
unionAll(query) {
|
|
233
|
-
return new Union(
|
|
234
|
-
new QueryData.Union({
|
|
235
|
-
a: this[DATA],
|
|
236
|
-
operator: "UnionAll" /* UnionAll */,
|
|
237
|
-
b: query[DATA]
|
|
238
|
-
})
|
|
239
|
-
);
|
|
240
|
-
}
|
|
241
|
-
except(query) {
|
|
242
|
-
return new Union(
|
|
243
|
-
new QueryData.Union({
|
|
244
|
-
a: this[DATA],
|
|
245
|
-
operator: "Except" /* Except */,
|
|
246
|
-
b: query[DATA]
|
|
247
|
-
})
|
|
248
|
-
);
|
|
249
|
-
}
|
|
250
|
-
intersect(query) {
|
|
251
|
-
return new Union(
|
|
252
|
-
new QueryData.Union({
|
|
253
|
-
a: this[DATA],
|
|
254
|
-
operator: "Intersect" /* Intersect */,
|
|
255
|
-
b: query[DATA]
|
|
256
|
-
})
|
|
257
|
-
);
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
Query2.Union = Union;
|
|
261
|
-
function joinTarget(joinType, query, to, on) {
|
|
262
|
-
const toQuery = Query2.isQuery(to) ? to[DATA] : void 0;
|
|
263
|
-
const target = toQuery ? toQuery.from : new Target.Table(to[Table.Data]);
|
|
264
|
-
if (!query.from || !target)
|
|
265
|
-
throw new Error("No from clause");
|
|
266
|
-
const conditions = [...on];
|
|
267
|
-
if (toQuery) {
|
|
268
|
-
const where = toQuery.where;
|
|
269
|
-
if (where)
|
|
270
|
-
conditions.push(new Expr(where));
|
|
271
|
-
}
|
|
272
|
-
return new Target.Join(
|
|
273
|
-
query.from,
|
|
274
|
-
target,
|
|
275
|
-
joinType,
|
|
276
|
-
Expr.and(...conditions)[Expr.Data]
|
|
277
|
-
);
|
|
278
|
-
}
|
|
279
|
-
class SelectMultiple extends Union {
|
|
280
|
-
constructor(query) {
|
|
281
|
-
super(query);
|
|
282
|
-
}
|
|
283
|
-
from(table) {
|
|
284
|
-
return new SelectMultiple(
|
|
285
|
-
this[DATA].with({ from: new Target.Table(table) })
|
|
286
|
-
);
|
|
287
|
-
}
|
|
288
|
-
leftJoin(that, ...on) {
|
|
289
|
-
const query = this[DATA];
|
|
290
|
-
return new SelectMultiple(
|
|
291
|
-
this[DATA].with({
|
|
292
|
-
from: joinTarget("left", query, that, on)
|
|
293
|
-
})
|
|
294
|
-
);
|
|
295
|
-
}
|
|
296
|
-
innerJoin(that, ...on) {
|
|
297
|
-
const query = this[DATA];
|
|
298
|
-
return new SelectMultiple(
|
|
299
|
-
this[DATA].with({
|
|
300
|
-
from: joinTarget("inner", query, that, on)
|
|
301
|
-
})
|
|
302
|
-
);
|
|
303
|
-
}
|
|
304
|
-
select(selection) {
|
|
305
|
-
return new SelectMultiple(
|
|
306
|
-
this[DATA].with({
|
|
307
|
-
selection: ExprData.create(selection)
|
|
308
|
-
})
|
|
309
|
-
);
|
|
310
|
-
}
|
|
311
|
-
count() {
|
|
312
|
-
return new SelectSingle(
|
|
313
|
-
this[DATA].with({
|
|
314
|
-
selection: Functions.count()[Expr.Data],
|
|
315
|
-
singleResult: true
|
|
316
|
-
})
|
|
317
|
-
);
|
|
318
|
-
}
|
|
319
|
-
orderBy(...orderBy) {
|
|
320
|
-
return new SelectMultiple(
|
|
321
|
-
this[DATA].with({
|
|
322
|
-
orderBy: orderBy.map((e) => {
|
|
323
|
-
return Expr.isExpr(e) ? e.asc() : e;
|
|
324
|
-
})
|
|
325
|
-
})
|
|
326
|
-
);
|
|
327
|
-
}
|
|
328
|
-
groupBy(...groupBy) {
|
|
329
|
-
return new SelectMultiple(
|
|
330
|
-
this[DATA].with({
|
|
331
|
-
groupBy: groupBy.map(ExprData.create)
|
|
332
|
-
})
|
|
333
|
-
);
|
|
334
|
-
}
|
|
335
|
-
maybeFirst() {
|
|
336
|
-
return new SelectSingle(this[DATA].with({ singleResult: true }));
|
|
337
|
-
}
|
|
338
|
-
first() {
|
|
339
|
-
return new SelectSingle(
|
|
340
|
-
this[DATA].with({
|
|
341
|
-
singleResult: true,
|
|
342
|
-
validate: true
|
|
343
|
-
})
|
|
344
|
-
);
|
|
345
|
-
}
|
|
346
|
-
where(...where) {
|
|
347
|
-
return new SelectMultiple(
|
|
348
|
-
this[DATA].with({
|
|
349
|
-
where: addWhere(this[DATA], where)
|
|
350
|
-
})
|
|
351
|
-
);
|
|
352
|
-
}
|
|
353
|
-
take(limit) {
|
|
354
|
-
return new SelectMultiple(this[DATA].with({ limit }));
|
|
355
|
-
}
|
|
356
|
-
skip(offset) {
|
|
357
|
-
return new SelectMultiple(this[DATA].with({ offset }));
|
|
358
|
-
}
|
|
359
|
-
recursiveUnion(create) {
|
|
360
|
-
return new SelectMultiple(
|
|
361
|
-
makeRecursiveUnion(this[DATA], create, "Union" /* Union */)
|
|
362
|
-
);
|
|
363
|
-
}
|
|
364
|
-
recursiveUnionAll(create) {
|
|
365
|
-
return new SelectMultiple(
|
|
366
|
-
makeRecursiveUnion(
|
|
367
|
-
this[DATA],
|
|
368
|
-
create,
|
|
369
|
-
"UnionAll" /* UnionAll */
|
|
370
|
-
)
|
|
371
|
-
);
|
|
372
|
-
}
|
|
373
|
-
[Expr.ToExpr]() {
|
|
374
|
-
return new Expr(new ExprData.Query(this[DATA]));
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
Query2.SelectMultiple = SelectMultiple;
|
|
378
|
-
class TableSelect extends SelectMultiple {
|
|
379
|
-
constructor(table, conditions = []) {
|
|
380
|
-
const target = new Target.Table(table);
|
|
381
|
-
super(
|
|
382
|
-
new QueryData.Select({
|
|
383
|
-
from: target,
|
|
384
|
-
selection: new ExprData.Row(target),
|
|
385
|
-
where: Expr.and(...conditions)[Expr.Data]
|
|
386
|
-
})
|
|
387
|
-
);
|
|
388
|
-
this.table = table;
|
|
389
|
-
}
|
|
390
|
-
as(alias) {
|
|
391
|
-
return createTable({ ...this.table, alias });
|
|
392
|
-
}
|
|
393
|
-
create() {
|
|
394
|
-
return new Query2.CreateTable(this.table);
|
|
395
|
-
}
|
|
396
|
-
insertSelect(query) {
|
|
397
|
-
return new Query2.Inserted(
|
|
398
|
-
new QueryData.Insert({ into: this.table, select: query[DATA] })
|
|
399
|
-
);
|
|
400
|
-
}
|
|
401
|
-
insertOne(record) {
|
|
402
|
-
return new Query2(
|
|
403
|
-
new QueryData.Insert({
|
|
404
|
-
into: this.table,
|
|
405
|
-
data: [record],
|
|
406
|
-
selection: new ExprData.Row(new Target.Table(this.table)),
|
|
407
|
-
singleResult: true
|
|
408
|
-
})
|
|
409
|
-
);
|
|
410
|
-
}
|
|
411
|
-
insertAll(data) {
|
|
412
|
-
return new Query2.Insert(this.table).values(...data);
|
|
413
|
-
}
|
|
414
|
-
set(data) {
|
|
415
|
-
return new Query2.Update(
|
|
416
|
-
new QueryData.Update({
|
|
417
|
-
table: this.table,
|
|
418
|
-
where: this[DATA].where
|
|
419
|
-
})
|
|
420
|
-
).set(data);
|
|
421
|
-
}
|
|
422
|
-
delete() {
|
|
423
|
-
return new Query2.Delete(
|
|
424
|
-
new QueryData.Delete({
|
|
425
|
-
table: this.table,
|
|
426
|
-
where: this[DATA].where
|
|
427
|
-
})
|
|
428
|
-
);
|
|
429
|
-
}
|
|
430
|
-
get(name) {
|
|
431
|
-
return new Expr(
|
|
432
|
-
new ExprData.Field(new ExprData.Row(new Target.Table(this.table)), name)
|
|
433
|
-
);
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
Query2.TableSelect = TableSelect;
|
|
437
|
-
class SelectSingle extends Query2 {
|
|
438
|
-
constructor(query) {
|
|
439
|
-
super(query);
|
|
440
|
-
}
|
|
441
|
-
from(table) {
|
|
442
|
-
return new SelectMultiple(
|
|
443
|
-
this[DATA].with({ from: new Target.Table(table) })
|
|
444
|
-
);
|
|
445
|
-
}
|
|
446
|
-
leftJoin(that, ...on) {
|
|
447
|
-
const query = this[DATA];
|
|
448
|
-
return new SelectSingle(
|
|
449
|
-
this[DATA].with({
|
|
450
|
-
from: joinTarget("left", query, that, on)
|
|
451
|
-
})
|
|
452
|
-
);
|
|
453
|
-
}
|
|
454
|
-
innerJoin(that, ...on) {
|
|
455
|
-
const query = this[DATA];
|
|
456
|
-
return new SelectSingle(
|
|
457
|
-
this[DATA].with({
|
|
458
|
-
from: joinTarget("inner", query, that, on)
|
|
459
|
-
})
|
|
460
|
-
);
|
|
461
|
-
}
|
|
462
|
-
select(selection) {
|
|
463
|
-
return new SelectSingle(
|
|
464
|
-
this[DATA].with({
|
|
465
|
-
selection: ExprData.create(selection)
|
|
466
|
-
})
|
|
467
|
-
);
|
|
468
|
-
}
|
|
469
|
-
orderBy(...orderBy) {
|
|
470
|
-
return new SelectSingle(
|
|
471
|
-
this[DATA].with({
|
|
472
|
-
orderBy
|
|
473
|
-
})
|
|
474
|
-
);
|
|
475
|
-
}
|
|
476
|
-
groupBy(...groupBy) {
|
|
477
|
-
return new SelectSingle(
|
|
478
|
-
this[DATA].with({
|
|
479
|
-
groupBy: groupBy.map(ExprData.create)
|
|
480
|
-
})
|
|
481
|
-
);
|
|
482
|
-
}
|
|
483
|
-
where(...where) {
|
|
484
|
-
return new SelectSingle(
|
|
485
|
-
this[DATA].with({
|
|
486
|
-
where: addWhere(this[DATA], where)
|
|
487
|
-
})
|
|
488
|
-
);
|
|
489
|
-
}
|
|
490
|
-
take(limit) {
|
|
491
|
-
return new SelectSingle(this[DATA].with({ limit }));
|
|
492
|
-
}
|
|
493
|
-
skip(offset) {
|
|
494
|
-
return new SelectSingle(this[DATA].with({ offset }));
|
|
495
|
-
}
|
|
496
|
-
all() {
|
|
497
|
-
return new SelectMultiple(this[DATA].with({ singleResult: false }));
|
|
498
|
-
}
|
|
499
|
-
[Expr.ToExpr]() {
|
|
500
|
-
return new Expr(new ExprData.Query(this[DATA]));
|
|
501
|
-
}
|
|
125
|
+
addWhere(where) {
|
|
126
|
+
const query = this[DATA];
|
|
127
|
+
const conditions = where.slice();
|
|
128
|
+
if (query.where)
|
|
129
|
+
conditions.push(new Expr(query.where));
|
|
130
|
+
return query.with({ where: Expr.and(...conditions)[Expr.Data] });
|
|
502
131
|
}
|
|
503
|
-
|
|
504
|
-
})(Query || (Query = {}));
|
|
132
|
+
};
|
|
505
133
|
((Query2) => {
|
|
506
134
|
Query2.Data = DATA;
|
|
507
135
|
function isQuery(input) {
|
|
508
136
|
return input !== null && Boolean(DATA in input);
|
|
509
137
|
}
|
|
510
138
|
Query2.isQuery = isQuery;
|
|
511
|
-
function isSingle(input) {
|
|
512
|
-
return input instanceof Query2.SelectSingle;
|
|
513
|
-
}
|
|
514
|
-
Query2.isSingle = isSingle;
|
|
515
|
-
function isMultiple(input) {
|
|
516
|
-
return input instanceof Query2.SelectSingle;
|
|
517
|
-
}
|
|
518
|
-
Query2.isMultiple = isMultiple;
|
|
519
139
|
})(Query || (Query = {}));
|
|
520
140
|
export {
|
|
521
141
|
Query,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Expr } from './Expr';
|
|
2
|
-
import type {
|
|
3
|
-
type SelectionBase = unknown | (() => any) | Expr<any> |
|
|
2
|
+
import type { SelectMultiple, SelectSingle } from './query/Select';
|
|
3
|
+
type SelectionBase = unknown | (() => any) | Expr<any> | SelectMultiple<any> | SelectSingle<any>;
|
|
4
4
|
interface SelectionRecord extends Record<string, Selection> {
|
|
5
5
|
}
|
|
6
6
|
export type Selection = SelectionBase | SelectionRecord;
|