Added a way to other operations than "WHERE" selectors
This commit is contained in:
parent
3d83c1dff8
commit
9eec4c9240
@ -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<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)));
|
||||
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<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 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<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 + "`;");
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user