Skip to content

Commit

Permalink
Refactor code and update SQL statements in various classes
Browse files Browse the repository at this point in the history
This commit includes refactoring of code, especially reduction of repetitive comments in the codebase. It involves updates of SQL statements in several classes, like `Table`, `Database`, `Row`, `MYSQL`, `QueryHelper`, and `Column` to include the database name in the execution for a more explicit call. It also includes removal of an unnecessary SQL statement in the `MYSQL` class.
  • Loading branch information
coilgner committed Mar 12, 2024
1 parent d52e547 commit 7ca6cb1
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 68 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>de.goldendeveloper</groupId>
<artifactId>MYSQL-Api</artifactId>
<version>5.7.3</version>
<version>5.7.4</version>
<packaging>jar</packaging>
<url>https://github.com/Golden-Developer/MYSQL-Api</url>

Expand Down Expand Up @@ -124,6 +124,11 @@
<artifactId>plexus-archiver</artifactId>
<version>4.9.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.26.1</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
Expand Down
1 change: 0 additions & 1 deletion src/main/java/de/goldendeveloper/mysql/MYSQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/de/goldendeveloper/mysql/entities/Column.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Column(String name, Table table, MYSQL mysql) {
*/
public SearchResults getAll() {
List<SearchResult> 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);
}

Expand All @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -65,15 +65,15 @@ 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());
}

/**
* Sets the value of the column to null for all rows where the column is not already null.
* 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());
}

/**
Expand All @@ -84,7 +84,7 @@ public void setNull() {
*/
public boolean getAsBoolean(int id) {
List<Boolean> 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 {
Expand All @@ -100,7 +100,7 @@ public boolean getAsBoolean(int id) {
*/
public String getAsString(int id) {
List<String> 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 {
Expand All @@ -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;
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/de/goldendeveloper/mysql/entities/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand All @@ -87,7 +87,7 @@ public Table getTable(String name) {
*/
public List<Table> 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);
}


Expand All @@ -97,7 +97,7 @@ public List<Table> 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);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/de/goldendeveloper/mysql/entities/Row.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public HashMap<String, SearchResult> 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);
}
Expand All @@ -79,7 +79,7 @@ public void setExportMap(HashMap<String, SearchResult> 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());
}

/**
Expand Down Expand Up @@ -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());
}
}
25 changes: 13 additions & 12 deletions src/main/java/de/goldendeveloper/mysql/entities/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public List<String> describe() {
list.add(rs.getString(1));
}
return list;
}, mysql).get(0);
}, mysql, db.getName()).get(0);
return description != null ? description : new ArrayList<>();
}

Expand All @@ -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());
}

/**
Expand All @@ -93,6 +93,7 @@ public List<Row> 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++;
Expand Down Expand Up @@ -127,7 +128,7 @@ public HashMap<String, SearchResult> 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<>();
}

Expand All @@ -137,7 +138,7 @@ public HashMap<String, SearchResult> getMap(Column column, String item) {
* @return The number of rows in the table.
*/
public int countRows() {
List<Integer> results = executeQuery("SELECT COUNT(*) FROM `" + this.name + "`;", rs -> rs.getInt(1), mysql);
List<Integer> results = executeQuery("SELECT COUNT(*) FROM `" + this.name + "`;", rs -> rs.getInt(1), mysql, db.getName());
return !results.isEmpty() ? results.get(0) : 0;
}

Expand Down Expand Up @@ -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());
}

/**
Expand All @@ -178,7 +179,7 @@ public List<Column> getColumns() {
List<Column> 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<>();
}

Expand All @@ -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<Boolean> 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<Boolean> 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);
}

Expand All @@ -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<Boolean> results = executeQuery("SELECT EXISTS(SELECT * FROM `" + this.getName() + "` WHERE `" + column.getName() + "` = '" + item + "')", rs -> rs.getBoolean(1), mysql);
List<Boolean> 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);
}

Expand All @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand Down Expand Up @@ -295,7 +296,7 @@ public void insert(HashMap<String, String> 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());
}

/**
Expand Down Expand Up @@ -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<String, SearchResult> exportMap = results.get(0);
String id = exportMap.get("id").getAsString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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();
*/
Expand Down Expand Up @@ -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();
*/
Expand All @@ -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.
*
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -39,39 +35,32 @@ 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();
*/
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();
*/
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();
Expand Down
Loading

0 comments on commit 7ca6cb1

Please sign in to comment.