Skip to content

Commit b6222ac

Browse files
committed
HW15 DB часть 1
1 parent 6e7fee6 commit b6222ac

2 files changed

Lines changed: 28 additions & 35 deletions

File tree

src/com/urise/webapp/model/StringSection.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/com/urise/webapp/storage/SqlStorage.java

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.urise.webapp.storage;
22

3-
import com.urise.webapp.Util.JsonParser;
43
import com.urise.webapp.exception.NotExistStorageException;
54
import com.urise.webapp.model.*;
65
import com.urise.webapp.sql.SqlHelper;
@@ -24,31 +23,31 @@ public void clear() {
2423
public Resume get(String uuid) {
2524
return sqlHelper.transactionalExecute(conn -> {
2625
Resume r;
27-
try (PreparedStatement ps = conn.prepareStatement("SELECT * FROM resume WHERE uuid =?")) {
26+
try (PreparedStatement ps = conn.prepareStatement("SELECT * FROM resume WHERE uuid=?");
27+
PreparedStatement psContacts = conn.prepareStatement("SELECT * FROM contact WHERE resume_uuid=?");
28+
PreparedStatement psSections = conn.prepareStatement("SELECT * FROM section WHERE resume_uuid=?")) {
29+
2830
ps.setString(1, uuid);
2931
ResultSet rs = ps.executeQuery();
3032
if (!rs.next()) {
3133
throw new NotExistStorageException(uuid);
3234
}
3335
r = new Resume(uuid, rs.getString("full_name"));
34-
}
3536

36-
try (PreparedStatement ps = conn.prepareStatement("SELECT * FROM contact WHERE resume_uuid =?")) {
37-
ps.setString(1, uuid);
38-
ResultSet rs = ps.executeQuery();
39-
while (rs.next()) {
40-
addContact(rs, r);
37+
psContacts.setString(1, uuid);
38+
ResultSet rsContacts = psContacts.executeQuery();
39+
while (rsContacts.next()) {
40+
String cType = rsContacts.getString("type");
41+
r.addContact(ContactType.valueOf(cType), rsContacts.getString("value"));
4142
}
42-
}
4343

44-
try (PreparedStatement ps = conn.prepareStatement("SELECT * FROM section WHERE resume_uuid =?")) {
45-
ps.setString(1, uuid);
46-
ResultSet rs = ps.executeQuery();
47-
while (rs.next()) {
48-
addSection(rs, r);
44+
psSections.setString(1, uuid);
45+
ResultSet rsSections = psSections.executeQuery();
46+
47+
while (rsSections.next()) {
48+
addSection(rsSections, r);
4949
}
5050
}
51-
5251
return r;
5352
});
5453
}
@@ -152,16 +151,24 @@ private void insertContact(Connection conn, Resume r) throws SQLException {
152151
}
153152

154153
private void insertSection(Connection conn, Resume r) throws SQLException {
155-
try (PreparedStatement ps = conn.prepareStatement("INSERT INTO section (resume_uuid, type, value) VALUES (?,?,?)")) {
156-
for (Map.Entry<SectionType, Section> e : r.getSections().entrySet()) {
154+
try (PreparedStatement ps = conn.prepareStatement("" +
155+
"INSERT INTO section (resume_uuid, type, value) VALUES (?,?,?)")) {
156+
for (Map.Entry<SectionType, Section> entry : r.getSections().entrySet()) {
157157
ps.setString(1, r.getUuid());
158-
ps.setString(2, e.getKey().name());
159-
Section section = e.getValue();
160-
ps.setString(3, JsonParser.write(section, Section.class));
158+
SectionType sectionType = entry.getKey();
159+
ps.setString(2, sectionType.name());
160+
Section section = entry.getValue();
161+
String value = switch (sectionType) {
162+
case OBJECTIVE, PERSONAL -> ((TextSection) section).getText();
163+
case ACHIEVEMENT, QUALIFICATIONS -> String.join("\n", ((ListSection) section).getItems());
164+
default -> "";
165+
};
166+
ps.setString(3, value);
161167
ps.addBatch();
162168
}
163169
ps.executeBatch();
164170
}
171+
165172
}
166173

167174
private void deleteContacts(Connection conn, Resume r) throws SQLException {
@@ -178,17 +185,10 @@ private void deleteSections(Connection conn, Resume r) throws SQLException {
178185
}
179186
}
180187

181-
private void addContact(ResultSet rs, Resume r) throws SQLException {
182-
String value = rs.getString("value");
183-
if (value != null) {
184-
r.addContact(ContactType.valueOf(rs.getString("type")), value);
185-
}
186-
}
187-
188188
private void addSection(ResultSet rs, Resume r) throws SQLException {
189189
SectionType type = SectionType.valueOf(rs.getString("type"));
190190
Section section = switch (type) {
191-
case PERSONAL, OBJECTIVE -> new StringSection(rs.getString("value"));
191+
case PERSONAL, OBJECTIVE -> new TextSection(rs.getString("value"));
192192
case ACHIEVEMENT, QUALIFICATIONS -> new ListSection(new ArrayList<>(Arrays.asList(
193193
rs.getString("value").split("\n"))));
194194
default -> throw new IllegalStateException("Unknown Section Type");

0 commit comments

Comments
 (0)