Added MySQL Support

This commit is contained in:
Eric 2016-06-01 21:30:46 +02:00
parent 0015adefb5
commit 70449399bc
8 changed files with 159 additions and 74 deletions

View File

@ -72,6 +72,32 @@ blacklist: []
# - "STONE:1" # - "STONE:1"
# - "DIAMOND_BLOCK" # - "DIAMOND_BLOCK"
database:
# Select the type of database which should be used
# Either use 'SQLite' or 'MySQL'. Otherwise you will break the plugin!
type: "SQLite"
# If the specified is 'MySQL', specify the...
# (You can leave this empty if you're using SQLite)
mysql:
# ...hostname where the database is accessible
hostname: ""
# ...port where the database is accessible (default: 3306)
port: 3306
# ...database you want to use
database: ""
# ...username you are going to login with
username: ""
# ...password you are going to login with
# Be careful, as anyone who can read this file, can read the password!
password: ""
# Priority: default < group < player # Priority: default < group < player
shop-limits: shop-limits:

View File

@ -11,6 +11,8 @@ import de.epiceric.shopchest.interfaces.utils.*;
import de.epiceric.shopchest.shop.Shop; import de.epiceric.shopchest.shop.Shop;
import de.epiceric.shopchest.shop.Shop.ShopType; import de.epiceric.shopchest.shop.Shop.ShopType;
import de.epiceric.shopchest.sql.Database; import de.epiceric.shopchest.sql.Database;
import de.epiceric.shopchest.sql.MySQL;
import de.epiceric.shopchest.sql.SQLite;
import de.epiceric.shopchest.utils.Metrics; import de.epiceric.shopchest.utils.Metrics;
import de.epiceric.shopchest.utils.Metrics.Graph; import de.epiceric.shopchest.utils.Metrics.Graph;
import de.epiceric.shopchest.utils.Metrics.Plotter; import de.epiceric.shopchest.utils.Metrics.Plotter;
@ -66,6 +68,7 @@ public class ShopChest extends JavaPlugin {
@Override @Override
public void onEnable() { public void onEnable() {
logger = getLogger(); logger = getLogger();
instance = this;
if (getServer().getPluginManager().getPlugin("Vault") == null) { if (getServer().getPluginManager().getPlugin("Vault") == null) {
logger.severe("Could not find plugin 'Vault'!"); logger.severe("Could not find plugin 'Vault'!");
@ -121,7 +124,13 @@ public class ShopChest extends JavaPlugin {
reloadConfig(); reloadConfig();
saveDefaultConfig(); saveDefaultConfig();
database = new Database(this); if (Config.database_type() == Database.DatabaseType.SQLite){
logger.info("Using SQLite");
database = new SQLite(this);
} else {
logger.info("Using MySQL");
database = new MySQL(this);
}
switch (Utils.getVersion(getServer())) { switch (Utils.getVersion(getServer())) {
@ -161,8 +170,6 @@ public class ShopChest extends JavaPlugin {
setupPermissions(); setupPermissions();
instance = this;
UpdateChecker uc = new UpdateChecker(this, getDescription().getWebsite()); UpdateChecker uc = new UpdateChecker(this, getDescription().getWebsite());
UpdateCheckerResult result = uc.updateNeeded(); UpdateCheckerResult result = uc.updateNeeded();

View File

@ -1,6 +1,7 @@
package de.epiceric.shopchest.config; package de.epiceric.shopchest.config;
import de.epiceric.shopchest.ShopChest; import de.epiceric.shopchest.ShopChest;
import de.epiceric.shopchest.sql.Database;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
@ -11,6 +12,30 @@ public class Config {
private static ShopChest plugin = ShopChest.getInstance(); private static ShopChest plugin = ShopChest.getInstance();
public static String database_mysql_host() {
return plugin.getConfig().getString("database.mysql.hostname");
}
public static int database_mysql_port() {
return plugin.getConfig().getInt("database.mysql.port");
}
public static String database_mysql_database() {
return plugin.getConfig().getString("database.mysql.database");
}
public static String database_mysql_username() {
return plugin.getConfig().getString("database.mysql.username");
}
public static String database_mysql_password() {
return plugin.getConfig().getString("database.mysql.password");
}
public static Database.DatabaseType database_type() {
return Database.DatabaseType.valueOf(plugin.getConfig().getString("database.type"));
}
public static Set<String> minimum_prices() { public static Set<String> minimum_prices() {
return (plugin.getConfig().getConfigurationSection("minimum-prices") == null) ? new HashSet<String>() : plugin.getConfig().getConfigurationSection("minimum-prices").getKeys(true); return (plugin.getConfig().getConfigurationSection("minimum-prices") == null) ? new HashSet<String>() : plugin.getConfig().getConfigurationSection("minimum-prices").getKeys(true);
} }

View File

@ -16,57 +16,33 @@ import java.io.IOException;
import java.sql.*; import java.sql.*;
import java.util.UUID; import java.util.UUID;
public class Database { public abstract class Database {
private ShopChest plugin; public ShopChest plugin;
private Connection connection; public Connection connection;
public Database(ShopChest instance) { public Database(ShopChest plugin) {
plugin = instance; this.plugin = plugin;
initialize(); initialize();
} }
private Connection getSQLConnection() { public abstract Connection getConnection();
File dbFile = new File(plugin.getDataFolder(), "shops.db");
if (!dbFile.exists()) {
try {
dbFile.createNewFile();
} catch (IOException ex) {
ex.printStackTrace();
}
}
try {
if (connection != null && !connection.isClosed()) {
return connection;
}
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:" + dbFile);
return connection;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
private void initialize() { private void initialize() {
connection = getSQLConnection(); connection = getConnection();
try { try {
String queryCreateTable = "CREATE TABLE IF NOT EXISTS shop_list (" + String queryCreateTable = "CREATE TABLE IF NOT EXISTS shop_list (" +
"`id` int(11) NOT NULL," + "`id` int(11) NOT NULL," +
"`vendor` varchar(32) NOT NULL," + "`vendor` tinytext NOT NULL," +
"`product` varchar(32) NOT NULL," + "`product` text NOT NULL," +
"`world` varchar(32) NOT NULL," + "`world` tinytext NOT NULL," +
"`x` int(11) NOT NULL," + "`x` int(11) NOT NULL," +
"`y` int(11) NOT NULL," + "`y` int(11) NOT NULL," +
"`z` int(11) NOT NULL," + "`z` int(11) NOT NULL," +
"`buyprice` float(32) NOT NULL," + "`buyprice` float(32) NOT NULL," +
"`sellprice` float(32) NOT NULL," + "`sellprice` float(32) NOT NULL," +
"`shoptype` varchar(32) NOT NULL," + "`shoptype` tinytext NOT NULL," +
"PRIMARY KEY (`id`)" + "PRIMARY KEY (`id`)" +
");"; ");";
@ -74,7 +50,7 @@ public class Database {
s.executeUpdate(queryCreateTable); s.executeUpdate(queryCreateTable);
s.close(); s.close();
PreparedStatement ps = connection.prepareStatement("SELECT * FROM shop_list WHERE id = ?"); PreparedStatement ps = connection.prepareStatement("SELECT * FROM shop_list");
ResultSet rs = ps.executeQuery(); ResultSet rs = ps.executeQuery();
close(ps, rs); close(ps, rs);
@ -283,4 +259,9 @@ public class Database {
SELLPRICE, SELLPRICE,
SHOPTYPE; SHOPTYPE;
} }
public enum DatabaseType {
SQLite,
MySQL;
}
} }

View File

@ -1,15 +0,0 @@
package de.epiceric.shopchest.sql;
import de.epiceric.shopchest.ShopChest;
import java.util.logging.Level;
public class Error {
public static void execute(ShopChest plugin, Exception ex) {
plugin.getLogger().log(Level.SEVERE, "Couldn't execute MySQL statement: ", ex);
}
public static void close(ShopChest plugin, Exception ex) {
plugin.getLogger().log(Level.SEVERE, "Failed to close MySQL connection: ", ex);
}
}

View File

@ -1,19 +0,0 @@
package de.epiceric.shopchest.sql;
public class Errors {
public static String sqlConnectionExecute() {
return "Couldn't execute MySQL statement: ";
}
public static String sqlConnectionClose() {
return "Failed to close MySQL connection: ";
}
public static String noSQLConnection() {
return "Unable to retreive MYSQL connection: ";
}
public static String noTableFound() {
return "Database Error: No Table Found";
}
}

View File

@ -0,0 +1,36 @@
package de.epiceric.shopchest.sql;
import de.epiceric.shopchest.ShopChest;
import de.epiceric.shopchest.config.Config;
import java.sql.Connection;
import java.sql.DriverManager;
public class MySQL extends Database {
public MySQL(ShopChest plugin) {
super(plugin);
}
@Override
public Connection getConnection() {
try {
if (connection != null && !connection.isClosed()) {
return connection;
}
Class.forName("com.mysql.jdbc.Driver");
String connectUrl = "jdbc:mysql://" + Config.database_mysql_host() + ":" + Config.database_mysql_port() + "/" + Config.database_mysql_database();
plugin.getLogger().info("Connecting to MySQL Server \"" + connectUrl + "\" as user \"" + Config.database_mysql_username() + "\"");
connection = DriverManager.getConnection(connectUrl, Config.database_mysql_username(), Config.database_mysql_password());
return connection;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
}

View File

@ -0,0 +1,44 @@
package de.epiceric.shopchest.sql;
import de.epiceric.shopchest.ShopChest;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
public class SQLite extends Database {
public SQLite(ShopChest plugin) {
super(plugin);
}
@Override
public Connection getConnection() {
File folder = plugin.getDataFolder();
File dbFile = new File(folder, "shops.db");
if (!dbFile.exists()) {
try {
dbFile.createNewFile();
} catch (IOException ex) {
ex.printStackTrace();
}
}
try {
if (connection != null && !connection.isClosed()) {
return connection;
}
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:" + dbFile);
return connection;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
}