Search in sources :

Example 1 with Code

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

the class SpannerOperationServiceImpl method removeMutationBuilderValue.

private void removeMutationBuilderValue(WriteBuilder mutation, AttributeData attribute, StructField attributeType) throws EntryConvertationException {
    ValueBinder<WriteBuilder> valueBinder = mutation.set(attributeType.getName());
    Code typeCode = attributeType.getType().getCode();
    if (Code.BOOL == typeCode) {
        valueBinder.to((Boolean) null);
    } else if (Code.DATE == typeCode) {
        valueBinder.to((com.google.cloud.Date) null);
    } else if (Code.TIMESTAMP == typeCode) {
        valueBinder.to((com.google.cloud.Timestamp) null);
    } else if (Code.INT64 == typeCode) {
        valueBinder.to((Long) null);
    } else if (Code.NUMERIC == typeCode) {
        valueBinder.to((BigDecimal) null);
    } else if (Code.STRING == typeCode) {
        valueBinder.to((String) null);
    } else if (Code.ARRAY == typeCode) {
        Code arrayCode = attributeType.getType().getArrayElementType().getCode();
        if (Code.BOOL == arrayCode) {
            valueBinder.toBoolArray((boolean[]) null);
        } else if (Code.DATE == arrayCode) {
            valueBinder.toDateArray((List<com.google.cloud.Date>) null);
        } else if (Code.TIMESTAMP == arrayCode) {
            valueBinder.toTimestampArray((List<com.google.cloud.Timestamp>) null);
        } else if (Code.INT64 == arrayCode) {
            valueBinder.toInt64Array((long[]) null);
        } else if (Code.NUMERIC == arrayCode) {
            valueBinder.toNumericArray((List<BigDecimal>) null);
        } else if (Code.STRING == arrayCode) {
            valueBinder.toStringArray((List<String>) null);
        }
    } else {
        throw new EntryConvertationException(String.format("Array column with name '%s' does not contain supported type '%s'", attribute.getName(), attributeType));
    }
}
Also used : Code(com.google.cloud.spanner.Type.Code) Date(java.util.Date) BigDecimal(java.math.BigDecimal) WriteBuilder(com.google.cloud.spanner.Mutation.WriteBuilder) List(java.util.List) ArrayList(java.util.ArrayList) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) LinkedList(java.util.LinkedList) EntryConvertationException(io.jans.orm.exception.operation.EntryConvertationException)

Example 2 with Code

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

the class SpannerFilterConverter method isMultiValue.

protected Boolean isMultiValue(TableMapping tableMapping, String attributeName, Filter currentGenericFilter, Map<String, PropertyAnnotation> propertiesAnnotationsMap) throws SearchException {
    StructField structField = getStructField(tableMapping, attributeName);
    boolean hasChildTableForAttribute = tableMapping.hasChildTableForAttribute(attributeName.toLowerCase());
    Code columnTypeCode = structField.getType().getCode();
    if ((Code.ARRAY == columnTypeCode) || hasChildTableForAttribute) {
        boolean multiValuedDetected = (Boolean.TRUE.equals(currentGenericFilter.getMultiValued()) || Boolean.TRUE.equals(determineMultiValuedByType(currentGenericFilter.getAttributeName(), propertiesAnnotationsMap)));
        if (!multiValuedDetected) {
            LOG.warn(String.format("Сolumn name '%s' was defined as multi valued in table/child table '%s' but detected as single value", attributeName, tableMapping.getTableName()));
        }
        return true;
    }
    return false;
}
Also used : ValueWithStructField(io.jans.orm.cloud.spanner.model.ValueWithStructField) StructField(com.google.cloud.spanner.Type.StructField) Code(com.google.cloud.spanner.Type.Code)

Example 3 with Code

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

the class JdbcResultSet method getTimestamp.

@Override
public Timestamp getTimestamp(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.toSqlTimestamp(spanner.getDate(spannerIndex));
        case STRING:
            return isNull ? null : parseTimestamp(spanner.getString(spannerIndex));
        case TIMESTAMP:
            return isNull ? null : JdbcTypeConverter.toSqlTimestamp(spanner.getTimestamp(spannerIndex));
        case BOOL:
        case FLOAT64:
        case INT64:
        case NUMERIC:
        case PG_NUMERIC:
        case BYTES:
        case JSON:
        case STRUCT:
        case ARRAY:
        default:
            throw createInvalidToGetAs("timestamp", type);
    }
}
Also used : Code(com.google.cloud.spanner.Type.Code)

Example 4 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 5 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)

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