1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var Query_1 = require("../driver/Query");
|
5 | var SqlInMemory_1 = require("../driver/SqlInMemory");
|
6 | var PromiseUtils_1 = require("../util/PromiseUtils");
|
7 | var BaseQueryRunner = (function () {
|
8 | function BaseQueryRunner() {
|
9 |
|
10 |
|
11 |
|
12 | |
13 |
|
14 |
|
15 |
|
16 | this.isReleased = false;
|
17 | |
18 |
|
19 |
|
20 | this.isTransactionActive = false;
|
21 | |
22 |
|
23 |
|
24 |
|
25 | this.data = {};
|
26 | |
27 |
|
28 |
|
29 | this.loadedTables = [];
|
30 | |
31 |
|
32 |
|
33 | this.loadedViews = [];
|
34 | |
35 |
|
36 |
|
37 | this.sqlMemoryMode = false;
|
38 | |
39 |
|
40 |
|
41 | this.sqlInMemory = new SqlInMemory_1.SqlInMemory();
|
42 | }
|
43 |
|
44 |
|
45 |
|
46 | |
47 |
|
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 , this.loadTables([tablePath])];
|
57 | case 1:
|
58 | _a.loadedTables = _b.sent();
|
59 | return [2 , this.loadedTables.length > 0 ? this.loadedTables[0] : undefined];
|
60 | }
|
61 | });
|
62 | });
|
63 | };
|
64 | |
65 |
|
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 , this.loadTables(tableNames)];
|
75 | case 1:
|
76 | _a.loadedTables = _b.sent();
|
77 | return [2 , this.loadedTables];
|
78 | }
|
79 | });
|
80 | });
|
81 | };
|
82 | |
83 |
|
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 , this.loadViews([viewPath])];
|
93 | case 1:
|
94 | _a.loadedViews = _b.sent();
|
95 | return [2 , this.loadedViews.length > 0 ? this.loadedViews[0] : undefined];
|
96 | }
|
97 | });
|
98 | });
|
99 | };
|
100 | |
101 |
|
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 , this.loadViews(viewPaths)];
|
111 | case 1:
|
112 | _a.loadedViews = _b.sent();
|
113 | return [2 , this.loadedViews];
|
114 | }
|
115 | });
|
116 | });
|
117 | };
|
118 | |
119 |
|
120 |
|
121 |
|
122 |
|
123 | BaseQueryRunner.prototype.enableSqlMemory = function () {
|
124 | this.sqlInMemory = new SqlInMemory_1.SqlInMemory();
|
125 | this.sqlMemoryMode = true;
|
126 | };
|
127 | |
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | BaseQueryRunner.prototype.disableSqlMemory = function () {
|
134 | this.sqlInMemory = new SqlInMemory_1.SqlInMemory();
|
135 | this.sqlMemoryMode = false;
|
136 | };
|
137 | |
138 |
|
139 |
|
140 | BaseQueryRunner.prototype.clearSqlMemory = function () {
|
141 | this.sqlInMemory = new SqlInMemory_1.SqlInMemory();
|
142 | };
|
143 | |
144 |
|
145 |
|
146 | BaseQueryRunner.prototype.getMemorySql = function () {
|
147 | return this.sqlInMemory;
|
148 | };
|
149 | |
150 |
|
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 , 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 ];
|
161 | }
|
162 | });
|
163 | });
|
164 | };
|
165 | |
166 |
|
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 , 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 ];
|
177 | }
|
178 | });
|
179 | });
|
180 | };
|
181 |
|
182 |
|
183 |
|
184 | |
185 |
|
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 , view];
|
196 | return [4 , this.loadViews([viewName])];
|
197 | case 1:
|
198 | foundViews = _a.sent();
|
199 | if (foundViews.length > 0) {
|
200 | this.loadedViews.push(foundViews[0]);
|
201 | return [2 , foundViews[0]];
|
202 | }
|
203 | else {
|
204 | throw new Error("View \"" + viewName + "\" does not exist.");
|
205 | }
|
206 | return [2 ];
|
207 | }
|
208 | });
|
209 | });
|
210 | };
|
211 | |
212 |
|
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 , table];
|
223 | return [4 , this.loadTables([tableName])];
|
224 | case 1:
|
225 | foundTables = _a.sent();
|
226 | if (foundTables.length > 0) {
|
227 | this.loadedTables.push(foundTables[0]);
|
228 | return [2 , foundTables[0]];
|
229 | }
|
230 | else {
|
231 | throw new Error("Table \"" + tableName + "\" does not exist.");
|
232 | }
|
233 | return [2 ];
|
234 | }
|
235 | });
|
236 | });
|
237 | };
|
238 | |
239 |
|
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 |
|
260 |
|
261 |
|
262 | BaseQueryRunner.prototype.isColumnChanged = function (oldColumn, newColumn, checkDefault, checkComment) {
|
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 | 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
|
293 | || oldColumn.zerofill !== newColumn.zerofill
|
294 | || oldColumn.unsigned !== newColumn.unsigned
|
295 | || oldColumn.asExpression !== newColumn.asExpression
|
296 | || (checkDefault && oldColumn.default !== newColumn.default)
|
297 | || oldColumn.onUpdate !== newColumn.onUpdate
|
298 | || oldColumn.isNullable !== newColumn.isNullable
|
299 | || (checkComment && oldColumn.comment !== newColumn.comment)
|
300 | || oldColumn.enum !== newColumn.enum;
|
301 | };
|
302 | |
303 |
|
304 |
|
305 | BaseQueryRunner.prototype.isDefaultColumnLength = function (table, column, length) {
|
306 |
|
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 |
|
322 |
|
323 | BaseQueryRunner.prototype.isDefaultColumnWidth = function (table, column, width) {
|
324 |
|
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 |
|
340 |
|
341 | BaseQueryRunner.prototype.isDefaultColumnPrecision = function (table, column, precision) {
|
342 |
|
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 |
|
358 |
|
359 | BaseQueryRunner.prototype.isDefaultColumnScale = function (table, column, scale) {
|
360 |
|
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 |
|
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 |
|
391 | if (this.sqlMemoryMode === true)
|
392 | return [2 , Promise.resolve()];
|
393 | return [4 , PromiseUtils_1.PromiseUtils.runInSequence(upQueries, function (upQuery) { return _this.query(upQuery.query, upQuery.parameters); })];
|
394 | case 1:
|
395 | _c.sent();
|
396 | return [2 ];
|
397 | }
|
398 | });
|
399 | });
|
400 | };
|
401 | return BaseQueryRunner;
|
402 | }());
|
403 | exports.BaseQueryRunner = BaseQueryRunner;
|
404 |
|
405 |
|