From 721acca534110682f7e423891b51c286985878af Mon Sep 17 00:00:00 2001 From: Pepich Date: Wed, 1 Feb 2017 22:01:30 +0100 Subject: [PATCH] Copied over mysql adapter --- .../redstoner/misc/mysql/FolderRegistry.java | 68 +++++++++++ src/com/redstoner/misc/mysql/JSONManager.java | 105 ++++++++++++++++ .../redstoner/misc/mysql/MysqlHandler.java | 114 ++++++++++++++++++ .../misc/mysql/MysqlQueryHandler.java | 33 +++++ .../mysql/elements/ConstraintOperator.java | 24 ++++ .../misc/mysql/elements/MysqlConstraint.java | 24 ++++ .../misc/mysql/elements/MysqlDatabase.java | 90 ++++++++++++++ .../misc/mysql/elements/MysqlField.java | 33 +++++ .../misc/mysql/elements/MysqlResult.java | 16 +++ .../misc/mysql/elements/MysqlTable.java | 96 +++++++++++++++ .../redstoner/misc/mysql/types/MysqlType.java | 96 +++++++++++++++ .../redstoner/misc/mysql/types/date/Date.java | 10 ++ .../misc/mysql/types/date/DateTime.java | 10 ++ .../redstoner/misc/mysql/types/date/Time.java | 10 ++ .../misc/mysql/types/date/TimeStamp.java | 10 ++ .../redstoner/misc/mysql/types/date/Year.java | 10 ++ .../misc/mysql/types/number/BigInt.java | 12 ++ .../misc/mysql/types/number/Decimal.java | 10 ++ .../misc/mysql/types/number/Double.java | 10 ++ .../misc/mysql/types/number/Float.java | 10 ++ .../misc/mysql/types/number/Int.java | 16 +++ .../misc/mysql/types/number/MediumInt.java | 12 ++ .../misc/mysql/types/number/SmallInt.java | 12 ++ .../misc/mysql/types/number/TinyInt.java | 12 ++ .../redstoner/misc/mysql/types/text/Blob.java | 10 ++ .../redstoner/misc/mysql/types/text/Char.java | 16 +++ .../redstoner/misc/mysql/types/text/Enum.java | 27 +++++ .../misc/mysql/types/text/LongBlob.java | 8 ++ .../misc/mysql/types/text/LongText.java | 8 ++ .../misc/mysql/types/text/MediumBlob.java | 8 ++ .../misc/mysql/types/text/MediumText.java | 8 ++ .../redstoner/misc/mysql/types/text/Set.java | 27 +++++ .../redstoner/misc/mysql/types/text/Text.java | 10 ++ .../misc/mysql/types/text/TinyText.java | 8 ++ .../misc/mysql/types/text/VarChar.java | 16 +++ 35 files changed, 989 insertions(+) create mode 100644 src/com/redstoner/misc/mysql/FolderRegistry.java create mode 100644 src/com/redstoner/misc/mysql/JSONManager.java create mode 100644 src/com/redstoner/misc/mysql/MysqlHandler.java create mode 100644 src/com/redstoner/misc/mysql/MysqlQueryHandler.java create mode 100644 src/com/redstoner/misc/mysql/elements/ConstraintOperator.java create mode 100644 src/com/redstoner/misc/mysql/elements/MysqlConstraint.java create mode 100644 src/com/redstoner/misc/mysql/elements/MysqlDatabase.java create mode 100644 src/com/redstoner/misc/mysql/elements/MysqlField.java create mode 100644 src/com/redstoner/misc/mysql/elements/MysqlResult.java create mode 100644 src/com/redstoner/misc/mysql/elements/MysqlTable.java create mode 100644 src/com/redstoner/misc/mysql/types/MysqlType.java create mode 100644 src/com/redstoner/misc/mysql/types/date/Date.java create mode 100644 src/com/redstoner/misc/mysql/types/date/DateTime.java create mode 100644 src/com/redstoner/misc/mysql/types/date/Time.java create mode 100644 src/com/redstoner/misc/mysql/types/date/TimeStamp.java create mode 100644 src/com/redstoner/misc/mysql/types/date/Year.java create mode 100644 src/com/redstoner/misc/mysql/types/number/BigInt.java create mode 100644 src/com/redstoner/misc/mysql/types/number/Decimal.java create mode 100644 src/com/redstoner/misc/mysql/types/number/Double.java create mode 100644 src/com/redstoner/misc/mysql/types/number/Float.java create mode 100644 src/com/redstoner/misc/mysql/types/number/Int.java create mode 100644 src/com/redstoner/misc/mysql/types/number/MediumInt.java create mode 100644 src/com/redstoner/misc/mysql/types/number/SmallInt.java create mode 100644 src/com/redstoner/misc/mysql/types/number/TinyInt.java create mode 100644 src/com/redstoner/misc/mysql/types/text/Blob.java create mode 100644 src/com/redstoner/misc/mysql/types/text/Char.java create mode 100644 src/com/redstoner/misc/mysql/types/text/Enum.java create mode 100644 src/com/redstoner/misc/mysql/types/text/LongBlob.java create mode 100644 src/com/redstoner/misc/mysql/types/text/LongText.java create mode 100644 src/com/redstoner/misc/mysql/types/text/MediumBlob.java create mode 100644 src/com/redstoner/misc/mysql/types/text/MediumText.java create mode 100644 src/com/redstoner/misc/mysql/types/text/Set.java create mode 100644 src/com/redstoner/misc/mysql/types/text/Text.java create mode 100644 src/com/redstoner/misc/mysql/types/text/TinyText.java create mode 100644 src/com/redstoner/misc/mysql/types/text/VarChar.java diff --git a/src/com/redstoner/misc/mysql/FolderRegistry.java b/src/com/redstoner/misc/mysql/FolderRegistry.java new file mode 100644 index 0000000..2c19087 --- /dev/null +++ b/src/com/redstoner/misc/mysql/FolderRegistry.java @@ -0,0 +1,68 @@ +package com.redstoner.misc.mysql; + +import java.io.File; + +import com.redstoner.misc.Main; + +public class FolderRegistry +{ + public static File moduleFolder, configFolder, commandFolder, tempFolder; + + public static void init() + { + File dataFolder = Main.plugin.getDataFolder(); + moduleFolder = new File(dataFolder + "/modules"); + configFolder = new File(dataFolder + "/config"); + commandFolder = new File(dataFolder + "/commands"); + tempFolder = new File(dataFolder + "/temp"); + if (!moduleFolder.exists()) + { + moduleFolder.mkdirs(); + } + if (!configFolder.exists()) + { + configFolder.mkdirs(); + } + if (!commandFolder.exists()) + { + commandFolder.mkdirs(); + } + if (tempFolder.exists()) + { + deleteFolder(tempFolder); + } + if (!tempFolder.exists()) + { + tempFolder.mkdirs(); + } + } + + public static boolean deleteFolder(File folder) + { + for (File file : folder.listFiles()) + { + if (file.isDirectory()) + { + if (!deleteFolder(file)) + { + return false; + } + } + else + { + if (!file.delete()) + { + return false; + } + } + } + if (folder.delete()) + { + return true; + } + else + { + return false; + } + } +} diff --git a/src/com/redstoner/misc/mysql/JSONManager.java b/src/com/redstoner/misc/mysql/JSONManager.java new file mode 100644 index 0000000..b40676a --- /dev/null +++ b/src/com/redstoner/misc/mysql/JSONManager.java @@ -0,0 +1,105 @@ +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 org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + +public class JSONManager +{ + public static Map getConfiguration(String fileName) + { + File file = new File(FolderRegistry.configFolder, fileName); + if (!file.exists()) + { + try + { + PrintWriter writer = new PrintWriter(file.getAbsolutePath(), "UTF-8"); + writer.println("{}"); + writer.close(); + } + catch (FileNotFoundException | UnsupportedEncodingException e) + { + e.printStackTrace(); + } + } + try + { + return loadMap(file); + } + catch (IOException | ParseException e) + { + e.printStackTrace(); + return null; + } + } + + public static void saveConfiguration(Map config, String fileName) + { + try + { + saveMap(new File(FolderRegistry.configFolder, fileName), config); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + + @SuppressWarnings("unchecked") + public static void saveList(File file, List entries) throws IOException + { + JSONArray array = new JSONArray(); + array.addAll(entries); + FileWriter writer = new FileWriter(file); + writer.write(array.toJSONString()); + writer.close(); + } + + public static List loadList(File file) throws IOException, ParseException + { + FileReader read = new FileReader(file); + List entries = new ArrayList<>(); + JSONArray array = (JSONArray) new JSONParser().parse(read); + for (Object o : array) + { + entries.add((Serializable) o); + } + return entries; + } + + @SuppressWarnings("unchecked") + public static void saveMap(File file, Map entries) throws IOException + { + JSONObject map = new JSONObject(); + map.putAll(entries); + FileWriter writer = new FileWriter(file); + writer.write(map.toJSONString()); + writer.close(); + } + + public static Map loadMap(File file) throws IOException, ParseException + { + FileReader reader = new FileReader(file); + JSONObject map = (JSONObject) new JSONParser().parse(reader); + Map entries = new HashMap<>(); + for (Object o : map.keySet()) + { + entries.put((Serializable) o, (Serializable) map.get(o)); + } + return entries; + } +} diff --git a/src/com/redstoner/misc/mysql/MysqlHandler.java b/src/com/redstoner/misc/mysql/MysqlHandler.java new file mode 100644 index 0000000..6245c5d --- /dev/null +++ b/src/com/redstoner/misc/mysql/MysqlHandler.java @@ -0,0 +1,114 @@ +package com.redstoner.misc.mysql; + +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.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.mysql.elements.MysqlDatabase; + +public class MysqlHandler +{ + public static MysqlHandler INSTANCE; + 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 mysqlCredentials = new HashMap<>(); + File mysqlCredentialsFile = new File(FolderRegistry.configFolder, "mysqlCredentials.json"); + if (mysqlCredentialsFile.exists()) + { + try + { + mysqlCredentials = JSONManager.loadMap(mysqlCredentialsFile); + } + catch (IOException | ParseException e) + { + e.printStackTrace(); + } + } + 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 + { + JSONManager.saveMap(mysqlCredentialsFile, mysqlCredentials); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + String hostname = (String) mysqlCredentials.get("hostname"); + 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 + { + Connection connection = null; + try + { + connection = DriverManager.getConnection(url + databaseName, username, password); + } + 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 getDatabases() + { + try + { + List 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) + { + e.printStackTrace(); + return null; + } + } +} diff --git a/src/com/redstoner/misc/mysql/MysqlQueryHandler.java b/src/com/redstoner/misc/mysql/MysqlQueryHandler.java new file mode 100644 index 0000000..f89a08a --- /dev/null +++ b/src/com/redstoner/misc/mysql/MysqlQueryHandler.java @@ -0,0 +1,33 @@ +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; + +public class MysqlQueryHandler { + public static ResultSet queryResult(Connection connection, String query) { + try { + Statement statement = connection.createStatement(); + 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(); + return false; + } + } +} diff --git a/src/com/redstoner/misc/mysql/elements/ConstraintOperator.java b/src/com/redstoner/misc/mysql/elements/ConstraintOperator.java new file mode 100644 index 0000000..45cb33c --- /dev/null +++ b/src/com/redstoner/misc/mysql/elements/ConstraintOperator.java @@ -0,0 +1,24 @@ +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: + return "<"; + case GREATER_THAN: + return ">"; + case EQUAL: + return "="; + case NOT_EQUAL: + return "!="; + case LESS_THAN_OR_EQUAL: + return "<="; + case GREATER_THAN_OR_EQUAL: + return ">="; + default: + return "="; + } + } +} diff --git a/src/com/redstoner/misc/mysql/elements/MysqlConstraint.java b/src/com/redstoner/misc/mysql/elements/MysqlConstraint.java new file mode 100644 index 0000000..d651344 --- /dev/null +++ b/src/com/redstoner/misc/mysql/elements/MysqlConstraint.java @@ -0,0 +1,24 @@ +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; + this.value = value; + } + + public String getFieldName() { + return fieldName; + } + + public String getValue() { + return value; + } + + public ConstraintOperator getOperator() { + return operator; + } +} diff --git a/src/com/redstoner/misc/mysql/elements/MysqlDatabase.java b/src/com/redstoner/misc/mysql/elements/MysqlDatabase.java new file mode 100644 index 0000000..91c0fe4 --- /dev/null +++ b/src/com/redstoner/misc/mysql/elements/MysqlDatabase.java @@ -0,0 +1,90 @@ +package com.redstoner.misc.mysql.elements; + +import java.sql.Connection; +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 MysqlDatabase { + private Connection connection; + + public MysqlDatabase(Connection connection) { + this.connection = connection; + } + + public String getName() { + try { + return connection.getCatalog(); + } catch (SQLException e) { + e.printStackTrace(); + 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 getTables() { + try { + List 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; + } +} diff --git a/src/com/redstoner/misc/mysql/elements/MysqlField.java b/src/com/redstoner/misc/mysql/elements/MysqlField.java new file mode 100644 index 0000000..61cba2e --- /dev/null +++ b/src/com/redstoner/misc/mysql/elements/MysqlField.java @@ -0,0 +1,33 @@ +package com.redstoner.misc.mysql.elements; + +import com.redstoner.misc.mysql.types.MysqlType; + +public class MysqlField { + private String name; + private MysqlType type; + 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; + } +} diff --git a/src/com/redstoner/misc/mysql/elements/MysqlResult.java b/src/com/redstoner/misc/mysql/elements/MysqlResult.java new file mode 100644 index 0000000..6db0769 --- /dev/null +++ b/src/com/redstoner/misc/mysql/elements/MysqlResult.java @@ -0,0 +1,16 @@ +package com.redstoner.misc.mysql.elements; + +import java.sql.ResultSet; +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); + } +} diff --git a/src/com/redstoner/misc/mysql/elements/MysqlTable.java b/src/com/redstoner/misc/mysql/elements/MysqlTable.java new file mode 100644 index 0000000..fa0008e --- /dev/null +++ b/src/com/redstoner/misc/mysql/elements/MysqlTable.java @@ -0,0 +1,96 @@ +package com.redstoner.misc.mysql.elements; + +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 { + private MysqlDatabase database; + private String name; + + public MysqlTable(MysqlDatabase database, String name) { + this.database = database; + this.name = name; + } + + public String getName() { + return this.name; + } + + public MysqlField[] describe() { + try { + List 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))); + } + + return description.toArray(new MysqlField[0]); + } 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) + ";"); + + List 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) { + String cons = ""; + + if (constraints.length > 0) { + cons += " WHERE "; + + 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) { + cons += " AND "; + } + } + } + + return cons; + } +} diff --git a/src/com/redstoner/misc/mysql/types/MysqlType.java b/src/com/redstoner/misc/mysql/types/MysqlType.java new file mode 100644 index 0000000..86413f9 --- /dev/null +++ b/src/com/redstoner/misc/mysql/types/MysqlType.java @@ -0,0 +1,96 @@ +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.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.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; + +public abstract class MysqlType +{ + public abstract String getName(); + + public static MysqlType getTypeFromString(String type) + { + String[] splitType = type.split("\\("); + String toSwitch = splitType[0].toUpperCase(); + String value = ""; + if (type.contains("(") && type.endsWith(")")) + { + value = splitType[1].substring(0, splitType[1].length() - 1); + } + switch (toSwitch) + { + case "CHAR": + return new Char(Integer.valueOf(value)); + case "ENUM": + return new Enum(value.replaceAll("'", "").split(",")); + case "VARCHAR": + return new VarChar(Integer.valueOf(value)); + case "SET": + return new Set(value.replaceAll("'", "").split(",")); + case "BLOB": + return new Blob(); + case "TEXT": + return new Text(); + case "MEDIUMBLOB": + return new MediumBlob(); + case "LONGBLOB": + return new LongBlob(); + case "TINYTEXT": + return new TinyText(); + case "MEDIUMTEXT": + return new MediumText(); + case "LONGTEXT": + return new LongText(); + case "INT": + return new Int(Integer.valueOf(value)); + case "TINYINT": + return new TinyInt(Integer.valueOf(value)); + case "SMALLINT": + return new SmallInt(Integer.valueOf(value)); + case "MEDIUMINT": + return new MediumInt(Integer.valueOf(value)); + case "BIGINT": + return new BigInt(Integer.valueOf(value)); + case "BIT": + return new TinyInt(1); + case "FLOAT": + return new Float(); + case "DOUBLE": + return new Double(); + case "DECIMAL": + return new Decimal(); + case "DATE": + return new Date(); + case "DATETIME": + return new DateTime(); + case "TIME": + return new Time(); + case "TIMESTAMP": + return new TimeStamp(); + case "YEAR": + return new Year(); + } + return null; + } +} diff --git a/src/com/redstoner/misc/mysql/types/date/Date.java b/src/com/redstoner/misc/mysql/types/date/Date.java new file mode 100644 index 0000000..9943333 --- /dev/null +++ b/src/com/redstoner/misc/mysql/types/date/Date.java @@ -0,0 +1,10 @@ +package com.redstoner.misc.mysql.types.date; + +import com.redstoner.misc.mysql.types.MysqlType; + +public class Date extends MysqlType { + @Override + public String getName() { + return "DATE"; + } +} diff --git a/src/com/redstoner/misc/mysql/types/date/DateTime.java b/src/com/redstoner/misc/mysql/types/date/DateTime.java new file mode 100644 index 0000000..b4d9623 --- /dev/null +++ b/src/com/redstoner/misc/mysql/types/date/DateTime.java @@ -0,0 +1,10 @@ +package com.redstoner.misc.mysql.types.date; + +import com.redstoner.misc.mysql.types.MysqlType; + +public class DateTime extends MysqlType { + @Override + public String getName() { + return "DATETIME"; + } +} diff --git a/src/com/redstoner/misc/mysql/types/date/Time.java b/src/com/redstoner/misc/mysql/types/date/Time.java new file mode 100644 index 0000000..4ead72c --- /dev/null +++ b/src/com/redstoner/misc/mysql/types/date/Time.java @@ -0,0 +1,10 @@ +package com.redstoner.misc.mysql.types.date; + +import com.redstoner.misc.mysql.types.MysqlType; + +public class Time extends MysqlType { + @Override + public String getName() { + return "TIME"; + } +} diff --git a/src/com/redstoner/misc/mysql/types/date/TimeStamp.java b/src/com/redstoner/misc/mysql/types/date/TimeStamp.java new file mode 100644 index 0000000..56205af --- /dev/null +++ b/src/com/redstoner/misc/mysql/types/date/TimeStamp.java @@ -0,0 +1,10 @@ +package com.redstoner.misc.mysql.types.date; + +import com.redstoner.misc.mysql.types.MysqlType; + +public class TimeStamp extends MysqlType { + @Override + public String getName() { + return "TIMESTAMP"; + } +} diff --git a/src/com/redstoner/misc/mysql/types/date/Year.java b/src/com/redstoner/misc/mysql/types/date/Year.java new file mode 100644 index 0000000..4c2ce1a --- /dev/null +++ b/src/com/redstoner/misc/mysql/types/date/Year.java @@ -0,0 +1,10 @@ +package com.redstoner.misc.mysql.types.date; + +import com.redstoner.misc.mysql.types.MysqlType; + +public class Year extends MysqlType { + @Override + public String getName() { + return "YEAR"; + } +} diff --git a/src/com/redstoner/misc/mysql/types/number/BigInt.java b/src/com/redstoner/misc/mysql/types/number/BigInt.java new file mode 100644 index 0000000..71086fd --- /dev/null +++ b/src/com/redstoner/misc/mysql/types/number/BigInt.java @@ -0,0 +1,12 @@ +package com.redstoner.misc.mysql.types.number; + +public class BigInt extends Int { + public BigInt(int maxSize) { + super(maxSize); + } + + @Override + public String getName() { + return "BIG" + super.getName(); + } +} \ No newline at end of file diff --git a/src/com/redstoner/misc/mysql/types/number/Decimal.java b/src/com/redstoner/misc/mysql/types/number/Decimal.java new file mode 100644 index 0000000..4e4dbb6 --- /dev/null +++ b/src/com/redstoner/misc/mysql/types/number/Decimal.java @@ -0,0 +1,10 @@ +package com.redstoner.misc.mysql.types.number; + +import com.redstoner.misc.mysql.types.MysqlType; + +public class Decimal extends MysqlType { + @Override + public String getName() { + return "DECIMAL"; + } +} diff --git a/src/com/redstoner/misc/mysql/types/number/Double.java b/src/com/redstoner/misc/mysql/types/number/Double.java new file mode 100644 index 0000000..b4b1dda --- /dev/null +++ b/src/com/redstoner/misc/mysql/types/number/Double.java @@ -0,0 +1,10 @@ +package com.redstoner.misc.mysql.types.number; + +import com.redstoner.misc.mysql.types.MysqlType; + +public class Double extends MysqlType { + @Override + public String getName() { + return "DOUBLE"; + } +} diff --git a/src/com/redstoner/misc/mysql/types/number/Float.java b/src/com/redstoner/misc/mysql/types/number/Float.java new file mode 100644 index 0000000..ea3047e --- /dev/null +++ b/src/com/redstoner/misc/mysql/types/number/Float.java @@ -0,0 +1,10 @@ +package com.redstoner.misc.mysql.types.number; + +import com.redstoner.misc.mysql.types.MysqlType; + +public class Float extends MysqlType { + @Override + public String getName() { + return "FLOAT"; + } +} diff --git a/src/com/redstoner/misc/mysql/types/number/Int.java b/src/com/redstoner/misc/mysql/types/number/Int.java new file mode 100644 index 0000000..4256f7b --- /dev/null +++ b/src/com/redstoner/misc/mysql/types/number/Int.java @@ -0,0 +1,16 @@ +package com.redstoner.misc.mysql.types.number; + +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 + ")"; + } +} \ No newline at end of file diff --git a/src/com/redstoner/misc/mysql/types/number/MediumInt.java b/src/com/redstoner/misc/mysql/types/number/MediumInt.java new file mode 100644 index 0000000..fbcb0f4 --- /dev/null +++ b/src/com/redstoner/misc/mysql/types/number/MediumInt.java @@ -0,0 +1,12 @@ +package com.redstoner.misc.mysql.types.number; + +public class MediumInt extends Int { + public MediumInt(int maxSize) { + super(maxSize); + } + + @Override + public String getName() { + return "MEDIUM" + super.getName(); + } +} \ No newline at end of file diff --git a/src/com/redstoner/misc/mysql/types/number/SmallInt.java b/src/com/redstoner/misc/mysql/types/number/SmallInt.java new file mode 100644 index 0000000..01bf97d --- /dev/null +++ b/src/com/redstoner/misc/mysql/types/number/SmallInt.java @@ -0,0 +1,12 @@ +package com.redstoner.misc.mysql.types.number; + +public class SmallInt extends Int { + public SmallInt(int maxSize) { + super(maxSize); + } + + @Override + public String getName() { + return "SMALL" + super.getName(); + } +} \ No newline at end of file diff --git a/src/com/redstoner/misc/mysql/types/number/TinyInt.java b/src/com/redstoner/misc/mysql/types/number/TinyInt.java new file mode 100644 index 0000000..63ad700 --- /dev/null +++ b/src/com/redstoner/misc/mysql/types/number/TinyInt.java @@ -0,0 +1,12 @@ +package com.redstoner.misc.mysql.types.number; + +public class TinyInt extends Int { + public TinyInt(int maxSize) { + super(maxSize); + } + + @Override + public String getName() { + return "TINY" + super.getName(); + } +} \ No newline at end of file diff --git a/src/com/redstoner/misc/mysql/types/text/Blob.java b/src/com/redstoner/misc/mysql/types/text/Blob.java new file mode 100644 index 0000000..d56ee45 --- /dev/null +++ b/src/com/redstoner/misc/mysql/types/text/Blob.java @@ -0,0 +1,10 @@ +package com.redstoner.misc.mysql.types.text; + +import com.redstoner.misc.mysql.types.MysqlType; + +public class Blob extends MysqlType { + @Override + public String getName() { + return "BLOB"; + } +} diff --git a/src/com/redstoner/misc/mysql/types/text/Char.java b/src/com/redstoner/misc/mysql/types/text/Char.java new file mode 100644 index 0000000..ece068c --- /dev/null +++ b/src/com/redstoner/misc/mysql/types/text/Char.java @@ -0,0 +1,16 @@ +package com.redstoner.misc.mysql.types.text; + +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 + ")"; + } +} diff --git a/src/com/redstoner/misc/mysql/types/text/Enum.java b/src/com/redstoner/misc/mysql/types/text/Enum.java new file mode 100644 index 0000000..e68476d --- /dev/null +++ b/src/com/redstoner/misc/mysql/types/text/Enum.java @@ -0,0 +1,27 @@ +package com.redstoner.misc.mysql.types.text; + +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 + ")"; + } + +} diff --git a/src/com/redstoner/misc/mysql/types/text/LongBlob.java b/src/com/redstoner/misc/mysql/types/text/LongBlob.java new file mode 100644 index 0000000..802caed --- /dev/null +++ b/src/com/redstoner/misc/mysql/types/text/LongBlob.java @@ -0,0 +1,8 @@ +package com.redstoner.misc.mysql.types.text; + +public class LongBlob extends Blob { + @Override + public String getName() { + return "LONG" + super.getName(); + } +} \ No newline at end of file diff --git a/src/com/redstoner/misc/mysql/types/text/LongText.java b/src/com/redstoner/misc/mysql/types/text/LongText.java new file mode 100644 index 0000000..5149ed2 --- /dev/null +++ b/src/com/redstoner/misc/mysql/types/text/LongText.java @@ -0,0 +1,8 @@ +package com.redstoner.misc.mysql.types.text; + +public class LongText extends Text { + @Override + public String getName() { + return "LONG" + super.getName(); + } +} \ No newline at end of file diff --git a/src/com/redstoner/misc/mysql/types/text/MediumBlob.java b/src/com/redstoner/misc/mysql/types/text/MediumBlob.java new file mode 100644 index 0000000..6ae25a7 --- /dev/null +++ b/src/com/redstoner/misc/mysql/types/text/MediumBlob.java @@ -0,0 +1,8 @@ +package com.redstoner.misc.mysql.types.text; + +public class MediumBlob extends Blob { + @Override + public String getName() { + return "MEDIUM" + super.getName(); + } +} \ No newline at end of file diff --git a/src/com/redstoner/misc/mysql/types/text/MediumText.java b/src/com/redstoner/misc/mysql/types/text/MediumText.java new file mode 100644 index 0000000..a0f6525 --- /dev/null +++ b/src/com/redstoner/misc/mysql/types/text/MediumText.java @@ -0,0 +1,8 @@ +package com.redstoner.misc.mysql.types.text; + +public class MediumText extends Text { + @Override + public String getName() { + return "MEDIUM" + super.getName(); + } +} \ No newline at end of file diff --git a/src/com/redstoner/misc/mysql/types/text/Set.java b/src/com/redstoner/misc/mysql/types/text/Set.java new file mode 100644 index 0000000..4e12ce6 --- /dev/null +++ b/src/com/redstoner/misc/mysql/types/text/Set.java @@ -0,0 +1,27 @@ +package com.redstoner.misc.mysql.types.text; + +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 + ")"; + } + +} diff --git a/src/com/redstoner/misc/mysql/types/text/Text.java b/src/com/redstoner/misc/mysql/types/text/Text.java new file mode 100644 index 0000000..7d1a38c --- /dev/null +++ b/src/com/redstoner/misc/mysql/types/text/Text.java @@ -0,0 +1,10 @@ +package com.redstoner.misc.mysql.types.text; + +import com.redstoner.misc.mysql.types.MysqlType; + +public class Text extends MysqlType { + @Override + public String getName() { + return "TEXT"; + } +} diff --git a/src/com/redstoner/misc/mysql/types/text/TinyText.java b/src/com/redstoner/misc/mysql/types/text/TinyText.java new file mode 100644 index 0000000..e78160c --- /dev/null +++ b/src/com/redstoner/misc/mysql/types/text/TinyText.java @@ -0,0 +1,8 @@ +package com.redstoner.misc.mysql.types.text; + +public class TinyText extends Text { + @Override + public String getName() { + return "TINY" + super.getName(); + } +} diff --git a/src/com/redstoner/misc/mysql/types/text/VarChar.java b/src/com/redstoner/misc/mysql/types/text/VarChar.java new file mode 100644 index 0000000..cb28ad1 --- /dev/null +++ b/src/com/redstoner/misc/mysql/types/text/VarChar.java @@ -0,0 +1,16 @@ +package com.redstoner.misc.mysql.types.text; + +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 + ")"; + } +} \ No newline at end of file