Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: getting resultset metadata twice could skip row #323

Merged
merged 2 commits into from Jan 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -547,7 +547,7 @@ public InputStream getBinaryStream(String columnLabel) throws SQLException {
@Override
public JdbcResultSetMetaData getMetaData() throws SQLException {
checkClosed();
if (isBeforeFirst()) {
if (isBeforeFirst() && !nextCalledForMetaData) {
// do a call to next() on the underlying resultset to initialize metadata
nextCalledForMetaData = true;
nextCalledForMetaDataResult = spanner.next();
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/com/google/cloud/spanner/jdbc/JdbcResultSetTest.java
Expand Up @@ -25,6 +25,7 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.google.cloud.ByteArray;
import com.google.cloud.Date;
Expand Down Expand Up @@ -802,6 +803,32 @@ public void testGetMetaData() throws SQLException {
assertNotNull(metadata);
}

@Test
public void testGetMetaDataBeforeNext() throws SQLException {
ResultSet spannerResultSet = mock(ResultSet.class);
when(spannerResultSet.next()).thenReturn(true, false);

JdbcResultSet resultSet = JdbcResultSet.of(spannerResultSet);
assertNotNull(resultSet.getMetaData());
assertTrue(resultSet.next());
assertFalse(resultSet.next());
}

@Test
public void testGetMetaDataTwiceBeforeNext() throws SQLException {
ResultSet spannerResultSet = mock(ResultSet.class);
when(spannerResultSet.next()).thenReturn(true, false);

JdbcResultSet resultSet = JdbcResultSet.of(spannerResultSet);
assertNotNull(resultSet.getMetaData());
assertNotNull(resultSet.getMetaData());

// This would have returned false before the fix in
// https://github.com/googleapis/java-spanner-jdbc/pull/323
assertTrue(resultSet.next());
assertFalse(resultSet.next());
}

@Test
public void testFindColumn() throws SQLException {
assertEquals(2, subject.findColumn(STRING_COL_NOT_NULL));
Expand Down