33import ru .javawebinar .basejava .exception .NotExistStorageException ;
44import ru .javawebinar .basejava .model .ContactType ;
55import ru .javawebinar .basejava .model .Resume ;
6+ import ru .javawebinar .basejava .model .Section ;
7+ import ru .javawebinar .basejava .model .SectionType ;
68import ru .javawebinar .basejava .sql .SqlHelper ;
9+ import ru .javawebinar .basejava .util .JsonParser ;
710
811import java .sql .*;
912import java .util .ArrayList ;
1013import java .util .LinkedHashMap ;
1114import java .util .List ;
1215import java .util .Map ;
1316
14- // TODO implement Section (except OrganizationSection)
15- // TODO Join and split ListSection by `\n`
1617public class SqlStorage implements Storage {
1718 public final SqlHelper sqlHelper ;
1819
@@ -27,23 +28,34 @@ public void clear() {
2728
2829 @ Override
2930 public Resume get (String uuid ) {
30- return sqlHelper .execute ("" +
31- " SELECT * FROM resume r " +
32- " LEFT JOIN contact c " +
33- " ON r.uuid = c.resume_uuid " +
34- " WHERE r.uuid =? " ,
35- ps -> {
31+ return sqlHelper .transactionalExecute (conn -> {
32+ Resume r ;
33+ try (PreparedStatement ps = conn .prepareStatement ("SELECT * FROM resume WHERE uuid =?" )) {
3634 ps .setString (1 , uuid );
3735 ResultSet rs = ps .executeQuery ();
3836 if (!rs .next ()) {
3937 throw new NotExistStorageException (uuid );
4038 }
41- Resume r = new Resume (uuid , rs .getString ("full_name" ));
42- do {
39+ r = new Resume (uuid , rs .getString ("full_name" ));
40+ }
41+
42+ try (PreparedStatement ps = conn .prepareStatement ("SELECT * FROM contact WHERE resume_uuid =?" )) {
43+ ps .setString (1 , uuid );
44+ ResultSet rs = ps .executeQuery ();
45+ while (rs .next ()) {
4346 addContact (rs , r );
44- } while (rs .next ());
47+ }
48+ }
4549
46- return r ;
50+ try (PreparedStatement ps = conn .prepareStatement ("SELECT * FROM section WHERE resume_uuid =?" )) {
51+ ps .setString (1 , uuid );
52+ ResultSet rs = ps .executeQuery ();
53+ while (rs .next ()) {
54+ addSection (rs , r );
55+ }
56+ }
57+
58+ return r ;
4759 });
4860 }
4961
@@ -58,22 +70,26 @@ public void update(Resume r) {
5870 }
5971 }
6072 deleteContacts (conn , r );
61- insertContact (conn , r );
73+ deleteSections (conn , r );
74+ insertContacts (conn , r );
75+ insertSections (conn , r );
6276 return null ;
6377 });
6478 }
6579
6680 @ Override
6781 public void save (Resume r ) {
6882 sqlHelper .transactionalExecute (conn -> {
69- try (PreparedStatement ps = conn .prepareStatement ("INSERT INTO resume (uuid, full_name) VALUES (?,?)" )) {
70- ps .setString (1 , r .getUuid ());
71- ps .setString (2 , r .getFullName ());
72- ps .execute ();
73- }
74- insertContact (conn , r );
75- return null ;
76- });
83+ try (PreparedStatement ps = conn .prepareStatement ("INSERT INTO resume (uuid, full_name) VALUES (?,?)" )) {
84+ ps .setString (1 , r .getUuid ());
85+ ps .setString (2 , r .getFullName ());
86+ ps .execute ();
87+ }
88+ insertContacts (conn , r );
89+ insertSections (conn , r );
90+ return null ;
91+ }
92+ );
7793 }
7894
7995 @ Override
@@ -89,22 +105,34 @@ public void delete(String uuid) {
89105
90106 @ Override
91107 public List <Resume > getAllSorted () {
92- return sqlHelper .execute ("" +
93- " SELECT * FROM resume r\n " +
94- "LEFT JOIN contact c ON r.uuid = c.resume_uuid\n " +
95- "ORDER BY full_name, uuid" , ps -> {
96- ResultSet rs = ps .executeQuery ();
97- Map <String , Resume > map = new LinkedHashMap <>();
98- while (rs .next ()) {
99- String uuid = rs .getString ("uuid" );
100- Resume resume = map .get (uuid );
101- if (resume == null ) {
102- resume = new Resume (uuid , rs .getString ("full_name" ));
103- map .put (uuid , resume );
108+ return sqlHelper .transactionalExecute (conn -> {
109+ Map <String , Resume > resumes = new LinkedHashMap <>();
110+
111+ try (PreparedStatement ps = conn .prepareStatement ("SELECT * FROM resume ORDER BY full_name, uuid" )) {
112+ ResultSet rs = ps .executeQuery ();
113+ while (rs .next ()) {
114+ String uuid = rs .getString ("uuid" );
115+ resumes .put (uuid , new Resume (uuid , rs .getString ("full_name" )));
116+ }
117+ }
118+
119+ try (PreparedStatement ps = conn .prepareStatement ("SELECT * FROM contact" )) {
120+ ResultSet rs = ps .executeQuery ();
121+ while (rs .next ()) {
122+ Resume r = resumes .get (rs .getString ("resume_uuid" ));
123+ addContact (rs , r );
104124 }
105- addContact (rs , resume );
106125 }
107- return new ArrayList <>(map .values ());
126+
127+ try (PreparedStatement ps = conn .prepareStatement ("SELECT * FROM section" )) {
128+ ResultSet rs = ps .executeQuery ();
129+ while (rs .next ()) {
130+ Resume r = resumes .get (rs .getString ("resume_uuid" ));
131+ addSection (rs , r );
132+ }
133+ }
134+
135+ return new ArrayList <>(resumes .values ());
108136 });
109137 }
110138
@@ -116,7 +144,7 @@ public int size() {
116144 });
117145 }
118146
119- private void insertContact (Connection conn , Resume r ) throws SQLException {
147+ private void insertContacts (Connection conn , Resume r ) throws SQLException {
120148 try (PreparedStatement ps = conn .prepareStatement ("INSERT INTO contact (resume_uuid, type, value) VALUES (?,?,?)" )) {
121149 for (Map .Entry <ContactType , String > e : r .getContacts ().entrySet ()) {
122150 ps .setString (1 , r .getUuid ());
@@ -128,12 +156,32 @@ private void insertContact(Connection conn, Resume r) throws SQLException {
128156 }
129157 }
130158
131- private void deleteContacts (Connection conn , Resume r ) {
132- sqlHelper .execute ("DELETE FROM contact WHERE resume_uuid=?" , ps -> {
159+ private void insertSections (Connection conn , Resume r ) throws SQLException {
160+ try (PreparedStatement ps = conn .prepareStatement ("INSERT INTO section (resume_uuid, type, content) VALUES (?,?,?)" )) {
161+ for (Map .Entry <SectionType , Section > e : r .getSections ().entrySet ()) {
162+ ps .setString (1 , r .getUuid ());
163+ ps .setString (2 , e .getKey ().name ());
164+ Section section = e .getValue ();
165+ ps .setString (3 , JsonParser .write (section , Section .class ));
166+ ps .addBatch ();
167+ }
168+ ps .executeBatch ();
169+ }
170+ }
171+
172+ private void deleteContacts (Connection conn , Resume r ) throws SQLException {
173+ deleteAttributes (conn , r , "DELETE FROM contact WHERE resume_uuid=?" );
174+ }
175+
176+ private void deleteSections (Connection conn , Resume r ) throws SQLException {
177+ deleteAttributes (conn , r , "DELETE FROM section WHERE resume_uuid=?" );
178+ }
179+
180+ private void deleteAttributes (Connection conn , Resume r , String sql ) throws SQLException {
181+ try (PreparedStatement ps = conn .prepareStatement (sql )) {
133182 ps .setString (1 , r .getUuid ());
134183 ps .execute ();
135- return null ;
136- });
184+ }
137185 }
138186
139187 private void addContact (ResultSet rs , Resume r ) throws SQLException {
@@ -142,4 +190,12 @@ private void addContact(ResultSet rs, Resume r) throws SQLException {
142190 r .addContact (ContactType .valueOf (rs .getString ("type" )), value );
143191 }
144192 }
193+
194+ private void addSection (ResultSet rs , Resume r ) throws SQLException {
195+ String content = rs .getString ("content" );
196+ if (content != null ) {
197+ SectionType type = SectionType .valueOf (rs .getString ("type" ));
198+ r .addSection (type , JsonParser .read (content , Section .class ));
199+ }
200+ }
145201}
0 commit comments