use of com.google.cloud.spanner.Type.Code in project java-spanner-jdbc by googleapis.
the class JdbcResultSet method getFloat.
@Override
public float getFloat(int columnIndex) throws SQLException {
checkClosedAndValidRow();
boolean isNull = isNull(columnIndex);
int spannerIndex = columnIndex - 1;
Code type = spanner.getColumnType(spannerIndex).getCode();
switch(type) {
case BOOL:
return isNull ? 0 : (spanner.getBoolean(spannerIndex) ? (float) 1 : 0);
case FLOAT64:
return isNull ? 0 : checkedCastToFloat(spanner.getDouble(spannerIndex));
case INT64:
return isNull ? 0 : checkedCastToFloat(spanner.getLong(spannerIndex));
case NUMERIC:
return isNull ? 0 : spanner.getBigDecimal(spannerIndex).floatValue();
case PG_NUMERIC:
return isNull ? 0 : parseFloat(spanner.getString(spannerIndex));
case STRING:
return isNull ? 0 : checkedCastToFloat(parseDouble(spanner.getString(spannerIndex)));
case BYTES:
case JSON:
case DATE:
case STRUCT:
case TIMESTAMP:
case ARRAY:
default:
throw createInvalidToGetAs("float", type);
}
}
use of com.google.cloud.spanner.Type.Code in project java-spanner-jdbc by googleapis.
the class JdbcResultSet method getString.
@Override
public String getString(int columnIndex) throws SQLException {
checkClosedAndValidRow();
boolean isNull = isNull(columnIndex);
int spannerIndex = columnIndex - 1;
Code type = spanner.getColumnType(spannerIndex).getCode();
switch(type) {
case BOOL:
return isNull ? null : String.valueOf(spanner.getBoolean(spannerIndex));
case BYTES:
return isNull ? null : spanner.getBytes(spannerIndex).toBase64();
case DATE:
return isNull ? null : spanner.getDate(spannerIndex).toString();
case FLOAT64:
return isNull ? null : Double.toString(spanner.getDouble(spannerIndex));
case INT64:
return isNull ? null : Long.toString(spanner.getLong(spannerIndex));
case NUMERIC:
return isNull ? null : spanner.getBigDecimal(spannerIndex).toString();
case PG_NUMERIC:
return isNull ? null : spanner.getString(spannerIndex);
case STRING:
return isNull ? null : spanner.getString(spannerIndex);
case JSON:
return isNull ? null : spanner.getJson(spannerIndex);
case TIMESTAMP:
return isNull ? null : spanner.getTimestamp(spannerIndex).toString();
case STRUCT:
case ARRAY:
default:
throw createInvalidToGetAs("string", type);
}
}
use of com.google.cloud.spanner.Type.Code in project java-spanner-jdbc by googleapis.
the class JdbcResultSet method getDate.
@Override
public Date getDate(int columnIndex) throws SQLException {
checkClosedAndValidRow();
boolean isNull = isNull(columnIndex);
int spannerIndex = columnIndex - 1;
Code type = spanner.getColumnType(spannerIndex).getCode();
switch(type) {
case DATE:
return isNull ? null : JdbcTypeConverter.toSqlDate(spanner.getDate(spannerIndex));
case STRING:
return isNull ? null : parseDate(spanner.getString(spannerIndex));
case TIMESTAMP:
return isNull ? null : new Date(spanner.getTimestamp(spannerIndex).toSqlTimestamp().getTime());
case BOOL:
case FLOAT64:
case INT64:
case NUMERIC:
case PG_NUMERIC:
case BYTES:
case JSON:
case STRUCT:
case ARRAY:
default:
throw createInvalidToGetAs("date", type);
}
}
use of com.google.cloud.spanner.Type.Code in project java-spanner-jdbc by googleapis.
the class JdbcResultSet method getBoolean.
@Override
public boolean getBoolean(int columnIndex) throws SQLException {
checkClosedAndValidRow();
boolean isNull = isNull(columnIndex);
int spannerIndex = columnIndex - 1;
Code type = spanner.getColumnType(spannerIndex).getCode();
switch(type) {
case BOOL:
return !isNull && spanner.getBoolean(spannerIndex);
case FLOAT64:
return !isNull && spanner.getDouble(spannerIndex) != 0D;
case INT64:
return !isNull && spanner.getLong(spannerIndex) != 0L;
case NUMERIC:
return !isNull && !spanner.getBigDecimal(spannerIndex).equals(BigDecimal.ZERO);
case PG_NUMERIC:
return !isNull && !spanner.getString(spannerIndex).equals("0");
case STRING:
return !isNull && Boolean.parseBoolean(spanner.getString(spannerIndex));
case BYTES:
case JSON:
case DATE:
case STRUCT:
case TIMESTAMP:
case ARRAY:
default:
throw createInvalidToGetAs("boolean", type);
}
}
use of com.google.cloud.spanner.Type.Code in project java-spanner-jdbc by googleapis.
the class JdbcResultSet method getBigDecimal.
private BigDecimal getBigDecimal(int columnIndex, boolean fixedScale, int scale) throws SQLException {
int spannerIndex = columnIndex - 1;
Code type = spanner.getColumnType(spannerIndex).getCode();
boolean isNull = isNull(columnIndex);
BigDecimal res;
switch(type) {
case BOOL:
res = isNull ? null : (spanner.getBoolean(columnIndex - 1) ? BigDecimal.ONE : BigDecimal.ZERO);
break;
case FLOAT64:
res = isNull ? null : BigDecimal.valueOf(spanner.getDouble(spannerIndex));
break;
case INT64:
res = isNull ? null : BigDecimal.valueOf(spanner.getLong(spannerIndex));
break;
case NUMERIC:
res = isNull ? null : spanner.getBigDecimal(spannerIndex);
break;
case PG_NUMERIC:
res = isNull ? null : parseBigDecimal(spanner.getString(spannerIndex));
break;
case STRING:
try {
res = isNull ? null : new BigDecimal(spanner.getString(spannerIndex));
break;
} catch (NumberFormatException e) {
throw JdbcSqlExceptionFactory.of("The column does not contain a valid BigDecimal", com.google.rpc.Code.INVALID_ARGUMENT, e);
}
case BYTES:
case JSON:
case DATE:
case TIMESTAMP:
case STRUCT:
case ARRAY:
default:
throw createInvalidToGetAs("BigDecimal", type);
}
if (res != null && fixedScale) {
res = res.setScale(scale, RoundingMode.HALF_UP);
}
return res;
}
Aggregations