diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryResultSetImpl.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryResultSetImpl.java index cfb5db442..faaacd835 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryResultSetImpl.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryResultSetImpl.java @@ -17,8 +17,6 @@ package com.google.cloud.bigquery; import com.google.api.services.bigquery.model.TableRow; -import java.io.InputStream; -import java.io.Reader; import java.math.BigDecimal; import java.sql.*; import java.util.Map; @@ -31,7 +29,7 @@ public class BigQueryResultSetImpl extends AbstractJdbcResultSet private final Schema schema; private final long totalRows; // private final ResultSet nextRow; - private final Map nameType; + private final Map nameType; // TODO: Remove private final BlockingQueue buffer; // TableRow private T cursor; @@ -79,197 +77,98 @@ public Object getObject(String fieldName) throws SQLException { if (cursor instanceof TableRow) { TableRow currentRow = (TableRow) cursor; if (currentRow == null) { - throw new SQLException("No rows left"); // TODO rephrase exception message + throw new SQLException("No rows left"); } return currentRow.get(fieldName); } - // TODO: Add similar clauses for Apache Arrow and Avro + // TODO: Add similar clauses for Apache Arrow return null; } @Override public String getString(String fieldName) throws SQLException { Object value = getObject(fieldName); - if (value == null || !(value instanceof String)) { - return null; + 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; } } - /** ****TODO Methods - to be impemented****** */ @Override - public long getLong(String columnLabel) throws SQLException { - // TODO: Implement the logic - throw new RuntimeException("Not implemented"); - } - - @Override - public float getFloat(String columnLabel) throws SQLException { - // TODO: Implement the logic - throw new RuntimeException("Not implemented"); - } - - @Override - public double getDouble(String columnLabel) throws SQLException { - // TODO: Implement the logic - throw new RuntimeException("Not implemented"); - } - - @Override - public BigDecimal getBigDecimal(String columnLabel, int scale) throws SQLException { - // TODO: Implement the logic - throw new RuntimeException("Not implemented"); - } - - @Override - public void close() throws SQLException { - // TODO: Implement the logic - throw new RuntimeException("Not implemented"); - } - - @Override - public boolean wasNull() throws SQLException { - // TODO: Implement the logic - throw new RuntimeException("Not implemented"); - } - - @Override - public String getString(int columnIndex) throws SQLException { - // TODO: Implement the logic - throw new RuntimeException("Not implemented"); - } - - @Override - public boolean getBoolean(int columnIndex) throws SQLException { - // TODO: Implement the logic - throw new RuntimeException("Not implemented"); - } - - @Override - public byte getByte(int columnIndex) throws SQLException { - // TODO: Implement the logic - throw new RuntimeException("Not implemented"); - } - - @Override - public short getShort(int columnIndex) throws SQLException { - // TODO: Implement the logic - throw new RuntimeException("Not implemented"); - } - - @Override - public int getInt(int columnIndex) throws SQLException { - // TODO: Implement the logic - throw new RuntimeException("Not implemented"); - } - - @Override - public long getLong(int columnIndex) throws SQLException { - // TODO: Implement the logic - throw new RuntimeException("Not implemented"); - } - - @Override - public float getFloat(int columnIndex) throws SQLException { - // TODO: Implement the logic - throw new RuntimeException("Not implemented"); - } - - @Override - public double getDouble(int columnIndex) throws SQLException { - // TODO: Implement the logic - throw new RuntimeException("Not implemented"); - } - - @Override - public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException { - // TODO: Implement the logic - throw new RuntimeException("Not implemented"); - } - - @Override - public byte[] getBytes(int columnIndex) throws SQLException { - // TODO: Implement the logic - throw new RuntimeException("Not implemented"); - } - - @Override - public Date getDate(int columnIndex) throws SQLException { - // TODO: Implement the logic - throw new RuntimeException("Not implemented"); - } - - @Override - public Time getTime(int columnIndex) throws SQLException { - // TODO: Implement the logic - throw new RuntimeException("Not implemented"); - } - - @Override - public Timestamp getTimestamp(int columnIndex) throws SQLException { - // TODO: Implement the logic - throw new RuntimeException("Not implemented"); - } - - @Override - public InputStream getAsciiStream(int columnIndex) throws SQLException { - // TODO: Implement the logic - throw new RuntimeException("Not implemented"); - } - - @Override - public InputStream getUnicodeStream(int columnIndex) throws SQLException { - // TODO: Implement the logic - throw new RuntimeException("Not implemented"); - } - - @Override - public InputStream getBinaryStream(int columnIndex) throws SQLException { - // TODO: Implement the logic - throw new RuntimeException("Not implemented"); + 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 int findColumn(String columnLabel) throws SQLException { - // TODO: Implement the logic - throw new RuntimeException("Not implemented"); + 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 Reader getCharacterStream(int columnIndex) throws SQLException { - // TODO: Implement the logic - throw new RuntimeException("Not implemented"); + 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 Reader getCharacterStream(String columnLabel) throws SQLException { - // TODO: Implement the logic - throw new RuntimeException("Not implemented"); + 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(int columnIndex) throws SQLException { - // TODO: Implement the logic - throw new RuntimeException("Not implemented"); + 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 BigDecimal getBigDecimal(String columnLabel) throws SQLException { - // TODO: Implement the logic - throw new RuntimeException("Not implemented"); - } - - /* - TODO: Create methods to meet the following signature? - while(resultSet.next()) { - - resultSet.getString ("name"); - resultSet.getInt ("age"); - resultSet.getBigDecimal("coefficient"); - - // etc. + 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)); + } } - */ - }