Skip to content

Commit 7ca6cb1

Browse files
committed
Refactor code and update SQL statements in various classes
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.
1 parent d52e547 commit 7ca6cb1

File tree

9 files changed

+81
-68
lines changed

9 files changed

+81
-68
lines changed

pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>de.goldendeveloper</groupId>
77
<artifactId>MYSQL-Api</artifactId>
8-
<version>5.7.3</version>
8+
<version>5.7.4</version>
99
<packaging>jar</packaging>
1010
<url>https://github.com/Golden-Developer/MYSQL-Api</url>
1111

@@ -124,6 +124,11 @@
124124
<artifactId>plexus-archiver</artifactId>
125125
<version>4.9.1</version>
126126
</dependency>
127+
<dependency>
128+
<groupId>org.apache.commons</groupId>
129+
<artifactId>commons-compress</artifactId>
130+
<version>1.26.1</version>
131+
</dependency>
127132
<dependency>
128133
<groupId>com.zaxxer</groupId>
129134
<artifactId>HikariCP</artifactId>

src/main/java/de/goldendeveloper/mysql/MYSQL.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ public void customExecute(String SQL) {
212212
* @return The Database object with the given name, or null if it doesn't exist.
213213
*/
214214
public Database getDatabase(String name) {
215-
executeUpdate("use `" + name + "`;", this);
216215
if (existsDatabase(name))
217216
return new Database(name, this);
218217
return null;

src/main/java/de/goldendeveloper/mysql/entities/Column.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public Column(String name, Table table, MYSQL mysql) {
3737
*/
3838
public SearchResults getAll() {
3939
List<SearchResult> list = executeQuery("SELECT `" + this.name + "` FROM `" + table.getName() + "`;",
40-
rs -> new SearchResult(rs.getString(1)), mysql);
40+
rs -> new SearchResult(rs.getString(1)), mysql, db.getName());
4141
return new SearchResults(list);
4242
}
4343

@@ -46,7 +46,7 @@ public SearchResults getAll() {
4646
* It executes an SQL query to alter the table and drop the specified column.
4747
*/
4848
public void drop() {
49-
executeUpdate("ALTER TABLE `" + this.getTable().getName() + "` DROP COLUMN `" + this.name + "`;", mysql);
49+
executeUpdate("ALTER TABLE `" + this.getTable().getName() + "` DROP COLUMN `" + this.name + "`;", mysql, db.getName());
5050
}
5151

5252
/**
@@ -56,7 +56,7 @@ public void drop() {
5656
*/
5757
public Object getRandom() {
5858
String query = "SELECT " + this.name + " FROM `" + this.table.getName() + "` ORDER BY RAND() LIMIT " + this.table.countRows();
59-
return executeQuery(query, (rs) -> rs.getObject(1), mysql);
59+
return executeQuery(query, (rs) -> rs.getObject(1), mysql, db.getName());
6060
}
6161

6262
/**
@@ -65,15 +65,15 @@ public Object getRandom() {
6565
* @param id ID of the item to update.
6666
*/
6767
public void setItemNull(int id) {
68-
executeUpdate("UPDATE `" + this.getTable().getName() + "` SET `" + this.getName() + "` = NULL where `id` = " + id + ";", mysql);
68+
executeUpdate("UPDATE `" + this.getTable().getName() + "` SET `" + this.getName() + "` = NULL where `id` = " + id + ";", mysql, db.getName());
6969
}
7070

7171
/**
7272
* Sets the value of the column to null for all rows where the column is not already null.
7373
* This method updates the specified column in the table to null for all rows where the column is not already null.
7474
*/
7575
public void setNull() {
76-
executeUpdate("UPDATE `" + this.getTable().getName() + "` SET `" + this.getName() + "` = NULL where `" + this.getName() + "` IS NOT NULL;", mysql);
76+
executeUpdate("UPDATE `" + this.getTable().getName() + "` SET `" + this.getName() + "` = NULL where `" + this.getName() + "` IS NOT NULL;", mysql, db.getName());
7777
}
7878

7979
/**
@@ -84,7 +84,7 @@ public void setNull() {
8484
*/
8585
public boolean getAsBoolean(int id) {
8686
List<Boolean> results = executeQuery("SELECT `" + this.name + "` FROM `" + table.getName() + "` WHERE id = " + id + ";",
87-
rs -> rs.getObject(1).toString().equalsIgnoreCase("true") ? Boolean.TRUE : rs.getObject(1).toString().equalsIgnoreCase("false") ? Boolean.FALSE : null, mysql);
87+
rs -> rs.getObject(1).toString().equalsIgnoreCase("true") ? Boolean.TRUE : rs.getObject(1).toString().equalsIgnoreCase("false") ? Boolean.FALSE : null, mysql, db.getName());
8888
if (!results.isEmpty()) {
8989
return results.get(0);
9090
} else {
@@ -100,7 +100,7 @@ public boolean getAsBoolean(int id) {
100100
*/
101101
public String getAsString(int id) {
102102
List<String> results = executeQuery("SELECT `" + this.name + "` FROM `" + table.getName() + "` WHERE id = " + id + ";",
103-
rs -> rs.getObject(1) != null ? rs.getObject(1).toString() : null, mysql);
103+
rs -> rs.getObject(1) != null ? rs.getObject(1).toString() : null, mysql, db.getName());
104104
if (!results.isEmpty()) {
105105
return results.get(0);
106106
} else {
@@ -115,7 +115,7 @@ public String getAsString(int id) {
115115
* @param name The new name for the column.
116116
*/
117117
public void setName(String name) {
118-
executeUpdate("ALTER TABLE `" + this.getTable().getName() + "` CHANGE " + this.name + name + " varchar (50)", mysql);
118+
executeUpdate("ALTER TABLE `" + this.getTable().getName() + "` CHANGE " + this.name + name + " varchar (50)", mysql, db.getName());
119119
this.name = name;
120120
}
121121

src/main/java/de/goldendeveloper/mysql/entities/Database.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void rename(String name) {
6363
* Drops the database.
6464
*/
6565
public void drop() {
66-
executeUpdate("DROP DATABASE `" + this.name + "`;", mysql);
66+
executeUpdate("DROP DATABASE `" + this.name + "`;", mysql, this.getName());
6767
}
6868

6969
/**
@@ -87,7 +87,7 @@ public Table getTable(String name) {
8787
*/
8888
public List<Table> getTables() {
8989
String query = "SHOW TABLES;";
90-
return executeQuery(query, rs -> new Table(rs.getString(1), this, mysql), mysql);
90+
return executeQuery(query, rs -> new Table(rs.getString(1), this, mysql), mysql, this.name);
9191
}
9292

9393

@@ -97,7 +97,7 @@ public List<Table> getTables() {
9797
* @param name the name of the table to create
9898
*/
9999
public void createTable(String name) {
100-
executeUpdate("CREATE TABLE `" + name + "` (id int NOT NULL AUTO_INCREMENT,PRIMARY KEY (id));", mysql);
100+
executeUpdate("CREATE TABLE `" + name + "` (id int NOT NULL AUTO_INCREMENT,PRIMARY KEY (id));", mysql, this.name);
101101
}
102102

103103
/**

src/main/java/de/goldendeveloper/mysql/entities/Row.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public HashMap<String, SearchResult> getData() {
5555
map.put(rsMetaData.getColumnName(i), rs.getString(i) != null ? new SearchResult(rs.getString(i)) : null);
5656
}
5757
return map;
58-
}, mysql);
58+
}, mysql, db.getName());
5959
if (!results.isEmpty()) {
6060
exportMap = results.get(0);
6161
}
@@ -79,7 +79,7 @@ public void setExportMap(HashMap<String, SearchResult> newMap) {
7979
* @param item The new value to set for the column.
8080
*/
8181
public void set(Column column, Object item) {
82-
executeUpdate("UPDATE `" + this.getTable().getName() + "` SET `" + column.getName() + "` = '" + item.toString() + "' WHERE `" + this.column.getName() + "` = '" + this.item + "';", mysql);
82+
executeUpdate("UPDATE `" + this.getTable().getName() + "` SET `" + column.getName() + "` = '" + item.toString() + "' WHERE `" + this.column.getName() + "` = '" + this.item + "';", mysql, db.getName());
8383
}
8484

8585
/**
@@ -137,6 +137,6 @@ public int getId() {
137137
* @see QueryHelper#executeUpdate(String, MYSQL) for executing the deletion query
138138
*/
139139
public void drop() {
140-
executeUpdate("DELETE FROM `" + this.getTable().getName() + "` WHERE `" + this.column.getName() + "` = '" + this.item + "';", mysql);
140+
executeUpdate("DELETE FROM `" + this.getTable().getName() + "` WHERE `" + this.column.getName() + "` = '" + this.item + "';", mysql, db.getName());
141141
}
142142
}

src/main/java/de/goldendeveloper/mysql/entities/Table.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public List<String> describe() {
4949
list.add(rs.getString(1));
5050
}
5151
return list;
52-
}, mysql).get(0);
52+
}, mysql, db.getName()).get(0);
5353
return description != null ? description : new ArrayList<>();
5454
}
5555

@@ -67,7 +67,7 @@ public String getName() {
6767
* This method executes a SQL query to drop the table with the given name.
6868
*/
6969
public void drop() {
70-
this.executeUpdate("DROP TABLE `" + this.name + "`;", mysql);
70+
this.executeUpdate("DROP TABLE `" + this.name + "`;", mysql, db.getName());
7171
}
7272

7373
/**
@@ -93,6 +93,7 @@ public List<Row> getRows() {
9393
int index = 0;
9494
try {
9595
Statement statement = mysql.getConnect().createStatement();
96+
statement.execute("USE " + db.getName() + ";");
9697
ResultSet rs = statement.executeQuery("SELECT * FROM " + this.getName() + ";");
9798
while (rs.next()) {
9899
index++;
@@ -127,7 +128,7 @@ public HashMap<String, SearchResult> getMap(Column column, String item) {
127128
map.put(rsMetaData.getColumnName(i), new SearchResult(rs.getString(i)));
128129
}
129130
return map;
130-
}, mysql);
131+
}, mysql, db.getName());
131132
return !results.isEmpty() ? results.get(0) : new HashMap<>();
132133
}
133134

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

@@ -165,7 +166,7 @@ public boolean isEmpty() {
165166
* @param id The ID of the row to be dropped.
166167
*/
167168
public void dropRow(int id) {
168-
executeUpdate("DELETE FROM `" + this.name + "` where id = " + id + ";", mysql);
169+
executeUpdate("DELETE FROM `" + this.name + "` where id = " + id + ";", mysql, db.getName());
169170
}
170171

171172
/**
@@ -178,7 +179,7 @@ public List<Column> getColumns() {
178179
List<Column> list = new ArrayList<>();
179180
list.add(new Column(rs.getString(1), this, mysql));
180181
return list;
181-
}, mysql);
182+
}, mysql, db.getName());
182183
return !results.isEmpty() ? results.get(0) : new ArrayList<>();
183184
}
184185

@@ -202,7 +203,7 @@ public Column getColumn(String name) {
202203
* @return {@code true} if the column exists, {@code false} otherwise.
203204
*/
204205
public boolean existsColumn(String name) {
205-
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);
206+
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());
206207
return !results.isEmpty() && results.get(0);
207208
}
208209

@@ -215,7 +216,7 @@ public boolean existsColumn(String name) {
215216
* @return true if the row exists in the table, false otherwise.
216217
*/
217218
public boolean existsRow(Column column, String item) {
218-
List<Boolean> results = executeQuery("SELECT EXISTS(SELECT * FROM `" + this.getName() + "` WHERE `" + column.getName() + "` = '" + item + "')", rs -> rs.getBoolean(1), mysql);
219+
List<Boolean> results = executeQuery("SELECT EXISTS(SELECT * FROM `" + this.getName() + "` WHERE `" + column.getName() + "` = '" + item + "')", rs -> rs.getBoolean(1), mysql, db.getName());
219220
return !results.isEmpty() && results.get(0);
220221
}
221222

@@ -227,7 +228,7 @@ public boolean existsRow(Column column, String item) {
227228
* @param name The name of the column to set as unique.
228229
*/
229230
public void setUniqueColumn(String name) {
230-
executeUpdate("ALTER IGNORE TABLE `" + this.name + "` ADD UNIQUE (" + name + ");", mysql);
231+
executeUpdate("ALTER IGNORE TABLE `" + this.name + "` ADD UNIQUE (" + name + ");", mysql, db.getName());
231232
}
232233

233234
/**
@@ -236,7 +237,7 @@ public void setUniqueColumn(String name) {
236237
* @param name the name of the column to add
237238
*/
238239
public void addColumn(String name) {
239-
executeUpdate("ALTER TABLE `" + this.name + "` ADD `" + name + "` " + MysqlTypes.TEXT.getMysqlTypeName() + " (" + 65555 + ");", mysql);
240+
executeUpdate("ALTER TABLE `" + this.name + "` ADD `" + name + "` " + MysqlTypes.TEXT.getMysqlTypeName() + " (" + 65555 + ");", mysql, db.getName());
240241
}
241242

242243
/**
@@ -295,7 +296,7 @@ public void insert(HashMap<String, String> rowBuilder) {
295296
items.append(",'").append(item).append("'");
296297
}
297298
});
298-
executeUpdate("INSERT INTO `" + this.name + "` (" + keys + ")VALUES (" + items + ");", mysql);
299+
executeUpdate("INSERT INTO `" + this.name + "` (" + keys + ")VALUES (" + items + ");", mysql, db.getName());
299300
}
300301

301302
/**
@@ -375,7 +376,7 @@ private Row getRow(Column column, String query) {
375376
map.put(key, value);
376377
}
377378
return map;
378-
}, mysql);
379+
}, mysql, db.getName());
379380
if (!results.isEmpty()) {
380381
HashMap<String, SearchResult> exportMap = results.get(0);
381382
String id = exportMap.get("id").getAsString();

src/main/java/de/goldendeveloper/mysql/entities/enums/MysqlTypes.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ public enum MysqlTypes {
88

99
/**
1010
* Enumeration representing the variable "BIT".
11-
*
1211
* This enumeration is used to represent a bit variable.
13-
*
1412
* The value of this enumeration is a string "bit".
1513
*/
1614
BIT("bit"),
@@ -49,7 +47,6 @@ public enum MysqlTypes {
4947
INT("int"),
5048
/**
5149
* Represents a variable of type BIGINT.
52-
*
5350
* The BIGINT type in SQL is typically used to represent large integer values.
5451
*/
5552
BIGINT("bigint"),
@@ -86,9 +83,7 @@ public enum MysqlTypes {
8683
DOUBLE("double"),
8784
/**
8885
* This is a constant variable representing a date.
89-
*
9086
* The variable is stored as a string with the value "date".
91-
*
9287
* Usage example:
9388
* String myDate = DATE.getValue();
9489
*/
@@ -125,10 +120,8 @@ public enum MysqlTypes {
125120
* The CHAR variable represents a character type in Java.
126121
* It is used to store single characters and occupies 2 bytes in memory.
127122
* The value of a CHAR variable can be any valid Unicode character.
128-
*
129123
* Usage:
130124
* CHAR("char")
131-
*
132125
* Example:
133126
* char myChar = CHAR.getValue();
134127
*/
@@ -151,10 +144,8 @@ public enum MysqlTypes {
151144
NVARCHAR("nvarchar"),
152145
/**
153146
* Represents a binary value.
154-
*
155147
* Usage:
156148
* BINARY binary = BINARY("binary");
157-
*
158149
* This class can be used to store binary values and perform operations on them.
159150
*
160151
*/

src/main/java/de/goldendeveloper/mysql/entities/enums/Permissions.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77
public enum Permissions {
88
/**
99
* Represents the permission for all operations.
10-
*
1110
* The ALL variable is a constant of the Permissions enum class and has a value of 0.
1211
* It represents the permission that grants all operations to a user or database.
1312
* Use this permission to assign all available permissions to a user or database.
14-
*
1513
* Example usage:
1614
* Permissions allPermission = Permissions.ALL;
1715
* int permissionValue = allPermission.getValue();
@@ -23,10 +21,8 @@ public enum Permissions {
2321
DELETE(1),
2422
/**
2523
* Represents the permission for inserting data into a database.
26-
*
2724
* The INSERT variable is a constant of the Permissions enum class and has a value of 2.
2825
* It represents the permission that allows a user or database to insert data into tables.
29-
*
3026
* Example usage:
3127
* Permissions insertPermission = Permissions.INSERT;
3228
* int permissionValue = insertPermission.getValue();
@@ -39,39 +35,32 @@ public enum Permissions {
3935
REFERENCES(3),
4036
/**
4137
* The SELECT variable represents the permission for querying or retrieving data from a database.
42-
*
4338
* The SELECT variable is a constant of the Permissions enum class and has a value of 4.
4439
* It represents the permission that allows a user or database to execute SELECT statements on tables and views,
4540
* retrieving data from specific columns or all columns.
46-
*
4741
* Example usage:
4842
* Permissions selectPermission = Permissions.SELECT;
4943
* int permissionValue = selectPermission.getValue();
5044
*/
5145
SELECT(4),
5246
/**
5347
* Represents the permission for creating and managing triggers in a database.
54-
*
5548
* The TRIGGER variable is a constant of the Permissions enum class and*/
5649
TRIGGER(5),
5750
/**
5851
* Represents the permission for updating data in a database.
59-
*
6052
* The UPDATE variable is a constant of the Permissions enum class and has a value of 6.
6153
* It represents the permission that allows a user or database to execute UPDATE statements on tables, modifying data.
62-
*
6354
* Example usage:
6455
* Permissions updatePermission = Permissions.UPDATE;
6556
* int permissionValue = updatePermission.getValue();
6657
*/
6758
UPDATE(6),
6859
/**
6960
* Represents the permission for executing a procedure or function in a database.
70-
*
7161
* The EXECUTE variable is a constant of the Permissions enum class and has a value of 7.
7262
* It represents the permission that allows a user or database to execute stored procedures or functions.
7363
* This permission enables the user or database to invoke pre-defined logic in the database.
74-
*
7564
* Example usage:
7665
* Permissions executePermission = Permissions.EXECUTE;
7766
* int permissionValue = executePermission.getValue();

0 commit comments

Comments
 (0)