Progress has been made

This commit is contained in:
Steven Tracey 2022-11-15 15:50:13 -05:00
parent a8820fafbe
commit fcdcfff7e0
9 changed files with 107 additions and 8 deletions

View File

@ -13,5 +13,20 @@
</library> </library>
</libraries> </libraries>
</data-source> </data-source>
<data-source source="LOCAL" name="database [2]" uuid="9dfa3ed2-d64d-4bfd-8385-34033293fa6c">
<driver-ref>sqlite.xerial</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>org.sqlite.JDBC</jdbc-driver>
<jdbc-url>jdbc:sqlite:C:\Users\stracey.intern\IdeaProjects\OSQL4J\database.db</jdbc-url>
<working-dir>$ProjectFileDir$</working-dir>
<libraries>
<library>
<url>file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.39.2/sqlite-jdbc-3.39.2.jar</url>
</library>
<library>
<url>file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.39.2/sqlite-jdbc-3.39.2.jar</url>
</library>
</libraries>
</data-source>
</component> </component>
</project> </project>

View File

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

View File

@ -2,9 +2,11 @@ package tech.nevets.osql4j;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; 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.Field;
import java.lang.reflect.Array; import java.lang.reflect.InvocationTargetException;
import java.sql.*; import java.sql.*;
import java.util.*; import java.util.*;
@ -41,9 +43,13 @@ public class SQLConnection<T> {
/** /**
* This constructor is for quickly creating a new instance when the globalSQLiteLocation is set with {@link tech.nevets.osql4j.SQLConnection#setGlobalDBLocation(String dbLocation)} * 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 * @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<T> clazz) { public SQLConnection(@NotNull Class<T> clazz) {
this.clazz = 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"); 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 { try {
connection = DriverManager.getConnection("jdbc:sqlite:" + globalSQLiteLocation); connection = DriverManager.getConnection("jdbc:sqlite:" + globalSQLiteLocation);
@ -62,9 +68,13 @@ public class SQLConnection<T> {
* This constructor is only to be used if using sqlite as a database * 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 clazz Class of the object you want to use to structure the database
* @param dbLocation location of the database file * @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<T> clazz, String dbLocation) { public SQLConnection(@NotNull Class<T> clazz, String dbLocation) {
this.clazz = clazz; this.clazz = clazz;
if (!clazz.isAnnotationPresent(SQLSerializable.class)) {
throw new InvalidTypeException("Type " + clazz.getSimpleName() + " is not annotated with SQLSerializable!");
}
try { try {
connection = DriverManager.getConnection("jdbc:sqlite:" + dbLocation); connection = DriverManager.getConnection("jdbc:sqlite:" + dbLocation);
statement = connection.createStatement(); statement = connection.createStatement();
@ -87,9 +97,13 @@ public class SQLConnection<T> {
* @param dbSchema Schema to use. EX: mysql://example.com/(schema). Pass (schema) * @param dbSchema Schema to use. EX: mysql://example.com/(schema). Pass (schema)
* @param dbUser Username credential for the Server * @param dbUser Username credential for the Server
* @param dbPass Password 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<T> clazz, @NotNull String dbType, @Nullable String dbLocation, @Nullable String dbUrl, @Nullable String dbSchema, @Nullable String dbUser, @Nullable String dbPass) { public SQLConnection(@NotNull Class<T> clazz, @NotNull String dbType, @Nullable String dbLocation, @Nullable String dbUrl, @Nullable String dbSchema, @Nullable String dbUser, @Nullable String dbPass) {
this.clazz = clazz; this.clazz = clazz;
if (!clazz.isAnnotationPresent(SQLSerializable.class)) {
throw new InvalidTypeException("Type " + clazz.getSimpleName() + " is not annotated with SQLSerializable!");
}
switch (dbType.toLowerCase()) { switch (dbType.toLowerCase()) {
case "mysql" -> { case "mysql" -> {
try { try {
@ -220,13 +234,35 @@ public class SQLConnection<T> {
} }
} }
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) { private T getFromDatabase(ResultSet results) {
try { try {
T object = clazz.getConstructor().newInstance(); T object = clazz.getConstructor().newInstance();
for (int i = 0; i < fields.size(); i++) { for (int i = 0; i < fields.size(); i++) {
String fieldName = fields.get(i).getName(); String fieldName = fields.get(i).getName();
String fieldType = fields.get(i).getType().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); objectField.setAccessible(true);
switch (fieldType) { switch (fieldType) {

View File

@ -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 {
}

View File

@ -0,0 +1,7 @@
package tech.nevets.osql4j.exceptions;
public class InvalidTypeException extends RuntimeException {
public InvalidTypeException(String error) {
super(error);
}
}

View File

@ -1,13 +1,13 @@
import examplebeans.Animal; import examplebeans.Cat;
import tech.nevets.osql4j.SQLConnection; import tech.nevets.osql4j.SQLConnection;
public class NewDBTest { public class NewDBTest {
public static void main(String[] args) { public static void main(String[] args) {
SQLConnection.setGlobalDBLocation("./database.db"); SQLConnection.setGlobalDBLocation("./database.db");
SQLConnection<Animal> connection = new SQLConnection<>(Animal.class); SQLConnection<Cat> connection = new SQLConnection<>(Cat.class);
connection.writeObject(new Animal("Fuzzy", 6, "brown")); connection.writeObject(new Cat("Fuzzy", 6, "brown", true));
for (Animal animal : connection.getAllObjects()) { for (Cat animal : connection.getAllObjects()) {
System.out.println(animal); System.out.println(animal);
} }
connection.close(); connection.close();

View File

@ -0,0 +1,14 @@
import java.util.List;
public class TestReverseForLoop {
public static void main(String[] args) {
List<List<String>> stringListList = List.of(List.of("Apple", "Orange", "Peach"), List.of("Green", "Blue", "Yellow", "Purple"), List.of("Dog", "Cat"));
for (List<String> stringList : stringListList) {
System.out.println(stringList.size());
for (int i = stringList.size() - 1; i > -1; i--) {
}
}
}
}

View File

@ -1,5 +1,8 @@
package examplebeans; package examplebeans;
import tech.nevets.osql4j.annotations.SQLSerializable;
@SQLSerializable
public class Animal { public class Animal {
private String name; private String name;
private int age; private int age;
@ -39,6 +42,6 @@ public class Animal {
@Override @Override
public String toString() { public String toString() {
return "Animal: " + name + ", " + age + ", " + color; return "(Animal: " + name + ", " + age + ", " + color + ")";
} }
} }

View File

@ -1,5 +1,17 @@
package examplebeans; package examplebeans;
public class Cat extends Animal { 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 + ")";
}
} }