From 480d8bd62c96980999f1e51fd8639dfb4278575b Mon Sep 17 00:00:00 2001 From: flifloo Date: Fri, 18 Dec 2020 14:02:37 +0100 Subject: [PATCH] Add M2M relations add and remove functions --- src/main/java/DB/Competition.java | 14 +++++++++++--- src/main/java/DB/DB.java | 26 ++++++++++++++++++++++++++ src/main/java/DB/Room.java | 10 +++++++++- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/main/java/DB/Competition.java b/src/main/java/DB/Competition.java index adfb604..b30c2b1 100644 --- a/src/main/java/DB/Competition.java +++ b/src/main/java/DB/Competition.java @@ -104,7 +104,7 @@ public class Competition extends Table { PreparedStatement ps = DB.getConnection().prepareStatement("SELECT id FROM Slot WHERE CompetitionName = ?"); ps.setString(1, name); for (ResultSet rs = ps.executeQuery(); rs.next();) - list.add(new Slot(rs.getInt("idk"))); + list.add(new Slot(rs.getInt("id"))); ps.close(); } catch (SQLException | NullPointerException | NotFoundInTable e) { return null; @@ -115,10 +115,10 @@ public class Competition extends Table { public ArrayList getRooms() { ArrayList list = new ArrayList<>(); try { - PreparedStatement ps = DB.getConnection().prepareStatement("SELECT CompetitionName FROM CompetitionRoom WHERE CompetitionName = ?"); + PreparedStatement ps = DB.getConnection().prepareStatement("SELECT RoomName FROM CompetitionRoom WHERE CompetitionName = ?"); ps.setString(1, name); for (ResultSet rs = ps.executeQuery(); rs.next();) - list.add(new Room(rs.getString("name"))); + list.add(new Room(rs.getString("RoomName"))); ps.close(); } catch (SQLException | NullPointerException | NotFoundInTable e) { return null; @@ -153,4 +153,12 @@ public class Competition extends Table { } return list; } + + public boolean addRoom(Room room) { + return DB.relationSet("CompetitionRoom", "CompetitionName", this.name, "RoomName", room.getName()); + } + + public boolean removeRoom(Room room) { + return DB.relationRemove("CompetitionRoom", "CompetitionName", this.name, "RoomName", room.getName()); + } } diff --git a/src/main/java/DB/DB.java b/src/main/java/DB/DB.java index 24e6627..6842933 100644 --- a/src/main/java/DB/DB.java +++ b/src/main/java/DB/DB.java @@ -132,6 +132,32 @@ public class DB { return false; } + static public boolean relationSet(String table, String valueName1, String value1, String valueName2, String value2) { + try { + PreparedStatement ps = DB.connection.prepareStatement("INSERT INTO "+table+" ("+valueName1+", "+valueName2+") VALUES (?, ?)"); + ps.setString(1, value1); + ps.setString(2, value2); + int r = ps.executeUpdate(); + ps.close(); + return r > 0; + } catch (SQLException e) { + return false; + } + } + + static public boolean relationRemove(String table, String valueName1, String value1, String valueName2, String value2) { + try { + PreparedStatement ps = DB.connection.prepareStatement("DELETE FROM "+table+" WHERE "+valueName1+" = ? AND "+valueName2+" = ?"); + ps.setString(1, value1); + ps.setString(2, value2); + int r = ps.executeUpdate(); + ps.close(); + return r > 0; + } catch (SQLException e) { + return false; + } + } + static public boolean exist(String table, String check, String checkValue) { try { boolean r = false; diff --git a/src/main/java/DB/Room.java b/src/main/java/DB/Room.java index 7ae4706..bcd354c 100644 --- a/src/main/java/DB/Room.java +++ b/src/main/java/DB/Room.java @@ -53,7 +53,7 @@ public class Room extends Table { PreparedStatement ps = DB.getConnection().prepareStatement("SELECT CompetitionName FROM CompetitionRoom WHERE RoomName = ?"); ps.setString(1, name); for (ResultSet rs = ps.executeQuery(); rs.next();) - list.add(new Competition(rs.getString("name"))); + list.add(new Competition(rs.getString("CompetitionName"))); ps.close(); } catch (SQLException | NullPointerException | NotFoundInTable e) { return null; @@ -74,4 +74,12 @@ public class Room extends Table { } return list; } + + public boolean addCompetition(Competition competition) { + return DB.relationSet("CompetitionRoom", "RoomName", this.name, "CompetitionName", competition.getName()); + } + + public boolean removeCompetition(Competition competition) { + return DB.relationRemove("CompetitionRoom", "RoomName", this.name, "CompetitionName", competition.getName()); + } }