Commit 730ba95e authored by Giant's avatar Giant
Browse files

Remove Database from GiantCore.

parent 43cc05c4
package nl.giantit.minecraft.giantcore.Database;
import nl.giantit.minecraft.giantcore.Database.drivers.MySQL;
import nl.giantit.minecraft.giantcore.Database.drivers.SQLite;
import nl.giantit.minecraft.giantcore.Database.drivers.hTwo;
import org.bukkit.plugin.Plugin;
import java.util.HashMap;
/**
*
* @author Giant
*/
public class Database {
private enum dbType {
MySQL("MySQL"),
hTwo("h2"),
SQLite("SQLite");
String value;
private dbType(String s) {
this.value = s;
}
@Override
public String toString() {
return this.value;
}
}
private HashMap<String, iDriver> instance = new HashMap<String, iDriver>();
private Plugin p;
public Database(Plugin p, HashMap<String, String> conf, String instID) {
this.p = p;
if(instID == null)
instID = "0";
this.instance = new HashMap<String, iDriver>();
String d = conf.get("driver");
iDriver dbDriver;
if(d.equalsIgnoreCase("MySQL")) {
dbDriver = new MySQL(p, conf);
}else if(d.equalsIgnoreCase("h2")) {
dbDriver = new hTwo(p, conf);
}else{
dbDriver = new SQLite(p, conf);
}
this.instance.put(instID, dbDriver);
}
public iDriver getEngine() {
return this.getEngine(null);
}
public iDriver getEngine(String instID) {
if(instID == null)
instID = "0";
if(this.instance.containsKey(instID))
return this.instance.get(instID);
// Instance doesn't exist, nor were there any config data passed to init
return null;
}
public iDriver getEngine(String instID, HashMap<String, String> conf) {
if(instID == null)
instID = "0";
if(this.instance.containsKey(instID))
return this.instance.get(instID);
String d = conf.get("driver");
iDriver dbDriver;
if(d.equalsIgnoreCase("MySQL")) {
dbDriver = new MySQL(p, conf);
}else if(d.equalsIgnoreCase("h2")) {
dbDriver = new hTwo(p, conf);
}else{
dbDriver = new SQLite(p, conf);
}
this.instance.put(instID, dbDriver);
return dbDriver;
}
}
package nl.giantit.minecraft.giantcore.Database;
/**
*
* @author Giant
*/
public enum DatabaseType {
SQLite("SQLite"),
MySQL("MySQL"),
hTwo("h2"),
Central("Central library");
private String name;
private DatabaseType(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
package nl.giantit.minecraft.giantcore.Database;
import java.util.ArrayList;
import java.util.HashMap;
/**
*
* @author Giant
*/
public class QueryResult {
private int pointer = 0;
private int size;
private ArrayList<HashMap<String, String>> data;
private QueryResult(ArrayList<HashMap<String, String>> data) {
this.pointer = 0;
this.data = data;
this.size = data.size();
}
public QueryRow getRow() {
if(data.size() > pointer) {
QueryRow qR = new QueryRow(data.get(pointer));
pointer++;
return qR;
}
pointer = 0;
return null;
}
public QueryRow getRow(int point) {
if(data.size() > point) {
QueryRow qR = new QueryRow(data.get(point));
return qR;
}
return null;
}
public void first() {
this.pointer = 0;
}
public void last() {
this.pointer = this.size - 1;
}
public int size() {
return this.size;
}
public ArrayList<HashMap<String, String>> getRawData() {
return this.data;
}
public class QueryRow {
private HashMap<String, String> row;
private QueryRow(HashMap<String, String> row) {
this.row = row;
}
public String getString(String key) {
if(row.containsKey(key.toLowerCase())) {
return row.get(key.toLowerCase());
}
return null;
}
public boolean getBoolean(String key) {
if(row.containsKey(key.toLowerCase())) {
//return Boolean.parseBoolean(row.get(key));
String v = row.get(key.toLowerCase());
return v.equalsIgnoreCase("true") || v.equals("1") || v.equals("yes") || v.equals("y");
}
return false;
}
public int getInt(String key) {
try{
if(row.containsKey(key.toLowerCase())) {
return Integer.parseInt(row.get(key.toLowerCase()));
}
}catch(NumberFormatException e) {
return 0;
}
return 0;
}
public Integer getInteger(String key) {
try{
if(row.containsKey(key.toLowerCase())) {
return Integer.valueOf(row.get(key.toLowerCase()));
}
}catch(NumberFormatException e) {
return 0;
}
return 0;
}
public float getFloat(String key) {
try{
if(row.containsKey(key.toLowerCase())) {
return Float.parseFloat(row.get(key.toLowerCase()));
}
}catch(NumberFormatException e) {
return 0F;
}
return 0F;
}
public double getDouble(String key) {
try{
if(row.containsKey(key.toLowerCase())) {
return Double.parseDouble(row.get(key.toLowerCase()));
}
}catch(NumberFormatException e) {
return 0D;
}
return 0D;
}
public long getLong(String key) {
try{
if(row.containsKey(key.toLowerCase())) {
return Long.parseLong(row.get(key.toLowerCase()));
}
}catch(NumberFormatException e) {
return 0L;
}
return 0L;
}
}
public static QueryResult QR(ArrayList<HashMap<String, String>> data) {
return new QueryResult(data);
}
}
package nl.giantit.minecraft.giantcore.Database.drivers;
import nl.giantit.minecraft.giantcore.Database.iDriver;
import nl.giantit.minecraft.giantcore.Database.DatabaseType;
import nl.giantit.minecraft.giantcore.Database.QueryResult;
import org.bukkit.plugin.Plugin;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
/**
*
* @author Giant
*/
public class SQLite implements iDriver {
private DatabaseType type = DatabaseType.SQLite;
private Plugin plugin;
private ArrayList<HashMap<String, String>> sql = new ArrayList<HashMap<String, String>>();
private String db, user, pass, prefix;
private Connection con = null;
private boolean dbg = false;
private boolean parseBool(String s, boolean d) {
if(s.equalsIgnoreCase("1") || s.equalsIgnoreCase("y") || s.equalsIgnoreCase("yes") || s.equalsIgnoreCase("true"))
return true;
return d;
}
public SQLite(Plugin p, HashMap<String, String> c) {
plugin = p;
this.db = c.get("database");
this.prefix = c.get("prefix");
this.user = c.get("user");
this.pass = c.get("password");
this.dbg = (c.containsKey("debug")) ? this.parseBool(c.get("debug"), false) : false;
String dbPath = "jdbc:sqlite:" + plugin.getDataFolder() + java.io.File.separator + this.db + ".db";
try{
Class.forName("org.sqlite.JDBC");
this.con = DriverManager.getConnection(dbPath, this.user, this.pass);
}catch(SQLException e) {
plugin.getLogger().log(Level.SEVERE, "Failed to connect to database: SQL error!");
if(this.dbg) {
plugin.getLogger().log(Level.INFO, e.getMessage(), e);
}
}catch(ClassNotFoundException e) {
plugin.getLogger().log(Level.SEVERE, "Failed to connect to database: SQLite library not found!");
if(this.dbg) {
plugin.getLogger().log(Level.INFO, e.getMessage(), e);
}
}
}
@Override
public void close() {
try {
if(!con.isClosed() || !con.isValid(0))
return;
this.con.close();
}catch(SQLException e) {
//ignore
}
}
@Override
public boolean isConnected() {
try {
return con != null && !con.isClosed();
}catch(SQLException e) {
return false;
}
}
@Override
public boolean tableExists(String table) {
ResultSet res = null;
table = table.replace("#__", prefix);
try {
DatabaseMetaData data = this.con.getMetaData();
res = data.getTables(null, null, table, null);
return res.next();
}catch (SQLException e) {
plugin.getLogger().log(Level.SEVERE, " Could not load table " + table);
if(this.dbg) {
plugin.getLogger().log(Level.INFO, e.getMessage(), e);
}
return false;
} finally {
try {
if(res != null) {
res.close();
}
}catch (Exception e) {
plugin.getLogger().log(Level.SEVERE, " Could not close result connection to database");
if(this.dbg) {
plugin.getLogger().log(Level.INFO, e.getMessage(), e);
}
return false;
}
}
}
@Override
public void buildQuery(String string) {
this.buildQuery(string, false, false, false);
return;
}
@Override
public void buildQuery(String string, boolean add) {
this.buildQuery(string, add, false, false);
return;
}
@Override
public void buildQuery(String string, boolean add, boolean finalize) {
this.buildQuery(string, add, finalize, false);
return;
}
@Override
public void buildQuery(String string, boolean add, boolean finalize, boolean debug) {
this.buildQuery(string, add, finalize, debug, false);
}
@Override
public void buildQuery(String string, boolean add, boolean finalize, boolean debug, boolean table) {
if(!add) {
if(table)
string = string.replace("#__", prefix);
HashMap<String, String> ad = new HashMap<String, String>();
ad.put("sql", string);
if(finalize)
ad.put("finalize", "true");
if(debug)
ad.put("debug", "true");
sql.add(ad);
}else{
int last = sql.size() - 1;
this.buildQuery(string, last, finalize, debug, table);
}
}
@Override
public void buildQuery(String string, Integer add) {
this.buildQuery(string, add, false, false);
}
@Override
public void buildQuery(String string, Integer add, boolean finalize) {
this.buildQuery(string, add, finalize, false);
}
@Override
public void buildQuery(String string, Integer add, boolean finalize, boolean debug) {
this.buildQuery(string, add, finalize, debug, false);
}
@Override
public void buildQuery(String string, Integer add, boolean finalize, boolean debug, boolean table) {
if(table)
string = string.replace("#__", prefix);
try {
HashMap<String, String> SQL = sql.get(add);
if(SQL.containsKey("sql")) {
if(SQL.containsKey("finalize")) {
if(true == debug)
plugin.getLogger().log(Level.SEVERE, " SQL syntax is finalized!");
return;
}else{
SQL.put("sql", SQL.get("sql") + string);
if(true == finalize)
SQL.put("finalize", "true");
sql.add(add, SQL);
}
}else
if(true == debug)
plugin.getLogger().log(Level.SEVERE, add.toString() + " is not a valid SQL query!");
if(debug == true)
plugin.getLogger().log(Level.INFO, sql.get(add).get("sql"));
}catch(NullPointerException e) {
if(true == debug)
plugin.getLogger().log(Level.SEVERE, "Query " + add.toString() + " could not be found!");
}
}
@Override
public QueryResult execQuery() {
Integer queryID = ((sql.size() - 1 > 0) ? (sql.size() - 1) : 0);
return this.execQuery(queryID);
}
@Override
public QueryResult execQuery(Integer queryID) {
Statement st = null;
ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
try {
HashMap<String, String> SQL = sql.get(queryID);
if(SQL.containsKey("sql")) {
try {
st = con.createStatement();
//query.add(queryID, st.executeQuery(SQL.get("sql")));
ResultSet res = st.executeQuery(SQL.get("sql"));
while(res.next()) {
HashMap<String, String> row = new HashMap<String, String>();
ResultSetMetaData rsmd = res.getMetaData();
int columns = rsmd.getColumnCount();
for(int i = 1; i < columns + 1; i++) {
row.put(rsmd.getColumnName(i).toLowerCase(), res.getString(i));
}
data.add(row);
}
}catch (SQLException e) {
plugin.getLogger().log(Level.SEVERE, " Could not execute query!");
if(this.dbg) {
plugin.getLogger().log(Level.INFO, e.getMessage(), e);
}
} finally {
try {
if(st != null) {
st.close();
}
}catch (Exception e) {
plugin.getLogger().log(Level.SEVERE, " Could not close database connection");
if(this.dbg) {
plugin.getLogger().log(Level.INFO, e.getMessage(), e);
}
}
}
}
}catch(NullPointerException e) {
plugin.getLogger().log(Level.SEVERE, "Query " + queryID.toString() + " could not be found!");
}
return QueryResult.QR(data);
}
@Override
public void updateQuery() {
Integer queryID = ((sql.size() - 1 > 0) ? (sql.size() - 1) : 0);
this.updateQuery(queryID);
}
@Override
public void updateQuery(Integer queryID) {
Statement st = null;
try {
HashMap<String, String> SQL = sql.get(queryID);
if(SQL.containsKey("sql")) {
try {
st = con.createStatement();
st.executeUpdate(SQL.get("sql"));
}catch (SQLException e) {
plugin.getLogger().log(Level.SEVERE, " Could not execute query!");
if(this.dbg) {
plugin.getLogger().log(Level.INFO, e.getMessage(), e);
}
} finally {
try {
if(st != null) {
st.close();
}
}catch (Exception e) {
plugin.getLogger().log(Level.SEVERE, " Could not close database connection");
if(this.dbg) {
plugin.getLogger().log(Level.INFO, e.getMessage(), e);
}
}