Generic Arrays just dont work

This commit is contained in:
Steven Tracey 2022-11-15 10:32:14 -05:00
parent c283f52f44
commit a8820fafbe
3 changed files with 23 additions and 22 deletions

View File

@ -5,7 +5,7 @@ plugins {
} }
group 'tech.nevets.osql4j' group 'tech.nevets.osql4j'
version '1.1.1' version '1.1.2'
sourceCompatibility = JavaVersion.VERSION_17 sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17

View File

@ -150,7 +150,7 @@ public class SQLConnection<T> {
* @param id the value that will be searched for * @param id the value that will be searched for
* @return Array of all found objects * @return Array of all found objects
*/ */
public T[] getObjectsById(String column, String id) { public List<T> getObjectsById(String column, String id) {
List<T> objects = new ArrayList<>(); List<T> objects = new ArrayList<>();
try { try {
String sql = "SELECT * FROM " + clazz.getSimpleName() + " WHERE " + column + "=" + id + ";"; String sql = "SELECT * FROM " + clazz.getSimpleName() + " WHERE " + column + "=" + id + ";";
@ -162,12 +162,10 @@ public class SQLConnection<T> {
System.out.println("Error parsing object from database..."); System.out.println("Error parsing object from database...");
e.printStackTrace(); e.printStackTrace();
} }
@SuppressWarnings("unchecked") return objects;
T[] objectArray = (T[]) Array.newInstance(this.clazz, objects.size());
return objectArray;
} }
public T[] getAllObjects() { public List<T> getAllObjects() {
List<T> objects = new ArrayList<>(); List<T> objects = new ArrayList<>();
try { try {
String sql = "SELECT * FROM " + clazz.getSimpleName() + ";"; String sql = "SELECT * FROM " + clazz.getSimpleName() + ";";
@ -179,9 +177,8 @@ public class SQLConnection<T> {
System.out.println("Error parsing objects from database..."); System.out.println("Error parsing objects from database...");
e.printStackTrace(); e.printStackTrace();
} }
@SuppressWarnings("unchecked")
T[] objectArray = (T[]) Array.newInstance(this.clazz, objects.size()); return objects;
return objectArray;
} }
public DatabaseMetaData getMetadata() { public DatabaseMetaData getMetadata() {
@ -233,19 +230,19 @@ public class SQLConnection<T> {
objectField.setAccessible(true); objectField.setAccessible(true);
switch (fieldType) { switch (fieldType) {
case "boolean", "java.lang.Boolean" -> objectField.setBoolean(object, results.getBoolean(i + 2)); case "boolean", "java.lang.Boolean" -> objectField.setBoolean(object, results.getBoolean(i + 1));
case "byte", "java.lang.Byte" -> objectField.setByte(object, results.getByte(i + 2)); case "byte", "java.lang.Byte" -> objectField.setByte(object, results.getByte(i + 1));
case "byte[]", "java.lang.Byte[]" -> objectField.set(object, results.getBytes(i + 2)); case "byte[]", "java.lang.Byte[]" -> objectField.set(object, results.getBytes(i + 1));
case "double", "java.lang.Double" -> objectField.setDouble(object, results.getFloat(i + 2)); case "double", "java.lang.Double" -> objectField.setDouble(object, results.getFloat(i + 1));
case "float", "java.lang.Float" -> objectField.setFloat(object, results.getFloat(i + 2)); case "float", "java.lang.Float" -> objectField.setFloat(object, results.getFloat(i + 1));
case "int", "java.lang.Integer" -> objectField.setInt(object, results.getInt(i + 2)); case "int", "java.lang.Integer" -> objectField.setInt(object, results.getInt(i + 1));
case "long", "java.lang.Long" -> objectField.setLong(object, results.getLong(i + 2)); case "long", "java.lang.Long" -> objectField.setLong(object, results.getLong(i + 1));
case "short", "java.lang.Short" -> objectField.setShort(object, results.getShort(i + 2)); case "short", "java.lang.Short" -> objectField.setShort(object, results.getShort(i + 1));
case "java.lang.String" -> objectField.set(object, results.getString(i + 2)); case "java.lang.String" -> objectField.set(object, results.getString(i + 1));
case "java.math.BigDecimal" -> objectField.set(object, results.getBigDecimal(i + 2)); case "java.math.BigDecimal" -> objectField.set(object, results.getBigDecimal(i + 1));
case "java.sql.Date" -> objectField.set(object, results.getDate(i + 2)); case "java.sql.Date" -> objectField.set(object, results.getDate(i + 1));
case "java.sql.Time" -> objectField.set(object, results.getTime(i + 2)); case "java.sql.Time" -> objectField.set(object, results.getTime(i + 1));
case "java.sql.TimeStamp" -> objectField.set(object, results.getTimestamp(i + 2)); case "java.sql.TimeStamp" -> objectField.set(object, results.getTimestamp(i + 1));
} }
} }
return object; return object;

View File

@ -6,6 +6,10 @@ public class NewDBTest {
SQLConnection.setGlobalDBLocation("./database.db"); SQLConnection.setGlobalDBLocation("./database.db");
SQLConnection<Animal> connection = new SQLConnection<>(Animal.class); SQLConnection<Animal> connection = new SQLConnection<>(Animal.class);
connection.writeObject(new Animal("Fuzzy", 6, "brown")); connection.writeObject(new Animal("Fuzzy", 6, "brown"));
for (Animal animal : connection.getAllObjects()) {
System.out.println(animal);
}
connection.close(); connection.close();
} }
} }