Moved to package / added pom.xml
This commit is contained in:
160
src/main/java/de/gnmyt/SQLToolkit/manager/DataBaseSelection.java
Normal file
160
src/main/java/de/gnmyt/SQLToolkit/manager/DataBaseSelection.java
Normal file
@ -0,0 +1,160 @@
|
||||
package de.gnmyt.SQLToolkit.manager;
|
||||
|
||||
import de.gnmyt.SQLToolkit.drivers.MySQLConnection;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/********************************
|
||||
* @author Mathias Wagner
|
||||
* Created 23.12.2020
|
||||
********************************/
|
||||
|
||||
public class DataBaseSelection {
|
||||
|
||||
private final MySQLConnection connection;
|
||||
private final HashMap<String, Object> whereList;
|
||||
private final ArrayList<Object> databaseParameters;
|
||||
public ArrayList<String> optionalQuery;
|
||||
private int limit;
|
||||
private String tableName;
|
||||
|
||||
/**
|
||||
* Basic constructor for selection
|
||||
* @param connection The current Connection
|
||||
* @param tableName The table name
|
||||
*/
|
||||
public DataBaseSelection(MySQLConnection connection, String tableName) {
|
||||
this.whereList = new HashMap<>();
|
||||
this.databaseParameters = new ArrayList<>();
|
||||
this.optionalQuery = new ArrayList<>();
|
||||
this.connection = connection;
|
||||
from(tableName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic constructor for selection
|
||||
* @param connection The current Connection
|
||||
*/
|
||||
public DataBaseSelection(MySQLConnection connection) {
|
||||
this.whereList = new HashMap<>();
|
||||
this.databaseParameters = new ArrayList<>();
|
||||
this.optionalQuery = new ArrayList<>();
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the temp-generated parameters
|
||||
* @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
|
||||
* @param tableName The table name
|
||||
* @return this class
|
||||
*/
|
||||
public DataBaseSelection from(String tableName) {
|
||||
this.tableName = connection.getTablePrefix().isEmpty() ? tableName : connection.getTablePrefix() + tableName;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adding another 'where'-statement
|
||||
* @param column Table column name
|
||||
* @param value Table value name
|
||||
* @return this class
|
||||
*/
|
||||
public DataBaseSelection where(String column, Object value) {
|
||||
whereList.put(column, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the limit of the rows
|
||||
* @param limit The new limit
|
||||
* @return this class
|
||||
*/
|
||||
public DataBaseSelection limit(int limit) {
|
||||
this.limit = limit;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the limit of the rows
|
||||
* @param limit The new limit
|
||||
* @return this class
|
||||
*/
|
||||
public DataBaseSelection limit(String limit) {
|
||||
this.limit = Integer.parseInt(limit);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the ResultManager
|
||||
* @return ResultManager
|
||||
*/
|
||||
public ResultManager getResult() {
|
||||
return connection.getResult(processStatement(), getTempParams().toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the ResultSet
|
||||
* @return ResultSet
|
||||
*/
|
||||
public ResultSet getResultSet() {
|
||||
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); });
|
||||
StackTraceElement[] st = Thread.currentThread().getStackTrace();
|
||||
StackTraceElement stack = st[st.length-1];
|
||||
connection.getLogManager()
|
||||
.sendInfo("DEBUG <" + stack.getFileName()+":"+stack.getLineNumber() + "> Statement: " + messageBuilder.toString());
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adding another factors
|
||||
* @param query MySQL Query
|
||||
* @param params Optional parameters for the Query
|
||||
* @return this class
|
||||
*/
|
||||
public DataBaseSelection add(String query, Object... params) {
|
||||
optionalQuery.add(query);
|
||||
this.databaseParameters.addAll(Arrays.asList(params));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(" = ? "); });
|
||||
if (limit != 0) sb.append("LIMIT ").append(limit);
|
||||
optionalQuery.forEach(v -> sb.append(" ").append(v).append(" "));
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
117
src/main/java/de/gnmyt/SQLToolkit/manager/ResultManager.java
Normal file
117
src/main/java/de/gnmyt/SQLToolkit/manager/ResultManager.java
Normal file
@ -0,0 +1,117 @@
|
||||
package de.gnmyt.SQLToolkit.manager;
|
||||
|
||||
import de.gnmyt.SQLToolkit.drivers.SqlLogManager;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
/********************************
|
||||
* @author Mathias Wagner
|
||||
* Created 23.12.2020
|
||||
********************************/
|
||||
|
||||
public class ResultManager {
|
||||
|
||||
private ResultSet resultSet;
|
||||
private SqlLogManager logManager;
|
||||
|
||||
/**
|
||||
* Basic constructor for the ResultManager
|
||||
* @param resultSet Existing ResultSet
|
||||
* @param logManager Existing LogManager
|
||||
*/
|
||||
public ResultManager(ResultSet resultSet, SqlLogManager logManager) {
|
||||
this.resultSet = resultSet;
|
||||
this.logManager = logManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getting the ResultSet
|
||||
* @return ResultSet
|
||||
*/
|
||||
public ResultSet getResultSet() {
|
||||
return resultSet;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) { logManager.sendError(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) { logManager.sendError(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) { logManager.sendError(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) { logManager.sendError(err.getMessage()); }
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the count of the Result
|
||||
* @return Integer (Count)
|
||||
*/
|
||||
public Integer getRowCount() {
|
||||
int count=0;
|
||||
try { while (resultSet.next()) { count++; } } catch (Exception err) { logManager.sendError(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) { logManager.sendError(err.getMessage()); }
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a List of all Results for 2 columns
|
||||
* @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) { logManager.sendError(err.getMessage()); }
|
||||
return results;
|
||||
}
|
||||
|
||||
}
|
206
src/main/java/de/gnmyt/SQLToolkit/manager/UpdateManager.java
Normal file
206
src/main/java/de/gnmyt/SQLToolkit/manager/UpdateManager.java
Normal file
@ -0,0 +1,206 @@
|
||||
package de.gnmyt.SQLToolkit.manager;
|
||||
|
||||
import de.gnmyt.SQLToolkit.drivers.MySQLConnection;
|
||||
import de.gnmyt.SQLToolkit.generator.TableGenerator;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/********************************
|
||||
* @author Mathias Wagner
|
||||
* Created 23.12.2020
|
||||
********************************/
|
||||
|
||||
public class UpdateManager {
|
||||
|
||||
private MySQLConnection connection;
|
||||
private String tableName;
|
||||
private HashMap<String, Object> whereList;
|
||||
private HashMap<String, Object> setList;
|
||||
|
||||
/**
|
||||
* Basic constructor for the UpdateManager
|
||||
* @param connection Existing MySQL connection
|
||||
*/
|
||||
public UpdateManager(MySQLConnection connection) {
|
||||
this.connection = connection;
|
||||
this.whereList = new HashMap<>();
|
||||
this.setList = new HashMap<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic constructor for the UpdateManager
|
||||
* @param connection Existing MySQL connection
|
||||
* @param tableName Table name
|
||||
*/
|
||||
public UpdateManager(MySQLConnection connection, String tableName){
|
||||
this.connection = connection;
|
||||
toTable(tableName);
|
||||
this.whereList = new HashMap<>();
|
||||
this.setList = new HashMap<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the parameters for the SQL statement
|
||||
* @return the parameters
|
||||
*/
|
||||
private ArrayList<Object> getTempParams() {
|
||||
ArrayList<Object> tempParams = new ArrayList<>();
|
||||
setList.forEach((str, obj) -> tempParams.add(obj));
|
||||
whereList.forEach((str, obj) -> tempParams.add(obj));
|
||||
return tempParams;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current connection
|
||||
* @return MySQL connection
|
||||
*/
|
||||
public MySQLConnection getConnection() {
|
||||
return connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the deletion statement
|
||||
* @return this class
|
||||
*/
|
||||
public UpdateManager printDeleteStatement() {
|
||||
StringBuilder messageBuilder = new StringBuilder();
|
||||
messageBuilder.append(processDeleteStatement());
|
||||
messageBuilder.append(" | params» ");
|
||||
AtomicBoolean added = new AtomicBoolean(false);
|
||||
whereList.values().forEach(v -> { messageBuilder.append((added.get()) ? ", " : "").append(v);added.set(true); });
|
||||
StackTraceElement[] st = Thread.currentThread().getStackTrace();
|
||||
StackTraceElement stack = st[st.length-1];
|
||||
connection.getLogManager()
|
||||
.sendInfo("DEBUG <" + stack.getFileName()+":"+stack.getLineNumber() + "> Statement: " + messageBuilder.toString());
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the update statement
|
||||
* @return this class
|
||||
*/
|
||||
public UpdateManager printUpdateStatement() {
|
||||
StringBuilder messageBuilder = new StringBuilder();
|
||||
messageBuilder.append(processUpdateStatement());
|
||||
messageBuilder.append(" | params» ");
|
||||
AtomicBoolean added = new AtomicBoolean(false);
|
||||
getTempParams().forEach(v -> { messageBuilder.append((added.get()) ? ", " : "").append(v);added.set(true); });
|
||||
StackTraceElement[] st = Thread.currentThread().getStackTrace();
|
||||
StackTraceElement stack = st[st.length-1];
|
||||
connection.getLogManager()
|
||||
.sendInfo("DEBUG <" + stack.getFileName()+":"+stack.getLineNumber() + "> Statement: " + messageBuilder.toString());
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the tableName
|
||||
* @param tableName New tableName
|
||||
* @return this class
|
||||
*/
|
||||
public UpdateManager toTable(String tableName) {
|
||||
this.tableName = connection.getTablePrefix().isEmpty() ? tableName : connection.getTablePrefix() + tableName;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a 'where'-clause
|
||||
* @param column Table column
|
||||
* @param value Value of the column
|
||||
* @return this class
|
||||
*/
|
||||
public UpdateManager where(String column, Object value) {
|
||||
whereList.put(column, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a 'set'-clause
|
||||
* @param column Table column
|
||||
* @param value Value of the column
|
||||
* @return this class
|
||||
*/
|
||||
public UpdateManager set(String column, Object value) {
|
||||
setList.put(column, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the entries with your current conditions
|
||||
* @return this class
|
||||
*/
|
||||
public UpdateManager delete() {
|
||||
connection.update(processDeleteStatement(), whereList.values().toArray());
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the entries with your current conditions
|
||||
* @return this class
|
||||
*/
|
||||
public UpdateManager update() {
|
||||
connection.update(processUpdateStatement(), getTempParams().toArray());
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Query of the 'update'-Statement
|
||||
* @return the Statement
|
||||
*/
|
||||
private String processUpdateStatement() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("UPDATE ").append(tableName);
|
||||
if (!setList.isEmpty()) sb.append(" SET ");
|
||||
AtomicBoolean used = new AtomicBoolean(false);
|
||||
setList.forEach((str, obj) -> {
|
||||
if (used.get()) sb.append(", ");
|
||||
sb.append(str).append(" = ?");
|
||||
used.set(true);
|
||||
});
|
||||
if (!whereList.isEmpty()) sb.append(" WHERE ");
|
||||
AtomicBoolean used2 = new AtomicBoolean(false);
|
||||
whereList.forEach((str, obj) -> {
|
||||
if (used2.get()) sb.append(" AND ");
|
||||
sb.append(str).append(" = ?");
|
||||
used2.set(true);
|
||||
});
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Query of the 'delete'-Statement
|
||||
* @return
|
||||
*/
|
||||
private String processDeleteStatement() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("DELETE FROM ").append(tableName);
|
||||
if (!whereList.isEmpty()) sb.append(" WHERE ");
|
||||
AtomicBoolean used = new AtomicBoolean(false);
|
||||
whereList.forEach((str, obj) -> {
|
||||
if (used.get()) sb.append(" AND ");
|
||||
sb.append(str).append(" = ?");
|
||||
used.set(true);
|
||||
});
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generate a new Table (with prefilled tableName)
|
||||
* @param tableName Name of the new table
|
||||
* @return the de.gnmyt.SQLToolkit.generator
|
||||
*/
|
||||
public TableGenerator generateTable(String tableName) {
|
||||
return new TableGenerator(this, tableName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a new Table
|
||||
* @return the de.gnmyt.SQLToolkit.generator
|
||||
*/
|
||||
public TableGenerator generateTable() {
|
||||
return (tableName.isEmpty()) ? null : new TableGenerator(this, tableName);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user