26
26
import java .io .IOException ;
27
27
import java .io .InputStreamReader ;
28
28
import java .sql .Connection ;
29
+ import java .sql .PreparedStatement ;
29
30
import java .sql .ResultSet ;
30
31
import java .sql .SQLException ;
31
32
import java .sql .Statement ;
@@ -54,15 +55,23 @@ public class DatasourceOperations {
54
55
55
56
private final DataSource datasource ;
56
57
private final RelationalJdbcConfiguration relationalJdbcConfiguration ;
58
+ private final DatabaseType databaseType ;
57
59
58
60
private final Random random = new Random ();
59
61
60
62
public DatasourceOperations (
61
- DataSource datasource , RelationalJdbcConfiguration relationalJdbcConfiguration ) {
63
+ DataSource datasource ,
64
+ DatabaseType databaseType ,
65
+ RelationalJdbcConfiguration relationalJdbcConfiguration ) {
62
66
this .datasource = datasource ;
67
+ this .databaseType = databaseType ;
63
68
this .relationalJdbcConfiguration = relationalJdbcConfiguration ;
64
69
}
65
70
71
+ public DatabaseType getDatabaseType () {
72
+ return databaseType ;
73
+ }
74
+
66
75
/**
67
76
* Execute SQL script
68
77
*
@@ -116,7 +125,8 @@ public void executeScript(String scriptFilePath) throws SQLException {
116
125
* @param <T> : Business entity class
117
126
* @throws SQLException : Exception during the query execution.
118
127
*/
119
- public <T > List <T > executeSelect (@ Nonnull String query , @ Nonnull Converter <T > converterInstance )
128
+ public <T > List <T > executeSelect (
129
+ @ Nonnull QueryGenerator .PreparedQuery query , @ Nonnull Converter <T > converterInstance )
120
130
throws SQLException {
121
131
ArrayList <T > results = new ArrayList <>();
122
132
executeSelectOverStream (query , converterInstance , stream -> stream .forEach (results ::add ));
@@ -134,38 +144,47 @@ public <T> List<T> executeSelect(@Nonnull String query, @Nonnull Converter<T> co
134
144
* @throws SQLException : Exception during the query execution.
135
145
*/
136
146
public <T > void executeSelectOverStream (
137
- @ Nonnull String query ,
147
+ @ Nonnull QueryGenerator . PreparedQuery query ,
138
148
@ Nonnull Converter <T > converterInstance ,
139
149
@ Nonnull Consumer <Stream <T >> consumer )
140
150
throws SQLException {
141
151
withRetries (
142
152
() -> {
143
153
try (Connection connection = borrowConnection ();
144
- Statement statement = connection .createStatement ();
145
- ResultSet resultSet = statement .executeQuery (query )) {
146
- ResultSetIterator <T > iterator = new ResultSetIterator <>(resultSet , converterInstance );
147
- consumer .accept (iterator .toStream ());
148
- return null ;
154
+ PreparedStatement statement = connection .prepareStatement (query .sql ())) {
155
+ List <Object > params = query .parameters ();
156
+ for (int i = 0 ; i < params .size (); i ++) {
157
+ statement .setObject (i + 1 , params .get (i ));
158
+ }
159
+ try (ResultSet resultSet = statement .executeQuery ()) {
160
+ ResultSetIterator <T > iterator = new ResultSetIterator <>(resultSet , converterInstance );
161
+ consumer .accept (iterator .toStream ());
162
+ return null ;
163
+ }
149
164
}
150
165
});
151
166
}
152
167
153
168
/**
154
169
* Executes the UPDATE or INSERT Query
155
170
*
156
- * @param query : query to be executed
171
+ * @param preparedQuery : query to be executed
157
172
* @return : Number of rows modified / inserted.
158
173
* @throws SQLException : Exception during Query Execution.
159
174
*/
160
- public int executeUpdate (String query ) throws SQLException {
175
+ public int executeUpdate (QueryGenerator . PreparedQuery preparedQuery ) throws SQLException {
161
176
return withRetries (
162
177
() -> {
163
178
try (Connection connection = borrowConnection ();
164
- Statement statement = connection .createStatement ()) {
179
+ PreparedStatement statement = connection .prepareStatement (preparedQuery .sql ())) {
180
+ List <Object > params = preparedQuery .parameters ();
181
+ for (int i = 0 ; i < params .size (); i ++) {
182
+ statement .setObject (i + 1 , params .get (i ));
183
+ }
165
184
boolean autoCommit = connection .getAutoCommit ();
166
185
connection .setAutoCommit (true );
167
186
try {
168
- return statement .executeUpdate (query );
187
+ return statement .executeUpdate ();
169
188
} finally {
170
189
connection .setAutoCommit (autoCommit );
171
190
}
@@ -188,9 +207,7 @@ public void runWithinTransaction(TransactionCallback callback) throws SQLExcepti
188
207
connection .setAutoCommit (false );
189
208
try {
190
209
try {
191
- try (Statement statement = connection .createStatement ()) {
192
- success = callback .execute (statement );
193
- }
210
+ success = callback .execute (connection );
194
211
} finally {
195
212
if (success ) {
196
213
connection .commit ();
@@ -206,6 +223,17 @@ public void runWithinTransaction(TransactionCallback callback) throws SQLExcepti
206
223
});
207
224
}
208
225
226
+ public Integer execute (Connection connection , QueryGenerator .PreparedQuery preparedQuery )
227
+ throws SQLException {
228
+ try (PreparedStatement statement = connection .prepareStatement (preparedQuery .sql ())) {
229
+ List <Object > params = preparedQuery .parameters ();
230
+ for (int i = 0 ; i < params .size (); i ++) {
231
+ statement .setObject (i + 1 , params .get (i ));
232
+ }
233
+ return statement .executeUpdate ();
234
+ }
235
+ }
236
+
209
237
private boolean isRetryable (SQLException e ) {
210
238
String sqlState = e .getSQLState ();
211
239
@@ -291,7 +319,7 @@ public interface Operation<T> {
291
319
292
320
// Interface for transaction callback
293
321
public interface TransactionCallback {
294
- boolean execute (Statement statement ) throws SQLException ;
322
+ boolean execute (Connection connection ) throws SQLException ;
295
323
}
296
324
297
325
public boolean isConstraintViolation (SQLException e ) {
0 commit comments