Skip to content

Commit 158318b

Browse files
committed
Update pom.xml and refactor database-related classes
Bumped the version of MYSQL-Api in pom.xml to 5.7.2. Refactored code for better readability and efficiency in Database.java, MYSQL.java, and other related classes. The modified queries used in the executeQuery() method of QueryHelper interface now return a list of results, addressing previous issues when more than one row was queried.
1 parent 320c3c3 commit 158318b

File tree

7 files changed

+98
-84
lines changed

7 files changed

+98
-84
lines changed

pom.xml

Lines changed: 1 addition & 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.1</version>
8+
<version>5.7.2</version>
99
<packaging>jar</packaging>
1010
<url>https://github.com/Golden-Developer/MYSQL-Api</url>
1111

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

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ public MYSQL(String hostname, String username, String password) {
6565
/**
6666
* Constructs a MYSQL object with the specified hostname, username, password, and port.
6767
*
68-
* @param hostname The hostname of the MySQL server.
69-
* @param username The username to connect to the MySQL server.
70-
* @param password The password to connect to the MySQL server.
71-
* @param port The port of the MySQL server.
68+
* @param hostname The hostname of the MySQL server.
69+
* @param username The username to connect to the MySQL server.
70+
* @param password The password to connect to the MySQL server.
71+
* @param port The port of the MySQL server.
7272
* @param exceptionHandlerClass The ExceptionHandler class for handling exceptions.
73-
* @param <T> The type of the ExceptionHandler class.
73+
* @param <T> The type of the ExceptionHandler class.
7474
*/
7575
public <T extends ExceptionHandler> MYSQL(String hostname, String username, String password, int port, T exceptionHandlerClass) {
7676
this.hostname = hostname;
@@ -151,7 +151,8 @@ public String getHostname() {
151151
* @return The version of the MySQL server.
152152
*/
153153
public String getVersion() {
154-
return executeQuery("SELECT @@VERSION AS 'SQL Server Version Details'", rs -> rs.getString(1), this);
154+
List<String> results = executeQuery("SELECT @@VERSION AS 'SQL Server Version Details'", rs -> rs.getString(1), this);
155+
return !results.isEmpty() ? results.get(0) : null;
155156
}
156157

157158
/**
@@ -160,14 +161,7 @@ public String getVersion() {
160161
* @return A list of User objects representing the users in the MySQL server.
161162
*/
162163
public List<User> getUsers() {
163-
List<User> users = executeQuery("SELECT user FROM mysql.user;", rs -> {
164-
List<User> list = new ArrayList<>();
165-
do {
166-
list.add(new User(rs.getString(1), this));
167-
} while (rs.next());
168-
return list;
169-
}, this);
170-
return users != null ? users : new ArrayList<>();
164+
return executeQuery("SELECT user FROM mysql.user;", rs -> new User(rs.getString(1), this), this);
171165
}
172166

173167
/**
@@ -197,9 +191,8 @@ public boolean existsDatabase(String databaseName) {
197191
* @return {@code true} if the user exists, {@code false} otherwise.
198192
*/
199193
public boolean existsUser(String name) {
200-
String query = "SELECT COUNT(*) FROM mysql.user WHERE user = '" + name + "';";
201-
Integer count = executeQuery(query, rs -> rs.getInt(1), this);
202-
return count != null && count > 0;
194+
List<Integer> results = executeQuery("SELECT COUNT(*) FROM mysql.user WHERE user = '" + name + "';", rs -> rs.getInt(1), this);
195+
return !results.isEmpty() && results.get(0) > 0;
203196
}
204197

205198
/**
@@ -308,14 +301,7 @@ public User getOrCreateUser(String username, String password) {
308301
* @return a list of Database objects representing the databases in the MySQL server.
309302
*/
310303
public List<Database> getDatabases() {
311-
List<Database> databases = executeQuery("SHOW DATABASES;", rs -> {
312-
List<Database> list = new ArrayList<>();
313-
do {
314-
list.add(new Database(rs.getString(1), this));
315-
} while (rs.next());
316-
return list;
317-
}, this);
318-
return databases != null ? databases : new ArrayList<>();
304+
return executeQuery("SHOW DATABASES;", rs -> new Database(rs.getString(1), this), this);
319305
}
320306

321307
/**

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import de.goldendeveloper.mysql.MYSQL;
44
import de.goldendeveloper.mysql.interfaces.QueryHelper;
55

6-
import java.util.ArrayList;
76
import java.util.List;
87

98
/**
@@ -37,8 +36,8 @@ public Column(String name, Table table, MYSQL mysql) {
3736
* @return A SearchResults object containing all search results.
3837
*/
3938
public SearchResults getAll() {
40-
List<SearchResult> list = new ArrayList<>();
41-
executeQuery("SELECT `" + this.name + "` FROM `" + table.getName() + "`;", rs -> list.add(new SearchResult(rs.getString(1))), mysql);
39+
List<SearchResult> list = executeQuery("SELECT `" + this.name + "` FROM `" + table.getName() + "`;",
40+
rs -> new SearchResult(rs.getString(1)), mysql);
4241
return new SearchResults(list);
4342
}
4443

@@ -84,7 +83,13 @@ public void setNull() {
8483
* @return The boolean value of the column for the specified ID, or null if the ID does not exist or there is an error.
8584
*/
8685
public boolean getAsBoolean(int id) {
87-
return executeQuery("SELECT `" + this.name + "` FROM `" + table.getName() + "`;", rs -> rs.getObject(1).toString().equalsIgnoreCase("true") ? Boolean.TRUE : rs.getObject(1).toString().equalsIgnoreCase("false") ? false : null, mysql);
86+
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);
88+
if (!results.isEmpty()) {
89+
return results.get(0);
90+
} else {
91+
return false;
92+
}
8893
}
8994

9095
/**
@@ -94,9 +99,16 @@ public boolean getAsBoolean(int id) {
9499
* @return The string value of the column for the specified ID, or null if the ID does not exist or there is an error.
95100
*/
96101
public String getAsString(int id) {
97-
return executeQuery("SELECT `" + this.name + "` FROM `" + table.getName() + "`;", rs -> rs.getObject(1).toString(), mysql);
102+
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);
104+
if (!results.isEmpty()) {
105+
return results.get(0);
106+
} else {
107+
return null;
108+
}
98109
}
99110

111+
100112
/**
101113
* Sets the name of the column.
102114
*

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,10 @@ public Table getTable(String name) {
8787
*/
8888
public List<Table> getTables() {
8989
String query = "SHOW TABLES;";
90-
List<Table> tables = executeQuery(query, rs -> {
91-
List<Table> list = new ArrayList<>();
92-
do {
93-
list.add(new Table(rs.getString(1), this, mysql));
94-
} while (rs.next());
95-
return list;
96-
}, mysql);
97-
return tables != null ? tables : new ArrayList<>();
90+
return executeQuery(query, rs -> new Table(rs.getString(1), this, mysql), mysql);
9891
}
9992

93+
10094
/**
10195
* Creates a new table with the given name.
10296
*

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,19 @@ public Row(Table table, Column column, MYSQL mysql, String item) {
4848
public HashMap<String, SearchResult> getData() {
4949
if (exportMap.isEmpty()) {
5050
String query = "SELECT * FROM `" + this.getTable().getName() + "` WHERE `" + this.column.getName() + "` = '" + this.item + "';";
51-
exportMap = executeQuery(query, rs -> {
51+
List<HashMap<String, SearchResult>> results = executeQuery(query, rs -> {
5252
HashMap<String, SearchResult> map = new HashMap<>();
5353
ResultSetMetaData rsMetaData = rs.getMetaData();
5454
for (int i = 1; i <= rsMetaData.getColumnCount(); i++) {
55-
if (rs.getString(rsMetaData.getColumnName(i)) != null) {
56-
map.put(rsMetaData.getColumnName(i), new SearchResult(rs.getString(rsMetaData.getColumnName(i))));
57-
} else {
58-
map.put(rsMetaData.getColumnName(i), null);
59-
}
55+
map.put(rsMetaData.getColumnName(i), rs.getString(i) != null ? new SearchResult(rs.getString(i)) : null);
6056
}
6157
return map;
6258
}, mysql);
59+
if (!results.isEmpty()) {
60+
exportMap = results.get(0);
61+
}
6362
}
64-
return this.exportMap != null ? this.exportMap : new HashMap<>();
63+
return exportMap != null ? exportMap : new HashMap<>();
6564
}
6665

6766
/**
@@ -135,8 +134,6 @@ public int getId() {
135134
* DELETE FROM `tableName` WHERE `columnName` = 'columnValue';
136135
*
137136
* @see Row#getTable() to retrieve the table associated with this row
138-
* @see Row#column to retrieve the column associated with this row
139-
* @see Row#item to retrieve the value of the column in this row
140137
* @see QueryHelper#executeUpdate(String, MYSQL) for executing the deletion query
141138
*/
142139
public void drop() {

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

Lines changed: 34 additions & 30 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);
52+
}, mysql).get(0);
5353
return description != null ? description : new ArrayList<>();
5454
}
5555

