From 9eec4c92408c730a469c55f6ecddcad84041dadf Mon Sep 17 00:00:00 2001 From: Pepich Date: Sat, 18 Feb 2017 16:33:18 +0100 Subject: [PATCH] Added a way to other operations than "WHERE" selectors --- .../misc/mysql/elements/MysqlTable.java | 109 ++++++++++++------ 1 file changed, 73 insertions(+), 36 deletions(-) diff --git a/src/com/redstoner/misc/mysql/elements/MysqlTable.java b/src/com/redstoner/misc/mysql/elements/MysqlTable.java index fa0008e..6656fcd 100644 --- a/src/com/redstoner/misc/mysql/elements/MysqlTable.java +++ b/src/com/redstoner/misc/mysql/elements/MysqlTable.java @@ -8,89 +8,126 @@ 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) { + 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 { + 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))); + 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) { + } + catch (SQLException e) + { e.printStackTrace(); return null; } } - public boolean insert(String... values) { + public boolean insert(String... values) + { MysqlField[] description = describe(); - - if (values.length > 0 && values.length == description.length) { + 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 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 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 boolean delete(MysqlConstraint... constraints) { - return MysqlQueryHandler.queryNoResult(database.getConnection(), "DELETE FROM `" + name + "`" + getConstraints(constraints) + ";"); + public Object[] get(String statement) + { + ResultSet results = MysqlQueryHandler.queryResult(database.getConnection(), statement); + 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 drop() { + 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) { + cons += constraint.getFieldName() + constraint.getOperator().toString() + "\"" + constraint.getValue() + + "\""; + if (i < constraints.length - 1) + { cons += " AND "; } } } - return cons; } }