1 package org.splat.kernel;
4 * @author Daniel Brunier-Coulin
5 * @copyright OPEN CASCADE 2012
8 import java.sql.Connection;
9 import java.sql.SQLException;
10 import java.sql.Statement;
11 import org.hibernate.Session;
12 import org.hibernate.SessionFactory;
13 import org.hibernate.jdbc.Work;
14 import org.apache.log4j.Logger;
17 public abstract class Database {
19 // private static String CONFIG_FILE = "/hibernate.cfg.xml";
20 private static SessionFactory mySessionFactory = null;
21 protected static IDPool myIDpool = null;
23 protected class CreateTables implements Work {
24 // -----------------------------------------------
25 protected Statement request;
27 public void execute(Connection connex) throws SQLException
29 request = connex.createStatement();
31 // Last identifier of Any objects
32 String create = "CREATE TABLE `any` (" +
33 "`rid` int(10) UNSIGNED NOT NULL," +
34 "`version` tinytext NOT NULL," +
35 "PRIMARY KEY (`rid`)" +
36 ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
37 request.execute(create);
39 // Relation from entities
40 create = "CREATE TABLE `relation` (" +
41 "`rid` int(10) UNSIGNED NOT NULL," +
42 "`name` tinytext NOT NULL," +
43 "`owner` int(10) NOT NULL," +
44 "`refer` int(10) NOT NULL," +
45 "PRIMARY KEY (`rid`)" +
46 ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
47 request.execute(create);
50 create = "CREATE TABLE `attribute` (" +
51 "`rid` int(10) UNSIGNED NOT NULL auto_increment," +
52 "`type` tinytext NOT NULL," +
53 "`owner` int(10) NOT NULL," +
54 "`value` int(10) NOT NULL," +
55 "PRIMARY KEY (`rid`)" +
56 ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
57 request.execute(create);
59 // Java String objects
60 create = "CREATE TABLE `text` (" +
61 "`rid` int(10) UNSIGNED NOT NULL auto_increment," +
62 "`value` longtext NOT NULL," +
63 "PRIMARY KEY (`rid`)" +
64 ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
65 request.execute(create);
67 // User and role objects
68 create = "CREATE TABLE `user` (" +
69 "`rid` int(10) UNSIGNED NOT NULL auto_increment," +
70 "`username` varchar(32) NOT NULL," +
71 "`password` varchar(32)," +
72 "`first` tinytext NOT NULL," +
73 "`last` tinytext NOT NULL," +
74 "`display` tinytext," +
76 "`organid` tinytext," +
77 "PRIMARY KEY (`rid`)" +
78 ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
79 request.execute(create);
81 create = "CREATE TABLE `role` (" +
82 "`username` varchar(32) NOT NULL," +
83 "`role` varchar(32) NOT NULL," +
84 "PRIMARY KEY (`username`)" +
85 ") ENGINE=MyISAM DEFAULT CHARSET=latin1";
86 request.execute(create);
90 protected final static Logger logger = Logger.getLogger(Database.class);
92 // ==============================================================================================================================
94 // ==============================================================================================================================
96 public static Session getSession () {
97 // -----------------------------------
98 return getInstance().getCurrentSession();
101 public static void close () {
102 // ---------------------------
103 if (mySessionFactory != null) mySessionFactory.close();
104 mySessionFactory = null;
107 public static IDPool getIDPool () {
108 // ---------------------------------
109 if (myIDpool == null) myIDpool = new IDPool(getSession());
113 // ==============================================================================================================================
114 // Protected services
115 // ==============================================================================================================================
117 protected String getSchemaVersion () {
118 // ------------------------------------
119 return getIDPool().getSchemaVersion();
122 protected void setIDPoolSize (int size) {
123 // ---------------------------------------
124 IDPool.setPoolSize(size);
127 protected void setSchemaVersion (String version) {
128 // ------------------------------------------------
129 myIDpool = new IDPool(version);
130 getSession().save(myIDpool);
133 // ==============================================================================================================================
135 // ==============================================================================================================================
137 private static SessionFactory getInstance () {
138 // --------------------------------------------
139 if (mySessionFactory == null) {
140 org.hibernate.cfg.Configuration cfg = new org.hibernate.cfg.Configuration();
142 cfg.configure(); // The configuration file (hibernate.cfg.xml)) is supposed to be on the classpath
143 mySessionFactory = cfg.buildSessionFactory();
145 catch (Exception error) {
146 logger.fatal("Could not initialize the Hibernate configuration, reason:", error);
149 return mySessionFactory;