feat/kindErstellen #9

Merged
PBS2H21ASH merged 23 commits from feat/kindErstellen into stable 2023-02-05 20:40:12 +01:00
Showing only changes of commit c4e720b9f3 - Show all commits

View File

@ -9,8 +9,10 @@ import java.util.List;
/** /**
* Basic operations on the database * Basic operations on the database
* Use init() -> createDb() -> fillDb() to create the skeleton with some default values * Use init() -> createDb() -> fillDb() to create the skeleton with some default
* values
* Provides select, insert, update, delete, count operations and more * Provides select, insert, update, delete, count operations and more
*
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
public class Database { public class Database {
@ -18,6 +20,7 @@ public class Database {
/** /**
* creates new database.db if it doesn't exist * creates new database.db if it doesn't exist
*
* @return true if a new database has been created * @return true if a new database has been created
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
@ -32,6 +35,7 @@ public class Database {
/** /**
* connects to the database * connects to the database
*
* @return Connection to the database * @return Connection to the database
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
@ -47,6 +51,7 @@ public class Database {
/** /**
* creates the initial structure of the db * creates the initial structure of the db
*
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
protected static void createDb() { protected static void createDb() {
@ -178,10 +183,12 @@ public class Database {
/** /**
* inserts fixed values into the database * inserts fixed values into the database
*
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
protected static void fillDb() { protected static void fillDb() {
List<String> sqls = new ArrayList<>(); List<String> sqls = new ArrayList<>();
// food_type
sqls.add(""" sqls.add("""
INSERT OR IGNORE INTO food_type (id,name) INSERT OR IGNORE INTO food_type (id,name)
VALUES ('1','Vegan');"""); VALUES ('1','Vegan');""");
@ -191,6 +198,7 @@ public class Database {
sqls.add(""" sqls.add("""
INSERT OR IGNORE INTO food_type (id,name) INSERT OR IGNORE INTO food_type (id,name)
VALUES ('3','Fleischhaltig');"""); VALUES ('3','Fleischhaltig');""");
// allergy
sqls.add(""" sqls.add("""
INSERT OR IGNORE INTO allergy (id,name,handle) INSERT OR IGNORE INTO allergy (id,name,handle)
VALUES('1','Eier','a');"""); VALUES('1','Eier','a');""");
@ -257,6 +265,7 @@ public class Database {
sqls.add(""" sqls.add("""
INSERT OR IGNORE INTO allergy (id,name,handle) INSERT OR IGNORE INTO allergy (id,name,handle)
VALUES('22','Konservierungsstoff','8');"""); VALUES('22','Konservierungsstoff','8');""");
// severity
sqls.add(""" sqls.add("""
INSERT OR IGNORE INTO severity (id,name) INSERT OR IGNORE INTO severity (id,name)
VALUES('1','Harmlos');"""); VALUES('1','Harmlos');""");
@ -269,6 +278,126 @@ public class Database {
sqls.add(""" sqls.add("""
INSERT OR IGNORE INTO price (id,price) INSERT OR IGNORE INTO price (id,price)
VALUES('1','500');"""); VALUES('1','500');""");
// user
sqls.add("""
INSERT OR IGNORE INTO address (id,street,number,plz,city)
VALUES('1','teststreet','69','1337','Mond');""");
sqls.add("""
INSERT OR IGNORE INTO user (id,name,firstname,addressid,password,email)
VALUES('1','testparent','testparent','1','YOD+TB0twF2SrueBj26t5OjEJK/Al4G6/hq+IMRyBz4=.f4zL2UJW4POrf/xgJdNaiw==','testparent@test.de');""");
sqls.add("""
INSERT OR IGNORE INTO parent (userid)
VALUES('1');""");
sqls.add("""
INSERT OR IGNORE INTO user (id,name,firstname,addressid,password,email)
VALUES('2','testworker','testworker','1','YOD+TB0twF2SrueBj26t5OjEJK/Al4G6/hq+IMRyBz4=.f4zL2UJW4POrf/xgJdNaiw==','testworker@test.de');""");
sqls.add("""
INSERT OR IGNORE INTO worker (userid)
VALUES('2');""");
// food
sqls.add("""
INSERT OR IGNORE INTO food (id,name,description,isdessert,food_typeid)
VALUES('1','Steak','69','0','3');""");
sqls.add("""
INSERT OR IGNORE INTO food (id,name,description,isdessert,food_typeid)
VALUES('2','Schnitzel','69','0','3');""");
sqls.add("""
INSERT OR IGNORE INTO food (id,name,description,isdessert,food_typeid)
VALUES('3','Hamburger','69','0','3');""");
sqls.add("""
INSERT OR IGNORE INTO food (id,name,description,isdessert,food_typeid)
VALUES('4','Nudeln','69','0','1');""");
sqls.add("""
INSERT OR IGNORE INTO food (id,name,description,isdessert,food_typeid)
VALUES('5','Salat','69','0','1');""");
sqls.add("""
INSERT OR IGNORE INTO food (id,name,description,isdessert,food_typeid)
VALUES('6','Pudding','69','1','1');""");
sqls.add("""
INSERT OR IGNORE INTO food (id,name,description,isdessert,food_typeid)
VALUES('7','Eis','69','1','1');""");
sqls.add("""
INSERT OR IGNORE INTO food (id,name,description,isdessert,food_typeid)
VALUES('8','Wackelpudding','69','1','1');""");
sqls.add("""
INSERT OR IGNORE INTO food (id,name,description,isdessert,food_typeid)
VALUES('9','Kuchen','69','1','1');""");
sqls.add("""
INSERT OR IGNORE INTO food (id,name,description,isdessert,food_typeid)
VALUES('10','Apfel','69','1','1');""");
sqls.add("""
INSERT OR IGNORE INTO food (id,name,description,isdessert,food_typeid)
VALUES('11','Banane','69','1','1');""");
sqls.add("""
INSERT OR IGNORE INTO food (id,name,description,isdessert,food_typeid)
VALUES('12','Nudelauflauf','69','0','3');""");
sqls.add("""
INSERT OR IGNORE INTO food (id,name,description,isdessert,food_typeid)
VALUES('13','Reibekuchen','69','0','1');""");
sqls.add("""
INSERT OR IGNORE INTO food (id,name,description,isdessert,food_typeid)
VALUES('14','Gefüllte Paprika','69','0','1');""");
sqls.add("""
INSERT OR IGNORE INTO food (id,name,description,isdessert,food_typeid)
VALUES('15','Suishi','69','0','2');""");
sqls.add("""
INSERT OR IGNORE INTO food (id,name,description,isdessert,food_typeid)
VALUES('16','Champignons','69','0','2');""");
// child
sqls.add("""
INSERT OR IGNORE INTO child (id,name,firstname,addressid)
VALUES('1','Lustig','Peter','1');""");
sqls.add("""
INSERT OR IGNORE INTO child (id,name,firstname,addressid)
VALUES('2','Wahnsinn','Rainer','1');""");
sqls.add("""
INSERT OR IGNORE INTO parent_child ('id',parentuserid,childid)
VALUES('1','1','1');""");
sqls.add("""
INSERT OR IGNORE INTO parent_child ('id',parentuserid,childid)
VALUES('2','1','2');""");
sqls.add("""
INSERT OR IGNORE INTO child_allergy (childid,allergyid,severityid)
VALUES('1','1','2');""");
sqls.add("""
INSERT OR IGNORE INTO child_allergy (childid,allergyid,severityid)
VALUES('1','3','2');""");
sqls.add("""
INSERT OR IGNORE INTO child_allergy (childid,allergyid,severityid)
VALUES('1','4','2');""");
// foodplan
sqls.add("""
INSERT OR IGNORE INTO food_plan ('id',date,food1,food2,dessert1,dessert2)
VALUES('1','2023-02-06','4','1','8','7');""");
sqls.add("""
INSERT OR IGNORE INTO food_plan ('id',date,food1,food2,dessert1,dessert2)
VALUES('2','2023-02-07','5','2','6','7');""");
sqls.add("""
INSERT OR IGNORE INTO food_plan ('id',date,food1,food2,dessert1,dessert2)
VALUES('3','2023-02-08','4','3','8','6');""");
sqls.add("""
INSERT OR IGNORE INTO food_plan ('id',date,food1,food2,dessert1,dessert2)
VALUES('4','2023-02-09','16','1','11','10');""");
sqls.add("""
INSERT OR IGNORE INTO food_plan ('id',date,food1,food2,dessert1,dessert2)
VALUES('5','2023-02-09','14','13','7','9');""");
sqls.add("""
INSERT OR IGNORE INTO food_plan ('id',date,food1,food2,dessert1,dessert2)
VALUES('6','2023-02-10','13','15','8','6');""");
// food_selection
sqls.add("""
INSERT OR IGNORE INTO food_selection ('id',childid,food_planid,foodid)
VALUES('1','1','1','4');""");
sqls.add("""
INSERT OR IGNORE INTO food_selection ('id',childid,food_planid,foodid)
VALUES('2','1','1','8');""");
sqls.add("""
INSERT OR IGNORE INTO food_selection ('id',childid,food_planid,foodid)
VALUES('3','2','2','5');""");
sqls.add("""
INSERT OR IGNORE INTO food_selection ('id',childid,food_planid,foodid)
VALUES('4','2','2','7');""");
try (Connection conn = connect(); Statement stmt = conn.createStatement()) { try (Connection conn = connect(); Statement stmt = conn.createStatement()) {
for (String sql : sqls) { for (String sql : sqls) {
stmt.execute(sql); stmt.execute(sql);
@ -281,6 +410,7 @@ public class Database {
/** /**
* inserts data into table and returns its id * inserts data into table and returns its id
* simple duplication check * simple duplication check
*
* @param table name of the database table * @param table name of the database table
* @param header String[] order should match with values * @param header String[] order should match with values
* @param values String[] order should match with header * @param values String[] order should match with header
@ -323,6 +453,7 @@ public class Database {
/** /**
* returns a single id that matches the given data * returns a single id that matches the given data
*
* @param table the table that contains the searched entry * @param table the table that contains the searched entry
* @param header the header of the table, order should match with values * @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 * @param values the data you want the id of, order should match witch values
@ -378,6 +509,7 @@ public class Database {
/** /**
* deletes an entry from table with matching id * deletes an entry from table with matching id
*
* @param table the table that contains the entry you want to delete * @param table the table that contains the entry you want to delete
* @param id the id of the entry you want to delete * @param id the id of the entry you want to delete
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
@ -398,8 +530,10 @@ public class Database {
* selectMatch: returns all matching rows from table * selectMatch: returns all matching rows from table
* insert: inserts or ignores into table * insert: inserts or ignores into table
* count: counts exact matches from table * count: counts exact matches from table
* update: updates table, header/values[0] is used as WHERE, using id is recommended header/values[1+] are used * update: updates table, header/values[0] is used as WHERE, using id is
* recommended header/values[1+] are used
* as SET * as SET
*
* @param type exists, selectMatch, insert, count, update * @param type exists, selectMatch, insert, count, update
* @param table table * @param table table
* @param header header * @param header header
@ -501,6 +635,7 @@ public class Database {
/** /**
* returns a list of all entries * returns a list of all entries
*
* @param table the table you want * @param table the table you want
* @return a list of all entries as String with the fields separated by ":" * @return a list of all entries as String with the fields separated by ":"
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
@ -530,7 +665,9 @@ public class Database {
} }
/** /**
* issues a select query on the database for the given table and the given values checked with LIKE * 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 table the table you want the data from
* @param header header for the WHERE portion, order should match with values * @param header header for the WHERE portion, order should match with values
* @param values values for the WHERE portion, order should match with header * @param values values for the WHERE portion, order should match with header
@ -564,6 +701,7 @@ public class Database {
/** /**
* returns the entry from table with the given id * returns the entry from table with the given id
*
* @param table the table you want the entry from * @param table the table you want the entry from
* @param id the id of the entry you want * @param id the id of the entry you want
* @return a list of String separated by ":" * @return a list of String separated by ":"
@ -597,6 +735,7 @@ public class Database {
/** /**
* counts the number of matching entries * counts the number of matching entries
*
* @param table the table you want to count * @param table the table you want to count
* @param header the properties you want to count on * @param header the properties you want to count on
* @param values the values for the properties * @param values the values for the properties
@ -616,6 +755,7 @@ public class Database {
/** /**
* updates an entry in the database * updates an entry in the database
*
* @param table the table you want to update * @param table the table you want to update
* @param header [0] is used as WHERE, everything else in SET * @param header [0] is used as WHERE, everything else in SET
* @param values [0] is used as WHERE, everything else in SET * @param values [0] is used as WHERE, everything else in SET