package io.github.thatsmusic99.headsplus.sql;

import io.github.thatsmusic99.headsplus.HeadsPlus;
import io.github.thatsmusic99.headsplus.config.MainConfig;

import java.sql.*;

public abstract class SQLManager {

    protected static String tablePrefix;
    protected static volatile boolean usingSqlite;

    public static void setupPrefix() {
        // TODO - table prefix
        tablePrefix = "headsplus";
        if (!tablePrefix.matches("^[_A-Za-z0-9]+$")) {
            HeadsPlus.get().getLogger().warning("Table prefix " + tablePrefix + " is not alphanumeric. Using headsplus...");
            tablePrefix = "headsplus";
        }
    }

    private Connection loadSqlite() {
        // Load JDBC
        try {
            Class.forName("org.sqlite.JDBC");
            Connection connection = DriverManager.getConnection("jdbc:sqlite:" + HeadsPlus.get().getDataFolder() + "/data.db");
            usingSqlite = true;
            return connection;
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Connection implementConnection() {
        Connection connection;
        if (MainConfig.get().getMySQL().ENABLE_MYSQL) {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                connection = DriverManager.getConnection("jdbc:mysql://"
                                + MainConfig.get().getMySQL().MYSQL_HOST + ":"
                                + MainConfig.get().getMySQL().MYSQL_PORT + "/"
                                + MainConfig.get().getMySQL().MYSQL_DATABASE + "?useSSL=false&autoReconnect=true",
                        MainConfig.get().getMySQL().MYSQL_USERNAME,
                        MainConfig.get().getMySQL().MYSQL_PASSWORD);
                usingSqlite = false;
                return connection;
            } catch (ClassNotFoundException | SQLException e) {
                e.printStackTrace();
                connection = loadSqlite();
            }
        } else {
            connection = loadSqlite();
        }
        return connection;
    }

    public abstract void createTable();

    public abstract void transferOldData();

    public static String getTablePrefix() {
        return tablePrefix;
    }

    public String getStupidAutoIncrementThing() {
        return usingSqlite ? "AUTOINCREMENT" : "AUTO_INCREMENT";
    }

    protected synchronized ResultSet executeQuery(PreparedStatement statement) throws SQLException {
        return statement.executeQuery();
    }

    protected synchronized void executeUpdate(PreparedStatement statement) throws SQLException {
        statement.executeUpdate();
    }

    protected synchronized PreparedStatement prepareStatement(Connection connection, String sql) throws SQLException {
        return connection.prepareStatement(sql);
    }
}

@@ -1124,24 +1124,58 @@
 nection() %7B%0A
+        synchronized (this) %7B%0A    
         Conn
@@ -1185,32 +1185,36 @@
 ion connection;%0A
+    
         if (Main
@@ -1257,32 +1257,36 @@
 ) %7B%0A            
+    
 try %7B%0A          
@@ -1283,32 +1283,36 @@
                 
+   
+ 
 Class.forName(%22c
@@ -1331,24 +1331,28 @@
 c.Driver%22);%0A
+    
             
@@ -1436,32 +1436,36 @@
                 
+   
+ 
 + MainConfig.get
@@ -1495,16 +1495,20 @@
 T + %22:%22%0A
+    
         
@@ -1602,32 +1602,36 @@
                 
+  
+  
 + MainConfig.get
@@ -1713,32 +1713,36 @@
                 
+    
 MainConfig.get()
@@ -1785,32 +1785,36 @@
                 
+  
+  
 MainConfig.get()
@@ -1850,32 +1850,36 @@
                 
+    
 usingSqlite = fa
@@ -1891,32 +1891,36 @@
                 
+ 
+   
 return connectio
@@ -1914,32 +1914,36 @@
 urn connection;%0A
+    
             %7D ca
@@ -1998,32 +1998,36 @@
                 
+    
 e.printStackTrac
@@ -2023,32 +2023,36 @@
 ntStackTrace();%0A
+    
                 
@@ -2082,34 +2082,42 @@
 ();%0A            
-%7D%0A
+    %7D%0A    
         %7D else %7B
@@ -2117,16 +2117,20 @@
  else %7B%0A
+    
         
@@ -2160,34 +2160,42 @@
 lite();%0A        
+   
+ 
 %7D%0A
+    
         return c
@@ -2201,24 +2201,34 @@
 connection;%0A
+        %7D%0A
     %7D%0A%0A    p

Synchronise implementConnection so we don't lock the SQLite database(?)