parent
fead10291c
commit
dbd92b41bc
@ -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<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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue