Integrated the UpdateQuery into the UpdateManager

This commit is contained in:
mathias 2021-08-26 23:30:48 +02:00
parent 75dc54b17a
commit 3cb1f2d151
No known key found for this signature in database
GPG Key ID: 8950DF62139C852A

View File

@ -1,9 +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.UpdateQuery;
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.HashMap; import java.util.HashMap;
public class UpdateManager { public class UpdateManager {
@ -34,7 +36,7 @@ public class UpdateManager {
*/ */
public UpdateManager(MySQLConnection connection, String tableName) { public UpdateManager(MySQLConnection connection, String tableName) {
this.connection = connection; this.connection = connection;
toTable(tableName); this.tableName = tableName;
this.whereList = new HashMap<>(); this.whereList = new HashMap<>();
this.setList = new HashMap<>(); this.setList = new HashMap<>();
} }
@ -50,18 +52,6 @@ public class UpdateManager {
return this; return this;
} }
/**
* Get the parameters for the SQL statement
*
* @return the parameters
*/
private ArrayList<Object> getTempParams() {
ArrayList<Object> tempParams = new ArrayList<>();
setList.forEach((str, obj) -> tempParams.add(obj));
whereList.forEach((str, obj) -> tempParams.add(obj));
return tempParams;
}
/** /**
* Add a 'where'-clause * Add a 'where'-clause
* *
@ -90,31 +80,20 @@ public class UpdateManager {
* Update the entries with your current conditions * Update the entries with your current conditions
*/ */
public void execute() { public void execute() {
connection.update(prepareUpdateStatement(), getTempParams().toArray()); connection.update(build());
} }
/** /**
* Get the Query of the 'update'-Statement * Builds the current statement
* *
* @return the Statement * @return the current statement
*/ */
private String prepareUpdateStatement() { public SQLQuery build() {
StringBuilder query = new StringBuilder().append("UPDATE ").append(tableName); return connection.createQuery(UpdateQuery.class)
if (!setList.isEmpty()) query.append(" SET "); .addParameter(QueryParameter.TABLE_NAME, tableName)
.addParameter(QueryParameter.WHERE_LIST, whereList)
for (int i = 0; i < setList.size(); i++) { .addParameter(QueryParameter.SET_LIST, setList)
if (i > 0) query.append(", "); .build();
query.append(setList.keySet().toArray()[i]);
}
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();
} }
} }