@@ -119,22 +119,16 @@ public List<Row> getRows() {
119119
* @return A map of search results, where the key is the column name and the value is a SearchResult object representing the retrieved item.
120120
*/
121121
public HashMap<String, SearchResult> getMap(Column column, String item) {
122-
String query = "SELECT * FROM `" + this.getName() + "` WHERE " + column.getName() + " = '" + item + "';";
123-
HashMap<String, SearchResult> exportMap = executeQuery(query, rs -> {
122+
String query = "SELECT * FROM `" + this.getName() + "` WHERE `" + column.getName() + "` = '" + item + "';";
123+
List<HashMap<String, SearchResult>> results = executeQuery(query, rs -> {
124124
HashMap<String, SearchResult> map = new HashMap<>();
125125
ResultSetMetaData rsMetaData = rs.getMetaData();
126-
for (int i = 1; i <= this.countColumn(); i++) {
127-
if (!rsMetaData.getColumnName(i).isEmpty()) {
128-
if (rs.getString(rsMetaData.getColumnName(i)) != null) {
129-
map.put(rsMetaData.getColumnName(i), new SearchResult(rs.getString(rsMetaData.getColumnName(i))));
130-
} else {
131-
map.put(rsMetaData.getColumnName(i), null);
132-
}
133-
}
126+
for (int i = 1; i <= rsMetaData.getColumnCount(); i++) {
127+
map.put(rsMetaData.getColumnName(i), new SearchResult(rs.getString(i)));
134128
}
135129
return map;
136130
}, mysql);
137-
return exportMap != null ? exportMap : new HashMap<>();
131+
return !results.isEmpty() ? results.get(0) : new HashMap<>();
138132
}
139133

140134
/**
@@ -143,7 +137,8 @@ public HashMap<String, SearchResult> getMap(Column column, String item) {
143137
* @return The number of rows in the table.
144138
*/
145139
public int countRows() {
146-
return executeQuery("SELECT COUNT(*) FROM `" + this.name + "`;", rs -> rs.getInt(1), mysql);
140+
List<Integer> results = executeQuery("SELECT COUNT(*) FROM `" + this.name + "`;", rs -> rs.getInt(1), mysql);
141+
return !results.isEmpty() ? results.get(0) : 0;
147142
}
148143

149144
/**
@@ -179,15 +174,12 @@ public void dropRow(int id) {
179174
* @return A List of Column objects representing the columns in the table.
180175
*/
181176
public List<Column> getColumns() {
182-
String query = "SHOW COLUMNS FROM `" + this.name + "` ;";
183-
List<Column> columns = executeQuery(query, rs -> {
177+
List<List<Column>> results = executeQuery("SHOW COLUMNS FROM `" + this.name + "`;", rs -> {
184178
List<Column> list = new ArrayList<>();
185-
do {
186-
list.add(new Column(rs.getString(1), this, mysql));
187-
} while (rs.next());
179+
list.add(new Column(rs.getString(1), this, mysql));
188180
return list;
189181
}, mysql);
190-
return columns != null ? columns : new ArrayList<>();
182+
return !results.isEmpty() ? results.get(0) : new ArrayList<>();
191183
}
192184

193185
/**
@@ -210,9 +202,8 @@ public Column getColumn(String name) {
210202
* @return {@code true} if the column exists, {@code false} otherwise.
211203
*/
212204
public boolean existsColumn(String name) {
213-
String query = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" + this.name + "' AND COLUMN_NAME = '" + name + "';";
214-
Boolean exists = executeQuery(query, rs -> true, mysql);
215-
return exists != null && exists;
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+
return !results.isEmpty() && results.get(0);
216207
}
217208

218209

@@ -224,9 +215,8 @@ public boolean existsColumn(String name) {
224215
* @return true if the row exists in the table, false otherwise.
225216
*/
226217
public boolean existsRow(Column column, String item) {
227-
String query = "SELECT EXISTS(SELECT * FROM `" + this.getName() + "` WHERE `" + column.getName() + "` = '" + item + "')";
228-
Boolean exists = executeQuery(query, rs -> rs.getBoolean(1), mysql);
229-
return exists != null && exists;
218+
List<Boolean> results = executeQuery("SELECT EXISTS(SELECT * FROM `" + this.getName() + "` WHERE `" + column.getName() + "` = '" + item + "')", rs -> rs.getBoolean(1), mysql);
219+
return !results.isEmpty() && results.get(0);
230220
}
231221

232222

@@ -376,13 +366,27 @@ public Row getOldestRow() {
376366
}
377367

378368
private Row getRow(Column column, String query) {
379-
HashMap<String, SearchResult> exportMap = executeQuery(query, this::fillMap, mysql);
380-
String id = exportMap.get("id").getAsString();
381-
Row row = new Row(this, column, mysql, id);
382-
row.setExportMap(exportMap);
383-
return row;
369+
List<HashMap<String, SearchResult>> results = executeQuery(query, rs -> {
370+
HashMap<String, SearchResult> map = new HashMap<>();
371+
ResultSetMetaData rsMetaData = rs.getMetaData();
372+
for (int i = 1; i <= rsMetaData.getColumnCount(); i++) {
373+
String key = rsMetaData.getColumnName(i);
374+
SearchResult value = new SearchResult(rs.getString(i));
375+
map.put(key, value);
376+
}
377+
return map;
378+
}, mysql);
379+
if (!results.isEmpty()) {
380+
HashMap<String, SearchResult> exportMap = results.get(0);
381+
String id = exportMap.get("id").getAsString();
382+
Row row = new Row(this, column, mysql, id);
383+
row.setExportMap(exportMap);
384+
return row;
385+
}
386+
return null; // Or throw an exception if no row is found
384387
}
385388

389+
386390
/**
387391
* Retrieves a random row from the table.
388392
*

src/main/java/de/goldendeveloper/mysql/interfaces/QueryHelper.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import java.sql.ResultSet;
66
import java.sql.Statement;
7+
import java.util.ArrayList;
8+
import java.util.List;
79

810
public interface QueryHelper {
911

@@ -16,20 +18,39 @@ public interface QueryHelper {
1618
* @param <T> the type of the query result
1719
* @return the processed query result, or null if an exception occurred
1820
*/
19-
default <T> T executeQuery(String query, ResultSetHandler<T> handler, MYSQL mysql) {
21+
// default <T> T executeQuery(String query, ResultSetHandler<T> handler, MYSQL mysql) {
22+
// try {
23+
// Statement statement = mysql.getConnect().createStatement();
24+
// ResultSet rs = statement.executeQuery(query);
25+
// T result = null;
26+
// if (rs.next()) {
27+
// result = handler.handle(rs);
28+
// }
29+
// mysql.closeRsAndSt(rs, statement);
30+
// return result;
31+
// } catch (Exception e) {
32+
// mysql.callException(e);
33+
// }
34+
// return null;
35+
// }
36+
37+
38+
default <T> List<T> executeQuery(String query, ResultSetHandler<T> handler, MYSQL mysql) {
39+
List<T> results = new ArrayList<>();
2040
try {
2141
Statement statement = mysql.getConnect().createStatement();
2242
ResultSet rs = statement.executeQuery(query);
23-
T result = null;
24-
if (rs.next()) {
25-
result = handler.handle(rs);
43+
while (rs.next()) { // Ändern Sie `if` zu `while`, um alle Zeilen zu verarbeiten
44+
T result = handler.handle(rs);
45+
if (result != null) {
46+
results.add(result);
47+
}
2648
}
2749
mysql.closeRsAndSt(rs, statement);
28-
return result;
2950
} catch (Exception e) {
3051
mysql.callException(e);
3152
}
32-
return null;
53+
return results; // Gibt eine Liste von Ergebnissen zurück
3354
}
3455

3556
/**

0 commit comments

Comments
 (0)