@grest-ts/sql 0.0.6 → 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/src/Symbols.ts CHANGED
@@ -1,11 +1,11 @@
1
- const SQL_EXPRESSION = Symbol("Table expression")
2
- const SQL_ALIAS = Symbol("Alias")
3
- const SQL_ALIAS_FOR_WITH_QUERY = Symbol("RefAlias")
4
- const SQL_ENTITY = Symbol("Entity")
5
- const SQL_EDIT_ENTITY = Symbol("Edit entity")
6
-
7
- export {SQL_ENTITY};
8
- export {SQL_EDIT_ENTITY};
9
- export {SQL_ALIAS_FOR_WITH_QUERY};
10
- export {SQL_ALIAS};
1
+ const SQL_EXPRESSION = Symbol("Table expression")
2
+ const SQL_ALIAS = Symbol("Alias")
3
+ const SQL_ALIAS_FOR_WITH_QUERY = Symbol("RefAlias")
4
+ const SQL_ENTITY = Symbol("Entity")
5
+ const SQL_EDIT_ENTITY = Symbol("Edit entity")
6
+
7
+ export {SQL_ENTITY};
8
+ export {SQL_EDIT_ENTITY};
9
+ export {SQL_ALIAS_FOR_WITH_QUERY};
10
+ export {SQL_ALIAS};
11
11
  export {SQL_EXPRESSION};
