@grest-ts/sql 0.0.5 → 0.0.7
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/LICENSE +21 -21
- package/README.md +503 -498
- package/dist/src/select/parts/S0Union.d.ts +2 -2
- package/dist/tsconfig.publish.tsbuildinfo +1 -1
- package/package.json +3 -3
- package/src/MysqlTable.ts +101 -101
- package/src/MysqlTableStructureParser.ts +283 -283
- package/src/SQL.ts +244 -244
- package/src/SqlExpression.ts +172 -172
- package/src/SqlFunctions.ts +471 -471
- package/src/Symbols.ts +10 -10
- package/src/Types.ts +244 -244
- package/src/delete/DeleteBuilder.ts +64 -64
- package/src/delete/DeleteInterfaces.ts +70 -70
- package/src/escape.ts +51 -51
- package/src/index.ts +5 -5
- package/src/insert/InsertBuilder.ts +75 -75
- package/src/insert/InsertInterfaces.ts +16 -16
- package/src/select/SelectBuilder.ts +371 -371
- package/src/select/parts/GatewayWhere.ts +30 -30
- package/src/select/parts/S0Union.ts +43 -43
- package/src/select/parts/S0Uses.ts +21 -21
- package/src/select/parts/S0With.ts +21 -21
- package/src/select/parts/S1Join.ts +38 -38
- package/src/select/parts/S2Columns.ts +68 -68
- package/src/select/parts/S2Where.ts +51 -51
- package/src/select/parts/S3GroupBy.ts +23 -23
- package/src/select/parts/S4Having.ts +24 -24
- package/src/select/parts/S5OrderBy.ts +21 -21
- package/src/select/parts/S6Limit.ts +9 -9
- package/src/select/parts/S7Exec.ts +41 -41
- package/src/update/UpdateBuilder.ts +95 -95
- package/src/update/UpdateInterfaces.ts +87 -87
package/src/SQL.ts
CHANGED
|
@@ -1,244 +1,244 @@
|
|
|
1
|
-
import {SelectBuilder} from "./select/SelectBuilder";
|
|
2
|
-
import {AliasedTable, DbTableDefinition, Key, NotUsingWithPart, PrepareQueryArgument, SqlQuery} from "./Types";
|
|
3
|
-
import {SQL_ALIAS, SQL_ENTITY, SQL_EXPRESSION} from "./Symbols";
|
|
4
|
-
import {escape, escapeId} from "./escape";
|
|
5
|
-
import {MysqlTable} from "./MysqlTable";
|
|
6
|
-
import {JoinMethods} from "./select/parts/S1Join";
|
|
7
|
-
import {UsesMethods} from "./select/parts/S0Uses";
|
|
8
|
-
import {WithMethods} from "./select/parts/S0With";
|
|
9
|
-
import {ExecMethods} from "./select/parts/S7Exec";
|
|
10
|
-
import {DeleteBuilder} from "./delete/DeleteBuilder";
|
|
11
|
-
import {DeleteWhereMethods} from "./delete/DeleteInterfaces";
|
|
12
|
-
import {InsertBuilder} from "./insert/InsertBuilder";
|
|
13
|
-
import {InsertSetMethods} from "./insert/InsertInterfaces";
|
|
14
|
-
import {UpdateSetMethods} from "./update/UpdateInterfaces";
|
|
15
|
-
import {UpdateBuilder} from "./update/UpdateBuilder";
|
|
16
|
-
|
|
17
|
-
export class SQL {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
public static insertInto<
|
|
21
|
-
Entity,
|
|
22
|
-
EditEntity,
|
|
23
|
-
Alias extends string,
|
|
24
|
-
TableRef extends `${string} as ${Alias}`,
|
|
25
|
-
>(
|
|
26
|
-
table: AliasedTable<Alias, TableRef, Entity, EditEntity, NotUsingWithPart>): InsertSetMethods<EditEntity> {
|
|
27
|
-
return new InsertBuilder().to(table[SQL_EXPRESSION]);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
public static insertIgnoreInto<
|
|
31
|
-
Entity,
|
|
32
|
-
EditEntity,
|
|
33
|
-
Alias extends string,
|
|
34
|
-
TableRef extends `${string} as ${Alias}`,
|
|
35
|
-
>(
|
|
36
|
-
table: AliasedTable<Alias, TableRef, Entity, EditEntity, NotUsingWithPart>
|
|
37
|
-
): InsertSetMethods<EditEntity> {
|
|
38
|
-
return new InsertBuilder().to(table[SQL_EXPRESSION]).ignore();
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
public static update<
|
|
42
|
-
Entity,
|
|
43
|
-
EditEntity,
|
|
44
|
-
Alias extends string,
|
|
45
|
-
TableRef extends `${string} as ${Alias}`,
|
|
46
|
-
>(
|
|
47
|
-
table: AliasedTable<Alias, TableRef, Entity, EditEntity, NotUsingWithPart>
|
|
48
|
-
): UpdateSetMethods<Entity, Key<TableRef>> {
|
|
49
|
-
return new UpdateBuilder().in(table[SQL_EXPRESSION], table[SQL_ALIAS]);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
public static updateIgnore<
|
|
53
|
-
Entity,
|
|
54
|
-
EditEntity,
|
|
55
|
-
Alias extends string,
|
|
56
|
-
TableRef extends `${string} as ${Alias}`,
|
|
57
|
-
>(
|
|
58
|
-
table: AliasedTable<Alias, TableRef, Entity, EditEntity, NotUsingWithPart>
|
|
59
|
-
): UpdateSetMethods<Entity, Key<TableRef>> {
|
|
60
|
-
return new UpdateBuilder().in(table[SQL_EXPRESSION], table[SQL_ALIAS]).ignore()
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
public static deleteFrom<
|
|
64
|
-
Alias extends string,
|
|
65
|
-
TableRef extends `${string} as ${Alias}`,
|
|
66
|
-
>(
|
|
67
|
-
table: AliasedTable<Alias, TableRef, object, object, string | NotUsingWithPart>
|
|
68
|
-
): DeleteWhereMethods<Key<TableRef>> {
|
|
69
|
-
return new DeleteBuilder().from(table[SQL_EXPRESSION], table[SQL_ALIAS]);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
public static selectFrom<
|
|
73
|
-
Alias extends string,
|
|
74
|
-
TableRef extends `${string} as ${Alias}`
|
|
75
|
-
>(
|
|
76
|
-
table: AliasedTable<Alias, TableRef, object, object, string | NotUsingWithPart>
|
|
77
|
-
): JoinMethods<Key<Alias>, {}, Key<TableRef>> {
|
|
78
|
-
return new SelectBuilder().selectFrom(table)
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Start building a select query that most probably will be used as a scalar subquery.
|
|
83
|
-
*
|
|
84
|
-
* for example:
|
|
85
|
-
* SELECT
|
|
86
|
-
* (SELECT id FROM child WHERE child.id = parent.id ) <--- scalar subquery
|
|
87
|
-
* FROM parent
|
|
88
|
-
*
|
|
89
|
-
* In the uses part you add parent table refererence.
|
|
90
|
-
*/
|
|
91
|
-
public static uses<
|
|
92
|
-
Alias1 extends string, TableRef1 extends `${string} as ${Alias1}`,
|
|
93
|
-
>(
|
|
94
|
-
table1: AliasedTable<Alias1, TableRef1, object, object, NotUsingWithPart>
|
|
95
|
-
): UsesMethods<Key<Alias1>, Key<TableRef1>>
|
|
96
|
-
public static uses<
|
|
97
|
-
Alias1 extends string, TableRef1 extends `${string} as ${Alias1}`,
|
|
98
|
-
Alias2 extends string, TableRef2 extends `${string} as ${Alias2}`,
|
|
99
|
-
>(
|
|
100
|
-
table1: AliasedTable<Alias1, TableRef1, object, object, NotUsingWithPart>,
|
|
101
|
-
table2: AliasedTable<Alias2, TableRef2, object, object, NotUsingWithPart>,
|
|
102
|
-
): UsesMethods<Key<Alias1> & Key<Alias2>, Key<TableRef1> & Key<TableRef2>>
|
|
103
|
-
public static uses<
|
|
104
|
-
Alias1 extends string, TableRef1 extends `${string} as ${Alias1}`,
|
|
105
|
-
Alias2 extends string, TableRef2 extends `${string} as ${Alias2}`,
|
|
106
|
-
Alias3 extends string, TableRef3 extends `${string} as ${Alias3}`
|
|
107
|
-
>(
|
|
108
|
-
table1: AliasedTable<Alias1, TableRef1, object, object, NotUsingWithPart>,
|
|
109
|
-
table2: AliasedTable<Alias2, TableRef2, object, object, NotUsingWithPart>,
|
|
110
|
-
table3: AliasedTable<Alias3, TableRef3, object, object, NotUsingWithPart>,
|
|
111
|
-
): UsesMethods<Key<Alias1> & Key<Alias2> & Key<Alias3>, Key<TableRef1> & Key<TableRef2> & Key<TableRef3>>
|
|
112
|
-
public static uses<
|
|
113
|
-
Alias1 extends string, TableRef1 extends `${string} as ${Alias1}`,
|
|
114
|
-
Alias2 extends string, TableRef2 extends `${string} as ${Alias2}`,
|
|
115
|
-
Alias3 extends string, TableRef3 extends `${string} as ${Alias3}`,
|
|
116
|
-
Alias4 extends string, TableRef4 extends `${string} as ${Alias4}`,
|
|
117
|
-
>(
|
|
118
|
-
table1: AliasedTable<Alias1, TableRef1, object, object, NotUsingWithPart>,
|
|
119
|
-
table2: AliasedTable<Alias2, TableRef2, object, object, NotUsingWithPart>,
|
|
120
|
-
table3: AliasedTable<Alias3, TableRef3, object, object, NotUsingWithPart>,
|
|
121
|
-
table4: AliasedTable<Alias4, TableRef4, object, object, NotUsingWithPart>,
|
|
122
|
-
): UsesMethods<Key<Alias1> & Key<Alias2> & Key<Alias3> & Key<Alias4>, Key<TableRef1> & Key<TableRef2> & Key<TableRef3> & Key<TableRef4>>
|
|
123
|
-
public static uses(table1: any, table2?: any, table3?: any, table4?: any) {
|
|
124
|
-
return new SelectBuilder();
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Start building a select query that uses WITH queries.
|
|
129
|
-
*
|
|
130
|
-
* for example:
|
|
131
|
-
* WITH user2 AS (
|
|
132
|
-
* SELECT id FROM user <-- this query AliasedTable is argument to this method. ( SQL.select()...as("user2") )
|
|
133
|
-
* )
|
|
134
|
-
* SELECT *
|
|
135
|
-
* FROM user2
|
|
136
|
-
*
|
|
137
|
-
* With a
|
|
138
|
-
*/
|
|
139
|
-
public static with<
|
|
140
|
-
Alias1 extends string, TableRef1 extends `${string} as ${Alias1}`,
|
|
141
|
-
>(
|
|
142
|
-
table1: AliasedTable<Alias1, TableRef1, object, object, NotUsingWithPart>
|
|
143
|
-
): WithMethods<Key<Alias1>>
|
|
144
|
-
public static with<
|
|
145
|
-
Alias1 extends string, TableRef1 extends `${string} as ${Alias1}`,
|
|
146
|
-
Alias2 extends string, TableRef2 extends `${string} as ${Alias2}`,
|
|
147
|
-
>(
|
|
148
|
-
table1: AliasedTable<Alias1, TableRef1, object, object, NotUsingWithPart>,
|
|
149
|
-
table2: AliasedTable<Alias2, TableRef2, object, object, NotUsingWithPart>,
|
|
150
|
-
): WithMethods<Key<Alias1> & Key<Alias2>>
|
|
151
|
-
public static with<
|
|
152
|
-
Alias1 extends string, TableRef1 extends `${string} as ${Alias1}`,
|
|
153
|
-
Alias2 extends string, TableRef2 extends `${string} as ${Alias2}`,
|
|
154
|
-
Alias3 extends string, TableRef3 extends `${string} as ${Alias3}`
|
|
155
|
-
>(
|
|
156
|
-
table1: AliasedTable<Alias1, TableRef1, object, object, NotUsingWithPart>,
|
|
157
|
-
table2: AliasedTable<Alias2, TableRef2, object, object, NotUsingWithPart>,
|
|
158
|
-
table3: AliasedTable<Alias3, TableRef3, object, object, NotUsingWithPart>,
|
|
159
|
-
): WithMethods<Key<Alias1> & Key<Alias2> & Key<Alias3>>
|
|
160
|
-
public static with<
|
|
161
|
-
Alias1 extends string, TableRef1 extends `${string} as ${Alias1}`,
|
|
162
|
-
Alias2 extends string, TableRef2 extends `${string} as ${Alias2}`,
|
|
163
|
-
Alias3 extends string, TableRef3 extends `${string} as ${Alias3}`,
|
|
164
|
-
Alias4 extends string, TableRef4 extends `${string} as ${Alias4}`,
|
|
165
|
-
>(
|
|
166
|
-
table1: AliasedTable<Alias1, TableRef1, object, object, NotUsingWithPart>,
|
|
167
|
-
table2: AliasedTable<Alias2, TableRef2, object, object, NotUsingWithPart>,
|
|
168
|
-
table3: AliasedTable<Alias3, TableRef3, object, object, NotUsingWithPart>,
|
|
169
|
-
table4: AliasedTable<Alias4, TableRef4, object, object, NotUsingWithPart>,
|
|
170
|
-
): WithMethods<Key<Alias1> & Key<Alias2> & Key<Alias3> & Key<Alias4>>
|
|
171
|
-
public static with(...tables: any[]) {
|
|
172
|
-
return new SelectBuilder().with(...tables);
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Creates a prepared query.
|
|
177
|
-
*
|
|
178
|
-
* Prepared query is a fixed query that will be prebuilt for higher performance. It will only replace arguments in a prebuilt SQL query string.
|
|
179
|
-
* Main drawback of this is that query has to be static and can't have anything dynamic in it.
|
|
180
|
-
*
|
|
181
|
-
* This is not Mysql prepared statement, it is only preparing and caching a string of the SQL query.
|
|
182
|
-
*
|
|
183
|
-
* Usage
|
|
184
|
-
*
|
|
185
|
-
* const prepared = SQL.prepare( ( args:{id: number} ) => {
|
|
186
|
-
* const c = MyDb.user("c");
|
|
187
|
-
* return SQL.select().from(c)...where( c.id.eq(args.id).noLimit()
|
|
188
|
-
* })
|
|
189
|
-
*
|
|
190
|
-
* exec(prepared({id: 10}))
|
|
191
|
-
*
|
|
192
|
-
*/
|
|
193
|
-
public static prepare<PrepareQueryArguments extends { [key: string]: any }, Result>(func: (args: PrepareQueryArguments) => ExecMethods<Result>): (args: PrepareQueryArguments) => SqlQuery<Result> {
|
|
194
|
-
let sqlQuery: string = undefined;
|
|
195
|
-
const seenArguments: Map<string, string> = new Map();
|
|
196
|
-
const getSqlString = () => {
|
|
197
|
-
if (sqlQuery === undefined) {
|
|
198
|
-
const p: PrepareQueryArguments = new Proxy({}, {
|
|
199
|
-
get(_: {}, p: string): PrepareQueryArgument {
|
|
200
|
-
if (!/^[A-Za-z]+$/.test(p)) {
|
|
201
|
-
throw new Error("Invalid prepare query argument named '" + p + "'. Can only contain ascii letters!")
|
|
202
|
-
}
|
|
203
|
-
const variable = "<<|_arg:|>>" + p + "<<|:_arg|>>";
|
|
204
|
-
seenArguments.set(p, variable);
|
|
205
|
-
return {__prepare_argument: true, expression: variable};
|
|
206
|
-
}
|
|
207
|
-
}) as any
|
|
208
|
-
sqlQuery = func(p).toSqlString();
|
|
209
|
-
}
|
|
210
|
-
return sqlQuery;
|
|
211
|
-
}
|
|
212
|
-
return (args: PrepareQueryArguments) => {
|
|
213
|
-
return {
|
|
214
|
-
[SQL_ENTITY]: undefined,
|
|
215
|
-
toSqlString: (): string => {
|
|
216
|
-
let usedQuery = getSqlString();
|
|
217
|
-
seenArguments.forEach((variable, key) => {
|
|
218
|
-
usedQuery = usedQuery.replace(variable, escape(args[key]));
|
|
219
|
-
})
|
|
220
|
-
return usedQuery
|
|
221
|
-
},
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
/**
|
|
227
|
-
* Create a reference for a query that is used in WITH part and you want to use it in from(X) or join(X) call.
|
|
228
|
-
*/
|
|
229
|
-
public static createRef<
|
|
230
|
-
Alias extends string,
|
|
231
|
-
TableRef extends `${string} as ${Alias}`,
|
|
232
|
-
Entity,
|
|
233
|
-
EditEntity,
|
|
234
|
-
NewAlias extends string
|
|
235
|
-
>(table: AliasedTable<Alias, TableRef, Entity, EditEntity, NotUsingWithPart>, newAlias: NewAlias): AliasedTable<NewAlias, `${Alias} as ${NewAlias}`, Entity, EditEntity, Alias> {
|
|
236
|
-
const definition: DbTableDefinition<Entity> = {} as any;
|
|
237
|
-
for (const k in table) {
|
|
238
|
-
(definition as any)[k] = (table as any)[k];
|
|
239
|
-
}
|
|
240
|
-
return MysqlTable.defineDbTable(escapeId(table[SQL_ALIAS]), newAlias, definition) as any;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
}
|
|
244
|
-
|
|
1
|
+
import {SelectBuilder} from "./select/SelectBuilder";
|
|
2
|
+
import {AliasedTable, DbTableDefinition, Key, NotUsingWithPart, PrepareQueryArgument, SqlQuery} from "./Types";
|
|
3
|
+
import {SQL_ALIAS, SQL_ENTITY, SQL_EXPRESSION} from "./Symbols";
|
|
4
|
+
import {escape, escapeId} from "./escape";
|
|
5
|
+
import {MysqlTable} from "./MysqlTable";
|
|
6
|
+
import {JoinMethods} from "./select/parts/S1Join";
|
|
7
|
+
import {UsesMethods} from "./select/parts/S0Uses";
|
|
8
|
+
import {WithMethods} from "./select/parts/S0With";
|
|
9
|
+
import {ExecMethods} from "./select/parts/S7Exec";
|
|
10
|
+
import {DeleteBuilder} from "./delete/DeleteBuilder";
|
|
11
|
+
import {DeleteWhereMethods} from "./delete/DeleteInterfaces";
|
|
12
|
+
import {InsertBuilder} from "./insert/InsertBuilder";
|
|
13
|
+
import {InsertSetMethods} from "./insert/InsertInterfaces";
|
|
14
|
+
import {UpdateSetMethods} from "./update/UpdateInterfaces";
|
|
15
|
+
import {UpdateBuilder} from "./update/UpdateBuilder";
|
|
16
|
+
|
|
17
|
+
export class SQL {
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
public static insertInto<
|
|
21
|
+
Entity,
|
|
22
|
+
EditEntity,
|
|
23
|
+
Alias extends string,
|
|
24
|
+
TableRef extends `${string} as ${Alias}`,
|
|
25
|
+
>(
|
|
26
|
+
table: AliasedTable<Alias, TableRef, Entity, EditEntity, NotUsingWithPart>): InsertSetMethods<EditEntity> {
|
|
27
|
+
return new InsertBuilder().to(table[SQL_EXPRESSION]);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public static insertIgnoreInto<
|
|
31
|
+
Entity,
|
|
32
|
+
EditEntity,
|
|
33
|
+
Alias extends string,
|
|
34
|
+
TableRef extends `${string} as ${Alias}`,
|
|
35
|
+
>(
|
|
36
|
+
table: AliasedTable<Alias, TableRef, Entity, EditEntity, NotUsingWithPart>
|
|
37
|
+
): InsertSetMethods<EditEntity> {
|
|
38
|
+
return new InsertBuilder().to(table[SQL_EXPRESSION]).ignore();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public static update<
|
|
42
|
+
Entity,
|
|
43
|
+
EditEntity,
|
|
44
|
+
Alias extends string,
|
|
45
|
+
TableRef extends `${string} as ${Alias}`,
|
|
46
|
+
>(
|
|
47
|
+
table: AliasedTable<Alias, TableRef, Entity, EditEntity, NotUsingWithPart>
|
|
48
|
+
): UpdateSetMethods<Entity, Key<TableRef>> {
|
|
49
|
+
return new UpdateBuilder().in(table[SQL_EXPRESSION], table[SQL_ALIAS]);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public static updateIgnore<
|
|
53
|
+
Entity,
|
|
54
|
+
EditEntity,
|
|
55
|
+
Alias extends string,
|
|
56
|
+
TableRef extends `${string} as ${Alias}`,
|
|
57
|
+
>(
|
|
58
|
+
table: AliasedTable<Alias, TableRef, Entity, EditEntity, NotUsingWithPart>
|
|
59
|
+
): UpdateSetMethods<Entity, Key<TableRef>> {
|
|
60
|
+
return new UpdateBuilder().in(table[SQL_EXPRESSION], table[SQL_ALIAS]).ignore()
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public static deleteFrom<
|
|
64
|
+
Alias extends string,
|
|
65
|
+
TableRef extends `${string} as ${Alias}`,
|
|
66
|
+
>(
|
|
67
|
+
table: AliasedTable<Alias, TableRef, object, object, string | NotUsingWithPart>
|
|
68
|
+
): DeleteWhereMethods<Key<TableRef>> {
|
|
69
|
+
return new DeleteBuilder().from(table[SQL_EXPRESSION], table[SQL_ALIAS]);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
public static selectFrom<
|
|
73
|
+
Alias extends string,
|
|
74
|
+
TableRef extends `${string} as ${Alias}`
|
|
75
|
+
>(
|
|
76
|
+
table: AliasedTable<Alias, TableRef, object, object, string | NotUsingWithPart>
|
|
77
|
+
): JoinMethods<Key<Alias>, {}, Key<TableRef>> {
|
|
78
|
+
return new SelectBuilder().selectFrom(table)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Start building a select query that most probably will be used as a scalar subquery.
|
|
83
|
+
*
|
|
84
|
+
* for example:
|
|
85
|
+
* SELECT
|
|
86
|
+
* (SELECT id FROM child WHERE child.id = parent.id ) <--- scalar subquery
|
|
87
|
+
* FROM parent
|
|
88
|
+
*
|
|
89
|
+
* In the uses part you add parent table refererence.
|
|
90
|
+
*/
|
|
91
|
+
public static uses<
|
|
92
|
+
Alias1 extends string, TableRef1 extends `${string} as ${Alias1}`,
|
|
93
|
+
>(
|
|
94
|
+
table1: AliasedTable<Alias1, TableRef1, object, object, NotUsingWithPart>
|
|
95
|
+
): UsesMethods<Key<Alias1>, Key<TableRef1>>
|
|
96
|
+
public static uses<
|
|
97
|
+
Alias1 extends string, TableRef1 extends `${string} as ${Alias1}`,
|
|
98
|
+
Alias2 extends string, TableRef2 extends `${string} as ${Alias2}`,
|
|
99
|
+
>(
|
|
100
|
+
table1: AliasedTable<Alias1, TableRef1, object, object, NotUsingWithPart>,
|
|
101
|
+
table2: AliasedTable<Alias2, TableRef2, object, object, NotUsingWithPart>,
|
|
102
|
+
): UsesMethods<Key<Alias1> & Key<Alias2>, Key<TableRef1> & Key<TableRef2>>
|
|
103
|
+
public static uses<
|
|
104
|
+
Alias1 extends string, TableRef1 extends `${string} as ${Alias1}`,
|
|
105
|
+
Alias2 extends string, TableRef2 extends `${string} as ${Alias2}`,
|
|
106
|
+
Alias3 extends string, TableRef3 extends `${string} as ${Alias3}`
|
|
107
|
+
>(
|
|
108
|
+
table1: AliasedTable<Alias1, TableRef1, object, object, NotUsingWithPart>,
|
|
109
|
+
table2: AliasedTable<Alias2, TableRef2, object, object, NotUsingWithPart>,
|
|
110
|
+
table3: AliasedTable<Alias3, TableRef3, object, object, NotUsingWithPart>,
|
|
111
|
+
): UsesMethods<Key<Alias1> & Key<Alias2> & Key<Alias3>, Key<TableRef1> & Key<TableRef2> & Key<TableRef3>>
|
|
112
|
+
public static uses<
|
|
113
|
+
Alias1 extends string, TableRef1 extends `${string} as ${Alias1}`,
|
|
114
|
+
Alias2 extends string, TableRef2 extends `${string} as ${Alias2}`,
|
|
115
|
+
Alias3 extends string, TableRef3 extends `${string} as ${Alias3}`,
|
|
116
|
+
Alias4 extends string, TableRef4 extends `${string} as ${Alias4}`,
|
|
117
|
+
>(
|
|
118
|
+
table1: AliasedTable<Alias1, TableRef1, object, object, NotUsingWithPart>,
|
|
119
|
+
table2: AliasedTable<Alias2, TableRef2, object, object, NotUsingWithPart>,
|
|
120
|
+
table3: AliasedTable<Alias3, TableRef3, object, object, NotUsingWithPart>,
|
|
121
|
+
table4: AliasedTable<Alias4, TableRef4, object, object, NotUsingWithPart>,
|
|
122
|
+
): UsesMethods<Key<Alias1> & Key<Alias2> & Key<Alias3> & Key<Alias4>, Key<TableRef1> & Key<TableRef2> & Key<TableRef3> & Key<TableRef4>>
|
|
123
|
+
public static uses(table1: any, table2?: any, table3?: any, table4?: any) {
|
|
124
|
+
return new SelectBuilder();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Start building a select query that uses WITH queries.
|
|
129
|
+
*
|
|
130
|
+
* for example:
|
|
131
|
+
* WITH user2 AS (
|
|
132
|
+
* SELECT id FROM user <-- this query AliasedTable is argument to this method. ( SQL.select()...as("user2") )
|
|
133
|
+
* )
|
|
134
|
+
* SELECT *
|
|
135
|
+
* FROM user2
|
|
136
|
+
*
|
|
137
|
+
* With a
|
|
138
|
+
*/
|
|
139
|
+
public static with<
|
|
140
|
+
Alias1 extends string, TableRef1 extends `${string} as ${Alias1}`,
|
|
141
|
+
>(
|
|
142
|
+
table1: AliasedTable<Alias1, TableRef1, object, object, NotUsingWithPart>
|
|
143
|
+
): WithMethods<Key<Alias1>>
|
|
144
|
+
public static with<
|
|
145
|
+
Alias1 extends string, TableRef1 extends `${string} as ${Alias1}`,
|
|
146
|
+
Alias2 extends string, TableRef2 extends `${string} as ${Alias2}`,
|
|
147
|
+
>(
|
|
148
|
+
table1: AliasedTable<Alias1, TableRef1, object, object, NotUsingWithPart>,
|
|
149
|
+
table2: AliasedTable<Alias2, TableRef2, object, object, NotUsingWithPart>,
|
|
150
|
+
): WithMethods<Key<Alias1> & Key<Alias2>>
|
|
151
|
+
public static with<
|
|
152
|
+
Alias1 extends string, TableRef1 extends `${string} as ${Alias1}`,
|
|
153
|
+
Alias2 extends string, TableRef2 extends `${string} as ${Alias2}`,
|
|
154
|
+
Alias3 extends string, TableRef3 extends `${string} as ${Alias3}`
|
|
155
|
+
>(
|
|
156
|
+
table1: AliasedTable<Alias1, TableRef1, object, object, NotUsingWithPart>,
|
|
157
|
+
table2: AliasedTable<Alias2, TableRef2, object, object, NotUsingWithPart>,
|
|
158
|
+
table3: AliasedTable<Alias3, TableRef3, object, object, NotUsingWithPart>,
|
|
159
|
+
): WithMethods<Key<Alias1> & Key<Alias2> & Key<Alias3>>
|
|
160
|
+
public static with<
|
|
161
|
+
Alias1 extends string, TableRef1 extends `${string} as ${Alias1}`,
|
|
162
|
+
Alias2 extends string, TableRef2 extends `${string} as ${Alias2}`,
|
|
163
|
+
Alias3 extends string, TableRef3 extends `${string} as ${Alias3}`,
|
|
164
|
+
Alias4 extends string, TableRef4 extends `${string} as ${Alias4}`,
|
|
165
|
+
>(
|
|
166
|
+
table1: AliasedTable<Alias1, TableRef1, object, object, NotUsingWithPart>,
|
|
167
|
+
table2: AliasedTable<Alias2, TableRef2, object, object, NotUsingWithPart>,
|
|
168
|
+
table3: AliasedTable<Alias3, TableRef3, object, object, NotUsingWithPart>,
|
|
169
|
+
table4: AliasedTable<Alias4, TableRef4, object, object, NotUsingWithPart>,
|
|
170
|
+
): WithMethods<Key<Alias1> & Key<Alias2> & Key<Alias3> & Key<Alias4>>
|
|
171
|
+
public static with(...tables: any[]) {
|
|
172
|
+
return new SelectBuilder().with(...tables);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Creates a prepared query.
|
|
177
|
+
*
|
|
178
|
+
* Prepared query is a fixed query that will be prebuilt for higher performance. It will only replace arguments in a prebuilt SQL query string.
|
|
179
|
+
* Main drawback of this is that query has to be static and can't have anything dynamic in it.
|
|
180
|
+
*
|
|
181
|
+
* This is not Mysql prepared statement, it is only preparing and caching a string of the SQL query.
|
|
182
|
+
*
|
|
183
|
+
* Usage
|
|
184
|
+
*
|
|
185
|
+
* const prepared = SQL.prepare( ( args:{id: number} ) => {
|
|
186
|
+
* const c = MyDb.user("c");
|
|
187
|
+
* return SQL.select().from(c)...where( c.id.eq(args.id).noLimit()
|
|
188
|
+
* })
|
|
189
|
+
*
|
|
190
|
+
* exec(prepared({id: 10}))
|
|
191
|
+
*
|
|
192
|
+
*/
|
|
193
|
+
public static prepare<PrepareQueryArguments extends { [key: string]: any }, Result>(func: (args: PrepareQueryArguments) => ExecMethods<Result>): (args: PrepareQueryArguments) => SqlQuery<Result> {
|
|
194
|
+
let sqlQuery: string = undefined;
|
|
195
|
+
const seenArguments: Map<string, string> = new Map();
|
|
196
|
+
const getSqlString = () => {
|
|
197
|
+
if (sqlQuery === undefined) {
|
|
198
|
+
const p: PrepareQueryArguments = new Proxy({}, {
|
|
199
|
+
get(_: {}, p: string): PrepareQueryArgument {
|
|
200
|
+
if (!/^[A-Za-z]+$/.test(p)) {
|
|
201
|
+
throw new Error("Invalid prepare query argument named '" + p + "'. Can only contain ascii letters!")
|
|
202
|
+
}
|
|
203
|
+
const variable = "<<|_arg:|>>" + p + "<<|:_arg|>>";
|
|
204
|
+
seenArguments.set(p, variable);
|
|
205
|
+
return {__prepare_argument: true, expression: variable};
|
|
206
|
+
}
|
|
207
|
+
}) as any
|
|
208
|
+
sqlQuery = func(p).toSqlString();
|
|
209
|
+
}
|
|
210
|
+
return sqlQuery;
|
|
211
|
+
}
|
|
212
|
+
return (args: PrepareQueryArguments) => {
|
|
213
|
+
return {
|
|
214
|
+
[SQL_ENTITY]: undefined,
|
|
215
|
+
toSqlString: (): string => {
|
|
216
|
+
let usedQuery = getSqlString();
|
|
217
|
+
seenArguments.forEach((variable, key) => {
|
|
218
|
+
usedQuery = usedQuery.replace(variable, escape(args[key]));
|
|
219
|
+
})
|
|
220
|
+
return usedQuery
|
|
221
|
+
},
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Create a reference for a query that is used in WITH part and you want to use it in from(X) or join(X) call.
|
|
228
|
+
*/
|
|
229
|
+
public static createRef<
|
|
230
|
+
Alias extends string,
|
|
231
|
+
TableRef extends `${string} as ${Alias}`,
|
|
232
|
+
Entity,
|
|
233
|
+
EditEntity,
|
|
234
|
+
NewAlias extends string
|
|
235
|
+
>(table: AliasedTable<Alias, TableRef, Entity, EditEntity, NotUsingWithPart>, newAlias: NewAlias): AliasedTable<NewAlias, `${Alias} as ${NewAlias}`, Entity, EditEntity, Alias> {
|
|
236
|
+
const definition: DbTableDefinition<Entity> = {} as any;
|
|
237
|
+
for (const k in table) {
|
|
238
|
+
(definition as any)[k] = (table as any)[k];
|
|
239
|
+
}
|
|
240
|
+
return MysqlTable.defineDbTable(escapeId(table[SQL_ALIAS]), newAlias, definition) as any;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
}
|
|
244
|
+
|