-
Notifications
You must be signed in to change notification settings - Fork 438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added BookDao and connected project to db #53
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there are extra spaces between the imports, but overall I have no complaints, great job!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good solution, let's improve it
Book create(Book book); | ||
Optional<Book> findById(Long id); | ||
List<Book> findAll(); | ||
Book update(Book book); | ||
boolean deleteById(Long id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add spaces between methods
private static final String CREATE_QUERY = "INSERT INTO books (id, title, price) VALUES (?, ?, ?)"; | ||
private static final String FIND_BY_ID_QUERY = "SELECT * FROM books WHERE id = ?"; | ||
private static final String FIND_ALL_QUERY = "SELECT * FROM books"; | ||
private static final String UPDATE_QUERY = "UPDATE books SET title = ?, price = ? WHERE id = ?"; | ||
private static final String DELETE_QUERY = "DELETE FROM books WHERE id = ?"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Create a query in the method where you will use it. Like method variable
private static final String ID_COLUMN = "books.id"; | ||
private static final String TITLE_COLUMN = "books.title"; | ||
private static final String PRICE_COLUMN = "books.price"; | ||
private static final String NO_UPDATES_EXCEPTION_TEXT = "Expected to insert at least 1 row, but inserted 0 rows."; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are you inserting rows during the update? Or do you just update values?
private static final int TITLE_INDEX_FOR_UPDATE = 1; | ||
private static final int PRICE_INDEX_FOR_UPDATE = 2; | ||
private static final int ID_INDEX_FOR_UPDATE = 3; | ||
private static final String CREATE_QUERY = "INSERT INTO books (id, title, price) VALUES (?, ?, ?)"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The id is auto-generated. you don't need to set it up by your hand.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not for updating “id” value, it's index of “id” column for update operation (“…WHERE id = ?”). But I'll rename it for better readability.
} | ||
} | ||
|
||
private Book formBookFromQuery(ResultSet resultSet) throws SQLException{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bad method name
private Book formBookFromQuery(ResultSet resultSet) throws SQLException{ | |
private Book parseBookFromQuery(ResultSet resultSet) throws SQLException{ |
public DataProcessingException(String message, Throwable ex) { | ||
super(message, ex); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/main/resources/init_db.sql
Outdated
@@ -0,0 +1,3 @@ | |||
CREATE DATABASE book_schema; | |||
USE book_schema; | |||
CREATE TABLE books (`id` BIGINT NOT NULL AUTO_INCREMENT, `title` VARCHAR(255), `price` DECIMAL, PRIMARY KEY (`id`)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd recommend you put this query in separate lines. It's hard to read it.
… to methods variables, renamed update index's constants for better readability, renamed "formBookFromQuery()" into "parseBookFromQuery", removed redundant line in DataProcessingException, put query at init_db.sql in separate lines for better readability, changed NO_UPDATES_EXCEPTION_TEXT for greater correctness of the message
No description provided.