@@ -31,9 +31,6 @@ public abstract class RecordBase<T extends RecordBase<?>> {
31
31
32
32
private static DataSource dataSource ;
33
33
34
- @ SuppressWarnings ({"unchecked" })
35
- private final Class <T > clazz = (Class <T >) getClass ();
36
-
37
34
private static final Set <Class <?>> STANDARD_TYPES = Set .of (
38
35
Boolean .class , Character .class , Byte .class , Short .class , Integer .class ,
39
36
Long .class , Float .class , Double .class , String .class
@@ -54,7 +51,7 @@ public static void setDataSource(DataSource dataSource) {
54
51
*
55
52
* @return the connection {@link Connection}.
56
53
*/
57
- protected Connection getConnection () {
54
+ protected static Connection getConnection () {
58
55
try {
59
56
return dataSource .getConnection ();
60
57
} catch (SQLException e ) {
@@ -77,26 +74,18 @@ protected Connection getConnection() {
77
74
*/
78
75
protected abstract void setFieldsFromResultSet (ResultSet rs ) throws SQLException ;
79
76
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
-
88
77
/**
89
78
* Find all the records for a corresponding domain model.
90
79
*
91
80
* @return all the domain model related records.
92
81
*/
93
- public List <T > findAll () {
82
+ public static < T extends RecordBase <?>> List <T > findAll (Class < T > clazz ) {
94
83
List <T > recordList = new ArrayList <>();
95
84
try (Connection conn = getConnection ();
96
- PreparedStatement pstmt = conn .prepareStatement (constructFindAllQuery ())) {
85
+ PreparedStatement pstmt = conn .prepareStatement (constructFindAllQuery (clazz ))) {
97
86
try (ResultSet rs = pstmt .executeQuery ()) {
98
87
while (rs .next ()) {
99
- T theRecord = getDeclaredClassInstance ();
88
+ T theRecord = getDeclaredClassInstance (clazz );
100
89
theRecord .setFieldsFromResultSet (rs );
101
90
recordList .add (theRecord );
102
91
}
@@ -113,17 +102,17 @@ public List<T> findAll() {
113
102
* @param id domain model identifier.
114
103
* @return the domain model.
115
104
*/
116
- public T findById (Long id ) {
105
+ public static < T extends RecordBase <?>> T findById (Long id , Class < T > clazz ) {
117
106
try (Connection conn = getConnection ();
118
- PreparedStatement pstmt = conn .prepareStatement (constructFindByIdQuery ())) {
107
+ PreparedStatement pstmt = conn .prepareStatement (constructFindByIdQuery (clazz ))) {
119
108
pstmt .setLong (1 , id );
120
109
try (ResultSet rs = pstmt .executeQuery ()) {
121
110
if (rs .next ()) {
122
- T theRecord = getDeclaredClassInstance ();
111
+ T theRecord = getDeclaredClassInstance (clazz );
123
112
theRecord .setFieldsFromResultSet (rs );
124
113
return theRecord ;
125
114
}
126
- return getDeclaredClassInstance ();
115
+ return getDeclaredClassInstance (clazz );
127
116
}
128
117
} catch (SQLException e ) {
129
118
throw new RecordDataAccessException (EXCEPTION_MESSAGE + clazz .getName () + " with id=" + id ,
@@ -134,30 +123,37 @@ public T findById(Long id) {
134
123
/**
135
124
* Save the record.
136
125
*/
137
- public void save () {
126
+ public static < T extends RecordBase <?>> void save (Class < T > clazz ) {
138
127
try (Connection connection = getConnection ();
139
- PreparedStatement pstmt = connection .prepareStatement (constructInsertionQuery (),
128
+ PreparedStatement pstmt = connection .prepareStatement (constructInsertionQuery (clazz ),
140
129
Statement .RETURN_GENERATED_KEYS )) {
141
130
142
- setPreparedStatementParams (pstmt );
131
+ setPreparedStatementParams (pstmt , clazz );
143
132
pstmt .executeUpdate ();
144
133
145
134
} catch (SQLException e ) {
146
135
throw new RecordDataAccessException (EXCEPTION_MESSAGE + clazz .getName (), e );
147
136
}
148
137
}
149
138
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 ) {
151
147
List <Object > arguments = new ArrayList <>();
152
148
try {
153
- InsertionQuery insert = Query .insertInto (getClass () .getSimpleName ());
149
+ InsertionQuery insert = Query .insertInto (clazz .getSimpleName ());
154
150
155
- List <Field > standardFields = filterStandardTypes ();
151
+ List <Field > standardFields = filterStandardTypes (clazz );
156
152
157
153
for (Field field : standardFields ) {
158
154
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 )) );
161
157
}
162
158
return insert .toString ();
163
159
} catch (IllegalAccessException ignored ) {
@@ -166,22 +162,22 @@ protected String constructInsertionQuery() {
166
162
return null ;
167
163
}
168
164
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 ())
171
167
.filter (field -> STANDARD_TYPES .contains (field .getType ()))
172
168
.toList ();
173
169
}
174
170
175
171
// 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 = ?" ;
178
174
}
179
175
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 ();
182
178
}
183
179
184
- private T getDeclaredClassInstance () {
180
+ private static < T extends RecordBase <?>> T getDeclaredClassInstance (Class < T > clazz ) {
185
181
try {
186
182
return clazz .getDeclaredConstructor ().newInstance ();
187
183
} catch (InvocationTargetException
0 commit comments