Skip to content

Commit 5dfb1c8

Browse files
authored
Merge pull request #35 from Software-Developers-IRL/f/lastlink
F/lastlink 0.0.2 initial working create table generator
2 parents 936b24b + 4f35555 commit 5dfb1c8

File tree

3 files changed

+181
-88
lines changed

3 files changed

+181
-88
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "little-mermaid-2-the-sql",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"description": "",
55
"main": "./lib/index.js",
66
"engines": {

samples/chinook-database-2.0.1.md

Lines changed: 89 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -10,121 +10,126 @@ sequenceDiagram
1010
```mermaid
1111
erDiagram
1212
13-
artists {
14-
INTEGER ArtistId PK "'Artist' nullable"
15-
NVARCHAR Name
13+
Artist {
14+
INTEGER ArtistId PK "NOT NULL"
15+
NVARCHAR120 Name
1616
}
1717
18-
employees {
19-
INTEGER EmployeeId
20-
NVARCHAR LastName
21-
NVARCHAR FirstName
22-
NVARCHAR Title
23-
INTEGER ReportsTo
18+
Employee {
19+
INTEGER EmployeeId PK "NOT NULL"
20+
NVARCHAR20 LastName "NOT NULL"
21+
NVARCHAR20 FirstName "NOT NULL"
22+
NVARCHAR30 Title
23+
INTEGER ReportsTo FK
2424
DATETIME BirthDate
2525
DATETIME HireDate
26-
NVARCHAR Address
27-
NVARCHAR City
28-
NVARCHAR State
29-
NVARCHAR Country
30-
NVARCHAR PostalCode
31-
NVARCHAR Phone
32-
NVARCHAR Fax
33-
NVARCHAR Email
26+
NVARCHAR70 Address
27+
NVARCHAR40 City
28+
NVARCHAR40 State
29+
NVARCHAR40 Country
30+
NVARCHAR10 PostalCode
31+
NVARCHAR24 Phone
32+
NVARCHAR24 Fax
33+
NVARCHAR60 Email
3434
}
3535
36-
genres {
37-
INTEGER GenreId
38-
NVARCHAR Name
36+
Genre {
37+
INTEGER GenreId PK "NOT NULL"
38+
NVARCHAR120 Name
3939
}
4040
41-
media_types {
42-
INTEGER MediaTypeId
43-
NVARCHAR Name
41+
MediaType {
42+
INTEGER MediaTypeId PK "NOT NULL"
43+
NVARCHAR120 Name
4444
}
4545
46-
playlists {
47-
INTEGER PlaylistId
48-
NVARCHAR Name
46+
Playlist {
47+
INTEGER PlaylistId PK "NOT NULL"
48+
NVARCHAR120 Name
4949
}
5050
51-
albums {
52-
INTEGER AlbumId
53-
NVARCHAR Title
54-
INTEGER ArtistId
51+
Album {
52+
INTEGER AlbumId PK "NOT NULL"
53+
NVARCHAR160 Title "NOT NULL"
54+
INTEGER ArtistId FK "NOT NULL"
5555
}
5656
57-
customers {
58-
INTEGER CustomerId
59-
NVARCHAR FirstName
60-
NVARCHAR LastName
61-
NVARCHAR Company
62-
NVARCHAR Address
63-
NVARCHAR City
64-
NVARCHAR State
65-
NVARCHAR Country
66-
NVARCHAR PostalCode
67-
NVARCHAR Phone
68-
NVARCHAR Fax
69-
NVARCHAR Email
70-
INTEGER SupportRepId
57+
Customer {
58+
INTEGER CustomerId PK "NOT NULL"
59+
NVARCHAR40 FirstName "NOT NULL"
60+
NVARCHAR20 LastName "NOT NULL"
61+
NVARCHAR80 Company
62+
NVARCHAR70 Address
63+
NVARCHAR40 City
64+
NVARCHAR40 State
65+
NVARCHAR40 Country
66+
NVARCHAR10 PostalCode
67+
NVARCHAR24 Phone
68+
NVARCHAR24 Fax
69+
NVARCHAR60 Email "NOT NULL"
70+
INTEGER SupportRepId FK
7171
}
7272
73-
invoices {
74-
INTEGER InvoiceId
75-
INTEGER CustomerId
76-
DATETIME InvoiceDate
77-
NVARCHAR BillingAddress
78-
NVARCHAR BillingCity
79-
NVARCHAR BillingState
80-
NVARCHAR BillingCountry
81-
NVARCHAR BillingPostalCode
82-
NUMERIC Total
73+
test_table {
74+
INTEGER id PK "NOT NULL"
75+
TEXT Field2_2 "'Field 2_2'"
76+
INTEGER ArtistId FK "'Artist Id'"
8377
}
8478
85-
tracks {
86-
INTEGER TrackId
87-
NVARCHAR Name
88-
INTEGER AlbumId
89-
INTEGER MediaTypeId
90-
INTEGER GenreId
91-
NVARCHAR Composer
92-
INTEGER Milliseconds
93-
INTEGER Bytes
94-
NUMERIC UnitPrice
79+
Invoice {
80+
INTEGER InvoiceId PK "NOT NULL"
81+
INTEGER CustomerId FK "NOT NULL"
82+
DATETIME InvoiceDate "NOT NULL"
83+
NVARCHAR70 BillingAddress
84+
NVARCHAR40 BillingCity
85+
NVARCHAR40 BillingState
86+
NVARCHAR40 BillingCountry
87+
NVARCHAR10 BillingPostalCode
88+
NUMERIC10_2 Total "NOT NULL"
9589
}
9690
97-
invoice_items {
98-
INTEGER InvoiceLineId
99-
INTEGER InvoiceId
100-
INTEGER TrackId
101-
NUMERIC UnitPrice
102-
INTEGER Quantity
91+
Track {
92+
INTEGER TrackId PK "NOT NULL"
93+
NVARCHAR200 Name "NOT NULL"
94+
INTEGER AlbumId FK
95+
INTEGER MediaTypeId FK "NOT NULL"
96+
INTEGER GenreId FK
97+
NVARCHAR220 Composer
98+
INTEGER Milliseconds "NOT NULL"
99+
INTEGER Bytes
100+
NUMERIC10_2 UnitPrice "NOT NULL"
103101
}
104102
105-
playlist_track {
106-
INTEGER PlaylistId
107-
INTEGER TrackId
103+
InvoiceLine {
104+
INTEGER InvoiceLineId PK "NOT NULL"
105+
INTEGER InvoiceId FK "NOT NULL"
106+
INTEGER TrackId FK "NOT NULL"
107+
NUMERIC10_2 UnitPrice "NOT NULL"
108+
INTEGER Quantity "NOT NULL"
108109
}
109110
110-
artists ||--o{ albums : "foreign key"
111+
PlaylistTrack {
112+
INTEGER PlaylistId PK "NOT NULL"
113+
INTEGER TrackId PK "NOT NULL"
114+
}
111115
112-
employees ||--o{ customers : "foreign key"
113-
employees ||--o{ employees : "foreign key"
116+
Artist ||--o{ Album : "[Artist.ArtistId] to [Album.ArtistId]"
114117
115-
genres ||--o{ tracks : "foreign key"
118+
Employee ||--o{ Customer : "[Employee.EmployeeId] to [Customer.SupportRepId]"
116119
117-
media_types ||--o{ tracks : "foreign key"
120+
Artist ||--o{ test_table : "[Artist.ArtistId] to ['test_table'.'Artist Id']"
118121
119-
playlists ||--o{ playlist_track : "foreign key"
122+
Customer ||--o{ Invoice : "[Customer.CustomerId] to [Invoice.CustomerId]"
120123
121-
albums ||--o{ tracks : "foreign key"
124+
Album ||--o{ Track : "[Album.AlbumId] to [Track.AlbumId]"
125+
Genre ||--o{ Track : "[Genre.GenreId] to [Track.GenreId]"
126+
MediaType ||--o{ Track : "[MediaType.MediaTypeId] to [Track.MediaTypeId]"
122127
123-
customers ||--o{ invoices : "foreign key"
128+
Invoice ||--o{ InvoiceLine : "[Invoice.InvoiceId] to [InvoiceLine.InvoiceId]"
129+
Track ||--o{ InvoiceLine : "[Track.TrackId] to [InvoiceLine.TrackId]"
124130
125-
invoices ||--o{ invoice_items : "foreign key"
131+
Playlist ||--o{ PlaylistTrack : "[Playlist.PlaylistId] to [PlaylistTrack.PlaylistId]"
132+
Track ||--o{ PlaylistTrack : "[Track.TrackId] to [PlaylistTrack.TrackId]"
126133
127-
tracks ||--o{ invoice_items : "foreign key"
128-
tracks ||--o{ playlist_track : "foreign key"
129134
130135
```

src/generate-sql-ddl.ts

Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ export class DbParser {
4444
if (this.entities) {
4545
for (const key in this.entities) {
4646
if (Object.prototype.hasOwnProperty.call(this.entities, key)) {
47-
const element = this.entities[key];
47+
const entity = this.entities[key];
48+
statementGeneration.push(this.createTable(key, entity));
4849
}
4950
}
5051
}
@@ -71,11 +72,98 @@ export class DbParser {
7172
// }
7273
return statementGeneration.join("");
7374
}
75+
/**
76+
* convert labels with start and end strings per database type
77+
* @param label
78+
* @returns
79+
*/
80+
private dbTypeEnds(label: string) {
81+
let char1 = '"';
82+
let char2 = '"';
83+
if (this.dbType == "mysql") {
84+
char1 = "`";
85+
char2 = "`";
86+
} else if (this.dbType == "sqlserver") {
87+
char1 = "[";
88+
char2 = "]";
89+
}
90+
return `${char1}${label}${char2}`;
91+
}
92+
/**
93+
* generate create table statement
94+
* @param entityKey
95+
* @param entity
96+
* @returns
97+
*/
98+
private createTable(entityKey: string, entity: DbEntityDefinition) {
99+
let statement = `CREATE TABLE ${this.dbTypeEnds(entityKey)} (\n`;
100+
// TODO: incorporate foreign keys using relationships
101+
for (let i = 0; i < entity.attributes.length; i++) {
102+
const attribute = entity.attributes[i];
103+
if (attribute.attributeType && attribute.attributeName) {
104+
// need to add parenthesis or commas
105+
let columnType = attribute.attributeType.replace("_", ",");
106+
let columnTypeLength = columnType.replace(/[^0-9,]/gim, "");
107+
columnType = (
108+
columnType.replace(/[^a-z]/gim, "") +
109+
(columnTypeLength ? `(${columnTypeLength})` : "")
110+
).trim();
111+
if (attribute.attributeComment) {
112+
// check if contains full column name
113+
statement += `\t${this.dbTypeEnds(
114+
attribute.attributeName
115+
)} ${columnType} ${attribute.attributeComment}`;
116+
} else {
117+
statement += `\t${this.dbTypeEnds(
118+
attribute.attributeName
119+
)} ${columnType}`;
120+
}
121+
statement += i != entity.attributes.length - 1 ? ",\n" : "\n";
122+
}
123+
}
74124

75-
private createTable(tableName: string) {
76-
return `CREATE TABLE ${tableName}`;
125+
statement += `\n)`;
126+
return statement;
77127
}
78128

129+
/* database create table examples:
130+
sqlite example:
131+
CREATE TABLE [test_table] (
132+
"id" INTEGER NOT NULL,
133+
"Field 2_2" TEXT,
134+
"Artist Id" INTEGER,
135+
PRIMARY KEY("id"),
136+
FOREIGN KEY("Artist Id") REFERENCES "Artist"("ArtistId") ON DELETE NO ACTION ON UPDATE NO ACTION
137+
)
138+
139+
mysql example:
140+
CREATE TABLE `test_table` (
141+
`id` INTEGER NOT NULL,
142+
`Field 2_2` TEXT,
143+
`Artist Id` INTEGER,
144+
PRIMARY KEY (`id`),
145+
FOREIGN KEY (`Artist Id`) REFERENCES `Artist`(`ArtistId`)
146+
);
147+
148+
postgres example:
149+
CREATE TABLE "test_table" (
150+
"id" INTEGER NOT NULL,
151+
"Field 2_2" TEXT,
152+
"Artist Id" INTEGER,
153+
PRIMARY KEY ("id"),
154+
FOREIGN KEY ("Artist Id"") REFERENCES "Artist"("ArtistId")
155+
);
156+
157+
sql server example:
158+
CREATE TABLE [test_table] (
159+
[id] INTEGER NOT NULL,
160+
[Field 2_2] TEXT,
161+
[Artist Id] INTEGER,
162+
PRIMARY KEY ([id]),
163+
FOREIGN KEY ([Artist Id]) REFERENCES [Artist]([ArtistId])
164+
);
165+
*/
166+
79167
private createColumn(
80168
tableName: string,
81169
columnName: string,

0 commit comments

Comments
 (0)