Added global sqlite location

This commit is contained in:
Steven Tracey 2022-11-14 08:41:48 -05:00
parent d05e074c2e
commit 93b07e29a5
2 changed files with 27 additions and 1 deletions

View File

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

View File

@ -9,6 +9,7 @@ import java.sql.*;
import java.util.*; import java.util.*;
public class SQLConnection<T> { public class SQLConnection<T> {
private static String globalSQLiteLocation = null;
private final Class<T> clazz; private final Class<T> clazz;
private Connection connection; private Connection connection;
private Statement statement; private Statement statement;
@ -37,9 +38,30 @@ public class SQLConnection<T> {
SQL_CMD_MAP.put("java.sql.Timestamp", "DATETIME"); 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<T> 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 * 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
*/ */
public SQLConnection(@NotNull Class<T> clazz, String dbLocation) { public SQLConnection(@NotNull Class<T> clazz, String dbLocation) {
this.clazz = clazz; this.clazz = clazz;
@ -183,6 +205,10 @@ public class SQLConnection<T> {
} }
} }
public static void setGlobalDBLocation(String dbLocation) {
globalSQLiteLocation = dbLocation;
}
private void createTable() { private void createTable() {
try { try {
String sql = "CREATE TABLE IF NOT EXISTS " + String sql = "CREATE TABLE IF NOT EXISTS " +