|
| 1 | +package com.urise.webapp.storage; |
| 2 | + |
| 3 | +import com.urise.webapp.exception.ExistStorageException; |
| 4 | +import com.urise.webapp.exception.NotExistStorageException; |
| 5 | +import com.urise.webapp.exception.StorageException; |
| 6 | +import com.urise.webapp.model.Resume; |
| 7 | +import com.urise.webapp.sql.ConnectionFactory; |
| 8 | + |
| 9 | +import java.sql.*; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | + |
| 13 | +public class SqlStorage implements Storage { |
| 14 | + |
| 15 | + public final ConnectionFactory connectionFactory; |
| 16 | + |
| 17 | + public SqlStorage(String dbUrl, String dbUser, String dbPassword) { |
| 18 | + connectionFactory = () -> DriverManager.getConnection(dbUrl, dbUser, dbPassword); |
| 19 | + } |
| 20 | + |
| 21 | + @Override |
| 22 | + 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"); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + 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->{ |
| 46 | + ps.setString(1, resume.getUuid()); |
| 47 | + ps.setString(2, resume.getFullName()); |
| 48 | + ps.execute(); |
| 49 | + return null; |
| 50 | + },"INSERT INTO RESUME (uuid, full_name) values (?,?)" ); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + 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->{ |
| 69 | + ps.setString(1, uuid); |
| 70 | + ResultSet rs = ps.executeQuery(); |
| 71 | + if (!rs.next()) { |
| 72 | + throw new NotExistStorageException(uuid); |
| 73 | + } |
| 74 | + return new Resume(uuid, rs.getString( "full_name")); |
| 75 | + },"SELECT * FROM RESUME where uuid=?"); |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + 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 | + }*/ |
| 89 | + transactionExecute(p -> { |
| 90 | + p.setString(1, uuid); |
| 91 | + if (p.executeUpdate()==0){throw new NotExistStorageException(uuid);} |
| 92 | + return null; |
| 93 | + }, "DELETE from RESUME where UUID=?"); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + 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 | + }*/ |
| 110 | + return transactionExecute(p -> { |
| 111 | + ResultSet rs = p.executeQuery(); |
| 112 | + List<Resume> list = new ArrayList<>(); |
| 113 | + while (rs.next()) { |
| 114 | + list.add(new Resume(rs.getString("uuid"), rs.getString("full_name"))); |
| 115 | + } |
| 116 | + return list; |
| 117 | + }, "SELECT * FROM RESUME ORDER BY full_name"); |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + 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(); |
| 127 | + 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"); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + 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 | + |
| 145 | + transactionExecute(ps -> { |
| 146 | + ps.setString(1, resume.getFullName()); |
| 147 | + ps.setString(2, resume.getUuid()); |
| 148 | + if (ps.executeUpdate()==0) {throw new NotExistStorageException(resume.getUuid());} |
| 149 | + return null; |
| 150 | + }, "UPDATE RESUME set full_name=? where uuid=?"); |
| 151 | + } |
| 152 | + |
| 153 | + private <T> T transactionExecute(ABlockOfCode<T> aBlockOfCode, String sqlQery) { |
| 154 | + try (Connection conn = connectionFactory.getConnection(); |
| 155 | + PreparedStatement ps = conn.prepareStatement(sqlQery) |
| 156 | + ) { |
| 157 | + return aBlockOfCode.execute(ps); |
| 158 | + } catch (SQLException e) { |
| 159 | + throw new StorageException(e); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments