From d96c9bff9bb3fe0c760da0ca01e92a957be712f0 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Sat, 21 Oct 2023 21:39:27 +0300 Subject: [PATCH] Add a repository for indicators. --- .../db/repositories/AbstractRepository.java | 28 ++++ .../db/repositories/IndicatorRepository.java | 135 ++++++++++++++++++ 2 files changed, 163 insertions(+) create mode 100644 src/main/java/space/kklochko/jdbc_hospital_example/db/repositories/AbstractRepository.java create mode 100644 src/main/java/space/kklochko/jdbc_hospital_example/db/repositories/IndicatorRepository.java diff --git a/src/main/java/space/kklochko/jdbc_hospital_example/db/repositories/AbstractRepository.java b/src/main/java/space/kklochko/jdbc_hospital_example/db/repositories/AbstractRepository.java new file mode 100644 index 0000000..c4dda4a --- /dev/null +++ b/src/main/java/space/kklochko/jdbc_hospital_example/db/repositories/AbstractRepository.java @@ -0,0 +1,28 @@ +package space.kklochko.jdbc_hospital_example.db.repositories; + +import lombok.Setter; +import space.kklochko.jdbc_hospital_example.db.factories.DataBaseConnection; + +import java.util.ArrayList; +import java.util.UUID; + +@Setter +public abstract class AbstractRepository { + DataBaseConnection db; + + public AbstractRepository(DataBaseConnection db){ + setDb(db); + } + + public abstract boolean createTable(); + + public abstract boolean create(T object); + + public abstract T read(UUID id); + + public abstract ArrayList readAll(); + + public abstract boolean update(T object); + + public abstract boolean delete(UUID id); +} 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 new file mode 100644 index 0000000..525083c --- /dev/null +++ b/src/main/java/space/kklochko/jdbc_hospital_example/db/repositories/IndicatorRepository.java @@ -0,0 +1,135 @@ +package space.kklochko.jdbc_hospital_example.db.repositories; + +import jdk.internal.foreign.ArenaAllocator; +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.Indicator; + +import java.sql.*; +import java.util.ArrayList; +import java.util.UUID; + +public class IndicatorRepository extends AbstractRepository { + private final String createTableStatement = "create table if not exists indicators (" + + "id uuid default uuid_generate_v1()," + + "type varchar(100) NOT NULL," + + "values varchar(100) NOT NULL," + + "timestamp timestamp NOT NULL)"; + + public IndicatorRepository(DataBaseConnection db){ + super(db); + } + + public boolean createTable() { + CreateTable createTable = new CreateTable(); + return createTable.create(db, createTableStatement); + } + + public boolean create(Indicator object) { + try (Connection connection = db.connect()) { + connection.setAutoCommit(false); + + String sql = "insert into indicators (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()); + + 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 Indicator read(UUID id) { + try (Connection connection = db.connect()) { + String sql = "select id, type, values, timestamp from indicators 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 type = resultSet.getString("type"); + String values = resultSet.getString("values"); + Timestamp timestamp = resultSet.getTimestamp("timestamp"); + + return new Indicator(uuid, type, values, timestamp); + } + + return null; + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public ArrayList readAll() { + try (Connection connection = db.connect()) { + String sql = "select id, type, values, timestamp from indicators;"; + Statement statement = connection.createStatement(); + ResultSet resultSet = statement.executeQuery(sql); + + ArrayList indicators = new ArrayList<>(); + + while (resultSet.next()) { + UUID uuid = (UUID) resultSet.getObject("id"); + String type = resultSet.getString("type"); + String values = resultSet.getString("values"); + Timestamp timestamp = resultSet.getTimestamp("timestamp"); + + indicators.add(new Indicator(uuid, type, values, timestamp)); + } + + return indicators; + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public boolean update(Indicator object) { + try (Connection connection = db.connect()) { + connection.setAutoCommit(false); + + String sql = "update indicators set type = ?, values = ?, timestamp = ? where id = ?;"; + PreparedStatement preparedStatement = connection.prepareStatement(sql); + + preparedStatement.setString(1, object.getType()); + preparedStatement.setString(2, object.getValues()); + preparedStatement.setTimestamp(3, object.getTimestamp()); + 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 indicators 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); + } + } +}