diff --git a/src/main/java/de/hsel/itech/db/Database.java b/src/main/java/de/hsel/itech/db/Database.java index 9243490..4e3c60a 100644 --- a/src/main/java/de/hsel/itech/db/Database.java +++ b/src/main/java/de/hsel/itech/db/Database.java @@ -30,6 +30,12 @@ import java.util.Map; */ public class Database { + private final String BOOK = "book"; + private final String AUTHOR = "author"; + private final String AUTHOR_BOOK = "author_book"; + private final String PUBLISHER = "publisher"; + private final String CATEGORY = "category"; + private static Database instance; public static Database getInstance() { @@ -100,7 +106,7 @@ public class Database { @Nullable public Author getAuthor(long id) { - Map.Entry entry = getResultSet(id, "author"); + Map.Entry entry = getResultSet(id, AUTHOR); assert entry != null; ResultSet rs = entry.getKey(); @@ -116,7 +122,7 @@ public class Database { @Nullable public Category getCategory(long id) { - Map.Entry entry = getResultSet(id, "category"); + Map.Entry entry = getResultSet(id, CATEGORY); assert entry != null; ResultSet rs = entry.getKey(); @@ -132,7 +138,7 @@ public class Database { @Nullable public Publisher getPublisher(long id) { - Map.Entry entry = getResultSet(id, "category"); + Map.Entry entry = getResultSet(id, PUBLISHER); assert entry != null; ResultSet rs = entry.getKey(); @@ -148,12 +154,12 @@ public class Database { @Nullable private List getAuthors(long id) { - Map.Entry, Connection> entry = getResultSets(id, "author_book", "book"); + Map.Entry, Connection> entry = getResultSets(id, AUTHOR_BOOK, BOOK); assert entry != null; try { List list = new ArrayList<>(); for (ResultSet rs : entry.getKey()) { - list.add(rs.getInt("author")); + list.add(rs.getInt(AUTHOR)); } entry.getValue().close(); return list; @@ -165,14 +171,14 @@ public class Database { @Nullable public Book getBook(long id) { - Map.Entry entry = getResultSet(id, "book"); + Map.Entry entry = getResultSet(id, BOOK); assert entry != null; ResultSet rs = entry.getKey(); Book book = null; try { - Category category = getCategory(rs.getLong("category")); - Publisher publisher = getPublisher(rs.getLong("publisher")); + Category category = getCategory(rs.getLong(CATEGORY)); + Publisher publisher = getPublisher(rs.getLong(PUBLISHER)); List authorIds = getAuthors(id); assert authorIds != null; List authors = new ArrayList<>(); @@ -191,7 +197,7 @@ public class Database { @Nullable public List getBooks() { - Map.Entry, Connection> entry = getResultSets("book", "id"); + Map.Entry, Connection> entry = getResultSets(BOOK, "id"); assert entry != null; List ids = new ArrayList<>(); @@ -298,7 +304,6 @@ public class Database { }catch (SQLException ex) { ex.printStackTrace(); } - return insertCount; } @@ -322,11 +327,90 @@ public class Database { }catch (SQLException ex) { ex.printStackTrace(); } - - return insertCount; } + /** + * inserts Publisher + * @param publisher Publisher + * @return insert count + */ + public int insert(@NotNull Publisher publisher) { + Connection connection = getConnection(); + assert connection != null; + + int insertCount = 0; + try{ + PreparedStatement statement = connection.prepareStatement("INSERT INTO publisher (name) VALUES (?)"); + statement.setString(1, publisher.getName()); + + insertCount += statement.executeUpdate(); + + connection.close(); + }catch (SQLException ex) { + ex.printStackTrace(); + } + return insertCount; + } + + public int deleteBook(long id) { + int deleteCount = 0; + + try{ + Connection connection = getConnection(); + assert connection != null; + + PreparedStatement statement = connection.prepareStatement("DELETE FROM author_book WHERE book = ?"); + statement.setLong(1, id); + deleteCount =+ statement.executeUpdate(); + + connection.close(); + }catch (SQLException ex) { + ex.printStackTrace(); + } + + deleteCount += delete(id, BOOK); + + return deleteCount; + } + + public int deleteAuthor(long id) { + return delete(id, AUTHOR); + } + + public int deletePublisher(long id) { + return delete(id, PUBLISHER); + } + + public int deleteCategory(long id) { + return delete(id, CATEGORY); + } + + + + /** + * + * @param id + * @param table + * @return delete count + */ + private int delete(long id, String table) { + Connection connection = getConnection(); + assert connection != null; + + int deleteCount = 0; + try{ + PreparedStatement statement = connection.prepareStatement("DELETE FROM ? WHERE id=?"); + statement.setString(1, table); + statement.setLong(2, id); + + deleteCount =+ statement.executeUpdate(); + + }catch (SQLException ex) { + ex.printStackTrace(); + } + return deleteCount; + } @@ -351,19 +435,8 @@ public class Database { return null; } - @NotNull - private Map.Entry, Connection> getListConnectionEntry(Connection connection, PreparedStatement statement) throws SQLException { - ResultSet resultSet = statement.executeQuery(); - - Map.Entry, Connection> entry = new AbstractMap.SimpleEntry<>(new ArrayList<>(), connection); - while (resultSet.next()) { - entry.getKey().add(resultSet); - } - return entry; - } - @Nullable - private Map.Entry, Connection> getResultSets(long id, String table, String column) { + private Map.Entry, Connection> getResultSets(long id, @NotNull String table, @NotNull String column) { Connection connection = getConnection(); try { assert connection != null; @@ -380,7 +453,7 @@ public class Database { } @Nullable - private Map.Entry, Connection> getResultSets(String table, String columns) { + private Map.Entry, Connection> getResultSets(@NotNull String table, @NotNull String columns) { Connection connection = getConnection(); try { assert connection != null; @@ -395,4 +468,14 @@ public class Database { return null; } + @NotNull + private Map.Entry, Connection> getListConnectionEntry(@NotNull Connection connection, @NotNull PreparedStatement statement) throws SQLException { + ResultSet resultSet = statement.executeQuery(); + + Map.Entry, Connection> entry = new AbstractMap.SimpleEntry<>(new ArrayList<>(), connection); + while (resultSet.next()) { + entry.getKey().add(resultSet); + } + return entry; + } } \ No newline at end of file