diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/file/FileConnectionFactory.java b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/file/FileConnectionFactory.java index a8f5a0d..499f2ce 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/file/FileConnectionFactory.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/file/FileConnectionFactory.java @@ -5,6 +5,7 @@ import dev.xhyrom.lighteco.common.storage.provider.sql.connection.ConnectionFact import java.io.File; import java.sql.Connection; import java.sql.SQLException; +import java.util.function.Function; abstract class FileConnectionFactory implements ConnectionFactory { private Connection connection; @@ -33,4 +34,9 @@ abstract class FileConnectionFactory implements ConnectionFactory { return connection; } + + @Override + public Function getStatementProcessor() { + return s -> s.replace('\'', '`'); + } } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/file/H2ConnectionFactory.java b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/file/H2ConnectionFactory.java index abb1236..4cd7389 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/file/H2ConnectionFactory.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/file/H2ConnectionFactory.java @@ -18,6 +18,7 @@ public class H2ConnectionFactory extends FileConnectionFactory { @Override public void init(LightEcoPlugin plugin) { + // TODO: implement //ClassLoader classLoader = plugin } @@ -37,9 +38,4 @@ public class H2ConnectionFactory extends FileConnectionFactory { throw new SQLException("Failed to create connection", e); } } - - @Override - public Function getStatementProcessor() { - return s -> s.replace('\'', '`'); - } } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/file/SqliteConnectionFactory.java b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/file/SqliteConnectionFactory.java new file mode 100644 index 0000000..2ee65f6 --- /dev/null +++ b/common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/file/SqliteConnectionFactory.java @@ -0,0 +1,40 @@ +package dev.xhyrom.lighteco.common.storage.provider.sql.connection.file; + +import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; + +import java.io.File; +import java.lang.reflect.Constructor; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.Properties; +import java.util.function.Function; + +public class SqliteConnectionFactory extends FileConnectionFactory { + private Constructor connectionConstructor; + + public SqliteConnectionFactory(File file) { + super(file); + } + + @Override + public void init(LightEcoPlugin plugin) { + // TODO: implement + } + + @Override + protected Connection createConnection(File file) throws SQLException { + try { + return (Connection) this.connectionConstructor.newInstance( + "jdbc:sqlite:" + file.getAbsolutePath(), + new Properties(), + null, null, false + ); + } catch (Exception e) { + if (e.getCause() instanceof SQLException) { + throw (SQLException) e.getCause(); + } + + throw new SQLException("Failed to create connection", e); + } + } +}