+ get(Author/Publisher/Category/Book)
This commit is contained in:
parent
388459b6a2
commit
18efa59177
@ -1,16 +1,26 @@
|
||||
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.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;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -41,7 +51,9 @@ public class Database {
|
||||
|
||||
Connection connection = getConnection();
|
||||
assert connection != null;
|
||||
try(BufferedReader br = new BufferedReader(new FileReader(getClass().getClassLoader().getResource("database.sql").getFile()))) {
|
||||
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();
|
||||
@ -88,8 +100,141 @@ public class Database {
|
||||
|
||||
}
|
||||
|
||||
@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, String table, 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);
|
||||
|
||||
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;
|
||||
|
||||
|
||||
}catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@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, "category");
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
@ -4,6 +4,7 @@ import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.Year;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Johannes Theiner
|
||||
@ -15,8 +16,8 @@ import java.time.Year;
|
||||
@AllArgsConstructor
|
||||
public class Book {
|
||||
|
||||
private int isbn;
|
||||
private Author author;
|
||||
private long isbn;
|
||||
private List<Author> authors;
|
||||
private Publisher publisher;
|
||||
private Category category;
|
||||
private String title;
|
||||
|
@ -13,7 +13,7 @@ import lombok.Data;
|
||||
@AllArgsConstructor
|
||||
public class Category {
|
||||
|
||||
private int id;
|
||||
private long id;
|
||||
private String name;
|
||||
private String image;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user