Skip to content

Commit

Permalink
Add support for sqlite (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
phemmer authored and Phoenix616 committed Jun 7, 2019
1 parent 31fc062 commit 6d9e484
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
38 changes: 35 additions & 3 deletions src/main/java/com/wfector/notifier/ChestShopNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;
import java.io.File;

import com.zaxxer.hikari.HikariDataSource;
import org.bukkit.ChatColor;
Expand All @@ -32,6 +33,7 @@
public class ChestShopNotifier extends JavaPlugin implements Listener {

private HikariDataSource ds;
private DbType dbType = DbType.SQLITE;

private List<Object[]> batch = new ArrayList<>();

Expand Down Expand Up @@ -83,15 +85,32 @@ public void updateConfiguration(final CommandSender sender) {
joinNotificationDelay = getConfig().getInt("notifications.delay-seconds");
logAdminShop = getConfig().getBoolean("logging.admin-shop");

String dbType = getConfig().getString("database.type", "mysql");
try {
this.dbType = DbType.valueOf(dbType.toUpperCase());
} catch (IllegalArgumentException e) {
this.dbType = DbType.SQLITE;
getLogger().log(Level.WARNING, "Unknown dbType setting '" + dbType + "'! Possible settings are MySQL and SQLite. Falling back to SQLite!");
}
String dbHost = getConfig().getString("database.host");
int dbPort = getConfig().getInt("database.port");
String dbName = getConfig().getString("database.dbname");
String dbName = getConfig().getString("database.dbname", "database");
String dbUsername = getConfig().getString("database.username");
String dbPassword = getConfig().getString("database.password");
boolean useSsl = getConfig().getBoolean("database.ssl");

ds = new HikariDataSource();
ds.setJdbcUrl("jdbc:mysql://" + dbHost + ":" + dbPort + "/" + dbName + "?useSSL=" + useSsl);

switch (this.dbType) {
default:
getLogger().log(Level.WARNING, "Unsupported database type setting '" + this.dbType + "'! Falling back to SQLite!");
case SQLITE:
ds.setJdbcUrl("jdbc:sqlite:" + new File(getDataFolder(), dbName + ".sqlite"));
break;
case MYSQL:
ds.setJdbcUrl("jdbc:" + dbType + "://" + dbHost + ":" + dbPort + "/" + dbName + "?useSSL=" + useSsl);
break;
}
ds.setUsername(dbUsername);
ds.setPassword(dbPassword);
ds.setConnectionTimeout(5000);
Expand All @@ -103,7 +122,15 @@ public void run() {
try (Connection c = getConnection()){
Statement statement = c.createStatement();

statement.executeUpdate("CREATE TABLE IF NOT EXISTS csnUUID (Id int(11) AUTO_INCREMENT, ShopOwnerId VARCHAR(36), CustomerId VARCHAR(36), ItemId VARCHAR(1000), Mode INT(11), Amount FLOAT(53), Quantity INT(11), Time INT(11), Unread INT(11), PRIMARY KEY (Id))");
switch (ChestShopNotifier.this.dbType) {
default:
case SQLITE:
statement.executeUpdate("CREATE TABLE IF NOT EXISTS csnUUID (Id INTEGER PRIMARY KEY AUTOINCREMENT, ShopOwnerId VARCHAR(36), CustomerId VARCHAR(36), ItemId VARCHAR(1000), Mode INT(11), Amount FLOAT(53), Quantity INT(11), Time INT(11), Unread INT(11))");
break;
case MYSQL:
statement.executeUpdate("CREATE TABLE IF NOT EXISTS csnUUID (Id int(11) AUTO_INCREMENT, ShopOwnerId VARCHAR(36), CustomerId VARCHAR(36), ItemId VARCHAR(1000), Mode INT(11), Amount FLOAT(53), Quantity INT(11), Time INT(11), Unread INT(11), PRIMARY KEY (Id))");
break;
}

pluginEnabled = true;
} catch (SQLException e) {
Expand Down Expand Up @@ -228,4 +255,9 @@ public Connection getConnection() throws SQLException {
public List<Object[]> getBatch() {
return batch;
}

private enum DbType {
SQLITE,
MYSQL;
}
}
5 changes: 4 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ debugging:
verbose: true

database:
# Possible types: SQLite and MySQL
type: sqlite
dbname: 'database'
# MySQL specific settings
host: 'localhost'
port: 3306
dbname: 'database1'
username: 'dbadmin'
password: '123456'
ssl: false
Expand Down

0 comments on commit 6d9e484

Please sign in to comment.