/
AbstractSqliteQueryRunner.ts
1174 lines (997 loc) · 50.6 KB
/
AbstractSqliteQueryRunner.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
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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
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/table/TableColumn";
import {ColumnMetadata} from "../../metadata/ColumnMetadata";
import {Table} from "../../schema-builder/table/Table";
import {TableIndex} from "../../schema-builder/table/TableIndex";
import {TableForeignKey} from "../../schema-builder/table/TableForeignKey";
import {View} from "../../schema-builder/view/View";
import {Query} from "../Query";
import {AbstractSqliteDriver} from "./AbstractSqliteDriver";
import {ReadStream} from "../../platform/PlatformTools";
import {TableIndexOptions} from "../../schema-builder/options/TableIndexOptions";
import {TableUnique} from "../../schema-builder/table/TableUnique";
import {BaseQueryRunner} from "../../query-runner/BaseQueryRunner";
import {OrmUtils} from "../../util/OrmUtils";
import {TableCheck} from "../../schema-builder/table/TableCheck";
import {IsolationLevel} from "../types/IsolationLevel";
import {TableExclusion} from "../../schema-builder/table/TableExclusion";
/**
* Runs queries on a single sqlite database connection.
*/
export abstract class AbstractSqliteQueryRunner extends BaseQueryRunner implements QueryRunner {
// -------------------------------------------------------------------------
// Public Implemented Properties
// -------------------------------------------------------------------------
/**
* Database driver used by connection.
*/
driver: AbstractSqliteDriver;
// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
constructor() {
super();
}
// -------------------------------------------------------------------------
// Public Methods
// -------------------------------------------------------------------------
/**
* Creates/uses database connection from the connection pool to perform further operations.
* Returns obtained database connection.
*/
connect(): Promise<any> {
return Promise.resolve(this.driver.databaseConnection);
}
/**
* Releases used database connection.
* We just clear loaded tables and sql in memory, because sqlite do not support multiple connections thus query runners.
*/
release(): Promise<void> {
this.loadedTables = [];
this.clearSqlMemory();
return Promise.resolve();
}
/**
* Starts transaction.
*/
async startTransaction(isolationLevel?: IsolationLevel): Promise<void> {
if (this.isTransactionActive)
throw new TransactionAlreadyStartedError();
this.isTransactionActive = true;
if (isolationLevel) {
if (isolationLevel !== "READ UNCOMMITTED" && isolationLevel !== "SERIALIZABLE") {
throw new Error(`SQLite only supports SERIALIZABLE and READ UNCOMMITTED isolation`);
}
if (isolationLevel === "READ UNCOMMITTED") {
await this.query("PRAGMA read_uncommitted = true");
} else {
await this.query("PRAGMA read_uncommitted = false");
}
}
await this.query("BEGIN TRANSACTION");
}
/**
* 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;
}
/**
* Returns raw data stream.
*/
stream(query: string, parameters?: any[], onEnd?: Function, onError?: Function): Promise<ReadStream> {
throw new Error(`Stream is not supported by sqlite driver.`);
}
/**
* Returns all available database names including system databases.
*/
async getDatabases(): Promise<string[]> {
return Promise.resolve([]);
}
/**
* Returns all available schema names including system schemas.
* If database parameter specified, returns schemas of that database.
*/
async getSchemas(database?: string): Promise<string[]> {
return Promise.resolve([]);
}
/**
* Checks if database with the given name exist.
*/
async hasDatabase(database: string): Promise<boolean> {
return Promise.resolve(false);
}
/**
* Checks if schema with the given name exist.
*/
async hasSchema(schema: string): Promise<boolean> {
throw new Error(`This driver does not support table schemas`);
}
/**
* Checks if table with the given name exist in the database.
*/
async hasTable(tableOrName: Table|string): Promise<boolean> {
const tableName = tableOrName instanceof Table ? tableOrName.name : tableOrName;
const sql = `SELECT * FROM "sqlite_master" WHERE "type" = 'table' AND "name" = '${tableName}'`;
const result = await this.query(sql);
return result.length ? true : false;
}
/**
* Checks if column with the given name exist in the given table.
*/
async hasColumn(tableOrName: Table|string, columnName: string): Promise<boolean> {
const tableName = tableOrName instanceof Table ? tableOrName.name : tableOrName;
const sql = `PRAGMA table_info("${tableName}")`;
const columns: ObjectLiteral[] = await this.query(sql);
return !!columns.find(column => column["name"] === columnName);
}
/**
* Creates a new database.
*/
async createDatabase(database: string, ifNotExist?: boolean): Promise<void> {
return Promise.resolve();
}
/**
* Drops database.
*/
async dropDatabase(database: string, ifExist?: boolean): Promise<void> {
return Promise.resolve();
}
/**
* Creates a new table schema.
*/
async createSchema(schema: string, ifNotExist?: boolean): Promise<void> {
return Promise.resolve();
}
/**
* Drops table schema.
*/
async dropSchema(schemaPath: string, ifExist?: boolean): Promise<void> {
return Promise.resolve();
}
/**
* Creates a new table.
*/
async createTable(table: Table, ifNotExist: boolean = false, createForeignKeys: boolean = true, createIndices: boolean = true): Promise<void> {
const upQueries: Query[] = [];
const downQueries: Query[] = [];
if (ifNotExist) {
const isTableExist = await this.hasTable(table);
if (isTableExist) return Promise.resolve();
}
upQueries.push(this.createTableSql(table, createForeignKeys));
downQueries.push(this.dropTableSql(table));
if (createIndices) {
table.indices.forEach(index => {
// new index may be passed without name. In this case we generate index name manually.
if (!index.name)
index.name = this.connection.namingStrategy.indexName(table.name, index.columnNames, index.where);
upQueries.push(this.createIndexSql(table, index));
downQueries.push(this.dropIndexSql(index));
});
}
await this.executeQueries(upQueries, downQueries);
}
/**
* Drops the table.
*/
async dropTable(tableOrName: Table|string, ifExist?: boolean, dropForeignKeys: boolean = true, dropIndices: boolean = true): Promise<void> {
if (ifExist) {
const isTableExist = await this.hasTable(tableOrName);
if (!isTableExist) return Promise.resolve();
}
// if dropTable called with dropForeignKeys = true, we must create foreign keys in down query.
const createForeignKeys: boolean = dropForeignKeys;
const table = tableOrName instanceof Table ? tableOrName : await this.getCachedTable(tableOrName);
const upQueries: Query[] = [];
const downQueries: Query[] = [];
if (dropIndices) {
table.indices.forEach(index => {
upQueries.push(this.dropIndexSql(index));
downQueries.push(this.createIndexSql(table, index));
});
}
upQueries.push(this.dropTableSql(table, ifExist));
downQueries.push(this.createTableSql(table, createForeignKeys));
await this.executeQueries(upQueries, downQueries);
}
/**
* Creates a new view.
*/
async createView(view: View): Promise<void> {
const upQueries: Query[] = [];
const downQueries: Query[] = [];
upQueries.push(this.createViewSql(view));
upQueries.push(this.insertViewDefinitionSql(view));
downQueries.push(this.dropViewSql(view));
downQueries.push(this.deleteViewDefinitionSql(view));
await this.executeQueries(upQueries, downQueries);
}
/**
* Drops the view.
*/
async dropView(target: View|string): Promise<void> {
const viewName = target instanceof View ? target.name : target;
const view = await this.getCachedView(viewName);
const upQueries: Query[] = [];
const downQueries: Query[] = [];
upQueries.push(this.deleteViewDefinitionSql(view));
upQueries.push(this.dropViewSql(view));
downQueries.push(this.insertViewDefinitionSql(view));
downQueries.push(this.createViewSql(view));
await this.executeQueries(upQueries, downQueries);
}
/**
* Renames the given table.
*/
async renameTable(oldTableOrName: Table|string, newTableName: string): Promise<void> {
const oldTable = oldTableOrName instanceof Table ? oldTableOrName : await this.getCachedTable(oldTableOrName);
const newTable = oldTable.clone();
newTable.name = newTableName;
// rename table
const up = new Query(`ALTER TABLE "${oldTable.name}" RENAME TO "${newTableName}"`);
const down = new Query(`ALTER TABLE "${newTableName}" RENAME TO "${oldTable.name}"`);
await this.executeQueries(up, down);
// rename old table;
oldTable.name = newTable.name;
// rename unique constraints
newTable.uniques.forEach(unique => {
unique.name = this.connection.namingStrategy.uniqueConstraintName(newTable, unique.columnNames);
});
// rename foreign key constraints
newTable.foreignKeys.forEach(foreignKey => {
foreignKey.name = this.connection.namingStrategy.foreignKeyName(newTable, foreignKey.columnNames, foreignKey.referencedTableName, foreignKey.referencedColumnNames);
});
// rename indices
newTable.indices.forEach(index => {
index.name = this.connection.namingStrategy.indexName(newTable, index.columnNames, index.where);
});
// recreate table with new constraint names
await this.recreateTable(newTable, oldTable);
}
/**
* Creates a new column from the column in the table.
*/
async addColumn(tableOrName: Table|string, column: TableColumn): Promise<void> {
const table = tableOrName instanceof Table ? tableOrName : await this.getCachedTable(tableOrName);
return this.addColumns(table!, [column]);
}
/**
* Creates a new columns from the column in the table.
*/
async addColumns(tableOrName: Table|string, columns: TableColumn[]): Promise<void> {
const table = tableOrName instanceof Table ? tableOrName : await this.getCachedTable(tableOrName);
const changedTable = table.clone();
columns.forEach(column => changedTable.addColumn(column));
await this.recreateTable(changedTable, table);
}
/**
* Renames column in the given table.
*/
async renameColumn(tableOrName: Table|string, oldTableColumnOrName: TableColumn|string, newTableColumnOrName: TableColumn|string): Promise<void> {
const table = tableOrName instanceof Table ? tableOrName : await this.getCachedTable(tableOrName);
const oldColumn = oldTableColumnOrName instanceof TableColumn ? oldTableColumnOrName : table.columns.find(c => c.name === oldTableColumnOrName);
if (!oldColumn)
throw new Error(`Column "${oldTableColumnOrName}" was not found in the "${table.name}" 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> {
const table = tableOrName instanceof Table ? tableOrName : await this.getCachedTable(tableOrName);
const oldColumn = oldTableColumnOrName instanceof TableColumn ? oldTableColumnOrName : table.columns.find(c => c.name === oldTableColumnOrName);
if (!oldColumn)
throw new Error(`Column "${oldTableColumnOrName}" was not found in the "${table.name}" table.`);
await this.changeColumns(table, [{oldColumn, newColumn}]);
}
/**
* Changes a column in the table.
* Changed column looses all its keys in the db.
*/
async changeColumns(tableOrName: Table|string, changedColumns: { oldColumn: TableColumn, newColumn: TableColumn }[]): Promise<void> {
const table = tableOrName instanceof Table ? tableOrName : await this.getCachedTable(tableOrName);
const changedTable = table.clone();
changedColumns.forEach(changedColumnSet => {
if (changedColumnSet.newColumn.name !== changedColumnSet.oldColumn.name) {
changedTable.findColumnUniques(changedColumnSet.oldColumn).forEach(unique => {
unique.columnNames.splice(unique.columnNames.indexOf(changedColumnSet.oldColumn.name), 1);
unique.columnNames.push(changedColumnSet.newColumn.name);
unique.name = this.connection.namingStrategy.uniqueConstraintName(changedTable, unique.columnNames);
});
changedTable.findColumnForeignKeys(changedColumnSet.oldColumn).forEach(fk => {
fk.columnNames.splice(fk.columnNames.indexOf(changedColumnSet.oldColumn.name), 1);
fk.columnNames.push(changedColumnSet.newColumn.name);
fk.name = this.connection.namingStrategy.foreignKeyName(changedTable, fk.columnNames, fk.referencedTableName, fk.referencedColumnNames);
});
changedTable.findColumnIndices(changedColumnSet.oldColumn).forEach(index => {
index.columnNames.splice(index.columnNames.indexOf(changedColumnSet.oldColumn.name), 1);
index.columnNames.push(changedColumnSet.newColumn.name);
index.name = this.connection.namingStrategy.indexName(changedTable, index.columnNames, index.where);
});
}
const originalColumn = changedTable.columns.find(column => column.name === changedColumnSet.oldColumn.name);
if (originalColumn)
changedTable.columns[changedTable.columns.indexOf(originalColumn)] = changedColumnSet.newColumn;
});
await this.recreateTable(changedTable, table);
}
/**
* Drops column in the table.
*/
async dropColumn(tableOrName: Table|string, columnOrName: TableColumn|string): Promise<void> {
const table = tableOrName instanceof Table ? tableOrName : await this.getCachedTable(tableOrName);
const column = columnOrName instanceof TableColumn ? columnOrName : table.findColumnByName(columnOrName);
if (!column)
throw new Error(`Column "${columnOrName}" was not found in table "${table.name}"`);
await this.dropColumns(table, [column]);
}
/**
* Drops the columns in the table.
*/
async dropColumns(tableOrName: Table|string, columns: TableColumn[]): Promise<void> {
const table = tableOrName instanceof Table ? tableOrName : await this.getCachedTable(tableOrName);
// clone original table and remove column and its constraints from cloned table
const changedTable = table.clone();
columns.forEach(column => {
changedTable.removeColumn(column);
changedTable.findColumnUniques(column).forEach(unique => changedTable.removeUniqueConstraint(unique));
changedTable.findColumnIndices(column).forEach(index => changedTable.removeIndex(index));
changedTable.findColumnForeignKeys(column).forEach(fk => changedTable.removeForeignKey(fk));
});
await this.recreateTable(changedTable, table);
// remove column and its constraints from original table.
columns.forEach(column => {
table.removeColumn(column);
table.findColumnUniques(column).forEach(unique => table.removeUniqueConstraint(unique));
table.findColumnIndices(column).forEach(index => table.removeIndex(index));
table.findColumnForeignKeys(column).forEach(fk => table.removeForeignKey(fk));
});
}
/**
* Creates a new primary key.
*/
async createPrimaryKey(tableOrName: Table|string, columnNames: string[]): Promise<void> {
const table = tableOrName instanceof Table ? tableOrName : await this.getCachedTable(tableOrName);
// clone original table and mark columns as primary
const changedTable = table.clone();
changedTable.columns.forEach(column => {
if (columnNames.find(columnName => columnName === column.name))
column.isPrimary = true;
});
await this.recreateTable(changedTable, table);
// mark columns as primary in original table
table.columns.forEach(column => {
if (columnNames.find(columnName => columnName === column.name))
column.isPrimary = true;
});
}
/**
* Updates composite primary keys.
*/
async updatePrimaryKeys(tableOrName: Table|string, columns: TableColumn[]): Promise<void> {
await Promise.resolve();
}
/**
* Drops a primary key.
*/
async dropPrimaryKey(tableOrName: Table|string): Promise<void> {
const table = tableOrName instanceof Table ? tableOrName : await this.getCachedTable(tableOrName);
// clone original table and mark primary columns as non-primary
const changedTable = table.clone();
changedTable.primaryColumns.forEach(column => {
column.isPrimary = false;
});
await this.recreateTable(changedTable, table);
// mark primary columns as non-primary in original table
table.primaryColumns.forEach(column => {
column.isPrimary = false;
});
}
/**
* Creates a new unique constraint.
*/
async createUniqueConstraint(tableOrName: Table|string, uniqueConstraint: TableUnique): Promise<void> {
await this.createUniqueConstraints(tableOrName, [uniqueConstraint]);
}
/**
* Creates a new unique constraints.
*/
async createUniqueConstraints(tableOrName: Table|string, uniqueConstraints: TableUnique[]): Promise<void> {
const table = tableOrName instanceof Table ? tableOrName : await this.getCachedTable(tableOrName);
// clone original table and add unique constraints in to cloned table
const changedTable = table.clone();
uniqueConstraints.forEach(uniqueConstraint => changedTable.addUniqueConstraint(uniqueConstraint));
await this.recreateTable(changedTable, table);
}
/**
* Drops an unique constraint.
*/
async dropUniqueConstraint(tableOrName: Table|string, uniqueOrName: TableUnique|string): Promise<void> {
const table = tableOrName instanceof Table ? tableOrName : await this.getCachedTable(tableOrName);
const uniqueConstraint = uniqueOrName instanceof TableUnique ? uniqueOrName : table.uniques.find(u => u.name === uniqueOrName);
if (!uniqueConstraint)
throw new Error(`Supplied unique constraint was not found in table ${table.name}`);
await this.dropUniqueConstraints(table, [uniqueConstraint]);
}
/**
* Creates an unique constraints.
*/
async dropUniqueConstraints(tableOrName: Table|string, uniqueConstraints: TableUnique[]): Promise<void> {
const table = tableOrName instanceof Table ? tableOrName : await this.getCachedTable(tableOrName);
// clone original table and remove unique constraints from cloned table
const changedTable = table.clone();
uniqueConstraints.forEach(uniqueConstraint => changedTable.removeUniqueConstraint(uniqueConstraint));
await this.recreateTable(changedTable, table);
}
/**
* Creates new check constraint.
*/
async createCheckConstraint(tableOrName: Table|string, checkConstraint: TableCheck): Promise<void> {
await this.createCheckConstraints(tableOrName, [checkConstraint]);
}
/**
* Creates new check constraints.
*/
async createCheckConstraints(tableOrName: Table|string, checkConstraints: TableCheck[]): Promise<void> {
const table = tableOrName instanceof Table ? tableOrName : await this.getCachedTable(tableOrName);
// clone original table and add check constraints in to cloned table
const changedTable = table.clone();
checkConstraints.forEach(checkConstraint => changedTable.addCheckConstraint(checkConstraint));
await this.recreateTable(changedTable, table);
}
/**
* Drops check constraint.
*/
async dropCheckConstraint(tableOrName: Table|string, checkOrName: TableCheck|string): Promise<void> {
const table = tableOrName instanceof Table ? tableOrName : await this.getCachedTable(tableOrName);
const checkConstraint = checkOrName instanceof TableCheck ? checkOrName : table.checks.find(c => c.name === checkOrName);
if (!checkConstraint)
throw new Error(`Supplied check constraint was not found in table ${table.name}`);
await this.dropCheckConstraints(table, [checkConstraint]);
}
/**
* Drops check constraints.
*/
async dropCheckConstraints(tableOrName: Table|string, checkConstraints: TableCheck[]): Promise<void> {
const table = tableOrName instanceof Table ? tableOrName : await this.getCachedTable(tableOrName);
// clone original table and remove check constraints from cloned table
const changedTable = table.clone();
checkConstraints.forEach(checkConstraint => changedTable.removeCheckConstraint(checkConstraint));
await this.recreateTable(changedTable, table);
}
/**
* Creates a new exclusion constraint.
*/
async createExclusionConstraint(tableOrName: Table|string, exclusionConstraint: TableExclusion): Promise<void> {
throw new Error(`Sqlite does not support exclusion constraints.`);
}
/**
* Creates a new exclusion constraints.
*/
async createExclusionConstraints(tableOrName: Table|string, exclusionConstraints: TableExclusion[]): Promise<void> {
throw new Error(`Sqlite does not support exclusion constraints.`);
}
/**
* Drops exclusion constraint.
*/
async dropExclusionConstraint(tableOrName: Table|string, exclusionOrName: TableExclusion|string): Promise<void> {
throw new Error(`Sqlite does not support exclusion constraints.`);
}
/**
* Drops exclusion constraints.
*/
async dropExclusionConstraints(tableOrName: Table|string, exclusionConstraints: TableExclusion[]): Promise<void> {
throw new Error(`Sqlite does not support exclusion constraints.`);
}
/**
* Creates a new foreign key.
*/
async createForeignKey(tableOrName: Table|string, foreignKey: TableForeignKey): Promise<void> {
await this.createForeignKeys(tableOrName, [foreignKey]);
}
/**
* Creates a new foreign keys.
*/
async createForeignKeys(tableOrName: Table|string, foreignKeys: TableForeignKey[]): Promise<void> {
const table = tableOrName instanceof Table ? tableOrName : await this.getCachedTable(tableOrName);
// clone original table and add foreign keys in to cloned table
const changedTable = table.clone();
foreignKeys.forEach(foreignKey => changedTable.addForeignKey(foreignKey));
await this.recreateTable(changedTable, table);
}
/**
* Drops a foreign key from the table.
*/
async dropForeignKey(tableOrName: Table|string, foreignKeyOrName: TableForeignKey|string): Promise<void> {
const table = tableOrName instanceof Table ? tableOrName : await this.getCachedTable(tableOrName);
const foreignKey = foreignKeyOrName instanceof TableForeignKey ? foreignKeyOrName : table.foreignKeys.find(fk => fk.name === foreignKeyOrName);
if (!foreignKey)
throw new Error(`Supplied foreign key was not found in table ${table.name}`);
await this.dropForeignKeys(tableOrName, [foreignKey]);
}
/**
* Drops a foreign keys from the table.
*/
async dropForeignKeys(tableOrName: Table|string, foreignKeys: TableForeignKey[]): Promise<void> {
const table = tableOrName instanceof Table ? tableOrName : await this.getCachedTable(tableOrName);
// clone original table and remove foreign keys from cloned table
const changedTable = table.clone();
foreignKeys.forEach(foreignKey => changedTable.removeForeignKey(foreignKey));
await this.recreateTable(changedTable, table);
}
/**
* Creates a new index.
*/
async createIndex(tableOrName: Table|string, index: TableIndex): Promise<void> {
const table = tableOrName instanceof Table ? tableOrName : await this.getCachedTable(tableOrName);
// new index may be passed without name. In this case we generate index name manually.
if (!index.name)
index.name = this.connection.namingStrategy.indexName(table.name, index.columnNames, index.where);
const up = this.createIndexSql(table, index);
const down = this.dropIndexSql(index);
await this.executeQueries(up, down);
table.addIndex(index);
}
/**
* Creates a new indices
*/
async createIndices(tableOrName: Table|string, indices: TableIndex[]): Promise<void> {
const promises = indices.map(index => this.createIndex(tableOrName, index));
await Promise.all(promises);
}
/**
* Drops an index from the table.
*/
async dropIndex(tableOrName: Table|string, indexOrName: TableIndex|string): Promise<void> {
const table = tableOrName instanceof Table ? tableOrName : await this.getCachedTable(tableOrName);
const index = indexOrName instanceof TableIndex ? indexOrName : table.indices.find(i => i.name === indexOrName);
if (!index)
throw new Error(`Supplied index was not found in table ${table.name}`);
const up = this.dropIndexSql(index);
const down = this.createIndexSql(table, index);
await this.executeQueries(up, down);
table.removeIndex(index);
}
/**
* Drops an indices from the table.
*/
async dropIndices(tableOrName: Table|string, indices: TableIndex[]): Promise<void> {
const promises = indices.map(index => this.dropIndex(tableOrName, index));
await Promise.all(promises);
}
/**
* Clears all table contents.
* Note: this operation uses SQL's TRUNCATE query which cannot be reverted in transactions.
*/
async clearTable(tableName: string): Promise<void> {
await this.query(`DELETE FROM "${tableName}"`);
}
/**
* Removes all tables from the currently connected database.
*/
async clearDatabase(): Promise<void> {
await this.query(`PRAGMA foreign_keys = OFF;`);
await this.startTransaction();
try {
const selectViewDropsQuery = `SELECT 'DROP VIEW "' || name || '";' as query FROM "sqlite_master" WHERE "type" = 'view'`;
const dropViewQueries: ObjectLiteral[] = await this.query(selectViewDropsQuery);
await Promise.all(dropViewQueries.map(q => this.query(q["query"])));
const selectTableDropsQuery = `SELECT 'DROP TABLE "' || name || '";' as query FROM "sqlite_master" WHERE "type" = 'table' AND "name" != 'sqlite_sequence'`;
const dropTableQueries: ObjectLiteral[] = await this.query(selectTableDropsQuery);
await Promise.all(dropTableQueries.map(q => this.query(q["query"])));
await this.commitTransaction();
} catch (error) {
try { // we throw original error even if rollback thrown an error
await this.rollbackTransaction();
} catch (rollbackError) { }
throw error;
} finally {
await this.query(`PRAGMA foreign_keys = ON;`);
}
}
// -------------------------------------------------------------------------
// Protected Methods
// -------------------------------------------------------------------------
protected async loadViews(viewNames: string[]): Promise<View[]> {
const hasTable = await this.hasTable(this.getTypeormMetadataTableName());
if (!hasTable)
return Promise.resolve([]);
const viewNamesString = viewNames.map(name => "'" + name + "'").join(", ");
let query = `SELECT "t".* FROM "${this.getTypeormMetadataTableName()}" "t" INNER JOIN "sqlite_master" s ON "s"."name" = "t"."name" AND "s"."type" = 'view' WHERE "t"."type" = 'VIEW'`;
if (viewNamesString.length > 0)
query += ` AND "t"."name" IN (${viewNamesString})`;
const dbViews = await this.query(query);
return dbViews.map((dbView: any) => {
const view = new View();
view.name = dbView["name"];
view.expression = dbView["value"];
return view;
});
}
/**
* Loads all tables (with given names) from the database and creates a Table from them.
*/
protected async loadTables(tableNames: string[]): Promise<Table[]> {
// if no tables given then no need to proceed
if (!tableNames || !tableNames.length)
return [];
const tableNamesString = tableNames.map(tableName => `'${tableName}'`).join(", ");
// load tables
const dbTables: ObjectLiteral[] = await this.query(`SELECT * FROM "sqlite_master" WHERE "type" = 'table' AND "name" IN (${tableNamesString})`);
// load indices
const dbIndicesDef: ObjectLiteral[] = await this.query(`SELECT * FROM "sqlite_master" WHERE "type" = 'index' AND "tbl_name" IN (${tableNamesString})`);
// if tables were not found in the db, no need to proceed
if (!dbTables || !dbTables.length)
return [];
// create table schemas for loaded tables
return Promise.all(dbTables.map(async dbTable => {
const table = new Table({name: dbTable["name"]});
const sql = dbTable["sql"];
// load columns and indices
const [dbColumns, dbIndices, dbForeignKeys]: ObjectLiteral[][] = await Promise.all([
this.query(`PRAGMA table_info("${dbTable["name"]}")`),
this.query(`PRAGMA index_list("${dbTable["name"]}")`),
this.query(`PRAGMA foreign_key_list("${dbTable["name"]}")`),
]);
// find column name with auto increment
let autoIncrementColumnName: string|undefined = undefined;
const tableSql: string = dbTable["sql"];
let autoIncrementIndex = tableSql.toUpperCase().indexOf("AUTOINCREMENT");
if (autoIncrementIndex !== -1) {
autoIncrementColumnName = tableSql.substr(0, autoIncrementIndex);
const comma = autoIncrementColumnName.lastIndexOf(",");
const bracket = autoIncrementColumnName.lastIndexOf("(");
if (comma !== -1) {
autoIncrementColumnName = autoIncrementColumnName.substr(comma);
autoIncrementColumnName = autoIncrementColumnName.substr(0, autoIncrementColumnName.lastIndexOf("\""));
autoIncrementColumnName = autoIncrementColumnName.substr(autoIncrementColumnName.indexOf("\"") + 1);
} else if (bracket !== -1) {
autoIncrementColumnName = autoIncrementColumnName.substr(bracket);
autoIncrementColumnName = autoIncrementColumnName.substr(0, autoIncrementColumnName.lastIndexOf("\""));
autoIncrementColumnName = autoIncrementColumnName.substr(autoIncrementColumnName.indexOf("\"") + 1);
}
}
// create columns from the loaded columns
table.columns = dbColumns.map(dbColumn => {
const tableColumn = new TableColumn();
tableColumn.name = dbColumn["name"];
tableColumn.type = dbColumn["type"].toLowerCase();
tableColumn.default = dbColumn["dflt_value"] !== null && dbColumn["dflt_value"] !== undefined ? dbColumn["dflt_value"] : undefined;
tableColumn.isNullable = dbColumn["notnull"] === 0;
// primary keys are numbered starting with 1, columns that aren't primary keys are marked with 0
tableColumn.isPrimary = dbColumn["pk"] > 0;
tableColumn.comment = ""; // SQLite does not support column comments
tableColumn.isGenerated = autoIncrementColumnName === dbColumn["name"];
if (tableColumn.isGenerated) {
tableColumn.generationStrategy = "increment";
}
if (tableColumn.type === "varchar") {
// Check if this is an enum
const enumMatch = sql.match(new RegExp("\"(" + tableColumn.name + ")\" varchar CHECK\\s*\\(\\s*\\1\\s+IN\\s*\\(('[^']+'(?:\\s*,\\s*'[^']+')+)\\s*\\)\\s*\\)"));
if (enumMatch) {
// This is an enum
tableColumn.type = "simple-enum";
tableColumn.enum = enumMatch[2].substr(1, enumMatch[2].length - 2).split("','");
}
}
// parse datatype and attempt to retrieve length, precision and scale
let pos = tableColumn.type.indexOf("(");
if (pos !== -1) {
const fullType = tableColumn.type;
let dataType = fullType.substr(0, pos);
if (!!this.driver.withLengthColumnTypes.find(col => col === dataType)) {
let len = parseInt(fullType.substring(pos + 1, fullType.length - 1));
if (len) {
tableColumn.length = len.toString();
tableColumn.type = dataType; // remove the length part from the datatype
}
}
if (!!this.driver.withPrecisionColumnTypes.find(col => col === dataType)) {
const re = new RegExp(`^${dataType}\\((\\d+),?\\s?(\\d+)?\\)`);
const matches = fullType.match(re);
if (matches && matches[1]) {
tableColumn.precision = +matches[1];
}
if (!!this.driver.withScaleColumnTypes.find(col => col === dataType)) {
if (matches && matches[2]) {
tableColumn.scale = +matches[2];
}
}
tableColumn.type = dataType; // remove the precision/scale part from the datatype
}
}
return tableColumn;
});
// build foreign keys
const tableForeignKeyConstraints = OrmUtils.uniq(dbForeignKeys, dbForeignKey => dbForeignKey["id"]);
table.foreignKeys = tableForeignKeyConstraints.map(foreignKey => {
const ownForeignKeys = dbForeignKeys.filter(dbForeignKey => dbForeignKey["id"] === foreignKey["id"] && dbForeignKey["table"] === foreignKey["table"]);
const columnNames = ownForeignKeys.map(dbForeignKey => dbForeignKey["from"]);
const referencedColumnNames = ownForeignKeys.map(dbForeignKey => dbForeignKey["to"]);
// build foreign key name, because we can not get it directly.
const fkName = this.connection.namingStrategy.foreignKeyName(table, columnNames, foreignKey.referencedTableName, foreignKey.referencedColumnNames);
return new TableForeignKey({
name: fkName,
columnNames: columnNames,
referencedTableName: foreignKey["table"],
referencedColumnNames: referencedColumnNames,
onDelete: foreignKey["on_delete"],
onUpdate: foreignKey["on_update"]
});
});
// build unique constraints
const tableUniquePromises = dbIndices
.filter(dbIndex => dbIndex["origin"] === "u")
.map(dbIndex => dbIndex["name"])
.filter((value, index, self) => self.indexOf(value) === index)
.map(async dbIndexName => {
const dbIndex = dbIndices.find(dbIndex => dbIndex["name"] === dbIndexName);
const indexInfos: ObjectLiteral[] = await this.query(`PRAGMA index_info("${dbIndex!["name"]}")`);
const indexColumns = indexInfos
.sort((indexInfo1, indexInfo2) => parseInt(indexInfo1["seqno"]) - parseInt(indexInfo2["seqno"]))
.map(indexInfo => indexInfo["name"]);
if (indexColumns.length === 1) {
const column = table.columns.find(column => {
return !!indexColumns.find(indexColumn => indexColumn === column.name);
});
if (column)
column.isUnique = true;
}
// Sqlite does not store unique constraint name, so we generate its name manually.
return new TableUnique({
name: this.connection.namingStrategy.uniqueConstraintName(table, indexColumns),
columnNames: indexColumns
});
});
table.uniques = (await Promise.all(tableUniquePromises)) as TableUnique[];
// build checks
let result;
const regexp = /CONSTRAINT "([^"]*)" CHECK (\(.*?\))([,]|[)]$)/g;
while (((result = regexp.exec(sql)) !== null)) {
table.checks.push(new TableCheck({ name: result[1], expression: result[2] }));
}
// build indices
const indicesPromises = dbIndices
.filter(dbIndex => dbIndex["origin"] === "c")
.map(dbIndex => dbIndex["name"])
.filter((value, index, self) => self.indexOf(value) === index) // unqiue
.map(async dbIndexName => {
const indexDef = dbIndicesDef.find(dbIndexDef => dbIndexDef["name"] === dbIndexName);
const condition = /WHERE (.*)/.exec(indexDef!["sql"]);
const dbIndex = dbIndices.find(dbIndex => dbIndex["name"] === dbIndexName);
const indexInfos: ObjectLiteral[] = await this.query(`PRAGMA index_info("${dbIndex!["name"]}")`);
const indexColumns = indexInfos
.sort((indexInfo1, indexInfo2) => parseInt(indexInfo1["seqno"]) - parseInt(indexInfo2["seqno"]))
.map(indexInfo => indexInfo["name"]);
const isUnique = dbIndex!["unique"] === "1" || dbIndex!["unique"] === 1;
return new TableIndex(<TableIndexOptions>{
table: table,
name: dbIndex!["name"],
columnNames: indexColumns,
isUnique: isUnique,
where: condition ? condition[1] : undefined
});
});
const indices = await Promise.all(indicesPromises);
table.indices = indices.filter(index => !!index) as TableIndex[];
return table;
}));
}
/**
* Builds create table sql.
*/
protected createTableSql(table: Table, createForeignKeys?: boolean): Query {
const primaryColumns = table.columns.filter(column => column.isPrimary);
const hasAutoIncrement = primaryColumns.find(column => column.isGenerated && column.generationStrategy === "increment");
const skipPrimary = primaryColumns.length > 1;
if (skipPrimary && hasAutoIncrement)
throw new Error(`Sqlite does not support AUTOINCREMENT on composite primary key`);
const columnDefinitions = table.columns.map(column => this.buildCreateColumnSql(column, skipPrimary)).join(", ");
let sql = `CREATE TABLE "${table.name}" (${columnDefinitions}`;
// need for `addColumn()` method, because it recreates table.
table.columns
.filter(column => column.isUnique)
.forEach(column => {
const isUniqueExist = table.uniques.some(unique => unique.columnNames.length === 1 && unique.columnNames[0] === column.name);
if (!isUniqueExist)
table.uniques.push(new TableUnique({
name: this.connection.namingStrategy.uniqueConstraintName(table.name, [column.name]),
columnNames: [column.name]
}));
});
if (table.uniques.length > 0) {
const uniquesSql = table.uniques.map(unique => {
const uniqueName = unique.name ? unique.name : this.connection.namingStrategy.uniqueConstraintName(table.name, unique.columnNames);
const columnNames = unique.columnNames.map(columnName => `"${columnName}"`).join(", ");
return `CONSTRAINT "${uniqueName}" UNIQUE (${columnNames})`;
}).join(", ");
sql += `, ${uniquesSql}`;
}
if (table.checks.length > 0) {
const checksSql = table.checks.map(check => {
const checkName = check.name ? check.name : this.connection.namingStrategy.checkConstraintName(table.name, check.expression!);
return `CONSTRAINT "${checkName}" CHECK (${check.expression})`;
}).join(", ");
sql += `, ${checksSql}`;
}
if (table.foreignKeys.length > 0 && createForeignKeys) {
const foreignKeysSql = table.foreignKeys.map(fk => {
const columnNames = fk.columnNames.map(columnName => `"${columnName}"`).join(", ");
if (!fk.name)
fk.name = this.connection.namingStrategy.foreignKeyName(table.name, fk.columnNames, fk.referencedTableName, fk.referencedColumnNames);
const referencedColumnNames = fk.referencedColumnNames.map(columnName => `"${columnName}"`).join(", ");
let constraint = `CONSTRAINT "${fk.name}" FOREIGN KEY (${columnNames}) REFERENCES "${fk.referencedTableName}" (${referencedColumnNames})`;
if (fk.onDelete)
constraint += ` ON DELETE ${fk.onDelete}`;
if (fk.onUpdate)
constraint += ` ON UPDATE ${fk.onUpdate}`;