forked from typeorm/typeorm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOracleQueryRunner.ts
More file actions
786 lines (674 loc) · 32 KB
/
OracleQueryRunner.ts
File metadata and controls
786 lines (674 loc) · 32 KB
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
import {QueryRunner} from "../../query-runner/QueryRunner";
import {ObjectLiteral} from "../../common/ObjectLiteral";
import {TransactionAlreadyStartedError} from "../../error/TransactionAlreadyStartedError";
import {TransactionNotStartedError} from "../../error/TransactionNotStartedError";
import {TableColumn} from "../../schema-builder/schema/TableColumn";
import {ColumnMetadata} from "../../metadata/ColumnMetadata";
import {Table} from "../../schema-builder/schema/Table";
import {TableForeignKey} from "../../schema-builder/schema/TableForeignKey";
import {TablePrimaryKey} from "../../schema-builder/schema/TablePrimaryKey";
import {TableIndex} from "../../schema-builder/schema/TableIndex";
import {QueryRunnerAlreadyReleasedError} from "../../error/QueryRunnerAlreadyReleasedError";
import {OracleDriver} from "./OracleDriver";
import {Connection} from "../../connection/Connection";
import {ReadStream} from "../../platform/PlatformTools";
import {EntityManager} from "../../entity-manager/EntityManager";
import {QueryFailedError} from "../../error/QueryFailedError";
/**
* Runs queries on a single oracle database connection.
*
* todo: this driver is not 100% finished yet, need to fix all issues that are left
*/
export class OracleQueryRunner implements QueryRunner {
// -------------------------------------------------------------------------
// Public Implemented Properties
// -------------------------------------------------------------------------
/**
* Database driver used by connection.
*/
driver: OracleDriver;
/**
* Connection used by this query runner.
*/
connection: Connection;
/**
* Isolated 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 = {};
// -------------------------------------------------------------------------
// Protected Properties
// -------------------------------------------------------------------------
/**
* Real database connection from a connection pool used to perform queries.
*/
protected databaseConnection: any;
/**
* Promise used to obtain a database connection for a first time.
*/
protected databaseConnectionPromise: Promise<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 sqlsInMemory: string[] = [];
/**
* Mode in which query runner executes.
* Used for replication.
* If replication is not setup its value is ignored.
*/
protected mode: "master"|"slave";
// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
constructor(driver: OracleDriver, mode: "master"|"slave" = "master") {
this.driver = driver;
this.connection = driver.connection;
this.mode = mode;
}
// -------------------------------------------------------------------------
// Public Methods
// -------------------------------------------------------------------------
/**
* Creates/uses database connection from the connection pool to perform further operations.
* Returns obtained database connection.
*/
connect(): Promise<any> {
if (this.databaseConnection)
return Promise.resolve(this.databaseConnection);
if (this.databaseConnectionPromise)
return this.databaseConnectionPromise;
if (this.mode === "slave" && this.driver.isReplicated) {
this.databaseConnectionPromise = this.driver.obtainSlaveConnection().then(connection => {
this.databaseConnection = connection;
return this.databaseConnection;
});
} else { // master
this.databaseConnectionPromise = this.driver.obtainMasterConnection().then(connection => {
this.databaseConnection = connection;
return this.databaseConnection;
});
}
return this.databaseConnectionPromise;
}
/**
* Releases used database connection.
* You cannot use query runner methods once its released.
*/
release(): Promise<void> {
return new Promise<void>((ok, fail) => {
this.isReleased = true;
if (this.databaseConnection) {
this.databaseConnection.close((err: any) => {
if (err)
return fail(err);
ok();
});
}
});
}
/**
* Starts transaction.
*/
async startTransaction(): Promise<void> {
if (this.isReleased)
throw new QueryRunnerAlreadyReleasedError();
if (this.isTransactionActive)
throw new TransactionAlreadyStartedError();
// await this.query("START TRANSACTION");
this.isTransactionActive = true;
}
/**
* Commits transaction.
* Error will be thrown if transaction was not started.
*/
async commitTransaction(): Promise<void> {
if (!this.isTransactionActive)
throw new TransactionNotStartedError();
await this.query("COMMIT");
this.isTransactionActive = false;
}
/**
* Rollbacks transaction.
* Error will be thrown if transaction was not started.
*/
async rollbackTransaction(): Promise<void> {
if (!this.isTransactionActive)
throw new TransactionNotStartedError();
await this.query("ROLLBACK");
this.isTransactionActive = false;
}
/**
* Executes a given SQL query.
*/
query(query: string, parameters?: any[]): Promise<any> {
if (this.isReleased)
throw new QueryRunnerAlreadyReleasedError();
return new Promise(async (ok, fail) => {
try {
this.driver.connection.logger.logQuery(query, parameters, this);
const queryStartTime = +new Date();
const handler = (err: any, result: any) => {
// log slow queries if maxQueryExecution time is set
const maxQueryExecutionTime = this.driver.connection.options.maxQueryExecutionTime;
const queryEndTime = +new Date();
const queryExecutionTime = queryEndTime - queryStartTime;
if (maxQueryExecutionTime && queryExecutionTime > maxQueryExecutionTime)
this.driver.connection.logger.logQuerySlow(queryExecutionTime, query, parameters, this);
if (err) {
this.driver.connection.logger.logQueryError(err, query, parameters, this);
return fail(new QueryFailedError(query, parameters, err));
}
ok(result.rows || result.outBinds);
};
const executionOptions = {
autoCommit: this.isTransactionActive ? false : true
};
const databaseConnection = await this.connect();
databaseConnection.execute(query, parameters || {}, executionOptions, handler);
} catch (err) {
fail(err);
}
});
}
/**
* Returns raw data stream.
*/
stream(query: string, parameters?: any[], onEnd?: Function, onError?: Function): Promise<ReadStream> {
throw new Error(`Stream is not supported by Oracle driver.`);
}
/**
* Insert a new row with given values into the given table.
* Returns value of the generated column if given and generate column exist in the table.
*/
async insert(tableName: string, keyValues: ObjectLiteral): Promise<any> {
// todo: fix generated columns
let generatedColumn: ColumnMetadata|undefined;
const keys = Object.keys(keyValues);
const columns = keys.map(key => `"${key}"`).join(", ");
const values = keys.map(key => ":" + key).join(", ");
const parameters = keys.map(key => keyValues[key]);
const generatedColumns = this.connection.hasMetadata(tableName) ? this.connection.getMetadata(tableName).generatedColumns : [];
if (generatedColumns.length > 0)
generatedColumn = generatedColumns.find(column => column.isPrimary && column.isGenerated);
const insertSql = columns.length > 0
? `INSERT INTO "${tableName}" (${columns}) VALUES (${values})`
: `INSERT INTO "${tableName}" DEFAULT VALUES`;
if (generatedColumn) {
const sql2 = `declare lastId number; begin ${insertSql} returning "${generatedColumn.databaseName}" into lastId; dbms_output.enable; dbms_output.put_line(lastId); dbms_output.get_line(:ln, :st); end;`;
const saveResult = await this.query(sql2, parameters.concat([
{ dir: this.driver.oracle.BIND_OUT, type: this.driver.oracle.STRING, maxSize: 32767 },
{ dir: this.driver.oracle.BIND_OUT, type: this.driver.oracle.NUMBER }
]));
return parseInt(saveResult[0]);
} else {
return this.query(insertSql, parameters);
}
}
/**
* Updates rows that match given conditions in the given table.
*/
async update(tableName: string, valuesMap: ObjectLiteral, conditions: ObjectLiteral): Promise<void> {
const updateValues = this.parametrize(valuesMap).join(", ");
const conditionString = this.parametrize(conditions).join(" AND ");
const sql = `UPDATE "${tableName}" SET ${updateValues} ${conditionString ? (" WHERE " + conditionString) : ""}`;
const conditionParams = Object.keys(conditions).map(key => conditions[key]);
const updateParams = Object.keys(valuesMap).map(key => valuesMap[key]);
const allParameters = updateParams.concat(conditionParams);
await this.query(sql, allParameters);
}
/**
* Deletes from the given table by a given conditions.
*/
async delete(tableName: string, conditions: ObjectLiteral|string, maybeParameters?: any[]): Promise<void> {
const conditionString = typeof conditions === "string" ? conditions : this.parametrize(conditions).join(" AND ");
const parameters = conditions instanceof Object ? Object.keys(conditions).map(key => (conditions as ObjectLiteral)[key]) : maybeParameters;
const sql = `DELETE FROM "${tableName}" WHERE ${conditionString}`;
await this.query(sql, parameters);
}
/**
* Inserts rows into the closure table.
*/
async insertIntoClosureTable(tableName: string, newEntityId: any, parentId: any, hasLevel: boolean): Promise<number> {
let sql = "";
if (hasLevel) {
sql = `INSERT INTO "${tableName}"("ancestor", "descendant", "level") ` +
`SELECT "ancestor", ${newEntityId}, "level" + 1 FROM "${tableName}" WHERE "descendant" = ${parentId} ` +
`UNION ALL SELECT ${newEntityId}, ${newEntityId}, 1`;
} else {
sql = `INSERT INTO "${tableName}" ("ancestor", "descendant") ` +
`SELECT "ancestor", ${newEntityId} FROM "${tableName}" WHERE "descendant" = ${parentId} ` +
`UNION ALL SELECT ${newEntityId}, ${newEntityId}`;
}
await this.query(sql);
const results: ObjectLiteral[] = await this.query(`SELECT MAX("level") as "level" FROM "${tableName}" WHERE "descendant" = ${parentId}`);
return results && results[0] && results[0]["level"] ? parseInt(results[0]["level"]) + 1 : 1;
}
/**
* Loads given table's data from the database.
*/
async getTable(tableName: string): Promise<Table|undefined> {
const tables = await this.getTables([tableName]);
return tables.length > 0 ? tables[0] : undefined;
}
/**
* Loads all tables (with given names) from the database and creates a Table from them.
*/
async getTables(tableNames: string[]): Promise<Table[]> {
// if no tables given then no need to proceed
if (!tableNames || !tableNames.length)
return [];
// load tables, columns, indices and foreign keys
const tableNamesString = tableNames.map(name => "'" + name + "'").join(", ");
const tablesSql = `SELECT TABLE_NAME FROM user_tables WHERE TABLE_NAME IN (${tableNamesString})`;
const columnsSql = `SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE, DATA_LENGTH, DATA_PRECISION, DATA_SCALE, NULLABLE, IDENTITY_COLUMN FROM all_tab_cols WHERE TABLE_NAME IN (${tableNamesString})`;
const indicesSql = `SELECT ind.INDEX_NAME, ind.TABLE_NAME, ind.UNIQUENESS, LISTAGG(cols.COLUMN_NAME, ',') WITHIN GROUP (ORDER BY cols.COLUMN_NAME) AS COLUMN_NAMES
FROM USER_INDEXES ind, USER_IND_COLUMNS cols
WHERE ind.INDEX_NAME = cols.INDEX_NAME AND ind.TABLE_NAME IN (${tableNamesString})
GROUP BY ind.INDEX_NAME, ind.TABLE_NAME, ind.UNIQUENESS`;
// const foreignKeysSql = `SELECT * FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = '${this.dbName}' AND REFERENCED_COLUMN_NAME IS NOT NULL`;
// const uniqueKeysSql = `SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_SCHEMA = '${this.dbName}' AND CONSTRAINT_TYPE = 'UNIQUE'`;
const constraintsSql = `SELECT cols.table_name, cols.column_name, cols.position, cons.constraint_type, cons.constraint_name
FROM all_constraints cons, all_cons_columns cols WHERE cols.table_name IN (${tableNamesString})
AND cons.constraint_name = cols.constraint_name AND cons.owner = cols.owner ORDER BY cols.table_name, cols.position`;
const [dbTables, dbColumns, dbIndices, /*dbForeignKeys, dbUniqueKeys, */constraints]: ObjectLiteral[][] = await Promise.all([
this.query(tablesSql),
this.query(columnsSql),
this.query(indicesSql),
// this.query(foreignKeysSql),
// this.query(uniqueKeysSql),
this.query(constraintsSql),
]);
// if tables were not found in the db, no need to proceed
if (!dbTables.length)
return [];
// create tables for loaded tables
return dbTables.map(dbTable => {
const table = new Table(dbTable["TABLE_NAME"]);
// create columns from the loaded columns
table.columns = dbColumns
.filter(dbColumn => dbColumn["TABLE_NAME"] === table.name)
.map(dbColumn => {
const isPrimary = !!constraints
.find(constraint => {
return constraint["TABLE_NAME"] === table.name &&
constraint["CONSTRAINT_TYPE"] === "P" &&
constraint["COLUMN_NAME"] === dbColumn["COLUMN_NAME"];
});
// TODO fix
let columnType = dbColumn["DATA_TYPE"].toLowerCase();
if (dbColumn["DATA_TYPE"].toLowerCase() === "varchar2" && dbColumn["DATA_LENGTH"] !== null) {
columnType += "(" + dbColumn["DATA_LENGTH"] + ")";
} else if (dbColumn["DATA_PRECISION"] !== null && dbColumn["DATA_SCALE"] !== null) {
columnType += "(" + dbColumn["DATA_PRECISION"] + "," + dbColumn["DATA_SCALE"] + ")";
} else if (dbColumn["DATA_SCALE"] !== null) {
columnType += "(0," + dbColumn["DATA_SCALE"] + ")";
} else if (dbColumn["DATA_PRECISION"] !== null) {
columnType += "(" + dbColumn["DATA_PRECISION"] + ")";
}
const tableColumn = new TableColumn();
tableColumn.name = dbColumn["COLUMN_NAME"];
tableColumn.type = columnType;
tableColumn.default = dbColumn["COLUMN_DEFAULT"] !== null && dbColumn["COLUMN_DEFAULT"] !== undefined ? dbColumn["COLUMN_DEFAULT"] : undefined;
tableColumn.isNullable = dbColumn["NULLABLE"] !== "N";
tableColumn.isPrimary = isPrimary;
tableColumn.isGenerated = dbColumn["IDENTITY_COLUMN"] === "YES"; // todo
tableColumn.comment = ""; // todo
return tableColumn;
});
// create primary key schema
table.primaryKeys = constraints
.filter(constraint =>
constraint["TABLE_NAME"] === table.name && constraint["CONSTRAINT_TYPE"] === "P"
)
.map(constraint =>
new TablePrimaryKey(constraint["CONSTRAINT_NAME"], constraint["COLUMN_NAME"])
);
// create foreign key schemas from the loaded indices
table.foreignKeys = constraints
.filter(constraint => constraint["TABLE_NAME"] === table.name && constraint["CONSTRAINT_TYPE"] === "R")
.map(constraint => new TableForeignKey(constraint["CONSTRAINT_NAME"], [], [], "", "")); // todo: fix missing params
// create index schemas from the loaded indices
table.indices = dbIndices
.filter(dbIndex => {
return dbIndex["TABLE_NAME"] === table.name &&
(!table.foreignKeys.find(foreignKey => foreignKey.name === dbIndex["INDEX_NAME"])) &&
(!table.primaryKeys.find(primaryKey => primaryKey.name === dbIndex["INDEX_NAME"]));
})
.map(dbIndex => {
return new TableIndex(dbTable["TABLE_NAME"], dbIndex["INDEX_NAME"], dbIndex["COLUMN_NAMES"], !!(dbIndex["COLUMN_NAMES"] === "UNIQUE"));
});
return table;
});
}
/**
* Checks if database with the given name exist.
*/
async hasDatabase(database: string): Promise<boolean> {
return Promise.resolve(false);
}
/**
* Checks if table with the given name exist in the database.
*/
async hasTable(tableName: string): Promise<boolean> {
const sql = `SELECT TABLE_NAME FROM user_tables WHERE TABLE_NAME = '${tableName}'`;
const result = await this.query(sql);
return result.length ? true : false;
}
/**
* Creates a database if it's not created.
*/
createDatabase(database: string): Promise<void[]> {
return this.query(`CREATE DATABASE IF NOT EXISTS ${database}`);
}
/**
* Creates a schema if it's not created.
*/
createSchema(schemas: string[]): Promise<void[]> {
return Promise.resolve([]);
}
/**
* Creates a new table from the given table metadata and column metadatas.
*/
async createTable(table: Table): Promise<void> {
const columnDefinitions = table.columns.map(column => this.buildCreateColumnSql(column)).join(", ");
let sql = `CREATE TABLE "${table.name}" (${columnDefinitions}`;
const primaryKeyColumns = table.columns.filter(column => column.isPrimary);
if (primaryKeyColumns.length > 0)
sql += `, PRIMARY KEY(${primaryKeyColumns.map(column => `"${column.name}"`).join(", ")})`;
sql += `)`;
await this.query(sql);
}
/**
* Drops the table.
*/
async dropTable(tableName: string): Promise<void> {
let sql = `DROP TABLE "${tableName}"`;
await this.query(sql);
}
/**
* Checks if column with the given name exist in the given table.
*/
async hasColumn(tableName: string, columnName: string): Promise<boolean> {
const sql = `SELECT COLUMN_NAME FROM all_tab_cols WHERE TABLE_NAME = '${tableName}' AND COLUMN_NAME = '${columnName}'`;
const result = await this.query(sql);
return result.length ? true : false;
}
/**
* Creates a new column from the column in the table.
*/
async addColumn(tableOrName: Table|string, column: TableColumn): Promise<void> {
const tableName = tableOrName instanceof Table ? tableOrName.name : tableOrName;
const sql = `ALTER TABLE "${tableName}" ADD ${this.buildCreateColumnSql(column)}`;
return this.query(sql);
}
/**
* Creates a new columns from the column in the table.
*/
async addColumns(tableOrName: Table|string, columns: TableColumn[]): Promise<void> {
const queries = columns.map(column => this.addColumn(tableOrName as any, column));
await Promise.all(queries);
}
/**
* Renames column in the given table.
*/
async renameColumn(tableOrName: Table|string, oldTableColumnOrName: TableColumn|string, newTableColumnOrName: TableColumn|string): Promise<void> {
let table: Table|undefined = undefined;
if (tableOrName instanceof Table) {
table = tableOrName;
} else {
table = await this.getTable(tableOrName);
}
if (!table)
throw new Error(`Table ${tableOrName} was not found.`);
let oldColumn: TableColumn|undefined = undefined;
if (oldTableColumnOrName instanceof TableColumn) {
oldColumn = oldTableColumnOrName;
} else {
oldColumn = table.columns.find(column => column.name === oldTableColumnOrName);
}
if (!oldColumn)
throw new Error(`Column "${oldTableColumnOrName}" was not found in the "${tableOrName}" table.`);
let newColumn: TableColumn|undefined = undefined;
if (newTableColumnOrName instanceof TableColumn) {
newColumn = newTableColumnOrName;
} else {
newColumn = oldColumn.clone();
newColumn.name = newTableColumnOrName;
}
return this.changeColumn(table, oldColumn, newColumn);
}
/**
* Changes a column in the table.
*/
async changeColumn(tableOrName: Table|string, oldTableColumnOrName: TableColumn|string, newColumn: TableColumn): Promise<void> {
let table: Table|undefined = undefined;
if (tableOrName instanceof Table) {
table = tableOrName;
} else {
table = await this.getTable(tableOrName);
}
if (!table)
throw new Error(`Table ${tableOrName} was not found.`);
let oldColumn: TableColumn|undefined = undefined;
if (oldTableColumnOrName instanceof TableColumn) {
oldColumn = oldTableColumnOrName;
} else {
oldColumn = table.columns.find(column => column.name === oldTableColumnOrName);
}
if (!oldColumn)
throw new Error(`Column "${oldTableColumnOrName}" was not found in the "${tableOrName}" table.`);
if (newColumn.isGenerated !== oldColumn.isGenerated) {
if (newColumn.isGenerated) {
if (table.primaryKeys.length > 0 && oldColumn.isPrimary) {
// console.log(table.primaryKeys);
const dropPrimarySql = `ALTER TABLE "${table.name}" DROP CONSTRAINT "${table.primaryKeys[0].name}"`;
await this.query(dropPrimarySql);
}
// since modifying identity column is not supported yet, we need to recreate this column
const dropSql = `ALTER TABLE "${table.name}" DROP COLUMN "${newColumn.name}"`;
await this.query(dropSql);
const createSql = `ALTER TABLE "${table.name}" ADD ${this.buildCreateColumnSql(newColumn)}`;
await this.query(createSql);
} else {
const sql = `ALTER TABLE "${table.name}" MODIFY "${newColumn.name}" DROP IDENTITY`;
await this.query(sql);
}
}
if (newColumn.isNullable !== oldColumn.isNullable) {
const sql = `ALTER TABLE "${table.name}" MODIFY "${newColumn.name}" ${this.connection.driver.createFullType(newColumn)} ${newColumn.isNullable ? "NULL" : "NOT NULL"}`;
await this.query(sql);
} else if (this.connection.driver.createFullType(newColumn) !== this.connection.driver.createFullType(oldColumn)) { // elseif is used because
const sql = `ALTER TABLE "${table.name}" MODIFY "${newColumn.name}" ${this.connection.driver.createFullType(newColumn)}`;
await this.query(sql);
}
}
/**
* Changes a column in the table.
*/
async changeColumns(table: Table, changedColumns: { newColumn: TableColumn, oldColumn: TableColumn }[]): Promise<void> {
const updatePromises = changedColumns.map(async changedColumn => {
return this.changeColumn(table, changedColumn.oldColumn, changedColumn.newColumn);
});
await Promise.all(updatePromises);
}
/**
* Drops column in the table.
*/
async dropColumn(table: Table, column: TableColumn): Promise<void> {
return this.query(`ALTER TABLE "${table.name}" DROP COLUMN "${column.name}"`);
}
/**
* Drops the columns in the table.
*/
async dropColumns(table: Table, columns: TableColumn[]): Promise<void> {
const dropPromises = columns.map(column => this.dropColumn(table, column));
await Promise.all(dropPromises);
}
/**
* Updates table's primary keys.
*/
async updatePrimaryKeys(dbTable: Table): Promise<void> {
const primaryColumnNames = dbTable.primaryKeys.map(primaryKey => "\"" + primaryKey.columnName + "\"");
// console.log(dbTable.primaryKeys);
if (dbTable.primaryKeys.length > 0 && dbTable.primaryKeys[0].name)
await this.query(`ALTER TABLE "${dbTable.name}" DROP CONSTRAINT "${dbTable.primaryKeys[0].name}"`);
if (primaryColumnNames.length > 0)
await this.query(`ALTER TABLE "${dbTable.name}" ADD PRIMARY KEY (${primaryColumnNames.join(", ")})`);
}
/**
* Creates a new foreign key.
*/
async createForeignKey(tableOrName: Table|string, foreignKey: TableForeignKey): Promise<void> {
const tableName = tableOrName instanceof Table ? tableOrName.name : tableOrName;
const columnNames = foreignKey.columnNames.map(column => "\"" + column + "\"").join(", ");
const referencedColumnNames = foreignKey.referencedColumnNames.map(column => "\"" + column + "\"").join(",");
let sql = `ALTER TABLE "${tableName}" ADD CONSTRAINT "${foreignKey.name}" ` +
`FOREIGN KEY (${columnNames}) ` +
`REFERENCES "${foreignKey.referencedTableName}"(${referencedColumnNames})`;
if (foreignKey.onDelete) sql += " ON DELETE " + foreignKey.onDelete;
return this.query(sql);
}
/**
* Creates a new foreign keys.
*/
async createForeignKeys(tableOrName: Table|string, foreignKeys: TableForeignKey[]): Promise<void> {
const promises = foreignKeys.map(foreignKey => this.createForeignKey(tableOrName as any, foreignKey));
await Promise.all(promises);
}
/**
* Drops a foreign key from the table.
*/
async dropForeignKey(tableOrName: Table|string, foreignKey: TableForeignKey): Promise<void> {
const tableName = tableOrName instanceof Table ? tableOrName.name : tableOrName;
const sql = `ALTER TABLE "${tableName}" DROP CONSTRAINT "${foreignKey.name}"`;
return this.query(sql);
}
/**
* Drops a foreign keys from the table.
*/
async dropForeignKeys(tableOrName: Table|string, foreignKeys: TableForeignKey[]): Promise<void> {
const promises = foreignKeys.map(foreignKey => this.dropForeignKey(tableOrName as any, foreignKey));
await Promise.all(promises);
}
/**
* Creates a new index.
*/
async createIndex(tableName: string, index: TableIndex): Promise<void> {
const columns = index.columnNames.map(columnName => "\"" + columnName + "\"").join(", ");
const sql = `CREATE ${index.isUnique ? "UNIQUE" : ""} INDEX "${index.name}" ON "${tableName}"(${columns})`;
await this.query(sql);
}
/**
* Drops an index from the table.
*/
async dropIndex(tableSchemeOrName: Table|string, indexName: string): Promise<void> {
const tableName = tableSchemeOrName instanceof Table ? tableSchemeOrName.name : tableSchemeOrName;
const sql = `ALTER TABLE "${tableName}" DROP INDEX "${indexName}"`;
await this.query(sql);
}
/**
* Truncates table.
*/
async truncate(tableName: string): Promise<void> {
await this.query(`TRUNCATE TABLE "${tableName}"`);
}
/**
* Removes all tables from the currently connected database.
*/
async clearDatabase(): Promise<void> {
await this.startTransaction();
try {
const disableForeignKeysCheckQuery = `SET FOREIGN_KEY_CHECKS = 0;`;
const dropTablesQuery = `SELECT concat('DROP TABLE IF EXISTS "', table_name, '";') AS query FROM information_schema.tables WHERE table_schema = '${this.dbName}'`;
const enableForeignKeysCheckQuery = `SET FOREIGN_KEY_CHECKS = 1;`;
await this.query(disableForeignKeysCheckQuery);
const dropQueries: ObjectLiteral[] = await this.query(dropTablesQuery);
await Promise.all(dropQueries.map(query => this.query(query["query"])));
await this.query(enableForeignKeysCheckQuery);
await this.commitTransaction();
} catch (error) {
try { // we throw original error even if rollback thrown an error
await this.rollbackTransaction();
} catch (rollbackError) { }
throw error;
}
}
/**
* 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.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.sqlsInMemory = [];
this.sqlMemoryMode = false;
}
/**
* Gets sql stored in the memory. Parameters in the sql are already replaced.
*/
getMemorySql(): (string|{ up: string, down: string })[] {
return this.sqlsInMemory;
}
// -------------------------------------------------------------------------
// Protected Methods
// -------------------------------------------------------------------------
/**
* Database name shortcut.
*/
protected get dbName(): string {
return this.driver.options.schema as string;
}
/**
* Parametrizes given object of values. Used to create column=value queries.
*/
protected parametrize(objectLiteral: ObjectLiteral): string[] {
return Object.keys(objectLiteral).map(key => `"${key}"=:${key}`);
}
/**
* Builds a query for create column.
*/
protected buildCreateColumnSql(column: TableColumn) {
let c = `"${column.name}" ` + this.connection.driver.createFullType(column);
if (column.charset)
c += " CHARACTER SET " + column.charset;
if (column.collation)
c += " COLLATE " + column.collation;
if (column.isNullable !== true && !column.isGenerated) // NOT NULL is not supported with GENERATED
c += " NOT NULL";
// if (column.isPrimary === true && addPrimary)
// c += " PRIMARY KEY";
if (column.isGenerated === true) // don't use skipPrimary here since updates can update already exist primary without auto inc.
c += " GENERATED BY DEFAULT ON NULL AS IDENTITY";
// if (column.comment) // todo: less priority, fix it later
// c += " COMMENT '" + column.comment + "'";
if (column.default !== undefined && column.default !== null) { // todo: same code in all drivers. make it DRY
c += " DEFAULT " + column.default;
}
return c;
}
}