维护入口
This commit is contained in:
@@ -33,6 +33,32 @@ public class JdbcBookDao implements BookDao {
|
||||
+ "FROM book_categories "
|
||||
+ "ORDER BY name";
|
||||
|
||||
private static final String FIND_CATEGORY_BY_ID = ""
|
||||
+ "SELECT id, name, description "
|
||||
+ "FROM book_categories "
|
||||
+ "WHERE id = ?";
|
||||
|
||||
private static final String FIND_CATEGORY_BY_NAME = ""
|
||||
+ "SELECT id, name, description "
|
||||
+ "FROM book_categories "
|
||||
+ "WHERE name = ?";
|
||||
|
||||
private static final String CREATE_CATEGORY = ""
|
||||
+ "INSERT INTO book_categories (name, description) "
|
||||
+ "VALUES (?, ?)";
|
||||
|
||||
private static final String UPDATE_CATEGORY = ""
|
||||
+ "UPDATE book_categories "
|
||||
+ "SET name = ?, description = ? "
|
||||
+ "WHERE id = ?";
|
||||
|
||||
private static final String DELETE_CATEGORY = "DELETE FROM book_categories WHERE id = ?";
|
||||
|
||||
private static final String COUNT_BOOKS_BY_CATEGORY = ""
|
||||
+ "SELECT COUNT(*) "
|
||||
+ "FROM books "
|
||||
+ "WHERE category_id = ?";
|
||||
|
||||
private static final String FIND_BY_ID = "SELECT " + BOOK_COLUMNS + BOOK_FROM + "WHERE b.id = ?";
|
||||
|
||||
private static final String FIND_BY_IDENTIFIER = "SELECT " + BOOK_COLUMNS + BOOK_FROM
|
||||
@@ -66,6 +92,86 @@ public class JdbcBookDao implements BookDao {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<BookCategory> findCategoryById(long id) {
|
||||
try (Connection connection = JdbcUtil.getConnection();
|
||||
PreparedStatement statement = connection.prepareStatement(FIND_CATEGORY_BY_ID)) {
|
||||
statement.setLong(1, id);
|
||||
try (ResultSet resultSet = statement.executeQuery()) {
|
||||
return resultSet.next() ? Optional.of(mapCategory(resultSet)) : Optional.empty();
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
throw new DaoException("Unable to load book category by id", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<BookCategory> findCategoryByName(String name) {
|
||||
try (Connection connection = JdbcUtil.getConnection();
|
||||
PreparedStatement statement = connection.prepareStatement(FIND_CATEGORY_BY_NAME)) {
|
||||
statement.setString(1, name);
|
||||
try (ResultSet resultSet = statement.executeQuery()) {
|
||||
return resultSet.next() ? Optional.of(mapCategory(resultSet)) : Optional.empty();
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
throw new DaoException("Unable to load book category by name", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long createCategory(BookCategory category) {
|
||||
try (Connection connection = JdbcUtil.getConnection();
|
||||
PreparedStatement statement = connection.prepareStatement(CREATE_CATEGORY, Statement.RETURN_GENERATED_KEYS)) {
|
||||
bindCategory(statement, category);
|
||||
statement.executeUpdate();
|
||||
|
||||
try (ResultSet generatedKeys = statement.getGeneratedKeys()) {
|
||||
if (generatedKeys.next()) {
|
||||
return generatedKeys.getLong(1);
|
||||
}
|
||||
}
|
||||
throw new DaoException("Unable to read generated book category id", null);
|
||||
} catch (SQLException ex) {
|
||||
throw new DaoException("Unable to create book category", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateCategory(BookCategory category) {
|
||||
try (Connection connection = JdbcUtil.getConnection();
|
||||
PreparedStatement statement = connection.prepareStatement(UPDATE_CATEGORY)) {
|
||||
bindCategory(statement, category);
|
||||
statement.setLong(3, category.getId());
|
||||
return statement.executeUpdate() == 1;
|
||||
} catch (SQLException ex) {
|
||||
throw new DaoException("Unable to update book category", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean deleteCategory(long id) {
|
||||
try (Connection connection = JdbcUtil.getConnection();
|
||||
PreparedStatement statement = connection.prepareStatement(DELETE_CATEGORY)) {
|
||||
statement.setLong(1, id);
|
||||
return statement.executeUpdate() == 1;
|
||||
} catch (SQLException ex) {
|
||||
throw new DaoException("Unable to delete book category", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int countBooksByCategoryId(long categoryId) {
|
||||
try (Connection connection = JdbcUtil.getConnection();
|
||||
PreparedStatement statement = connection.prepareStatement(COUNT_BOOKS_BY_CATEGORY)) {
|
||||
statement.setLong(1, categoryId);
|
||||
try (ResultSet resultSet = statement.executeQuery()) {
|
||||
return resultSet.next() ? resultSet.getInt(1) : 0;
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
throw new DaoException("Unable to count books by category", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Book> search(BookSearchCriteria criteria) {
|
||||
List<Object> parameters = new ArrayList<>();
|
||||
@@ -194,6 +300,11 @@ public class JdbcBookDao implements BookDao {
|
||||
statement.setString(7, book.getStatus().getCode());
|
||||
}
|
||||
|
||||
private void bindCategory(PreparedStatement statement, BookCategory category) throws SQLException {
|
||||
statement.setString(1, category.getName());
|
||||
statement.setString(2, category.getDescription());
|
||||
}
|
||||
|
||||
private Book mapBook(ResultSet resultSet) throws SQLException {
|
||||
Book book = new Book();
|
||||
book.setId(resultSet.getLong("id"));
|
||||
|
||||
Reference in New Issue
Block a user