Cleaned up code

This commit is contained in:
2021-08-19 16:44:15 +02:00
parent 961778bb0e
commit 2fecd3b402
11 changed files with 301 additions and 161 deletions

View File

@ -22,8 +22,9 @@ public class DataBaseSelection {
/**
* Basic constructor for selection
*
* @param connection The current Connection
* @param tableName The table name
* @param tableName The table name
*/
public DataBaseSelection(MySQLConnection connection, String tableName) {
this.whereList = new HashMap<>();
@ -35,6 +36,7 @@ public class DataBaseSelection {
/**
* Basic constructor for selection
*
* @param connection The current Connection
*/
public DataBaseSelection(MySQLConnection connection) {
@ -46,6 +48,7 @@ public class DataBaseSelection {
/**
* Get the temp-generated parameters
*
* @return ArrayList of parameters
*/
private ArrayList<Object> getTempParams() {
@ -57,6 +60,7 @@ public class DataBaseSelection {
/**
* Sets the table name
*
* @param tableName The table name
* @return this class
*/
@ -67,8 +71,9 @@ public class DataBaseSelection {
/**
* Adding another 'where'-statement
*
* @param column Table column name
* @param value Table value name
* @param value Table value name
* @return this class
*/
public DataBaseSelection where(String column, Object value) {
@ -78,6 +83,7 @@ public class DataBaseSelection {
/**
* Sets the limit of the rows
*
* @param limit The new limit
* @return this class
*/
@ -88,6 +94,7 @@ public class DataBaseSelection {
/**
* Sets the limit of the rows
*
* @param limit The new limit
* @return this class
*/
@ -98,6 +105,7 @@ public class DataBaseSelection {
/**
* Get the ResultManager
*
* @return ResultManager
*/
public ResultManager getResult() {
@ -106,6 +114,7 @@ public class DataBaseSelection {
/**
* Get the ResultSet
*
* @return ResultSet
*/
public ResultSet getResultSet() {
@ -114,6 +123,7 @@ public class DataBaseSelection {
/**
* Debug the current Statement
*
* @return this class
*/
public DataBaseSelection printStatement() {
@ -121,16 +131,20 @@ public class DataBaseSelection {
messageBuilder.append(processStatement());
messageBuilder.append(" | params» ");
AtomicBoolean added = new AtomicBoolean(false);
getTempParams().forEach(v -> { messageBuilder.append((added.get()) ? ", " : "").append(v);added.set(true); });
getTempParams().forEach(v -> {
messageBuilder.append((added.get()) ? ", " : "").append(v);
added.set(true);
});
StackTraceElement[] st = Thread.currentThread().getStackTrace();
StackTraceElement stack = st[st.length-1];
LOG.debug("DEBUG <" + stack.getFileName()+":"+stack.getLineNumber() + "> Statement: " + messageBuilder.toString());
StackTraceElement stack = st[st.length - 1];
LOG.debug("DEBUG <" + stack.getFileName() + ":" + stack.getLineNumber() + "> Statement: " + messageBuilder);
return this;
}
/**
* Adding another factors
* @param query MySQL Query
*
* @param query MySQL Query
* @param params Optional parameters for the Query
* @return this class
*/
@ -142,13 +156,19 @@ public class DataBaseSelection {
/**
* Get the current statement query
*
* @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(" = ? "); });
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();

View File

@ -7,12 +7,13 @@ import java.util.concurrent.atomic.AtomicBoolean;
public class InsertManager {
private MySQLConnection connection;
private final MySQLConnection connection;
private String tableName;
private HashMap<String, Object> values;
private final HashMap<String, Object> values;
/**
* Basic constructor for the InsertManager
*
* @param connection The current connection
*/
public InsertManager(MySQLConnection connection) {
@ -22,8 +23,9 @@ public class InsertManager {
/**
* Constructor with direct tableName integration
*
* @param connection The current connection
* @param tableName The name of the table
* @param tableName The name of the table
*/
public InsertManager(MySQLConnection connection, String tableName) {
this.connection = connection;
@ -33,6 +35,7 @@ public class InsertManager {
/**
* Optionally add the tableName / Change the name of the table
*
* @param tableName The name of the table
*/
public void from(String tableName) {
@ -41,8 +44,9 @@ public class InsertManager {
/**
* Query value
*
* @param fieldName The name of the column
* @param value The value of the object you want to insert
* @param value The value of the object you want to insert
* @return this class
*/
public InsertManager value(String fieldName, Object value) {
@ -52,6 +56,7 @@ public class InsertManager {
/**
* Prepares the SQL Query
*
* @return the SQL Query
*/
public String prepareStatement() {
@ -66,7 +71,7 @@ public class InsertManager {
query.append(")");
if (values.size() > 0) query.append(" VALUES (");
AtomicBoolean used_values = new AtomicBoolean(false);
for (int i=0; i < values.size(); i++) {
for (int i = 0; i < values.size(); i++) {
if (used_values.get()) query.append(", ");
used_values.set(true);
query.append("?");
@ -77,6 +82,7 @@ public class InsertManager {
/**
* Execute the current SQL query
*
* @return this class
*/
public InsertManager execute() {

View File

@ -16,6 +16,7 @@ public class ResultManager {
/**
* Basic constructor for the ResultManager
*
* @param resultSet Existing ResultSet
*/
public ResultManager(ResultSet resultSet) {
@ -24,6 +25,7 @@ public class ResultManager {
/**
* Getting the ResultSet
*
* @return ResultSet
*/
public ResultSet getResultSet() {
@ -32,92 +34,140 @@ public class ResultManager {
/**
* Get the Object of an Result
*
* @param column Table column
* @return Object
*/
public Object getObject(String column) {
try { while (resultSet.next()) { return resultSet.getObject(column); }
} catch (Exception err) { LOG.error(err.getMessage()); }
try {
while (resultSet.next()) {
return resultSet.getObject(column);
}
} catch (Exception err) {
LOG.error(err.getMessage());
}
return null;
}
/**
* Get the String of an Result
*
* @param column Table column
* @return String
*/
public String getString(String column) {
try { while (resultSet.next()) { return resultSet.getString(column); }
} catch (Exception err) { LOG.error(err.getMessage()); }
try {
while (resultSet.next()) {
return resultSet.getString(column);
}
} catch (Exception err) {
LOG.error(err.getMessage());
}
return "";
}
/**
* Get the Integer of an Result
*
* @param column Table column
* @return Integer
*/
public Integer getInteger(String column) {
try { while (resultSet.next()) { return resultSet.getInt(column); }
} catch (Exception err) { LOG.error(err.getMessage()); }
try {
while (resultSet.next()) {
return resultSet.getInt(column);
}
} catch (Exception err) {
LOG.error(err.getMessage());
}
return 0;
}
/**
* Get the Boolean of an Result
*
* @param column Table column
* @return Boolean
*/
public Boolean getBoolean(String column) {
try { while (resultSet.next()) { return resultSet.getBoolean(column); }
} catch (Exception err) { LOG.error(err.getMessage()); }
try {
while (resultSet.next()) {
return resultSet.getBoolean(column);
}
} catch (Exception err) {
LOG.error(err.getMessage());
}
return false;
}
public Timestamp getTimestamp(String column) {
try { while (resultSet.next()) { return resultSet.getTimestamp(column); }
} catch (Exception err) { LOG.error(err.getMessage()); }
try {
while (resultSet.next()) {
return resultSet.getTimestamp(column);
}
} catch (Exception err) {
LOG.error(err.getMessage());
}
return null;
}
/**
* Get the count of the Result
*
* @return Integer (Count)
*/
public Integer getRowCount() {
int count=0;
try { while (resultSet.next()) { count++; } } catch (Exception err) { LOG.error(err.getMessage()); }
int count = 0;
try {
while (resultSet.next()) {
count++;
}
} catch (Exception err) {
LOG.error(err.getMessage());
}
return count;
}
/**
* Get a List of all Results
*
* @param column Table column
* @return ArrayList of Result
*/
public ArrayList<String> getList(String column) {
ArrayList<String> results = new ArrayList<>();
try { while (resultSet.next()) { results.add(resultSet.getString(column)); }
} catch (Exception err) { LOG.error(err.getMessage()); }
try {
while (resultSet.next()) {
results.add(resultSet.getString(column));
}
} catch (Exception err) {
LOG.error(err.getMessage());
}
return results;
}
/**
* Get a List of all Results for 2 columns
* @param column Table column #1
*
* @param column Table column #1
* @param column2 Table column #2
* @return HashMap of Result
*/
public HashMap<String, String> getMultipleList(String column, String column2) {
HashMap<String, String> results = new HashMap<>();
try { while (resultSet.next()) {
results.put(resultSet.getString(column), resultSet.getString(column2)); }
} catch (Exception err) { LOG.error(err.getMessage()); }
try {
while (resultSet.next()) {
results.put(resultSet.getString(column), resultSet.getString(column2));
}
} catch (Exception err) {
LOG.error(err.getMessage());
}
return results;
}
/**
* Get a list of all results with all columns
*
* @return ArrayList with the result
*/
public ArrayList<HashMap<String, Object>> getList() {
@ -125,13 +175,15 @@ public class ResultManager {
try {
while (resultSet.next()) {
HashMap<String, Object> result = new HashMap<>();
for (int i=0; i < resultSet.getMetaData().getColumnCount(); i++) {
String columnName = resultSet.getMetaData().getColumnName(i+1);
for (int i = 0; i < resultSet.getMetaData().getColumnCount(); i++) {
String columnName = resultSet.getMetaData().getColumnName(i + 1);
result.put(columnName, resultSet.getObject(columnName));
}
results.add(result);
}
} catch (Exception err) { LOG.error(err.getMessage()); }
} catch (Exception err) {
LOG.error(err.getMessage());
}
return results;
}

View File

@ -12,13 +12,14 @@ public class UpdateManager {
private final Logger LOG = MySQLConnection.LOG;
private MySQLConnection connection;
private final MySQLConnection connection;
private String tableName;
private HashMap<String, Object> whereList;
private HashMap<String, Object> setList;
private final HashMap<String, Object> whereList;
private final HashMap<String, Object> setList;
/**
* Basic constructor for the UpdateManager
*
* @param connection Existing MySQL connection
*/
public UpdateManager(MySQLConnection connection) {
@ -29,10 +30,11 @@ public class UpdateManager {
/**
* Basic constructor for the UpdateManager
*
* @param connection Existing MySQL connection
* @param tableName Table name
* @param tableName Table name
*/
public UpdateManager(MySQLConnection connection, String tableName){
public UpdateManager(MySQLConnection connection, String tableName) {
this.connection = connection;
toTable(tableName);
this.whereList = new HashMap<>();
@ -41,6 +43,7 @@ public class UpdateManager {
/**
* Get the parameters for the SQL statement
*
* @return the parameters
*/
private ArrayList<Object> getTempParams() {
@ -52,6 +55,7 @@ public class UpdateManager {
/**
* Get the current connection
*
* @return MySQL connection
*/
public MySQLConnection getConnection() {
@ -60,6 +64,7 @@ public class UpdateManager {
/**
* Print the deletion statement
*
* @return this class
*/
public UpdateManager printDeleteStatement() {
@ -67,15 +72,19 @@ public class UpdateManager {
messageBuilder.append(processDeleteStatement());
messageBuilder.append(" | params» ");
AtomicBoolean added = new AtomicBoolean(false);
whereList.values().forEach(v -> { messageBuilder.append((added.get()) ? ", " : "").append(v);added.set(true); });
whereList.values().forEach(v -> {
messageBuilder.append((added.get()) ? ", " : "").append(v);
added.set(true);
});
StackTraceElement[] st = Thread.currentThread().getStackTrace();
StackTraceElement stack = st[st.length-1];
LOG.debug("DEBUG <" + stack.getFileName()+":"+stack.getLineNumber() + "> Statement: " + messageBuilder.toString());
StackTraceElement stack = st[st.length - 1];
LOG.debug("DEBUG <" + stack.getFileName() + ":" + stack.getLineNumber() + "> Statement: " + messageBuilder);
return this;
}
/**
* Print the update statement
*
* @return this class
*/
public UpdateManager printUpdateStatement() {
@ -83,15 +92,19 @@ public class UpdateManager {
messageBuilder.append(processUpdateStatement());
messageBuilder.append(" | params» ");
AtomicBoolean added = new AtomicBoolean(false);
getTempParams().forEach(v -> { messageBuilder.append((added.get()) ? ", " : "").append(v);added.set(true); });
getTempParams().forEach(v -> {
messageBuilder.append((added.get()) ? ", " : "").append(v);
added.set(true);
});
StackTraceElement[] st = Thread.currentThread().getStackTrace();
StackTraceElement stack = st[st.length-1];
LOG.debug("DEBUG <" + stack.getFileName()+":"+stack.getLineNumber() + "> Statement: " + messageBuilder.toString());
StackTraceElement stack = st[st.length - 1];
LOG.debug("DEBUG <" + stack.getFileName() + ":" + stack.getLineNumber() + "> Statement: " + messageBuilder);
return this;
}
/**
* Change the tableName
*
* @param tableName New tableName
* @return this class
*/
@ -102,8 +115,9 @@ public class UpdateManager {
/**
* Add a 'where'-clause
*
* @param column Table column
* @param value Value of the column
* @param value Value of the column
* @return this class
*/
public UpdateManager where(String column, Object value) {
@ -113,8 +127,9 @@ public class UpdateManager {
/**
* Add a 'set'-clause
*
* @param column Table column
* @param value Value of the column
* @param value Value of the column
* @return this class
*/
public UpdateManager set(String column, Object value) {
@ -124,6 +139,7 @@ public class UpdateManager {
/**
* Delete the entries with your current conditions
*
* @return this class
*/
public UpdateManager delete() {
@ -133,6 +149,7 @@ public class UpdateManager {
/**
* Update the entries with your current conditions
*
* @return this class
*/
public UpdateManager update() {
@ -142,6 +159,7 @@ public class UpdateManager {
/**
* Get the Query of the 'update'-Statement
*
* @return the Statement
*/
private String processUpdateStatement() {
@ -166,6 +184,7 @@ public class UpdateManager {
/**
* Get the Query of the 'delete'-Statement
*
* @return
*/
private String processDeleteStatement() {
@ -184,6 +203,7 @@ public class UpdateManager {
/**
* Generate a new Table (with prefilled tableName)
*
* @param tableName Name of the new table
* @return the de.gnmyt.SQLToolkit.generator
*/
@ -193,6 +213,7 @@ public class UpdateManager {
/**
* Generate a new Table
*
* @return the de.gnmyt.SQLToolkit.generator
*/
public TableGenerator generateTable() {