11package com .urise .webapp .storage ;
22
3- import com .urise .webapp .exception .ExistStorageException ;
43import com .urise .webapp .exception .NotExistStorageException ;
54import com .urise .webapp .exception .StorageException ;
65import com .urise .webapp .model .Resume ;
@@ -20,93 +19,45 @@ public SqlStorage(String dbUrl, String dbUser, String dbPassword) {
2019
2120 @ Override
2221 public void clear () {
23- /* try (Connection conn = connectionFactory.getConnection();
24- PreparedStatement ps = conn.prepareStatement("DELETE FROM RESUME")
25- ) {
26- ps.execute();
27- } catch (SQLException e) {
28- e.printStackTrace();
29- }*/
30- transactionExecute (ps ->ps .execute (),"DELETE FROM RESUME" );
22+ transactionExecute (ps -> ps .execute (), "DELETE FROM RESUME" );
3123 }
3224
3325 @ Override
3426 public void save (Resume resume ) {
35- /* try (Connection conn = connectionFactory.getConnection();
36- PreparedStatement ps = conn.prepareStatement("INSERT INTO RESUME (uuid, full_name) values (?,?)")
37- ) {
38- ps.setString(1, resume.getUuid());
39- ps.setString(2, resume.getFullName());
40- ps.execute();
41- } catch (SQLException e) {
42- throw new StorageException(e);
43- }
44- */
45- transactionExecute (ps ->{
27+ transactionExecute (ps -> {
4628 ps .setString (1 , resume .getUuid ());
4729 ps .setString (2 , resume .getFullName ());
4830 ps .execute ();
4931 return null ;
50- },"INSERT INTO RESUME (uuid, full_name) values (?,?)" );
32+ }, "INSERT INTO RESUME (uuid, full_name) values (?,?)" );
5133 }
5234
5335 @ Override
5436 public Resume get (String uuid ) {
55- /* try (Connection conn = connectionFactory.getConnection();
56- PreparedStatement ps = conn.prepareStatement("SELECT * FROM RESUME where uuid=?")
57- ) {
58- ps.setString(1, uuid);
59- ResultSet rs = ps.executeQuery();
60- if (rs.next()) {
61- throw new NotExistStorageException(uuid);
62- }
63- Resume r = new Resume(uuid, rs.getString("full_name"));
64- return r;
65- } catch (SQLException e) {
66- throw new StorageException(e);
67- }*/
68- return transactionExecute (ps ->{
37+ return transactionExecute (ps -> {
6938 ps .setString (1 , uuid );
7039 ResultSet rs = ps .executeQuery ();
7140 if (!rs .next ()) {
7241 throw new NotExistStorageException (uuid );
7342 }
74- return new Resume (uuid , rs .getString ( "full_name" ));
75- },"SELECT * FROM RESUME where uuid=?" );
43+ return new Resume (uuid , rs .getString ("full_name" ));
44+ }, "SELECT * FROM RESUME where uuid=?" );
7645
7746 }
7847
7948 @ Override
8049 public void delete (String uuid ) {
81- /* try (Connection conn=connectionFactory.getConnection();
82- PreparedStatement ps=conn.prepareStatement("DELETE from RESUME where UUID=?")
83- ){
84- ps.setString(1,uuid);
85- ps.execute();
86- } catch (SQLException e) {
87- throw new StorageException(e);
88- }*/
8950 transactionExecute (p -> {
9051 p .setString (1 , uuid );
91- if (p .executeUpdate ()==0 ){throw new NotExistStorageException (uuid );}
52+ if (p .executeUpdate () == 0 ) {
53+ throw new NotExistStorageException (uuid );
54+ }
9255 return null ;
9356 }, "DELETE from RESUME where UUID=?" );
9457 }
9558
9659 @ Override
9760 public List <Resume > getAllSorted () {
98- /* try (Connection conn=connectionFactory.getConnection();
99- PreparedStatement ps=conn.prepareStatement("SELECT * FROM RESUME ORDER BY full_name")
100- ){
101- ResultSet rs=ps.executeQuery();
102- List<Resume> list = new ArrayList<>();
103- while (rs.next()){
104- list.add(new Resume(rs.getString("uuid"), rs.getString("full_name")));
105- }
106- return list;
107- } catch (SQLException e) {
108- throw new StorageException(e);
109- }*/
11061 return transactionExecute (p -> {
11162 ResultSet rs = p .executeQuery ();
11263 List <Resume > list = new ArrayList <>();
@@ -120,32 +71,21 @@ public List<Resume> getAllSorted() {
12071
12172 @ Override
12273 public int size () {
123- /* try (Connection conn=connectionFactory.getConnection();
124- PreparedStatement ps=conn.prepareStatement("SELECT count (uuid) FROM RESUME")
125- ){
126- ResultSet rs=ps.executeQuery();
74+ return transactionExecute (ps -> {
75+ ResultSet rs = ps .executeQuery ();
76+ rs .next ();
12777 return rs .getInt (1 );
128- } catch (SQLException e) {
129- throw new StorageException(e);
130- }*/
131- return transactionExecute (ps -> {ResultSet rs = ps .executeQuery ();rs .next (); return rs .getInt (1 );}, "SELECT count (uuid) FROM RESUME" );
78+ }, "SELECT count (uuid) FROM RESUME" );
13279 }
13380
13481 @ Override
13582 public void update (Resume resume ) {
136- /* try (Connection conn=connectionFactory.getConnection();
137- PreparedStatement ps=conn.prepareStatement("UPDATE RESUME set full_name=? where uuid=?")
138- ){ ps.setString(1,resume.getFullName());
139- ps.setString(2,resume.getUuid());
140- ps.execute();
141- } catch (SQLException e) {
142- throw new StorageException(e);
143- }*/
144-
14583 transactionExecute (ps -> {
14684 ps .setString (1 , resume .getFullName ());
14785 ps .setString (2 , resume .getUuid ());
148- if (ps .executeUpdate ()==0 ) {throw new NotExistStorageException (resume .getUuid ());}
86+ if (ps .executeUpdate () == 0 ) {
87+ throw new NotExistStorageException (resume .getUuid ());
88+ }
14989 return null ;
15090 }, "UPDATE RESUME set full_name=? where uuid=?" );
15191 }
@@ -159,4 +99,8 @@ private <T> T transactionExecute(ABlockOfCode<T> aBlockOfCode, String sqlQery) {
15999 throw new StorageException (e );
160100 }
161101 }
102+
103+ public interface ABlockOfCode <T > {
104+ T execute (PreparedStatement ps ) throws SQLException ;
105+ }
162106}
0 commit comments