UNPKG

18.6 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var Query_1 = require("../driver/Query");
5var SqlInMemory_1 = require("../driver/SqlInMemory");
6var PromiseUtils_1 = require("../util/PromiseUtils");
7var BaseQueryRunner = /** @class */ (function () {
8 function BaseQueryRunner() {
9 // -------------------------------------------------------------------------
10 // Public Properties
11 // -------------------------------------------------------------------------
12 /**
13 * Indicates if connection for this query runner is released.
14 * Once its released, query runner cannot run queries anymore.
15 */
16 this.isReleased = false;
17 /**
18 * Indicates if transaction is in progress.
19 */
20 this.isTransactionActive = false;
21 /**
22 * Stores temporarily user data.
23 * Useful for sharing data with subscribers.
24 */
25 this.data = {};
26 /**
27 * All synchronized tables in the database.
28 */
29 this.loadedTables = [];
30 /**
31 * All synchronized views in the database.
32 */
33 this.loadedViews = [];
34 /**
35 * Indicates if special query runner mode in which sql queries won't be executed is enabled.
36 */
37 this.sqlMemoryMode = false;
38 /**
39 * Sql-s stored if "sql in memory" mode is enabled.
40 */
41 this.sqlInMemory = new SqlInMemory_1.SqlInMemory();
42 }
43 // -------------------------------------------------------------------------
44 // Public Methods
45 // -------------------------------------------------------------------------
46 /**
47 * Loads given table's data from the database.
48 */
49 BaseQueryRunner.prototype.getTable = function (tablePath) {
50 return tslib_1.__awaiter(this, void 0, void 0, function () {
51 var _a;
52 return tslib_1.__generator(this, function (_b) {
53 switch (_b.label) {
54 case 0:
55 _a = this;
56 return [4 /*yield*/, this.loadTables([tablePath])];
57 case 1:
58 _a.loadedTables = _b.sent();
59 return [2 /*return*/, this.loadedTables.length > 0 ? this.loadedTables[0] : undefined];
60 }
61 });
62 });
63 };
64 /**
65 * Loads all tables (with given names) from the database.
66 */
67 BaseQueryRunner.prototype.getTables = function (tableNames) {
68 return tslib_1.__awaiter(this, void 0, void 0, function () {
69 var _a;
70 return tslib_1.__generator(this, function (_b) {
71 switch (_b.label) {
72 case 0:
73 _a = this;
74 return [4 /*yield*/, this.loadTables(tableNames)];
75 case 1:
76 _a.loadedTables = _b.sent();
77 return [2 /*return*/, this.loadedTables];
78 }
79 });
80 });
81 };
82 /**
83 * Loads given view's data from the database.
84 */
85 BaseQueryRunner.prototype.getView = function (viewPath) {
86 return tslib_1.__awaiter(this, void 0, void 0, function () {
87 var _a;
88 return tslib_1.__generator(this, function (_b) {
89 switch (_b.label) {
90 case 0:
91 _a = this;
92 return [4 /*yield*/, this.loadViews([viewPath])];
93 case 1:
94 _a.loadedViews = _b.sent();
95 return [2 /*return*/, this.loadedViews.length > 0 ? this.loadedViews[0] : undefined];
96 }
97 });
98 });
99 };
100 /**
101 * Loads given view's data from the database.
102 */
103 BaseQueryRunner.prototype.getViews = function (viewPaths) {
104 return tslib_1.__awaiter(this, void 0, void 0, function () {
105 var _a;
106 return tslib_1.__generator(this, function (_b) {
107 switch (_b.label) {
108 case 0:
109 _a = this;
110 return [4 /*yield*/, this.loadViews(viewPaths)];
111 case 1:
112 _a.loadedViews = _b.sent();
113 return [2 /*return*/, this.loadedViews];
114 }
115 });
116 });
117 };
118 /**
119 * Enables special query runner mode in which sql queries won't be executed,
120 * instead they will be memorized into a special variable inside query runner.
121 * You can get memorized sql using getMemorySql() method.
122 */
123 BaseQueryRunner.prototype.enableSqlMemory = function () {
124 this.sqlInMemory = new SqlInMemory_1.SqlInMemory();
125 this.sqlMemoryMode = true;
126 };
127 /**
128 * Disables special query runner mode in which sql queries won't be executed
129 * started by calling enableSqlMemory() method.
130 *
131 * Previously memorized sql will be flushed.
132 */
133 BaseQueryRunner.prototype.disableSqlMemory = function () {
134 this.sqlInMemory = new SqlInMemory_1.SqlInMemory();
135 this.sqlMemoryMode = false;
136 };
137 /**
138 * Flushes all memorized sqls.
139 */
140 BaseQueryRunner.prototype.clearSqlMemory = function () {
141 this.sqlInMemory = new SqlInMemory_1.SqlInMemory();
142 };
143 /**
144 * Gets sql stored in the memory. Parameters in the sql are already replaced.
145 */
146 BaseQueryRunner.prototype.getMemorySql = function () {
147 return this.sqlInMemory;
148 };
149 /**
150 * Executes up sql queries.
151 */
152 BaseQueryRunner.prototype.executeMemoryUpSql = function () {
153 return tslib_1.__awaiter(this, void 0, void 0, function () {
154 var _this = this;
155 return tslib_1.__generator(this, function (_a) {
156 switch (_a.label) {
157 case 0: return [4 /*yield*/, PromiseUtils_1.PromiseUtils.runInSequence(this.sqlInMemory.upQueries, function (upQuery) { return _this.query(upQuery.query, upQuery.parameters); })];
158 case 1:
159 _a.sent();
160 return [2 /*return*/];
161 }
162 });
163 });
164 };
165 /**
166 * Executes down sql queries.
167 */
168 BaseQueryRunner.prototype.executeMemoryDownSql = function () {
169 return tslib_1.__awaiter(this, void 0, void 0, function () {
170 var _this = this;
171 return tslib_1.__generator(this, function (_a) {
172 switch (_a.label) {
173 case 0: return [4 /*yield*/, PromiseUtils_1.PromiseUtils.runInSequence(this.sqlInMemory.downQueries.reverse(), function (downQuery) { return _this.query(downQuery.query, downQuery.parameters); })];
174 case 1:
175 _a.sent();
176 return [2 /*return*/];
177 }
178 });
179 });
180 };
181 // -------------------------------------------------------------------------
182 // Protected Methods
183 // -------------------------------------------------------------------------
184 /**
185 * Gets view from previously loaded views, otherwise loads it from database.
186 */
187 BaseQueryRunner.prototype.getCachedView = function (viewName) {
188 return tslib_1.__awaiter(this, void 0, void 0, function () {
189 var view, foundViews;
190 return tslib_1.__generator(this, function (_a) {
191 switch (_a.label) {
192 case 0:
193 view = this.loadedViews.find(function (view) { return view.name === viewName; });
194 if (view)
195 return [2 /*return*/, view];
196 return [4 /*yield*/, this.loadViews([viewName])];
197 case 1:
198 foundViews = _a.sent();
199 if (foundViews.length > 0) {
200 this.loadedViews.push(foundViews[0]);
201 return [2 /*return*/, foundViews[0]];
202 }
203 else {
204 throw new Error("View \"" + viewName + "\" does not exist.");
205 }
206 return [2 /*return*/];
207 }
208 });
209 });
210 };
211 /**
212 * Gets table from previously loaded tables, otherwise loads it from database.
213 */
214 BaseQueryRunner.prototype.getCachedTable = function (tableName) {
215 return tslib_1.__awaiter(this, void 0, void 0, function () {
216 var table, foundTables;
217 return tslib_1.__generator(this, function (_a) {
218 switch (_a.label) {
219 case 0:
220 table = this.loadedTables.find(function (table) { return table.name === tableName; });
221 if (table)
222 return [2 /*return*/, table];
223 return [4 /*yield*/, this.loadTables([tableName])];
224 case 1:
225 foundTables = _a.sent();
226 if (foundTables.length > 0) {
227 this.loadedTables.push(foundTables[0]);
228 return [2 /*return*/, foundTables[0]];
229 }
230 else {
231 throw new Error("Table \"" + tableName + "\" does not exist.");
232 }
233 return [2 /*return*/];
234 }
235 });
236 });
237 };
238 /**
239 * Replaces loaded table with given changed table.
240 */
241 BaseQueryRunner.prototype.replaceCachedTable = function (table, changedTable) {
242 var foundTable = this.loadedTables.find(function (loadedTable) { return loadedTable.name === table.name; });
243 if (foundTable) {
244 foundTable.name = changedTable.name;
245 foundTable.columns = changedTable.columns;
246 foundTable.indices = changedTable.indices;
247 foundTable.foreignKeys = changedTable.foreignKeys;
248 foundTable.uniques = changedTable.uniques;
249 foundTable.checks = changedTable.checks;
250 foundTable.justCreated = changedTable.justCreated;
251 foundTable.engine = changedTable.engine;
252 }
253 };
254 BaseQueryRunner.prototype.getTypeormMetadataTableName = function () {
255 var options = this.connection.driver.options;
256 return this.connection.driver.buildTableName("typeorm_metadata", options.schema, options.database);
257 };
258 /**
259 * Checks if at least one of column properties was changed.
260 * Does not checks column type, length and autoincrement, because these properties changes separately.
261 */
262 BaseQueryRunner.prototype.isColumnChanged = function (oldColumn, newColumn, checkDefault, checkComment) {
263 // this logs need to debug issues in column change detection. Do not delete it!
264 // console.log("charset ---------------");
265 // console.log(oldColumn.charset !== newColumn.charset);
266 // console.log(oldColumn.charset, newColumn.charset);
267 // console.log("collation ---------------");
268 // console.log(oldColumn.collation !== newColumn.collation);
269 // console.log(oldColumn.collation, newColumn.collation);
270 // console.log("precision ---------------");
271 // console.log(oldColumn.precision !== newColumn.precision);
272 // console.log(oldColumn.precision, newColumn.precision);
273 // console.log("scale ---------------");
274 // console.log(oldColumn.scale !== newColumn.scale);
275 // console.log(oldColumn.scale, newColumn.scale);
276 // console.log("default ---------------");
277 // console.log((checkDefault && oldColumn.default !== newColumn.default));
278 // console.log(oldColumn.default, newColumn.default);
279 // console.log("isNullable ---------------");
280 // console.log(oldColumn.isNullable !== newColumn.isNullable);
281 // console.log(oldColumn.isNullable, newColumn.isNullable);
282 // console.log("comment ---------------");
283 // console.log((checkComment && oldColumn.comment !== newColumn.comment));
284 // console.log(oldColumn.comment, newColumn.comment);
285 // console.log("enum ---------------");
286 // console.log(oldColumn.enum !== newColumn.enum);
287 // console.log(oldColumn.enum, newColumn.enum);
288 return oldColumn.charset !== newColumn.charset
289 || oldColumn.collation !== newColumn.collation
290 || oldColumn.precision !== newColumn.precision
291 || oldColumn.scale !== newColumn.scale
292 || oldColumn.width !== newColumn.width // MySQL only
293 || oldColumn.zerofill !== newColumn.zerofill // MySQL only
294 || oldColumn.unsigned !== newColumn.unsigned // MySQL only
295 || oldColumn.asExpression !== newColumn.asExpression // MySQL only
296 || (checkDefault && oldColumn.default !== newColumn.default)
297 || oldColumn.onUpdate !== newColumn.onUpdate // MySQL only
298 || oldColumn.isNullable !== newColumn.isNullable
299 || (checkComment && oldColumn.comment !== newColumn.comment)
300 || oldColumn.enum !== newColumn.enum;
301 };
302 /**
303 * Checks if column length is by default.
304 */
305 BaseQueryRunner.prototype.isDefaultColumnLength = function (table, column, length) {
306 // if table have metadata, we check if length is specified in column metadata
307 if (this.connection.hasMetadata(table.name)) {
308 var metadata = this.connection.getMetadata(table.name);
309 var columnMetadata = metadata.findColumnWithDatabaseName(column.name);
310 if (columnMetadata && columnMetadata.length)
311 return false;
312 }
313 if (this.connection.driver.dataTypeDefaults
314 && this.connection.driver.dataTypeDefaults[column.type]
315 && this.connection.driver.dataTypeDefaults[column.type].length) {
316 return this.connection.driver.dataTypeDefaults[column.type].length.toString() === length.toString();
317 }
318 return false;
319 };
320 /**
321 * Checks if column display width is by default. Used only for MySQL.
322 */
323 BaseQueryRunner.prototype.isDefaultColumnWidth = function (table, column, width) {
324 // if table have metadata, we check if length is specified in column metadata
325 if (this.connection.hasMetadata(table.name)) {
326 var metadata = this.connection.getMetadata(table.name);
327 var columnMetadata = metadata.findColumnWithDatabaseName(column.name);
328 if (columnMetadata && columnMetadata.width)
329 return false;
330 }
331 if (this.connection.driver.dataTypeDefaults
332 && this.connection.driver.dataTypeDefaults[column.type]
333 && this.connection.driver.dataTypeDefaults[column.type].width) {
334 return this.connection.driver.dataTypeDefaults[column.type].width === width;
335 }
336 return false;
337 };
338 /**
339 * Checks if column precision is by default.
340 */
341 BaseQueryRunner.prototype.isDefaultColumnPrecision = function (table, column, precision) {
342 // if table have metadata, we check if length is specified in column metadata
343 if (this.connection.hasMetadata(table.name)) {
344 var metadata = this.connection.getMetadata(table.name);
345 var columnMetadata = metadata.findColumnWithDatabaseName(column.name);
346 if (columnMetadata && columnMetadata.precision !== null && columnMetadata.precision !== undefined)
347 return false;
348 }
349 if (this.connection.driver.dataTypeDefaults
350 && this.connection.driver.dataTypeDefaults[column.type]
351 && this.connection.driver.dataTypeDefaults[column.type].precision !== null
352 && this.connection.driver.dataTypeDefaults[column.type].precision !== undefined)
353 return this.connection.driver.dataTypeDefaults[column.type].precision === precision;
354 return false;
355 };
356 /**
357 * Checks if column scale is by default.
358 */
359 BaseQueryRunner.prototype.isDefaultColumnScale = function (table, column, scale) {
360 // if table have metadata, we check if length is specified in column metadata
361 if (this.connection.hasMetadata(table.name)) {
362 var metadata = this.connection.getMetadata(table.name);
363 var columnMetadata = metadata.findColumnWithDatabaseName(column.name);
364 if (columnMetadata && columnMetadata.scale !== null && columnMetadata.scale !== undefined)
365 return false;
366 }
367 if (this.connection.driver.dataTypeDefaults
368 && this.connection.driver.dataTypeDefaults[column.type]
369 && this.connection.driver.dataTypeDefaults[column.type].scale !== null
370 && this.connection.driver.dataTypeDefaults[column.type].scale !== undefined)
371 return this.connection.driver.dataTypeDefaults[column.type].scale === scale;
372 return false;
373 };
374 /**
375 * Executes sql used special for schema build.
376 */
377 BaseQueryRunner.prototype.executeQueries = function (upQueries, downQueries) {
378 return tslib_1.__awaiter(this, void 0, void 0, function () {
379 var _a, _b;
380 var _this = this;
381 return tslib_1.__generator(this, function (_c) {
382 switch (_c.label) {
383 case 0:
384 if (upQueries instanceof Query_1.Query)
385 upQueries = [upQueries];
386 if (downQueries instanceof Query_1.Query)
387 downQueries = [downQueries];
388 (_a = this.sqlInMemory.upQueries).push.apply(_a, tslib_1.__spread(upQueries));
389 (_b = this.sqlInMemory.downQueries).push.apply(_b, tslib_1.__spread(downQueries));
390 // if sql-in-memory mode is enabled then simply store sql in memory and return
391 if (this.sqlMemoryMode === true)
392 return [2 /*return*/, Promise.resolve()];
393 return [4 /*yield*/, PromiseUtils_1.PromiseUtils.runInSequence(upQueries, function (upQuery) { return _this.query(upQuery.query, upQuery.parameters); })];
394 case 1:
395 _c.sent();
396 return [2 /*return*/];
397 }
398 });
399 });
400 };
401 return BaseQueryRunner;
402}());
403exports.BaseQueryRunner = BaseQueryRunner;
404
405//# sourceMappingURL=BaseQueryRunner.js.map