package/src/Types.ts CHANGED
@@ -1,244 +1,244 @@
1
- import {Expr} from "./SqlExpression";
2
- import {SQL_ALIAS, SQL_ALIAS_FOR_WITH_QUERY, SQL_EDIT_ENTITY, SQL_ENTITY, SQL_EXPRESSION} from "./Symbols";
3
- import {ColumnDataType} from "./MysqlTableStructureParser";
4
-
5
- export type Key<Alias extends string> = Record<Alias, true>;
6
-
7
- export type SqlQuery<Result> = {
8
-
9
- [SQL_ENTITY]: Result; // This is needed for typescript typing. It is never populated
10
-
11
- toSqlString(): string
12
-
13
- }
14
-
15
- export type SqlSelectQuery<Result> = SqlQuery<Result> & {
16
-
17
- transformResult<T>(row: T[]): Promise<T[]>;
18
- }
19
-
20
- export type SelectExecutor<Result, Args extends any[] = never, Arg1 extends any = never> = (sql: string, ...args: Args) => Promise<Result[]>
21
-
22
- const TAB = " ";
23
- const DISTINCT = "DISTINCT";
24
- export {DISTINCT, TAB}
25
-
26
- // -----------------------------------------------------
27
-
28
- export type AliasedTable<Alias extends string, TableRef, Entity, EditEntity, AliasForWithQuery extends string | NotUsingWithPart> = {
29
- [SQL_ALIAS]: Alias
30
- [SQL_EXPRESSION]: TableRef
31
- [SQL_ALIAS_FOR_WITH_QUERY]: AliasForWithQuery
32
- [SQL_ENTITY]: Entity
33
- [SQL_EDIT_ENTITY]: EditEntity
34
- } & {
35
- [K in keyof Entity]: Expr<TableRef, K, Entity[K]>
36
- }
37
-
38
- export type AnyAliasedTableDef = AliasedTable<string, string, {}, {}, string | NotUsingWithPart>
39
-
40
- export type DbTableDefinition<T> = {
41
- [P in keyof T]: { type: ColumnDataType, len?: number, default?: any, isAutoIncrement?: true, isPrimary?: true }
42
- };
43
-
44
- export type InsertRow<InsertRow, FullRow> = {
45
- [P in keyof InsertRow]: P extends keyof FullRow ? FullRow[P] : never;
46
- };
47
-
48
- export type UpdateRow<InsertRow, FullRow> = {
49
- [P in keyof InsertRow]?: P extends keyof FullRow ? FullRow[P] : never;
50
- };
51
-
52
- // ----------------------------------------------
53
-
54
- export type NotUsingWithPart = { __not_referenced: true }
55
-
56
- export type PrepareQueryArgument = { __prepare_argument: true, expression: string }
57
-
58
- export function isPrepareArgument(arg: unknown): arg is PrepareQueryArgument {
59
- return (arg as any)?.__prepare_argument === true;
60
- }
61
-
62
- // ----------------------------------------------
63
-
64
- export type SQL_BOOL = 0 | 1;
65
-
66
- export type COMPARISON_SIGNS = "!=" | "<>" | "=" | ">=" | ">" | "<" | "<=" | "LIKE" | "NOT LIKE"
67
-
68
- const ComparisonOperandsLookup: Set<COMPARISON_SIGNS> = new Set(["!=", "=", ">=", ">", "<", "<=", "LIKE", "NOT LIKE"] as const)
69
-
70
- export {ComparisonOperandsLookup}
71
-
72
- export type DATE_ADD_UNITS = "MICROSECOND"
73
- | 'SECOND'
74
- | 'MINUTE'
75
- | 'HOUR'
76
- | 'DAY'
77
- | 'WEEK'
78
- | 'MONTH'
79
- | 'QUARTER'
80
- | 'YEAR'
81
- | 'SECOND_MICROSECOND'
82
- | 'MINUTE_MICROSECOND'
83
- | 'MINUTE_SECOND'
84
- | 'HOUR_MICROSECOND'
85
- | 'HOUR_SECOND'
86
- | 'HOUR_MINUTE'
87
- | 'DAY_MICROSECOND'
88
- | 'DAY_SECOND'
89
- | 'DAY_MINUTE'
90
- | 'DAY_HOUR'
91
- | 'YEAR_MONTH';
92
-
93
- const DateAddUnitsLookup: Set<DATE_ADD_UNITS> = new Set([
94
- "MICROSECOND",
95
- 'SECOND',
96
- 'MINUTE',
97
- 'HOUR',
98
- 'DAY',
99
- 'WEEK',
100
- 'MONTH',
101
- 'QUARTER',
102
- 'YEAR',
103
- 'SECOND_MICROSECOND',
104
- 'MINUTE_MICROSECOND',
105
- 'MINUTE_SECOND',
106
- 'HOUR_MICROSECOND',
107
- 'HOUR_SECOND',
108
- 'HOUR_MINUTE',
109
- 'DAY_MICROSECOND',
110
- 'DAY_SECOND',
111
- 'DAY_MINUTE',
112
- 'DAY_HOUR',
113
- 'YEAR_MONTH'
114
- ] as const);
115
-
116
- export {DateAddUnitsLookup}
117
-
118
- // ----------------------------------------------
119
-
120
- /**
121
- * Check if Alias ("c") already exists in UsedAliases=(R<"c"> & ...)
122
- */
123
- export type isAliasAlreadyUsed<Aliases, Alias extends string, OUT> = Alias extends keyof Aliases ? `Alias '${Alias}' is already used!` : OUT
124
-
125
- // ----------------------------------------------
126
- // This stuff helps to check if 'Table as Alias' exists in usedTables
127
-
128
- type _extractMissingAlias<Tables, CheckTables> = { [K in keyof CheckTables]: K extends keyof Tables ? never : K }[keyof CheckTables];
129
- /**
130
- * Check if UsedTablesToMatch=(R<Alias.Table> & ...) exists in UsedTables=(R<Alias.Table> & ...)
131
- */
132
- // type _AllKeys<T> = T extends any ? keyof T : never;
133
- // export type isTableReferenced<Tables, CheckTables, OUT> = _AllKeys<CheckTables> extends _AllKeys<Tables> might be more correct if unions would be used, but we don't really use them in this place.
134
- export type isTableReferenced<Tables, CheckTables, OUT> = keyof CheckTables extends keyof Tables
135
- ? OUT
136
- : `Table '${_extractMissingAlias<Tables, CheckTables> extends `${infer TableRef}` ? `${TableRef}` : "???"}' is not used in this query!`
137
-
138
- // ----------------------------------------------
139
-
140
- /**
141
- * Anyone finds a better way, please write it. Rules are as follows:
142
- * 1) A is always first
143
- * 2) B can only appear after A
144
- *
145
- To generate this stuff :)
146
- function gen(len) {
147
- if (len <= 0) return [];
148
- const results = new Set(["A"]);
149
- function generateCombinations(currentLen, currentStr) {
150
- if (currentLen >= len) return;
151
- const nextA = currentStr + ',A';
152
- generateCombinations(currentLen + 1, nextA);
153
- results.add(nextA);
154
- if (currentStr[currentStr.length - 1] !== 'B') {
155
- const nextB = currentStr + ',B';
156
- generateCombinations(currentLen, nextB);
157
- results.add(nextB);
158
- }
159
- }
160
- generateCombinations(1, 'A');
161
- return "[" + Array.from(results.values()).sort().join("]\n | [") + "]";
162
- }
163
- console.log(gen(5)); // Example usage
164
- */
165
- export type ORDER_BY = "asc" | "desc" | "ASC" | "DESC"
166
-
167
- export type OrderByStructure<A, B = ORDER_BY> =
168
- [A]
169
- | [A, A]
170
- | [A, A, A]
171
- | [A, A, A, A]
172
- | [A, A, A, A, A]
173
- | [A, A, A, A, B]
174
- | [A, A, A, A, B, A]
175
- | [A, A, A, B]
176
- | [A, A, A, B, A]
177
- | [A, A, A, B, A, A]
178
- | [A, A, A, B, A, B]
179
- | [A, A, A, B, A, B, A]
180
- | [A, A, B]
181
- | [A, A, B, A]
182
- | [A, A, B, A, A]
183
- | [A, A, B, A, A, A]
184
- | [A, A, B, A, A, B]
185
- | [A, A, B, A, A, B, A]
186
- | [A, A, B, A, B]
187
- | [A, A, B, A, B, A]
188
- | [A, A, B, A, B, A, A]
189
- | [A, A, B, A, B, A, B]
190
- | [A, A, B, A, B, A, B, A]
191
- | [A, B]
192
- | [A, B, A]
193
- | [A, B, A, A]
194
- | [A, B, A, A, A]
195
- | [A, B, A, A, A, A]
196
- | [A, B, A, A, A, B]
197
- | [A, B, A, A, A, B, A]
198
- | [A, B, A, A, B]
199
- | [A, B, A, A, B, A]
200
- | [A, B, A, A, B, A, A]
201
- | [A, B, A, A, B, A, B]
202
- | [A, B, A, A, B, A, B, A]
203
- | [A, B, A, B]
204
- | [A, B, A, B, A]
205
- | [A, B, A, B, A, A]
206
- | [A, B, A, B, A, A, A]
207
- | [A, B, A, B, A, A, B]
208
- | [A, B, A, B, A, A, B, A]
209
- | [A, B, A, B, A, B]
210
- | [A, B, A, B, A, B, A]
211
- | [A, B, A, B, A, B, A, A]
212
- | [A, B, A, B, A, B, A, B]
213
- | [A, B, A, B, A, B, A, B, A]
214
-
215
- // ----------------------------------------------
216
-
217
- export type isColumnOkToUse<Tables, ColumnExpressions> =
218
- ColumnExpressions extends []
219
- ? ColumnExpressions
220
- : ColumnExpressions extends [infer A, ...(infer Rest)]
221
- ? [_checkThatTableOrColumnCanBeReferenced<Tables, A>, ...isColumnOkToUse<Tables, Rest>]
222
- : ColumnExpressions;
223
-
224
- type _checkThatTableOrColumnCanBeReferenced<Tables, Expr> =
225
- Expr extends { tableRef: string } ?
226
- Expr["tableRef"] extends keyof Tables ?
227
- Expr
228
- : `Table '${_getMissing<Tables, Expr["tableRef"]>}' is not used in this query!`
229
- : Expr
230
-
231
- type _getMissing<Tables, Check> = Check extends keyof Tables ? never : Check;
232
-
233
- // ----------------------------------------------
234
-
235
- export type IsAMatchingB<Result, Entity, OUT> = _findDifferentPropertyNames<Result, Entity> extends never ? OUT : _findDifferentPropertyNames<Result, Entity>;
236
-
237
- type _valuesToUnion<T> = T[keyof T];
238
-
239
- type _findDifferentPropertyNames<Result, Entity> =
240
- _valuesToUnion<{ [K in Exclude<keyof Entity, keyof Result>]: `Property '${K extends string ? K : ""}' is missing!` }>
241
- | _valuesToUnion<{ [K in Exclude<keyof Result, keyof Entity>]: `Property '${K extends string ? K : ""}' should not be listed!` }>
242
- | _valuesToUnion<{ [K in keyof Result & keyof Entity]: Result[K] extends Entity[K] ? never : `Property '${K extends string ? K : ""}' type does not match!` }>;
243
-
244
- // ----------------------------------------------
1
+ import {Expr} from "./SqlExpression";
2
+ import {SQL_ALIAS, SQL_ALIAS_FOR_WITH_QUERY, SQL_EDIT_ENTITY, SQL_ENTITY, SQL_EXPRESSION} from "./Symbols";
3
+ import {ColumnDataType} from "./MysqlTableStructureParser";
4
+
5
+ export type Key<Alias extends string> = Record<Alias, true>;
6
+
7
+ export type SqlQuery<Result> = {
8
+
9
+ [SQL_ENTITY]: Result; // This is needed for typescript typing. It is never populated
10
+
11
+ toSqlString(): string
12
+
13
+ }
14
+
15
+ export type SqlSelectQuery<Result> = SqlQuery<Result> & {
16
+
17
+ transformResult<T>(row: T[]): Promise<T[]>;
18
+ }
19
+
20
+ export type SelectExecutor<Result, Args extends any[] = never, Arg1 extends any = never> = (sql: string, ...args: Args) => Promise<Result[]>
21
+
22
+ const TAB = " ";
23
+ const DISTINCT = "DISTINCT";
24
+ export {DISTINCT, TAB}
25
+
26
+ // -----------------------------------------------------
27
+
28
+ export type AliasedTable<Alias extends string, TableRef, Entity, EditEntity, AliasForWithQuery extends string | NotUsingWithPart> = {
29
+ [SQL_ALIAS]: Alias
30
+ [SQL_EXPRESSION]: TableRef
31
+ [SQL_ALIAS_FOR_WITH_QUERY]: AliasForWithQuery
32
+ [SQL_ENTITY]: Entity
33
+ [SQL_EDIT_ENTITY]: EditEntity
34
+ } & {
35
+ [K in keyof Entity]: Expr<TableRef, K, Entity[K]>
36
+ }
37
+
38
+ export type AnyAliasedTableDef = AliasedTable<string, string, {}, {}, string | NotUsingWithPart>
39
+
40
+ export type DbTableDefinition<T> = {
41
+ [P in keyof T]: { type: ColumnDataType, len?: number, default?: any, isAutoIncrement?: true, isPrimary?: true }
42
+ };
43
+
44
+ export type InsertRow<InsertRow, FullRow> = {
45
+ [P in keyof InsertRow]: P extends keyof FullRow ? FullRow[P] : never;
46
+ };
47
+
48
+ export type UpdateRow<InsertRow, FullRow> = {
49
+ [P in keyof InsertRow]?: P extends keyof FullRow ? FullRow[P] : never;
50
+ };
51
+
52
+ // ----------------------------------------------
53
+
54
+ export type NotUsingWithPart = { __not_referenced: true }
55
+
56
+ export type PrepareQueryArgument = { __prepare_argument: true, expression: string }
57
+
58
+ export function isPrepareArgument(arg: unknown): arg is PrepareQueryArgument {
59
+ return (arg as any)?.__prepare_argument === true;
60
+ }
61
+
62
+ // ----------------------------------------------
63
+
64
+ export type SQL_BOOL = 0 | 1;
65
+
66
+ export type COMPARISON_SIGNS = "!=" | "<>" | "=" | ">=" | ">" | "<" | "<=" | "LIKE" | "NOT LIKE"
67
+
68
+ const ComparisonOperandsLookup: Set<COMPARISON_SIGNS> = new Set(["!=", "=", ">=", ">", "<", "<=", "LIKE", "NOT LIKE"] as const)
69
+
70
+ export {ComparisonOperandsLookup}
71
+
72
+ export type DATE_ADD_UNITS = "MICROSECOND"
73
+ | 'SECOND'
74
+ | 'MINUTE'
75
+ | 'HOUR'
76
+ | 'DAY'
77
+ | 'WEEK'
78
+ | 'MONTH'
79
+ | 'QUARTER'
80
+ | 'YEAR'
81
+ | 'SECOND_MICROSECOND'
82
+ | 'MINUTE_MICROSECOND'
83
+ | 'MINUTE_SECOND'
84
+ | 'HOUR_MICROSECOND'
85
+ | 'HOUR_SECOND'
86
+ | 'HOUR_MINUTE'
87
+ | 'DAY_MICROSECOND'
88
+ | 'DAY_SECOND'
89
+ | 'DAY_MINUTE'
90
+ | 'DAY_HOUR'
91
+ | 'YEAR_MONTH';
92
+
93
+ const DateAddUnitsLookup: Set<DATE_ADD_UNITS> = new Set([
94
+ "MICROSECOND",
95
+ 'SECOND',
96
+ 'MINUTE',
97
+ 'HOUR',
98
+ 'DAY',
99
+ 'WEEK',
100
+ 'MONTH',
101
+ 'QUARTER',
102
+ 'YEAR',
103
+ 'SECOND_MICROSECOND',
104
+ 'MINUTE_MICROSECOND',
105
+ 'MINUTE_SECOND',
106
+ 'HOUR_MICROSECOND',
107
+ 'HOUR_SECOND',
108
+ 'HOUR_MINUTE',
109
+ 'DAY_MICROSECOND',
110
+ 'DAY_SECOND',
111
+ 'DAY_MINUTE',
112
+ 'DAY_HOUR',
113
+ 'YEAR_MONTH'
114
+ ] as const);
115
+
116
+ export {DateAddUnitsLookup}
117
+
118
+ // ----------------------------------------------
119
+
120
+ /**
121
+ * Check if Alias ("c") already exists in UsedAliases=(R<"c"> & ...)
122
+ */
123
+ export type isAliasAlreadyUsed<Aliases, Alias extends string, OUT> = Alias extends keyof Aliases ? `Alias '${Alias}' is already used!` : OUT
124
+
125
+ // ----------------------------------------------
126
+ // This stuff helps to check if 'Table as Alias' exists in usedTables
127
+
128
+ type _extractMissingAlias<Tables, CheckTables> = { [K in keyof CheckTables]: K extends keyof Tables ? never : K }[keyof CheckTables];
129
+ /**
130
+ * Check if UsedTablesToMatch=(R<Alias.Table> & ...) exists in UsedTables=(R<Alias.Table> & ...)
131
+ */
132
+ // type _AllKeys<T> = T extends any ? keyof T : never;
133
+ // export type isTableReferenced<Tables, CheckTables, OUT> = _AllKeys<CheckTables> extends _AllKeys<Tables> might be more correct if unions would be used, but we don't really use them in this place.
134
+ export type isTableReferenced<Tables, CheckTables, OUT> = keyof CheckTables extends keyof Tables
135
+ ? OUT
136
+ : `Table '${_extractMissingAlias<Tables, CheckTables> extends `${infer TableRef}` ? `${TableRef}` : "???"}' is not used in this query!`
137
+
138
+ // ----------------------------------------------
139
+
140
+ /**
141
+ * Anyone finds a better way, please write it. Rules are as follows:
142
+ * 1) A is always first
143
+ * 2) B can only appear after A
144
+ *
145
+ To generate this stuff :)
146
+ function gen(len) {
147
+ if (len <= 0) return [];
148
+ const results = new Set(["A"]);
149
+ function generateCombinations(currentLen, currentStr) {
150
+ if (currentLen >= len) return;
151
+ const nextA = currentStr + ',A';
152
+ generateCombinations(currentLen + 1, nextA);
153
+ results.add(nextA);
154
+ if (currentStr[currentStr.length - 1] !== 'B') {
155
+ const nextB = currentStr + ',B';
156
+ generateCombinations(currentLen, nextB);
157
+ results.add(nextB);
158
+ }
159
+ }
160
+ generateCombinations(1, 'A');
161
+ return "[" + Array.from(results.values()).sort().join("]\n | [") + "]";
162
+ }
163
+ console.log(gen(5)); // Example usage
164
+ */
165
+ export type ORDER_BY = "asc" | "desc" | "ASC" | "DESC"
166
+
167
+ export type OrderByStructure<A, B = ORDER_BY> =
168
+ [A]
169
+ | [A, A]
170
+ | [A, A, A]
171
+ | [A, A, A, A]
172
+ | [A, A, A, A, A]
173
+ | [A, A, A, A, B]
174
+ | [A, A, A, A, B, A]
175
+ | [A, A, A, B]
176
+ | [A, A, A, B, A]
177
+ | [A, A, A, B, A, A]
178
+ | [A, A, A, B, A, B]
179
+ | [A, A, A, B, A, B, A]
180
+ | [A, A, B]
181
+ | [A, A, B, A]
182
+ | [A, A, B, A, A]
183
+ | [A, A, B, A, A, A]
184
+ | [A, A, B, A, A, B]
185
+ | [A, A, B, A, A, B, A]
186
+ | [A, A, B, A, B]
187
+ | [A, A, B, A, B, A]
188
+ | [A, A, B, A, B, A, A]
189
+ | [A, A, B, A, B, A, B]
190
+ | [A, A, B, A, B, A, B, A]
191
+ | [A, B]
192
+ | [A, B, A]
193
+ | [A, B, A, A]
194
+ | [A, B, A, A, A]
195
+ | [A, B, A, A, A, A]
196
+ | [A, B, A, A, A, B]
197
+ | [A, B, A, A, A, B, A]
198
+ | [A, B, A, A, B]
199
+ | [A, B, A, A, B, A]
200
+ | [A, B, A, A, B, A, A]
201
+ | [A, B, A, A, B, A, B]
202
+ | [A, B, A, A, B, A, B, A]
203
+ | [A, B, A, B]
204
+ | [A, B, A, B, A]
205
+ | [A, B, A, B, A, A]
206
+ | [A, B, A, B, A, A, A]
207
+ | [A, B, A, B, A, A, B]
208
+ | [A, B, A, B, A, A, B, A]
209
+ | [A, B, A, B, A, B]
210
+ | [A, B, A, B, A, B, A]
211
+ | [A, B, A, B, A, B, A, A]
212
+ | [A, B, A, B, A, B, A, B]
213
+ | [A, B, A, B, A, B, A, B, A]
214
+
215
+ // ----------------------------------------------
216
+
217
+ export type isColumnOkToUse<Tables, ColumnExpressions> =
218
+ ColumnExpressions extends []
219
+ ? ColumnExpressions
220
+ : ColumnExpressions extends [infer A, ...(infer Rest)]
221
+ ? [_checkThatTableOrColumnCanBeReferenced<Tables, A>, ...isColumnOkToUse<Tables, Rest>]
222
+ : ColumnExpressions;
223
+
224
+ type _checkThatTableOrColumnCanBeReferenced<Tables, Expr> =
225
+ Expr extends { tableRef: string } ?
226
+ Expr["tableRef"] extends keyof Tables ?
227
+ Expr
228
+ : `Table '${_getMissing<Tables, Expr["tableRef"]>}' is not used in this query!`
229
+ : Expr
230
+
231
+ type _getMissing<Tables, Check> = Check extends keyof Tables ? never : Check;
232
+
233
+ // ----------------------------------------------
234
+
235
+ export type IsAMatchingB<Result, Entity, OUT> = _findDifferentPropertyNames<Result, Entity> extends never ? OUT : _findDifferentPropertyNames<Result, Entity>;
236
+
237
+ type _valuesToUnion<T> = T[keyof T];
238
+
239
+ type _findDifferentPropertyNames<Result, Entity> =
240
+ _valuesToUnion<{ [K in Exclude<keyof Entity, keyof Result>]: `Property '${K extends string ? K : ""}' is missing!` }>
241
+ | _valuesToUnion<{ [K in Exclude<keyof Result, keyof Entity>]: `Property '${K extends string ? K : ""}' should not be listed!` }>
242
+ | _valuesToUnion<{ [K in keyof Result & keyof Entity]: Result[K] extends Entity[K] ? never : `Property '${K extends string ? K : ""}' type does not match!` }>;
243
+
244
+ // ----------------------------------------------
@@ -1,65 +1,65 @@
1
- import {OrderByStructure, TAB} from "../Types";
2
- import {escapeId} from "../escape";
3
- import {AnyExpr, SqlExpression} from "../SqlExpression";
4
- import {orderByStructureToSqlString} from "../SqlFunctions";
5
- import {DeleteLimitMethods, DeleteOrderByMethods, DeleteWhereMethods, ExecDeleteMethods, GatewayDeleteOrderByMethods, GatewayDeleteWhereMethods} from "./DeleteInterfaces";
6
- import {SQL_ENTITY} from "../Symbols";
7
-
8
- export class DeleteBuilder<Tables, Entity> implements DeleteWhereMethods<Tables>,
9
- DeleteOrderByMethods<Tables>,
10
- DeleteLimitMethods,
11
- ExecDeleteMethods,
12
- GatewayDeleteWhereMethods<Entity>,
13
- GatewayDeleteOrderByMethods<Entity> {
14
-
15
- public readonly [SQL_ENTITY]: undefined; // never used
16
-
17
- private _from: string;
18
- private readonly _where: string[] = [];
19
- private readonly _orderBy: string[] = [];
20
- private _limit: string;
21
-
22
- public from(table: string, alias: string): this {
23
- this._from = table + (alias ? " as " + escapeId(alias) : "");
24
- return this;
25
- }
26
-
27
- public where(...cols: any | SqlExpression<any, any, any>): this {
28
- for (let i = 0; i < cols.length; i++) {
29
- if (cols[i]) {
30
- this._where.push(cols[i].expression)
31
- }
32
- }
33
- return this
34
- }
35
-
36
- public orderBy(...items: any | OrderByStructure<AnyExpr>): this {
37
- this._orderBy.push(...orderByStructureToSqlString(items))
38
- return this;
39
- }
40
-
41
- public noLimit(): this {
42
- return this;
43
- }
44
-
45
- public limit1(): this {
46
- return this.limit(1);
47
- }
48
-
49
- public limit(limit: number | [number, number]): this {
50
- if (Array.isArray(limit)) {
51
- this._limit = Number(limit[0]) + "," + Number(limit[1]);
52
- } else {
53
- this._limit = String(Number(limit));
54
- }
55
- return this;
56
- }
57
-
58
- public toSqlString(lvl: number = 0): string {
59
- const tabs = TAB.repeat(lvl);
60
- return "DELETE FROM " + this._from + "\n" +
61
- (this._where.length === 0 ? tabs + "WHERE " + this._where.join(" AND ") + "\n" : "WHERE 1=2") +
62
- (this._orderBy.length > 0 ? tabs + "ORDER BY " + this._orderBy.join(", ") + "\n" : "") +
63
- (this._limit ? tabs + "LIMIT " + this._limit + "\n" : "")
64
- }
1
+ import {OrderByStructure, TAB} from "../Types";
2
+ import {escapeId} from "../escape";
3
+ import {AnyExpr, SqlExpression} from "../SqlExpression";
4
+ import {orderByStructureToSqlString} from "../SqlFunctions";
5
+ import {DeleteLimitMethods, DeleteOrderByMethods, DeleteWhereMethods, ExecDeleteMethods, GatewayDeleteOrderByMethods, GatewayDeleteWhereMethods} from "./DeleteInterfaces";
6
+ import {SQL_ENTITY} from "../Symbols";
7
+
8
+ export class DeleteBuilder<Tables, Entity> implements DeleteWhereMethods<Tables>,
9
+ DeleteOrderByMethods<Tables>,
10
+ DeleteLimitMethods,
11
+ ExecDeleteMethods,
12
+ GatewayDeleteWhereMethods<Entity>,
13
+ GatewayDeleteOrderByMethods<Entity> {
14
+
15
+ public readonly [SQL_ENTITY]: undefined; // never used
16
+
17
+ private _from: string;
18
+ private readonly _where: string[] = [];
19
+ private readonly _orderBy: string[] = [];
20
+ private _limit: string;
21
+
22
+ public from(table: string, alias: string): this {
23
+ this._from = table + (alias ? " as " + escapeId(alias) : "");
24
+ return this;
25
+ }
26
+
27
+ public where(...cols: any | SqlExpression<any, any, any>): this {
28
+ for (let i = 0; i < cols.length; i++) {
29
+ if (cols[i]) {
30
+ this._where.push(cols[i].expression)
31
+ }
32
+ }
33
+ return this
34
+ }
35
+
36
+ public orderBy(...items: any | OrderByStructure<AnyExpr>): this {
37
+ this._orderBy.push(...orderByStructureToSqlString(items))
38
+ return this;
39
+ }
40
+
41
+ public noLimit(): this {
42
+ return this;
43
+ }
44
+
45
+ public limit1(): this {
46
+ return this.limit(1);
47
+ }
48
+
49
+ public limit(limit: number | [number, number]): this {
50
+ if (Array.isArray(limit)) {
51
+ this._limit = Number(limit[0]) + "," + Number(limit[1]);
52
+ } else {
53
+ this._limit = String(Number(limit));
54
+ }
55
+ return this;
56
+ }
57
+
58
+ public toSqlString(lvl: number = 0): string {
59
+ const tabs = TAB.repeat(lvl);
60
+ return "DELETE FROM " + this._from + "\n" +
61
+ (this._where.length === 0 ? tabs + "WHERE " + this._where.join(" AND ") + "\n" : "WHERE 1=2") +
62
+ (this._orderBy.length > 0 ? tabs + "ORDER BY " + this._orderBy.join(", ") + "\n" : "") +
63
+ (this._limit ? tabs + "LIMIT " + this._limit + "\n" : "")
64
+ }
65
65
  }