@@ -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