diff --git a/pom.xml b/pom.xml index a062998..3734c7c 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ de.goldendeveloper MYSQL-Api - 5.7.3 + 5.7.4 jar https://github.com/Golden-Developer/MYSQL-Api @@ -124,6 +124,11 @@ plexus-archiver 4.9.1 + + org.apache.commons + commons-compress + 1.26.1 + com.zaxxer HikariCP diff --git a/src/main/java/de/goldendeveloper/mysql/MYSQL.java b/src/main/java/de/goldendeveloper/mysql/MYSQL.java index ccd4b3a..dca8b1d 100644 --- a/src/main/java/de/goldendeveloper/mysql/MYSQL.java +++ b/src/main/java/de/goldendeveloper/mysql/MYSQL.java @@ -212,7 +212,6 @@ public void customExecute(String SQL) { * @return The Database object with the given name, or null if it doesn't exist. */ public Database getDatabase(String name) { - executeUpdate("use `" + name + "`;", this); if (existsDatabase(name)) return new Database(name, this); return null; diff --git a/src/main/java/de/goldendeveloper/mysql/entities/Column.java b/src/main/java/de/goldendeveloper/mysql/entities/Column.java index 6cc76c0..2c30873 100644 --- a/src/main/java/de/goldendeveloper/mysql/entities/Column.java +++ b/src/main/java/de/goldendeveloper/mysql/entities/Column.java @@ -37,7 +37,7 @@ public Column(String name, Table table, MYSQL mysql) { */ public SearchResults getAll() { List list = executeQuery("SELECT `" + this.name + "` FROM `" + table.getName() + "`;", - rs -> new SearchResult(rs.getString(1)), mysql); + rs -> new SearchResult(rs.getString(1)), mysql, db.getName()); return new SearchResults(list); } @@ -46,7 +46,7 @@ public SearchResults getAll() { * It executes an SQL query to alter the table and drop the specified column. */ public void drop() { - executeUpdate("ALTER TABLE `" + this.getTable().getName() + "` DROP COLUMN `" + this.name + "`;", mysql); + executeUpdate("ALTER TABLE `" + this.getTable().getName() + "` DROP COLUMN `" + this.name + "`;", mysql, db.getName()); } /** @@ -56,7 +56,7 @@ public void drop() { */ public Object getRandom() { String query = "SELECT " + this.name + " FROM `" + this.table.getName() + "` ORDER BY RAND() LIMIT " + this.table.countRows(); - return executeQuery(query, (rs) -> rs.getObject(1), mysql); + return executeQuery(query, (rs) -> rs.getObject(1), mysql, db.getName()); } /** @@ -65,7 +65,7 @@ public Object getRandom() { * @param id ID of the item to update. */ public void setItemNull(int id) { - executeUpdate("UPDATE `" + this.getTable().getName() + "` SET `" + this.getName() + "` = NULL where `id` = " + id + ";", mysql); + executeUpdate("UPDATE `" + this.getTable().getName() + "` SET `" + this.getName() + "` = NULL where `id` = " + id + ";", mysql, db.getName()); } /** @@ -73,7 +73,7 @@ public void setItemNull(int id) { * This method updates the specified column in the table to null for all rows where the column is not already null. */ public void setNull() { - executeUpdate("UPDATE `" + this.getTable().getName() + "` SET `" + this.getName() + "` = NULL where `" + this.getName() + "` IS NOT NULL;", mysql); + executeUpdate("UPDATE `" + this.getTable().getName() + "` SET `" + this.getName() + "` = NULL where `" + this.getName() + "` IS NOT NULL;", mysql, db.getName()); } /** @@ -84,7 +84,7 @@ public void setNull() { */ public boolean getAsBoolean(int id) { List results = executeQuery("SELECT `" + this.name + "` FROM `" + table.getName() + "` WHERE id = " + id + ";", - rs -> rs.getObject(1).toString().equalsIgnoreCase("true") ? Boolean.TRUE : rs.getObject(1).toString().equalsIgnoreCase("false") ? Boolean.FALSE : null, mysql); + rs -> rs.getObject(1).toString().equalsIgnoreCase("true") ? Boolean.TRUE : rs.getObject(1).toString().equalsIgnoreCase("false") ? Boolean.FALSE : null, mysql, db.getName()); if (!results.isEmpty()) { return results.get(0); } else { @@ -100,7 +100,7 @@ public boolean getAsBoolean(int id) { */ public String getAsString(int id) { List results = executeQuery("SELECT `" + this.name + "` FROM `" + table.getName() + "` WHERE id = " + id + ";", - rs -> rs.getObject(1) != null ? rs.getObject(1).toString() : null, mysql); + rs -> rs.getObject(1) != null ? rs.getObject(1).toString() : null, mysql, db.getName()); if (!results.isEmpty()) { return results.get(0); } else { @@ -115,7 +115,7 @@ public String getAsString(int id) { * @param name The new name for the column. */ public void setName(String name) { - executeUpdate("ALTER TABLE `" + this.getTable().getName() + "` CHANGE " + this.name + name + " varchar (50)", mysql); + executeUpdate("ALTER TABLE `" + this.getTable().getName() + "` CHANGE " + this.name + name + " varchar (50)", mysql, db.getName()); this.name = name; } diff --git a/src/main/java/de/goldendeveloper/mysql/entities/Database.java b/src/main/java/de/goldendeveloper/mysql/entities/Database.java index 37ac400..d59f18a 100644 --- a/src/main/java/de/goldendeveloper/mysql/entities/Database.java +++ b/src/main/java/de/goldendeveloper/mysql/entities/Database.java @@ -63,7 +63,7 @@ public void rename(String name) { * Drops the database. */ public void drop() { - executeUpdate("DROP DATABASE `" + this.name + "`;", mysql); + executeUpdate("DROP DATABASE `" + this.name + "`;", mysql, this.getName()); } /** @@ -87,7 +87,7 @@ public Table getTable(String name) { */ public List getTables() { String query = "SHOW TABLES;"; - return executeQuery(query, rs -> new Table(rs.getString(1), this, mysql), mysql); + return executeQuery(query, rs -> new Table(rs.getString(1), this, mysql), mysql, this.name); } @@ -97,7 +97,7 @@ public List
getTables() { * @param name the name of the table to create */ public void createTable(String name) { - executeUpdate("CREATE TABLE `" + name + "` (id int NOT NULL AUTO_INCREMENT,PRIMARY KEY (id));", mysql); + executeUpdate("CREATE TABLE `" + name + "` (id int NOT NULL AUTO_INCREMENT,PRIMARY KEY (id));", mysql, this.name); } /** diff --git a/src/main/java/de/goldendeveloper/mysql/entities/Row.java b/src/main/java/de/goldendeveloper/mysql/entities/Row.java index 89f5b68..3ed3b04 100644 --- a/src/main/java/de/goldendeveloper/mysql/entities/Row.java +++ b/src/main/java/de/goldendeveloper/mysql/entities/Row.java @@ -55,7 +55,7 @@ public HashMap getData() { map.put(rsMetaData.getColumnName(i), rs.getString(i) != null ? new SearchResult(rs.getString(i)) : null); } return map; - }, mysql); + }, mysql, db.getName()); if (!results.isEmpty()) { exportMap = results.get(0); } @@ -79,7 +79,7 @@ public void setExportMap(HashMap newMap) { * @param item The new value to set for the column. */ public void set(Column column, Object item) { - executeUpdate("UPDATE `" + this.getTable().getName() + "` SET `" + column.getName() + "` = '" + item.toString() + "' WHERE `" + this.column.getName() + "` = '" + this.item + "';", mysql); + executeUpdate("UPDATE `" + this.getTable().getName() + "` SET `" + column.getName() + "` = '" + item.toString() + "' WHERE `" + this.column.getName() + "` = '" + this.item + "';", mysql, db.getName()); } /** @@ -137,6 +137,6 @@ public int getId() { * @see QueryHelper#executeUpdate(String, MYSQL) for executing the deletion query */ public void drop() { - executeUpdate("DELETE FROM `" + this.getTable().getName() + "` WHERE `" + this.column.getName() + "` = '" + this.item + "';", mysql); + executeUpdate("DELETE FROM `" + this.getTable().getName() + "` WHERE `" + this.column.getName() + "` = '" + this.item + "';", mysql, db.getName()); } } diff --git a/src/main/java/de/goldendeveloper/mysql/entities/Table.java b/src/main/java/de/goldendeveloper/mysql/entities/Table.java index 6ba9a2c..630c2cd 100644 --- a/src/main/java/de/goldendeveloper/mysql/entities/Table.java +++ b/src/main/java/de/goldendeveloper/mysql/entities/Table.java @@ -49,7 +49,7 @@ public List describe() { list.add(rs.getString(1)); } return list; - }, mysql).get(0); + }, mysql, db.getName()).get(0); return description != null ? description : new ArrayList<>(); } @@ -67,7 +67,7 @@ public String getName() { * This method executes a SQL query to drop the table with the given name. */ public void drop() { - this.executeUpdate("DROP TABLE `" + this.name + "`;", mysql); + this.executeUpdate("DROP TABLE `" + this.name + "`;", mysql, db.getName()); } /** @@ -93,6 +93,7 @@ public List getRows() { int index = 0; try { Statement statement = mysql.getConnect().createStatement(); + statement.execute("USE " + db.getName() + ";"); ResultSet rs = statement.executeQuery("SELECT * FROM " + this.getName() + ";"); while (rs.next()) { index++; @@ -127,7 +128,7 @@ public HashMap getMap(Column column, String item) { map.put(rsMetaData.getColumnName(i), new SearchResult(rs.getString(i))); } return map; - }, mysql); + }, mysql, db.getName()); return !results.isEmpty() ? results.get(0) : new HashMap<>(); } @@ -137,7 +138,7 @@ public HashMap getMap(Column column, String item) { * @return The number of rows in the table. */ public int countRows() { - List results = executeQuery("SELECT COUNT(*) FROM `" + this.name + "`;", rs -> rs.getInt(1), mysql); + List results = executeQuery("SELECT COUNT(*) FROM `" + this.name + "`;", rs -> rs.getInt(1), mysql, db.getName()); return !results.isEmpty() ? results.get(0) : 0; } @@ -165,7 +166,7 @@ public boolean isEmpty() { * @param id The ID of the row to be dropped. */ public void dropRow(int id) { - executeUpdate("DELETE FROM `" + this.name + "` where id = " + id + ";", mysql); + executeUpdate("DELETE FROM `" + this.name + "` where id = " + id + ";", mysql, db.getName()); } /** @@ -178,7 +179,7 @@ public List getColumns() { List list = new ArrayList<>(); list.add(new Column(rs.getString(1), this, mysql)); return list; - }, mysql); + }, mysql, db.getName()); return !results.isEmpty() ? results.get(0) : new ArrayList<>(); } @@ -202,7 +203,7 @@ public Column getColumn(String name) { * @return {@code true} if the column exists, {@code false} otherwise. */ public boolean existsColumn(String name) { - List results = executeQuery("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '" + this.getDatabase().getName() + "' AND TABLE_NAME = '" + this.name + "' AND COLUMN_NAME = '" + name + "';", rs -> true, mysql); + List results = executeQuery("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '" + db.getName() + "' AND TABLE_NAME = '" + this.name + "' AND COLUMN_NAME = '" + name + "';", rs -> true, mysql, db.getName()); return !results.isEmpty() && results.get(0); } @@ -215,7 +216,7 @@ public boolean existsColumn(String name) { * @return true if the row exists in the table, false otherwise. */ public boolean existsRow(Column column, String item) { - List results = executeQuery("SELECT EXISTS(SELECT * FROM `" + this.getName() + "` WHERE `" + column.getName() + "` = '" + item + "')", rs -> rs.getBoolean(1), mysql); + List results = executeQuery("SELECT EXISTS(SELECT * FROM `" + this.getName() + "` WHERE `" + column.getName() + "` = '" + item + "')", rs -> rs.getBoolean(1), mysql, db.getName()); return !results.isEmpty() && results.get(0); } @@ -227,7 +228,7 @@ public boolean existsRow(Column column, String item) { * @param name The name of the column to set as unique. */ public void setUniqueColumn(String name) { - executeUpdate("ALTER IGNORE TABLE `" + this.name + "` ADD UNIQUE (" + name + ");", mysql); + executeUpdate("ALTER IGNORE TABLE `" + this.name + "` ADD UNIQUE (" + name + ");", mysql, db.getName()); } /** @@ -236,7 +237,7 @@ public void setUniqueColumn(String name) { * @param name the name of the column to add */ public void addColumn(String name) { - executeUpdate("ALTER TABLE `" + this.name + "` ADD `" + name + "` " + MysqlTypes.TEXT.getMysqlTypeName() + " (" + 65555 + ");", mysql); + executeUpdate("ALTER TABLE `" + this.name + "` ADD `" + name + "` " + MysqlTypes.TEXT.getMysqlTypeName() + " (" + 65555 + ");", mysql, db.getName()); } /** @@ -295,7 +296,7 @@ public void insert(HashMap rowBuilder) { items.append(",'").append(item).append("'"); } }); - executeUpdate("INSERT INTO `" + this.name + "` (" + keys + ")VALUES (" + items + ");", mysql); + executeUpdate("INSERT INTO `" + this.name + "` (" + keys + ")VALUES (" + items + ");", mysql, db.getName()); } /** @@ -375,7 +376,7 @@ private Row getRow(Column column, String query) { map.put(key, value); } return map; - }, mysql); + }, mysql, db.getName()); if (!results.isEmpty()) { HashMap exportMap = results.get(0); String id = exportMap.get("id").getAsString(); diff --git a/src/main/java/de/goldendeveloper/mysql/entities/enums/MysqlTypes.java b/src/main/java/de/goldendeveloper/mysql/entities/enums/MysqlTypes.java index 85d9547..3faa83f 100644 --- a/src/main/java/de/goldendeveloper/mysql/entities/enums/MysqlTypes.java +++ b/src/main/java/de/goldendeveloper/mysql/entities/enums/MysqlTypes.java @@ -8,9 +8,7 @@ public enum MysqlTypes { /** * Enumeration representing the variable "BIT". - * * This enumeration is used to represent a bit variable. - * * The value of this enumeration is a string "bit". */ BIT("bit"), @@ -49,7 +47,6 @@ public enum MysqlTypes { INT("int"), /** * Represents a variable of type BIGINT. - * * The BIGINT type in SQL is typically used to represent large integer values. */ BIGINT("bigint"), @@ -86,9 +83,7 @@ public enum MysqlTypes { DOUBLE("double"), /** * This is a constant variable representing a date. - * * The variable is stored as a string with the value "date". - * * Usage example: * String myDate = DATE.getValue(); */ @@ -125,10 +120,8 @@ public enum MysqlTypes { * The CHAR variable represents a character type in Java. * It is used to store single characters and occupies 2 bytes in memory. * The value of a CHAR variable can be any valid Unicode character. - * * Usage: * CHAR("char") - * * Example: * char myChar = CHAR.getValue(); */ @@ -151,10 +144,8 @@ public enum MysqlTypes { NVARCHAR("nvarchar"), /** * Represents a binary value. - * * Usage: * BINARY binary = BINARY("binary"); - * * This class can be used to store binary values and perform operations on them. * */ diff --git a/src/main/java/de/goldendeveloper/mysql/entities/enums/Permissions.java b/src/main/java/de/goldendeveloper/mysql/entities/enums/Permissions.java index ab494b5..2f7ec85 100644 --- a/src/main/java/de/goldendeveloper/mysql/entities/enums/Permissions.java +++ b/src/main/java/de/goldendeveloper/mysql/entities/enums/Permissions.java @@ -7,11 +7,9 @@ public enum Permissions { /** * Represents the permission for all operations. - * * The ALL variable is a constant of the Permissions enum class and has a value of 0. * It represents the permission that grants all operations to a user or database. * Use this permission to assign all available permissions to a user or database. - * * Example usage: * Permissions allPermission = Permissions.ALL; * int permissionValue = allPermission.getValue(); @@ -23,10 +21,8 @@ public enum Permissions { DELETE(1), /** * Represents the permission for inserting data into a database. - * * The INSERT variable is a constant of the Permissions enum class and has a value of 2. * It represents the permission that allows a user or database to insert data into tables. - * * Example usage: * Permissions insertPermission = Permissions.INSERT; * int permissionValue = insertPermission.getValue(); @@ -39,11 +35,9 @@ public enum Permissions { REFERENCES(3), /** * The SELECT variable represents the permission for querying or retrieving data from a database. - * * The SELECT variable is a constant of the Permissions enum class and has a value of 4. * It represents the permission that allows a user or database to execute SELECT statements on tables and views, * retrieving data from specific columns or all columns. - * * Example usage: * Permissions selectPermission = Permissions.SELECT; * int permissionValue = selectPermission.getValue(); @@ -51,15 +45,12 @@ public enum Permissions { SELECT(4), /** * Represents the permission for creating and managing triggers in a database. - * * The TRIGGER variable is a constant of the Permissions enum class and*/ TRIGGER(5), /** * Represents the permission for updating data in a database. - * * The UPDATE variable is a constant of the Permissions enum class and has a value of 6. * It represents the permission that allows a user or database to execute UPDATE statements on tables, modifying data. - * * Example usage: * Permissions updatePermission = Permissions.UPDATE; * int permissionValue = updatePermission.getValue(); @@ -67,11 +58,9 @@ public enum Permissions { UPDATE(6), /** * Represents the permission for executing a procedure or function in a database. - * * The EXECUTE variable is a constant of the Permissions enum class and has a value of 7. * It represents the permission that allows a user or database to execute stored procedures or functions. * This permission enables the user or database to invoke pre-defined logic in the database. - * * Example usage: * Permissions executePermission = Permissions.EXECUTE; * int permissionValue = executePermission.getValue(); diff --git a/src/main/java/de/goldendeveloper/mysql/interfaces/QueryHelper.java b/src/main/java/de/goldendeveloper/mysql/interfaces/QueryHelper.java index 53921d4..5cb60b8 100644 --- a/src/main/java/de/goldendeveloper/mysql/interfaces/QueryHelper.java +++ b/src/main/java/de/goldendeveloper/mysql/interfaces/QueryHelper.java @@ -18,29 +18,12 @@ public interface QueryHelper { * @param the type of the query result * @return the processed query result, or null if an exception occurred */ -// default T executeQuery(String query, ResultSetHandler handler, MYSQL mysql) { -// try { -// Statement statement = mysql.getConnect().createStatement(); -// ResultSet rs = statement.executeQuery(query); -// T result = null; -// if (rs.next()) { -// result = handler.handle(rs); -// } -// mysql.closeRsAndSt(rs, statement); -// return result; -// } catch (Exception e) { -// mysql.callException(e); -// } -// return null; -// } - - default List executeQuery(String query, ResultSetHandler handler, MYSQL mysql) { List results = new ArrayList<>(); try { Statement statement = mysql.getConnect().createStatement(); ResultSet rs = statement.executeQuery(query); - while (rs.next()) { // Ändern Sie `if` zu `while`, um alle Zeilen zu verarbeiten + while (rs.next()) { T result = handler.handle(rs); if (result != null) { results.add(result); @@ -50,7 +33,51 @@ default List executeQuery(String query, ResultSetHandler handler, MYSQ } catch (Exception e) { mysql.callException(e); } - return results; // Gibt eine Liste von Ergebnissen zurück + return results; + } + + /** + * Executes a query on the database and returns the result. + * + * @param query the SQL query to execute + * @param handler the ResultSetHandler to process the query result + * @param mysql the MYSQL object representing the database connection + * @param the type of the query result + * @return the processed query result, or null if an exception occurred + */ + default List executeQuery(String query, ResultSetHandler handler, MYSQL mysql, String database) { + List results = new ArrayList<>(); + try { + Statement statement = mysql.getConnect().createStatement(); + statement.execute("USE " + database); + ResultSet rs = statement.executeQuery(query); + while (rs.next()) { + T result = handler.handle(rs); + if (result != null) { + results.add(result); + } + } + mysql.closeRsAndSt(rs, statement); + } catch (Exception e) { + mysql.callException(e); + } + return results; + } + + /** + * Executes an update query on the database. + * + * @param query the SQL update query to execute + * @param mysql the MYSQL object representing the database connection + */ + default void executeUpdate(String query, MYSQL mysql) { + try { + Statement statement = mysql.getConnect().createStatement(); + statement.execute(query); + mysql.closeRsAndSt(null, statement); + } catch (Exception e) { + mysql.callException(e); + } } /** @@ -59,9 +86,10 @@ default List executeQuery(String query, ResultSetHandler handler, MYSQ * @param query the SQL update query to execute * @param mysql the MYSQL object representing the database connection */ - default void executeUpdate(String query, MYSQL mysql) { + default void executeUpdate(String query, MYSQL mysql, String database) { try { Statement statement = mysql.getConnect().createStatement(); + statement.execute("USE " + database); statement.execute(query); mysql.closeRsAndSt(null, statement); } catch (Exception e) {