rado 1.0.15 → 1.2.1
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 +2 -2
- package/dist/compat.d.ts +3 -3
- package/dist/compat.js +2 -0
- package/dist/core/Builder.d.ts +14 -9
- package/dist/core/Builder.js +24 -32
- package/dist/core/Column.d.ts +26 -8
- package/dist/core/Column.js +62 -14
- package/dist/core/Constraint.d.ts +16 -5
- package/dist/core/Constraint.js +17 -6
- package/dist/core/Database.d.ts +11 -4
- package/dist/core/Database.js +20 -4
- package/dist/core/Dialect.js +2 -2
- package/dist/core/Driver.d.ts +3 -3
- package/dist/core/Emitter.d.ts +7 -29
- package/dist/core/Emitter.js +31 -139
- package/dist/core/Index.d.ts +1 -1
- package/dist/core/Internal.d.ts +5 -4
- package/dist/core/Queries.d.ts +46 -0
- package/dist/core/{Query.js → Queries.js} +10 -8
- package/dist/core/Resolver.d.ts +2 -2
- package/dist/core/Selection.d.ts +7 -10
- package/dist/core/Selection.js +15 -13
- package/dist/core/Sql.d.ts +8 -29
- package/dist/core/Sql.js +67 -118
- package/dist/core/Table.d.ts +7 -3
- package/dist/core/Table.js +24 -8
- package/dist/core/Virtual.d.ts +1 -1
- package/dist/core/Virtual.js +15 -14
- package/dist/core/expr/Conditions.d.ts +4 -4
- package/dist/core/expr/Conditions.js +18 -8
- package/dist/core/expr/Field.d.ts +8 -6
- package/dist/core/expr/Field.js +3 -3
- package/dist/core/expr/Include.d.ts +10 -7
- package/dist/core/expr/Include.js +29 -12
- package/dist/core/expr/Input.d.ts +3 -1
- package/dist/core/expr/Input.js +24 -7
- package/dist/core/expr/Json.js +2 -2
- package/dist/core/query/CTE.d.ts +8 -0
- package/dist/core/query/CTE.js +33 -0
- package/dist/core/query/Delete.d.ts +14 -13
- package/dist/core/query/Delete.js +32 -6
- package/dist/core/query/Insert.d.ts +17 -36
- package/dist/core/query/Insert.js +121 -72
- package/dist/core/query/Query.d.ts +124 -0
- package/dist/core/query/Select.d.ts +74 -33
- package/dist/core/query/Select.js +255 -57
- package/dist/core/query/Shared.d.ts +3 -0
- package/dist/core/query/Shared.js +23 -0
- package/dist/core/query/Update.d.ts +13 -13
- package/dist/core/query/Update.js +51 -22
- package/dist/driver/d1.d.ts +2 -2
- package/dist/driver/libsql.d.ts +2 -2
- package/dist/driver/mysql2.d.ts +2 -2
- package/dist/driver/pg.d.ts +2 -2
- package/dist/driver/pglite.d.ts +2 -2
- package/dist/index.d.ts +7 -7
- package/dist/index.js +7 -7
- package/dist/mysql/columns.d.ts +37 -37
- package/dist/mysql/columns.js +32 -15
- package/dist/mysql/dialect.js +6 -7
- package/dist/mysql/diff.js +2 -1
- package/dist/mysql.d.ts +3 -2
- package/dist/mysql.js +11 -2
- package/dist/postgres/columns.d.ts +13 -13
- package/dist/postgres/columns.js +15 -6
- package/dist/postgres/dialect.js +6 -7
- package/dist/postgres/diff.js +2 -1
- package/dist/postgres.d.ts +3 -2
- package/dist/postgres.js +11 -2
- package/dist/sqlite/columns.d.ts +16 -1
- package/dist/sqlite/columns.js +36 -3
- package/dist/sqlite/dialect.js +6 -6
- package/dist/sqlite/diff.js +3 -5
- package/dist/sqlite.d.ts +3 -2
- package/dist/sqlite.js +11 -2
- package/dist/universal/columns.js +34 -18
- package/dist/universal/functions.js +4 -1
- package/package.json +4 -4
- package/dist/core/Join.d.ts +0 -1
- package/dist/core/Query.d.ts +0 -46
- package/dist/core/query/Union.d.ts +0 -35
- package/dist/core/query/Union.js +0 -107
- /package/dist/core/{Join.js → query/Query.js} +0 -0
|
@@ -1,68 +1,89 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
import type { QueryMeta } from '../MetaData.js';
|
|
1
|
+
import { type HasQuery, type HasSelection, type HasSql, type HasTarget, internalData, internalQuery, internalSelection, internalSql } from '../Internal.js';
|
|
2
|
+
import type { IsMysql, IsPostgres, QueryMeta } from '../MetaData.js';
|
|
3
|
+
import { type QueryData, SingleQuery } from '../Queries.js';
|
|
3
4
|
import { type IsNullable, type MakeNullable, type Selection, type SelectionRecord, type SelectionRow } from '../Selection.js';
|
|
4
|
-
import {
|
|
5
|
+
import { Sql } from '../Sql.js';
|
|
5
6
|
import type { Table, TableDefinition, TableFields } from '../Table.js';
|
|
6
7
|
import type { Expand } from '../Types.js';
|
|
7
|
-
import type { Field } from '../expr/Field.js';
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
orderBy?: HasSql;
|
|
19
|
-
limit?: HasSql;
|
|
20
|
-
offset?: HasSql;
|
|
8
|
+
import type { Field, StripFieldMeta } from '../expr/Field.js';
|
|
9
|
+
import type { Input as UserInput } from '../expr/Input.js';
|
|
10
|
+
import { type CTE } from './CTE.js';
|
|
11
|
+
import type { CompoundSelect, SelectQuery, UnionQuery } from './Query.js';
|
|
12
|
+
type UnionTarget<Input, Meta extends QueryMeta> = UnionBase<Input, Meta> | ((self: Input & HasTarget) => UnionBase<Input, Meta>);
|
|
13
|
+
export declare class SelectFirst<Input, Meta extends QueryMeta = QueryMeta> extends SingleQuery<SelectionRow<Input>, Meta> implements HasQuery<SelectionRow<Input>> {
|
|
14
|
+
readonly [internalData]: QueryData<Meta> & SelectQuery;
|
|
15
|
+
constructor(data: QueryData<Meta> & SelectQuery);
|
|
16
|
+
get [internalSelection](): Selection;
|
|
17
|
+
get [internalQuery](): Sql<SelectionRow<Input>>;
|
|
18
|
+
get [internalSql](): Sql<SelectionRow<Input>>;
|
|
21
19
|
}
|
|
22
|
-
export declare class
|
|
20
|
+
export declare abstract class UnionBase<Input, Meta extends QueryMeta = QueryMeta> extends SingleQuery<Array<SelectionRow<Input>>, Meta> implements HasSelection {
|
|
23
21
|
#private;
|
|
24
|
-
readonly [internalData]:
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
22
|
+
readonly [internalData]: QueryData<Meta>;
|
|
23
|
+
abstract [internalSelection]: Selection;
|
|
24
|
+
constructor(data: QueryData<Meta> & {
|
|
25
|
+
compound: CompoundSelect;
|
|
26
|
+
});
|
|
27
|
+
as<Name extends string>(alias: Name): SubQuery<Input, Name>;
|
|
28
|
+
union(target: UnionTarget<Input, Meta>): Union<Input, Meta>;
|
|
29
|
+
unionAll(target: UnionTarget<Input, Meta>): Union<Input, Meta>;
|
|
30
|
+
intersect(target: UnionTarget<Input, Meta>): Union<Input, Meta>;
|
|
31
|
+
intersectAll<Meta extends IsPostgres | IsMysql>(this: UnionBase<Input, Meta>, target: UnionTarget<Input, Meta>): Union<Input, Meta>;
|
|
32
|
+
except(target: UnionTarget<Input, Meta>): Union<Input, Meta>;
|
|
33
|
+
exceptAll<Meta extends IsPostgres | IsMysql>(this: UnionBase<Input, Meta>, target: UnionTarget<Input, Meta>): Union<Input, Meta>;
|
|
34
|
+
}
|
|
35
|
+
export declare class Select<Input, Meta extends QueryMeta = QueryMeta> extends UnionBase<StripFieldMeta<Input>, Meta> implements HasSelection, SelectBase<Input, Meta>, HasQuery<Array<SelectionRow<Input>>> {
|
|
36
|
+
#private;
|
|
37
|
+
readonly [internalData]: QueryData<Meta> & SelectQuery;
|
|
38
|
+
constructor(data: QueryData<Meta> & SelectQuery);
|
|
39
|
+
from(from: HasTarget | Sql): Select<Input, Meta>;
|
|
40
|
+
leftJoin(leftJoin: HasTarget | Sql, on: HasSql<boolean>): Select<Input, Meta>;
|
|
41
|
+
rightJoin(rightJoin: HasTarget | Sql, on: HasSql<boolean>): Select<Input, Meta>;
|
|
42
|
+
innerJoin(innerJoin: HasTarget | Sql, on: HasSql<boolean>): Select<Input, Meta>;
|
|
43
|
+
fullJoin(fullJoin: HasTarget | Sql, on: HasSql<boolean>): Select<Input, Meta>;
|
|
44
|
+
crossJoin(crossJoin: HasTarget | Sql, on: HasSql<boolean>): Select<Input, Meta>;
|
|
32
45
|
where(...where: Array<HasSql<boolean> | undefined>): Select<Input, Meta>;
|
|
33
|
-
groupBy(...
|
|
46
|
+
groupBy(...groupBy: Array<HasSql>): Select<Input, Meta>;
|
|
34
47
|
having(having: HasSql<boolean> | ((self: Input) => HasSql<boolean>)): Select<Input, Meta>;
|
|
35
|
-
orderBy(...
|
|
48
|
+
orderBy(...orderBy: Array<HasSql>): Select<Input, Meta>;
|
|
36
49
|
limit(limit: UserInput<number>): Select<Input, Meta>;
|
|
37
50
|
offset(offset: UserInput<number>): Select<Input, Meta>;
|
|
51
|
+
$dynamic(): this;
|
|
52
|
+
$first(): SelectFirst<Input, Meta>;
|
|
38
53
|
get [internalSelection](): Selection;
|
|
39
|
-
get [internalQuery](): Sql
|
|
54
|
+
get [internalQuery](): Sql<Array<SelectionRow<Input>>>;
|
|
40
55
|
get [internalSql](): Sql<SelectionRow<Input>>;
|
|
41
56
|
}
|
|
42
|
-
export type SubQuery<Input> = Input & HasTarget;
|
|
43
|
-
export interface SelectBase<Input, Meta extends QueryMeta = QueryMeta> extends UnionBase<Input
|
|
57
|
+
export type SubQuery<Input, Name extends string = string> = Input & HasTarget<Name> & HasSelection;
|
|
58
|
+
export interface SelectBase<Input, Meta extends QueryMeta = QueryMeta> extends UnionBase<StripFieldMeta<Input>, Meta>, HasSql<SelectionRow<Input>> {
|
|
44
59
|
where(...where: Array<HasSql<boolean> | undefined>): Select<Input, Meta>;
|
|
45
60
|
groupBy(...exprs: Array<HasSql>): Select<Input, Meta>;
|
|
46
61
|
having(having: HasSql<boolean>): Select<Input, Meta>;
|
|
47
62
|
orderBy(...exprs: Array<HasSql>): Select<Input, Meta>;
|
|
48
63
|
limit(limit: UserInput<number>): Select<Input, Meta>;
|
|
49
64
|
offset(offset: UserInput<number>): Select<Input, Meta>;
|
|
50
|
-
|
|
65
|
+
$dynamic(): this;
|
|
51
66
|
}
|
|
52
67
|
export interface WithoutSelection<Meta extends QueryMeta> {
|
|
53
68
|
from<Definition extends TableDefinition, Name extends string>(from: Table<Definition, Name>): AllFrom<TableFields<Definition>, Meta, Record<Name, TableFields<Definition>>>;
|
|
54
69
|
from<Input>(from: SubQuery<Input>): SelectionFrom<Input, Meta>;
|
|
70
|
+
from<Input>(from: CTE<Input>): SelectionFrom<Input, Meta>;
|
|
55
71
|
}
|
|
56
72
|
export interface WithSelection<Input, Meta extends QueryMeta> extends SelectBase<Input, Meta>, HasSql<SelectionRow<Input>> {
|
|
57
73
|
from<Definition extends TableDefinition, Name extends string>(from: Table<Definition, Name>): SelectionFrom<Input, Meta>;
|
|
74
|
+
from(from: HasTarget): SelectionFrom<Input, Meta>;
|
|
58
75
|
from(from: SubQuery<unknown>): SelectionFrom<Input, Meta>;
|
|
59
|
-
from(
|
|
76
|
+
from(from: HasSql): Select<Input, Meta>;
|
|
60
77
|
}
|
|
61
78
|
export interface AllFrom<Input, Meta extends QueryMeta, Tables = Input> extends SelectBase<Input, Meta> {
|
|
62
79
|
leftJoin<Definition extends TableDefinition, Name extends string>(right: Table<Definition, Name>, on: HasSql<boolean>): AllFrom<Expand<Tables & MakeNullable<Record<Name, TableFields<Definition>>>>, Meta>;
|
|
80
|
+
leftJoin<Input, Name extends string>(right: SubQuery<Input, Name>, on: HasSql<boolean>): AllFrom<Expand<Tables & MakeNullable<Record<Name, Input>>>, Meta>;
|
|
63
81
|
rightJoin<Definition extends TableDefinition, Name extends string>(right: Table<Definition, Name>, on: HasSql<boolean>): AllFrom<Expand<MakeNullable<Tables> & Record<Name, TableFields<Definition>>>, Meta>;
|
|
82
|
+
rightJoin<Input, Name extends string>(right: SubQuery<Input, Name>, on: HasSql<boolean>): AllFrom<Expand<MakeNullable<Tables> & Record<Name, Input>>, Meta>;
|
|
64
83
|
innerJoin<Definition extends TableDefinition, Name extends string>(right: Table<Definition, Name>, on: HasSql<boolean>): AllFrom<Expand<Tables & Record<Name, TableFields<Definition>>>, Meta>;
|
|
84
|
+
innerJoin<Input, Name extends string>(right: SubQuery<Input, Name>, on: HasSql<boolean>): AllFrom<Expand<Tables & Record<Name, Input>>, Meta>;
|
|
65
85
|
fullJoin<Definition extends TableDefinition, Name extends string>(right: Table<Definition, Name>, on: HasSql<boolean>): AllFrom<Expand<MakeNullable<Tables> & MakeNullable<Record<Name, TableFields<Definition>>>>, Meta>;
|
|
86
|
+
fullJoin<Input, Name extends string>(right: SubQuery<Input, Name>, on: HasSql<boolean>): AllFrom<Expand<MakeNullable<Tables> & MakeNullable<Record<Name, Input>>>, Meta>;
|
|
66
87
|
}
|
|
67
88
|
type MarkFieldsAsNullable<Input, TableName extends string> = Expand<{
|
|
68
89
|
[K in keyof Input]: Input[K] extends Field<infer T, TableName> ? HasSql<T | null> : Input[K] extends Table<infer Definition, TableName> ? TableFields<Definition> & IsNullable : Input[K] extends Record<string, Field<unknown, TableName> | HasSql<unknown>> ? Input[K] & IsNullable : Input[K] extends SelectionRecord ? MarkFieldsAsNullable<Input[K], TableName> : Input[K];
|
|
@@ -74,7 +95,27 @@ export interface SelectionFrom<Input, Meta extends QueryMeta> extends SelectBase
|
|
|
74
95
|
rightJoin(right: HasTarget, on: HasSql<boolean>): SelectionFrom<Input, Meta>;
|
|
75
96
|
innerJoin<Definition extends TableDefinition, Name extends string>(right: Table<Definition, Name>, on: HasSql<boolean>): SelectionFrom<Input, Meta>;
|
|
76
97
|
innerJoin(right: HasTarget, on: HasSql<boolean>): SelectionFrom<Input, Meta>;
|
|
98
|
+
crossJoin<Definition extends TableDefinition, Name extends string>(right: Table<Definition, Name>, on: HasSql<boolean>): SelectionFrom<Input, Meta>;
|
|
99
|
+
crossJoin(right: HasTarget, on: HasSql<boolean>): SelectionFrom<Input, Meta>;
|
|
77
100
|
fullJoin<Definition extends TableDefinition, Name extends string>(right: Table<Definition, Name>, on: HasSql<boolean>): SelectionFrom<Input, Meta>;
|
|
78
101
|
fullJoin(right: HasTarget, on: HasSql<boolean>): SelectionFrom<Input, Meta>;
|
|
79
102
|
}
|
|
103
|
+
export declare function querySelection({ select, from }: SelectQuery): Selection;
|
|
104
|
+
export declare function selectQuery(query: SelectQuery): Sql;
|
|
105
|
+
export declare class Union<Result, Meta extends QueryMeta = QueryMeta> extends UnionBase<Result, Meta> implements HasSelection {
|
|
106
|
+
readonly [internalData]: QueryData<Meta> & UnionQuery;
|
|
107
|
+
constructor(data: QueryData<Meta> & UnionQuery);
|
|
108
|
+
get [internalQuery](): Sql;
|
|
109
|
+
get [internalSelection](): Selection;
|
|
110
|
+
orderBy(...orderBy: Array<HasSql>): Union<Result, Meta>;
|
|
111
|
+
limit(limit: UserInput<number>): Union<Result, Meta>;
|
|
112
|
+
offset(offset: UserInput<number>): Union<Result, Meta>;
|
|
113
|
+
}
|
|
114
|
+
export declare function union<Result, Meta extends QueryMeta>(left: UnionBase<Result, Meta>, right: UnionBase<Result, Meta>, ...rest: Array<UnionBase<Result, Meta>>): Union<Result, Meta>;
|
|
115
|
+
export declare function unionAll<Result, Meta extends QueryMeta>(left: UnionBase<Result, Meta>, right: UnionBase<Result, Meta>, ...rest: Array<UnionBase<Result, Meta>>): Union<Result, Meta>;
|
|
116
|
+
export declare function intersect<Result, Meta extends QueryMeta>(left: UnionBase<Result, Meta>, right: UnionBase<Result, Meta>, ...rest: Array<UnionBase<Result, Meta>>): Union<Result, Meta>;
|
|
117
|
+
export declare function intersectAll<Result, Meta extends IsPostgres | IsMysql>(left: UnionBase<Result, Meta>, right: UnionBase<Result, Meta>, ...rest: Array<UnionBase<Result, Meta>>): Union<Result, Meta>;
|
|
118
|
+
export declare function except<Result, Meta extends QueryMeta>(left: UnionBase<Result, Meta>, right: UnionBase<Result, Meta>, ...rest: Array<UnionBase<Result, Meta>>): Union<Result, Meta>;
|
|
119
|
+
export declare function exceptAll<Result, Meta extends IsPostgres | IsMysql>(left: UnionBase<Result, Meta>, right: UnionBase<Result, Meta>, ...rest: Array<UnionBase<Result, Meta>>): Union<Result, Meta>;
|
|
120
|
+
export declare function unionQuery(query: UnionQuery): Sql;
|
|
80
121
|
export {};
|
|
@@ -5,7 +5,8 @@ import {
|
|
|
5
5
|
getSelection,
|
|
6
6
|
getSql,
|
|
7
7
|
getTarget,
|
|
8
|
-
|
|
8
|
+
hasSelection,
|
|
9
|
+
hasSql,
|
|
9
10
|
hasTarget,
|
|
10
11
|
internalData,
|
|
11
12
|
internalQuery,
|
|
@@ -13,14 +14,32 @@ import {
|
|
|
13
14
|
internalSql,
|
|
14
15
|
internalTarget
|
|
15
16
|
} from "../Internal.js";
|
|
17
|
+
import { SingleQuery } from "../Queries.js";
|
|
16
18
|
import {
|
|
17
19
|
selection
|
|
18
20
|
} from "../Selection.js";
|
|
19
|
-
import { sql } from "../Sql.js";
|
|
21
|
+
import { Sql, sql } from "../Sql.js";
|
|
20
22
|
import { and } from "../expr/Conditions.js";
|
|
21
|
-
import {
|
|
22
|
-
import {
|
|
23
|
-
var
|
|
23
|
+
import { formatCTE } from "./CTE.js";
|
|
24
|
+
import { formatModifiers } from "./Shared.js";
|
|
25
|
+
var SelectFirst = class extends SingleQuery {
|
|
26
|
+
[internalData];
|
|
27
|
+
constructor(data) {
|
|
28
|
+
const inner = { ...data, first: true };
|
|
29
|
+
super(inner);
|
|
30
|
+
this[internalData] = inner;
|
|
31
|
+
}
|
|
32
|
+
get [internalSelection]() {
|
|
33
|
+
return querySelection(getData(this));
|
|
34
|
+
}
|
|
35
|
+
get [internalQuery]() {
|
|
36
|
+
return selectQuery(getData(this));
|
|
37
|
+
}
|
|
38
|
+
get [internalSql]() {
|
|
39
|
+
return sql`(${getQuery(this)})`;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
var UnionBase = class extends SingleQuery {
|
|
24
43
|
[internalData];
|
|
25
44
|
constructor(data) {
|
|
26
45
|
super(data);
|
|
@@ -29,89 +48,268 @@ var Select = class _Select extends UnionBase {
|
|
|
29
48
|
as(alias) {
|
|
30
49
|
const fields = getSelection(this).makeVirtual(alias);
|
|
31
50
|
return Object.assign(fields, {
|
|
51
|
+
[internalSelection]: selection(fields),
|
|
32
52
|
[internalTarget]: sql`(${getQuery(this)}) as ${sql.identifier(
|
|
33
53
|
alias
|
|
34
54
|
)}`.inlineFields(true)
|
|
35
55
|
});
|
|
36
56
|
}
|
|
37
|
-
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
const isTable = hasTable(target);
|
|
41
|
-
const selected = current ?? (isTable ? selection.table(target) : selection(sql`*`));
|
|
42
|
-
return new _Select({
|
|
43
|
-
...getData(this),
|
|
44
|
-
select: selected,
|
|
45
|
-
from
|
|
46
|
-
});
|
|
57
|
+
#makeSelf() {
|
|
58
|
+
const selected = getSelection(this);
|
|
59
|
+
return selected.makeVirtual(Sql.SELF_TARGET);
|
|
47
60
|
}
|
|
48
|
-
#
|
|
49
|
-
const
|
|
50
|
-
|
|
61
|
+
#getSelect(base) {
|
|
62
|
+
const data = getData(base);
|
|
63
|
+
if (!("compound" in data)) throw new Error("No compound defined");
|
|
64
|
+
return data.compound;
|
|
65
|
+
}
|
|
66
|
+
#compound(op, target) {
|
|
67
|
+
const left = this.#getSelect(this);
|
|
68
|
+
const right = this.#getSelect(
|
|
69
|
+
typeof target === "function" ? target(this.#makeSelf()) : target
|
|
70
|
+
);
|
|
71
|
+
const [on, ...rest] = right;
|
|
72
|
+
const select = [...left, { [op]: on }, ...rest];
|
|
73
|
+
return new Union({
|
|
51
74
|
...getData(this),
|
|
52
|
-
select
|
|
53
|
-
from: sql.join([
|
|
54
|
-
from,
|
|
55
|
-
sql.unsafe(`${operator} join`),
|
|
56
|
-
getTarget(right),
|
|
57
|
-
sql`on ${on}`
|
|
58
|
-
])
|
|
75
|
+
select
|
|
59
76
|
});
|
|
60
77
|
}
|
|
61
|
-
|
|
62
|
-
return this.#
|
|
78
|
+
union(target) {
|
|
79
|
+
return this.#compound("union", target);
|
|
80
|
+
}
|
|
81
|
+
unionAll(target) {
|
|
82
|
+
return this.#compound("unionAll", target);
|
|
83
|
+
}
|
|
84
|
+
intersect(target) {
|
|
85
|
+
return this.#compound("intersect", target);
|
|
86
|
+
}
|
|
87
|
+
intersectAll(target) {
|
|
88
|
+
return this.#compound("intersectAll", target);
|
|
89
|
+
}
|
|
90
|
+
except(target) {
|
|
91
|
+
return this.#compound("except", target);
|
|
92
|
+
}
|
|
93
|
+
exceptAll(target) {
|
|
94
|
+
return this.#compound("exceptAll", target);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
var Select = class _Select extends UnionBase {
|
|
98
|
+
[internalData];
|
|
99
|
+
constructor(data) {
|
|
100
|
+
const compound = [data];
|
|
101
|
+
const withCompound = { ...data, compound };
|
|
102
|
+
super(withCompound);
|
|
103
|
+
this[internalData] = withCompound;
|
|
104
|
+
}
|
|
105
|
+
from(from) {
|
|
106
|
+
return new _Select({ ...getData(this), from });
|
|
107
|
+
}
|
|
108
|
+
#fromTarget() {
|
|
109
|
+
const { from } = getData(this);
|
|
110
|
+
if (!from) throw new Error("No target defined");
|
|
111
|
+
if (Array.isArray(from)) return from;
|
|
112
|
+
return [from];
|
|
113
|
+
}
|
|
114
|
+
#join(join) {
|
|
115
|
+
return new _Select({ ...getData(this), from: [...this.#fromTarget(), join] });
|
|
63
116
|
}
|
|
64
|
-
|
|
65
|
-
return this.#join(
|
|
117
|
+
leftJoin(leftJoin, on) {
|
|
118
|
+
return this.#join({ leftJoin, on });
|
|
66
119
|
}
|
|
67
|
-
|
|
68
|
-
return this.#join(
|
|
120
|
+
rightJoin(rightJoin, on) {
|
|
121
|
+
return this.#join({ rightJoin, on });
|
|
69
122
|
}
|
|
70
|
-
|
|
71
|
-
return this.#join(
|
|
123
|
+
innerJoin(innerJoin, on) {
|
|
124
|
+
return this.#join({ innerJoin, on });
|
|
125
|
+
}
|
|
126
|
+
fullJoin(fullJoin, on) {
|
|
127
|
+
return this.#join({ fullJoin, on });
|
|
128
|
+
}
|
|
129
|
+
crossJoin(crossJoin, on) {
|
|
130
|
+
return this.#join({ crossJoin, on });
|
|
72
131
|
}
|
|
73
132
|
where(...where) {
|
|
74
133
|
return new _Select({ ...getData(this), where: and(...where) });
|
|
75
134
|
}
|
|
76
|
-
groupBy(...
|
|
77
|
-
return new _Select({
|
|
78
|
-
...getData(this),
|
|
79
|
-
groupBy: sql.join(exprs, sql.unsafe(", "))
|
|
80
|
-
});
|
|
135
|
+
groupBy(...groupBy) {
|
|
136
|
+
return new _Select({ ...getData(this), groupBy });
|
|
81
137
|
}
|
|
82
138
|
having(having) {
|
|
83
|
-
return new _Select({
|
|
84
|
-
...getData(this),
|
|
85
|
-
having: typeof having === "function" ? having(getSelection(this).input) : having
|
|
86
|
-
});
|
|
139
|
+
return new _Select({ ...getData(this), having });
|
|
87
140
|
}
|
|
88
|
-
orderBy(...
|
|
89
|
-
return new _Select({
|
|
90
|
-
...getData(this),
|
|
91
|
-
orderBy: sql.join(exprs, sql.unsafe(", "))
|
|
92
|
-
});
|
|
141
|
+
orderBy(...orderBy) {
|
|
142
|
+
return new _Select({ ...getData(this), orderBy });
|
|
93
143
|
}
|
|
94
144
|
limit(limit) {
|
|
95
|
-
return new _Select({ ...getData(this), limit
|
|
145
|
+
return new _Select({ ...getData(this), limit });
|
|
96
146
|
}
|
|
97
147
|
offset(offset) {
|
|
98
|
-
return new _Select({
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
148
|
+
return new _Select({ ...getData(this), offset });
|
|
149
|
+
}
|
|
150
|
+
$dynamic() {
|
|
151
|
+
return this;
|
|
152
|
+
}
|
|
153
|
+
$first() {
|
|
154
|
+
return new SelectFirst(getData(this));
|
|
102
155
|
}
|
|
103
156
|
get [internalSelection]() {
|
|
104
|
-
|
|
105
|
-
if (!select) throw new Error("No selection defined");
|
|
106
|
-
return select;
|
|
157
|
+
return querySelection(getData(this));
|
|
107
158
|
}
|
|
108
159
|
get [internalQuery]() {
|
|
109
|
-
return
|
|
160
|
+
return selectQuery(getData(this));
|
|
110
161
|
}
|
|
111
162
|
get [internalSql]() {
|
|
112
163
|
return sql`(${getQuery(this)})`;
|
|
113
164
|
}
|
|
114
165
|
};
|
|
166
|
+
function querySelection({ select, from }) {
|
|
167
|
+
if (select) return selection(select);
|
|
168
|
+
if (!from) throw new Error("No selection defined");
|
|
169
|
+
if (Array.isArray(from)) {
|
|
170
|
+
const [target, ...joins] = from;
|
|
171
|
+
return joins.reduce((result, join) => {
|
|
172
|
+
const { target: target2, op } = joinOp(join);
|
|
173
|
+
return result.join(target2, op);
|
|
174
|
+
}, selection(target));
|
|
175
|
+
}
|
|
176
|
+
return hasSelection(from) ? getSelection(from) : selection(sql`*`);
|
|
177
|
+
}
|
|
178
|
+
function joinOp(join) {
|
|
179
|
+
const { on, ...rest } = join;
|
|
180
|
+
const op = Object.keys(rest)[0];
|
|
181
|
+
const target = rest[op];
|
|
182
|
+
return { target, op, on };
|
|
183
|
+
}
|
|
184
|
+
function formatFrom(from) {
|
|
185
|
+
if (!from) throw new Error("No target defined");
|
|
186
|
+
if (Array.isArray(from)) {
|
|
187
|
+
return sql.join(
|
|
188
|
+
from.map((join) => {
|
|
189
|
+
if (hasTarget(join)) return getTarget(join);
|
|
190
|
+
if (hasSql(join)) return getSql(join);
|
|
191
|
+
const { target, op, on } = joinOp(join);
|
|
192
|
+
return sql.query({
|
|
193
|
+
[op]: hasTarget(target) ? getTarget(target) : getSql(target),
|
|
194
|
+
on
|
|
195
|
+
});
|
|
196
|
+
})
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
return hasTarget(from) ? getTarget(from) : getSql(from);
|
|
200
|
+
}
|
|
201
|
+
function selectQuery(query) {
|
|
202
|
+
const { from, where, groupBy, having, distinct, distinctOn } = query;
|
|
203
|
+
const prefix = distinctOn ? sql`distinct on (${sql.join(distinctOn, sql`, `)})` : distinct && sql`distinct`;
|
|
204
|
+
const selected = querySelection(query);
|
|
205
|
+
const select = sql.join([prefix, selected]);
|
|
206
|
+
return sql.query(
|
|
207
|
+
formatCTE(query),
|
|
208
|
+
{
|
|
209
|
+
select,
|
|
210
|
+
from: from && formatFrom(from),
|
|
211
|
+
where,
|
|
212
|
+
groupBy: groupBy && sql.join(groupBy, sql`, `),
|
|
213
|
+
having: typeof having === "function" ? having(selected.input) : having
|
|
214
|
+
},
|
|
215
|
+
formatModifiers(query)
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
var Union = class _Union extends UnionBase {
|
|
219
|
+
[internalData];
|
|
220
|
+
constructor(data) {
|
|
221
|
+
const compound = data.select;
|
|
222
|
+
const withCompound = { ...data, compound };
|
|
223
|
+
super(withCompound);
|
|
224
|
+
this[internalData] = withCompound;
|
|
225
|
+
}
|
|
226
|
+
get [internalQuery]() {
|
|
227
|
+
return unionQuery(getData(this));
|
|
228
|
+
}
|
|
229
|
+
get [internalSelection]() {
|
|
230
|
+
const {
|
|
231
|
+
select: [first]
|
|
232
|
+
} = getData(this);
|
|
233
|
+
return querySelection(first);
|
|
234
|
+
}
|
|
235
|
+
orderBy(...orderBy) {
|
|
236
|
+
return new _Union({ ...getData(this), orderBy });
|
|
237
|
+
}
|
|
238
|
+
limit(limit) {
|
|
239
|
+
return new _Union({ ...getData(this), limit });
|
|
240
|
+
}
|
|
241
|
+
offset(offset) {
|
|
242
|
+
return new _Union({ ...getData(this), offset });
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
function union(left, right, ...rest) {
|
|
246
|
+
return [right, ...rest].reduce(
|
|
247
|
+
(acc, query) => acc.union(query),
|
|
248
|
+
left
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
function unionAll(left, right, ...rest) {
|
|
252
|
+
return [right, ...rest].reduce(
|
|
253
|
+
(acc, query) => acc.unionAll(query),
|
|
254
|
+
left
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
function intersect(left, right, ...rest) {
|
|
258
|
+
return [right, ...rest].reduce(
|
|
259
|
+
(acc, query) => acc.intersect(query),
|
|
260
|
+
left
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
function intersectAll(left, right, ...rest) {
|
|
264
|
+
return [right, ...rest].reduce(
|
|
265
|
+
(acc, query) => acc.intersectAll(query),
|
|
266
|
+
left
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
function except(left, right, ...rest) {
|
|
270
|
+
return [right, ...rest].reduce(
|
|
271
|
+
(acc, query) => acc.except(query),
|
|
272
|
+
left
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
function exceptAll(left, right, ...rest) {
|
|
276
|
+
return [right, ...rest].reduce(
|
|
277
|
+
(acc, query) => acc.exceptAll(query),
|
|
278
|
+
left
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
function unionQuery(query) {
|
|
282
|
+
const { select } = query;
|
|
283
|
+
let fields;
|
|
284
|
+
const segments = sql.join(
|
|
285
|
+
select.map((segment, i) => {
|
|
286
|
+
if (i === 0) {
|
|
287
|
+
fields = querySelection(segment).fieldNames();
|
|
288
|
+
return selectQuery(segment);
|
|
289
|
+
}
|
|
290
|
+
const op = Object.keys(segment)[0];
|
|
291
|
+
const query2 = segment[op];
|
|
292
|
+
const names = querySelection(query2).fieldNames();
|
|
293
|
+
for (let i2 = 0; i2 < fields.length; i2++)
|
|
294
|
+
if (fields[i2] !== names[i2])
|
|
295
|
+
throw new Error("Union segments must have the same fields");
|
|
296
|
+
return sql.query({ [op]: selectQuery(query2) });
|
|
297
|
+
})
|
|
298
|
+
);
|
|
299
|
+
return sql.query(formatCTE(query), segments, formatModifiers(query));
|
|
300
|
+
}
|
|
115
301
|
export {
|
|
116
|
-
Select
|
|
302
|
+
Select,
|
|
303
|
+
SelectFirst,
|
|
304
|
+
Union,
|
|
305
|
+
UnionBase,
|
|
306
|
+
except,
|
|
307
|
+
exceptAll,
|
|
308
|
+
intersect,
|
|
309
|
+
intersectAll,
|
|
310
|
+
querySelection,
|
|
311
|
+
selectQuery,
|
|
312
|
+
union,
|
|
313
|
+
unionAll,
|
|
314
|
+
unionQuery
|
|
117
315
|
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// src/core/query/Shared.ts
|
|
2
|
+
import { getSql } from "../Internal.js";
|
|
3
|
+
import { sql } from "../Sql.js";
|
|
4
|
+
import { input } from "../expr/Input.js";
|
|
5
|
+
function formatModifiers(modifiers) {
|
|
6
|
+
const { orderBy, limit, offset } = modifiers;
|
|
7
|
+
if (!orderBy && limit === void 0 && offset === void 0) return void 0;
|
|
8
|
+
return sql.query({
|
|
9
|
+
orderBy: orderBy && sql.join(
|
|
10
|
+
orderBy.map((part) => {
|
|
11
|
+
const { alias } = getSql(part);
|
|
12
|
+
if (alias) return sql.identifier(alias);
|
|
13
|
+
return part;
|
|
14
|
+
}),
|
|
15
|
+
sql`, `
|
|
16
|
+
),
|
|
17
|
+
limit: limit !== void 0 && input(limit),
|
|
18
|
+
offset: offset !== void 0 && input(offset)
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
export {
|
|
22
|
+
formatModifiers
|
|
23
|
+
};
|
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type HasQuery, type HasSql, internalData, internalQuery, internalSelection } from '../Internal.js';
|
|
2
2
|
import type { IsPostgres, IsSqlite, QueryMeta } from '../MetaData.js';
|
|
3
|
-
import {
|
|
3
|
+
import { type QueryData, SingleQuery } from '../Queries.js';
|
|
4
4
|
import { type Selection, type SelectionInput, type SelectionRow } from '../Selection.js';
|
|
5
5
|
import { type Sql } from '../Sql.js';
|
|
6
6
|
import type { TableDefinition, TableRow, TableUpdate } from '../Table.js';
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
returning?: Selection;
|
|
12
|
-
}
|
|
13
|
-
export declare class Update<Result, Meta extends QueryMeta = QueryMeta> extends Query<Result, Meta> {
|
|
14
|
-
readonly [internalData]: UpdateData<Meta>;
|
|
7
|
+
import { type Input } from '../expr/Input.js';
|
|
8
|
+
import type { UpdateQuery } from './Query.js';
|
|
9
|
+
export declare class Update<Result, Meta extends QueryMeta = QueryMeta> extends SingleQuery<Array<Result>, Meta> implements HasQuery<Result> {
|
|
10
|
+
readonly [internalData]: QueryData<Meta> & UpdateQuery;
|
|
15
11
|
readonly [internalSelection]?: Selection;
|
|
16
|
-
constructor(data:
|
|
17
|
-
get [internalQuery](): Sql
|
|
12
|
+
constructor(data: QueryData<Meta> & UpdateQuery);
|
|
13
|
+
get [internalQuery](): Sql<Result>;
|
|
14
|
+
limit(limit: Input<number>): Update<Result, Meta>;
|
|
15
|
+
offset(offset: Input<number>): Update<Result, Meta>;
|
|
16
|
+
orderBy(...orderBy: Array<HasSql>): Update<Result, Meta>;
|
|
18
17
|
}
|
|
19
18
|
export declare class UpdateTable<Definition extends TableDefinition, Meta extends QueryMeta> extends Update<void, Meta> {
|
|
20
|
-
set(
|
|
19
|
+
set(set: TableUpdate<Definition>): UpdateTable<Definition, Meta>;
|
|
21
20
|
where(...where: Array<HasSql<boolean> | undefined>): UpdateTable<Definition, Meta>;
|
|
22
21
|
returning(this: UpdateTable<Definition, IsPostgres | IsSqlite>): Update<TableRow<Definition>, Meta>;
|
|
23
22
|
returning<Input extends SelectionInput>(this: UpdateTable<Definition, IsPostgres | IsSqlite>, returning?: Input): Update<SelectionRow<Input>, Meta>;
|
|
24
23
|
}
|
|
24
|
+
export declare function updateQuery(query: UpdateQuery): Sql;
|