forked from JavaOPs/basejava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlStorage.java
More file actions
192 lines (170 loc) · 7 KB
/
SqlStorage.java
File metadata and controls
192 lines (170 loc) · 7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package ru.javaops.webapp.storage;
import ru.javaops.webapp.exception.NotExistStorageException;
import ru.javaops.webapp.model.*;
import ru.javaops.webapp.sql.SqlHelper;
import ru.javaops.webapp.util.JsonParser;
import java.sql.*;
import java.util.*;
public class SqlStorage implements IStorage {
private final SqlHelper sqlHelper;
public SqlStorage(String dbUrl, String dbUser, String dbPassword) {
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
throw new IllegalStateException(e);
}
sqlHelper = new SqlHelper(() -> DriverManager.getConnection(dbUrl, dbUser, dbPassword));
}
@Override
public void clear() {
sqlHelper.execute("DELETE FROM resume");
}
@Override
public Resume get(String uuid) {
return sqlHelper.transactionalExecute(conn -> {
try (PreparedStatement ps = conn.prepareStatement("SELECT * FROM resume r WHERE r.uuid = ?")) {
ps.setString(1, uuid);
ResultSet rs = ps.executeQuery();
if (!rs.next()) {
throw new NotExistStorageException(uuid);
}
Resume resume = new Resume(uuid, rs.getString("full_name"));
try (PreparedStatement ps2 = conn.prepareStatement("SELECT * FROM resume r LEFT JOIN contact c on r.uuid = c.resume_uuid WHERE c.resume_uuid = ?")) {
ps2.setString(1, uuid);
ResultSet rs2 = ps2.executeQuery();
if (rs2.next()) {
addContacts(rs2, resume);
}
}
try (PreparedStatement ps1 = conn.prepareStatement("SELECT * FROM resume r LEFT JOIN section s on r.uuid = s.resume_uuid WHERE s.resume_uuid = ?")) {
ps1.setString(1, uuid);
ResultSet rs1 = ps1.executeQuery();
if (rs1.next()) {
addSections(rs1, resume);
}
}
return resume;
}
});
}
@Override
public void update(Resume resume) {
sqlHelper.transactionalExecute(conn -> {
try (PreparedStatement ps = conn.prepareStatement("UPDATE resume SET full_name = ? WHERE uuid = ?")) {
ps.setString(1, resume.getFullName());
ps.setString(2, resume.getUuid());
if (ps.executeUpdate() == 0) {
throw new NotExistStorageException(resume.getUuid());
}
}
deleteContacts(resume, conn);
deleteSections(resume, conn);
insertContacts(resume, conn);
insertSections(resume, conn);
return null;
});
}
@Override
public void save(Resume resume) {
sqlHelper.transactionalExecute(conn -> {
try (PreparedStatement ps = conn.prepareStatement("INSERT INTO resume (uuid, full_name) VALUES (?,?)")) {
ps.setString(1, resume.getUuid());
ps.setString(2, resume.getFullName());
ps.execute();
}
insertContacts(resume, conn);
insertSections(resume, conn);
return null;
});
}
@Override
public void delete(String uuid) {
sqlHelper.execute("DELETE FROM resume r WHERE r.uuid = ?", ps -> {
ps.setString(1, uuid);
if (ps.executeUpdate() == 0) {
throw new NotExistStorageException(uuid);
}
return null;
});
}
@Override
public List<Resume> getAllSorted() {
return sqlHelper.transactionalExecute(conn -> {
try (PreparedStatement ps = conn.prepareStatement("SELECT * FROM resume ORDER BY full_name, uuid ASC ")) {
ResultSet rs = ps.executeQuery();
List<Resume> resumes = new ArrayList<>();
while (rs.next()) {
String uuid = rs.getString("uuid").replaceAll("\\s", "");
resumes.add(get(uuid));
}
return resumes;
}
});
}
@Override
public int size() {
return sqlHelper.execute("SELECT COUNT(*) FROM resume", (ps) -> {
ResultSet rs = ps.executeQuery();
return rs.next() ? rs.getInt(1) : 0;
});
}
private void addContact(ResultSet rs, Resume resume) throws SQLException {
String value = rs.getString("value");
if (rs.getString("value") != null) {
resume.setContact(ContactType.valueOf(rs.getString("type")), value);
}
}
private void addSection(ResultSet rs, Resume resume) throws SQLException {
String value = rs.getString("value");
if (value != null) {
SectionType type = SectionType.valueOf(rs.getString("type"));
resume.setSection(type, JsonParser.read(value, Section.class));
}
}
private void insertContacts(Resume resume, Connection conn) throws SQLException {
try (PreparedStatement ps = conn.prepareStatement("INSERT INTO contact (resume_uuid, type, value) VALUES (?,?,?)")) {
for (Map.Entry<ContactType, String> e : resume.getContacts().entrySet()) {
ps.setString(1, resume.getUuid());
ps.setString(2, e.getKey().name());
ps.setString(3, e.getValue());
ps.addBatch();
}
ps.executeBatch();
}
}
private void insertSections(Resume resume, Connection conn) throws SQLException {
List<String> list;
try (PreparedStatement ps = conn.prepareStatement("INSERT INTO section (resume_uuid, type, value) VALUES (?,?,?)")) {
for (Map.Entry<SectionType, Section> e : resume.getSections().entrySet()) {
ps.setString(1, resume.getUuid());
ps.setString(2, e.getKey().name());
Section section = e.getValue();
ps.setString(3, JsonParser.write(section, Section.class));
ps.addBatch();
}
ps.executeBatch();
}
}
private void addContacts(ResultSet rs, Resume resume) throws SQLException {
do {
addContact(rs, resume);
} while (rs.next());
}
private void addSections(ResultSet rs, Resume resume) throws SQLException {
do {
addSection(rs, resume);
} while (rs.next());
}
private void deleteContacts(Resume resume, Connection conn) throws SQLException {
deleteAttributes(resume, conn, "DELETE FROM contact c WHERE c.resume_uuid = ?");
}
private void deleteSections(Resume resume, Connection conn) throws SQLException {
deleteAttributes(resume, conn, "DELETE FROM section s WHERE s.resume_uuid = ?");
}
private void deleteAttributes(Resume resume, Connection conn, String sql) throws SQLException {
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, resume.getUuid());
ps.execute();
}
}
}