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
120 lines (104 loc) · 4.05 KB
/
SqlStorage.java
File metadata and controls
120 lines (104 loc) · 4.05 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
package com.urise.webapp.storage;
import com.urise.webapp.exception.NotExistStorageException;
import com.urise.webapp.model.ContactType;
import com.urise.webapp.model.Resume;
import com.urise.webapp.sql.SqlHelper;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class SqlStorage implements Storage {
public final SqlHelper sqlHelper;
public SqlStorage(String dbUrl, String dbUser, String dbPassword) {
sqlHelper = new SqlHelper(dbUrl, dbUser, dbPassword);
}
@Override
public void clear() {
sqlHelper.transactionExecute(ps -> ps.execute(), "DELETE FROM RESUME");
}
@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();
}
try (PreparedStatement ps = conn.prepareStatement("INSERT INTO contact (resume_uuid, type, value) values (?,?,?)")) {
for (Map.Entry<ContactType, String> entry : resume.getContacts().entrySet()) {
ps.setString(1, resume.getUuid());
ps.setString(2, entry.getKey().name());
ps.setString(3, entry.getValue());
ps.addBatch();
}
ps.executeBatch();
}
return null;
}
);
}
@Override
public Resume get(String uuid) {
return sqlHelper.transactionExecute(ps -> {
ps.setString(1, uuid);
ResultSet rs = ps.executeQuery();
if (!rs.next()) {
throw new NotExistStorageException(uuid);
}
Resume r = new Resume(uuid, rs.getString("full_name"));
do {
String value = rs.getString("value");
ContactType type = ContactType.valueOf(rs.getString("type"));
r.addContact(type, value);
} while (rs.next());
return r;
},
"SELECT * FROM RESUME r " +
"LEFT JOIN CONTACT c on (r.uuid=c.resume_uuid) " +
"WHERE r.uuid=?");
}
@Override
public void delete(String uuid) {
/*удаление по каскаду*/
sqlHelper.transactionExecute(p -> {
p.setString(1, uuid);
if (p.executeUpdate() == 0) {
throw new NotExistStorageException(uuid);
}
return null;
}, "DELETE from RESUME where UUID=?");
}
@Override
public List<Resume> getAllSorted() {
return sqlHelper.transactionExecute(p -> {
ResultSet rs = p.executeQuery();
List<Resume> list = new ArrayList<>();
while (rs.next()) {
list.add(new Resume(rs.getString("uuid"), rs.getString("full_name")));
}
return list;
}, "SELECT * FROM RESUME ORDER BY full_name,uuid");
}
@Override
public int size() {
return sqlHelper.transactionExecute(ps -> {
ResultSet rs = ps.executeQuery();
if (!rs.next()) {
return 0;
}
return rs.getInt(1);
}, "SELECT count (uuid) FROM RESUME");
}
@Override
public void update(Resume resume) {
sqlHelper.transactionExecute(ps -> {
ps.setString(1, resume.getFullName());
ps.setString(2, resume.getUuid());
if (ps.executeUpdate() == 0) {
throw new NotExistStorageException(resume.getUuid());
}
return null;
}, "UPDATE RESUME set full_name=? where uuid=?");
}
}