Reformatted code.
This commit is contained in:
@@ -1,215 +1,170 @@
|
||||
package com.redstoner.misc.mysql;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import com.redstoner.exceptions.NonSaveableConfigException;
|
||||
import com.redstoner.misc.Main;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import com.redstoner.exceptions.NonSaveableConfigException;
|
||||
import com.redstoner.misc.Main;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.*;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
public class Config
|
||||
{
|
||||
private File file;
|
||||
public class Config {
|
||||
private File file;
|
||||
private JSONObject config;
|
||||
private JSONParser parser;
|
||||
|
||||
public Config()
|
||||
{
|
||||
|
||||
public Config() {
|
||||
file = null;
|
||||
parser = new JSONParser();
|
||||
config = new JSONObject();
|
||||
}
|
||||
|
||||
public Config(JSONObject config)
|
||||
{
|
||||
|
||||
public Config(JSONObject config) {
|
||||
this.file = null;
|
||||
this.parser = new JSONParser();
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
private Config(File file) throws IOException, ParseException
|
||||
{
|
||||
|
||||
private Config(File file) throws IOException, ParseException {
|
||||
this.file = file;
|
||||
parser = new JSONParser();
|
||||
if (file.exists())
|
||||
{
|
||||
if (file.exists()) {
|
||||
config = loadConfig(file);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
config = new JSONObject();
|
||||
}
|
||||
}
|
||||
|
||||
public static final Config getConfig(String fileName) throws IOException, ParseException
|
||||
{
|
||||
return new Config(new File(Main.plugin.getDataFolder(), fileName));
|
||||
}
|
||||
|
||||
public static final Config getConfig(File file) throws IOException, ParseException
|
||||
{
|
||||
return new Config(file);
|
||||
}
|
||||
|
||||
private JSONObject loadConfig(File file) throws IOException, ParseException
|
||||
{
|
||||
|
||||
private JSONObject loadConfig(File file) throws IOException, ParseException {
|
||||
FileReader reader = new FileReader(file);
|
||||
JSONObject object = (JSONObject) parser.parse(reader);
|
||||
reader.close();
|
||||
return object;
|
||||
}
|
||||
|
||||
|
||||
public static final Config getConfig(String fileName) throws IOException, ParseException {
|
||||
return new Config(new File(Main.plugin.getDataFolder(), fileName));
|
||||
}
|
||||
|
||||
public static final Config getConfig(File file) throws IOException, ParseException {
|
||||
return new Config(file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
public String toString() {
|
||||
return config.toJSONString();
|
||||
}
|
||||
|
||||
public JSONObject asObject()
|
||||
{
|
||||
|
||||
public JSONObject asObject() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public void save() throws IOException, NonSaveableConfigException
|
||||
{
|
||||
if (file == null)
|
||||
{
|
||||
|
||||
public void save() throws IOException, NonSaveableConfigException {
|
||||
if (file == null) {
|
||||
throw new NonSaveableConfigException();
|
||||
}
|
||||
PrintWriter writer = new PrintWriter(file);
|
||||
writer.write(config.toJSONString());
|
||||
writer.close();
|
||||
}
|
||||
|
||||
public void refresh() throws IOException, ParseException, NonSaveableConfigException
|
||||
{
|
||||
if (file == null)
|
||||
{
|
||||
|
||||
public void refresh() throws IOException, ParseException, NonSaveableConfigException {
|
||||
if (file == null) {
|
||||
throw new NonSaveableConfigException();
|
||||
}
|
||||
loadConfig(file);
|
||||
}
|
||||
|
||||
public void setFile(String fileName)
|
||||
{
|
||||
|
||||
public void setFile(String fileName) {
|
||||
file = new File(Main.plugin.getDataFolder(), fileName);
|
||||
}
|
||||
|
||||
public void setFile(File file)
|
||||
{
|
||||
|
||||
public void setFile(File file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void put(String key, String value)
|
||||
{
|
||||
|
||||
@SuppressWarnings ("unchecked")
|
||||
public void put(String key, String value) {
|
||||
config.put(key, value);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void put(String key, List<String> value)
|
||||
{
|
||||
|
||||
@SuppressWarnings ("unchecked")
|
||||
public void put(String key, List<String> value) {
|
||||
JSONArray array = new JSONArray();
|
||||
for (String entry : value)
|
||||
{
|
||||
for (String entry : value) {
|
||||
array.add(entry);
|
||||
}
|
||||
config.put(key, array);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void putArray(String key, JSONArray value)
|
||||
{
|
||||
|
||||
@SuppressWarnings ("unchecked")
|
||||
public void putArray(String key, JSONArray value) {
|
||||
config.put(key, value);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void put(String key, Map<String, String> value)
|
||||
{
|
||||
|
||||
@SuppressWarnings ("unchecked")
|
||||
public void put(String key, Map<String, String> value) {
|
||||
JSONObject object = new JSONObject();
|
||||
for (String valKey : value.keySet())
|
||||
{
|
||||
for (String valKey : value.keySet()) {
|
||||
String valVal = value.get(valKey);
|
||||
object.put(valKey, valVal);
|
||||
}
|
||||
config.put(key, object);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void put(String key, JSONObject value)
|
||||
{
|
||||
|
||||
@SuppressWarnings ("unchecked")
|
||||
public void put(String key, JSONObject value) {
|
||||
config.put(key, value);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void putAll(Map<String, String> entry)
|
||||
{
|
||||
for (String key : entry.keySet())
|
||||
{
|
||||
|
||||
@SuppressWarnings ("unchecked")
|
||||
public void putAll(Map<String, String> entry) {
|
||||
for (String key : entry.keySet()) {
|
||||
String value = entry.get(key);
|
||||
config.put(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean containsKey(String key)
|
||||
{
|
||||
return config.containsKey(key);
|
||||
}
|
||||
|
||||
public String get(String key)
|
||||
{
|
||||
if (containsKey(key))
|
||||
{
|
||||
|
||||
public String get(String key) {
|
||||
if (containsKey(key)) {
|
||||
Object value = config.get(key);
|
||||
if (value instanceof String)
|
||||
{
|
||||
if (value instanceof String) {
|
||||
return (String) value;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getOrDefault(String key, String defaultValue)
|
||||
{
|
||||
if (containsKey(key))
|
||||
{
|
||||
|
||||
public boolean containsKey(String key) {
|
||||
return config.containsKey(key);
|
||||
}
|
||||
|
||||
public String getOrDefault(String key, String defaultValue) {
|
||||
if (containsKey(key)) {
|
||||
Object value = config.get(key);
|
||||
if (value instanceof String)
|
||||
{
|
||||
if (value instanceof String) {
|
||||
return (String) value;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<String> getList(String key)
|
||||
{
|
||||
if (containsKey(key))
|
||||
{
|
||||
|
||||
@SuppressWarnings ("unchecked")
|
||||
public List<String> getList(String key) {
|
||||
if (containsKey(key)) {
|
||||
Object value = config.get(key);
|
||||
if (value instanceof JSONArray)
|
||||
{
|
||||
JSONArray array = (JSONArray) value;
|
||||
if (value instanceof JSONArray) {
|
||||
JSONArray array = (JSONArray) value;
|
||||
List<String> output = new ArrayList<String>();
|
||||
for (String entry : (String[]) array.toArray(new String[0]))
|
||||
{
|
||||
for (String entry : (String[]) array.toArray(new String[0])) {
|
||||
output.add(entry);
|
||||
}
|
||||
return output;
|
||||
@@ -217,34 +172,27 @@ public class Config
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public JSONArray getArray(String key)
|
||||
{
|
||||
if (containsKey(key))
|
||||
{
|
||||
|
||||
public JSONArray getArray(String key) {
|
||||
if (containsKey(key)) {
|
||||
Object value = config.get(key);
|
||||
if (value instanceof JSONArray)
|
||||
{
|
||||
if (value instanceof JSONArray) {
|
||||
JSONArray array = (JSONArray) value;
|
||||
return array;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Map<String, String> getMap(String key)
|
||||
{
|
||||
if (containsKey(key))
|
||||
{
|
||||
|
||||
public Map<String, String> getMap(String key) {
|
||||
if (containsKey(key)) {
|
||||
Object value = config.get(key);
|
||||
if (value instanceof JSONObject)
|
||||
{
|
||||
if (value instanceof JSONObject) {
|
||||
JSONObject object = (JSONObject) value;
|
||||
@SuppressWarnings("unchecked")
|
||||
@SuppressWarnings ("unchecked")
|
||||
Set<Map.Entry<String, String>> entrySet = object.entrySet();
|
||||
Map<String, String> output = new HashMap<String, String>();
|
||||
for (Map.Entry<String, String> entry : entrySet)
|
||||
{
|
||||
for (Map.Entry<String, String> entry : entrySet) {
|
||||
output.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
return output;
|
||||
@@ -252,29 +200,24 @@ public class Config
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public JSONObject getObject(String key)
|
||||
{
|
||||
if (containsKey(key))
|
||||
{
|
||||
|
||||
public JSONObject getObject(String key) {
|
||||
if (containsKey(key)) {
|
||||
Object value = config.get(key);
|
||||
if (value instanceof JSONObject)
|
||||
{
|
||||
if (value instanceof JSONObject) {
|
||||
JSONObject object = (JSONObject) value;
|
||||
return object;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void remove(String key)
|
||||
{
|
||||
|
||||
public void remove(String key) {
|
||||
config.remove(key);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Set<Entry<String, String>> getAll()
|
||||
{
|
||||
|
||||
@SuppressWarnings ("unchecked")
|
||||
public Set<Entry<String, String>> getAll() {
|
||||
return config.entrySet();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,106 +1,79 @@
|
||||
package com.redstoner.misc.mysql;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.Serializable;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.redstoner.misc.Main;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import com.redstoner.misc.Main;
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class JSONManager
|
||||
{
|
||||
public static Map<Serializable, Serializable> getConfiguration(String fileName)
|
||||
{
|
||||
public class JSONManager {
|
||||
public static Map<Serializable, Serializable> getConfiguration(String fileName) {
|
||||
File file = new File(Main.plugin.getDataFolder(), fileName);
|
||||
if (!file.exists())
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!file.exists()) {
|
||||
try {
|
||||
PrintWriter writer = new PrintWriter(file.getAbsolutePath(), "UTF-8");
|
||||
writer.println("{}");
|
||||
writer.close();
|
||||
}
|
||||
catch (FileNotFoundException | UnsupportedEncodingException e)
|
||||
{
|
||||
} catch (FileNotFoundException | UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
try
|
||||
{
|
||||
try {
|
||||
return loadMap(file);
|
||||
}
|
||||
catch (IOException | ParseException e)
|
||||
{
|
||||
} catch (IOException | ParseException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static void saveConfiguration(Map<Serializable, Serializable> config, String fileName)
|
||||
{
|
||||
try
|
||||
{
|
||||
saveMap(new File(Main.plugin.getDataFolder(), fileName), config);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void saveList(File file, List<Serializable> entries) throws IOException
|
||||
{
|
||||
JSONArray array = new JSONArray();
|
||||
array.addAll(entries);
|
||||
FileWriter writer = new FileWriter(file);
|
||||
writer.write(array.toJSONString());
|
||||
writer.close();
|
||||
}
|
||||
|
||||
public static List<Serializable> loadList(File file) throws IOException, ParseException
|
||||
{
|
||||
FileReader read = new FileReader(file);
|
||||
List<Serializable> entries = new ArrayList<>();
|
||||
JSONArray array = (JSONArray) new JSONParser().parse(read);
|
||||
for (Object o : array)
|
||||
{
|
||||
entries.add((Serializable) o);
|
||||
|
||||
public static Map<Serializable, Serializable> loadMap(File file) throws IOException, ParseException {
|
||||
FileReader reader = new FileReader(file);
|
||||
JSONObject map = (JSONObject) new JSONParser().parse(reader);
|
||||
Map<Serializable, Serializable> entries = new HashMap<>();
|
||||
for (Object o : map.keySet()) {
|
||||
entries.put((Serializable) o, (Serializable) map.get(o));
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void saveMap(File file, Map<Serializable, Serializable> entries) throws IOException
|
||||
{
|
||||
|
||||
public static void saveConfiguration(Map<Serializable, Serializable> config, String fileName) {
|
||||
try {
|
||||
saveMap(new File(Main.plugin.getDataFolder(), fileName), config);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings ("unchecked")
|
||||
public static void saveMap(File file, Map<Serializable, Serializable> entries) throws IOException {
|
||||
JSONObject map = new JSONObject();
|
||||
map.putAll(entries);
|
||||
FileWriter writer = new FileWriter(file);
|
||||
writer.write(map.toJSONString());
|
||||
writer.close();
|
||||
}
|
||||
|
||||
public static Map<Serializable, Serializable> loadMap(File file) throws IOException, ParseException
|
||||
{
|
||||
FileReader reader = new FileReader(file);
|
||||
JSONObject map = (JSONObject) new JSONParser().parse(reader);
|
||||
Map<Serializable, Serializable> entries = new HashMap<>();
|
||||
for (Object o : map.keySet())
|
||||
{
|
||||
entries.put((Serializable) o, (Serializable) map.get(o));
|
||||
|
||||
@SuppressWarnings ("unchecked")
|
||||
public static void saveList(File file, List<Serializable> entries) throws IOException {
|
||||
JSONArray array = new JSONArray();
|
||||
array.addAll(entries);
|
||||
FileWriter writer = new FileWriter(file);
|
||||
writer.write(array.toJSONString());
|
||||
writer.close();
|
||||
}
|
||||
|
||||
public static List<Serializable> loadList(File file) throws IOException, ParseException {
|
||||
FileReader read = new FileReader(file);
|
||||
List<Serializable> entries = new ArrayList<>();
|
||||
JSONArray array = (JSONArray) new JSONParser().parse(read);
|
||||
for (Object o : array) {
|
||||
entries.add((Serializable) o);
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
@@ -1,113 +1,86 @@
|
||||
package com.redstoner.misc.mysql;
|
||||
|
||||
import com.redstoner.misc.Main;
|
||||
import com.redstoner.misc.mysql.elements.MysqlDatabase;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import com.redstoner.misc.Main;
|
||||
import com.redstoner.misc.mysql.elements.MysqlDatabase;
|
||||
|
||||
public class MysqlHandler
|
||||
{
|
||||
public class MysqlHandler {
|
||||
public static MysqlHandler INSTANCE;
|
||||
private String url, username, password;
|
||||
|
||||
public MysqlHandler(String hostname, int port, String username, String password)
|
||||
{
|
||||
private String url, username, password;
|
||||
|
||||
public MysqlHandler(String hostname, int port, String username, String password) {
|
||||
this.url = "jdbc:mysql://" + hostname + ":" + port + "/";
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public static void init()
|
||||
{
|
||||
Map<Serializable, Serializable> mysqlCredentials = new HashMap<>();
|
||||
File mysqlCredentialsFile = new File(Main.plugin.getDataFolder(), "mysqlCredentials.json");
|
||||
if (mysqlCredentialsFile.exists())
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
public static void init() {
|
||||
Map<Serializable, Serializable> mysqlCredentials = new HashMap<>();
|
||||
File mysqlCredentialsFile = new File(Main.plugin.getDataFolder(), "mysqlCredentials.json");
|
||||
if (mysqlCredentialsFile.exists()) {
|
||||
try {
|
||||
mysqlCredentials = JSONManager.loadMap(mysqlCredentialsFile);
|
||||
}
|
||||
catch (IOException | ParseException e)
|
||||
{
|
||||
} catch (IOException | ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
Bukkit.getConsoleSender().sendMessage(
|
||||
ChatColor.RED + "MySQL config does not exist, creating an example one, things might (will) break!");
|
||||
mysqlCredentials.put("hostname", "localhost");
|
||||
mysqlCredentials.put("port", "3306");
|
||||
mysqlCredentials.put("username", "your username here");
|
||||
mysqlCredentials.put("password", "your password here");
|
||||
try
|
||||
{
|
||||
try {
|
||||
JSONManager.saveMap(mysqlCredentialsFile, mysqlCredentials);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
String hostname = (String) mysqlCredentials.get("hostname");
|
||||
int port = Integer.valueOf((String) mysqlCredentials.get("port"));
|
||||
int port = Integer.valueOf((String) mysqlCredentials.get("port"));
|
||||
String username = (String) mysqlCredentials.get("username");
|
||||
String password = (String) mysqlCredentials.get("password");
|
||||
INSTANCE = new MysqlHandler(hostname, port, username, password);
|
||||
}
|
||||
|
||||
private Connection getConnection(String databaseName) throws IllegalStateException
|
||||
{
|
||||
|
||||
public MysqlDatabase getDatabase(String databaseName) {
|
||||
return new MysqlDatabase(getConnection(databaseName));
|
||||
}
|
||||
|
||||
private Connection getConnection(String databaseName) throws IllegalStateException {
|
||||
Connection connection = null;
|
||||
try
|
||||
{
|
||||
try {
|
||||
connection = DriverManager.getConnection(url + databaseName, username, password);
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
} catch (SQLException e) {
|
||||
throw new IllegalStateException("Cannot connect to the database!", e);
|
||||
}
|
||||
return connection;
|
||||
}
|
||||
|
||||
public MysqlDatabase getDatabase(String databaseName)
|
||||
{
|
||||
return new MysqlDatabase(getConnection(databaseName));
|
||||
}
|
||||
|
||||
public List<MysqlDatabase> getDatabases()
|
||||
{
|
||||
try
|
||||
{
|
||||
List<MysqlDatabase> databases = new ArrayList<>();
|
||||
Connection connection = DriverManager.getConnection(url.substring(0, url.length()), username, password);
|
||||
DatabaseMetaData metadata = connection.getMetaData();
|
||||
ResultSet queryResults = metadata.getCatalogs();
|
||||
while (queryResults.next())
|
||||
{
|
||||
|
||||
public List<MysqlDatabase> getDatabases() {
|
||||
try {
|
||||
List<MysqlDatabase> databases = new ArrayList<>();
|
||||
Connection connection = DriverManager.getConnection(url.substring(0, url.length()), username, password);
|
||||
DatabaseMetaData metadata = connection.getMetaData();
|
||||
ResultSet queryResults = metadata.getCatalogs();
|
||||
while (queryResults.next()) {
|
||||
String databaseName = queryResults.getString("TABLE_CAT");
|
||||
databases.add(new MysqlDatabase(getConnection(databaseName)));
|
||||
}
|
||||
connection.close();
|
||||
return databases;
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -1,29 +1,25 @@
|
||||
package com.redstoner.misc.mysql;
|
||||
|
||||
import java.sql.CallableStatement;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.sql.*;
|
||||
|
||||
public class MysqlQueryHandler {
|
||||
public static ResultSet queryResult(Connection connection, String query) {
|
||||
try {
|
||||
Statement statement = connection.createStatement();
|
||||
ResultSet results = statement.executeQuery(query);
|
||||
|
||||
ResultSet results = statement.executeQuery(query);
|
||||
|
||||
return results;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static boolean queryNoResult(Connection connection, String query) {
|
||||
try {
|
||||
CallableStatement statement = connection.prepareCall(query);
|
||||
statement.execute();
|
||||
|
||||
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
@@ -2,7 +2,7 @@ package com.redstoner.misc.mysql.elements;
|
||||
|
||||
public enum ConstraintOperator {
|
||||
LESS_THAN, GREATER_THAN, EQUAL, NOT_EQUAL, LESS_THAN_OR_EQUAL, GREATER_THAN_OR_EQUAL;
|
||||
|
||||
|
||||
public String toString() {
|
||||
switch (this) {
|
||||
case LESS_THAN:
|
||||
|
||||
@@ -3,7 +3,7 @@ package com.redstoner.misc.mysql.elements;
|
||||
public class MysqlConstraint {
|
||||
private String fieldName, value;
|
||||
private ConstraintOperator operator;
|
||||
|
||||
|
||||
public MysqlConstraint(String fieldName, ConstraintOperator operator, String value) {
|
||||
this.fieldName = fieldName;
|
||||
this.operator = operator;
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.redstoner.misc.mysql.elements;
|
||||
|
||||
import com.redstoner.misc.mysql.MysqlQueryHandler;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.ResultSet;
|
||||
@@ -7,15 +9,55 @@ import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.redstoner.misc.mysql.MysqlQueryHandler;
|
||||
|
||||
public class MysqlDatabase {
|
||||
private Connection connection;
|
||||
|
||||
|
||||
public MysqlDatabase(Connection connection) {
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
|
||||
public MysqlTable getTable(String name) {
|
||||
return new MysqlTable(this, name);
|
||||
}
|
||||
|
||||
public boolean createTable(String name, MysqlField... description) {
|
||||
return MysqlQueryHandler.queryNoResult(connection, "CREATE TABLE `" + name + "` " + getDescription(description) + ";");
|
||||
}
|
||||
|
||||
private String getDescription(MysqlField... description) {
|
||||
String desc = "(";
|
||||
|
||||
for (int i = 0; i < description.length; i++) {
|
||||
String nil = "";
|
||||
|
||||
if (description[i].canBeNull()) {
|
||||
nil = " NOT NULL";
|
||||
}
|
||||
|
||||
desc += "`" + description[i].getName() + "` " + description[i].getType().getName() + nil;
|
||||
|
||||
if (i < description.length - 1) {
|
||||
desc += ",";
|
||||
}
|
||||
}
|
||||
|
||||
desc += ")";
|
||||
|
||||
return desc;
|
||||
}
|
||||
|
||||
public boolean createTableIfNotExists(String name, MysqlField... description) {
|
||||
return MysqlQueryHandler.queryNoResult(connection, "CREATE TABLE IF NOT EXISTS `" + name + "` " + getDescription(description) + ";");
|
||||
}
|
||||
|
||||
public boolean dropTable(String name) {
|
||||
return MysqlQueryHandler.queryNoResult(connection, "DROP TABLE `" + name + "`;");
|
||||
}
|
||||
|
||||
public boolean drop() {
|
||||
return MysqlQueryHandler.queryNoResult(connection, "DROP DATABASE `" + getName() + "`;");
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
try {
|
||||
return connection.getCatalog();
|
||||
@@ -24,67 +66,25 @@ public class MysqlDatabase {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public MysqlTable getTable(String name) {
|
||||
return new MysqlTable(this, name);
|
||||
}
|
||||
|
||||
public boolean createTable(String name, MysqlField... description) {
|
||||
return MysqlQueryHandler.queryNoResult(connection, "CREATE TABLE `" + name + "` " + getDescription(description) + ";");
|
||||
}
|
||||
|
||||
public boolean createTableIfNotExists(String name, MysqlField... description) {
|
||||
return MysqlQueryHandler.queryNoResult(connection, "CREATE TABLE IF NOT EXISTS `" + name + "` " + getDescription(description) + ";");
|
||||
}
|
||||
|
||||
public boolean dropTable(String name) {
|
||||
return MysqlQueryHandler.queryNoResult(connection, "DROP TABLE `" + name + "`;");
|
||||
}
|
||||
|
||||
public boolean drop() {
|
||||
return MysqlQueryHandler.queryNoResult(connection, "DROP DATABASE `" + getName() + "`;");
|
||||
}
|
||||
|
||||
|
||||
public List<MysqlTable> getTables() {
|
||||
try {
|
||||
List<MysqlTable> tables = new ArrayList<>();
|
||||
DatabaseMetaData metadata = connection.getMetaData();
|
||||
ResultSet queryResults = metadata.getTables(null, null, "%", null);
|
||||
|
||||
List<MysqlTable> tables = new ArrayList<>();
|
||||
DatabaseMetaData metadata = connection.getMetaData();
|
||||
ResultSet queryResults = metadata.getTables(null, null, "%", null);
|
||||
|
||||
while (queryResults.next()) {
|
||||
tables.add(new MysqlTable(this, queryResults.getString(3)));
|
||||
}
|
||||
|
||||
|
||||
return tables;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected Connection getConnection() {
|
||||
return connection;
|
||||
}
|
||||
|
||||
private String getDescription(MysqlField... description) {
|
||||
String desc = "(";
|
||||
|
||||
for (int i = 0; i < description.length; i++) {
|
||||
String nil = "";
|
||||
|
||||
if (description[i].canBeNull()) {
|
||||
nil = " NOT NULL";
|
||||
}
|
||||
|
||||
desc += "`" + description[i].getName() + "` " + description[i].getType().getName() + nil;
|
||||
|
||||
if (i < description.length - 1) {
|
||||
desc += ",";
|
||||
}
|
||||
}
|
||||
|
||||
desc += ")";
|
||||
|
||||
return desc;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,30 +3,30 @@ package com.redstoner.misc.mysql.elements;
|
||||
import com.redstoner.misc.mysql.types.MysqlType;
|
||||
|
||||
public class MysqlField {
|
||||
private String name;
|
||||
private String name;
|
||||
private MysqlType type;
|
||||
private boolean canBeNull;
|
||||
|
||||
private boolean canBeNull;
|
||||
|
||||
public MysqlField(String name, MysqlType type, boolean canBeNull) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
this.canBeNull = canBeNull;
|
||||
}
|
||||
|
||||
|
||||
public MysqlField(String name, String type, boolean canBeNull) {
|
||||
this.name = name;
|
||||
this.type = MysqlType.getTypeFromString(type);
|
||||
this.canBeNull = canBeNull;
|
||||
}
|
||||
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
public MysqlType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
|
||||
public boolean canBeNull() {
|
||||
return canBeNull;
|
||||
}
|
||||
|
||||
@@ -5,11 +5,11 @@ import java.sql.SQLException;
|
||||
|
||||
public class MysqlResult {
|
||||
private ResultSet results;
|
||||
|
||||
|
||||
public MysqlResult(ResultSet results) {
|
||||
this.results = results;
|
||||
}
|
||||
|
||||
|
||||
public Object getObject(int columnIndex, Class<?> type) throws SQLException {
|
||||
return results.getObject(columnIndex, type);
|
||||
}
|
||||
|
||||
@@ -1,133 +1,113 @@
|
||||
package com.redstoner.misc.mysql.elements;
|
||||
|
||||
import com.redstoner.misc.mysql.MysqlQueryHandler;
|
||||
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.redstoner.misc.mysql.MysqlQueryHandler;
|
||||
|
||||
public class MysqlTable
|
||||
{
|
||||
public class MysqlTable {
|
||||
private MysqlDatabase database;
|
||||
private String name;
|
||||
|
||||
public MysqlTable(MysqlDatabase database, String name)
|
||||
{
|
||||
private String name;
|
||||
|
||||
public MysqlTable(MysqlDatabase database, String name) {
|
||||
this.database = database;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName()
|
||||
{
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public MysqlField[] describe()
|
||||
{
|
||||
try
|
||||
{
|
||||
List<MysqlField> description = new ArrayList<>();
|
||||
DatabaseMetaData metadata = database.getConnection().getMetaData();
|
||||
ResultSet queryResults = metadata.getColumns(null, null, name, null);
|
||||
while (queryResults.next())
|
||||
{
|
||||
description.add(new MysqlField(queryResults.getString(4),
|
||||
|
||||
public boolean insert(String... values) {
|
||||
MysqlField[] description = describe();
|
||||
if (values.length > 0 && values.length == description.length) {
|
||||
String val = "(\"" + String.join("\",\"", values) + "\")";
|
||||
return MysqlQueryHandler.queryNoResult(
|
||||
database.getConnection(),
|
||||
"INSERT INTO `" + name + "` VALUES " + val + ";"
|
||||
);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public MysqlField[] describe() {
|
||||
try {
|
||||
List<MysqlField> description = new ArrayList<>();
|
||||
DatabaseMetaData metadata = database.getConnection().getMetaData();
|
||||
ResultSet queryResults = metadata.getColumns(null, null, name, null);
|
||||
while (queryResults.next()) {
|
||||
description.add(new MysqlField(
|
||||
queryResults.getString(4),
|
||||
queryResults.getString(6).split(" ")[0] + "(" + queryResults.getString(7) + ")",
|
||||
queryResults.getBoolean(11)));
|
||||
queryResults.getBoolean(11)
|
||||
));
|
||||
}
|
||||
return description.toArray(new MysqlField[0]);
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean insert(String... values)
|
||||
{
|
||||
MysqlField[] description = describe();
|
||||
if (values.length > 0 && values.length == description.length)
|
||||
{
|
||||
String val = "(\"" + String.join("\",\"", values) + "\")";
|
||||
return MysqlQueryHandler.queryNoResult(database.getConnection(),
|
||||
"INSERT INTO `" + name + "` VALUES " + val + ";");
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public Object[] get(String fieldName, MysqlConstraint... constraints)
|
||||
{
|
||||
ResultSet results = MysqlQueryHandler.queryResult(database.getConnection(),
|
||||
"SELECT " + fieldName + " FROM `" + name + "`" + getConstraints(constraints) + ";");
|
||||
|
||||
public Object[] get(String fieldName, MysqlConstraint... constraints) {
|
||||
ResultSet results = MysqlQueryHandler.queryResult(
|
||||
database.getConnection(),
|
||||
"SELECT " + fieldName + " FROM `" + name + "`" + getConstraints(constraints) + ";"
|
||||
);
|
||||
List<Object> resObj = new ArrayList<>();
|
||||
try
|
||||
{
|
||||
while (results.next())
|
||||
{
|
||||
try {
|
||||
while (results.next()) {
|
||||
resObj.add(results.getObject(1));
|
||||
}
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return new Object[0];
|
||||
}
|
||||
return resObj.toArray(new Object[0]);
|
||||
}
|
||||
|
||||
public Object[] get(String statement)
|
||||
{
|
||||
ResultSet results = MysqlQueryHandler.queryResult(database.getConnection(), statement);
|
||||
List<Object> resObj = new ArrayList<>();
|
||||
try
|
||||
{
|
||||
while (results.next())
|
||||
{
|
||||
resObj.add(results.getObject(1));
|
||||
}
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
return new Object[0];
|
||||
}
|
||||
return resObj.toArray(new Object[0]);
|
||||
}
|
||||
|
||||
public boolean delete(MysqlConstraint... constraints)
|
||||
{
|
||||
return MysqlQueryHandler.queryNoResult(database.getConnection(),
|
||||
"DELETE FROM `" + name + "`" + getConstraints(constraints) + ";");
|
||||
}
|
||||
|
||||
public boolean drop()
|
||||
{
|
||||
return MysqlQueryHandler.queryNoResult(database.getConnection(), "DROP TABLE `" + name + "`;");
|
||||
}
|
||||
|
||||
private String getConstraints(MysqlConstraint... constraints)
|
||||
{
|
||||
|
||||
private String getConstraints(MysqlConstraint... constraints) {
|
||||
String cons = "";
|
||||
if (constraints.length > 0)
|
||||
{
|
||||
if (constraints.length > 0) {
|
||||
cons += " WHERE ";
|
||||
for (int i = 0; i < constraints.length; i++)
|
||||
{
|
||||
for (int i = 0; i < constraints.length; i++) {
|
||||
MysqlConstraint constraint = constraints[i];
|
||||
cons += constraint.getFieldName() + constraint.getOperator().toString() + "\"" + constraint.getValue()
|
||||
+ "\"";
|
||||
if (i < constraints.length - 1)
|
||||
{
|
||||
+ "\"";
|
||||
if (i < constraints.length - 1) {
|
||||
cons += " AND ";
|
||||
}
|
||||
}
|
||||
}
|
||||
return cons;
|
||||
}
|
||||
|
||||
public Object[] get(String statement) {
|
||||
ResultSet results = MysqlQueryHandler.queryResult(database.getConnection(), statement);
|
||||
List<Object> resObj = new ArrayList<>();
|
||||
try {
|
||||
while (results.next()) {
|
||||
resObj.add(results.getObject(1));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return new Object[0];
|
||||
}
|
||||
return resObj.toArray(new Object[0]);
|
||||
}
|
||||
|
||||
public boolean delete(MysqlConstraint... constraints) {
|
||||
return MysqlQueryHandler.queryNoResult(
|
||||
database.getConnection(),
|
||||
"DELETE FROM `" + name + "`" + getConstraints(constraints) + ";"
|
||||
);
|
||||
}
|
||||
|
||||
public boolean drop() {
|
||||
return MysqlQueryHandler.queryNoResult(database.getConnection(), "DROP TABLE `" + name + "`;");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,45 +1,21 @@
|
||||
package com.redstoner.misc.mysql.types;
|
||||
|
||||
import com.redstoner.misc.mysql.types.date.Date;
|
||||
import com.redstoner.misc.mysql.types.date.DateTime;
|
||||
import com.redstoner.misc.mysql.types.date.Time;
|
||||
import com.redstoner.misc.mysql.types.date.TimeStamp;
|
||||
import com.redstoner.misc.mysql.types.date.Year;
|
||||
import com.redstoner.misc.mysql.types.number.BigInt;
|
||||
import com.redstoner.misc.mysql.types.number.Decimal;
|
||||
import com.redstoner.misc.mysql.types.date.*;
|
||||
import com.redstoner.misc.mysql.types.number.Double;
|
||||
import com.redstoner.misc.mysql.types.number.Float;
|
||||
import com.redstoner.misc.mysql.types.number.Int;
|
||||
import com.redstoner.misc.mysql.types.number.MediumInt;
|
||||
import com.redstoner.misc.mysql.types.number.SmallInt;
|
||||
import com.redstoner.misc.mysql.types.number.TinyInt;
|
||||
import com.redstoner.misc.mysql.types.text.Blob;
|
||||
import com.redstoner.misc.mysql.types.text.Char;
|
||||
import com.redstoner.misc.mysql.types.number.*;
|
||||
import com.redstoner.misc.mysql.types.text.Enum;
|
||||
import com.redstoner.misc.mysql.types.text.LongBlob;
|
||||
import com.redstoner.misc.mysql.types.text.LongText;
|
||||
import com.redstoner.misc.mysql.types.text.MediumBlob;
|
||||
import com.redstoner.misc.mysql.types.text.MediumText;
|
||||
import com.redstoner.misc.mysql.types.text.Set;
|
||||
import com.redstoner.misc.mysql.types.text.Text;
|
||||
import com.redstoner.misc.mysql.types.text.TinyText;
|
||||
import com.redstoner.misc.mysql.types.text.VarChar;
|
||||
import com.redstoner.misc.mysql.types.text.*;
|
||||
|
||||
public abstract class MysqlType
|
||||
{
|
||||
public abstract String getName();
|
||||
|
||||
public static MysqlType getTypeFromString(String type)
|
||||
{
|
||||
public abstract class MysqlType {
|
||||
public static MysqlType getTypeFromString(String type) {
|
||||
String[] splitType = type.split("\\(");
|
||||
String toSwitch = splitType[0].toUpperCase();
|
||||
String value = "";
|
||||
if (type.contains("(") && type.endsWith(")"))
|
||||
{
|
||||
String toSwitch = splitType[0].toUpperCase();
|
||||
String value = "";
|
||||
if (type.contains("(") && type.endsWith(")")) {
|
||||
value = splitType[1].substring(0, splitType[1].length() - 1);
|
||||
}
|
||||
switch (toSwitch)
|
||||
{
|
||||
switch (toSwitch) {
|
||||
case "CHAR":
|
||||
return new Char(Integer.valueOf(value));
|
||||
case "ENUM":
|
||||
@@ -93,4 +69,6 @@ public abstract class MysqlType
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public abstract String getName();
|
||||
}
|
||||
|
||||
@@ -4,11 +4,11 @@ import com.redstoner.misc.mysql.types.MysqlType;
|
||||
|
||||
public class Int extends MysqlType {
|
||||
private int maxSize;
|
||||
|
||||
|
||||
public Int(int maxSize) {
|
||||
this.maxSize = maxSize;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "INT(" + maxSize + ")";
|
||||
|
||||
@@ -4,11 +4,11 @@ import com.redstoner.misc.mysql.types.MysqlType;
|
||||
|
||||
public class Char extends MysqlType {
|
||||
private int size;
|
||||
|
||||
|
||||
public Char(int size) {
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "CHAR(" + size + ")";
|
||||
|
||||
@@ -4,24 +4,24 @@ import com.redstoner.misc.mysql.types.MysqlType;
|
||||
|
||||
public class Enum extends MysqlType {
|
||||
private String[] possibleValues;
|
||||
|
||||
|
||||
public Enum(String... possibleValues) {
|
||||
this.possibleValues = possibleValues;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
String name = "ENUM(";
|
||||
|
||||
|
||||
for (int i = 0; i < possibleValues.length; i++) {
|
||||
name += "'" + possibleValues[i] + "'";
|
||||
|
||||
|
||||
if (i != possibleValues.length - 1) {
|
||||
name += ",";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return name + ")";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -4,24 +4,24 @@ import com.redstoner.misc.mysql.types.MysqlType;
|
||||
|
||||
public class Set extends MysqlType {
|
||||
private String[] possibleValues;
|
||||
|
||||
|
||||
public Set(String... possibleValues) {
|
||||
this.possibleValues = possibleValues;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
String name = "SET(";
|
||||
|
||||
|
||||
for (int i = 0; i < possibleValues.length; i++) {
|
||||
name += "'" + possibleValues[i] + "'";
|
||||
|
||||
|
||||
if (i != possibleValues.length - 1) {
|
||||
name += ",";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return name + ")";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -4,11 +4,11 @@ import com.redstoner.misc.mysql.types.MysqlType;
|
||||
|
||||
public class VarChar extends MysqlType {
|
||||
private int maxSize;
|
||||
|
||||
|
||||
public VarChar(int maxSize) {
|
||||
this.maxSize = maxSize;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "VARCHAR(" + maxSize + ")";
|
||||
|
||||
Reference in New Issue
Block a user