72 lines
No EOL
2.1 KiB
Java
72 lines
No EOL
2.1 KiB
Java
package tech.sbdevelopment.v10lift.sbutils;
|
|
|
|
import com.craftmend.storm.Storm;
|
|
import com.craftmend.storm.connection.hikaricp.HikariDriver;
|
|
import com.zaxxer.hikari.HikariConfig;
|
|
import com.zaxxer.hikari.HikariDataSource;
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.sql.Connection;
|
|
import java.sql.SQLException;
|
|
|
|
public class StormSQLiteDB {
|
|
private final JavaPlugin plugin;
|
|
private final String dbName;
|
|
private final Storm storm;
|
|
|
|
/**
|
|
* Initialize a new database source
|
|
*
|
|
* @param dbName The database name
|
|
*/
|
|
public StormSQLiteDB(JavaPlugin plugin, String dbName) throws SQLException {
|
|
this.plugin = plugin;
|
|
this.dbName = dbName;
|
|
|
|
File dbFile = new File(plugin.getDataFolder(), dbName + ".db");
|
|
|
|
if (!dbFile.exists()) {
|
|
try {
|
|
plugin.getLogger().info("Generating the " + dbName + ".db...");
|
|
if (!dbFile.createNewFile()) {
|
|
throw new IOException("Couldn't generate the " + dbName + ".db!");
|
|
}
|
|
} catch (IOException e) {
|
|
throw new RuntimeException("Couldn't generate the " + dbName + ".db!", e);
|
|
}
|
|
}
|
|
|
|
HikariConfig config = new HikariConfig();
|
|
config.setPoolName(plugin.getName());
|
|
config.setJdbcUrl("jdbc:sqlite:" + dbFile.getAbsolutePath());
|
|
config.setUsername(null);
|
|
config.setPassword(null);
|
|
config.setDriverClassName("org.sqlite.JDBC");
|
|
config.setConnectionTestQuery("SELECT 1");
|
|
config.setMaxLifetime(60000);
|
|
config.setIdleTimeout(45000);
|
|
config.setMinimumIdle(10);
|
|
config.setMaximumPoolSize(50);
|
|
|
|
this.storm = new Storm(new HikariDriver(config));
|
|
}
|
|
|
|
/**
|
|
* Close the connection
|
|
*/
|
|
public void closeSource() {
|
|
plugin.getLogger().info("Closing the database connection for " + dbName + ".db...");
|
|
this.storm.getDriver().close();
|
|
}
|
|
|
|
/**
|
|
* Get the database connection
|
|
*
|
|
* @return The database connection
|
|
*/
|
|
public Storm getStorm() {
|
|
return storm;
|
|
}
|
|
} |