Integrated the DeletionQuery into the DeletionManager
This commit is contained in:
parent
3897733d6d
commit
4a6f0c6901
@ -1,10 +1,11 @@
|
|||||||
package de.gnmyt.sqltoolkit.manager;
|
package de.gnmyt.sqltoolkit.manager;
|
||||||
|
|
||||||
import de.gnmyt.sqltoolkit.drivers.MySQLConnection;
|
import de.gnmyt.sqltoolkit.drivers.MySQLConnection;
|
||||||
|
import de.gnmyt.sqltoolkit.queries.DeletionQuery;
|
||||||
|
import de.gnmyt.sqltoolkit.querybuilder.QueryParameter;
|
||||||
|
import de.gnmyt.sqltoolkit.querybuilder.SQLQuery;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
public class DeletionManager {
|
public class DeletionManager {
|
||||||
@ -12,9 +13,8 @@ public class DeletionManager {
|
|||||||
private final Logger LOG = MySQLConnection.LOG;
|
private final Logger LOG = MySQLConnection.LOG;
|
||||||
|
|
||||||
private final MySQLConnection connection;
|
private final MySQLConnection connection;
|
||||||
private final HashMap<String, Object> whereList;
|
|
||||||
private final ArrayList<Object> databaseParameters;
|
private final HashMap<String, Object> whereList = new HashMap<>();
|
||||||
private final ArrayList<String> optionalQuery;
|
|
||||||
private String tableName;
|
private String tableName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -24,11 +24,8 @@ public class DeletionManager {
|
|||||||
* @param tableName The table name
|
* @param tableName The table name
|
||||||
*/
|
*/
|
||||||
public DeletionManager(MySQLConnection connection, String tableName) {
|
public DeletionManager(MySQLConnection connection, String tableName) {
|
||||||
this.whereList = new HashMap<>();
|
|
||||||
this.databaseParameters = new ArrayList<>();
|
|
||||||
this.optionalQuery = new ArrayList<>();
|
|
||||||
this.connection = connection;
|
this.connection = connection;
|
||||||
from(tableName);
|
this.tableName = tableName;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -37,24 +34,9 @@ public class DeletionManager {
|
|||||||
* @param connection The current connection
|
* @param connection The current connection
|
||||||
*/
|
*/
|
||||||
public DeletionManager(MySQLConnection connection) {
|
public DeletionManager(MySQLConnection connection) {
|
||||||
this.whereList = new HashMap<>();
|
|
||||||
this.databaseParameters = new ArrayList<>();
|
|
||||||
this.optionalQuery = new ArrayList<>();
|
|
||||||
this.connection = connection;
|
this.connection = connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the temp-generated parameters
|
|
||||||
*
|
|
||||||
* @return ArrayList of parameters
|
|
||||||
*/
|
|
||||||
private ArrayList<Object> getTempParams() {
|
|
||||||
ArrayList<Object> tempParameters = new ArrayList<>();
|
|
||||||
whereList.forEach((k, v) -> tempParameters.add(v));
|
|
||||||
tempParameters.addAll(databaseParameters);
|
|
||||||
return tempParameters;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the table name
|
* Sets the table name
|
||||||
*
|
*
|
||||||
@ -82,38 +64,18 @@ public class DeletionManager {
|
|||||||
* Executes the 'delete'-statement
|
* Executes the 'delete'-statement
|
||||||
*/
|
*/
|
||||||
public void execute() {
|
public void execute() {
|
||||||
connection.update(prepareStatement(), getTempParams().toArray());
|
connection.update(build());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adding another factors
|
* Builds the current statement
|
||||||
*
|
*
|
||||||
* @param query MySQL Query
|
* @return the current statement
|
||||||
* @param params Optional parameters for the Query
|
|
||||||
* @return this class
|
|
||||||
*/
|
*/
|
||||||
public DeletionManager add(String query, Object... params) {
|
private SQLQuery build() {
|
||||||
optionalQuery.add(query);
|
return connection.createQuery(DeletionQuery.class)
|
||||||
this.databaseParameters.addAll(Arrays.asList(params));
|
.addParameter(QueryParameter.TABLE_NAME, tableName)
|
||||||
return this;
|
.addParameter(QueryParameter.WHERE_LIST, whereList).build();
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the current statement query
|
|
||||||
*
|
|
||||||
* @return the current statement query
|
|
||||||
*/
|
|
||||||
private String prepareStatement() {
|
|
||||||
StringBuilder query = new StringBuilder().append("DELETE FROM ").append(tableName);
|
|
||||||
|
|
||||||
if (!whereList.isEmpty()) query.append(" WHERE ");
|
|
||||||
|
|
||||||
for (int i = 0; i < whereList.size(); i++) {
|
|
||||||
if (i > 0) query.append(" AND ");
|
|
||||||
query.append(whereList.keySet().toArray()[i]).append(" = ?");
|
|
||||||
}
|
|
||||||
|
|
||||||
return query.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user