From 0578cdf3f43154e803dd06050b490d74966695ca Mon Sep 17 00:00:00 2001 From: xHyroM Date: Mon, 28 Aug 2023 12:18:26 +0200 Subject: [PATCH] feat: sqlite connection factory --- .../file/FileConnectionFactory.java | 6 +++ .../connection/file/H2ConnectionFactory.java | 6 +-- .../file/SqliteConnectionFactory.java | 40 +++++++++++++++++++ 3 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 common/src/main/java/dev/xhyrom/lighteco/common/storage/provider/sql/connection/file/SqliteConnectionFactory.java 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); + } + } +}