115115import static com .ing .data .cassandra .jdbc .utils .WarningConstants .GET_SET_FAILED ;
116116import static com .ing .data .cassandra .jdbc .utils .WarningConstants .GET_VECTOR_FAILED ;
117117import static java .lang .String .format ;
118+ import static org .apache .commons .collections4 .CollectionUtils .isEmpty ;
118119import static org .apache .commons .collections4 .IteratorUtils .chainedIterator ;
119120import static org .apache .commons .io .IOUtils .toCharArray ;
120121import 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 ;
0 commit comments