Added an InsertManager for quickly inserting to a table without writing a SQL Query

This commit is contained in:
mathias 2021-06-19 10:15:07 +02:00
parent fa5fb6f26e
commit 81e5fdecaa
2 changed files with 105 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package de.gnmyt.SQLToolkit.drivers;
import de.gnmyt.SQLToolkit.api.SQLConsumer;
import de.gnmyt.SQLToolkit.manager.InsertManager;
import de.gnmyt.SQLToolkit.manager.ResultManager;
import de.gnmyt.SQLToolkit.manager.DataBaseSelection;
import de.gnmyt.SQLToolkit.manager.UpdateManager;
@ -233,6 +234,23 @@ public class MySQLConnection {
return new DataBaseSelection(this);
}
/**
* Get the InsertManager for easier inserting to a table
* @return InsertManager
*/
public InsertManager insert() {
return new InsertManager(this);
}
/**
* Get the InsertManager for easier inserting to a table
* @param tableName The name of the table you want to insert a object
* @return InsertManager
*/
public InsertManager insertFrom(String tableName) {
return new InsertManager(this, tableName);
}
/**
* Connect with your MySQL server
* @return this class

View File

@ -0,0 +1,87 @@
package de.gnmyt.SQLToolkit.manager;
import de.gnmyt.SQLToolkit.drivers.MySQLConnection;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicBoolean;
public class InsertManager {
private MySQLConnection connection;
private String tableName;
private HashMap<String, Object> values;
/**
* Basic constructor for the InsertManager
* @param connection The current connection
*/
public InsertManager(MySQLConnection connection) {
this.connection = connection;
values = new HashMap<>();
}
/**
* Constructor with direct tableName integration
* @param connection The current connection
* @param tableName The name of the table
*/
public InsertManager(MySQLConnection connection, String tableName) {
this.connection = connection;
this.tableName = tableName;
values = new HashMap<>();
}
/**
* Optionally add the tableName / Change the name of the table
* @param tableName The name of the table
*/
public void from(String tableName) {
this.tableName = tableName;
}
/**
* Query value
* @param fieldName The name of the column
* @param value The value of the object you want to insert
* @return this class
*/
public InsertManager value(String fieldName, Object value) {
values.put(fieldName, value);
return this;
}
/**
* Prepares the SQL Query
* @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);
for (int i=0; i < values.size(); i++) {
if (used_values.get()) query.append(", ");
used_values.set(true);
query.append("?");
}
if (values.size() > 0) query.append(")");
return query.toString();
}
/**
* Execute the current SQL query
* @return this class
*/
public InsertManager execute() {
connection.update(prepareStatement(), values.values().toArray());
return this;
}
}