481 lines
15 KiB
Java
481 lines
15 KiB
Java
package de.hsel.itech.db;
|
|
|
|
import de.hsel.itech.config.Configuration;
|
|
import de.hsel.itech.db.pojo.Author;
|
|
import de.hsel.itech.db.pojo.Book;
|
|
import de.hsel.itech.db.pojo.Category;
|
|
import de.hsel.itech.db.pojo.Publisher;
|
|
import org.jetbrains.annotations.NotNull;
|
|
import org.jetbrains.annotations.Nullable;
|
|
import org.mariadb.jdbc.MariaDbPoolDataSource;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.FileReader;
|
|
import java.io.IOException;
|
|
import java.net.URL;
|
|
import java.sql.Connection;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import java.time.Year;
|
|
import java.util.AbstractMap;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* @author Johannes Theiner
|
|
* @version 0.1
|
|
* @since 0.1
|
|
*/
|
|
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() {
|
|
if (instance == null)
|
|
instance = new Database();
|
|
return instance;
|
|
}
|
|
|
|
private MariaDbPoolDataSource dataSource;
|
|
|
|
private Database() {
|
|
Configuration config = Configuration.get();
|
|
dataSource = new MariaDbPoolDataSource("jdbc:mysql://" + config.getDatabase().getHostname() + ":" + config.getDatabase().getPort() + "/" + config.getDatabase().getDatabase());
|
|
try {
|
|
dataSource.setUser(config.getDatabase().getUsername());
|
|
dataSource.setPassword(config.getDatabase().getPassword());
|
|
dataSource.initialize();
|
|
|
|
Connection connection = getConnection();
|
|
assert connection != null;
|
|
URL file = getClass().getClassLoader().getResource("database.sql");
|
|
assert file != null;
|
|
try (BufferedReader br = new BufferedReader(new FileReader(file.getFile()))) {
|
|
for (String line = br.readLine(); line != null; line = br.readLine()) {
|
|
PreparedStatement statement = connection.prepareStatement(line);
|
|
statement.executeUpdate();
|
|
statement.close();
|
|
}
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
} finally {
|
|
connection.close();
|
|
}
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
@Nullable
|
|
private Connection getConnection() {
|
|
try {
|
|
return dataSource.getConnection();
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public String getHello() {
|
|
Connection connection = getConnection();
|
|
|
|
String hello = "";
|
|
try {
|
|
assert connection != null;
|
|
PreparedStatement statement = connection.prepareStatement("SELECT * FROM test WHERE id = 1");
|
|
ResultSet resultSet = statement.executeQuery();
|
|
resultSet.next();
|
|
hello = resultSet.getString("hello");
|
|
|
|
connection.close();
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
return hello;
|
|
|
|
}
|
|
|
|
@Nullable
|
|
public Author getAuthor(long id) {
|
|
Map.Entry<ResultSet, Connection> entry = getResultSet(id, AUTHOR);
|
|
assert entry != null;
|
|
|
|
ResultSet rs = entry.getKey();
|
|
Author author = null;
|
|
try {
|
|
author = new Author(rs.getLong("id"), rs.getString("name"));
|
|
entry.getValue().close();
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return author;
|
|
}
|
|
|
|
@Nullable
|
|
public Category getCategory(long id) {
|
|
Map.Entry<ResultSet, Connection> entry = getResultSet(id, CATEGORY);
|
|
assert entry != null;
|
|
|
|
ResultSet rs = entry.getKey();
|
|
Category category = null;
|
|
try {
|
|
category = new Category(rs.getLong("id"), rs.getString("name"), rs.getString("image"));
|
|
entry.getValue().close();
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return category;
|
|
}
|
|
|
|
@Nullable
|
|
public Publisher getPublisher(long id) {
|
|
Map.Entry<ResultSet, Connection> entry = getResultSet(id, PUBLISHER);
|
|
assert entry != null;
|
|
|
|
ResultSet rs = entry.getKey();
|
|
Publisher publisher = null;
|
|
try {
|
|
publisher = new Publisher(rs.getLong("id"), rs.getString("name"));
|
|
entry.getValue().close();
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return publisher;
|
|
}
|
|
|
|
@Nullable
|
|
private List<Integer> getAuthors(long id) {
|
|
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));
|
|
}
|
|
entry.getValue().close();
|
|
return list;
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Nullable
|
|
public Book getBook(long id) {
|
|
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));
|
|
List<Integer> authorIds = getAuthors(id);
|
|
assert authorIds != null;
|
|
List<Author> authors = new ArrayList<>();
|
|
for (int i : authorIds) {
|
|
Author author = getAuthor(i);
|
|
assert author != null;
|
|
authors.add(author);
|
|
}
|
|
book = new Book(rs.getLong("id"), authors, publisher, category, rs.getString("title"), Year.of(rs.getInt("year")), rs.getInt("price"), rs.getString("description"), rs.getString("image"));
|
|
entry.getValue().close();
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return book;
|
|
}
|
|
|
|
@Nullable
|
|
public List<Book> getBooks() {
|
|
Map.Entry<List<ResultSet>, Connection> entry = getResultSets(BOOK, "id");
|
|
assert entry != null;
|
|
|
|
List<Long> ids = new ArrayList<>();
|
|
try {
|
|
for (ResultSet resultSet : entry.getKey()) {
|
|
ids.add(resultSet.getLong("id"));
|
|
}
|
|
entry.getValue().close();
|
|
} catch (SQLException ex) {
|
|
ex.printStackTrace();
|
|
}
|
|
List<Book> books = new ArrayList<>();
|
|
for(long id : ids) {
|
|
books.add(getBook(id));
|
|
}
|
|
return books;
|
|
}
|
|
|
|
@Nullable
|
|
public List<Book> getBooks(long category) {
|
|
Connection connection = getConnection();
|
|
assert connection != null;
|
|
|
|
List<Book> books = new ArrayList<>();
|
|
try {
|
|
PreparedStatement statement = connection.prepareStatement("SELECT (id) FROM book WHERE category = ?");
|
|
statement.setLong(1, category);
|
|
|
|
ResultSet resultSet = statement.executeQuery();
|
|
|
|
while(resultSet.next()) {
|
|
books.add(getBook(resultSet.getLong("id")));
|
|
}
|
|
|
|
connection.close();
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return books;
|
|
}
|
|
|
|
@Nullable
|
|
public List<Book> getBooks(@NotNull Category category) {
|
|
return getBooks(category.getId());
|
|
}
|
|
|
|
|
|
/**
|
|
* Inserts book into database
|
|
*
|
|
* authors, publishers, and category have to exist
|
|
* @param book Book
|
|
* @return insert count
|
|
*/
|
|
public int insert(@NotNull Book book) {
|
|
Connection connection = getConnection();
|
|
assert connection != null;
|
|
|
|
int insertCount = 0;
|
|
|
|
try {
|
|
PreparedStatement statement = connection.prepareStatement("INSERT INTO book (title, description, price, year, publisher, category, image) VALUES (?, ?, ?, ?, ?, ?, ?)");
|
|
statement.setString(1, book.getTitle());
|
|
statement.setString(2, book.getDescription());
|
|
statement.setInt(3, book.getPrice());
|
|
statement.setInt(4, book.getYear().getValue());
|
|
statement.setLong(5, book.getPublisher().getId());
|
|
statement.setLong(6, book.getCategory().getId());
|
|
statement.setString(7, book.getImage());
|
|
insertCount += statement.executeUpdate();
|
|
|
|
for(Author author : book.getAuthors()) {
|
|
PreparedStatement authorStatement = connection.prepareStatement("INSERT INTO author_book (author, book) VALUES (?, ?)");
|
|
statement.setLong(1, author.getId());
|
|
statement.setLong(2, book.getIsbn());
|
|
insertCount += authorStatement.executeUpdate();
|
|
}
|
|
|
|
connection.close();
|
|
} catch (SQLException ex) {
|
|
ex.printStackTrace();
|
|
}
|
|
return insertCount;
|
|
}
|
|
|
|
/**
|
|
* inserts Category into database
|
|
* @param category Category
|
|
* @return insert count
|
|
*/
|
|
public int insert(@NotNull Category category) {
|
|
Connection connection = getConnection();
|
|
assert connection != null;
|
|
|
|
int insertCount = 0;
|
|
|
|
try{
|
|
PreparedStatement statement = connection.prepareStatement("INSERT INTO category (name, image) VALUES (?, ?)");
|
|
statement.setString(1, category.getName());
|
|
statement.setString(2, category.getImage());
|
|
insertCount += statement.executeUpdate();
|
|
|
|
connection.close();
|
|
}catch (SQLException ex) {
|
|
ex.printStackTrace();
|
|
}
|
|
return insertCount;
|
|
}
|
|
|
|
/**
|
|
* inserts Author
|
|
* @param author Author
|
|
* @return insert count
|
|
*/
|
|
public int insert(@NotNull Author author) {
|
|
Connection connection = getConnection();
|
|
assert connection != null;
|
|
|
|
int insertCount = 0;
|
|
try{
|
|
PreparedStatement statement = connection.prepareStatement("INSERT INTO author (name) VALUES (?)");
|
|
statement.setString(1, author.getName());
|
|
|
|
insertCount += statement.executeUpdate();
|
|
|
|
connection.close();
|
|
}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;
|
|
}
|
|
|
|
|
|
|
|
|
|
@Nullable
|
|
private Map.Entry<ResultSet, Connection> getResultSet(long id, String table) {
|
|
Connection connection = getConnection();
|
|
try {
|
|
assert connection != null;
|
|
PreparedStatement statement = connection.prepareStatement("SELECT * FROM ? WHERE ID = ?");
|
|
statement.setString(1, table);
|
|
statement.setLong(2, id);
|
|
ResultSet resultSet = statement.executeQuery();
|
|
if (resultSet.next()) {
|
|
return new AbstractMap.SimpleEntry<>(resultSet, connection);
|
|
}
|
|
return null;
|
|
|
|
} catch (SQLException ex) {
|
|
ex.printStackTrace();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Nullable
|
|
private Map.Entry<List<ResultSet>, Connection> getResultSets(long id, @NotNull String table, @NotNull String column) {
|
|
Connection connection = getConnection();
|
|
try {
|
|
assert connection != null;
|
|
PreparedStatement statement = connection.prepareStatement("SELECT * FROM ? WHERE ? = ?");
|
|
statement.setString(1, table);
|
|
statement.setString(2, column);
|
|
statement.setLong(3, id);
|
|
|
|
return getListConnectionEntry(connection, statement);
|
|
} catch (SQLException ex) {
|
|
ex.printStackTrace();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Nullable
|
|
private Map.Entry<List<ResultSet>, Connection> getResultSets(@NotNull String table, @NotNull String columns) {
|
|
Connection connection = getConnection();
|
|
try {
|
|
assert connection != null;
|
|
PreparedStatement statement = connection.prepareStatement("SELECT (?) FROM ?");
|
|
statement.setString(1, columns);
|
|
statement.setString(2, table);
|
|
|
|
return getListConnectionEntry(connection, statement);
|
|
} catch (SQLException ex) {
|
|
ex.printStackTrace();
|
|
}
|
|
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;
|
|
}
|
|
} |