Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2019
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.34 KB | None | 0 0
  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.  
  13. package demo.persistence;
  14.  
  15. import java.sql.Connection;
  16. import java.sql.DriverManager;
  17. import java.sql.SQLException;
  18. import java.sql.Statement;
  19.  
  20. public 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. [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. [/if]
  69. [/for]
  70. [/if]
  71. [/for]
  72. [/for]
  73. + " PRIMARY KEY ( "
  74. [for (col : Column | table.columns)]
  75. [if (col.key)]
  76. + "[col.name.toLowerFirst()/] ), "
  77. [/if]
  78. [/for]
  79. ;
  80. sql[table.entity/] = sql[table.entity/].substring(0, sql[table.entity/].length()-2);
  81. sql[table.entity/] = sql[table.entity/] + ")";
  82. stmt.executeUpdate(sql[table.entity/]);
  83. [/for]
  84. stmt.close();
  85. } catch (SQLException se) {
  86. // Handle errors for JDBC
  87. se.printStackTrace();
  88. } catch (Exception e) {
  89. e.printStackTrace();
  90. } finally {
  91. // finally block used to close resources
  92. try {
  93. if (stmt != null)
  94. stmt.close();
  95. } catch (SQLException se2) {
  96. se2.printStackTrace();
  97. } // nothing we can do
  98. } // end try
  99. }
  100.  
  101. private static Connection initConnection(){
  102. try {
  103. // Register JDBC driver
  104. Class.forName(JDBC_DRIVER);
  105.  
  106. // Open a connection
  107. conn = DriverManager.getConnection(DB_URL, USER, PASS);
  108.  
  109. initDatabase();
  110.  
  111. } catch (SQLException se) {
  112. // Handle errors for JDBC
  113. se.printStackTrace();
  114. return null;
  115. } catch (Exception e) {
  116. // Handle errors for Class.forName
  117. e.printStackTrace();
  118. return null;
  119. }
  120.  
  121. return conn;
  122. }
  123.  
  124. public static Connection getConnection(){
  125. if (conn == null) {
  126. conn = initConnection();
  127. }
  128. return conn;
  129. }
  130.  
  131. public static void closeConnection() {
  132. if (conn != null) {
  133. try {
  134. conn.close();
  135. } catch (SQLException se) {
  136. // Handle errors for JDBC
  137. se.printStackTrace();
  138. } catch (Exception e) {
  139. e.printStackTrace();
  140. } finally {
  141. conn = null;
  142. }
  143. }
  144. }
  145.  
  146. }
  147.  
  148. [/file]
  149.  
  150. [for (table : Table | aDbaseModel.tables)]
  151.  
  152. [file ('persistence/'+table.entity+'Repository.java', false, 'UTF-8')]
  153.  
  154. package demo.persistence;
  155.  
  156. import java.sql.Connection;
  157. import java.sql.ResultSet;
  158. import java.sql.SQLException;
  159. import java.sql.Statement;
  160. import java.util.ArrayList;
  161. import java.util.List;
  162. import pt.isep.edom.project.c4.mm.dbase.Column;
  163. import pt.isep.edom.project.c4.mm.dbase.Table;
  164.  
  165. import demo.domain.[table.entity/];
  166.  
  167.  
  168. public class [table.entity/]Repository{
  169.  
  170. public [table.entity/] [table.entity.toLowerFirst()/]OfId (int a[table.entity/]Id) {
  171. Statement stmt = null;
  172. [table.entity/] [table.entity.toLowerFirst()/] = null;
  173. try {
  174. Connection conn=Database.getConnection();
  175.  
  176. ArrayList<Column> aux = new ArrayList<>();
  177. String sql = "SELECT "
  178. [for (col : Column | table.columns)]
  179. [if (col.key)]
  180. + "[col.name/], "
  181. [/if]
  182. [if (col.key = false)]
  183. [for (fk : Column | col.foreignKey)]
  184. + "[fk.name/], "
  185. [/for]
  186. [/if]
  187.  
  188. [/for]
  189. [for (ent : Entity | getDomainModel(aDbaseModel).entities)]
  190. [if (ent.name = table.entity)]
  191. [for (field : Field | ent.fields)]
  192. [if (field.name.contains('Id') = false)]
  193. + "[field.name/], "
  194. [/if]
  195. [/for]
  196. [/if]
  197. [/for]
  198.  
  199. ;
  200. sql = sql.substring(0, sql.length()-2);
  201. sql = sql + " FROM [table.entity.toUpper()/] WHERE [table.entity.toLowerFirst()/]Id=" + a[table.entity/]Id;
  202.  
  203. stmt = conn.createStatement();
  204.  
  205. stmt.executeQuery(sql);
  206. ResultSet res = stmt.getResultSet();
  207. if(res.next()) {
  208. [table.entity.toLowerFirst()/] = new [table.entity/]();
  209. } else {
  210.  
  211. }
  212. stmt.close();
  213. } catch (Exception e){
  214. e.printStackTrace();
  215. } finally {
  216. try {
  217. if(stmt != null)
  218. stmt.close();
  219. } catch (SQLException se2) {
  220. se2.printStackTrace();
  221. }
  222. }
  223. return [table.entity.toLowerFirst()/];
  224.  
  225.  
  226. }
  227.  
  228.  
  229.  
  230. public int remove(int [table.entity.toLowerFirst()/]Id) {
  231. Statement stmt = null;
  232. try {
  233. Connection conn=Database.getConnection();
  234.  
  235. String sql = "DELETE FROM [table.entity.toUpper()/] WHERE [table.entity.toLowerFirst()/]Id =" + [table.entity.toLowerFirst()/]Id;
  236. stmt = conn.createStatement();
  237.  
  238. stmt.execute(sql);
  239.  
  240. stmt.close();
  241. } catch (SQLException se) {
  242.  
  243. se.printStackTrace();
  244. return -1;
  245. } catch (Exception e) {
  246. e.printStackTrace();
  247. return -1;
  248. } finally {
  249.  
  250. try {
  251. if (stmt != null)
  252. stmt.close();
  253. } catch (SQLException se2) {
  254. se2.printStackTrace();
  255. return -1;
  256. }
  257. }
  258. return 0;
  259. }
  260.  
  261. public int remove([table.entity/] a[table.entity/]){
  262. return remove(a[table.entity/].get[table.entity/]Id());
  263. }
  264.  
  265. public int sqlInsert([table.entity/] a[table.entity/]){
  266. Statement stmt = null;
  267. int id = 0;
  268. try {
  269. Connection conn = Database.getConnection();
  270.  
  271. String sql = "INSERT INTO [table.entity.toUpper()/] ("
  272. [for (col : Column | table.columns)]
  273. [if (col.key)]
  274. + "'" + a[table.entity/].get[col.name/]() + "', "
  275. [/if]
  276. [if (col.key = false)]
  277. [for (fk : Column | col.foreignKey)]
  278. + "'" + a[table.entity/].get[fk.name/]()+ "', "
  279. [/for]
  280. [/if]
  281. [/for]
  282. [for (ent : Entity | getDomainModel(aDbaseModel).entities)]
  283. [if (ent.name = table.entity)]
  284. [for (field : Field | ent.fields)]
  285. [if (field.name.contains('Id') = false)]
  286. + "'" + a[table.entity/].get[field.name/]() +"', "
  287. [/if]
  288. [/for]
  289. [/if]
  290. [/for]
  291.  
  292.  
  293. ;
  294. ;
  295. sql = sql.substring(0, sql.length()-2);
  296. sql = sql + ")";
  297.  
  298. stmt.execute(sql, Statement.RETURN_GENERATED_KEYS);
  299. ResultSet generatedKeys = stmt.getGeneratedKeys();
  300. if (generatedKeys.next()) {
  301. id = generatedKeys.getInt(1);
  302. } else {
  303. // Throw exception?
  304. id=0;
  305. }
  306.  
  307. // Clean-up environment
  308. stmt.close();
  309. } catch (SQLException se) {
  310.  
  311. se.printStackTrace();
  312. } catch (Exception e) {
  313. e.printStackTrace();
  314. } finally {
  315.  
  316. try {
  317. if (stmt != null)
  318. stmt.close();
  319. } catch (SQLException se2) {
  320. se2.printStackTrace();
  321. }
  322. }
  323. return id;
  324. }
  325.  
  326. private boolean sqlUpdate([table.entity/] a[table.entity/]){
  327. Statement stmt = null;
  328. try {
  329. Connection conn=Database.getConnection();
  330.  
  331. stmt = conn.createStatement();
  332. String sql = "UPDATE [table.entity.toUpper()/] SET "
  333. [for (col : Column | table.columns)]
  334. [if (col.key)]
  335. + "[table.entity/] ='" + a[table.entity/].get[col.name/]() + "', "
  336. [/if]
  337. [if (col.key = false)]
  338. [for (fk : Column | col.foreignKey)]
  339. + "[table.entity/] ='" + a[table.entity/].get[fk.name/]()+ "', "
  340. [/for]
  341. [/if]
  342. [/for]
  343. [for (ent : Entity | getDomainModel(aDbaseModel).entities)]
  344. [if (ent.name = table.entity)]
  345. [for (field : Field | ent.fields)]
  346. [if (field.name.contains('Id') = false)]
  347. + "[table.entity/] ='" + a[table.entity/].get[field.name/]() +"', "
  348. [/if]
  349. [/for]
  350. [/if]
  351. [/for]
  352.  
  353.  
  354.  
  355. ;
  356. stmt.executeUpdate(sql);
  357.  
  358. // Clean-up environment
  359. stmt.close();
  360. } catch (SQLException se) {
  361. // Handle errors for JDBC
  362. se.printStackTrace();
  363. return false;
  364. } catch (Exception e) {
  365. e.printStackTrace();
  366. return false;
  367. } finally {
  368. // finally block used to close resources
  369. try {
  370. if (stmt != null)
  371. stmt.close();
  372. } catch (SQLException se2) {
  373. se2.printStackTrace();
  374. return false;
  375. } // nothing we can do
  376. } // end try
  377. return true;
  378. }
  379.  
  380. public [table.entity/] save([table.entity/] a[table.entity/]) {
  381. if(a[table.entity/].get[table.entity/]Id()==0){
  382. int newId = sqlInsert(a[table.entity/]);
  383. return [table.entity.toLowerFirst()/]OfId(newId);
  384. }
  385. else
  386. {
  387. sqlUpdate(a[table.entity/]);
  388. return a[table.entity/];
  389. }
  390. }
  391.  
  392. public List<[table.entity/]> all[table.entity/]s() {
  393. Statement stmt = null;
  394. [table.entity/] [table.entity.toLowerFirst()/] = null;
  395. ArrayList<[table.entity/]> [table.entity.toLowerFirst()/]List = new ArrayList<[table.entity/]>();
  396. try {
  397. Connection conn = Database.getConnection();
  398.  
  399. String sql = "SELECT "
  400. [for (col : Column | table.columns)]
  401. [if (col.key)]
  402. + "[col.name/], "
  403. [/if]
  404. [if (col.key = false)]
  405. [for (fk : Column | col.foreignKey)]
  406. + "[fk.name/], "
  407. [/for]
  408. [/if]
  409.  
  410. [/for]
  411.  
  412.  
  413. [comment MISSING FOR LOOP FOR FIELDS FROM DOMAIN MODEL /]
  414.  
  415. ;
  416. sql = sql.substring(0, sql.length()-2);
  417. sql = sql + " FROM [table.entity.toUpper()/]";
  418.  
  419. stmt = conn.createStatement();
  420. stmt.executeQuery(sql);
  421. ResultSet res = stmt.getResultSet();
  422. while (res.next()) {
  423.  
  424. [table.entity.toLowerFirst()/] = new [table.entity/](
  425. [comment MISSING /]
  426. );
  427. [table.entity.toLowerFirst()/]List.add([table.entity.toLowerFirst()/]);
  428. }
  429.  
  430.  
  431.  
  432. } catch (SQLException se) {
  433.  
  434. se.printStackTrace();
  435. } catch (Exception e) {
  436. e.printStackTrace();
  437. } finally {
  438.  
  439. try {
  440. if (stmt != null)
  441. stmt.close();
  442. } catch (SQLException se2) {
  443. se2.printStackTrace();
  444. }
  445. }
  446. return [table.entity.toLowerFirst()/]List;
  447. }
  448. }
  449. [/file]
  450.  
  451. [/for]
  452. [/template]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement