Search in sources :

Example 16 with Code

use of com.google.cloud.spanner.Type.Code in project java-spanner-jdbc by googleapis.

the class JdbcResultSet method getShort.

@Override
public short getShort(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) ? (short) 1 : 0);
        case FLOAT64:
            return isNull ? 0 : checkedCastToShort(Double.valueOf(spanner.getDouble(spannerIndex)).longValue());
        case INT64:
            return isNull ? 0 : checkedCastToShort(spanner.getLong(spannerIndex));
        case NUMERIC:
            return isNull ? 0 : checkedCastToShort(spanner.getBigDecimal(spannerIndex));
        case PG_NUMERIC:
            return isNull ? 0 : checkedCastToShort(parseBigDecimal(spanner.getString(spannerIndex)).toBigInteger());
        case STRING:
            return isNull ? 0 : checkedCastToShort(parseLong(spanner.getString(spannerIndex)));
        case BYTES:
        case JSON:
        case DATE:
        case STRUCT:
        case TIMESTAMP:
        case ARRAY:
        default:
            throw createInvalidToGetAs("short", type);
    }
}
Also used : Code(com.google.cloud.spanner.Type.Code)

Example 17 with Code

use of com.google.cloud.spanner.Type.Code in project java-spanner-jdbc by googleapis.

the class JdbcResultSet method getLong.

@Override
public long getLong(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 ? 0L : (spanner.getBoolean(spannerIndex) ? 1L : 0L);
        case FLOAT64:
            return isNull ? 0L : Double.valueOf(spanner.getDouble(spannerIndex)).longValue();
        case INT64:
            return isNull ? 0L : spanner.getLong(spannerIndex);
        case NUMERIC:
            return isNull ? 0 : checkedCastToLong(parseBigDecimal(spanner.getString(spannerIndex)));
        case PG_NUMERIC:
            return isNull ? 0L : checkedCastToLong(parseBigDecimal(spanner.getString(spannerIndex)).toBigInteger());
        case STRING:
            return isNull ? 0L : parseLong(spanner.getString(spannerIndex));
        case BYTES:
        case JSON:
        case DATE:
        case STRUCT:
        case TIMESTAMP:
        case ARRAY:
        default:
            throw createInvalidToGetAs("long", type);
    }
}
Also used : Code(com.google.cloud.spanner.Type.Code)

Example 18 with Code

use of com.google.cloud.spanner.Type.Code in project java-spanner-jdbc by googleapis.

the class JdbcResultSet method getDouble.

@Override
public double getDouble(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) ? (double) 1 : 0);
        case FLOAT64:
            return isNull ? 0 : spanner.getDouble(spannerIndex);
        case INT64:
            return isNull ? 0 : spanner.getLong(spannerIndex);
        case NUMERIC:
            return isNull ? 0 : spanner.getBigDecimal(spannerIndex).doubleValue();
        case PG_NUMERIC:
            return isNull ? 0 : parseDouble(spanner.getString(spannerIndex));
        case STRING:
            return isNull ? 0 : parseDouble(spanner.getString(spannerIndex));
        case BYTES:
        case JSON:
        case DATE:
        case STRUCT:
        case TIMESTAMP:
        case ARRAY:
        default:
            throw createInvalidToGetAs("double", type);
    }
}
Also used : Code(com.google.cloud.spanner.Type.Code)

Example 19 with Code

use of com.google.cloud.spanner.Type.Code in project jans by JanssenProject.

the class SpannerOperationServiceImpl method getAttributeDataList.

