parent
736aa2c530
commit
3f4896349a
@ -1,146 +0,0 @@
|
|||||||
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.TransactionalInsert;
|
|
||||||
import space.kklochko.jdbc_hospital_example.db.repositories.blocks.TransactionalUpdate;
|
|
||||||
import space.kklochko.jdbc_hospital_example.db.repositories.blocks.CreateTable;
|
|
||||||
import space.kklochko.jdbc_hospital_example.models.Department;
|
|
||||||
|
|
||||||
import java.sql.*;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
public class DepartmentRepository extends AbstractRepository<Department> {
|
|
||||||
private final String createTableStatement = "create table if not exists departments ("
|
|
||||||
+ "id uuid default uuid_generate_v1(),"
|
|
||||||
+ "name varchar(100) not null,"
|
|
||||||
+ "location varchar(100) not null,"
|
|
||||||
+ "phone varchar(25) not null)";
|
|
||||||
|
|
||||||
public DepartmentRepository(DataBaseConnection db){
|
|
||||||
super(db);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean createTable() {
|
|
||||||
CreateTable createTable = new CreateTable();
|
|
||||||
return createTable.create(db, createTableStatement);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean create(Department object) {
|
|
||||||
try (Connection connection = db.connect()) {
|
|
||||||
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);
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
if (resultSet.next()) {
|
|
||||||
UUID uuid = (UUID) resultSet.getObject("id");
|
|
||||||
object.setId(uuid);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Department read(UUID id) {
|
|
||||||
try (Connection connection = db.connect()) {
|
|
||||||
String sql = "select id, name, location, phone from departments 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");
|
|
||||||
String location = resultSet.getString("location");
|
|
||||||
String phone = resultSet.getString("phone");
|
|
||||||
|
|
||||||
return new Department(uuid, name, location, phone);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public ArrayList<Department> readAll() {
|
|
||||||
try (Connection connection = db.connect()) {
|
|
||||||
String sql = "select id, name, location, phone from departments;";
|
|
||||||
Statement statement = connection.createStatement();
|
|
||||||
ResultSet resultSet = statement.executeQuery(sql);
|
|
||||||
|
|
||||||
ArrayList<Department> patients = new ArrayList<>();
|
|
||||||
|
|
||||||
while (resultSet.next()) {
|
|
||||||
UUID uuid = (UUID) resultSet.getObject("id");
|
|
||||||
String name = resultSet.getString("name");
|
|
||||||
String location = resultSet.getString("location");
|
|
||||||
String phone = resultSet.getString("phone");
|
|
||||||
|
|
||||||
patients.add(new Department(uuid, name, location, phone));
|
|
||||||
}
|
|
||||||
|
|
||||||
return patients;
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean update(Department object) {
|
|
||||||
try (Connection connection = db.connect()) {
|
|
||||||
connection.setAutoCommit(false);
|
|
||||||
|
|
||||||
String sql = "update departments set name = ?, location = ?, phone = ? where id = ?;";
|
|
||||||
PreparedStatement preparedStatement = connection.prepareStatement(sql);
|
|
||||||
|
|
||||||
preparedStatement.setString(1, object.getName());
|
|
||||||
preparedStatement.setString(2, object.getLocation());
|
|
||||||
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 departments 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,146 +0,0 @@
|
|||||||
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.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;";
|
|
||||||
|
|
||||||
if(object.getId() != null) {
|
|
||||||
sql = "insert into indicators (id, type, values, timestamp) values (?, ?, ?, ?) returning id;";
|
|
||||||
}
|
|
||||||
|
|
||||||
PreparedStatement preparedStatement = connection.prepareStatement(sql);
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,146 +0,0 @@
|
|||||||
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<Patient> {
|
|
||||||
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<Patient> 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<Patient> 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
package space.kklochko.jdbc_hospital_example.db.repositories.blocks;
|
|
||||||
|
|
||||||
import space.kklochko.jdbc_hospital_example.db.factories.DataBaseConnection;
|
|
||||||
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.sql.Statement;
|
|
||||||
|
|
||||||
public class CreateTable {
|
|
||||||
public boolean create(DataBaseConnection db, String createTableStatement) {
|
|
||||||
try (Connection connection = db.connect()) {
|
|
||||||
Statement statement = connection.createStatement();
|
|
||||||
statement.execute(createTableStatement);
|
|
||||||
return true;
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,23 +0,0 @@
|
|||||||
package space.kklochko.jdbc_hospital_example.db.repositories.blocks;
|
|
||||||
|
|
||||||
import space.kklochko.jdbc_hospital_example.db.factories.DataBaseConnection;
|
|
||||||
import space.kklochko.jdbc_hospital_example.db.repositories.AbstractRepository;
|
|
||||||
import space.kklochko.jdbc_hospital_example.db.repositories.DepartmentRepository;
|
|
||||||
import space.kklochko.jdbc_hospital_example.db.repositories.IndicatorRepository;
|
|
||||||
import space.kklochko.jdbc_hospital_example.db.repositories.PatientRepository;
|
|
||||||
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.sql.Statement;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
public class CreateTables {
|
|
||||||
public boolean create(DataBaseConnection dbc) {
|
|
||||||
ArrayList<AbstractRepository> tables = new ArrayList<>();
|
|
||||||
tables.add(new IndicatorRepository(dbc));
|
|
||||||
tables.add(new PatientRepository(dbc));
|
|
||||||
tables.add(new DepartmentRepository(dbc));
|
|
||||||
tables.forEach(repo -> repo.createTable());
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
package space.kklochko.jdbc_hospital_example.db.repositories.blocks;
|
|
||||||
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.PreparedStatement;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
|
|
||||||
public class TransactionalInsert {
|
|
||||||
public ResultSet run(Connection connection, PreparedStatement insertStatement) throws SQLException {
|
|
||||||
try {
|
|
||||||
ResultSet result = insertStatement.executeQuery();
|
|
||||||
connection.commit();
|
|
||||||
|
|
||||||
return result;
|
|
||||||
} catch (SQLException e) {
|
|
||||||
connection.rollback();
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
package space.kklochko.jdbc_hospital_example.db.repositories.blocks;
|
|
||||||
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.PreparedStatement;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
|
|
||||||
public class TransactionalUpdate {
|
|
||||||
public int run(Connection connection, PreparedStatement updateStatement) throws SQLException {
|
|
||||||
try {
|
|
||||||
int rowCount = updateStatement.executeUpdate();
|
|
||||||
connection.commit();
|
|
||||||
|
|
||||||
return rowCount;
|
|
||||||
} catch (SQLException e) {
|
|
||||||
connection.rollback();
|
|
||||||
}
|
|
||||||
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
package space.kklochko.jdbc_hospital_example.db.repositories.blocks;
|
|
||||||
|
|
||||||
import space.kklochko.jdbc_hospital_example.db.factories.DataBaseConnection;
|
|
||||||
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.sql.Statement;
|
|
||||||
|
|
||||||
public class TurnOnUUIDPlugin {
|
|
||||||
public boolean create(DataBaseConnection db) {
|
|
||||||
try (Connection connection = db.connect()) {
|
|
||||||
Statement statement = connection.createStatement();
|
|
||||||
statement.execute("CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";");
|
|
||||||
return true;
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,47 @@
|
|||||||
|
package space.kklochko.jpa_hospital_example.db.repositories;
|
||||||
|
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
|
import jakarta.persistence.TypedQuery;
|
||||||
|
import space.kklochko.jpa_hospital_example.db.repositories.blocks.TransactionalInsert;
|
||||||
|
import space.kklochko.jpa_hospital_example.db.repositories.blocks.TransactionalRemove;
|
||||||
|
import space.kklochko.jpa_hospital_example.db.repositories.blocks.TransactionalUpdate;
|
||||||
|
import space.kklochko.jpa_hospital_example.models.Department;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class DepartmentRepository extends AbstractRepository<Department> {
|
||||||
|
public DepartmentRepository(EntityManager manager){
|
||||||
|
super(manager);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean create(Department object) {
|
||||||
|
TransactionalInsert insert = new TransactionalInsert();
|
||||||
|
|
||||||
|
return insert.run(manager, object);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Department read(UUID id) {
|
||||||
|
return manager.find(Department.class, id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList<Department> readAll() {
|
||||||
|
TypedQuery<Department> query = manager.createQuery("SELECT d FROM Department d", Department.class);
|
||||||
|
|
||||||
|
return (ArrayList<Department>) query.getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean update(Department object) {
|
||||||
|
TransactionalUpdate update = new TransactionalUpdate();
|
||||||
|
|
||||||
|
return update.run(manager, object);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean delete(UUID id) {
|
||||||
|
Department object = read(id);
|
||||||
|
|
||||||
|
TransactionalRemove remove = new TransactionalRemove();
|
||||||
|
|
||||||
|
return remove.run(manager, object);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,47 @@
|
|||||||
|
package space.kklochko.jpa_hospital_example.db.repositories;
|
||||||
|
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
|
import jakarta.persistence.TypedQuery;
|
||||||
|
import space.kklochko.jpa_hospital_example.db.repositories.blocks.TransactionalInsert;
|
||||||
|
import space.kklochko.jpa_hospital_example.db.repositories.blocks.TransactionalRemove;
|
||||||
|
import space.kklochko.jpa_hospital_example.db.repositories.blocks.TransactionalUpdate;
|
||||||
|
import space.kklochko.jpa_hospital_example.models.Indicator;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class IndicatorRepository extends AbstractRepository<Indicator> {
|
||||||
|
public IndicatorRepository(EntityManager manager){
|
||||||
|
super(manager);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean create(Indicator object) {
|
||||||
|
TransactionalInsert insert = new TransactionalInsert();
|
||||||
|
|
||||||
|
return insert.run(manager, object);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Indicator read(UUID id) {
|
||||||
|
return manager.find(Indicator.class, id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList<Indicator> readAll() {
|
||||||
|
TypedQuery<Indicator> query = manager.createQuery("SELECT i FROM Indicator i", Indicator.class);
|
||||||
|
|
||||||
|
return (ArrayList<Indicator>) query.getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean update(Indicator object) {
|
||||||
|
TransactionalUpdate update = new TransactionalUpdate();
|
||||||
|
|
||||||
|
return update.run(manager, object);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean delete(UUID id) {
|
||||||
|
Indicator object = read(id);
|
||||||
|
|
||||||
|
TransactionalRemove remove = new TransactionalRemove();
|
||||||
|
|
||||||
|
return remove.run(manager, object);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,47 @@
|
|||||||
|
package space.kklochko.jpa_hospital_example.db.repositories;
|
||||||
|
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
|
import jakarta.persistence.TypedQuery;
|
||||||
|
import space.kklochko.jpa_hospital_example.db.repositories.blocks.TransactionalInsert;
|
||||||
|
import space.kklochko.jpa_hospital_example.db.repositories.blocks.TransactionalRemove;
|
||||||
|
import space.kklochko.jpa_hospital_example.db.repositories.blocks.TransactionalUpdate;
|
||||||
|
import space.kklochko.jpa_hospital_example.models.Patient;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class PatientRepository extends AbstractRepository<Patient> {
|
||||||
|
public PatientRepository(EntityManager manager) {
|
||||||
|
super(manager);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean create(Patient object) {
|
||||||
|
TransactionalInsert insert = new TransactionalInsert();
|
||||||
|
|
||||||
|
return insert.run(manager, object);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Patient read(UUID id) {
|
||||||
|
return manager.find(Patient.class, id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList<Patient> readAll() {
|
||||||
|
TypedQuery<Patient> query = manager.createQuery("SELECT p FROM Patient p", Patient.class);
|
||||||
|
|
||||||
|
return (ArrayList<Patient>) query.getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean update(Patient object) {
|
||||||
|
TransactionalUpdate update = new TransactionalUpdate();
|
||||||
|
|
||||||
|
return update.run(manager, object);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean delete(UUID id) {
|
||||||
|
Patient object = read(id);
|
||||||
|
|
||||||
|
TransactionalRemove remove = new TransactionalRemove();
|
||||||
|
|
||||||
|
return remove.run(manager, object);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
package space.kklochko.jpa_hospital_example.db.repositories.blocks;
|
||||||
|
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
|
import jakarta.persistence.PersistenceException;
|
||||||
|
import jakarta.persistence.RollbackException;
|
||||||
|
import space.kklochko.jpa_hospital_example.models.Department;
|
||||||
|
import space.kklochko.jpa_hospital_example.models.Indicator;
|
||||||
|
import space.kklochko.jpa_hospital_example.models.Patient;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class TransactionalInsert {
|
||||||
|
public <T> boolean run(EntityManager manager, T object) {
|
||||||
|
try {
|
||||||
|
if (!manager.getTransaction().isActive())
|
||||||
|
manager.getTransaction().begin();
|
||||||
|
|
||||||
|
manager.persist(object);
|
||||||
|
manager.getTransaction().commit();
|
||||||
|
return true;
|
||||||
|
} catch (PersistenceException e){
|
||||||
|
if (manager.getTransaction().isActive()) {
|
||||||
|
manager.getTransaction().rollback();
|
||||||
|
}
|
||||||
|
|
||||||
|
return (new TransactionalUpdate()).run(manager, object);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package space.kklochko.jpa_hospital_example.db.repositories.blocks;
|
||||||
|
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
|
import jakarta.persistence.RollbackException;
|
||||||
|
|
||||||
|
public class TransactionalRemove {
|
||||||
|
public <T> boolean run(EntityManager manager, T object) {
|
||||||
|
if(object == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
try {
|
||||||
|
manager.getTransaction().begin();
|
||||||
|
manager.remove(object);
|
||||||
|
manager.getTransaction().commit();
|
||||||
|
return true;
|
||||||
|
} catch (RollbackException e) {
|
||||||
|
if (manager.getTransaction().isActive()) {
|
||||||
|
manager.getTransaction().rollback();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package space.kklochko.jpa_hospital_example.db.repositories.blocks;
|
||||||
|
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
|
import jakarta.persistence.RollbackException;
|
||||||
|
|
||||||
|
public class TransactionalUpdate {
|
||||||
|
public <T> boolean run(EntityManager manager, T object) {
|
||||||
|
try {
|
||||||
|
manager.getTransaction().begin();
|
||||||
|
manager.merge(object);
|
||||||
|
manager.getTransaction().commit();
|
||||||
|
return true;
|
||||||
|
} catch (RollbackException e) {
|
||||||
|
if (manager.getTransaction().isActive()) {
|
||||||
|
manager.getTransaction().rollback();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue