|
| 1 | +package com.clevergang.dbtests.repository.impl.jdbi; |
| 2 | + |
| 3 | +import com.clevergang.dbtests.repository.api.DataRepository; |
| 4 | +import com.clevergang.dbtests.repository.api.data.*; |
| 5 | +import org.skife.jdbi.v2.DBI; |
| 6 | +import org.skife.jdbi.v2.Handle; |
| 7 | +import org.skife.jdbi.v2.PreparedBatch; |
| 8 | +import org.skife.jdbi.v2.util.IntegerColumnMapper; |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; |
| 10 | +import org.springframework.stereotype.Repository; |
| 11 | +import org.springframework.util.Assert; |
| 12 | + |
| 13 | +import java.math.BigDecimal; |
| 14 | +import java.sql.ResultSet; |
| 15 | +import java.sql.SQLException; |
| 16 | +import java.sql.Statement; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +/** |
| 20 | + * JDBI implementation of the DataRepository interface |
| 21 | + * |
| 22 | + * @author Bretislav Wajtr |
| 23 | + */ |
| 24 | +@Repository |
| 25 | +public class JDBIDataRepositoryImpl implements DataRepository { |
| 26 | + |
| 27 | + private DBI dbi; |
| 28 | + |
| 29 | + @Autowired |
| 30 | + public JDBIDataRepositoryImpl(DBI dbi) { |
| 31 | + this.dbi = dbi; |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public Company findCompany(Integer pid) { |
| 36 | + try (Handle h = dbi.open()) { |
| 37 | + return h.createQuery("SELECT * FROM company WHERE pid = :pid") |
| 38 | + .bind("pid", pid) |
| 39 | + .map(Company.class) |
| 40 | + .first(); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public Company findCompanyUsingSimpleStaticStatement(Integer pid) { |
| 46 | + // I didn't find a way how to setup JDBI so the query is executed statically (using JDBC regular Statement |
| 47 | + // not PreparedStatement). However, JDBI is polite and provides way how to access the underlying JDBC connection |
| 48 | + // -> therefore we can actually fall back to JDBC way of doing things (which is not such a big deal since we need simple |
| 49 | + // Statements only rarely): |
| 50 | + try (Handle h = dbi.open()) { |
| 51 | + String query = "SELECT pid, address, name " + |
| 52 | + "FROM company " + |
| 53 | + "WHERE pid = " + pid; |
| 54 | + |
| 55 | + Statement statement = h.getConnection().createStatement(); |
| 56 | + ResultSet resultSet = statement.executeQuery(query); |
| 57 | + Company result = null; |
| 58 | + if (resultSet.next()) { |
| 59 | + result = new Company(); |
| 60 | + result.setPid(resultSet.getInt("pid")); |
| 61 | + result.setAddress(resultSet.getString("address")); |
| 62 | + result.setName(resultSet.getString("name")); |
| 63 | + |
| 64 | + } |
| 65 | + return result; |
| 66 | + } catch (SQLException e) { |
| 67 | + // just wrap checked exceptions as e |
| 68 | + throw new RuntimeException(e); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public Department findDepartment(Integer pid) { |
| 74 | + try (Handle h = dbi.open()) { |
| 75 | + return h.createQuery("SELECT pid, name, company_pid companyPid FROM department WHERE pid = :pid") |
| 76 | + .bind("pid", pid) |
| 77 | + .map(Department.class) |
| 78 | + .first(); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public List<Department> findDepartmentsOfCompany(Company company) { |
| 84 | + Assert.notNull(company); |
| 85 | + Assert.notNull(company.getPid()); |
| 86 | + |
| 87 | + try (Handle h = dbi.open()) { |
| 88 | + return h.createQuery("SELECT pid, name, company_pid companyPid " + |
| 89 | + "FROM department " + |
| 90 | + "WHERE company_pid = :company_pid " + |
| 91 | + "ORDER BY pid ") |
| 92 | + .bind("company_pid", company.getPid()) |
| 93 | + .map(Department.class) |
| 94 | + .list(); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void deleteDepartments(List<Department> departmentsToDelete) { |
| 100 | + try (Handle h = dbi.open()) { |
| 101 | + PreparedBatch preparedBatch = h.prepareBatch("DELETE FROM department WHERE pid = :pid"); |
| 102 | + |
| 103 | + for (Department department : departmentsToDelete) { |
| 104 | + preparedBatch.bind("pid", department.getPid()).add(); |
| 105 | + } |
| 106 | + |
| 107 | + preparedBatch.execute(); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void updateDepartments(List<Department> departmentsToUpdate) { |
| 113 | + try (Handle h = dbi.open()) { |
| 114 | + PreparedBatch preparedBatch = h.prepareBatch("UPDATE department SET company_pid = :company_pid, name = :name WHERE pid = :pid"); |
| 115 | + for (Department department : departmentsToUpdate) { |
| 116 | + preparedBatch |
| 117 | + .bind("company_pid", department.getCompanyPid()) |
| 118 | + .bind("name", department.getName()) |
| 119 | + .bind("pid", department.getPid()) |
| 120 | + .add(); |
| 121 | + } |
| 122 | + preparedBatch.execute(); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public void insertDepartments(List<Department> departmentsToInsert) { |
| 128 | + try (Handle h = dbi.open()) { |
| 129 | + PreparedBatch preparedBatch = h.prepareBatch("INSERT INTO department (company_pid, name) VALUES (:company_pid, :name)"); |
| 130 | + for (Department department : departmentsToInsert) { |
| 131 | + preparedBatch |
| 132 | + .bind("company_pid", department.getCompanyPid()) |
| 133 | + .bind("name", department.getName()) |
| 134 | + .add(); |
| 135 | + } |
| 136 | + preparedBatch.execute(); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public Project findProject(Integer pid) { |
| 142 | + try (Handle h = dbi.open()) { |
| 143 | + return h.createQuery("SELECT pid, name, datestarted FROM project WHERE pid = :pid") |
| 144 | + .bind("pid", pid) |
| 145 | + .map((index, r, ctx) -> { |
| 146 | + Project project = new Project(); |
| 147 | + project.setPid(r.getInt("pid")); |
| 148 | + project.setName(r.getString("name")); |
| 149 | + project.setDate(r.getDate("datestarted").toLocalDate()); |
| 150 | + return project; |
| 151 | + }) |
| 152 | + .first(); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public Integer insertProject(Project project) { |
| 158 | + try (Handle h = dbi.open()) { |
| 159 | + return h.createStatement("INSERT INTO project (name, datestarted) VALUES (:name, :datestarted)") |
| 160 | + .bind("name", project.getName()) |
| 161 | + .bind("datestarted", project.getDate()) |
| 162 | + .executeAndReturnGeneratedKeys(IntegerColumnMapper.WRAPPER) |
| 163 | + .first(); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public List<Integer> insertProjects(List<Project> projects) { |
| 169 | + try (Handle h = dbi.open()) { |
| 170 | + PreparedBatch preparedBatch = h.prepareBatch("INSERT INTO project (name, datestarted) VALUES (:name, :datestarted)"); |
| 171 | + for (Project project : projects) { |
| 172 | + preparedBatch |
| 173 | + .bind("name", project.getName()) |
| 174 | + .bind("datestarted", project.getDate()) |
| 175 | + .add(); |
| 176 | + } |
| 177 | + return preparedBatch.executeAndGenerateKeys(IntegerColumnMapper.WRAPPER).list(); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + @Override |
| 182 | + public List<ProjectsWithCostsGreaterThanOutput> getProjectsWithCostsGreaterThan(int totalCostBoundary) { |
| 183 | + try (Handle h = dbi.open()) { |
| 184 | + String query; |
| 185 | + query = "WITH project_info AS (\n" + |
| 186 | + " SELECT project.pid project_pid, project.name project_name, salary monthly_cost, company.name company_name\n" + |
| 187 | + " FROM project\n" + |
| 188 | + " JOIN projectemployee ON project.pid = projectemployee.project_pid\n" + |
| 189 | + " JOIN employee ON projectemployee.employee_pid = employee.pid\n" + |
| 190 | + " LEFT JOIN department ON employee.department_pid = department.pid\n" + |
| 191 | + " LEFT JOIN company ON department.company_pid = company.pid\n" + |
| 192 | + "),\n" + |
| 193 | + "project_cost AS (\n" + |
| 194 | + " SELECT project_pid, sum(monthly_cost) total_cost\n" + |
| 195 | + " FROM project_info GROUP BY project_pid\n" + |
| 196 | + ")\n" + |
| 197 | + "SELECT project_name projectName, total_cost totalCost, company_name companyName, sum(monthly_cost) companyCost FROM project_info\n" + |
| 198 | + " JOIN project_cost USING (project_pid)\n" + |
| 199 | + "WHERE total_cost > :totalCostBoundary\n" + |
| 200 | + "GROUP BY project_name, total_cost, company_name\n" + |
| 201 | + "ORDER BY company_name"; |
| 202 | + |
| 203 | + return h.createQuery(query) |
| 204 | + .bind("totalCostBoundary", totalCostBoundary) |
| 205 | + .map(ProjectsWithCostsGreaterThanOutput.class) |
| 206 | + .list(); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + @Override |
| 211 | + public Employee findEmployee(Integer pid) { |
| 212 | + try (Handle h = dbi.open()) { |
| 213 | + return h.createQuery("SELECT pid, name, surname, email, department_pid departmentPid, salary FROM employee WHERE pid = :pid") |
| 214 | + .bind("pid", pid) |
| 215 | + .map(Employee.class) |
| 216 | + .first(); |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + @Override |
| 221 | + public List<Employee> employeesWithSalaryGreaterThan(Integer minSalary) { |
| 222 | + try (Handle h = dbi.open()) { |
| 223 | + return h.createQuery("SELECT * FROM employee WHERE salary > :salary") |
| 224 | + .bind("salary", minSalary) |
| 225 | + .map(Employee.class) |
| 226 | + .list(); |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + @Override |
| 231 | + public void updateEmployee(Employee employeeToUpdate) { |
| 232 | + Assert.notNull(employeeToUpdate); |
| 233 | + Assert.notNull(employeeToUpdate.getPid()); |
| 234 | + |
| 235 | + try (Handle h = dbi.open()) { |
| 236 | + h.createStatement(" UPDATE EMPLOYEE SET " + |
| 237 | + " department_pid = :department_pid, " + |
| 238 | + " name = :name," + |
| 239 | + " surname = :surname," + |
| 240 | + " email = :email," + |
| 241 | + " salary = :salary" + |
| 242 | + " WHERE pid = :pid") |
| 243 | + .bind("department_pid", employeeToUpdate.getDepartmentPid()) |
| 244 | + .bind("name", employeeToUpdate.getName()) |
| 245 | + .bind("surname", employeeToUpdate.getSurname()) |
| 246 | + .bind("email", employeeToUpdate.getEmail()) |
| 247 | + .bind("salary", employeeToUpdate.getSalary()) |
| 248 | + .bind("pid", employeeToUpdate.getPid()) |
| 249 | + .execute(); |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + @Override |
| 254 | + public RegisterEmployeeOutput callRegisterEmployee(String name, String surname, String email, BigDecimal salary, String departmentName, String companyName) { |
| 255 | + try (Handle h = dbi.open()) { |
| 256 | + return h.createQuery("SELECT employee_id employeePid, department_id departmentPid, company_id companyPid FROM register_employee(" + |
| 257 | + " :name, \n" + |
| 258 | + " :surname, \n" + |
| 259 | + " :email, \n" + |
| 260 | + " :salary, \n" + |
| 261 | + " :departmentName, \n" + |
| 262 | + " :companyName\n" + |
| 263 | + ")") |
| 264 | + .bind("name", name) |
| 265 | + .bind("surname", surname) |
| 266 | + .bind("email", email) |
| 267 | + .bind("salary", salary) |
| 268 | + .bind("departmentName", departmentName) |
| 269 | + .bind("companyName", companyName) |
| 270 | + .map(RegisterEmployeeOutput.class) |
| 271 | + .first(); |
| 272 | + } |
| 273 | + } |
| 274 | + |
| 275 | + @Override |
| 276 | + public Integer getProjectsCount() { |
| 277 | + try (Handle h = dbi.open()) { |
| 278 | + return h.createQuery("SELECT count(*) FROM project") |
| 279 | + .map(IntegerColumnMapper.WRAPPER) |
| 280 | + .first(); |
| 281 | + } |
| 282 | + |
| 283 | + } |
| 284 | +} |
0 commit comments