Search in sources :

Example 1 with StructField

use of com.google.cloud.spanner.Type.StructField in project spanner-jdbc by olavloite.

the class CloudSpannerResultSetMetaDataTest method getFooTestResultSet.

private CloudSpannerResultSet getFooTestResultSet(CloudSpannerStatement statement) throws SQLException {
    List<Struct> rows = new ArrayList<>(4);
    for (int row = 1; row <= 4; row++) {
        Struct.Builder builder = Struct.newBuilder();
        for (TestColumn col : TEST_COLUMNS) {
            builder.set(col.name).to(getDefaultValue(col.type, row));
        }
        rows.add(builder.build());
    }
    StructField[] fields = new StructField[TEST_COLUMNS.size()];
    int index = 0;
    for (TestColumn col : TEST_COLUMNS) {
        fields[index] = StructField.of(col.name, col.type);
        index++;
    }
    ResultSet rs = ResultSets.forRows(Type.struct(fields), rows);
    return new CloudSpannerResultSet(statement, rs, SELECT_ALL_FROM_FOO);
}
Also used : StructField(com.google.cloud.spanner.Type.StructField) ArrayList(java.util.ArrayList) ResultSet(com.google.cloud.spanner.ResultSet) Struct(com.google.cloud.spanner.Struct)

Example 2 with StructField

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

the class SpannerOperationServiceImpl method buildSelectAttributes.

private List<SelectItem> buildSelectAttributes(TableMapping tableMapping, String key, String... attributes) throws SearchException {
    String tableName = tableMapping.getTableName();
    Map<String, StructField> columTypes = tableMapping.getColumTypes();
    // Table alias for columns
    // Column dn
    Column selectDnColumn = new Column(tableAlias, DN);
    SelectExpressionItem selectDnItem = new SelectExpressionItem(selectDnColumn);
    // Column doc_id
    Column selectDocIdColumn = new Column(tableAlias, DOC_ID);
    SelectExpressionItem selectDocIdItem = new SelectExpressionItem(selectDocIdColumn);
    if (ArrayHelper.isEmpty(attributes)) {
        // Select all columns
        AllTableColumns allColumns = new AllTableColumns(tableAlias);
        List<SelectItem> selectColumns = new ArrayList<SelectItem>();
        selectColumns.add(allColumns);
        // Add columns from child tables
        List<SelectExpressionItem> selectChildColumns = buildSelectAttributeFromChildTables(tableName);
        selectColumns.addAll(selectChildColumns);
        return selectColumns;
    } else if ((attributes.length == 1) && StringHelper.isEmpty(attributes[0])) {
        // Compatibility with base persistence layer when application pass attributes new String[] { "" }
        List<SelectItem> selectColumns = Arrays.asList(selectDnItem, selectDocIdItem);
        // Add columns from child tables
        List<SelectExpressionItem> selectChildColumns = buildSelectAttributeFromChildTables(tableName);
        selectColumns.addAll(selectChildColumns);
        return selectColumns;
    }
    List<SelectItem> expresisons = new ArrayList<SelectItem>(attributes.length + 2);
    boolean hasDn = false;
    for (String attributeName : attributes) {
        StructField attributeType = columTypes.get(attributeName.toLowerCase());
        SelectExpressionItem selectExpressionItem;
        // If column not inside table we should check if there is child table
        if (attributeType == null) {
            TableMapping childTableMapping = connectionProvider.getChildTableMappingByKey(key, tableMapping, attributeName);
            if (childTableMapping == null) {
                throw new SearchException(String.format("Failed to build select attributes. Column '%s' is undefined", attributeName));
            }
            // Add columns from child table
            selectExpressionItem = buildSelectAttributeFromChildTable(tableName, attributeName);
        } else {
            Column selectColumn = new Column(tableAlias, attributeName);
            selectExpressionItem = new SelectExpressionItem(selectColumn);
        }
        expresisons.add(selectExpressionItem);
        hasDn |= StringHelper.equals(attributeName, DN);
    }
    if (!hasDn) {
        expresisons.add(selectDnItem);
    }
    expresisons.add(selectDocIdItem);
    return expresisons;
}
Also used : SelectExpressionItem(net.sf.jsqlparser.statement.select.SelectExpressionItem) ArrayList(java.util.ArrayList) TableMapping(io.jans.orm.cloud.spanner.model.TableMapping) SearchException(io.jans.orm.exception.operation.SearchException) ValueWithStructField(io.jans.orm.cloud.spanner.model.ValueWithStructField) StructField(com.google.cloud.spanner.Type.StructField) Column(net.sf.jsqlparser.schema.Column) AllTableColumns(net.sf.jsqlparser.statement.select.AllTableColumns) SelectItem(net.sf.jsqlparser.statement.select.SelectItem) List(java.util.List) ArrayList(java.util.ArrayList) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) LinkedList(java.util.LinkedList)

Example 3 with StructField

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

the class SpannerFilterConverter method getStructField.

