Skip to content

Commit 60d2410

Browse files
push non-working solution
1 parent 98807e3 commit 60d2410

File tree

7 files changed

+47
-63
lines changed

7 files changed

+47
-63
lines changed

active-record/src/main/java/com/iluwatar/activerecord/App.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,16 @@ private static void executeOperation() {
6060
order.setOrderNumber("O123");
6161

6262
// customer.addOrder(order);
63-
customer.save();
63+
customer.save(Customer.class);
6464

65-
LOGGER.info("The customer data by ID={}", customer.findById(1L));
65+
LOGGER.info("The customer data by ID={}", customer.findById(1L, Customer.class));
6666

67-
LOGGER.info("find all the customers={}", customer.findAll());
67+
LOGGER.info("find all the customers={}", customer.findAll(Customer.class));
6868
}
6969

7070
private static void createSchema(DataSource dataSource) throws SQLException {
7171
try (Connection conn = dataSource.getConnection();
72-
Statement stmt = conn.createStatement()) {
72+
Statement stmt = conn.createStatement()) {
7373
stmt.execute(CREATE_SCHEMA_SQL);
7474
}
7575
}

active-record/src/main/java/com/iluwatar/activerecord/Customer.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.iluwatar.activerecord;
22

33
import com.iluwatar.activerecord.base.RecordBase;
4-
import java.sql.PreparedStatement;
54
import java.sql.ResultSet;
65
import java.sql.SQLException;
76
import java.util.List;
@@ -45,11 +44,11 @@ protected void setFieldsFromResultSet(ResultSet rs) throws SQLException {
4544
this.lastName = rs.getString("lastName");
4645
}
4746

48-
@Override
49-
protected void setPreparedStatementParams(PreparedStatement pstmt) throws SQLException {
50-
pstmt.setLong(1, id);
51-
pstmt.setString(2, customerNumber);
52-
pstmt.setString(3, firstName);
53-
pstmt.setString(4, lastName);
54-
}
47+
// @Override
48+
// protected void setPreparedStatementParams(PreparedStatement pstmt) throws SQLException {
49+
// pstmt.setLong(1, id);
50+
// pstmt.setString(2, customerNumber);
51+
// pstmt.setString(3, firstName);
52+
// pstmt.setString(4, lastName);
53+
// }
5554
}

active-record/src/main/java/com/iluwatar/activerecord/Order.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.iluwatar.activerecord;
22

33
import com.iluwatar.activerecord.base.RecordBase;
4-
import java.sql.PreparedStatement;
54
import java.sql.ResultSet;
65
import java.sql.SQLException;
76
import lombok.AllArgsConstructor;
@@ -32,8 +31,4 @@ protected void setFieldsFromResultSet(ResultSet rs) throws SQLException {
3231
this.orderNumber = rs.getString("order_number");
3332
}
3433

35-
@Override
36-
protected void setPreparedStatementParams(PreparedStatement pstmt) throws SQLException {
37-
// TODO
38-
}
3934
}

