/
BaseQueryRunner.ts
349 lines (298 loc) · 13.5 KB
/
BaseQueryRunner.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import {SqlInMemory} from "../driver/SqlInMemory";
import {PromiseUtils} from "../util/PromiseUtils";
import {Connection} from "../connection/Connection";
import {Table} from "../schema-builder/table/Table";
import {EntityManager} from "../entity-manager/EntityManager";
import {TableColumn} from "../schema-builder/table/TableColumn";
import {Broadcaster} from "../subscriber/Broadcaster";
export abstract class BaseQueryRunner {
// -------------------------------------------------------------------------
// Public Properties
// -------------------------------------------------------------------------
/**
* Connection used by this query runner.
*/
connection: Connection;
/**
* Entity manager working only with current query runner.
*/
manager: EntityManager;
/**
* Indicates if connection for this query runner is released.
* Once its released, query runner cannot run queries anymore.
*/
isReleased = false;
/**
* Indicates if transaction is in progress.
*/
isTransactionActive = false;
/**
* Stores temporarily user data.
* Useful for sharing data with subscribers.
*/
data = {};
/**
* All synchronized tables in the database.
*/
loadedTables: Table[] = [];
/**
* Broadcaster used on this query runner to broadcast entity events.
*/
broadcaster: Broadcaster;
// -------------------------------------------------------------------------
// Protected Properties
// -------------------------------------------------------------------------
/**
* Real database connection from a connection pool used to perform queries.
*/
protected databaseConnection: any;
/**
* Indicates if special query runner mode in which sql queries won't be executed is enabled.
*/
protected sqlMemoryMode: boolean = false;
/**
* Sql-s stored if "sql in memory" mode is enabled.
*/
protected sqlInMemory: SqlInMemory = new SqlInMemory();
/**
* Mode in which query runner executes.
* Used for replication.
* If replication is not setup its value is ignored.
*/
protected mode: "master"|"slave";
// -------------------------------------------------------------------------
// Public Abstract Methods
// -------------------------------------------------------------------------
/**
* Executes a given SQL query.
*/
abstract query(query: string, parameters?: any[]): Promise<any>;
// -------------------------------------------------------------------------
// Protected Abstract Methods
// -------------------------------------------------------------------------
protected abstract async loadTables(tablePaths: string[]): Promise<Table[]>;
// -------------------------------------------------------------------------
// Public Methods
// -------------------------------------------------------------------------
/**
* Loads given table's data from the database.
*/
async getTable(tablePath: string): Promise<Table|undefined> {
this.loadedTables = await this.loadTables([tablePath]);
return this.loadedTables.length > 0 ? this.loadedTables[0] : undefined;
}
/**
* Loads all tables (with given names) from the database.
*/
async getTables(tableNames: string[]): Promise<Table[]> {
this.loadedTables = await this.loadTables(tableNames);
return this.loadedTables;
}
/**
* Enables special query runner mode in which sql queries won't be executed,
* instead they will be memorized into a special variable inside query runner.
* You can get memorized sql using getMemorySql() method.
*/
enableSqlMemory(): void {
this.sqlInMemory = new SqlInMemory();
this.sqlMemoryMode = true;
}
/**
* Disables special query runner mode in which sql queries won't be executed
* started by calling enableSqlMemory() method.
*
* Previously memorized sql will be flushed.
*/
disableSqlMemory(): void {
this.sqlInMemory = new SqlInMemory();
this.sqlMemoryMode = false;
}
/**
* Flushes all memorized sqls.
*/
clearSqlMemory(): void {
this.sqlInMemory = new SqlInMemory();
}
/**
* Gets sql stored in the memory. Parameters in the sql are already replaced.
*/
getMemorySql(): SqlInMemory {
return this.sqlInMemory;
}
/**
* Executes up sql queries.
*/
async executeMemoryUpSql(): Promise<void> {
await PromiseUtils.runInSequence(this.sqlInMemory.upQueries, downQuery => this.query(downQuery));
}
/**
* Executes down sql queries.
*/
async executeMemoryDownSql(): Promise<void> {
await PromiseUtils.runInSequence(this.sqlInMemory.downQueries.reverse(), downQuery => this.query(downQuery));
}
// -------------------------------------------------------------------------
// Protected Methods
// -------------------------------------------------------------------------
/**
* Gets table from previously loaded tables, otherwise loads it from database.
*/
protected async getCachedTable(tableName: string): Promise<Table> {
const table = this.loadedTables.find(table => table.name === tableName);
if (table) return table;
const foundTables = await this.loadTables([tableName]);
if (foundTables.length > 0) {
this.loadedTables.push(foundTables[0]);
return foundTables[0];
} else {
throw new Error(`Table "${tableName}" does not exist.`);
}
}
/**
* Replaces loaded table with given changed table.
*/
protected replaceCachedTable(table: Table, changedTable: Table): void {
const foundTable = this.loadedTables.find(loadedTable => loadedTable.name === table.name);
if (foundTable) {
foundTable.name = changedTable.name;
foundTable.columns = changedTable.columns;
foundTable.indices = changedTable.indices;
foundTable.foreignKeys = changedTable.foreignKeys;
foundTable.uniques = changedTable.uniques;
foundTable.checks = changedTable.checks;
foundTable.justCreated = changedTable.justCreated;
foundTable.engine = changedTable.engine;
}
}
/**
* Checks if at least one of column properties was changed.
* Does not checks column type, length and autoincrement, because these properties changes separately.
*/
protected isColumnChanged(oldColumn: TableColumn, newColumn: TableColumn, checkDefault?: boolean, checkComment?: boolean): boolean {
// this logs need to debug issues in column change detection. Do not delete it!
// console.log("charset ---------------");
// console.log(oldColumn.charset !== newColumn.charset);
// console.log(oldColumn.charset, newColumn.charset);
// console.log("collation ---------------");
// console.log(oldColumn.collation !== newColumn.collation);
// console.log(oldColumn.collation, newColumn.collation);
// console.log("precision ---------------");
// console.log(oldColumn.precision !== newColumn.precision);
// console.log(oldColumn.precision, newColumn.precision);
// console.log("scale ---------------");
// console.log(oldColumn.scale !== newColumn.scale);
// console.log(oldColumn.scale, newColumn.scale);
// console.log("default ---------------");
// console.log((checkDefault && oldColumn.default !== newColumn.default));
// console.log(oldColumn.default, newColumn.default);
// console.log("isNullable ---------------");
// console.log(oldColumn.isNullable !== newColumn.isNullable);
// console.log(oldColumn.isNullable, newColumn.isNullable);
// console.log("comment ---------------");
// console.log((checkComment && oldColumn.comment !== newColumn.comment));
// console.log(oldColumn.comment, newColumn.comment);
// console.log("enum ---------------");
// console.log(oldColumn.enum !== newColumn.enum);
// console.log(oldColumn.enum, newColumn.enum);
return oldColumn.charset !== newColumn.charset
|| oldColumn.collation !== newColumn.collation
|| oldColumn.precision !== newColumn.precision
|| oldColumn.scale !== newColumn.scale
|| oldColumn.width !== newColumn.width // MySQL only
|| oldColumn.zerofill !== newColumn.zerofill // MySQL only
|| oldColumn.unsigned !== newColumn.unsigned // MySQL only
|| oldColumn.asExpression !== newColumn.asExpression // MySQL only
|| (checkDefault && oldColumn.default !== newColumn.default)
|| oldColumn.onUpdate !== newColumn.onUpdate // MySQL only
|| oldColumn.isNullable !== newColumn.isNullable
|| (checkComment && oldColumn.comment !== newColumn.comment)
|| oldColumn.enum !== newColumn.enum;
}
/**
* Checks if column length is by default.
*/
protected isDefaultColumnLength(table: Table, column: TableColumn, length: string): boolean {
// if table have metadata, we check if length is specified in column metadata
if (this.connection.hasMetadata(table.name)) {
const metadata = this.connection.getMetadata(table.name);
const columnMetadata = metadata.findColumnWithDatabaseName(column.name);
if (columnMetadata && columnMetadata.length)
return false;
}
if (this.connection.driver.dataTypeDefaults
&& this.connection.driver.dataTypeDefaults[column.type]
&& this.connection.driver.dataTypeDefaults[column.type].length) {
return this.connection.driver.dataTypeDefaults[column.type].length!.toString() === length.toString();
}
return false;
}
/**
* Checks if column display width is by default. Used only for MySQL.
*/
protected isDefaultColumnWidth(table: Table, column: TableColumn, width: number): boolean {
// if table have metadata, we check if length is specified in column metadata
if (this.connection.hasMetadata(table.name)) {
const metadata = this.connection.getMetadata(table.name);
const columnMetadata = metadata.findColumnWithDatabaseName(column.name);
if (columnMetadata && columnMetadata.width)
return false;
}
if (this.connection.driver.dataTypeDefaults
&& this.connection.driver.dataTypeDefaults[column.type]
&& this.connection.driver.dataTypeDefaults[column.type].width) {
return this.connection.driver.dataTypeDefaults[column.type].width === width;
}
return false;
}
/**
* Checks if column precision is by default.
*/
protected isDefaultColumnPrecision(table: Table, column: TableColumn, precision: number): boolean {
// if table have metadata, we check if length is specified in column metadata
if (this.connection.hasMetadata(table.name)) {
const metadata = this.connection.getMetadata(table.name);
const columnMetadata = metadata.findColumnWithDatabaseName(column.name);
if (columnMetadata && columnMetadata.precision !== null && columnMetadata.precision !== undefined)
return false;
}
if (this.connection.driver.dataTypeDefaults
&& this.connection.driver.dataTypeDefaults[column.type]
&& this.connection.driver.dataTypeDefaults[column.type].precision !== null
&& this.connection.driver.dataTypeDefaults[column.type].precision !== undefined)
return this.connection.driver.dataTypeDefaults[column.type].precision === precision;
return false;
}
/**
* Checks if column scale is by default.
*/
protected isDefaultColumnScale(table: Table, column: TableColumn, scale: number): boolean {
// if table have metadata, we check if length is specified in column metadata
if (this.connection.hasMetadata(table.name)) {
const metadata = this.connection.getMetadata(table.name);
const columnMetadata = metadata.findColumnWithDatabaseName(column.name);
if (columnMetadata && columnMetadata.scale !== null && columnMetadata.scale !== undefined)
return false;
}
if (this.connection.driver.dataTypeDefaults
&& this.connection.driver.dataTypeDefaults[column.type]
&& this.connection.driver.dataTypeDefaults[column.type].scale !== null
&& this.connection.driver.dataTypeDefaults[column.type].scale !== undefined)
return this.connection.driver.dataTypeDefaults[column.type].scale === scale;
return false;
}
/**
* Executes sql used special for schema build.
*/
protected async executeQueries(upQueries: string|string[], downQueries: string|string[]): Promise<void> {
if (typeof upQueries === "string")
upQueries = [upQueries];
if (typeof downQueries === "string")
downQueries = [downQueries];
this.sqlInMemory.upQueries.push(...upQueries);
this.sqlInMemory.downQueries.push(...downQueries);
// if sql-in-memory mode is enabled then simply store sql in memory and return
if (this.sqlMemoryMode === true)
return Promise.resolve() as Promise<any>;
await PromiseUtils.runInSequence(upQueries, upQuery => this.query(upQuery));
}
}