private StructField getStructField(TableMapping tableMapping, String attributeName) throws SearchException {
    String attributeNameLower = attributeName.toLowerCase();
    StructField structField = tableMapping.getColumTypes().get(attributeNameLower);
    if (structField == null) {
        TableMapping childTableMapping = tableMapping.getChildTableMappingForAttribute(attributeNameLower);
        if (childTableMapping != null) {
            structField = childTableMapping.getColumTypes().get(attributeNameLower);
        }
    }
    if (structField == null) {
        throw new SearchException(String.format("Unknown column name '%s' in table/child table '%s'", attributeName, tableMapping.getTableName()));
    }
    return structField;
}
Also used : ValueWithStructField(io.jans.orm.cloud.spanner.model.ValueWithStructField) StructField(com.google.cloud.spanner.Type.StructField) TableMapping(io.jans.orm.cloud.spanner.model.TableMapping) SearchException(io.jans.orm.exception.operation.SearchException)

Example 4 with StructField

use of com.google.cloud.spanner.Type.StructField 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 5 with StructField

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

the class SpannerOperationServiceImpl method addEntryImpl.

private boolean addEntryImpl(TableMapping tableMapping, String key, Collection<AttributeData> attributes) throws PersistenceException {
    try {
        MessageDigest messageDigest = getMessageDigestInstance();
        Map<String, StructField> columTypes = tableMapping.getColumTypes();
        WriteBuilder mutationBuilder = Mutation.newInsertOrUpdateBuilder(tableMapping.getTableName());
        List<Mutation> mutations = new LinkedList<>();
        for (AttributeData attribute : attributes) {
            String attributeName = attribute.getName();
            StructField attributeType = columTypes.get(attributeName.toLowerCase());
            // If column not inside table we should check if there is child table
            if (attributeType == null) {
                TableMapping childTableMapping = connectionProvider.getChildTableMappingByKey(key, tableMapping, attributeName);
                if (childTableMapping == null) {
                    throw new PersistenceException(String.format("Failed to add entry. Column '%s' is undefined", attributeName));
                }
                Map<String, StructField> childColumTypes = childTableMapping.getColumTypes();
                if (childColumTypes == null) {
                    throw new PersistenceException(String.format("Failed to add entry. Column '%s' is undefined", attributeName));
                }
                StructField childAttributeType = childColumTypes.get(attributeName.toLowerCase());
                // Build Mutation for child table
                for (Object value : attribute.getValues()) {
                    // Build Mutation for child table
                    String dictDocId = getStringUniqueKey(messageDigest, value);
                    WriteBuilder childMutationBuilder = Mutation.newInsertOrUpdateBuilder(childTableMapping.getTableName());
                    childMutationBuilder.set(SpannerOperationService.DOC_ID).to(key).set(SpannerOperationService.DICT_DOC_ID).to(dictDocId);
                    setMutationBuilderValue(childMutationBuilder, childAttributeType, value);
                    mutations.add(childMutationBuilder.build());
                }
            } else {
                setMutationBuilderValue(mutationBuilder, attributeType, attribute.getValues());
            }
        }
        mutations.add(0, mutationBuilder.build());
        databaseClient.write(mutations);
        return true;
    } catch (SpannerException | IllegalStateException ex) {
        throw new PersistenceException("Failed to add entry", ex);
    }
}
Also used : TableMapping(io.jans.orm.cloud.spanner.model.TableMapping) LinkedList(java.util.LinkedList) ValueWithStructField(io.jans.orm.cloud.spanner.model.ValueWithStructField) StructField(com.google.cloud.spanner.Type.StructField) WriteBuilder(com.google.cloud.spanner.Mutation.WriteBuilder) PersistenceException(io.jans.orm.exception.operation.PersistenceException) Mutation(com.google.cloud.spanner.Mutation) SpannerException(com.google.cloud.spanner.SpannerException) MessageDigest(java.security.MessageDigest) AttributeData(io.jans.orm.model.AttributeData)

Aggregations

StructField (com.google.cloud.spanner.Type.StructField)11 ValueWithStructField (io.jans.orm.cloud.spanner.model.ValueWithStructField)7 TableMapping (io.jans.orm.cloud.spanner.model.TableMapping)5 SpannerException (com.google.cloud.spanner.SpannerException)4 ArrayList (java.util.ArrayList)4 ResultSet (com.google.cloud.spanner.ResultSet)3 PersistenceException (io.jans.orm.exception.operation.PersistenceException)3 SearchException (io.jans.orm.exception.operation.SearchException)3 AttributeData (io.jans.orm.model.AttributeData)3 LinkedList (java.util.LinkedList)3 Mutation (com.google.cloud.spanner.Mutation)2 WriteBuilder (com.google.cloud.spanner.Mutation.WriteBuilder)2 Struct (com.google.cloud.spanner.Struct)2 Code (com.google.cloud.spanner.Type.Code)2 MessageDigest (java.security.MessageDigest)2 KeySet (com.google.cloud.spanner.KeySet)1 Builder (com.google.cloud.spanner.Statement.Builder)1 Type (com.google.cloud.spanner.Type)1 KeyConversionException (io.jans.orm.exception.KeyConversionException)1 MappingException (io.jans.orm.exception.MappingException)1