11package com .urise .webapp .storage ;
22
3- import com .urise .webapp .exception .ExistStorageException ;
43import com .urise .webapp .exception .NotExistStorageException ;
54import com .urise .webapp .model .ContactType ;
65import com .urise .webapp .model .Resume ;
76import com .urise .webapp .sql .SqlHelper ;
87
8+ import java .sql .PreparedStatement ;
99import java .sql .ResultSet ;
10- import java .sql .SQLException ;
1110import java .util .ArrayList ;
1211import java .util .List ;
1312import java .util .Map ;
@@ -26,57 +25,52 @@ public void clear() {
2625
2726 @ Override
2827 public void save (Resume resume ) {
29- sqlHelper .transactionExecute (ps -> {
30- ps .setString (1 , resume .getUuid ());
31- ps .setString (2 , resume .getFullName ());
32- try {
33- ps .execute ();
34- } catch (SQLException e ) {
35- if (e .getSQLState ().equals ("23505" ))
36- throw new ExistStorageException (resume .getUuid ());
37- }
38- return null ;
39- }, "INSERT INTO contact (resume_uuid, type, value) values (?,?,?)" );
40- for (Map .Entry <ContactType ,String > entry :resume .getContacts ().entrySet ()) {
41- sqlHelper .transactionExecute (ps -> {
42- ps .setString (1 , resume .getUuid ());
43- ps .setString (2 , entry .getKey ().name ());
44- ps .setString (3 , entry .getValue ());
45- try {
46- ps .execute ();
47- } catch (SQLException e ) {
48- if (e .getSQLState ().equals ("23505" ))
49- throw new ExistStorageException (resume .getUuid ());
50- }
28+ sqlHelper .transactionalExecute (conn -> {
29+ try (PreparedStatement ps = conn .prepareStatement ("INSERT INTO RESUME (uuid, full_name) values (?,?)" )) {
30+ ps .setString (1 , resume .getUuid ());
31+ ps .setString (2 , resume .getFullName ());
32+ ps .execute ();
33+ }
5134
52- return null ;
53- }, "INSERT INTO RESUME (uuid, full_name) values (?,?)" );
54- }
35+ try (PreparedStatement ps = conn .prepareStatement ("INSERT INTO contact (resume_uuid, type, value) values (?,?,?)" )) {
36+ for (Map .Entry <ContactType , String > entry : resume .getContacts ().entrySet ()) {
37+ ps .setString (1 , resume .getUuid ());
38+ ps .setString (2 , entry .getKey ().name ());
39+ ps .setString (3 , entry .getValue ());
40+ ps .addBatch ();
41+ }
42+ ps .executeBatch ();
43+ }
44+ return null ;
45+ }
46+ );
5547 }
5648
49+
5750 @ Override
5851 public Resume get (String uuid ) {
5952 return sqlHelper .transactionExecute (ps -> {
60- ps .setString (1 , uuid );
61- ResultSet rs = ps .executeQuery ();
62- if (!rs .next ()) {
63- throw new NotExistStorageException (uuid );
64- }
65- Resume r = new Resume (uuid , rs .getString ("full_name" ));
66- do {
67- String value =rs .getString ("value" );
68- ContactType type = ContactType .valueOf (rs .getString ("type" ));
69- r .addContact (type ,value );
70- } while (rs .next ());
71- return r ;
72- },
73- "SELECT * FROM RESUME r " +
74- "LEFT JOIN CONTACT c on (r.uuid=c.resume_uuid) " +
75- "WHERE r.uuid=?" );
53+ ps .setString (1 , uuid );
54+ ResultSet rs = ps .executeQuery ();
55+ if (!rs .next ()) {
56+ throw new NotExistStorageException (uuid );
57+ }
58+ Resume r = new Resume (uuid , rs .getString ("full_name" ));
59+ do {
60+ String value = rs .getString ("value" );
61+ ContactType type = ContactType .valueOf (rs .getString ("type" ));
62+ r .addContact (type , value );
63+ } while (rs .next ());
64+ return r ;
65+ },
66+ "SELECT * FROM RESUME r " +
67+ "LEFT JOIN CONTACT c on (r.uuid=c.resume_uuid) " +
68+ "WHERE r.uuid=?" );
7669 }
7770
7871 @ Override
7972 public void delete (String uuid ) {
73+ /*удаление по каскаду*/
8074 sqlHelper .transactionExecute (p -> {
8175 p .setString (1 , uuid );
8276 if (p .executeUpdate () == 0 ) {
@@ -103,7 +97,9 @@ public List<Resume> getAllSorted() {
10397 public int size () {
10498 return sqlHelper .transactionExecute (ps -> {
10599 ResultSet rs = ps .executeQuery ();
106- if (!rs .next ()) {return 0 ;}
100+ if (!rs .next ()) {
101+ return 0 ;
102+ }
107103 return rs .getInt (1 );
108104 }, "SELECT count (uuid) FROM RESUME" );
109105 }
0 commit comments