From 489efb62aa4244ec8d9d4d2c00be780d1494953e Mon Sep 17 00:00:00 2001 From: lastlink Date: Sat, 1 Oct 2022 15:05:50 -0400 Subject: [PATCH 1/3] new example source --- samples/chinook-database-2.0.1.md | 173 +++++++++++++++--------------- 1 file changed, 89 insertions(+), 84 deletions(-) diff --git a/samples/chinook-database-2.0.1.md b/samples/chinook-database-2.0.1.md index 49bbccc..04d9355 100644 --- a/samples/chinook-database-2.0.1.md +++ b/samples/chinook-database-2.0.1.md @@ -10,121 +10,126 @@ sequenceDiagram ```mermaid erDiagram - artists { - INTEGER ArtistId PK "'Artist' nullable" - NVARCHAR Name + Artist { + INTEGER ArtistId PK "NOT NULL" + NVARCHAR120 Name } - employees { - INTEGER EmployeeId - NVARCHAR LastName - NVARCHAR FirstName - NVARCHAR Title - INTEGER ReportsTo + Employee { + INTEGER EmployeeId PK "NOT NULL" + NVARCHAR20 LastName "NOT NULL" + NVARCHAR20 FirstName "NOT NULL" + NVARCHAR30 Title + INTEGER ReportsTo FK DATETIME BirthDate DATETIME HireDate - NVARCHAR Address - NVARCHAR City - NVARCHAR State - NVARCHAR Country - NVARCHAR PostalCode - NVARCHAR Phone - NVARCHAR Fax - NVARCHAR Email + NVARCHAR70 Address + NVARCHAR40 City + NVARCHAR40 State + NVARCHAR40 Country + NVARCHAR10 PostalCode + NVARCHAR24 Phone + NVARCHAR24 Fax + NVARCHAR60 Email } - genres { - INTEGER GenreId - NVARCHAR Name + Genre { + INTEGER GenreId PK "NOT NULL" + NVARCHAR120 Name } - media_types { - INTEGER MediaTypeId - NVARCHAR Name + MediaType { + INTEGER MediaTypeId PK "NOT NULL" + NVARCHAR120 Name } - playlists { - INTEGER PlaylistId - NVARCHAR Name + Playlist { + INTEGER PlaylistId PK "NOT NULL" + NVARCHAR120 Name } - albums { - INTEGER AlbumId - NVARCHAR Title - INTEGER ArtistId + Album { + INTEGER AlbumId PK "NOT NULL" + NVARCHAR160 Title "NOT NULL" + INTEGER ArtistId FK "NOT NULL" } - customers { - INTEGER CustomerId - NVARCHAR FirstName - NVARCHAR LastName - NVARCHAR Company - NVARCHAR Address - NVARCHAR City - NVARCHAR State - NVARCHAR Country - NVARCHAR PostalCode - NVARCHAR Phone - NVARCHAR Fax - NVARCHAR Email - INTEGER SupportRepId + Customer { + INTEGER CustomerId PK "NOT NULL" + NVARCHAR40 FirstName "NOT NULL" + NVARCHAR20 LastName "NOT NULL" + NVARCHAR80 Company + NVARCHAR70 Address + NVARCHAR40 City + NVARCHAR40 State + NVARCHAR40 Country + NVARCHAR10 PostalCode + NVARCHAR24 Phone + NVARCHAR24 Fax + NVARCHAR60 Email "NOT NULL" + INTEGER SupportRepId FK } - invoices { - INTEGER InvoiceId - INTEGER CustomerId - DATETIME InvoiceDate - NVARCHAR BillingAddress - NVARCHAR BillingCity - NVARCHAR BillingState - NVARCHAR BillingCountry - NVARCHAR BillingPostalCode - NUMERIC Total + test_table { + INTEGER id PK "NOT NULL" + TEXT Field2_2 "'Field 2_2'" + INTEGER ArtistId FK "'Artist Id'" } - tracks { - INTEGER TrackId - NVARCHAR Name - INTEGER AlbumId - INTEGER MediaTypeId - INTEGER GenreId - NVARCHAR Composer - INTEGER Milliseconds - INTEGER Bytes - NUMERIC UnitPrice + Invoice { + INTEGER InvoiceId PK "NOT NULL" + INTEGER CustomerId FK "NOT NULL" + DATETIME InvoiceDate "NOT NULL" + NVARCHAR70 BillingAddress + NVARCHAR40 BillingCity + NVARCHAR40 BillingState + NVARCHAR40 BillingCountry + NVARCHAR10 BillingPostalCode + NUMERIC10_2 Total "NOT NULL" } - invoice_items { - INTEGER InvoiceLineId - INTEGER InvoiceId - INTEGER TrackId - NUMERIC UnitPrice - INTEGER Quantity + Track { + INTEGER TrackId PK "NOT NULL" + NVARCHAR200 Name "NOT NULL" + INTEGER AlbumId FK + INTEGER MediaTypeId FK "NOT NULL" + INTEGER GenreId FK + NVARCHAR220 Composer + INTEGER Milliseconds "NOT NULL" + INTEGER Bytes + NUMERIC10_2 UnitPrice "NOT NULL" } - playlist_track { - INTEGER PlaylistId - INTEGER TrackId + InvoiceLine { + INTEGER InvoiceLineId PK "NOT NULL" + INTEGER InvoiceId FK "NOT NULL" + INTEGER TrackId FK "NOT NULL" + NUMERIC10_2 UnitPrice "NOT NULL" + INTEGER Quantity "NOT NULL" } - artists ||--o{ albums : "foreign key" + PlaylistTrack { + INTEGER PlaylistId PK "NOT NULL" + INTEGER TrackId PK "NOT NULL" + } - employees ||--o{ customers : "foreign key" - employees ||--o{ employees : "foreign key" + Artist ||--o{ Album : "[Artist.ArtistId] to [Album.ArtistId]" - genres ||--o{ tracks : "foreign key" + Employee ||--o{ Customer : "[Employee.EmployeeId] to [Customer.SupportRepId]" - media_types ||--o{ tracks : "foreign key" + Artist ||--o{ test_table : "[Artist.ArtistId] to ['test_table'.'Artist Id']" - playlists ||--o{ playlist_track : "foreign key" + Customer ||--o{ Invoice : "[Customer.CustomerId] to [Invoice.CustomerId]" - albums ||--o{ tracks : "foreign key" + Album ||--o{ Track : "[Album.AlbumId] to [Track.AlbumId]" + Genre ||--o{ Track : "[Genre.GenreId] to [Track.GenreId]" + MediaType ||--o{ Track : "[MediaType.MediaTypeId] to [Track.MediaTypeId]" - customers ||--o{ invoices : "foreign key" + Invoice ||--o{ InvoiceLine : "[Invoice.InvoiceId] to [InvoiceLine.InvoiceId]" + Track ||--o{ InvoiceLine : "[Track.TrackId] to [InvoiceLine.TrackId]" - invoices ||--o{ invoice_items : "foreign key" + Playlist ||--o{ PlaylistTrack : "[Playlist.PlaylistId] to [PlaylistTrack.PlaylistId]" + Track ||--o{ PlaylistTrack : "[Track.TrackId] to [PlaylistTrack.TrackId]" - tracks ||--o{ invoice_items : "foreign key" - tracks ||--o{ playlist_track : "foreign key" ``` \ No newline at end of file From 79346dbc46440f8a9dfc6ef3b081f96df14531d0 Mon Sep 17 00:00:00 2001 From: lastlink Date: Sat, 1 Oct 2022 16:09:20 -0400 Subject: [PATCH 2/3] working table generator --- src/generate-sql-ddl.ts | 94 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 91 insertions(+), 3 deletions(-) diff --git a/src/generate-sql-ddl.ts b/src/generate-sql-ddl.ts index e887113..0349c0a 100644 --- a/src/generate-sql-ddl.ts +++ b/src/generate-sql-ddl.ts @@ -44,7 +44,8 @@ export class DbParser { if (this.entities) { for (const key in this.entities) { if (Object.prototype.hasOwnProperty.call(this.entities, key)) { - const element = this.entities[key]; + const entity = this.entities[key]; + statementGeneration.push(this.createTable(key, entity)); } } } @@ -71,11 +72,98 @@ export class DbParser { // } return statementGeneration.join(""); } + /** + * convert labels with start and end strings per database type + * @param label + * @returns + */ + private dbTypeEnds(label: string) { + let char1 = '"'; + let char2 = '"'; + if (this.dbType == "mysql") { + char1 = "`"; + char2 = "`"; + } else if (this.dbType == "sqlserver") { + char1 = "["; + char2 = "]"; + } + return `${char1}${label}${char2}`; + } + /** + * generate create table statement + * @param entityKey + * @param entity + * @returns + */ + private createTable(entityKey: string, entity: DbEntityDefinition) { + let statement = `CREATE TABLE ${this.dbTypeEnds(entityKey)} (\n`; + // TODO: incorporate foreign keys using relationships + for (let i = 0; i < entity.attributes.length; i++) { + const attribute = entity.attributes[i]; + if (attribute.attributeType && attribute.attributeName) { + // need to add parenthesis or commas + let columnType = attribute.attributeType.replace("_", ","); + let columnTypeLength = columnType.replace(/[^0-9,]/gim, ""); + columnType = ( + columnType.replace(/[^a-z]/gim, "") + + (columnTypeLength ? `(${columnTypeLength})` : "") + ).trim(); + if (attribute.attributeComment) { + // check if contains full column name + statement += `\t${this.dbTypeEnds( + attribute.attributeName + )} ${columnType} ${attribute.attributeComment}`; + } else { + statement += `\t${this.dbTypeEnds( + attribute.attributeName + )} ${columnType}`; + } + statement += i != entity.attributes.length - 1 ? ",\n" : "\n"; + } + } - private createTable(tableName: string) { - return `CREATE TABLE ${tableName}`; + statement += `\n)`; + return statement; } + /* database create table examples: + sqlite example: + CREATE TABLE [test_table] ( + "id" INTEGER NOT NULL, + "Field 2_2" TEXT, + "Artist Id" INTEGER, + PRIMARY KEY("id"), + FOREIGN KEY("Artist Id") REFERENCES "Artist"("ArtistId") ON DELETE NO ACTION ON UPDATE NO ACTION + ) + + mysql example: + CREATE TABLE `test_table` ( + `id` INTEGER NOT NULL, + `Field 2_2` TEXT, + `Artist Id` INTEGER, + PRIMARY KEY (`id`), + FOREIGN KEY (`Artist Id`) REFERENCES `Artist`(`ArtistId`) + ); + + postgres example: + CREATE TABLE "test_table" ( + "id" INTEGER NOT NULL, + "Field 2_2" TEXT, + "Artist Id" INTEGER, + PRIMARY KEY ("id"), + FOREIGN KEY ("Artist Id"") REFERENCES "Artist"("ArtistId") + ); + + sql server example: + CREATE TABLE [test_table] ( + [id] INTEGER NOT NULL, + [Field 2_2] TEXT, + [Artist Id] INTEGER, + PRIMARY KEY ([id]), + FOREIGN KEY ([Artist Id]) REFERENCES [Artist]([ArtistId]) + ); + */ + private createColumn( tableName: string, columnName: string, From 4f35555fe75b097701b786201123e34000f29356 Mon Sep 17 00:00:00 2001 From: lastlink Date: Sat, 1 Oct 2022 16:10:45 -0400 Subject: [PATCH 3/3] 0.0.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 7d249de..0d23745 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "little-mermaid-2-the-sql", - "version": "0.0.1", + "version": "0.0.2", "description": "", "main": "./lib/index.js", "engines": {