0

Added a way to other operations than "WHERE" selectors

This commit is contained in:
Pepich 2017-02-18 16:33:18 +01:00
parent 3d83c1dff8
commit 9eec4c9240

View File

@ -8,89 +8,126 @@ import java.util.List;
import com.redstoner.misc.mysql.MysqlQueryHandler; import com.redstoner.misc.mysql.MysqlQueryHandler;
public class MysqlTable { public class MysqlTable
{
private MysqlDatabase database; private MysqlDatabase database;
private String name; private String name;
public MysqlTable(MysqlDatabase database, String name) { public MysqlTable(MysqlDatabase database, String name)
{
this.database = database; this.database = database;
this.name = name; this.name = name;
} }
public String getName() { public String getName()
{
return this.name; return this.name;
} }
public MysqlField[] describe() { public MysqlField[] describe()
try { {
try
{
List<MysqlField> description = new ArrayList<>(); List<MysqlField> description = new ArrayList<>();
DatabaseMetaData metadata = database.getConnection().getMetaData(); DatabaseMetaData metadata = database.getConnection().getMetaData();
ResultSet queryResults = metadata.getColumns(null, null, name, null); ResultSet queryResults = metadata.getColumns(null, null, name, null);
while (queryResults.next())
while (queryResults.next()) { {
description.add(new MysqlField(queryResults.getString(4), queryResults.getString(6).split(" ")[0] + "(" + queryResults.getString(7) + ")", queryResults.getBoolean(11))); description.add(new MysqlField(queryResults.getString(4),
queryResults.getString(6).split(" ")[0] + "(" + queryResults.getString(7) + ")",
queryResults.getBoolean(11)));
} }
return description.toArray(new MysqlField[0]); return description.toArray(new MysqlField[0]);
} catch (SQLException e) { }
catch (SQLException e)
{
e.printStackTrace(); e.printStackTrace();
return null; return null;
} }
} }
public boolean insert(String... values) { public boolean insert(String... values)
{
MysqlField[] description = describe(); MysqlField[] description = describe();
if (values.length > 0 && values.length == description.length)
if (values.length > 0 && values.length == description.length) { {
String val = "(\"" + String.join("\",\"", values) + "\")"; String val = "(\"" + String.join("\",\"", values) + "\")";
return MysqlQueryHandler.queryNoResult(database.getConnection(),
return MysqlQueryHandler.queryNoResult(database.getConnection(), "INSERT INTO `" + name + "` VALUES " + val + ";"); "INSERT INTO `" + name + "` VALUES " + val + ";");
} else { }
else
{
return false; return false;
} }
} }
public Object[] get(String fieldName, MysqlConstraint... constraints) { public Object[] get(String fieldName, MysqlConstraint... constraints)
ResultSet results = MysqlQueryHandler.queryResult(database.getConnection(), "SELECT " + fieldName + " FROM `" + name + "`" + getConstraints(constraints) + ";"); {
ResultSet results = MysqlQueryHandler.queryResult(database.getConnection(),
"SELECT " + fieldName + " FROM `" + name + "`" + getConstraints(constraints) + ";");
List<Object> resObj = new ArrayList<>(); List<Object> resObj = new ArrayList<>();
try { try
while (results.next()) { {
while (results.next())
{
resObj.add(results.getObject(1)); resObj.add(results.getObject(1));
} }
} catch (SQLException e) { }
catch (SQLException e)
{
e.printStackTrace(); e.printStackTrace();
return new Object[0]; return new Object[0];
} }
return resObj.toArray(new Object[0]); return resObj.toArray(new Object[0]);
} }
public boolean delete(MysqlConstraint... constraints) { public Object[] get(String statement)
return MysqlQueryHandler.queryNoResult(database.getConnection(), "DELETE FROM `" + name + "`" + getConstraints(constraints) + ";"); {
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 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 + "`;"); return MysqlQueryHandler.queryNoResult(database.getConnection(), "DROP TABLE `" + name + "`;");
} }
private String getConstraints(MysqlConstraint... constraints) { private String getConstraints(MysqlConstraint... constraints)
{
String cons = ""; String cons = "";
if (constraints.length > 0)
if (constraints.length > 0) { {
cons += " WHERE "; cons += " WHERE ";
for (int i = 0; i < constraints.length; i++)
for (int i = 0; i < constraints.length; i++) { {
MysqlConstraint constraint = constraints[i]; MysqlConstraint constraint = constraints[i];
cons += constraint.getFieldName() + constraint.getOperator().toString() + "\"" + constraint.getValue()
cons += constraint.getFieldName() + constraint.getOperator().toString() + "\"" + constraint.getValue() + "\""; + "\"";
if (i < constraints.length - 1)
if (i < constraints.length - 1) { {
cons += " AND "; cons += " AND ";
} }
} }
} }
return cons; return cons;
} }
} }