Skip to content
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

create hw-jdbc #52

Open
wants to merge 35 commits into
base: main
Choose a base branch
from

Conversation

AntonZhdanov
Copy link

No description provided.

Comment on lines 16 to 28

bookDao.create(book);

System.out.println(book);

book.setPrice(BigDecimal.valueOf(500));
System.out.println(bookDao.update(book));

System.out.println(bookDao.findById(book.getId()));

System.out.println(bookDao.findAll());

System.out.println(bookDao.deleteById(book.getId()));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe empty lines is not good? (But it just my opinion)

Comment on lines 51 to 53
while (resultSet.next()) {
book = getBookFromResultSet(resultSet);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while in getting one result

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not resolved

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't agree with you in this case. I think I need this while loop for iterating data in resultSet.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you need to iterate if you get one value from result set?
"SELECT * FROM books WHERE id = ?" returns one row
you don`t need to iterate on something

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. The book's id is unique. When you try to find it by id you can get only one row
Of course, while works, in this case, the same as if.
but from the spectator's perspective, it could trick.

Comment on lines 79 to 81
updateStatement.setLong(3, book.getId());
updateStatement.setString(1, book.getTitle());
updateStatement.setBigDecimal(2, book.getPrice());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe better use constants and regroup? (FIRST_PARAMETER...)

Comment on lines 40 to 43
"id=" + id +
", title='" + title + '\'' +
", price=" + price +
'}';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you install maven plugin in pom xml?

Comment on lines 2 to 4
id bigint(10) NOT NULL AUTO_INCREMENT,
title varchar(225) NOT NULL,
price bigint(10),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in checklist there is option to style .sql like this:

Suggested change
id bigint(10) NOT NULL AUTO_INCREMENT,
title varchar(225) NOT NULL,
price bigint(10),
id BIGINT(10) NOT NULL AUTO_INCREMENT,
title VARCHAR(225) NOT NULL,
price BIGINT(10),

Comment on lines 51 to 53
while (resultSet.next()) {
book = getBookFromResultSet(resultSet);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not resolved

Comment on lines 51 to 53
while (resultSet.next()) {
book = getBookFromResultSet(resultSet);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you need to iterate if you get one value from result set?
"SELECT * FROM books WHERE id = ?" returns one row
you don`t need to iterate on something

@AntonZhdanov AntonZhdanov marked this pull request as draft August 11, 2023 14:51
@AntonZhdanov AntonZhdanov marked this pull request as ready for review August 11, 2023 14:52
Copy link

@nacenik nacenik left a 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 :)

Comment on lines 22 to 26
private static final String INSERT_REQUEST = "INSERT INTO books(title, price) VALUES(?, ?)";
private static final String REQUEST_BOOK_FROM_DB = "SELECT * FROM books WHERE id = ?";
private static final String GET_ALL_REQUEST_BOOK = "SELECT * FROM books";
private static final String UPDATE_REQUEST = "UPDATE books SET title = ?, price = ? WHERE id = ?";
private static final String DELETE_BOOK_REQUEST = "DELETE FROM books WHERE id = ?";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend you to put this queries in the methods where they are used in the way of good practice
image
It will be hard to menage them, if you have a lot of queries and with a lot of condition and sub-queries

Comment on lines 51 to 53
while (resultSet.next()) {
book = getBookFromResultSet(resultSet);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. The book's id is unique. When you try to find it by id you can get only one row
Of course, while works, in this case, the same as if.
but from the spectator's perspective, it could trick.

Comment on lines 82 to 84
updateStatement.setLong(ID_INDEX, book.getId());
updateStatement.setString(TITLE_INDEX, book.getTitle());
updateStatement.setBigDecimal(PRICE_INDEX, book.getPrice());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it suit your query? I mean, Does the id have first place in the query and etc?

deleteStatement.setLong(1, id);
return deleteStatement.executeUpdate() > 0;
} catch (SQLException e) {
throw new DataProcessingException("Can't delete book by id " + id, e);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new DataProcessingException("Can't delete book by id " + id, e);
throw new DataProcessingException("Can't delete book by id = " + id, e);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants