Add a repository for indicators.

main
KKlochko 2 years ago
parent fc8a227df8
commit d96c9bff9b

@ -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<T> {
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<T> readAll();
public abstract boolean update(T object);
public abstract boolean delete(UUID id);
}

@ -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<Indicator> {
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<Indicator> 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<Indicator> 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);
}
}
}
Loading…
Cancel
Save