Skip to content

Commit d679072

Browse files
committed
Refactor CassandraResultSet constructors
1 parent 68cb030 commit d679072

File tree

3 files changed

+37
-37
lines changed

3 files changed

+37
-37
lines changed

checkstyle-suppressions.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@
44
"https://checkstyle.org/dtds/suppressions_1_2.dtd">
55

66
<suppressions>
7-
7+
<!-- Accept EMPTY_RESULT_SET name, even if the variable is not final (cannot be declared as final since it has to
8+
be instantiated in a static block). -->
9+
<suppress checks="StaticVariableName" files="CassandraResultSet.java"/>
810
</suppressions>

src/main/java/com/ing/data/cassandra/jdbc/CassandraResultSet.java

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
import static com.ing.data.cassandra.jdbc.utils.WarningConstants.GET_SET_FAILED;
116116
import static com.ing.data.cassandra.jdbc.utils.WarningConstants.GET_VECTOR_FAILED;
117117
import static java.lang.String.format;
118+
import static org.apache.commons.collections4.CollectionUtils.isEmpty;
118119
import static org.apache.commons.collections4.IteratorUtils.chainedIterator;
119120
import static org.apache.commons.io.IOUtils.toCharArray;
120121
import static org.apache.commons.lang3.StringUtils.SPACE;
@@ -190,7 +191,18 @@ public class CassandraResultSet extends AbstractResultSet
190191
* An empty Cassandra result set. It can be used to provide default implementations to methods returning
191192
* {@link ResultSet} objects.
192193
*/
193-
public static final CassandraResultSet EMPTY_RESULT_SET = new CassandraResultSet();
194+
public static CassandraResultSet EMPTY_RESULT_SET;
195+
196+
static {
197+
try {
198+
EMPTY_RESULT_SET = new CassandraResultSet();
199+
} catch (final SQLException e) {
200+
// This should not happen, an SQLException is thrown when the statement passed to the method is closed, but
201+
// here the statement is null.
202+
log.warn(e.getMessage());
203+
}
204+
}
205+
194206
/**
195207
* Default result set type for Cassandra implementation: {@link #TYPE_FORWARD_ONLY}.
196208
*/
@@ -206,32 +218,26 @@ public class CassandraResultSet extends AbstractResultSet
206218

207219
int rowNumber = 0;
208220
// Metadata of this result set.
209-
private final CResultSetMetaData metadata;
221+
private CResultSetMetaData metadata;
210222
private CassandraStatement statement;
211223
private Row currentRow;
212-
private Iterator<Row> rowsIterator;
213224
private int resultSetType;
214225
private int fetchDirection;
215226
private int fetchSize;
216227
private boolean wasNull;
217228
private boolean isClosed;
229+
private Iterator<Row> rowsIterator;
218230
// Result set from the Cassandra driver.
219231
private ResultSet driverResultSet;
220232
private final List<String> driverWarnings = new ArrayList<>();
221233

