diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml index be81b15..1cfff17 100644 --- a/.idea/dataSources.xml +++ b/.idea/dataSources.xml @@ -13,5 +13,20 @@ + + sqlite.xerial + true + org.sqlite.JDBC + jdbc:sqlite:C:\Users\stracey.intern\IdeaProjects\OSQL4J\database.db + $ProjectFileDir$ + + + file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.39.2/sqlite-jdbc-3.39.2.jar + + + file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.39.2/sqlite-jdbc-3.39.2.jar + + + \ No newline at end of file diff --git a/build.gradle b/build.gradle index 2d9c632..710a7b5 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { } group 'tech.nevets.osql4j' -version '1.1.2' +version '1.1.3' sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 diff --git a/src/main/java/tech/nevets/osql4j/SQLConnection.java b/src/main/java/tech/nevets/osql4j/SQLConnection.java index d633e87..e1bd06f 100644 --- a/src/main/java/tech/nevets/osql4j/SQLConnection.java +++ b/src/main/java/tech/nevets/osql4j/SQLConnection.java @@ -2,9 +2,11 @@ package tech.nevets.osql4j; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import tech.nevets.osql4j.annotations.SQLSerializable; +import tech.nevets.osql4j.exceptions.InvalidTypeException; import java.lang.reflect.Field; -import java.lang.reflect.Array; +import java.lang.reflect.InvocationTargetException; import java.sql.*; import java.util.*; @@ -41,9 +43,13 @@ public class SQLConnection { /** * This constructor is for quickly creating a new instance when the globalSQLiteLocation is set with {@link tech.nevets.osql4j.SQLConnection#setGlobalDBLocation(String dbLocation)} * @param clazz Class of the object you want to use to structure the database + * @throws InvalidTypeException Throws this exception if the passed type is not properly annotated with {@link tech.nevets.osql4j.annotations.SQLSerializable} */ public SQLConnection(@NotNull Class clazz) { this.clazz = clazz; + if (!clazz.isAnnotationPresent(SQLSerializable.class)) { + throw new InvalidTypeException("Type " + clazz.getSimpleName() + " is not annotated with SQLSerializable!"); + } if (globalSQLiteLocation == null) throw new UnsupportedOperationException("Global Location is not set, please use the constructor that passes a location or set the global db location"); try { connection = DriverManager.getConnection("jdbc:sqlite:" + globalSQLiteLocation); @@ -62,9 +68,13 @@ public class SQLConnection { * This constructor is only to be used if using sqlite as a database * @param clazz Class of the object you want to use to structure the database * @param dbLocation location of the database file + * @throws InvalidTypeException Throws this exception if the passed type is not properly annotated with {@link tech.nevets.osql4j.annotations.SQLSerializable} */ public SQLConnection(@NotNull Class clazz, String dbLocation) { this.clazz = clazz; + if (!clazz.isAnnotationPresent(SQLSerializable.class)) { + throw new InvalidTypeException("Type " + clazz.getSimpleName() + " is not annotated with SQLSerializable!"); + } try { connection = DriverManager.getConnection("jdbc:sqlite:" + dbLocation); statement = connection.createStatement(); @@ -87,9 +97,13 @@ public class SQLConnection { * @param dbSchema Schema to use. EX: mysql://example.com/(schema). Pass (schema) * @param dbUser Username credential for the Server * @param dbPass Password credential for the Server + * @throws InvalidTypeException Throws this exception if the passed type is not properly annotated with {@link tech.nevets.osql4j.annotations.SQLSerializable} */ public SQLConnection(@NotNull Class clazz, @NotNull String dbType, @Nullable String dbLocation, @Nullable String dbUrl, @Nullable String dbSchema, @Nullable String dbUser, @Nullable String dbPass) { this.clazz = clazz; + if (!clazz.isAnnotationPresent(SQLSerializable.class)) { + throw new InvalidTypeException("Type " + clazz.getSimpleName() + " is not annotated with SQLSerializable!"); + } switch (dbType.toLowerCase()) { case "mysql" -> { try { @@ -220,13 +234,35 @@ public class SQLConnection { } } + private T testCreateObj() { + T obj = null; + try { + obj = clazz.getConstructor(Class.forName(fields.get(0).getType().getName())).newInstance("Hahaha"); + } catch (NoSuchMethodException | ClassNotFoundException | InstantiationException | IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + return obj; + } + private T getFromDatabase(ResultSet results) { try { T object = clazz.getConstructor().newInstance(); for (int i = 0; i < fields.size(); i++) { String fieldName = fields.get(i).getName(); String fieldType = fields.get(i).getType().getName(); - Field objectField = object.getClass().getDeclaredField(fieldName); + Field objectField = null; + for (Class c = clazz; c != null; c = c.getSuperclass()) { + try { + objectField = object.getClass().getDeclaredField(fieldName); + } catch (NoSuchFieldException e) { + continue; + } + break; + } + if (objectField == null) { + break; + } + objectField.setAccessible(true); switch (fieldType) { diff --git a/src/main/java/tech/nevets/osql4j/annotations/SQLSerializable.java b/src/main/java/tech/nevets/osql4j/annotations/SQLSerializable.java new file mode 100644 index 0000000..1d9391d --- /dev/null +++ b/src/main/java/tech/nevets/osql4j/annotations/SQLSerializable.java @@ -0,0 +1,12 @@ +package tech.nevets.osql4j.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface SQLSerializable { + +} diff --git a/src/main/java/tech/nevets/osql4j/exceptions/InvalidTypeException.java b/src/main/java/tech/nevets/osql4j/exceptions/InvalidTypeException.java new file mode 100644 index 0000000..81e1dd8 --- /dev/null +++ b/src/main/java/tech/nevets/osql4j/exceptions/InvalidTypeException.java @@ -0,0 +1,7 @@ +package tech.nevets.osql4j.exceptions; + +public class InvalidTypeException extends RuntimeException { + public InvalidTypeException(String error) { + super(error); + } +} diff --git a/src/test/java/NewDBTest.java b/src/test/java/NewDBTest.java index 0eba2ef..52e486d 100644 --- a/src/test/java/NewDBTest.java +++ b/src/test/java/NewDBTest.java @@ -1,13 +1,13 @@ -import examplebeans.Animal; +import examplebeans.Cat; import tech.nevets.osql4j.SQLConnection; public class NewDBTest { public static void main(String[] args) { SQLConnection.setGlobalDBLocation("./database.db"); - SQLConnection connection = new SQLConnection<>(Animal.class); - connection.writeObject(new Animal("Fuzzy", 6, "brown")); + SQLConnection connection = new SQLConnection<>(Cat.class); + connection.writeObject(new Cat("Fuzzy", 6, "brown", true)); - for (Animal animal : connection.getAllObjects()) { + for (Cat animal : connection.getAllObjects()) { System.out.println(animal); } connection.close(); diff --git a/src/test/java/TestReverseForLoop.java b/src/test/java/TestReverseForLoop.java new file mode 100644 index 0000000..459763e --- /dev/null +++ b/src/test/java/TestReverseForLoop.java @@ -0,0 +1,14 @@ +import java.util.List; + +public class TestReverseForLoop { + public static void main(String[] args) { + List> stringListList = List.of(List.of("Apple", "Orange", "Peach"), List.of("Green", "Blue", "Yellow", "Purple"), List.of("Dog", "Cat")); + + for (List stringList : stringListList) { + System.out.println(stringList.size()); + for (int i = stringList.size() - 1; i > -1; i--) { + + } + } + } +} diff --git a/src/test/java/examplebeans/Animal.java b/src/test/java/examplebeans/Animal.java index 503cbc4..f391e76 100644 --- a/src/test/java/examplebeans/Animal.java +++ b/src/test/java/examplebeans/Animal.java @@ -1,5 +1,8 @@ package examplebeans; +import tech.nevets.osql4j.annotations.SQLSerializable; + +@SQLSerializable public class Animal { private String name; private int age; @@ -39,6 +42,6 @@ public class Animal { @Override public String toString() { - return "Animal: " + name + ", " + age + ", " + color; + return "(Animal: " + name + ", " + age + ", " + color + ")"; } } diff --git a/src/test/java/examplebeans/Cat.java b/src/test/java/examplebeans/Cat.java index e0d805a..fc00aca 100644 --- a/src/test/java/examplebeans/Cat.java +++ b/src/test/java/examplebeans/Cat.java @@ -1,5 +1,17 @@ package examplebeans; public class Cat extends Animal { + private boolean isFerrel; + public Cat() {} + + public Cat(String name, int age, String color, boolean isFerrel) { + super(name, age, color); + this.isFerrel = isFerrel; + } + + @Override + public String toString() { + return "(Cat: " + getName() + ", " + getAge() + ", " + getColor() + ", " + isFerrel + ")"; + } }