X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2Fdatabase%2FDatabaseConnection.java;h=5d987191a248452717c61e312f77d28fceaa6b95;hp=21d701cb4bc4048589fcd845040e5660a405912d;hb=ff0add5c210e30565099394d8d6b4c3f5595c6c6;hpb=d895448cb685adc4c2bfac8d92759252d2ce8c36 diff --git a/src/org/cacert/gigi/database/DatabaseConnection.java b/src/org/cacert/gigi/database/DatabaseConnection.java index 21d701cb..5d987191 100644 --- a/src/org/cacert/gigi/database/DatabaseConnection.java +++ b/src/org/cacert/gigi/database/DatabaseConnection.java @@ -1,5 +1,7 @@ package org.cacert.gigi.database; +import java.io.IOException; +import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; @@ -9,13 +11,17 @@ import java.sql.Statement; import java.util.HashMap; import java.util.Properties; +import org.cacert.gigi.database.SQLFileManager.ImportType; + public class DatabaseConnection { + public static final int CURRENT_SCHEMA_VERSION = 1; + public static final int CONNECTION_TIMEOUT = 24 * 60 * 60; private Connection c; - private HashMap statements = new HashMap(); + private HashMap statements = new HashMap(); private static Properties credentials; @@ -34,21 +40,28 @@ public class DatabaseConnection { private void tryConnect() { try { c = DriverManager.getConnection(credentials.getProperty("sql.url") + "?zeroDateTimeBehavior=convertToNull", credentials.getProperty("sql.user"), credentials.getProperty("sql.password")); - PreparedStatement ps = c.prepareStatement("SET SESSION wait_timeout=?;"); - ps.setInt(1, CONNECTION_TIMEOUT); - ps.execute(); - ps.close(); - adHoc = c.createStatement(); + PreparedStatement ps = c.prepareStatement("SET SESSION wait_timeout=?, time_zone='+0:00';"); + try { + ps.setInt(1, CONNECTION_TIMEOUT); + ps.execute(); + adHoc = c.createStatement(); + } finally { + ps.close(); + } } catch (SQLException e) { e.printStackTrace(); } } - public PreparedStatement prepare(String query) throws SQLException { + public GigiPreparedStatement prepare(String query) { ensureOpen(); - PreparedStatement statement = statements.get(query); + GigiPreparedStatement statement = statements.get(query); if (statement == null) { - statement = c.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); + try { + statement = new GigiPreparedStatement(c.prepareStatement(query, Statement.RETURN_GENERATED_KEYS)); + } catch (SQLException e) { + throw new Error(e); + } statements.put(query, statement); } return statement; @@ -71,14 +84,6 @@ public class DatabaseConnection { lastAction = System.currentTimeMillis(); } - public static int lastInsertId(PreparedStatement query) throws SQLException { - ResultSet rs = query.getGeneratedKeys(); - rs.next(); - int id = rs.getInt(1); - rs.close(); - return id; - } - private static ThreadLocal instances = new ThreadLocal() { @Override @@ -100,6 +105,45 @@ public class DatabaseConnection { throw new Error("Re-initiaizing is forbidden."); } credentials = conf; + GigiResultSet rs = getInstance().prepare("SELECT version FROM schemeVersion ORDER BY version DESC LIMIT 1").executeQuery(); + int version = 0; + if (rs.next()) { + version = rs.getInt(1); + } + if (version == CURRENT_SCHEMA_VERSION) { + return; // Good to go + } + if (version > CURRENT_SCHEMA_VERSION) { + throw new Error("Invalid database version. Please fix this."); + } + upgrade(version); + } + + private static void upgrade(int version) { + try { + Statement s = getInstance().c.createStatement(); + try { + while (version < CURRENT_SCHEMA_VERSION) { + try (InputStream resourceAsStream = DatabaseConnection.class.getResourceAsStream("upgrade/from_" + version + ".sql")) { + if (resourceAsStream == null) { + throw new Error("Upgrade script from version " + version + " was not found."); + } + SQLFileManager.addFile(s, resourceAsStream, ImportType.PRODUCTION); + } + version++; + } + s.addBatch("UPDATE schemeVersion SET version='" + version + "'"); + System.out.println("UPGRADING Database to version " + version); + s.executeBatch(); + System.out.println("done."); + } finally { + s.close(); + } + } catch (SQLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } } public void beginTransaction() throws SQLException {