+ delete(Book/Author/Publisher/Category)

This commit is contained in:
Johannes Theiner 2019-04-18 09:10:13 +02:00
parent 59ecac912c
commit 8b9d81bfc1

View File

@ -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<ResultSet, Connection> entry = getResultSet(id, "author");
Map.Entry<ResultSet, Connection> 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<ResultSet, Connection> entry = getResultSet(id, "category");
Map.Entry<ResultSet, Connection> 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<ResultSet, Connection> entry = getResultSet(id, "category");
Map.Entry<ResultSet, Connection> entry = getResultSet(id, PUBLISHER);
assert entry != null;
ResultSet rs = entry.getKey();
@ -148,12 +154,12 @@ public class Database {
@Nullable
private List<Integer> getAuthors(long id) {
Map.Entry<List<ResultSet>, Connection> entry = getResultSets(id, "author_book", "book");
Map.Entry<List<ResultSet>, Connection> entry = getResultSets(id, AUTHOR_BOOK, BOOK);
assert entry != null;
try {
List<Integer> 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<ResultSet, Connection> entry = getResultSet(id, "book");
Map.Entry<ResultSet, Connection> 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<Integer> authorIds = getAuthors(id);
assert authorIds != null;
List<Author> authors = new ArrayList<>();
@ -191,7 +197,7 @@ public class Database {
@Nullable
public List<Book> getBooks() {
Map.Entry<List<ResultSet>, Connection> entry = getResultSets("book", "id");
Map.Entry<List<ResultSet>, Connection> entry = getResultSets(BOOK, "id");
assert entry != null;
List<Long> 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<List<ResultSet>, Connection> getListConnectionEntry(Connection connection, PreparedStatement statement) throws SQLException {
ResultSet resultSet = statement.executeQuery();
Map.Entry<List<ResultSet>, Connection> entry = new AbstractMap.SimpleEntry<>(new ArrayList<>(), connection);
while (resultSet.next()) {
entry.getKey().add(resultSet);
}
return entry;
}
@Nullable
private Map.Entry<List<ResultSet>, Connection> getResultSets(long id, String table, String column) {
private Map.Entry<List<ResultSet>, 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<List<ResultSet>, Connection> getResultSets(String table, String columns) {
private Map.Entry<List<ResultSet>, 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<List<ResultSet>, Connection> getListConnectionEntry(@NotNull Connection connection, @NotNull PreparedStatement statement) throws SQLException {
ResultSet resultSet = statement.executeQuery();
Map.Entry<List<ResultSet>, Connection> entry = new AbstractMap.SimpleEntry<>(new ArrayList<>(), connection);
while (resultSet.next()) {
entry.getKey().add(resultSet);
}
return entry;
}
}