von der Wiege bis zur Bahre, Kommentare, Kommentare
This commit is contained in:
@@ -13,6 +13,11 @@ import java.util.List;
|
||||
|
||||
public class Database {
|
||||
private static final String dbLocation = "jdbc:sqlite:"+Path.of("").toAbsolutePath()+"/database.db";
|
||||
|
||||
/**
|
||||
* creates new database.db if it doesn't exist
|
||||
* @return true if a new database has been created
|
||||
*/
|
||||
protected static boolean init(){
|
||||
File db = new File(Path.of("").toAbsolutePath()+"/database.db");
|
||||
try {
|
||||
@@ -21,6 +26,11 @@ public class Database {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* connects to the database
|
||||
* @return Connection to the database
|
||||
*/
|
||||
protected static Connection connect(){
|
||||
Connection conn = null;
|
||||
try{
|
||||
@@ -31,6 +41,9 @@ public class Database {
|
||||
return conn;
|
||||
}
|
||||
|
||||
/**
|
||||
* creates the initial structure of the db
|
||||
*/
|
||||
protected static void createDb(){
|
||||
String[] sql = new String[14];
|
||||
sql[0] = """
|
||||
@@ -152,6 +165,9 @@ public class Database {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* inserts fixed values into the database
|
||||
*/
|
||||
protected static void fillDb(){
|
||||
List<String> sqls = new ArrayList<>();
|
||||
sqls.add("""
|
||||
@@ -249,6 +265,7 @@ public class Database {
|
||||
|
||||
/**
|
||||
* inserts data into table and returns its id
|
||||
* does not insert if the exact set already exists
|
||||
* @param table name of the database table
|
||||
* @param header String[] order should match with values
|
||||
* @param values String[] order should match with header
|
||||
@@ -288,6 +305,13 @@ public class Database {
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns a single id that matches the given data
|
||||
* @param table the table that contains the searched entry
|
||||
* @param header the header of the table, order should match with values
|
||||
* @param values the data you want the id of, order should match witch values
|
||||
* @return one id matching the given data or -1 if no match has been found
|
||||
*/
|
||||
protected static long getSingleId(String table, String[] header, String[] values){
|
||||
long id = -1;
|
||||
try(Connection conn = connect()){
|
||||
@@ -307,6 +331,9 @@ public class Database {
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
protected static void printSampleQuery(){
|
||||
String sql = """
|
||||
SELECT * FROM food_type WHERE id > ?;""";
|
||||
@@ -332,6 +359,11 @@ public class Database {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* deletes an entry from table with matching id
|
||||
* @param table the table that contains the entry you want to delete
|
||||
* @param id the id of the entry you want to delete
|
||||
*/
|
||||
protected static void delete(String table, long id){
|
||||
String sql = "DELETE FROM " + table + " WHERE id = ?;";
|
||||
try(Connection conn = connect();PreparedStatement ps = conn.prepareStatement(sql)){
|
||||
@@ -434,6 +466,11 @@ public class Database {
|
||||
return sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns a list of all entries
|
||||
* @param table the table you want
|
||||
* @return a list of all entries as String with the fields separated by ":"
|
||||
*/
|
||||
protected static List<String> getTable(String table){
|
||||
List<String> data = new ArrayList<>();
|
||||
StringBuilder sb;
|
||||
@@ -458,6 +495,13 @@ public class Database {
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* issues a select query on the database for the given table and the given values checked with LIKE
|
||||
* @param table the table you want the data from
|
||||
* @param header header for the WHERE portion, order should match with values
|
||||
* @param values values for the WHERE portion, order should match with header
|
||||
* @return a list of the matching data as String separated by ":"
|
||||
*/
|
||||
protected static List<String> select(String table,String[] header, String[] values){
|
||||
List<String> data = new ArrayList<>();
|
||||
StringBuilder sb;
|
||||
@@ -483,6 +527,12 @@ public class Database {
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the entry from table with the given id
|
||||
* @param table the table you want the entry from
|
||||
* @param id the id of the entry you want
|
||||
* @return a list of String separated by ":"
|
||||
*/
|
||||
protected static List<String> getEntryById(String table, long id){
|
||||
List<String> data = new ArrayList<>();
|
||||
StringBuilder sb;
|
||||
@@ -509,6 +559,13 @@ public class Database {
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* counts the number of matching entries
|
||||
* @param table the table you want to count
|
||||
* @param header the properties you want to count on
|
||||
* @param values the values for the properties
|
||||
* @return the number of found rows
|
||||
*/
|
||||
protected static int count(String table,String[] header,String[] values){
|
||||
String sql = queryBuilder("count",table,header,values);
|
||||
try(Connection conn = connect()) {
|
||||
@@ -520,6 +577,13 @@ public class Database {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* updates an entry in the database
|
||||
* @param table the table you want to update
|
||||
* @param header [0] is used as WHERE, everything else in SET
|
||||
* @param values [0] is used as WHERE, everything else in SET
|
||||
* @return number of rows affected or -1 on error
|
||||
*/
|
||||
protected static int update(String table,String[] header,String[] values){
|
||||
try(Connection conn = connect()) {
|
||||
String sql = queryBuilder("update",table,header,values);
|
||||
|
Reference in New Issue
Block a user