Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
update javadoc

Removing getInt from AbstractJdbcResultSet

Removing getInt from AbstractJdbcResultSet

Implemented getInt

Added getResultSet method, removed unimplemented methods

Wrapped ResultSet Implementation and exposed getResultSet method

Modified executeSelect to Return BigQueryDryRunResult

Modified executeSelect to Return BigQueryDryRunResult

Modified executeSelect to Return BigQueryDryRunResult

update javadoc
  • Loading branch information
stephaniewang526 committed Sep 24, 2021
1 parent 3595799 commit e2dc6b9
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 142 deletions.
Expand Up @@ -932,11 +932,6 @@ public short getShort(String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException();
}

@Override
public int getInt(String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException();
}

@Override
public byte[] getBytes(String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException();
Expand Down
Expand Up @@ -16,7 +16,7 @@

package com.google.cloud.bigquery;

import java.sql.SQLException;
import java.sql.ResultSet;

public interface BigQueryResultSet<T> {

Expand All @@ -29,12 +29,6 @@ public interface BigQueryResultSet<T> {
*/
long getTotalRows();

/** Returns the next row. Null if there is no more rows left. */
// ResultSet getNext();

/*Advances the result set to the next row, returning false if no such row exists. Potentially blocking operation*/
boolean next() throws SQLException;

/*Returns the value of a String field if the field exists, otherwise returns null*/
String getString(String fieldName) throws SQLException;
/*Returns the The underlying ResultSet Implementation*/
ResultSet getResultSet();
}
Expand Up @@ -18,28 +18,26 @@

import com.google.api.services.bigquery.model.TableRow;
import java.math.BigDecimal;
import java.sql.*;
import java.util.Map;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.concurrent.BlockingQueue;

// TODO: This implementation deals with the JSON response. We can have respective implementations
public class BigQueryResultSetImpl<T> extends AbstractJdbcResultSet
implements BigQueryResultSet<T> {
public class BigQueryResultSetImpl<T> implements BigQueryResultSet<T> {

private final Schema schema;
private final long totalRows;
// private final ResultSet nextRow;
private final Map<String, String> nameType; // TODO: Remove
private final BlockingQueue<T> buffer; // TableRow
private final BlockingQueue<T> buffer;
private T cursor;
private ResultSetWrapper underlyingResultSet = null;

// TODO : Implement a wrapper/struct like spanner
public BigQueryResultSetImpl(
Schema schema, long totalRows, Map<String, String> nameType, BlockingQueue<T> buffer) {
public BigQueryResultSetImpl(Schema schema, long totalRows, BlockingQueue<T> buffer) {
this.schema = schema;
this.totalRows = totalRows;
this.nameType = nameType;
this.buffer = buffer;
this.underlyingResultSet = new ResultSetWrapper();
}

@Override
Expand All @@ -52,123 +50,139 @@ public long getTotalRows() {
return totalRows;
}

/* @Override
public ResultSet getNext() {
return null;
}*/

@Override
/*Advances the result set to the next row, returning false if no such row exists. Potentially blocking operation*/
public boolean next() throws SQLException {
if (buffer.peek() == null) { // producer has no more rows left.
return false;
}
try {
cursor = buffer.take(); // advance the cursor,Potentially blocking operation
} catch (InterruptedException e) {
throw new SQLException("No rows left");
}

return true;
public ResultSet getResultSet() {
return underlyingResultSet;
}

@Override
public Object getObject(String fieldName) throws SQLException {
if (cursor instanceof TableRow) {
TableRow currentRow = (TableRow) cursor;
if (currentRow == null) {
private class ResultSetWrapper extends AbstractJdbcResultSet {
@Override
/*Advances the result set to the next row, returning false if no such row exists. Potentially blocking operation*/
public boolean next() throws SQLException {
if (buffer.peek() == null) { // producer has no more rows left.
return false;
}
try {
cursor = buffer.take(); // advance the cursor,Potentially blocking operation
} catch (InterruptedException e) {
throw new SQLException("No rows left");
}
return currentRow.get(fieldName);

return true;
}
// TODO: Add similar clauses for Apache Arrow
return null;
}

@Override
public String getString(String fieldName) throws SQLException {
Object value = getObject(fieldName);
if (value == null) {
throw new SQLException("fieldName can't be null");
} else if (!(value instanceof String)) {
throw new SQLException("value not in instance of String");
} else {
return (String) value;
@Override
public Object getObject(String fieldName) throws SQLException {
if (cursor instanceof TableRow) {
TableRow currentRow = (TableRow) cursor;
if (currentRow == null) {
throw new SQLException("No rows left");
}
return currentRow.get(fieldName);
}
// TODO: Add similar clauses for Apache Arrow
return null;
}
}

@Override
public long getLong(String fieldName) throws SQLException {
Object value = getObject(fieldName);
if (value == null) {
throw new SQLException("fieldName can't be null");
} else if (!(value instanceof Long)) {
throw new SQLException("value not in instance of Long");
} else {
return Long.parseLong(String.valueOf(value));
@Override
public String getString(String fieldName) throws SQLException {
Object value = getObject(fieldName);
if (value == null) {
throw new SQLException("fieldName can't be null");
} else if (!(value instanceof String)) {
throw new SQLException("value not in instance of String");
} else {
return (String) value;
}
}
}

@Override
public double getDouble(String fieldName) throws SQLException {
Object value = getObject(fieldName);
if (value == null) {
throw new SQLException("fieldName can't be null");
} else if (!(value instanceof Double)) {
throw new SQLException("value not in instance of Double");
} else {
return Double.parseDouble(String.valueOf(value));
@Override
public long getLong(String fieldName) throws SQLException {
Object value = getObject(fieldName);
if (value == null) {
throw new SQLException("fieldName can't be null");
} else if (!(value instanceof Long)) {
throw new SQLException("value not in instance of Long");
} else {
return Long.parseLong(String.valueOf(value));
}
}
}

@Override
public BigDecimal getBigDecimal(String fieldName) throws SQLException {
Object value = getObject(fieldName);
if (value == null) {
throw new SQLException("fieldName can't be null");
} else if (!(value instanceof Long
|| value instanceof Double
|| value instanceof BigDecimal
|| value instanceof String)) {
throw new SQLException("value cannot be converted to BigDecimal");
} else {
return new BigDecimal(String.valueOf(value));
@Override
public double getDouble(String fieldName) throws SQLException {
Object value = getObject(fieldName);
if (value == null) {
throw new SQLException("fieldName can't be null");
} else if (!(value instanceof Double)) {
throw new SQLException("value not in instance of Double");
} else {
return Double.parseDouble(String.valueOf(value));
}
}
}

@Override
public boolean getBoolean(String fieldName) throws SQLException {
Object value = getObject(fieldName);
if (value == null) {
throw new SQLException("fieldName can't be null");
} else if (!(value instanceof Boolean || (value instanceof String))) {
throw new SQLException("value not in instance of Boolean");
} else {
return Boolean.parseBoolean(String.valueOf(value));
@Override
public BigDecimal getBigDecimal(String fieldName) throws SQLException {
Object value = getObject(fieldName);
if (value == null) {
throw new SQLException("fieldName can't be null");
} else if (!(value instanceof Long
|| value instanceof Double
|| value instanceof BigDecimal
|| value instanceof String)) {
throw new SQLException("value cannot be converted to BigDecimal");
} else {
return new BigDecimal(String.valueOf(value));
}
}
}

@Override
public byte getByte(String fieldName) throws SQLException {
Object value = getObject(fieldName);
if (value == null) {
throw new SQLException("fieldName can't be null");
} else if (!(value instanceof Byte || (value instanceof String))) {
throw new SQLException("value not in instance of Boolean");
} else {
return Byte.parseByte(String.valueOf(value));
@Override
public boolean getBoolean(String fieldName) throws SQLException {
Object value = getObject(fieldName);
if (value == null) {
throw new SQLException("fieldName can't be null");
} else if (!(value instanceof Boolean || (value instanceof String))) {
throw new SQLException("value not in instance of Boolean");
} else {
return Boolean.parseBoolean(String.valueOf(value));
}
}
}

@Override
public Timestamp getTimestamp(String fieldName) throws SQLException {
Object value = getObject(fieldName);
if (value == null) {
throw new SQLException("fieldName can't be null");
} else if (!(value instanceof Long || value instanceof Timestamp || value instanceof String)) {
throw new SQLException("value cannot be converted to Timestamp");
} else {
return Timestamp.valueOf(String.valueOf(value));
@Override
public byte getByte(String fieldName) throws SQLException {
Object value = getObject(fieldName);
if (value == null) {
throw new SQLException("fieldName can't be null");
} else if (!(value instanceof Byte || (value instanceof String))) {
throw new SQLException("value not in instance of Boolean");
} else {
return Byte.parseByte(String.valueOf(value));
}
}

@Override
public Timestamp getTimestamp(String fieldName) throws SQLException {
Object value = getObject(fieldName);
if (value == null) {
throw new SQLException("fieldName can't be null");
} else if (!(value instanceof Long
|| value instanceof Timestamp
|| value instanceof String)) {
throw new SQLException("value cannot be converted to Timestamp");
} else {
return Timestamp.valueOf(String.valueOf(value));
}
}

@Override
public int getInt(String fieldName) throws SQLException {
Object value = getObject(fieldName);
if (value == null) {
throw new SQLException("fieldName can't be null");
} else if (!(value instanceof Integer || value instanceof String)) {
throw new SQLException("value cannot be converted to int");
} else {
return Integer.parseInt(String.valueOf(value));
}
}
}
}
Expand Up @@ -17,7 +17,6 @@
package com.google.cloud.bigquery;

import com.google.api.services.bigquery.model.QueryParameter;
import java.sql.ResultSet;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -55,7 +54,8 @@ public interface Connection {
* // .build();
* // Connection connection = bigquery.createConnection(connectionSettings);
* String selectQuery = "SELECT corpus FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;";
* try (ResultSet rs = connection.executeSelect(selectQuery)) {
* try (BigQueryResultSet bqResultSet = connection.executeSelect(selectQuery)) {
* ResultSet rs = bqResultSet.getResultSet();
* while (rs.next()) {
* System.out.printf("%s,", rs.getString("corpus"));
* }
Expand All @@ -69,7 +69,7 @@ public interface Connection {
* @return a ResultSet that contains the data produced by the query
* @exception BigQuerySQLException if a database access error occurs
*/
ResultSet executeSelect(String sql) throws BigQuerySQLException;
BigQueryResultSet executeSelect(String sql) throws BigQuerySQLException;

/**
* Execute a SQL statement with query parameters that returns a single ResultSet
Expand All @@ -86,6 +86,7 @@ public interface Connection {
* @return a ResultSet that contains the data produced by the query
* @exception BigQuerySQLException if a database access error occurs
*/
ResultSet executeSelect(String sql, List<QueryParameter> parameters, Map<String, String> labels)
BigQueryResultSet executeSelect(
String sql, List<QueryParameter> parameters, Map<String, String> labels)
throws BigQuerySQLException;
}

0 comments on commit e2dc6b9

Please sign in to comment.