1 package club.wpia.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 club.wpia.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 = 31;
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)) {
238 try (GigiPreparedStatement empty = new GigiPreparedStatement("SELECT * from information_schema.tables WHERE table_schema='public' AND table_name='schemeVersion'")) {
239 if ( !empty.executeQuery().next()) {
240 try (InputStream resourceAsStream = DatabaseConnection.class.getResourceAsStream("tableStructure.sql")) {
241 if (resourceAsStream == null) {
242 throw new Error("DB-Install-Script not found.");
244 try (Statement s = getInstance().c.createStatement()) {
245 SQLFileManager.addFile(s, resourceAsStream, ImportType.PRODUCTION);
251 } catch (IOException e) {
253 } catch (SQLException e) {
257 try (GigiPreparedStatement gigiPreparedStatement = new GigiPreparedStatement("SELECT version FROM \"schemeVersion\" ORDER BY version DESC LIMIT 1;")) {
258 GigiResultSet rs = gigiPreparedStatement.executeQuery();
260 version = rs.getInt(1);
263 if (version == CURRENT_SCHEMA_VERSION) {
264 return; // Good to go
266 if (version > CURRENT_SCHEMA_VERSION) {
267 throw new Error("Invalid database version. Please fix this.");
270 } catch (InterruptedException e) {
275 private static void upgrade(int version) {
277 try (Statement s = getInstance().c.createStatement()) {
278 while (version < CURRENT_SCHEMA_VERSION) {
279 addUpgradeScript(Integer.toString(version), s);
282 s.addBatch("UPDATE \"schemeVersion\" SET version='" + version + "'");
283 System.out.println("UPGRADING Database to version " + version);
285 System.out.println("done.");
287 } catch (SQLException e) {
289 } catch (IOException e) {
294 private static void addUpgradeScript(String version, Statement s) throws Error, IOException, SQLException {
295 try (InputStream resourceAsStream = DatabaseConnection.class.getResourceAsStream("upgrade/from_" + version + ".sql")) {
296 if (resourceAsStream == null) {
297 throw new Error("Upgrade script from version " + version + " was not found.");
299 SQLFileManager.addFile(s, resourceAsStream, ImportType.PRODUCTION);
303 public static final String preprocessQuery(String originalQuery) {
304 originalQuery = originalQuery.replace('`', '"');
305 if (originalQuery.matches("^INSERT INTO [^ ]+ SET .*")) {
306 Pattern p = Pattern.compile("INSERT INTO ([^ ]+) SET (.*)");
307 Matcher m = p.matcher(originalQuery);
309 String replacement = "INSERT INTO " + toIdentifier(m.group(1));
310 String[] parts = m.group(2).split(",");
311 StringJoiner columns = new StringJoiner(", ");
312 StringJoiner values = new StringJoiner(", ");
313 for (int i = 0; i < parts.length; i++) {
314 String[] split = parts[i].split("=", 2);
315 columns.add(toIdentifier(split[0]));
316 values.add(split[1]);
318 replacement += "(" + columns.toString() + ") VALUES(" + values.toString() + ")";
324 return originalQuery;
327 private static CharSequence toIdentifier(String ident) {
328 ident = ident.trim();
329 if ( !ident.startsWith("\"")) {
330 ident = "\"" + ident;
332 if ( !ident.endsWith("\"")) {
333 ident = ident + "\"";
338 protected synchronized void returnStatement(PreparedStatement target) throws SQLException {
339 if ( !underUse.remove(target)) {
344 public synchronized int getNumberOfLockedStatements() {
345 return underUse.size();
348 public synchronized void lockedStatements(PrintWriter writer) {
349 writer.println(underUse.size());
350 for (PreparedStatement ps : underUse) {
351 for (Entry<StatementDescriptor, PreparedStatement> e : statements.entrySet()) {
352 if (e.getValue() == ps) {
353 writer.println("<br/>");
354 writer.println(e.getKey().instance + ":");
356 writer.println(e.getKey().query);
362 public static Link newLink(boolean readOnly) throws InterruptedException {
363 synchronized (DatabaseConnection.class) {
365 if (instances.get(Thread.currentThread()) != null) {
366 throw new Error("There is already a connection allocated for this thread.");
368 if (pool.isEmpty() && connCount < 5) {
369 pool.addLast(new DatabaseConnection());
373 DatabaseConnection conn = pool.takeFirst();
374 synchronized (DatabaseConnection.class) {
376 conn.c.setReadOnly(readOnly);
377 } catch (SQLException e) {
380 Link l = new Link(conn);
381 instances.put(Thread.currentThread(), l);