Skip to content

Commit

Permalink
provide an initial implementation - not tested (iluwatar#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergejsvisockis committed Apr 15, 2024
1 parent 8940396 commit 33aecdc
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 12 deletions.
25 changes: 13 additions & 12 deletions active-record/src/main/java/com/iluwatar/activerecord/Customer.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
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;
private String firstName;
private String lastName;
private List<Order> 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<Customer> findAll() {
return List.of();
@Override
protected String getTableName() {
return "customer";
}

public void save(Customer customer) {
@Override
protected void setFieldsFromResultSet(ResultSet rs) {

}

}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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 <T extends RecordBase> List<T> findAll(Class<T> clazz) {
List<T> 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 <T> domain model type.
* @return the domain model.
*/
public <T extends RecordBase> T findById(Long id, Class<T> 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 <T extends RecordBase> String constructGetByIdQuery(Class<T> clazz) {
return "SELECT * FROM " + getDeclaredClassInstance(clazz).getTableName()
+ " WHERE id = ?";
}

private <T extends RecordBase> T getDeclaredClassInstance(Class<T> 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);
}
}

}

0 comments on commit 33aecdc

Please sign in to comment.