diff --git a/MobiReader/my_books.db b/MobiReader/my_books.db new file mode 100644 index 0000000..408a0ac Binary files /dev/null and b/MobiReader/my_books.db differ diff --git a/MobiReader/sample.db b/MobiReader/sample.db index de744ad..0798753 100644 Binary files a/MobiReader/sample.db and b/MobiReader/sample.db differ diff --git a/MobiReader/src/mobireader/SqliteDatabaseHandler.java b/MobiReader/src/mobireader/SqliteDatabaseHandler.java index 7cc16ad..087809e 100644 --- a/MobiReader/src/mobireader/SqliteDatabaseHandler.java +++ b/MobiReader/src/mobireader/SqliteDatabaseHandler.java @@ -53,7 +53,9 @@ class StatementBuilder public PreparedStatement findBookStatement(Connection connection) throws SQLException, ClassNotFoundException { - String statementText = "select * from books where title=?"; + String statementText = "select b.title, b.author_name, b.path_to_content," + + " a.additional_info from books b left join authors a on" + + " b.author_name = a.name where b.title=?"; return prepareStatement(connection, statementText); } @@ -67,7 +69,9 @@ class StatementBuilder public PreparedStatement getAllBooksStatement(Connection connection) throws SQLException, ClassNotFoundException { - String statementText = "select * from books"; + String statementText = "select b.title, b.author_name, b.path_to_content," + + " a.additional_info from books b left join authors a on" + + " b.author_name = a.name"; return prepareStatement(connection, statementText); } @@ -156,7 +160,9 @@ public class SqliteDatabaseHandler stat.executeQuery(); ResultSet rs = stat.executeQuery(); String path = rs.getString("path_to_content"); - Author author = new Author("Gepetto", title); + String author_name = rs.getString("author_name"); + String info_about_author = rs.getString("additional_info"); + Author author = new Author(author_name, info_about_author); return new Book(title, author, path); } catch(SQLException e) { System.err.println(e.getMessage()); } @@ -218,7 +224,7 @@ public class SqliteDatabaseHandler return null; } - public ArrayList getAllBooks(Author author) throws ClassNotFoundException + public ArrayList getAllBooks() throws ClassNotFoundException { try { @@ -233,8 +239,12 @@ public class SqliteDatabaseHandler { String path = rs.getString("path_to_content"); String title = rs.getString("title"); + String author_name = rs.getString("author_name"); + String info_about_author = rs.getString("additional_info"); + Author author = new Author(author_name, info_about_author); books.add(new Book(title, author, path)); } + rs.close(); return books; } catch(SQLException e) { System.err.println(e.getMessage()); } diff --git a/MobiReader/src/mobireader/gui/ui/AppMain.java b/MobiReader/src/mobireader/gui/ui/AppMain.java index 562d972..8022f3a 100644 --- a/MobiReader/src/mobireader/gui/ui/AppMain.java +++ b/MobiReader/src/mobireader/gui/ui/AppMain.java @@ -4,6 +4,7 @@ */ package mobireader.gui.ui; +import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import javafx.application.Application; @@ -121,7 +122,7 @@ public class AppMain extends Application { Label htmlLabel; Popup alertPopup; Tab webViewTab; - ArrayList myBooks = Book.createSomeExamples(); + ArrayList myBooks; SqliteDatabaseHandler db = new SqliteDatabaseHandler("my_books.db"); public static void main(String[] args) { @@ -129,8 +130,7 @@ public class AppMain extends Application { } @Override - public void start(final Stage primaryStage) throws ClassNotFoundException { - db.addBooks(myBooks); + public void start(final Stage primaryStage) throws ClassNotFoundException, SQLException { myBooks = db.getAllBooks(); stage = primaryStage; Scene scene = SceneBuilder.create() diff --git a/MobiReader/test/tests/SqlConnectionJUnitTest.java b/MobiReader/test/tests/SqlConnectionJUnitTest.java index 4176cc8..89b6365 100644 --- a/MobiReader/test/tests/SqlConnectionJUnitTest.java +++ b/MobiReader/test/tests/SqlConnectionJUnitTest.java @@ -67,6 +67,7 @@ public class SqlConnectionJUnitTest { assertEquals(path, bookFromDB.getPathToContent()); } + @Test public void testAddingFewBooksOfSameAuthorToDb() throws ClassNotFoundException { @@ -91,6 +92,34 @@ public class SqlConnectionJUnitTest { } + @Test + public void testGettingAllBooksFromDb() throws ClassNotFoundException + { + String title1 = "Title1"; + String title2 = "Title2"; + String title3 = "Title3"; + String authorName1 = "Gepetto"; + String authorName2 = "Geronimo"; + Author gepetto = new Author(authorName1); + Author geronimo = new Author(authorName2); + String path = "/somepath/book.txt"; + ArrayList books = new ArrayList<>(); + books.add(new Book(title1, gepetto, path)); + books.add(new Book(title2, gepetto, path)); + books.add(new Book(title3, gepetto, path)); + books.add(new Book(title1, geronimo, path)); + books.add(new Book(title2, geronimo, path)); + books.add(new Book(title3, geronimo, path)); + handler.addBooks(books); + ArrayList booksFromDB = handler.getAllBooks(); + assertEquals(books.size(), booksFromDB.size()); + for(int i = 0; i < books.size(); i++ ) + { + assertEquals(books.get(i).getTitle(), booksFromDB.get(i).getTitle()); + assertEquals(books.get(i).getAuthor().getName(), booksFromDB.get(i).getAuthor().getName()); + } + } + @Test public void testAddingAuthorToDb() throws Exception { @@ -103,6 +132,7 @@ public class SqlConnectionJUnitTest { assertEquals(additionalInfo, authorFromDB.getAdditionalInfo()); } + @Test public void testAddingTheSameAuthorToDbFewTimes() throws Exception {