diff --git a/active-record/src/main/java/com/iluwatar/activerecord/Customer.java b/active-record/src/main/java/com/iluwatar/activerecord/Customer.java index cdabef4cbff7..fab6b2a7fd5e 100644 --- a/active-record/src/main/java/com/iluwatar/activerecord/Customer.java +++ b/active-record/src/main/java/com/iluwatar/activerecord/Customer.java @@ -1,16 +1,14 @@ package com.iluwatar.activerecord; +import java.sql.ResultSet; import java.util.List; -import lombok.EqualsAndHashCode; +import javax.sql.DataSource; import lombok.Getter; -import lombok.RequiredArgsConstructor; import lombok.Setter; @Getter @Setter -@RequiredArgsConstructor -@EqualsAndHashCode(onlyExplicitlyIncluded = true) -public class Customer { +public class Customer extends RecordBase { private Long id; private String customerNumber; @@ -18,20 +16,23 @@ public class Customer { private String lastName; private List orders; - public Customer findById(Long id) { - return new Customer(); + public Customer(DataSource dataSource) { + super(dataSource); } public Customer findByNumber(String customerNumber) { - return new Customer(); + // TODO + return null; +// return new Customer(); } - public List findAll() { - return List.of(); + @Override + protected String getTableName() { + return "customer"; } - public void save(Customer customer) { + @Override + protected void setFieldsFromResultSet(ResultSet rs) { } - } diff --git a/active-record/src/main/java/com/iluwatar/activerecord/RecordBase.java b/active-record/src/main/java/com/iluwatar/activerecord/RecordBase.java index 32fcc39a0dc5..4fc4862df970 100644 --- a/active-record/src/main/java/com/iluwatar/activerecord/RecordBase.java +++ b/active-record/src/main/java/com/iluwatar/activerecord/RecordBase.java @@ -1,5 +1,12 @@ package com.iluwatar.activerecord; +import java.lang.reflect.InvocationTargetException; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; import javax.sql.DataSource; import lombok.RequiredArgsConstructor; @@ -8,4 +15,78 @@ public abstract class RecordBase { private final DataSource dataSource; + protected Connection getConnection() throws SQLException { + return dataSource.getConnection(); + } + + /** + * Returns an underlying table name defined within the domain model. + * + * @return the table name. + */ + protected abstract String getTableName(); + + protected abstract void setFieldsFromResultSet(ResultSet rs); + + public List findAll(Class clazz) { + List recordList = new ArrayList<>(); + try (Connection conn = getConnection(); + PreparedStatement pstmt = conn.prepareStatement(constructGetByIdQuery(clazz))) { + try (ResultSet rs = pstmt.executeQuery()) { + while (rs.next()) { + T record = getDeclaredClassInstance(clazz); + record.setFieldsFromResultSet(rs); + recordList.add(record); + } + return recordList; + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + /** + * Find a domain model by its ID. + * + * @param id domain model identifier. + * @param clazz domain model class. + * @param domain model type. + * @return the domain model. + */ + public T findById(Long id, Class clazz) { + try (Connection conn = getConnection(); + PreparedStatement pstmt = conn.prepareStatement(constructGetByIdQuery(clazz))) { + pstmt.setLong(1, id); + try (ResultSet rs = pstmt.executeQuery()) { + if (rs.next()) { + T record = getDeclaredClassInstance(clazz); + record.setFieldsFromResultSet(rs); + return record; + } + return getDeclaredClassInstance(clazz); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public void save() { + // TODO + } + + private String constructGetByIdQuery(Class clazz) { + return "SELECT * FROM " + getDeclaredClassInstance(clazz).getTableName() + + " WHERE id = ?"; + } + + private T getDeclaredClassInstance(Class clazz) { + try { + return clazz.getDeclaredConstructor().newInstance(); + } catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException | + InstantiationException e) { + throw new IllegalStateException( + "Unable to create a new instance of the class=" + clazz.getName(), e); + } + } + }