active-record/src/main/java/com/iluwatar/activerecord/base/RecordBase.java

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ public abstract class RecordBase<T extends RecordBase<?>> {
3131

3232
private static DataSource dataSource;
3333

34-
@SuppressWarnings({"unchecked"})
35-
private final Class<T> clazz = (Class<T>) getClass();
36-
3734
private static final Set<Class<?>> STANDARD_TYPES = Set.of(
3835
Boolean.class, Character.class, Byte.class, Short.class, Integer.class,
3936
Long.class, Float.class, Double.class, String.class
@@ -54,7 +51,7 @@ public static void setDataSource(DataSource dataSource) {
5451
*
5552
* @return the connection {@link Connection}.
5653
*/
57-
protected Connection getConnection() {
54+
protected static Connection getConnection() {
5855
try {
5956
return dataSource.getConnection();
6057
} catch (SQLException e) {
@@ -77,26 +74,18 @@ protected Connection getConnection() {
7774
*/
7875
protected abstract void setFieldsFromResultSet(ResultSet rs) throws SQLException;
7976

80-
/**
81-
* Set the prepared statement parameters for the SQL statement to insert/update record.
82-
*
83-
* @param pstmt prepared statement {@link PreparedStatement}.
84-
* @throws SQLException an SQL exception.
85-
*/
86-
protected abstract void setPreparedStatementParams(PreparedStatement pstmt) throws SQLException;
87-
8877
/**
8978
* Find all the records for a corresponding domain model.
9079
*
9180
* @return all the domain model related records.
9281
*/
93-
public List<T> findAll() {
82+
public static <T extends RecordBase<?>> List<T> findAll(Class<T> clazz) {
9483
List<T> recordList = new ArrayList<>();
9584
try (Connection conn = getConnection();
96-
PreparedStatement pstmt = conn.prepareStatement(constructFindAllQuery())) {
85+
PreparedStatement pstmt = conn.prepareStatement(constructFindAllQuery(clazz))) {
9786
try (ResultSet rs = pstmt.executeQuery()) {
9887
while (rs.next()) {
99-
T theRecord = getDeclaredClassInstance();
88+
T theRecord = getDeclaredClassInstance(clazz);
10089
theRecord.setFieldsFromResultSet(rs);
10190
recordList.add(theRecord);
10291
}
@@ -113,17 +102,17 @@ public List<T> findAll() {
113102
* @param id domain model identifier.
114103
* @return the domain model.
115104
*/
116-
public T findById(Long id) {
105+
public static <T extends RecordBase<?>> T findById(Long id, Class<T> clazz) {
117106
try (Connection conn = getConnection();
118-
PreparedStatement pstmt = conn.prepareStatement(constructFindByIdQuery())) {
107+
PreparedStatement pstmt = conn.prepareStatement(constructFindByIdQuery(clazz))) {
119108
pstmt.setLong(1, id);
120109
try (ResultSet rs = pstmt.executeQuery()) {
121110
if (rs.next()) {
122-
T theRecord = getDeclaredClassInstance();
111+
T theRecord = getDeclaredClassInstance(clazz);
123112
theRecord.setFieldsFromResultSet(rs);
124113
return theRecord;
125114
}
126-
return getDeclaredClassInstance();
115+
return getDeclaredClassInstance(clazz);
127116
}
128117
} catch (SQLException e) {
129118
throw new RecordDataAccessException(EXCEPTION_MESSAGE + clazz.getName() + " with id=" + id,
@@ -134,30 +123,37 @@ public T findById(Long id) {
134123
/**
135124
* Save the record.
136125
*/
137-
public void save() {
126+
public static <T extends RecordBase<?>> void save(Class<T> clazz) {
138127
try (Connection connection = getConnection();
139-
PreparedStatement pstmt = connection.prepareStatement(constructInsertionQuery(),
128+
PreparedStatement pstmt = connection.prepareStatement(constructInsertionQuery(clazz),
140129
Statement.RETURN_GENERATED_KEYS)) {
141130

142-
setPreparedStatementParams(pstmt);
131+
setPreparedStatementParams(pstmt, clazz);
143132
pstmt.executeUpdate();
144133

145134
} catch (SQLException e) {
146135
throw new RecordDataAccessException(EXCEPTION_MESSAGE + clazz.getName(), e);
147136
}
148137
}
149138

150-
protected String constructInsertionQuery() {
139+
private static <T extends RecordBase<?>> void setPreparedStatementParams(PreparedStatement pstmt,
140+
Class<T> clazz)
141+
throws SQLException {
142+
List<Field> standardFields = filterStandardTypes(clazz);
143+
144+
}
145+
146+
protected static <T extends RecordBase<?>> String constructInsertionQuery(Class<T> clazz) {
151147
List<Object> arguments = new ArrayList<>();
152148
try {
153-
InsertionQuery insert = Query.insertInto(getClass().getSimpleName());
149+
InsertionQuery insert = Query.insertInto(clazz.getSimpleName());
154150

155-
List<Field> standardFields = filterStandardTypes();
151+
List<Field> standardFields = filterStandardTypes(clazz);
156152

157153
for (Field field : standardFields) {
158154
field.setAccessible(true);
159-
arguments.add(field.get(this));
160-
insert.column(field.getName()).value("?");
155+
arguments.add(field.get(clazz)); // FIXME: it doesn't work and fail - fix it
156+
insert.column(field.getName()).value(String.valueOf(field.get(clazz)));
161157
}
162158
return insert.toString();
163159
} catch (IllegalAccessException ignored) {
@@ -166,22 +162,22 @@ protected String constructInsertionQuery() {
166162
return null;
167163
}
168164

169-
private List<Field> filterStandardTypes() {
170-
return Arrays.stream(getClass().getDeclaredFields())
165+
private static <T extends RecordBase<?>> List<Field> filterStandardTypes(Class<T> clazz) {
166+
return Arrays.stream(clazz.getDeclaredFields())
171167
.filter(field -> STANDARD_TYPES.contains(field.getType()))
172168
.toList();
173169
}
174170

175171
// TODO: implement Select query within the Query class
176-
private String constructFindByIdQuery() {
177-
return constructFindAllQuery() + " WHERE id = ?";
172+
private static <T extends RecordBase<?>> String constructFindByIdQuery(Class<T> clazz) {
173+
return constructFindAllQuery(clazz) + " WHERE id = ?";
178174
}
179175

180-
private String constructFindAllQuery() {
181-
return "SELECT * FROM " + getDeclaredClassInstance().getTableName();
176+
private static <T extends RecordBase<?>> String constructFindAllQuery(Class<T> clazz) {
177+
return "SELECT * FROM " + getDeclaredClassInstance(clazz).getTableName();
182178
}
183179

184-
private T getDeclaredClassInstance() {
180+
private static <T extends RecordBase<?>> T getDeclaredClassInstance(Class<T> clazz) {
185181
try {
186182
return clazz.getDeclaredConstructor().newInstance();
187183
} catch (InvocationTargetException

active-record/src/test/java/com/iluwatar/activerecord/Courier.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.iluwatar.activerecord;
22

33
import com.iluwatar.activerecord.base.RecordBase;
4-
import java.sql.PreparedStatement;
54
import java.sql.ResultSet;
65
import java.sql.SQLException;
76
import lombok.Getter;
@@ -24,9 +23,4 @@ protected String getTableName() {
2423
protected void setFieldsFromResultSet(ResultSet rs) throws SQLException {
2524

2625
}
27-
28-
@Override
29-
protected void setPreparedStatementParams(PreparedStatement pstmt) throws SQLException {
30-
31-
}
3226
}

active-record/src/test/java/com/iluwatar/activerecord/CustomerCrudTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@ void shouldExecuteCustomerLifecycle() {
1616
customer.setFirstName("John");
1717
customer.setLastName("Smith");
1818

19-
customer.save();
19+
customer.save(Customer.class);
2020

2121
Customer customerTwo = new Customer();
2222
customerTwo.setId(2L);
2323
customerTwo.setCustomerNumber("C798237");
2424
customerTwo.setFirstName("SecondCustomerName");
2525
customerTwo.setLastName("SecondCustomerLastName");
2626

27-
customerTwo.save();
27+
customerTwo.save(Customer.class);
2828

2929
// find all the customers
30-
List<Customer> customers = customerTwo.findAll();
30+
List<Customer> customers = customerTwo.findAll(Customer.class);
3131
Customer firstCustomer = customers.get(0);
3232
assertEquals(2, customers.size());
3333
assertEquals(1L, firstCustomer.getId());
@@ -36,8 +36,7 @@ void shouldExecuteCustomerLifecycle() {
3636
assertEquals("Smith", firstCustomer.getLastName());
3737

3838
// find the second customer
39-
Customer secondCustomer =
40-
new Customer().findById(2L); // TODO: has to be referenced fom the static context
39+
Customer secondCustomer = Customer.findById(2L, Customer.class);
4140
assertEquals(2L, secondCustomer.getId());
4241
assertEquals("C798237", secondCustomer.getCustomerNumber());
4342
assertEquals("SecondCustomerName", secondCustomer.getFirstName());

active-record/src/test/java/com/iluwatar/activerecord/base/QueryTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

55
import com.iluwatar.activerecord.Courier;
6+
import com.iluwatar.activerecord.Customer;
67
import org.junit.jupiter.api.Test;
78

89
class QueryTest {
@@ -16,7 +17,7 @@ void shouldConstructFullInsertQuery() {
1617

1718
final String expected = "INSERT INTO Courier (id,firstName,lastName) VALUES (?,?,?)";
1819

19-
String insertQuery = courier.constructInsertionQuery();
20+
String insertQuery = courier.constructInsertionQuery(Customer.class);
2021

2122
assertEquals(expected, insertQuery);
2223
}

0 commit comments

Comments
 (0)