-
-
Notifications
You must be signed in to change notification settings - Fork 496
/
SchemaGenerator.ts
155 lines (113 loc) · 4.96 KB
/
SchemaGenerator.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
import { Cascade, IDatabaseDriver, ReferenceType } from '..';
import { EntityMetadata, EntityProperty } from '../decorators';
import { Platform } from '../platforms';
export class SchemaGenerator {
private readonly platform: Platform = this.driver.getPlatform();
private readonly helper = this.platform.getSchemaHelper();
constructor(private readonly driver: IDatabaseDriver,
private readonly metadata: Record<string, EntityMetadata>) { }
generate(): string {
let ret = this.helper.getSchemaBeginning();
Object.values(this.metadata).forEach(meta => {
ret += this.helper.dropTable(meta) + '\n';
ret += this.createTable(meta) + '\n';
});
Object.values(this.metadata).forEach(meta => {
ret += this.createForeignKeys(meta);
});
ret += this.helper.getSchemaEnd();
return ret;
}
private createTable(meta: EntityMetadata): string {
const pkProp = meta.properties[meta.primaryKey];
let ret = '';
if (this.helper.supportsSequences() && pkProp.type === 'number') {
ret += `CREATE SEQUENCE ${this.helper.quoteIdentifier(meta.collection + '_seq')};\n`;
}
ret += `CREATE TABLE ${this.helper.quoteIdentifier(meta.collection)} (\n`;
Object
.values(meta.properties)
.filter(prop => (prop.reference === ReferenceType.SCALAR || (this.helper.supportsSchemaConstraints() && prop.reference === ReferenceType.MANY_TO_ONE)))
.forEach(prop => ret += ' ' + this.createTableColumn(meta, prop) + ',\n');
if (this.helper.supportsSchemaConstraints()) {
ret += this.createIndexes(meta);
} else {
ret = ret.substr(0, ret.length - 2) + '\n';
}
ret += `)${this.helper.getSchemaTableEnd()};\n\n`;
return ret;
}
private createTableColumn(meta: EntityMetadata, prop: EntityProperty): string {
const fieldName = prop.fieldName;
let ret = this.helper.quoteIdentifier(fieldName) + ' ' + this.type(prop);
if (!prop.primary) {
return ret + ` DEFAULT ${prop.default || 'NULL'}`;
}
ret += ' ' + this.helper.getPrimaryKeySubtype(meta);
if (prop.type === 'number') {
ret += ' ' + this.helper.getAutoIncrementStatement(meta);
}
return ret;
}
private createIndexes(meta: EntityMetadata): string {
let ret = ` PRIMARY KEY (${this.helper.quoteIdentifier(meta.properties[meta.primaryKey].fieldName)})`;
if (this.helper.indexForeignKeys()) {
Object
.values(meta.properties)
.filter(prop => prop.reference === ReferenceType.MANY_TO_ONE)
.forEach(prop => ret += `,\n KEY ${this.helper.quoteIdentifier(prop.fieldName)} (${this.helper.quoteIdentifier(prop.fieldName)})`);
}
return ret + '\n';
}
private createForeignKeys(meta: EntityMetadata): string {
const ret = `ALTER TABLE ${this.helper.quoteIdentifier(meta.collection)}`;
let i = 1;
const constraints = Object
.values(meta.properties)
.filter(prop => prop.reference === ReferenceType.MANY_TO_ONE)
.map(prop => this.createForeignKey(meta, prop, i++));
if (constraints.length === 0) {
return '';
}
if (this.helper.supportsSchemaMultiAlter()) {
return ret + '\n ' + constraints.join(',\n ') + ';\n\n\n';
}
return constraints.map(c => ret + c + ';').join('\n') + '\n\n';
}
private createForeignKey(meta: EntityMetadata, prop: EntityProperty, index: number): string {
if (this.helper.supportsSchemaConstraints()) {
return this.createForeignConstraint(meta, prop, index);
}
let ret = ' ADD ' + this.createTableColumn(meta, prop) + ' ';
ret += this.createForeignKeyReference(prop);
return ret;
}
private createForeignConstraint(meta: EntityMetadata, prop: EntityProperty, index: number): string {
let ret = ' ADD CONSTRAINT ' + this.helper.quoteIdentifier(meta.collection + '_ibfk_' + index);
ret += ` FOREIGN KEY (${this.helper.quoteIdentifier(prop.fieldName)}) `;
ret += this.createForeignKeyReference(prop);
return ret;
}
private createForeignKeyReference(prop: EntityProperty): string {
const meta2 = this.metadata[prop.type];
const pk2 = meta2.properties[meta2.primaryKey].fieldName;
let ret = `REFERENCES ${this.helper.quoteIdentifier(meta2.collection)} (${this.helper.quoteIdentifier(pk2)})`;
const cascade = prop.cascade.includes(Cascade.REMOVE) || prop.cascade.includes(Cascade.ALL);
ret += ` ON DELETE ${cascade ? 'CASCADE' : 'SET NULL'}`;
if (prop.cascade.includes(Cascade.PERSIST) || prop.cascade.includes(Cascade.ALL)) {
ret += ' ON UPDATE CASCADE';
}
return ret;
}
private type(prop: EntityProperty, foreignKey?: EntityProperty): string {
const type = this.helper.getTypeDefinition(prop);
if (prop.reference !== ReferenceType.SCALAR) {
const meta = this.metadata[prop.type];
return this.type(meta.properties[meta.primaryKey], prop);
}
if (prop.type === 'number' && prop.primary) {
return type + this.helper.getUnsignedSuffix(foreignKey || prop);
}
return type;
}
}