mirror of
https://github.com/xHyroM/lighteco.git
synced 2024-11-24 15:51:06 +01:00
feat: sqlite connection factory
This commit is contained in:
parent
e340b32e86
commit
0578cdf3f4
3 changed files with 47 additions and 5 deletions
|
@ -5,6 +5,7 @@ import dev.xhyrom.lighteco.common.storage.provider.sql.connection.ConnectionFact
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
abstract class FileConnectionFactory implements ConnectionFactory {
|
abstract class FileConnectionFactory implements ConnectionFactory {
|
||||||
private Connection connection;
|
private Connection connection;
|
||||||
|
@ -33,4 +34,9 @@ abstract class FileConnectionFactory implements ConnectionFactory {
|
||||||
|
|
||||||
return connection;
|
return connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Function<String, String> getStatementProcessor() {
|
||||||
|
return s -> s.replace('\'', '`');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@ public class H2ConnectionFactory extends FileConnectionFactory {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init(LightEcoPlugin plugin) {
|
public void init(LightEcoPlugin plugin) {
|
||||||
|
// TODO: implement
|
||||||
//ClassLoader classLoader = plugin
|
//ClassLoader classLoader = plugin
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,9 +38,4 @@ public class H2ConnectionFactory extends FileConnectionFactory {
|
||||||
throw new SQLException("Failed to create connection", e);
|
throw new SQLException("Failed to create connection", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Function<String, String> getStatementProcessor() {
|
|
||||||
return s -> s.replace('\'', '`');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue