Search in sources :

Example 6 with Code

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);
    }
}
Also used : Code(com.google.cloud.spanner.Type.Code)

Example 7 with Code

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);
    }
}
Also used : Code(com.google.cloud.spanner.Type.Code)

Example 8 with Code

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);
    }
}
Also used : Code(com.google.cloud.spanner.Type.Code) Date(java.sql.Date)

Example 9 with Code

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);
    }
}
Also used : Code(com.google.cloud.spanner.Type.Code)

Example 10 with Code

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;
}
Also used : Code(com.google.cloud.spanner.Type.Code) BigDecimal(java.math.BigDecimal)

Aggregations

Code (com.google.cloud.spanner.Type.Code)19 StructField (com.google.cloud.spanner.Type.StructField)2 ValueWithStructField (io.jans.orm.cloud.spanner.model.ValueWithStructField)2 EntryConvertationException (io.jans.orm.exception.operation.EntryConvertationException)2 BigDecimal (java.math.BigDecimal)2 Date (java.sql.Date)2 ArrayList (java.util.ArrayList)2 WriteBuilder (com.google.cloud.spanner.Mutation.WriteBuilder)1 SpannerException (com.google.cloud.spanner.SpannerException)1 Type (com.google.cloud.spanner.Type)1 Value (com.google.cloud.spanner.Value)1 DeleteException (io.jans.orm.exception.operation.DeleteException)1 DuplicateEntryException (io.jans.orm.exception.operation.DuplicateEntryException)1 EntryNotFoundException (io.jans.orm.exception.operation.EntryNotFoundException)1 IncompatibleTypeException (io.jans.orm.exception.operation.IncompatibleTypeException)1 PersistenceException (io.jans.orm.exception.operation.PersistenceException)1 SearchException (io.jans.orm.exception.operation.SearchException)1 AttributeData (io.jans.orm.model.AttributeData)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Date (java.util.Date)1