Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [comment encoding = UTF-8 /]
- [module generate('http://www.example.org/dbase', 'http://www.example.org/domain' ) ]
- [query public getDomainModel(parm: DbaseModel) : DomainModel =
- invoke('pt.isep.edom.project.c4.mtl.dbase.main.DomainModelQuery', 'getDomainModel(pt.isep.edom.project.c4.mm.dbase.DbaseModel)', Sequence{parm})
- /]
- [template public generateDbaseModel(aDbaseModel : DbaseModel)]
- [comment @main/]
- [file ('persistence/Database.java', false, 'UTF-8')]
- package demo.persistence;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class Database {
- // JDBC driver name and database URL
- static final String JDBC_DRIVER = "org.h2.Driver";
- static final String DB_URL = "jdbc:h2:~/test-gorgeous-food";
- // Database credentials
- static final String USER = "sa";
- static final String PASS = "";
- static Connection conn = null;
- private static void initDatabase() {
- Statement stmt = null;
- try {
- stmt = conn.createStatement();
- [for (table : Table | aDbaseModel.tables)]
- String sql[table.entity/] = "CREATE TABLE IF NOT EXISTS [table.entity.toUpper()/] "
- [for (col : Column | table.columns)]
- [if (col.key)]
- + "[col.name.toLowerFirst()/] INTEGER auto_increment, "
- [/if]
- [if (col.key = false)]
- [for (fk : Column | col.foreignKey)]
- [if (fk.type.toString() = 'INTEGER')]
- + "[fk.name.toLowerFirst()/] INTEGER not NULL, "
- [/if]
- [if (fk.type.toString() = 'VARCHAR')]
- + "[fk.name.toLowerFirst()/] VARCHAR(255) not NULL, "
- [/if]
- [if (fk.type.toString() = 'REAL')]
- + "[fk.name.toLowerFirst()/] DOUBLE not NULL, "
- [/if]
- [/for]
- [/if]
- [for (ent : Entity | getDomainModel(aDbaseModel).entities)]
- [if (ent.name = table.entity)]
- [for (field : Field | ent.fields)]
- [if (field.name.contains('Id') = false)]
- [if (field.type.toString() = 'INTEGER')]
- + "[field.name/] INTEGER, "
- [/if]
- [if (field.type.toString() = 'STRING')]
- + "[field.name/] VARCHAR(255), "
- [/if]
- [if (field.type.toString() = 'REAL')]
- + "[field.name/] DOUBLE, "
- [/if]
- [/if]
- [/for]
- [/if]
- [/for]
- [/for]
- + " PRIMARY KEY ( "
- [for (col : Column | table.columns)]
- [if (col.key)]
- + "[col.name.toLowerFirst()/] ), "
- [/if]
- [/for]
- ;
- sql[table.entity/] = sql[table.entity/].substring(0, sql[table.entity/].length()-2);
- sql[table.entity/] = sql[table.entity/] + ")";
- stmt.executeUpdate(sql[table.entity/]);
- [/for]
- stmt.close();
- } catch (SQLException se) {
- // Handle errors for JDBC
- se.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- // finally block used to close resources
- try {
- if (stmt != null)
- stmt.close();
- } catch (SQLException se2) {
- se2.printStackTrace();
- } // nothing we can do
- } // end try
- }
- private static Connection initConnection(){
- try {
- // Register JDBC driver
- Class.forName(JDBC_DRIVER);
- // Open a connection
- conn = DriverManager.getConnection(DB_URL, USER, PASS);
- initDatabase();
- } catch (SQLException se) {
- // Handle errors for JDBC
- se.printStackTrace();
- return null;
- } catch (Exception e) {
- // Handle errors for Class.forName
- e.printStackTrace();
- return null;
- }
- return conn;
- }
- public static Connection getConnection(){
- if (conn == null) {
- conn = initConnection();
- }
- return conn;
- }
- public static void closeConnection() {
- if (conn != null) {
- try {
- conn.close();
- } catch (SQLException se) {
- // Handle errors for JDBC
- se.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- conn = null;
- }
- }
- }
- }
- [/file]
- [for (table : Table | aDbaseModel.tables)]
- [file ('persistence/'+table.entity+'Repository.java', false, 'UTF-8')]
- package demo.persistence;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.List;
- import pt.isep.edom.project.c4.mm.dbase.Column;
- import pt.isep.edom.project.c4.mm.dbase.Table;
- import demo.domain.[table.entity/];
- public class [table.entity/]Repository{
- public [table.entity/] [table.entity.toLowerFirst()/]OfId (int a[table.entity/]Id) {
- Statement stmt = null;
- [table.entity/] [table.entity.toLowerFirst()/] = null;
- try {
- Connection conn=Database.getConnection();
- ArrayList<Column> aux = new ArrayList<>();
- String sql = "SELECT "
- [for (col : Column | table.columns)]
- [if (col.key)]
- + "[col.name/], "
- [/if]
- [if (col.key = false)]
- [for (fk : Column | col.foreignKey)]
- + "[fk.name/], "
- [/for]
- [/if]
- [/for]
- [for (ent : Entity | getDomainModel(aDbaseModel).entities)]
- [if (ent.name = table.entity)]
- [for (field : Field | ent.fields)]
- [if (field.name.contains('Id') = false)]
- + "[field.name/], "
- [/if]
- [/for]
- [/if]
- [/for]
- ;
- sql = sql.substring(0, sql.length()-2);
- sql = sql + " FROM [table.entity.toUpper()/] WHERE [table.entity.toLowerFirst()/]Id=" + a[table.entity/]Id;
- stmt = conn.createStatement();
- stmt.executeQuery(sql);
- ResultSet res = stmt.getResultSet();
- if(res.next()) {
- [table.entity.toLowerFirst()/] = new [table.entity/]();
- } else {
- }
- stmt.close();
- } catch (Exception e){
- e.printStackTrace();
- } finally {
- try {
- if(stmt != null)
- stmt.close();
- } catch (SQLException se2) {
- se2.printStackTrace();
- }
- }
- return [table.entity.toLowerFirst()/];
- }
- public int remove(int [table.entity.toLowerFirst()/]Id) {
- Statement stmt = null;
- try {
- Connection conn=Database.getConnection();
- String sql = "DELETE FROM [table.entity.toUpper()/] WHERE [table.entity.toLowerFirst()/]Id =" + [table.entity.toLowerFirst()/]Id;
- stmt = conn.createStatement();
- stmt.execute(sql);
- stmt.close();
- } catch (SQLException se) {
- se.printStackTrace();
- return -1;
- } catch (Exception e) {
- e.printStackTrace();
- return -1;
- } finally {
- try {
- if (stmt != null)
- stmt.close();
- } catch (SQLException se2) {
- se2.printStackTrace();
- return -1;
- }
- }
- return 0;
- }
- public int remove([table.entity/] a[table.entity/]){
- return remove(a[table.entity/].get[table.entity/]Id());
- }
- public int sqlInsert([table.entity/] a[table.entity/]){
- Statement stmt = null;
- int id = 0;
- try {
- Connection conn = Database.getConnection();
- String sql = "INSERT INTO [table.entity.toUpper()/] ("
- [for (col : Column | table.columns)]
- [if (col.key)]
- + "'" + a[table.entity/].get[col.name/]() + "', "
- [/if]
- [if (col.key = false)]
- [for (fk : Column | col.foreignKey)]
- + "'" + a[table.entity/].get[fk.name/]()+ "', "
- [/for]
- [/if]
- [/for]
- [for (ent : Entity | getDomainModel(aDbaseModel).entities)]
- [if (ent.name = table.entity)]
- [for (field : Field | ent.fields)]
- [if (field.name.contains('Id') = false)]
- + "'" + a[table.entity/].get[field.name/]() +"', "
- [/if]
- [/for]
- [/if]
- [/for]
- ;
- ;
- sql = sql.substring(0, sql.length()-2);
- sql = sql + ")";
- stmt.execute(sql, Statement.RETURN_GENERATED_KEYS);
- ResultSet generatedKeys = stmt.getGeneratedKeys();
- if (generatedKeys.next()) {
- id = generatedKeys.getInt(1);
- } else {
- // Throw exception?
- id=0;
- }
- // Clean-up environment
- stmt.close();
- } catch (SQLException se) {
- se.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (stmt != null)
- stmt.close();
- } catch (SQLException se2) {
- se2.printStackTrace();
- }
- }
- return id;
- }
- private boolean sqlUpdate([table.entity/] a[table.entity/]){
- Statement stmt = null;
- try {
- Connection conn=Database.getConnection();
- stmt = conn.createStatement();
- String sql = "UPDATE [table.entity.toUpper()/] SET "
- [for (col : Column | table.columns)]
- [if (col.key)]
- + "[table.entity/] ='" + a[table.entity/].get[col.name/]() + "', "
- [/if]
- [if (col.key = false)]
- [for (fk : Column | col.foreignKey)]
- + "[table.entity/] ='" + a[table.entity/].get[fk.name/]()+ "', "
- [/for]
- [/if]
- [/for]
- [for (ent : Entity | getDomainModel(aDbaseModel).entities)]
- [if (ent.name = table.entity)]
- [for (field : Field | ent.fields)]
- [if (field.name.contains('Id') = false)]
- + "[table.entity/] ='" + a[table.entity/].get[field.name/]() +"', "
- [/if]
- [/for]
- [/if]
- [/for]
- ;
- stmt.executeUpdate(sql);
- // Clean-up environment
- stmt.close();
- } catch (SQLException se) {
- // Handle errors for JDBC
- se.printStackTrace();
- return false;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- } finally {
- // finally block used to close resources
- try {
- if (stmt != null)
- stmt.close();
- } catch (SQLException se2) {
- se2.printStackTrace();
- return false;
- } // nothing we can do
- } // end try
- return true;
- }
- public [table.entity/] save([table.entity/] a[table.entity/]) {
- if(a[table.entity/].get[table.entity/]Id()==0){
- int newId = sqlInsert(a[table.entity/]);
- return [table.entity.toLowerFirst()/]OfId(newId);
- }
- else
- {
- sqlUpdate(a[table.entity/]);
- return a[table.entity/];
- }
- }
- public List<[table.entity/]> all[table.entity/]s() {
- Statement stmt = null;
- [table.entity/] [table.entity.toLowerFirst()/] = null;
- ArrayList<[table.entity/]> [table.entity.toLowerFirst()/]List = new ArrayList<[table.entity/]>();
- try {
- Connection conn = Database.getConnection();
- String sql = "SELECT "
- [for (col : Column | table.columns)]
- [if (col.key)]
- + "[col.name/], "
- [/if]
- [if (col.key = false)]
- [for (fk : Column | col.foreignKey)]
- + "[fk.name/], "
- [/for]
- [/if]
- [/for]
- [comment MISSING FOR LOOP FOR FIELDS FROM DOMAIN MODEL /]
- ;
- sql = sql.substring(0, sql.length()-2);
- sql = sql + " FROM [table.entity.toUpper()/]";
- stmt = conn.createStatement();
- stmt.executeQuery(sql);
- ResultSet res = stmt.getResultSet();
- while (res.next()) {
- [table.entity.toLowerFirst()/] = new [table.entity/](
- [comment MISSING /]
- );
- [table.entity.toLowerFirst()/]List.add([table.entity.toLowerFirst()/]);
- }
- } catch (SQLException se) {
- se.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (stmt != null)
- stmt.close();
- } catch (SQLException se2) {
- se2.printStackTrace();
- }
- }
- return [table.entity.toLowerFirst()/]List;
- }
- }
- [/file]
- [/for]
- [/template]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement