Skip to content

Commit

Permalink
Start some initial work on the sql portion, no where near close. #18
Browse files Browse the repository at this point in the history
  • Loading branch information
graywolf336 committed Feb 20, 2014
1 parent e54dbc8 commit 356fab6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
49 changes: 43 additions & 6 deletions src/main/java/com/graywolf336/jail/JailIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Set;

import org.bukkit.Location;
Expand All @@ -25,6 +28,7 @@
public class JailIO {
private JailMain pl;
private FileConfiguration flat, lang;
private Connection con;
private int storage; //0 = flatfile, 1 = sqlite, 2 = mysql

public JailIO(JailMain plugin) {
Expand Down Expand Up @@ -103,21 +107,54 @@ public String getLanguageString(LangString langString, String... variables) {
return Util.getColorfulMessage(message);
}

/**
* Prepares the storage engine to be used.
*/
public void prepareStorage() {
/** Prepares the storage engine to be used, returns true if everything went good. */
public boolean prepareStorage() {
switch(storage) {
case 1:
//prepare sqlite, I need to research this
break;
return false;
case 2:
//prepare mysql, research this as well
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://" + pl.getConfig().getString("storage.mysql.host") + ":"
+ pl.getConfig().getString("storage.mysql.port") + "/"
+ pl.getConfig().getString("storage.mysql.database"), pl.getConfig().getString("storage.mysql.username"), pl.getConfig().getString("storage.mysql.password"));
connection.setAutoCommit(false);
this.con = connection;
}catch(ClassNotFoundException e) {
e.printStackTrace();
pl.getLogger().severe("---------- Jail Error!! ----------");
pl.getLogger().severe("MySQL driver not found, disabling the plugin.");
return false;
} catch (SQLException e) {
e.printStackTrace();
pl.getLogger().severe("---------- Jail Error!! ----------");
pl.getLogger().severe("Unable to connect to the MySQL database, please update your config accordingly.");
return false;
}
break;
default:
flat = YamlConfiguration.loadConfiguration(new File(pl.getDataFolder(), "data.yml"));
break;
}

return true;
}

/**
* Gets the connection for the sqlite and mysql, null if flatfile.
*
* @return The connection for the sql database.
*/
public Connection getConnection() {
switch(storage) {
case 1:
case 2:
if(con == null) this.prepareStorage();
return con;
default:
return null;
}
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ storage:
port: 3306
username: 'root'
password: 'password'
prefix: 'j3_'
database: 'jailDatabase'
jails:
endermenProtection: true
explosionProtection: true
Expand Down

0 comments on commit 356fab6

Please sign in to comment.