🛠️ Code cleanup & added many new features #1

Merged
gnmyt merged 121 commits from features/code-cleanup into master 2021-09-02 13:34:00 +00:00
Showing only changes of commit b9139e77ab - Show all commits

View File

@ -1,6 +1,10 @@
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.SelectionQuery;
import de.gnmyt.sqltoolkit.querybuilder.QueryBuilder;
import de.gnmyt.sqltoolkit.querybuilder.QueryParameter;
import de.gnmyt.sqltoolkit.querybuilder.SQLQuery;
import org.slf4j.Logger; import org.slf4j.Logger;
import java.sql.ResultSet; import java.sql.ResultSet;
@ -11,56 +15,43 @@ import java.util.HashMap;
public class SelectionManager { public class SelectionManager {
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> WHERE_LIST = new HashMap<>();
private final ArrayList<String> optionalQuery; private final ArrayList<String> SELECTION_LIST = new ArrayList<>();
private int limit;
private String tableName; private String limit = "";
private String tableName = "default";
/** /**
* Basic constructor for selection * Advanced constructor of the {@link SelectionManager} with a prefilled table name
* *
* @param connection The current Connection * @param connection The current connection
* @param tableName The table name * @param tableName The table name
*/ */
public SelectionManager(MySQLConnection connection, String tableName) { public SelectionManager(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;
} }
/** /**
* Basic constructor for selection * Basic constructor of the {@link SelectionManager}
* *
* @param connection The current Connection * @param connection The current connection
*/ */
public SelectionManager(MySQLConnection connection) { public SelectionManager(MySQLConnection connection) {
this.whereList = new HashMap<>();
this.databaseParameters = new ArrayList<>();
this.optionalQuery = new ArrayList<>();
this.connection = connection; this.connection = connection;
} }
/** public SelectionManager select(String... selection) {
* Get the temp-generated parameters SELECTION_LIST.addAll(Arrays.asList(selection));
* return this;
* @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 of the selection
* *
* @param tableName The table name * @param tableName The new table name
* @return this class * @return this class
*/ */
public SelectionManager from(String tableName) { public SelectionManager from(String tableName) {
@ -76,7 +67,7 @@ public class SelectionManager {
* @return this class * @return this class
*/ */
public SelectionManager where(String column, Object value) { public SelectionManager where(String column, Object value) {
whereList.put(column, value); WHERE_LIST.put(column, value);
return this; return this;
} }
@ -87,7 +78,19 @@ public class SelectionManager {
* @return this class * @return this class
*/ */
public SelectionManager limit(int limit) { public SelectionManager limit(int limit) {
this.limit = limit; this.limit = String.valueOf(limit);
return this;
}
/**
* Sets the limit of the rows
*
* @param startRow The first row that should be counted
* @param limit The new limit
* @return this class
*/
public SelectionManager limit(int startRow, int limit) {
this.limit = startRow + "," + limit;
return this; return this;
} }
@ -97,7 +100,7 @@ public class SelectionManager {
* @return ResultManager * @return ResultManager
*/ */
public ResultManager getResult() { public ResultManager getResult() {
return connection.getResult(prepareStatement(), getTempParams().toArray()); return connection.getResult(build());
} }
/** /**
@ -106,40 +109,23 @@ public class SelectionManager {
* @return ResultSet * @return ResultSet
*/ */
public ResultSet getResultSet() { public ResultSet getResultSet() {
return connection.getResultSet(prepareStatement(), getTempParams().toArray()); return connection.getResultSet(build());
} }
/** /**
* Adding another factors * Builds the query
* *
* @param query MySQL Query * @return the built query
* @param params Optional parameters for the Query
* @return this class
*/ */
public SelectionManager add(String query, Object... params) { private SQLQuery build() {
optionalQuery.add(query); QueryBuilder query = connection.createQuery(SelectionQuery.class)
this.databaseParameters.addAll(Arrays.asList(params)); .addParameter(QueryParameter.TABLE_NAME, tableName)
return this; .addParameter(QueryParameter.LIMIT, limit)
} .addParameter(QueryParameter.WHERE_LIST, WHERE_LIST);
/** if (!SELECTION_LIST.isEmpty()) query.addParameter(QueryParameter.SELECT_LIST, SELECTION_LIST);
* Get the current statement query
*
* @return the current statement query
*/
private String prepareStatement() {
StringBuilder query = new StringBuilder().append("SELECT * FROM ").append(tableName).append(" ");
for (int i = 0; i < whereList.size(); i++) { return query.build();
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();
} }
} }