Skip to content

Commit 7f0c6fb

Browse files
committed
feat: Added options to retrieve every object from a table
1 parent 2bc7b1f commit 7f0c6fb

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

src/main/java/com/seailz/databaseapi/Database.java

+107
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,40 @@ public Optional<List<Object>> getList(@NotNull String table, @NotNull String key
360360
return Optional.of(objects);
361361
}
362362

363+
/**
364+
* Get something from the database
365+
* <p></p>
366+
* <p>The "column" parameter would be the specific detail you'd like to get. For example, </p>
367+
* <p>if my table contained a "age" column, and I wanted to get the player's age,</p>
368+
* <p>I'd set the column parameter to "age"</p>
369+
* <p>
370+
*
371+
* @param table the table you'd like to pull from
372+
* @param column The column you'd like to get
373+
* @return An object
374+
* @throws SQLException if there is an error retrieving the request value
375+
*/
376+
@Nullable
377+
public Optional<List<Object>> getList(@NotNull String table, @NotNull String column) throws SQLException {
378+
String statement = "SELECT * FROM `" + table + "`";
379+
ResultSet set = new Statement(statement, connection).executeWithResults();
380+
381+
if (debug)
382+
log("Getting " + column + " from " + table);
383+
384+
List<Object> objects = new ArrayList<>();
385+
while (set.next()) {
386+
objects.add(set.getObject(column));
387+
}
388+
389+
if (debug)
390+
log("Getting value from table " + table + " failed");
391+
392+
if (objects.isEmpty())
393+
return Optional.empty();
394+
return Optional.of(objects);
395+
}
396+
363397
/**
364398
* Check if a table exists
365399
*
@@ -1014,6 +1048,32 @@ public Optional<List<?>> getList(String key, String value, String table, Class<?
10141048
return returnObjects.isEmpty() ? Optional.empty() : Optional.of(returnObjects);
10151049
}
10161050

1051+
/**
1052+
* Reads a list of {@code Java Objects} from a table. This method assumes that every row in the list represents the same object.
1053+
* @param table The table you'd like to read from
1054+
* @param clazz The type of class you want to return
1055+
* @return An Optional class either containing a List<?> or nothing if it didn't find any results</?>
1056+
* @throws SQLException If there is an error communicating with the database
1057+
* @throws InvocationTargetException If there is an error invoking the object
1058+
* @throws InstantiationException If there is an error instantiating the object
1059+
* @throws IllegalAccessException If there is an error accessing some parameters within the object
1060+
*/
1061+
public Optional<List<?>> getList(String table, Class<?> clazz) throws SQLException, InvocationTargetException, InstantiationException, IllegalAccessException {
1062+
String statement = "SELECT * FROM `" + table + "`;";
1063+
if (debug)
1064+
log("Reading objects from table: " + table);
1065+
ResultSet resultSet = new Statement(statement, connection).executeWithResults();
1066+
List<Object> returnObjects = new ArrayList<>();
1067+
1068+
int i = 0;
1069+
while (resultSet.next()) {
1070+
returnObjects.add(get(table, clazz, i));
1071+
i++;
1072+
}
1073+
1074+
return returnObjects.isEmpty() ? Optional.empty() : Optional.of(returnObjects);
1075+
}
1076+
10171077
/**
10181078
* Reads {@code Java Objects} from a table
10191079
*
@@ -1062,6 +1122,52 @@ private Object get(String table, String key, String value, Class<?> clazz, int i
10621122
return object;
10631123
}
10641124

1125+
/**
1126+
* Reads {@code Java Objects} from a table
1127+
*
1128+
* @param table The table you'd like to read from
1129+
* @param clazz The class you'd like to read into
1130+
* @return The object you read into
1131+
* @throws SQLException if there is an error communicating with the database
1132+
* @throws IllegalAccessException if there is an error accessing the object
1133+
* @throws InstantiationException if there is an error instantiating the object
1134+
* @throws InvocationTargetException if there is an error invoking the object
1135+
*/
1136+
private Object get(String table, Class<?> clazz, int index) throws SQLException, InvocationTargetException, InstantiationException, IllegalAccessException {
1137+
String statement = "SELECT * FROM `" + table + "`;";
1138+
if (debug)
1139+
log("Reading object from table: " + table);
1140+
ResultSet resultSet = new Statement(statement, connection).executeWithResults();
1141+
1142+
// Creates a new instance of the class
1143+
Object object;
1144+
Constructor<?> constructor = retrieveConstructor(clazz);
1145+
ArrayList<Object> parameters = new ArrayList<>();
1146+
1147+
HashMap<String, Object> keyValuesHashMap = new HashMap<>();
1148+
1149+
int resultSetIndex = 0;
1150+
while (resultSet.next()) {
1151+
// Loops through all the columns and adds them to the HashMap
1152+
if (resultSetIndex == index) {
1153+
for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) {
1154+
keyValuesHashMap.put(resultSet.getMetaData().getColumnName(i), resultSet.getObject(i));
1155+
}
1156+
}
1157+
resultSetIndex++;
1158+
}
1159+
1160+
for (Parameter p : constructor.getParameters()) {
1161+
if (hasAnnotation(p))
1162+
parameters.add(keyValuesHashMap.get(p.getAnnotation(com.seailz.databaseapi.annotation.Column.class).value()));
1163+
}
1164+
1165+
if (debug)
1166+
log("Read object from table: " + table);
1167+
object = constructor.newInstance(parameters.toArray());
1168+
return object;
1169+
}
1170+
10651171
/**
10661172
* Retrieves the correct constructor for a class
10671173
*
@@ -1100,5 +1206,6 @@ private boolean hasAnnotation(Parameter param) {
11001206
private void log(@NotNull String text) {
11011207
System.out.println("[Database] " + text);
11021208
}
1209+
11031210
}
11041211

0 commit comments

Comments
 (0)