222234
/**
223235
* No argument constructor.
236+
*
237+
* @throws SQLException if a database access error occurs.
224238
*/
225-
CassandraResultSet() {
226-
this.metadata = new CResultSetMetaData();
227-
try {
228-
populateStatementRelatedProperties(null);
229-
} catch (final SQLException e) {
230-
// This should not happen, an SQLException is thrown when the statement passed to the method is closed, but
231-
// here the statement is null.
232-
log.warn(e.getMessage());
233-
}
234-
this.isClosed = false;
239+
CassandraResultSet() throws SQLException {
240+
this(null, new ArrayList<>());
235241
}
236242

237243
/**
@@ -243,19 +249,7 @@ public class CassandraResultSet extends AbstractResultSet
243249
* {@link Statement}.
244250
*/
245251
CassandraResultSet(final CassandraStatement statement, final ResultSet resultSet) throws SQLException {
246-
this.metadata = new CResultSetMetaData();
247-
populateStatementRelatedProperties(statement);
248-
this.driverResultSet = resultSet;
249-
this.rowsIterator = resultSet.iterator();
250-
this.isClosed = false;
251-
if (!resultSet.getExecutionInfos().isEmpty()) {
252-
this.driverWarnings.addAll(resultSet.getExecutionInfo().getWarnings());
253-
}
254-
255-
// Initialize the column values from the first row.
256-
if (hasMoreRows()) {
257-
populateColumns();
258-
}
252+
this(statement, new ArrayList<>(List.of(resultSet)));
259253
}
260254

261255
/**
@@ -268,11 +262,11 @@ public class CassandraResultSet extends AbstractResultSet
268262
*/
269263
@SuppressWarnings("unchecked")
270264
CassandraResultSet(final CassandraStatement statement, final ArrayList<ResultSet> resultSets) throws SQLException {
271-
// TODO: factorize constructors to use this one and avoid code duplication.
272-
this.metadata = new CResultSetMetaData();
273-
populateStatementRelatedProperties(statement);
274-
this.isClosed = false;
265+
initResultSetProperties(statement);
275266

267+
if (isEmpty(resultSets)) {
268+
return;
269+
}
276270
// We have several result sets, but we will use only the first one for metadata needs. However, we aggregate the
277271
// warnings of all the available result sets.
278272
this.driverResultSet = resultSets.get(0);
@@ -293,7 +287,7 @@ public class CassandraResultSet extends AbstractResultSet
293287

294288
// Initialize the column values from the first row.
295289
if (hasMoreRows()) {
296-
populateColumns();
290+
populateFirstRow();
297291
}
298292
}
299293

@@ -308,11 +302,13 @@ public static CassandraResultSet from(final ResultSet driverResultSet) throws SQ
308302
return new CassandraResultSet(null, driverResultSet);
309303
}
310304

311-
private void populateColumns() {
305+
private void populateFirstRow() {
312306
this.currentRow = this.rowsIterator.next();
313307
}
314308

315-
private void populateStatementRelatedProperties(final CassandraStatement statement) throws SQLException {
309+
private void initResultSetProperties(final CassandraStatement statement) throws SQLException {
310+
this.metadata = new CResultSetMetaData();
311+
this.isClosed = false;
316312
this.statement = statement;
317313
if (statement != null) {
318314
this.resultSetType = statement.getResultSetType();
@@ -1619,9 +1615,9 @@ public boolean isLast() throws SQLException {
16191615
@Override
16201616
public synchronized boolean next() {
16211617
if (hasMoreRows()) {
1622-
// 'populateColumns()' is called upon init to set up the metadata fields; so skip the first call.
1618+
// 'populateFirstRow()' is called upon init to set up the metadata fields; so skip the first call.
16231619
if (this.rowNumber != 0) {
1624-
populateColumns();
1620+
populateFirstRow();
16251621
}
16261622
this.rowNumber++;
16271623
return true;

src/test/java/com/ing/data/cassandra/jdbc/ResultSetUnitTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.nio.charset.StandardCharsets;
2424
import java.sql.Connection;
2525
import java.sql.ResultSet;
26+
import java.sql.SQLException;
2627
import java.sql.SQLSyntaxErrorException;
2728
import java.sql.SQLWarning;
2829
import java.sql.Statement;
@@ -79,7 +80,7 @@ void givenResultSetWithoutRows_whenFindColumns_returnExpectedIndex() throws Exce
7980
}
8081

8182
@Test
82-
void givenIncompleteResultSet_whenFindColumns_throwException() {
83+
void givenIncompleteResultSet_whenFindColumns_throwException() throws SQLException {
8384
final CassandraResultSet rs = new CassandraResultSet();
8485
final SQLSyntaxErrorException exception = assertThrows(SQLSyntaxErrorException.class,
8586
() -> rs.findColumn("keyname"));
@@ -95,6 +96,7 @@ void givenSelectStatementGeneratingWarning_whenGetWarnings_returnExpectedWarning
9596
when(mockDriverRs.getExecutionInfo()).thenReturn(mock(ExecutionInfo.class));
9697
when(mockDriverRs.getExecutionInfo().getWarnings())
9798
.thenReturn(Arrays.asList("First warning message", "Second warning message"));
99+
when(mockDriverRs.iterator()).thenReturn(Collections.emptyIterator());
98100
final ResultSet fakeRs = new CassandraResultSet(mockStmt, mockDriverRs);
99101
when(mockStmt.executeQuery(anyString())).thenReturn(fakeRs);
100102

0 commit comments

Comments
 (0)