Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/ru/javawebinar/basejava/sql/SqlHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import ru.javawebinar.basejava.exception.StorageException;

public class SqlHelper {

private SqlExecutor sqlExecutor;
private final ConnectionFactory connectionFactory;

public SqlHelper(ConnectionFactory connectionFactory) {
Expand Down
24 changes: 22 additions & 2 deletions src/ru/javawebinar/basejava/storage/SqlStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import ru.javawebinar.basejava.exception.NotExistStorageException;
import ru.javawebinar.basejava.model.ContactType;
import ru.javawebinar.basejava.model.Resume;
import ru.javawebinar.basejava.sql.SqlHelper;

Expand Down Expand Up @@ -41,17 +43,35 @@ public void save(Resume r) {
ps.execute();
return null;
});
for (Map.Entry<ContactType, String> e : r.getContacts().entrySet()) {
sqlHelper.<Void>execute("INSERT INTO contact (resume_uuid, type, value) VALUES (?,?,?)",
ps -> {
ps.setString(1, r.getUuid());
ps.setString(2, e.getKey().name());
ps.setString(3, e.getValue());
return null;
});
}
}

@Override
public Resume get(String uuid) {
return sqlHelper.execute("SELECT * FROM resume r WHERE r.uuid = ?", ps -> {
return sqlHelper.execute("SELECT * FROM resume r " +
" LEFT JOIN contact c " +
" ON r.uuid = c.resume_uuid " +
" WHERE r.uuid = ?", ps -> {
ps.setString(1, uuid);
ResultSet rs = ps.executeQuery();
if (!rs.next()) {
throw new NotExistStorageException(uuid);
}
return new Resume(uuid, rs.getString("full_name"));
Resume resume = new Resume(uuid, rs.getString("full_name"));
do {
String value = rs.getString("value");
ContactType type = ContactType.valueOf(rs.getString("type"));
resume.addContact(type, value);
} while (rs.next());
return resume;
});
}

Expand Down