Skip to content

Commit 2454313

Browse files
committed
Code claning
1 parent a8bfb31 commit 2454313

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

utils/SqlExecutor.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,11 @@ private <T> void assertEquals(T expected, T result) {
130130
}
131131

132132
/**
133-
* Less than 180 lines long class to simplify work with JDBC.
133+
* Less than 170 lines long class to simplify work with JDBC.
134134
* Original source: <a href="https://github.com/pponec/DirectoryBookmarks/blob/development/utils/SqlExecutor.java">GitHub</a>
135135
* Licence: Apache License, Version 2.0
136136
* @author Pavel Ponec, https://github.com/pponec
137-
* @version 1.0.5
137+
* @version 1.0.6
138138
*/
139139
static class SqlParamBuilder implements Closeable {
140140
/** SQL parameter mark type of {@code :param} */
@@ -143,7 +143,6 @@ static class SqlParamBuilder implements Closeable {
143143
private final Map<String, Object> params = new HashMap<>();
144144
private String sqlTemplate = null;
145145
private PreparedStatement preparedStatement = null;
146-
private ResultSet resultSet = null;
147146

148147
public SqlParamBuilder(Connection dbConnection) {
149148
this.dbConnection = dbConnection;
@@ -167,14 +166,11 @@ public int execute() throws IllegalStateException, SQLException {
167166
return prepareStatement().executeUpdate();
168167
}
169168

169+
/** A ResultSet object is automatically closed when the Statement object that generated it is closed,
170+
* re-executed, or used to retrieve the next result from a sequence of multiple results. */
170171
private ResultSet executeSelect() throws IllegalStateException {
171-
try (AutoCloseable rs = resultSet) {
172-
} catch (Exception e) {
173-
throw new IllegalStateException("Closing fails", e);
174-
}
175172
try {
176-
resultSet = prepareStatement().executeQuery();
177-
return resultSet;
173+
return prepareStatement().executeQuery();
178174
} catch (Exception ex) {
179175
throw (ex instanceof RuntimeException re) ? re : new IllegalStateException(ex);
180176
}
@@ -216,11 +212,10 @@ public Connection getConnection() {
216212
/** The method closes a PreparedStatement object with related objects, not the database connection. */
217213
@Override
218214
public void close() {
219-
try (AutoCloseable c1 = resultSet; PreparedStatement c2 = preparedStatement) {
215+
try (AutoCloseable c2 = preparedStatement) {
220216
} catch (Exception e) {
221217
throw new IllegalStateException("Closing fails", e);
222218
} finally {
223-
resultSet = null;
224219
preparedStatement = null;
225220
}
226221
}
@@ -238,7 +233,7 @@ public PreparedStatement prepareStatement() throws SQLException {
238233
return result;
239234
}
240235

241-
protected String buildSql( List<Object> sqlValues, boolean toLog) {
236+
private String buildSql(List<Object> sqlValues, boolean toLog) {
242237
final var result = new StringBuilder(256);
243238
final var matcher = SQL_MARK.matcher(sqlTemplate);
244239
final var missingKeys = new HashSet<>();
@@ -256,7 +251,7 @@ protected String buildSql( List<Object> sqlValues, boolean toLog) {
256251
sqlValues.add(values[i]);
257252
}
258253
} else {
259-
matcher.appendReplacement(result, Matcher.quoteReplacement(":" + key));
254+
matcher.appendReplacement(result, Matcher.quoteReplacement(matcher.group()));
260255
missingKeys.add(key);
261256
}
262257
}

0 commit comments

Comments
 (0)