Cleaned up code of the DataBaseSelection & renamed it to SelectionManager
This commit is contained in:
@ -7,9 +7,8 @@ import java.sql.ResultSet;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public class DataBaseSelection {
|
||||
public class SelectionManager {
|
||||
|
||||
private final Logger LOG = MySQLConnection.LOG;
|
||||
|
||||
@ -26,7 +25,7 @@ public class DataBaseSelection {
|
||||
* @param connection The current Connection
|
||||
* @param tableName The table name
|
||||
*/
|
||||
public DataBaseSelection(MySQLConnection connection, String tableName) {
|
||||
public SelectionManager(MySQLConnection connection, String tableName) {
|
||||
this.whereList = new HashMap<>();
|
||||
this.databaseParameters = new ArrayList<>();
|
||||
this.optionalQuery = new ArrayList<>();
|
||||
@ -39,7 +38,7 @@ public class DataBaseSelection {
|
||||
*
|
||||
* @param connection The current Connection
|
||||
*/
|
||||
public DataBaseSelection(MySQLConnection connection) {
|
||||
public SelectionManager(MySQLConnection connection) {
|
||||
this.whereList = new HashMap<>();
|
||||
this.databaseParameters = new ArrayList<>();
|
||||
this.optionalQuery = new ArrayList<>();
|
||||
@ -64,7 +63,7 @@ public class DataBaseSelection {
|
||||
* @param tableName The table name
|
||||
* @return this class
|
||||
*/
|
||||
public DataBaseSelection from(String tableName) {
|
||||
public SelectionManager from(String tableName) {
|
||||
this.tableName = tableName;
|
||||
return this;
|
||||
}
|
||||
@ -76,7 +75,7 @@ public class DataBaseSelection {
|
||||
* @param value Table value name
|
||||
* @return this class
|
||||
*/
|
||||
public DataBaseSelection where(String column, Object value) {
|
||||
public SelectionManager where(String column, Object value) {
|
||||
whereList.put(column, value);
|
||||
return this;
|
||||
}
|
||||
@ -87,7 +86,7 @@ public class DataBaseSelection {
|
||||
* @param limit The new limit
|
||||
* @return this class
|
||||
*/
|
||||
public DataBaseSelection limit(int limit) {
|
||||
public SelectionManager limit(int limit) {
|
||||
this.limit = limit;
|
||||
return this;
|
||||
}
|
||||
@ -98,7 +97,7 @@ public class DataBaseSelection {
|
||||
* @param limit The new limit
|
||||
* @return this class
|
||||
*/
|
||||
public DataBaseSelection limit(String limit) {
|
||||
public SelectionManager limit(String limit) {
|
||||
this.limit = Integer.parseInt(limit);
|
||||
return this;
|
||||
}
|
||||
@ -121,24 +120,6 @@ public class DataBaseSelection {
|
||||
return connection.getResultSet(processStatement(), getTempParams().toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Debug the current Statement
|
||||
*
|
||||
* @return this class
|
||||
*/
|
||||
public DataBaseSelection printStatement() {
|
||||
StringBuilder messageBuilder = new StringBuilder();
|
||||
messageBuilder.append(processStatement());
|
||||
messageBuilder.append(" | params» ");
|
||||
AtomicBoolean added = new AtomicBoolean(false);
|
||||
getTempParams().forEach(v -> {
|
||||
messageBuilder.append((added.get()) ? ", " : "").append(v);
|
||||
added.set(true);
|
||||
});
|
||||
LOG.debug("Statement: " + messageBuilder);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adding another factors
|
||||
*
|
||||
@ -146,7 +127,7 @@ public class DataBaseSelection {
|
||||
* @param params Optional parameters for the Query
|
||||
* @return this class
|
||||
*/
|
||||
public DataBaseSelection add(String query, Object... params) {
|
||||
public SelectionManager add(String query, Object... params) {
|
||||
optionalQuery.add(query);
|
||||
this.databaseParameters.addAll(Arrays.asList(params));
|
||||
return this;
|
||||
@ -158,18 +139,18 @@ public class DataBaseSelection {
|
||||
* @return the current statement query
|
||||
*/
|
||||
private String processStatement() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("SELECT * FROM ").append(tableName).append(" ");
|
||||
AtomicBoolean used = new AtomicBoolean(false);
|
||||
whereList.forEach((k, v) -> {
|
||||
if (!used.get()) sb.append("WHERE ");
|
||||
else sb.append("AND ");
|
||||
used.set(true);
|
||||
sb.append(k).append(" = ? ");
|
||||
});
|
||||
if (limit != 0) sb.append("LIMIT ").append(limit);
|
||||
optionalQuery.forEach(v -> sb.append(" ").append(v).append(" "));
|
||||
return sb.toString();
|
||||
StringBuilder query = new StringBuilder().append("SELECT * FROM ").append(tableName).append(" ");
|
||||
|
||||
for (int i = 0; i < whereList.size(); i++) {
|
||||
if (i > 0) query.append("WHERE ");
|
||||
else query.append("AND ");
|
||||
|
||||
query.append(whereList.keySet().toArray()[i]).append(" = ? ");
|
||||
}
|
||||
|
||||
if (limit != 0) query.append("LIMIT ").append(limit);
|
||||
optionalQuery.forEach(v -> query.append(" ").append(v).append(" "));
|
||||
return query.toString();
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user