@@ -360,6 +360,40 @@ public Optional<List<Object>> getList(@NotNull String table, @NotNull String key
360
360
return Optional .of (objects );
361
361
}
362
362
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
+
363
397
/**
364
398
* Check if a table exists
365
399
*
@@ -1014,6 +1048,32 @@ public Optional<List<?>> getList(String key, String value, String table, Class<?
1014
1048
return returnObjects .isEmpty () ? Optional .empty () : Optional .of (returnObjects );
1015
1049
}
1016
1050
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
+
1017
1077
/**
1018
1078
* Reads {@code Java Objects} from a table
1019
1079
*
@@ -1062,6 +1122,52 @@ private Object get(String table, String key, String value, Class<?> clazz, int i
1062
1122
return object ;
1063
1123
}
1064
1124
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
+
1065
1171
/**
1066
1172
* Retrieves the correct constructor for a class
1067
1173
*
@@ -1100,5 +1206,6 @@ private boolean hasAnnotation(Parameter param) {
1100
1206
private void log (@ NotNull String text ) {
1101
1207
System .out .println ("[Database] " + text );
1102
1208
}
1209
+
1103
1210
}
1104
1211
0 commit comments