Cleaned up code of the insert manager
This commit is contained in:
parent
387f9d7b35
commit
3f2987b576
@ -3,7 +3,6 @@ package de.gnmyt.SQLToolkit.manager;
|
||||
import de.gnmyt.SQLToolkit.drivers.MySQLConnection;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public class InsertManager {
|
||||
|
||||
@ -60,26 +59,26 @@ public class InsertManager {
|
||||
* @return the SQL Query
|
||||
*/
|
||||
public String prepareStatement() {
|
||||
StringBuilder query = new StringBuilder();
|
||||
query.append("INSERT INTO ").append(tableName).append(" ").append("(");
|
||||
AtomicBoolean used = new AtomicBoolean(false);
|
||||
values.forEach((field, object) -> {
|
||||
if (used.get()) query.append(", ");
|
||||
used.set(true);
|
||||
query.append("`").append(field).append("`");
|
||||
});
|
||||
query.append(")");
|
||||
if (values.size() > 0) query.append(" VALUES (");
|
||||
AtomicBoolean used_values = new AtomicBoolean(false);
|
||||
StringBuilder query = new StringBuilder().append("INSERT INTO ").append(tableName).append(" (");
|
||||
|
||||
for (int i = 0; i < values.size(); i++) {
|
||||
if (used_values.get()) query.append(", ");
|
||||
used_values.set(true);
|
||||
if (i > 0) query.append(", ");
|
||||
query.append("`").append(values.keySet().toArray()[i]).append("`");
|
||||
}
|
||||
|
||||
query.append(")").append(values.size() > 0 ? "VALUES (" : "");
|
||||
|
||||
for (int i = 0; i < values.size(); i++) {
|
||||
if (i > 0) query.append(", ");
|
||||
query.append("?");
|
||||
}
|
||||
|
||||
if (values.size() > 0) query.append(")");
|
||||
|
||||
return query.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Execute the current SQL query
|
||||
*
|
||||
|
Reference in New Issue
Block a user