What the fuck was I doing
This commit is contained in:
parent
1416d1039d
commit
6d3666ee08
|
@ -1,10 +1,16 @@
|
|||
package com.ryderbelserion.crazyauctions.api.database;
|
||||
|
||||
import com.ryderbelserion.crazyauctions.CrazyAuctions;
|
||||
import com.ryderbelserion.crazyauctions.api.database.impl.StorageImpl;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class Storage {
|
||||
|
||||
private final CrazyAuctions plugin = CrazyAuctions.get();
|
||||
|
||||
private final StorageImpl storage;
|
||||
|
||||
public Storage(StorageImpl storage) {
|
||||
|
@ -16,14 +22,24 @@ public class Storage {
|
|||
}
|
||||
|
||||
public void init() {
|
||||
this.storage.init();
|
||||
this.storage.start();
|
||||
}
|
||||
|
||||
public Connection getConnection() {
|
||||
try {
|
||||
return getStorage().getConnection();
|
||||
} catch (SQLException exception) {
|
||||
this.plugin.getLogger().log(Level.SEVERE, "Failed to get: " + this.storage.getImplName(), exception);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void shutdown() {
|
||||
try {
|
||||
this.storage.shutdown();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
} catch (SQLException exception) {
|
||||
this.plugin.getLogger().log(Level.SEVERE, "Failed to shutdown: " + this.storage.getImplName(), exception);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,21 +1,16 @@
|
|||
package com.ryderbelserion.crazyauctions.api.database;
|
||||
|
||||
import com.badbones69.crazyauctions.common.api.CrazyAuctionsPlugin;
|
||||
import com.badbones69.crazyauctions.common.config.types.Config;
|
||||
import com.badbones69.crazyauctions.common.enums.storage.StorageType;
|
||||
import com.ryderbelserion.crazyauctions.api.database.impl.StorageImpl;
|
||||
import com.ryderbelserion.crazyauctions.api.database.impl.sql.file.types.SqliteConnection;
|
||||
import com.ryderbelserion.crazyauctions.CrazyAuctions;
|
||||
import com.ryderbelserion.crazyauctions.api.database.impl.sql.SqlStorage;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class StorageFactory {
|
||||
|
||||
public Storage getInstance() {
|
||||
Storage storage;
|
||||
|
||||
storage = new Storage(create(CrazyAuctionsPlugin.get().getConfig().getProperty(Config.storage_type)));
|
||||
Storage storage = new Storage(create(StorageType.SQLITE));
|
||||
storage.init();
|
||||
|
||||
return storage;
|
||||
|
|
|
@ -1,16 +1,19 @@
|
|||
package com.ryderbelserion.crazyauctions.api.database.impl;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public interface StorageImpl {
|
||||
|
||||
String getImplName();
|
||||
|
||||
void init();
|
||||
void start();
|
||||
|
||||
void shutdown() throws SQLException;
|
||||
|
||||
File getFile();
|
||||
|
||||
Connection getConnection() throws SQLException;
|
||||
|
||||
}
|
|
@ -2,8 +2,8 @@ package com.ryderbelserion.crazyauctions.api.database.impl.sql;
|
|||
|
||||
import com.ryderbelserion.crazyauctions.api.database.impl.StorageImpl;
|
||||
import com.ryderbelserion.crazyauctions.api.database.impl.sql.file.ConnectionImpl;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class SqlStorage implements StorageImpl {
|
||||
|
@ -12,6 +12,7 @@ public class SqlStorage implements StorageImpl {
|
|||
|
||||
public SqlStorage(ConnectionImpl connection) {
|
||||
this.connection = connection;
|
||||
this.connection.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -20,9 +21,7 @@ public class SqlStorage implements StorageImpl {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
this.connection.init();
|
||||
}
|
||||
public void start() {}
|
||||
|
||||
@Override
|
||||
public void shutdown() throws SQLException {
|
||||
|
@ -33,4 +32,9 @@ public class SqlStorage implements StorageImpl {
|
|||
public File getFile() {
|
||||
return this.connection.getFile();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnection() throws SQLException {
|
||||
return this.connection.getConnection();
|
||||
}
|
||||
}
|
|
@ -1,7 +1,6 @@
|
|||
package com.ryderbelserion.crazyauctions.api.database.impl.sql.file.types;
|
||||
|
||||
import com.ryderbelserion.crazyauctions.api.database.impl.sql.file.ConnectionImpl;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
|
@ -20,7 +19,7 @@ public class SqliteConnection extends ConnectionImpl {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
public void start() {
|
||||
try {
|
||||
getFile().createNewFile();
|
||||
} catch (IOException exception) {
|
||||
|
@ -30,16 +29,6 @@ public class SqliteConnection extends ConnectionImpl {
|
|||
|
||||
@Override
|
||||
protected Connection create() throws SQLException {
|
||||
try {
|
||||
Class.forName("org.sqlite.jdbc4.JDBC4Connection").getDeclaredConstructor().newInstance();
|
||||
|
||||
return DriverManager.getConnection("jdbc:sqlite:" + getFile());
|
||||
} catch (ReflectiveOperationException exception) {
|
||||
if (exception.getCause() instanceof SQLException) {
|
||||
throw (SQLException) exception.getCause();
|
||||
}
|
||||
|
||||
throw new RuntimeException(exception);
|
||||
}
|
||||
return DriverManager.getConnection("jdbc:sqlite:" + getFile());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue