diff --git a/build.gradle b/build.gradle index 5b97452..d2ac8a8 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { } group 'tech.nevets.osql4j' -version '1.0.0' +version '1.1.0' 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 764713d..8619ae5 100644 --- a/src/main/java/tech/nevets/osql4j/SQLConnection.java +++ b/src/main/java/tech/nevets/osql4j/SQLConnection.java @@ -9,6 +9,7 @@ import java.sql.*; import java.util.*; public class SQLConnection { + private static String globalSQLiteLocation = null; private final Class clazz; private Connection connection; private Statement statement; @@ -37,9 +38,30 @@ public class SQLConnection { SQL_CMD_MAP.put("java.sql.Timestamp", "DATETIME"); } + /** + * 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 + */ + public SQLConnection(@NotNull Class clazz) { + this.clazz = clazz; + 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); + statement = connection.createStatement(); + } catch (SQLException e) { + e.printStackTrace(); + } + fields = getFields(); + sqlFormattedFields = getFieldArray(); + sqlFormattedFieldNames = getFieldNameArray(); + sqlFormattedFieldTypes = getFieldTypeArray(); + createTable(); + } + /** * 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 */ public SQLConnection(@NotNull Class clazz, String dbLocation) { this.clazz = clazz; @@ -183,6 +205,10 @@ public class SQLConnection { } } + public static void setGlobalDBLocation(String dbLocation) { + globalSQLiteLocation = dbLocation; + } + private void createTable() { try { String sql = "CREATE TABLE IF NOT EXISTS " +