private List<AttributeData> getAttributeDataList(String objectClass, ResultSet resultSet, boolean skipDn) throws EntryConvertationException {
    try {
        if ((resultSet == null)) {
            return null;
        }
        if (!resultSet.next()) {
            return null;
        }
        List<AttributeData> result = new ArrayList<AttributeData>();
        // TODO: Include child table columns
        Set<String> nullableColumns = connectionProvider.getTableNullableColumns(objectClass);
        List<StructField> structFields = resultSet.getType().getStructFields();
        int columnsCount = resultSet.getColumnCount();
        for (int i = 0; i < columnsCount; i++) {
            StructField structField = structFields.get(i);
            String attributeName = structField.getName();
            Code columnTypeCode = structField.getType().getCode();
            boolean isNullable = nullableColumns.contains(attributeName.toLowerCase());
            if (SpannerOperationService.DOC_ID.equalsIgnoreCase(attributeName) || SpannerOperationService.ID.equalsIgnoreCase(attributeName)) {
                // Skip internal attributes
                continue;
            }
            if (skipDn && SpannerOperationService.DN.equalsIgnoreCase(attributeName)) {
                // Skip DN attribute
                continue;
            }
            Boolean multiValued = Boolean.FALSE;
            Object[] attributeValueObjects;
            if (resultSet.isNull(i)) {
                attributeValueObjects = NO_OBJECTS;
                if (isNullable) {
                    // Ignore columns with default NULL values
                    continue;
                }
            } else {
                if (Code.ARRAY == columnTypeCode) {
                    attributeValueObjects = convertDbArrayToValue(resultSet, structField.getType().getArrayElementType(), i, attributeName);
                    multiValued = Boolean.TRUE;
                } else if (Code.BOOL == columnTypeCode) {
                    attributeValueObjects = new Object[] { resultSet.getBoolean(i) };
                } else if (Code.DATE == columnTypeCode) {
                    attributeValueObjects = new Object[] { com.google.cloud.Date.toJavaUtilDate(resultSet.getDate(i)) };
                } else if (Code.TIMESTAMP == columnTypeCode) {
                    attributeValueObjects = new Object[] { resultSet.getTimestamp(i).toDate() };
                } else if (Code.INT64 == columnTypeCode) {
                    attributeValueObjects = new Object[] { resultSet.getLong(i) };
                } else if (Code.NUMERIC == columnTypeCode) {
                    attributeValueObjects = new Object[] { resultSet.getBigDecimal(i).longValue() };
                } else if (Code.STRING == columnTypeCode) {
                    Object value = resultSet.getString(i);
                    try {
                        value = com.google.cloud.Timestamp.parseTimestamp(value.toString());
                    } catch (Exception ex) {
                    }
                    attributeValueObjects = new Object[] { value };
                } else {
                    throw new EntryConvertationException(String.format("Column with name '%s' does not contain unsupported type '%s'", attributeName, columnTypeCode));
                }
            }
            unescapeValues(attributeValueObjects);
            AttributeData tmpAttribute = new AttributeData(attributeName, attributeValueObjects, multiValued);
            if (multiValued != null) {
                tmpAttribute.setMultiValued(multiValued);
            }
            result.add(tmpAttribute);
        }
        return result;
    } catch (SpannerException ex) {
        throw new EntryConvertationException("Failed to convert entry!", ex);
    }
}
Also used : ArrayList(java.util.ArrayList) Code(com.google.cloud.spanner.Type.Code) DeleteException(io.jans.orm.exception.operation.DeleteException) IncompatibleTypeException(io.jans.orm.exception.operation.IncompatibleTypeException) PersistenceException(io.jans.orm.exception.operation.PersistenceException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) EntryConvertationException(io.jans.orm.exception.operation.EntryConvertationException) DuplicateEntryException(io.jans.orm.exception.operation.DuplicateEntryException) SpannerException(com.google.cloud.spanner.SpannerException) SearchException(io.jans.orm.exception.operation.SearchException) EntryNotFoundException(io.jans.orm.exception.operation.EntryNotFoundException) ValueWithStructField(io.jans.orm.cloud.spanner.model.ValueWithStructField) StructField(com.google.cloud.spanner.Type.StructField) EntryConvertationException(io.jans.orm.exception.operation.EntryConvertationException) SpannerException(com.google.cloud.spanner.SpannerException) AttributeData(io.jans.orm.model.AttributeData)

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