From dbd92b41bc79267f5c2564e242f5893593c468aa Mon Sep 17 00:00:00 2001 From: KKlochko Date: Sat, 28 Oct 2023 21:59:31 +0300 Subject: [PATCH] Update the model repositories. --- .../db/repositories/DepartmentRepository.java | 18 ++- .../db/repositories/IndicatorRepository.java | 18 ++- .../db/repositories/PatientRepository.java | 146 ++++++++++++++++++ 3 files changed, 176 insertions(+), 6 deletions(-) create mode 100644 src/main/java/space/kklochko/jdbc_hospital_example/db/repositories/PatientRepository.java diff --git a/src/main/java/space/kklochko/jdbc_hospital_example/db/repositories/DepartmentRepository.java b/src/main/java/space/kklochko/jdbc_hospital_example/db/repositories/DepartmentRepository.java index a21a8a3..b7f8f30 100644 --- a/src/main/java/space/kklochko/jdbc_hospital_example/db/repositories/DepartmentRepository.java +++ b/src/main/java/space/kklochko/jdbc_hospital_example/db/repositories/DepartmentRepository.java @@ -31,11 +31,23 @@ public class DepartmentRepository extends AbstractRepository { connection.setAutoCommit(false); String sql = "insert into departments (name, location, phone) values (?, ?, ?) returning id;"; + + if(object.getId() != null) { + sql = "insert into departments (id, name, location, phone) values (?, ?, ?, ?) returning id;"; + } + PreparedStatement preparedStatement = connection.prepareStatement(sql); - preparedStatement.setString(1, object.getName()); - preparedStatement.setString(2, object.getLocation()); - preparedStatement.setString(3, object.getPhone()); + if(object.getId() == null) { + preparedStatement.setString(1, object.getName()); + preparedStatement.setString(2, object.getLocation()); + preparedStatement.setString(3, object.getPhone()); + } else { + preparedStatement.setObject(1, object.getId()); + preparedStatement.setString(2, object.getName()); + preparedStatement.setString(3, object.getLocation()); + preparedStatement.setString(4, object.getPhone()); + } ResultSet resultSet = (new TransactionalInsert()).run(connection, preparedStatement); diff --git a/src/main/java/space/kklochko/jdbc_hospital_example/db/repositories/IndicatorRepository.java b/src/main/java/space/kklochko/jdbc_hospital_example/db/repositories/IndicatorRepository.java index 525083c..fb927d7 100644 --- a/src/main/java/space/kklochko/jdbc_hospital_example/db/repositories/IndicatorRepository.java +++ b/src/main/java/space/kklochko/jdbc_hospital_example/db/repositories/IndicatorRepository.java @@ -32,11 +32,23 @@ public class IndicatorRepository extends AbstractRepository { connection.setAutoCommit(false); String sql = "insert into indicators (type, values, timestamp) values (?, ?, ?) returning id;"; + + if(object.getId() != null) { + sql = "insert into indicators (id, type, values, timestamp) values (?, ?, ?, ?) returning id;"; + } + PreparedStatement preparedStatement = connection.prepareStatement(sql); - preparedStatement.setString(1, object.getType()); - preparedStatement.setString(2, object.getValues()); - preparedStatement.setTimestamp(3, object.getTimestamp()); + if(object.getId() == null) { + preparedStatement.setString(1, object.getType()); + preparedStatement.setString(2, object.getValues()); + preparedStatement.setTimestamp(3, object.getTimestamp()); + }else { + preparedStatement.setObject(1, object.getId()); + preparedStatement.setString(2, object.getType()); + preparedStatement.setString(3, object.getValues()); + preparedStatement.setTimestamp(4, object.getTimestamp()); + } ResultSet resultSet = (new TransactionalInsert()).run(connection, preparedStatement); diff --git a/src/main/java/space/kklochko/jdbc_hospital_example/db/repositories/PatientRepository.java b/src/main/java/space/kklochko/jdbc_hospital_example/db/repositories/PatientRepository.java new file mode 100644 index 0000000..d6f2baa --- /dev/null +++ b/src/main/java/space/kklochko/jdbc_hospital_example/db/repositories/PatientRepository.java @@ -0,0 +1,146 @@ +package space.kklochko.jdbc_hospital_example.db.repositories; + +import space.kklochko.jdbc_hospital_example.db.factories.DataBaseConnection; +import space.kklochko.jdbc_hospital_example.db.repositories.blocks.CreateTable; +import space.kklochko.jdbc_hospital_example.db.repositories.blocks.TransactionalInsert; +import space.kklochko.jdbc_hospital_example.db.repositories.blocks.TransactionalUpdate; +import space.kklochko.jdbc_hospital_example.models.Patient; + +import java.sql.*; +import java.util.ArrayList; +import java.util.UUID; + +public class PatientRepository extends AbstractRepository { + private final String createTableStatement = "create table if not exists patients (" + + "id uuid default uuid_generate_v1()," + + "name varchar(100) NOT NULL," + + "age smallint NOT NULL," + + "phone varchar(25) NOT NULL)"; + + public PatientRepository(DataBaseConnection db){ + super(db); + } + + public boolean createTable() { + CreateTable createTable = new CreateTable(); + return createTable.create(db, createTableStatement); + } + + public boolean create(Patient object) { + try (Connection connection = db.connect()) { + connection.setAutoCommit(false); + + String sql = "insert into patients (name, age, phone) values (?, ?, ?) returning id;"; + + if(object.getId() != null) { + sql = "insert into patients (id, name, age, phone) values (?, ?, ?, ?) returning id;"; + } + + PreparedStatement preparedStatement = connection.prepareStatement(sql); + + if(object.getId() == null) { + preparedStatement.setString(1, object.getName()); + preparedStatement.setShort(2, object.getAge()); + preparedStatement.setString(3, object.getPhone()); + } else { + preparedStatement.setObject(1, object.getId()); + preparedStatement.setString(2, object.getName()); + preparedStatement.setShort(3, object.getAge()); + preparedStatement.setString(4, object.getPhone()); + } + + ResultSet resultSet = (new TransactionalInsert()).run(connection, preparedStatement); + + if (resultSet.next()) { + UUID uuid = (UUID) resultSet.getObject("id"); + object.setId(uuid); + return true; + } + + return false; + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public Patient read(UUID id) { + try (Connection connection = db.connect()) { + String sql = "select id, name, age, phone from patients where id = ?;"; + PreparedStatement preparedStatement = connection.prepareStatement(sql); + preparedStatement.setObject(1, id); + ResultSet resultSet = preparedStatement.executeQuery(); + + if (resultSet.next()) { + UUID uuid = (UUID) resultSet.getObject("id"); + String name = resultSet.getString("name"); + short age = resultSet.getShort("age"); + String phone = resultSet.getString("phone"); + + return new Patient(uuid, name, age, phone); + } + + return null; + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public ArrayList readAll() { + try (Connection connection = db.connect()) { + String sql = "select id, name, age, phone from patients;"; + Statement statement = connection.createStatement(); + ResultSet resultSet = statement.executeQuery(sql); + + ArrayList patients = new ArrayList<>(); + + while (resultSet.next()) { + UUID uuid = (UUID) resultSet.getObject("id"); + String name = resultSet.getString("name"); + short age = resultSet.getShort("age"); + String phone = resultSet.getString("phone"); + + patients.add(new Patient(uuid, name, age, phone)); + } + + return patients; + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public boolean update(Patient object) { + try (Connection connection = db.connect()) { + connection.setAutoCommit(false); + + String sql = "update patients set name = ?, age = ?, phone = ? where id = ?;"; + PreparedStatement preparedStatement = connection.prepareStatement(sql); + + preparedStatement.setString(1, object.getName()); + preparedStatement.setShort(2, object.getAge()); + preparedStatement.setString(3, object.getPhone()); + preparedStatement.setObject(4, object.getId()); + + int rowCount = (new TransactionalUpdate()).run(connection, preparedStatement); + + return rowCount > 0; + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public boolean delete(UUID id) { + try (Connection connection = db.connect()) { + connection.setAutoCommit(false); + + String sql = "delete from patients where id = ?;"; + PreparedStatement preparedStatement = connection.prepareStatement(sql); + preparedStatement.setObject(1, id); + + int rowCount = (new TransactionalUpdate()).run(connection, preparedStatement); + + return rowCount > 0; + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +}