· 6 years ago · Dec 02, 2019, 10:44 PM
1[comment encoding = UTF-8 /]
2[module generate('http://www.example.org/dbase', 'http://www.example.org/domain' ) ]
3
4[query public getDomainModel(parm: DbaseModel) : DomainModel =
5 invoke('pt.isep.edom.project.c4.mtl.dbase.main.DomainModelQuery', 'getDomainModel(pt.isep.edom.project.c4.mm.dbase.DbaseModel)', Sequence{parm})
6/]
7
8[template public generateDbaseModel(aDbaseModel : DbaseModel)]
9[comment @main/]
10
11[file ('persistence/Database.java', false, 'UTF-8')]
12
13package demo.persistence;
14
15import java.sql.Connection;
16import java.sql.DriverManager;
17import java.sql.SQLException;
18import java.sql.Statement;
19
20public class Database {
21
22 // JDBC driver name and database URL
23 static final String JDBC_DRIVER = "org.h2.Driver";
24 static final String DB_URL = "jdbc:h2:~/test-gorgeous-food";
25
26 // Database credentials
27 static final String USER = "sa";
28 static final String PASS = "";
29
30 static Connection conn = null;
31
32 private static void initDatabase() {
33 Statement stmt = null;
34 try {
35 stmt = conn.createStatement();
36 [for (table : Table | aDbaseModel.tables)]
37 String sql[table.entity/] = "CREATE TABLE IF NOT EXISTS [table.entity.toUpper()/] "
38 [for (col : Column | table.columns)]
39 [if (col.key)]
40 + "[col.name.toLowerFirst()/] INTEGER auto_increment, "
41 [/if]
42 [if (col.key = false)]
43 [for (fk : Column | col.foreignKey)]
44 [if (fk.type.toString() = 'INTEGER')]
45 + "[fk.name.toLowerFirst()/] INTEGER not NULL, "
46 [/if]
47 [if (fk.type.toString() = 'VARCHAR')]
48 + "[fk.name.toLowerFirst()/] VARCHAR(255) not NULL, "
49 [/if]
50 [if (fk.type.toString() = 'REAL')]
51 + "[fk.name.toLowerFirst()/] DOUBLE not NULL, "
52 [/if]
53 [/for]
54 [/if]
55[comment] [for (ent : Entity | getDomainModel(aDbaseModel).entities)]
56 [if (ent.name = table.entity)]
57 [for (field : Field | ent.fields)]
58 [if (field.name.contains('Id') = false)]
59 [if (field.type.toString() = 'INTEGER')]
60 + "[field.name/] INTEGER, "
61 [/if]
62 [if (field.type.toString() = 'STRING')]
63 + "[field.name/] VARCHAR(255), "
64 [/if]
65 [if (field.type.toString() = 'REAL')]
66 + "[field.name/] DOUBLE, "
67 [/if]
68 [/for]
69 [/if]
70 [/for][/comment]
71 [/for]
72 + " PRIMARY KEY ( "
73 [for (col : Column | table.columns)]
74 [if (col.key)]
75 + "[col.name.toLowerFirst()/] ), "
76 [/if]
77 [/for]
78 ;
79 sql[table.entity/] = sql[table.entity/].substring(0, sql[table.entity/].length()-2);
80 sql[table.entity/] = sql[table.entity/] + ")";
81 stmt.executeUpdate(sql[table.entity/]);
82 [/for]
83 stmt.close();
84 } catch (SQLException se) {
85 // Handle errors for JDBC
86 se.printStackTrace();
87 } catch (Exception e) {
88 e.printStackTrace();
89 } finally {
90 // finally block used to close resources
91 try {
92 if (stmt != null)
93 stmt.close();
94 } catch (SQLException se2) {
95 se2.printStackTrace();
96 } // nothing we can do
97 } // end try
98 }
99
100 private static Connection initConnection(){
101 try {
102 // Register JDBC driver
103 Class.forName(JDBC_DRIVER);
104
105 // Open a connection
106 conn = DriverManager.getConnection(DB_URL, USER, PASS);
107
108 initDatabase();
109
110 } catch (SQLException se) {
111 // Handle errors for JDBC
112 se.printStackTrace();
113 return null;
114 } catch (Exception e) {
115 // Handle errors for Class.forName
116 e.printStackTrace();
117 return null;
118 }
119
120 return conn;
121 }
122
123 public static Connection getConnection(){
124 if (conn == null) {
125 conn = initConnection();
126 }
127 return conn;
128 }
129
130 public static void closeConnection() {
131 if (conn != null) {
132 try {
133 conn.close();
134 } catch (SQLException se) {
135 // Handle errors for JDBC
136 se.printStackTrace();
137 } catch (Exception e) {
138 e.printStackTrace();
139 } finally {
140 conn = null;
141 }
142 }
143 }
144
145}
146
147[/file]
148
149[for (table : Table | aDbaseModel.tables)]
150
151[file ('persistence/'+table.entity+'Repository.java', false, 'UTF-8')]
152
153package demo.persistence;
154
155import java.sql.Connection;
156import java.sql.ResultSet;
157import java.sql.SQLException;
158import java.sql.Statement;
159import java.util.ArrayList;
160import java.util.List;
161import pt.isep.edom.project.c4.mm.dbase.Column;
162import pt.isep.edom.project.c4.mm.dbase.Table;
163
164import demo.domain.[table.entity/];
165
166
167public class [table.entity/]Repository{
168
169 public [table.entity/] [table.entity.toLowerFirst()/]OfId (int a[table.entity/]Id) {
170 Statement stmt = null;
171 [table.entity/] [table.entity.toLowerFirst()/] = null;
172 try {
173 Connection conn=Database.getConnection();
174
175 ArrayList<Column> aux = new ArrayList<>();
176 String sql = "SELECT "
177 [for (col : Column | table.columns)]
178 [if (col.key)]
179 + "[col.name/], "
180 [/if]
181 [if (col.key = false)]
182 [for (fk : Column | col.foreignKey)]
183 + "[fk.name/], "
184 [/for]
185 [/if]
186
187 [/for]
188[comment] [for (ent : Entity | getDomainModel(aDbaseModel).entities)]
189 [if (ent.name = table.entity)]
190 [for (field : Field | ent.fields)]
191 [if (field.name.contains('Id') = false)]
192 + "[field.name/], "
193 [/if]
194 [/for]
195 [/if]
196 [/for][/comment]
197
198 [comment MISSING FOR LOOP FOR FIELDS FROM DOMAIN MODEL /]
199
200 ;
201 sql = sql.substring(0, sql.length()-2);
202 sql = sql + " FROM [table.entity.toUpper()/] WHERE [table.entity.toLowerFirst()/]Id=" + a[table.entity/]Id;
203
204 stmt = conn.createStatement();
205
206 stmt.executeQuery(sql);
207 ResultSet res = stmt.getResultSet();
208 if(res.next()) {
209 [table.entity.toLowerFirst()/] = new [table.entity/]();
210 } else {
211
212 }
213 stmt.close();
214 } catch (Exception e){
215 e.printStackTrace();
216 } finally {
217 try {
218 if(stmt != null)
219 stmt.close();
220 } catch (SQLException se2) {
221 se2.printStackTrace();
222 }
223 }
224 return [table.entity.toLowerFirst()/];
225
226
227 }
228
229
230
231 public int remove(int [table.entity.toLowerFirst()/]Id) {
232 Statement stmt = null;
233 try {
234 Connection conn=Database.getConnection();
235
236 String sql = "DELETE FROM [table.entity.toUpper()/] WHERE [table.entity.toLowerFirst()/]Id =" + [table.entity.toLowerFirst()/]Id;
237 stmt = conn.createStatement();
238
239 stmt.execute(sql);
240
241 stmt.close();
242 } catch (SQLException se) {
243
244 se.printStackTrace();
245 return -1;
246 } catch (Exception e) {
247 e.printStackTrace();
248 return -1;
249 } finally {
250
251 try {
252 if (stmt != null)
253 stmt.close();
254 } catch (SQLException se2) {
255 se2.printStackTrace();
256 return -1;
257 }
258 }
259 return 0;
260 }
261
262 public int remove([table.entity/] a[table.entity/]){
263 return remove(a[table.entity/].get[table.entity/]Id());
264 }
265
266 public int sqlInsert([table.entity/] a[table.entity/]){
267 Statement stmt = null;
268 int id = 0;
269 try {
270 Connection conn = Database.getConnection();
271
272 String sql = "INSERT INTO [table.entity.toUpper()/] ("
273 [for (col : Column | table.columns)]
274 [if (col.key)]
275 + "'" + a[table.entity/].get[col.name/]() + "', "
276 [/if]
277 [if (col.key = false)]
278 [for (fk : Column | col.foreignKey)]
279 + "'" + a[table.entity/].get[fk.name/]()+ "', "
280 [/for]
281 [/if]
282 [/for]
283[comment] [for (ent : Entity | getDomainModel(aDbaseModel).entities)]
284 [if (ent.name = table.entity)]
285 [for (field : Field | ent.fields)]
286 [if (field.name.contains('Id') = false)]
287 + "'" + a[table.entity/].get[field.name/]() +"', "
288 [/if]
289 [/for]
290 [/if]
291 [/for][/comment]
292[comment MISSING FOR LOOP FOR FIELDS FROM DOMAIN MODEL /]
293
294 ;
295 ;
296 sql = sql.substring(0, sql.length()-2);
297 sql = sql + ")";
298
299 stmt.execute(sql, Statement.RETURN_GENERATED_KEYS);
300 ResultSet generatedKeys = stmt.getGeneratedKeys();
301 if (generatedKeys.next()) {
302 id = generatedKeys.getInt(1);
303 } else {
304 // Throw exception?
305 id=0;
306 }
307
308 // Clean-up environment
309 stmt.close();
310 } catch (SQLException se) {
311
312 se.printStackTrace();
313 } catch (Exception e) {
314 e.printStackTrace();
315 } finally {
316
317 try {
318 if (stmt != null)
319 stmt.close();
320 } catch (SQLException se2) {
321 se2.printStackTrace();
322 }
323 }
324 return id;
325 }
326
327 private boolean sqlUpdate([table.entity/] a[table.entity/]){
328 Statement stmt = null;
329 try {
330 Connection conn=Database.getConnection();
331
332 stmt = conn.createStatement();
333 String sql = "UPDATE [table.entity.toUpper()/] SET "
334 [for (col : Column | table.columns)]
335 [if (col.key)]
336 + "[table.entity/] ='" + a[table.entity/].get[col.name/]() + "', "
337 [/if]
338 [if (col.key = false)]
339 [for (fk : Column | col.foreignKey)]
340 + "[table.entity/] ='" + a[table.entity/].get[fk.name/]()+ "', "
341 [/for]
342 [/if]
343 [/for]
344[comment MISSING FOR LOOP FOR FIELDS FROM DOMAIN MODEL /]
345
346 ;
347 ;
348 stmt.executeUpdate(sql);
349
350 // Clean-up environment
351 stmt.close();
352 } catch (SQLException se) {
353 // Handle errors for JDBC
354 se.printStackTrace();
355 return false;
356 } catch (Exception e) {
357 e.printStackTrace();
358 return false;
359 } finally {
360 // finally block used to close resources
361 try {
362 if (stmt != null)
363 stmt.close();
364 } catch (SQLException se2) {
365 se2.printStackTrace();
366 return false;
367 } // nothing we can do
368 } // end try
369 return true;
370 }
371
372 public [table.entity/] save([table.entity/] a[table.entity/]) {
373 if(a[table.entity/].get[table.entity/]Id()==0){
374 int newId = sqlInsert(a[table.entity/]);
375 return [table.entity.toLowerFirst()/]OfId(newId);
376 }
377 else
378 {
379 sqlUpdate(a[table.entity/]);
380 return a[table.entity/];
381 }
382 }
383
384 public List<[table.entity/]> all[table.entity/]s() {
385 Statement stmt = null;
386 [table.entity/] [table.entity.toLowerFirst()/] = null;
387 ArrayList<[table.entity/]> [table.entity.toLowerFirst()/]List = new ArrayList<[table.entity/]>();
388 try {
389 Connection conn = Database.getConnection();
390
391 String sql = "SELECT "
392 [for (col : Column | table.columns)]
393 [if (col.key)]
394 + "[col.name/], "
395 [/if]
396 [if (col.key = false)]
397 [for (fk : Column | col.foreignKey)]
398 + "[fk.name/], "
399 [/for]
400 [/if]
401
402 [/for]
403
404
405 [comment MISSING FOR LOOP FOR FIELDS FROM DOMAIN MODEL /]
406
407 ;
408 sql = sql.substring(0, sql.length()-2);
409 sql = sql + " FROM [table.entity.toUpper()/]";
410
411 stmt = conn.createStatement();
412 stmt.executeQuery(sql);
413 ResultSet res = stmt.getResultSet();
414 while (res.next()) {
415
416 [table.entity.toLowerFirst()/] = new [table.entity/](
417 [comment MISSING /]
418 );
419 [table.entity.toLowerFirst()/]List.add([table.entity.toLowerFirst()/]);
420 }
421
422
423
424 } catch (SQLException se) {
425
426 se.printStackTrace();
427 } catch (Exception e) {
428 e.printStackTrace();
429 } finally {
430
431 try {
432 if (stmt != null)
433 stmt.close();
434 } catch (SQLException se2) {
435 se2.printStackTrace();
436 }
437 }
438 return [table.entity.toLowerFirst()/]List;
439 }
440}
441[/file]
442
443[/for]
444[/template]