Skip to content

Commit

Permalink
Merge pull request #185 from looker-open-source/goomrw-get-by-label
Browse files Browse the repository at this point in the history
Fix `findColumn` and `get*(String label)` methods.
  • Loading branch information
goomrw authored Dec 20, 2023
2 parents fd1165e + 30350ea commit 94614a9
Show file tree
Hide file tree
Showing 7 changed files with 356 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -581,28 +586,10 @@ public void deleteRow() throws SQLException {
throw new BQSQLFeatureNotSupportedException("deleteRow()");
}

/**
*
*
* <h1>Implementation Details:</h1>
*
* <br>
* Not implemented yet.
*
* @throws BQSQLException
*/
/** {@inheritDoc} */
@Override
public int findColumn(String columnLabel) throws SQLException {
if (this.isClosed()) {
throw new BQSQLException("This Resultset is Closed");
}
int columncount = this.getMetaData().getColumnCount();
for (int i = 1; i <= columncount; i++) {
if (this.getMetaData().getCatalogName(i).equals(columnLabel)) {
return i;
}
}
throw new BQSQLException("No Such column labeled: " + columnLabel);
return CommonResultSet.findColumn(columnLabel, getMetaData());
}

/**
Expand Down
12 changes: 1 addition & 11 deletions src/main/java/net/starschema/clouddb/jdbc/BQResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,7 @@ public BQResultSet(
/** {@inheritDoc} */
@Override
public int findColumn(String columnLabel) throws SQLException {
if (this.isClosed()) {
throw new BQSQLException("This Resultset is Closed");
}
int columncount = this.getMetaData().getColumnCount();
for (int i = 1; i <= columncount; i++) {
if (this.getMetaData().getCatalogName(i).equals(columnLabel)) {
return i;
}
}
SQLException e = new BQSQLException("No Such column labeled: " + columnLabel);
throw e;
return CommonResultSet.findColumn(columnLabel, getMetaData());
}

/** {@inheritDoc} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,7 @@ public BQScrollableResultSet(
/** {@inheritDoc} */
@Override
public int findColumn(String columnLabel) throws SQLException {
if (this.isClosed()) {
throw new BQSQLException("This Resultset is Closed");
}
int columncount = this.getMetaData().getColumnCount();
for (int i = 1; i <= columncount; i++) {
if (this.getMetaData().getCatalogName(i).equals(columnLabel)) {
return i;
}
}
throw new BQSQLException("No Such column labeled: " + columnLabel);
return CommonResultSet.findColumn(columnLabel, getMetaData());
}

/** {@inheritDoc} */
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/net/starschema/clouddb/jdbc/CommonResultSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package net.starschema.clouddb.jdbc;

import com.google.cloud.bigquery.BigQuerySQLException;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

/** A static utility class of methods common to all {@link java.sql.ResultSet} implementations. */
class CommonResultSet {

/**
* A common implementation of {@link java.sql.ResultSet#findColumn(String)}
*
* @param label the column-to-find's label
* @param metadata the metadata from the {@link java.sql.ResultSet}
* @return the integer index of the labeled column, if it's found
* @throws SQLException if no column with the given label is found
*/
static int findColumn(final String label, final ResultSetMetaData metadata) throws SQLException {
int columnCount = metadata.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
if (metadata.getColumnLabel(column).equals(label)) {
return column;
}
}
throw new BigQuerySQLException("No such Column labeled: " + label);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.gson.Gson;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
Expand Down Expand Up @@ -58,7 +59,7 @@
* @author Horváth Attila
* @author Gunics Balazs
*/
public class BQForwardOnlyResultSetFunctionTest {
public class BQForwardOnlyResultSetFunctionTest extends CommonTestsForResultSets {

private static java.sql.Connection con = null;
private java.sql.ResultSet resultForTest = null;
Expand Down Expand Up @@ -845,4 +846,10 @@ public void testStatelessQuery() throws SQLException {
Assertions.assertThat(bqResultSet.getJobId()).isNull();
Assertions.assertThat(bqResultSet.getQueryId()).contains("!");
}

@Override
protected Statement createStatementForCommonTests(final Connection connection)
throws SQLException {
return connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.google.api.client.testing.http.MockHttpTransport;
import com.google.api.client.testing.http.MockLowLevelHttpResponse;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
Expand All @@ -31,6 +32,7 @@
import org.assertj.core.api.Assertions;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -41,7 +43,7 @@
* @author Horváth Attila
* @author Gunics Balazs
*/
public class BQScrollableResultSetFunctionTest {
public class BQScrollableResultSetFunctionTest extends CommonTestsForResultSets {

private static java.sql.Connection con = null;
private static java.sql.ResultSet Result = null;
Expand Down Expand Up @@ -745,4 +747,31 @@ public void testStatelessQuery() throws SQLException {
Assertions.assertThat(bqResultSet.getJobId()).isNull();
Assertions.assertThat(bqResultSet.getQueryId()).contains("!");
}

@Override
protected Statement createStatementForCommonTests(Connection connection) throws SQLException {
return connection.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
}

@Override
@Test
@Ignore(
"Contradiction between BQSCrollableResultSet and BQForwardOnlyResultSet "
+ "dates and times: b/317107706")
public void testGetTimeByLabel() throws SQLException {}

@Override
@Test
@Ignore(
"Contradiction between BQSCrollableResultSet and BQForwardOnlyResultSet "
+ "dates and times: b/317107706")
public void testGetDateByLabel() throws SQLException {}

@Override
@Test
@Ignore(
"Contradiction between BQSCrollableResultSet and BQForwardOnlyResultSet "
+ "dates and times: b/317107706")
public void testGetTimestampByLabel() throws SQLException {}
}
Loading

0 comments on commit 94614a9

Please sign in to comment.