9
9
import java .util .List ;
10
10
import javax .sql .DataSource ;
11
11
import lombok .RequiredArgsConstructor ;
12
+ import lombok .Setter ;
12
13
13
14
@ RequiredArgsConstructor
14
- public abstract class RecordBase {
15
+ public abstract class RecordBase < T extends RecordBase <?>> {
15
16
16
- private final DataSource dataSource ;
17
+ @ Setter
18
+ private static DataSource dataSource ;
19
+
20
+ @ SuppressWarnings ({"unchecked" })
21
+ private final Class <T > clazz = (Class <T >) getClass ();
17
22
18
23
protected Connection getConnection () throws SQLException {
19
24
return dataSource .getConnection ();
@@ -26,22 +31,22 @@ protected Connection getConnection() throws SQLException {
26
31
*/
27
32
protected abstract String getTableName ();
28
33
29
- protected abstract void setFieldsFromResultSet (ResultSet rs );
34
+ protected abstract void setFieldsFromResultSet (ResultSet rs ) throws SQLException ;
35
+
36
+ protected abstract void setPreparedStatementParams (PreparedStatement pstmt ) throws SQLException ;
30
37
31
38
/**
32
39
* Find all the records for a corresponding domain model.
33
40
*
34
- * @param clazz domain model class.
35
- * @param <T> domain model type.
36
41
* @return all the domain model related records.
37
42
*/
38
- public < T extends RecordBase > List <T > findAll (Class < T > clazz ) {
43
+ public List <T > findAll () {
39
44
List <T > recordList = new ArrayList <>();
40
45
try (Connection conn = getConnection ();
41
- PreparedStatement pstmt = conn .prepareStatement (constructGetByIdQuery ( clazz ))) {
46
+ PreparedStatement pstmt = conn .prepareStatement (constructFindAllQuery ( ))) {
42
47
try (ResultSet rs = pstmt .executeQuery ()) {
43
48
while (rs .next ()) {
44
- T record = getDeclaredClassInstance (clazz );
49
+ T record = getDeclaredClassInstance ();
45
50
record .setFieldsFromResultSet (rs );
46
51
recordList .add (record );
47
52
}
@@ -57,26 +62,24 @@ public <T extends RecordBase> List<T> findAll(Class<T> clazz) {
57
62
/**
58
63
* Find a domain model by its ID.
59
64
*
60
- * @param id domain model identifier.
61
- * @param clazz domain model class.
62
- * @param <T> domain model type.
65
+ * @param id domain model identifier.
63
66
* @return the domain model.
64
67
*/
65
- public < T extends RecordBase > T findById (Long id , Class < T > clazz ) {
68
+ public T findById (Long id ) {
66
69
try (Connection conn = getConnection ();
67
- PreparedStatement pstmt = conn .prepareStatement (constructGetByIdQuery ( clazz ))) {
70
+ PreparedStatement pstmt = conn .prepareStatement (constructFindByIdQuery ( ))) {
68
71
pstmt .setLong (1 , id );
69
72
try (ResultSet rs = pstmt .executeQuery ()) {
70
73
if (rs .next ()) {
71
- T record = getDeclaredClassInstance (clazz );
74
+ T record = getDeclaredClassInstance ();
72
75
record .setFieldsFromResultSet (rs );
73
76
return record ;
74
77
}
75
- return getDeclaredClassInstance (clazz );
78
+ return getDeclaredClassInstance ();
76
79
}
77
80
} catch (SQLException e ) {
78
81
throw new RuntimeException (
79
- "Unable to fine a record for the following domain model : " + clazz .getName () + " by id="
82
+ "Unable to find a record for the following domain model : " + clazz .getName () + " by id="
80
83
+ id
81
84
+ " due to the data persistence error" , e );
82
85
}
@@ -86,15 +89,32 @@ public <T extends RecordBase> T findById(Long id, Class<T> clazz) {
86
89
* Save the record.
87
90
*/
88
91
public void save () {
89
- // TODO
92
+ try (Connection connection = getConnection ();
93
+ // TODO
94
+ PreparedStatement pstmt = connection .prepareStatement (null ,
95
+ PreparedStatement .RETURN_GENERATED_KEYS )) {
96
+
97
+ setPreparedStatementParams (pstmt );
98
+ pstmt .executeUpdate ();
99
+
100
+ } catch (SQLException e ) {
101
+ throw new RuntimeException (
102
+ "Unable to save the record for the following domain model : " + clazz .getName ()
103
+ + " due to the data persistence error" , e );
104
+ }
105
+ }
106
+
107
+
108
+ private String constructFindAllQuery () {
109
+ return "SELECT * FROM " + getDeclaredClassInstance ().getTableName ();
90
110
}
91
111
92
- private < T extends RecordBase > String constructGetByIdQuery ( Class < T > clazz ) {
93
- return "SELECT * FROM " + getDeclaredClassInstance (clazz ).getTableName ()
112
+ private String constructFindByIdQuery ( ) {
113
+ return "SELECT * FROM " + getDeclaredClassInstance ().getTableName ()
94
114
+ " WHERE id = ?" ;
95
115
}
96
116
97
- private < T extends RecordBase > T getDeclaredClassInstance (Class < T > clazz ) {
117
+ private T getDeclaredClassInstance () {
98
118
try {
99
119
return clazz .getDeclaredConstructor ().newInstance ();
100
120
} catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException |
0 commit comments