1 package org.cacert.gigi.database;
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.PrintWriter;
6 import java.sql.Connection;
7 import java.sql.DriverManager;
8 import java.sql.PreparedStatement;
9 import java.sql.ResultSet;
10 import java.sql.SQLException;
11 import java.sql.Statement;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.Map.Entry;
15 import java.util.Properties;
16 import java.util.StringJoiner;
17 import java.util.concurrent.LinkedBlockingDeque;
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
21 import org.cacert.gigi.database.SQLFileManager.ImportType;
23 public class DatabaseConnection {
25 public static class Link implements AutoCloseable {
27 private DatabaseConnection target;
29 protected Link(DatabaseConnection target) {
35 synchronized (DatabaseConnection.class) {
36 Link i = instances.get(Thread.currentThread());
40 instances.remove(Thread.currentThread());
47 public static final int MAX_CACHED_INSTANCES = 3;
49 private static class StatementDescriptor {
57 PreparedStatement target;
59 public StatementDescriptor(String query, boolean scrollable) {
61 this.scrollable = scrollable;
65 public synchronized void instanciate(Connection c) throws SQLException {
67 target = c.prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
69 target = c.prepareStatement(query, query.startsWith("SELECT ") ? Statement.NO_GENERATED_KEYS : Statement.RETURN_GENERATED_KEYS);
74 public synchronized PreparedStatement getTarget() {
78 public synchronized void increase() {
80 throw new IllegalStateException();
86 public int hashCode() {
89 result = prime * result + instance;
90 result = prime * result + ((query == null) ? 0 : query.hashCode());
91 result = prime * result + (scrollable ? 1231 : 1237);
96 public boolean equals(Object obj) {
103 if (getClass() != obj.getClass()) {
106 StatementDescriptor other = (StatementDescriptor) obj;
107 if (instance != other.instance) {
111 if (other.query != null) {
114 } else if ( !query.equals(other.query)) {
117 if (scrollable != other.scrollable) {
125 public static final int CURRENT_SCHEMA_VERSION = 20;
127 public static final int CONNECTION_TIMEOUT = 24 * 60 * 60;
129 private Connection c;
131 private HashMap<StatementDescriptor, PreparedStatement> statements = new HashMap<StatementDescriptor, PreparedStatement>();
133 private HashSet<PreparedStatement> underUse = new HashSet<>();
135 private static Properties credentials;
137 private Statement adHoc;
139 public DatabaseConnection() {
141 Class.forName(credentials.getProperty("sql.driver"));
142 } catch (ClassNotFoundException e) {
149 private void tryConnect() {
151 c = DriverManager.getConnection(credentials.getProperty("sql.url") + "?socketTimeout=" + CONNECTION_TIMEOUT, credentials.getProperty("sql.user"), credentials.getProperty("sql.password"));
152 adHoc = c.createStatement();
153 } catch (SQLException e) {
158 protected synchronized PreparedStatement prepareInternal(String query) throws SQLException {
159 return prepareInternal(query, false);
162 protected synchronized PreparedStatement prepareInternal(String query, boolean scrollable) throws SQLException {
165 query = preprocessQuery(query);
166 StatementDescriptor searchHead = new StatementDescriptor(query, scrollable);
167 PreparedStatement statement = null;
168 while (statement == null) {
169 statement = statements.get(searchHead);
170 if (statement == null) {
171 searchHead.instanciate(c);
172 statement = searchHead.getTarget();
173 if (searchHead.instance >= MAX_CACHED_INSTANCES) {
176 underUse.add(statement);
177 statements.put(searchHead, statement);
178 } else if (underUse.contains(statement)) {
179 searchHead.increase();
182 underUse.add(statement);
188 protected synchronized PreparedStatement prepareInternalScrollable(String query) throws SQLException {
189 return prepareInternal(query, true);
192 private long lastAction = System.currentTimeMillis();
194 private void ensureOpen() {
195 if (System.currentTimeMillis() - lastAction > CONNECTION_TIMEOUT * 1000L) {
197 ResultSet rs = adHoc.executeQuery("SELECT 1");
199 lastAction = System.currentTimeMillis();
201 } catch (SQLException e) {
206 lastAction = System.currentTimeMillis();
209 private static HashMap<Thread, Link> instances = new HashMap<>();
211 private static LinkedBlockingDeque<DatabaseConnection> pool = new LinkedBlockingDeque<>();
213 private static int connCount = 0;
215 public static synchronized DatabaseConnection getInstance() {
216 Link l = instances.get(Thread.currentThread());
218 throw new Error("No database connection allocated");
223 public static synchronized boolean hasInstance() {
224 Link l = instances.get(Thread.currentThread());
228 public static boolean isInited() {
229 return credentials != null;
232 public static void init(Properties conf) {
233 if (credentials != null) {
234 throw new Error("Re-initiaizing is forbidden.");
237 try (Link i = newLink(false)) {
239 try (GigiPreparedStatement gigiPreparedStatement = new GigiPreparedStatement("SELECT version FROM \"schemeVersion\" ORDER BY version DESC LIMIT 1;")) {
240 GigiResultSet rs = gigiPreparedStatement.executeQuery();
242 version = rs.getInt(1);
245 if (version == CURRENT_SCHEMA_VERSION) {
246 return; // Good to go
248 if (version > CURRENT_SCHEMA_VERSION) {
249 throw new Error("Invalid database version. Please fix this.");
252 } catch (InterruptedException e) {
257 private static void upgrade(int version) {
259 Statement s = getInstance().c.createStatement();
261 while (version < CURRENT_SCHEMA_VERSION) {
262 addUpgradeScript(Integer.toString(version), s);
265 s.addBatch("UPDATE \"schemeVersion\" SET version='" + version + "'");
266 System.out.println("UPGRADING Database to version " + version);
268 System.out.println("done.");
272 } catch (SQLException e) {
274 } catch (IOException e) {
279 private static void addUpgradeScript(String version, Statement s) throws Error, IOException, SQLException {
280 try (InputStream resourceAsStream = DatabaseConnection.class.getResourceAsStream("upgrade/from_" + version + ".sql")) {
281 if (resourceAsStream == null) {
282 throw new Error("Upgrade script from version " + version + " was not found.");
284 SQLFileManager.addFile(s, resourceAsStream, ImportType.PRODUCTION);
288 public static final String preprocessQuery(String originalQuery) {
289 originalQuery = originalQuery.replace('`', '"');
290 if (originalQuery.matches("^INSERT INTO [^ ]+ SET .*")) {
291 Pattern p = Pattern.compile("INSERT INTO ([^ ]+) SET (.*)");
292 Matcher m = p.matcher(originalQuery);
294 String replacement = "INSERT INTO " + toIdentifier(m.group(1));
295 String[] parts = m.group(2).split(",");
296 StringJoiner columns = new StringJoiner(", ");
297 StringJoiner values = new StringJoiner(", ");
298 for (int i = 0; i < parts.length; i++) {
299 String[] split = parts[i].split("=", 2);
300 columns.add(toIdentifier(split[0]));
301 values.add(split[1]);
303 replacement += "(" + columns.toString() + ") VALUES(" + values.toString() + ")";
309 return originalQuery;
312 private static CharSequence toIdentifier(String ident) {
313 ident = ident.trim();
314 if ( !ident.startsWith("\"")) {
315 ident = "\"" + ident;
317 if ( !ident.endsWith("\"")) {
318 ident = ident + "\"";
323 protected synchronized void returnStatement(PreparedStatement target) throws SQLException {
324 if ( !underUse.remove(target)) {
329 public synchronized int getNumberOfLockedStatements() {
330 return underUse.size();
333 public synchronized void lockedStatements(PrintWriter writer) {
334 writer.println(underUse.size());
335 for (PreparedStatement ps : underUse) {
336 for (Entry<StatementDescriptor, PreparedStatement> e : statements.entrySet()) {
337 if (e.getValue() == ps) {
338 writer.println("<br/>");
339 writer.println(e.getKey().instance + ":");
341 writer.println(e.getKey().query);
347 public static synchronized Link newLink(boolean readOnly) throws InterruptedException {
348 if (instances.get(Thread.currentThread()) != null) {
349 throw new Error("There is already a connection allocated for this thread.");
351 if (pool.isEmpty() && connCount < 5) {
352 pool.addLast(new DatabaseConnection());
355 DatabaseConnection conn = pool.takeFirst();
357 conn.c.setReadOnly(readOnly);
358 } catch (SQLException e) {
361 Link l = new Link(conn);
362 instances.put(Thread.currentThread(), l);