-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLStorage.java
More file actions
257 lines (225 loc) · 10.4 KB
/
SQLStorage.java
File metadata and controls
257 lines (225 loc) · 10.4 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package webapp.storage;
import webapp.exceptions.StorageException;
import webapp.model.Resume;
import webapp.model.Section;
import webapp.model.TypeOfContact;
import webapp.model.TypeOfSection;
import webapp.sql.SQLHelper;
import webapp.util.JsonParser;
import java.sql.*;
import java.util.*;
// TODO implement Section (except OrganizationSection)
// TODO Join and split ListSection by `\n`
public class SQLStorage implements Storage {
private static final String SQL_CLEAR_ALL = "DELETE FROM resume";
private static final String SQL_GET_JOIN = "SELECT * FROM resume r LEFT JOIN contact c ON r.uuid =c.resume_uuid WHERE r.uuid =?";
private static final String SQL_GET_FROM_RESUME = "SELECT * FROM resume WHERE uuid =?";
private static final String SQL_GET_FROM_CONTACT = "SELECT * FROM contact WHERE resume_uuid =?";
private static final String SQL_GET_FROM_SECTION = "SELECT * FROM section WHERE resume_uuid =?";
private static final String SQL_UPDATE_RESUME = "UPDATE resume SET full_name = ? WHERE uuid = ?";
private static final String SQL_UPDATE_CONTACTS = "UPDATE contact SET value = ? WHERE type =? AND resume_uuid = ?";
private static final String SQL_INSERT_RESUME = "INSERT INTO resume (uuid, full_name) VALUES (?,?)";
private static final String SQL_INSERT_CONTACT = "INSERT INTO contact (resume_uuid, type, value) VALUES (?,?,?)";
private static final String SQL_INSERT_SECTION = "INSERT INTO section (resume_uuid, type, value) VALUES (?,?,?)";
private static final String SQL_SAVE_RESUME = "INSERT INTO resume (full_name,uuid) VALUES (?,?)";
private static final String SQL_SAVE_CONTACTS = "INSERT INTO contact (value, type,resume_uuid) VALUES (?,?,?)";
private static final String SQL_DELETE_RESUME = "DELETE FROM resume WHERE uuid=?";
private static final String SQL_DELETE_CONTACT = "DELETE FROM contact WHERE resume_uuid=?";
private static final String SQL_DELETE_SECTION = "DELETE FROM section WHERE resume_uuid=?";
private static final String SQL_GET_ALL_SORTED = "SELECT * FROM resume r LEFT JOIN contact c ON r.uuid = c.resume_uuid ORDER BY full_name, uuid";
private static final String SQL_SELECT_CONTACT = "SELECT * FROM contact";
private static final String SQL_SELECT_SECTION = "SELECT * FROM section";
private static final String SQL_GET_RESUME_SORTED = "SELECT * FROM resume ORDER BY full_name, uuid";
private static final String SQL_TABLE_SIZE = "SELECT count(*) FROM resume";
private final SQLHelper sqlHelper;
public SQLStorage(String dbUrl, String dbUser, String dbPassword) {
try {
Class.forName("org.postgresql.Driver");
sqlHelper = new SQLHelper(() -> DriverManager.getConnection(dbUrl, dbUser, dbPassword));
} catch (ClassNotFoundException e) {
throw new StorageException("Can't load PostgreSQL driver " + e);
}
}
@Override
public void clear() {
sqlHelper.execution(SQL_CLEAR_ALL, preparedStatement -> {
preparedStatement.executeUpdate();
return null;
}
);
}
@Override
public Resume get(UUID uuid) {
return sqlHelper.transactionalExecute(conn -> {
Resume r;
try (PreparedStatement ps = conn.prepareStatement(SQL_GET_FROM_RESUME)) {
ps.setString(1, String.valueOf(uuid));
ResultSet resultSet = ps.executeQuery();
if (!resultSet.next()) {
throw new StorageException("UUID = " + String.valueOf(uuid) + " is not EXIST!");
}
r = new Resume(uuid, resultSet.getString("full_name"));
}
try (PreparedStatement ps = conn.prepareStatement(SQL_GET_FROM_CONTACT)) {
ps.setString(1, String.valueOf(uuid));
ResultSet rs = ps.executeQuery();
while (rs.next()) {
addContact(rs, r);
}
}
try (PreparedStatement ps = conn.prepareStatement(SQL_GET_FROM_SECTION)) {
ps.setString(1, String.valueOf(uuid));
ResultSet rs = ps.executeQuery();
while (rs.next()) {
addSection(rs, r);
}
}
return r;
});
}
@Override
public void update(UUID uuid, Resume r) {
sqlHelper.transactionalExecute(conn -> {
try (PreparedStatement ps = conn.prepareStatement(SQL_UPDATE_RESUME)) {
ps.setString(1, r.getFullName());
ps.setString(2, String.valueOf(r.getUuid()));
if (ps.executeUpdate() != 1) {
throw new StorageException("UUID is " + String.valueOf(r.getUuid()));
}
}
deleteContacts(conn, r);
deleteSections(conn, r);
insertContacts(conn, r);
insertSections(conn, r);
return null;
});
}
@Override
public void save(Resume r) {
sqlHelper.transactionalExecute(conn -> {
try (PreparedStatement ps = conn.prepareStatement(SQL_INSERT_RESUME)) {
ps.setString(1, String.valueOf(r.getUuid()));
ps.setString(2, r.getFullName());
ps.execute();
}
insertContacts(conn, r);
insertSections(conn, r);
return null;
}
);
}
@Override
public void delete(UUID uuid) {
sqlHelper.execution(SQL_DELETE_RESUME, preparedStatement -> {
preparedStatement.setString(1, String.valueOf(uuid));
if (preparedStatement.executeUpdate() == 0) {
throw new StorageException(String.valueOf(uuid));
}
return null;
});
}
@Override
public List<Resume> getAllSorted() {
return sqlHelper.transactionalExecute(conn -> {
Map<UUID, Resume> resumes = new LinkedHashMap<>();
try (PreparedStatement ps = conn.prepareStatement(SQL_GET_RESUME_SORTED)) {
ResultSet rs = ps.executeQuery();
while (rs.next()) {
UUID uuid = UUID.fromString(rs.getString("uuid"));
resumes.put(uuid, new Resume(uuid, rs.getString("full_name")));
}
}
try (PreparedStatement ps = conn.prepareStatement(SQL_SELECT_CONTACT)) {
ResultSet rs = ps.executeQuery();
while (rs.next()) {
UUID uuid = UUID.fromString(rs.getString("resume_uuid"));
Resume r = resumes.get(uuid);
addContact(rs, r);
}
}
try (PreparedStatement ps = conn.prepareStatement(SQL_SELECT_SECTION)) {
ResultSet rs = ps.executeQuery();
while (rs.next()) {
UUID uuid = UUID.fromString(rs.getString("resume_uuid"));
Resume r = resumes.get(uuid);
addSection(rs, r);
}
}
return new ArrayList<>(resumes.values());
});
}
@Override
public int size() {
return sqlHelper.execution(SQL_TABLE_SIZE, preparedStatement -> {
ResultSet resultSet = preparedStatement.executeQuery();
return resultSet.next() ? resultSet.getInt(1) : 0;
});
}
private void updateResumeToDB(Resume r, Connection conn, String sql, String exception) throws SQLException {
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, r.getFullName());
ps.setString(2, String.valueOf(r.getUuid()));
if (ps.executeUpdate() != 1) {
throw new StorageException(exception + String.valueOf(r.getUuid()));
}
}
}
private void updateContactsToDB(Resume r, Connection conn, String sql) throws SQLException {
try (PreparedStatement ps = conn.prepareStatement(sql)) {
for (Map.Entry<TypeOfContact, String> e : r.getContacts().entrySet()) {
ps.setString(3, String.valueOf(r.getUuid()));
ps.setString(2, e.getKey().name());
ps.setString(1, e.getValue());
ps.addBatch();
}
ps.executeBatch();
}
}
private void insertContacts(Connection conn, Resume r) throws SQLException {
try (PreparedStatement ps = conn.prepareStatement(SQL_INSERT_CONTACT)) {
for (Map.Entry<TypeOfContact, String> e : r.getContacts().entrySet()) {
ps.setString(1, String.valueOf(r.getUuid()));
ps.setString(2, e.getKey().name());
ps.setString(3, e.getValue());
ps.addBatch();
}
ps.executeBatch();
}
}
private void insertSections(Connection conn, Resume r) throws SQLException {
try (PreparedStatement ps = conn.prepareStatement(SQL_INSERT_SECTION)) {
for (Map.Entry<TypeOfSection, Section> e : r.getSections().entrySet()) {
ps.setString(1, String.valueOf(r.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 deleteContacts(Connection conn, Resume r) throws SQLException {
deleteAttributes(conn, r, SQL_DELETE_CONTACT);
}
private void deleteSections(Connection conn, Resume r) throws SQLException {
deleteAttributes(conn, r, SQL_DELETE_SECTION);
}
private void deleteAttributes(Connection conn, Resume r, String sql) throws SQLException {
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, String.valueOf(r.getUuid()));
ps.execute();
}
}
private void addContact(ResultSet rs, Resume r) throws SQLException {
String value = rs.getString("value");
if (value != null) {
r.setContact(TypeOfContact.valueOf(rs.getString("type")), value);
}
}
private void addSection(ResultSet rs, Resume r) throws SQLException {
String value = rs.getString("value");
if (value != null) {
r.setSection(TypeOfSection.valueOf(rs.getString("type")), JsonParser.read(value, Section.class));
}
}
}