Search in sources :

Example 6 with TableMapping

use of io.jans.orm.cloud.spanner.model.TableMapping 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 7 with TableMapping

use of io.jans.orm.cloud.spanner.model.TableMapping 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)

Example 8 with TableMapping

use of io.jans.orm.cloud.spanner.model.TableMapping in project jans by JanssenProject.

the class SpannerFilterConverter method addJoinTable.

private void addJoinTable(TableMapping tableMapping, String attributeName, Map<String, Join> joinTables) {
    if (joinTables.containsKey(attributeName)) {
        return;
    }
    TableMapping childTableMapping = tableMapping.getChildTableMappingForAttribute(attributeName.toLowerCase());
    Table childTable = new Table(childTableMapping.getTableName());
    childTable.setAlias(new Alias(attributeName, false));
    EqualsTo onExpression = new EqualsTo().withLeftExpression(new Column().withTable(tableAlias).withColumnName(SpannerOperationService.DOC_ID)).withRightExpression(new Column().withTable(childTable).withColumnName(SpannerOperationService.DOC_ID));
    Join join = new Join();
    join.setRightItem(childTable);
    join.setOnExpression(onExpression);
    joinTables.put(attributeName, join);
}
Also used : Table(net.sf.jsqlparser.schema.Table) Column(net.sf.jsqlparser.schema.Column) Alias(net.sf.jsqlparser.expression.Alias) TableMapping(io.jans.orm.cloud.spanner.model.TableMapping) Join(net.sf.jsqlparser.statement.select.Join) EqualsTo(net.sf.jsqlparser.expression.operators.relational.EqualsTo)

Example 9 with TableMapping

use of io.jans.orm.cloud.spanner.model.TableMapping in project jans by JanssenProject.

the class SpannerOperationServiceImpl method delete.

@Override
public boolean delete(String key, String objectClass) throws EntryNotFoundException {
    Instant startTime = OperationDurationUtil.instance().now();
    TableMapping tableMapping = connectionProvider.getTableMappingByKey(key, objectClass);
    boolean result = deleteImpl(tableMapping, key);
    Duration duration = OperationDurationUtil.instance().duration(startTime);
    OperationDurationUtil.instance().logDebug("SQL operation: delete, duration: {}, table: {}, key: {}", duration, tableMapping.getTableName(), key);
    return result;
}
Also used : Instant(java.time.Instant) TableMapping(io.jans.orm.cloud.spanner.model.TableMapping) Duration(java.time.Duration)

Example 10 with TableMapping

use of io.jans.orm.cloud.spanner.model.TableMapping in project jans by JanssenProject.

the class SpannerOperationServiceImpl method deleteRecursively.

@Override
public boolean deleteRecursively(String key, String objectClass) throws EntryNotFoundException, SearchException {
    Instant startTime = OperationDurationUtil.instance().now();
    TableMapping tableMapping = connectionProvider.getTableMappingByKey(key, objectClass);
    boolean result = deleteRecursivelyImpl(tableMapping, key);
    Duration duration = OperationDurationUtil.instance().duration(startTime);
    OperationDurationUtil.instance().logDebug("SQL operation: delete_tree, duration: {}, table: {}, key: {}", duration, tableMapping.getTableName(), key);
    return result;
}
Also used : Instant(java.time.Instant) TableMapping(io.jans.orm.cloud.spanner.model.TableMapping) Duration(java.time.Duration)

Aggregations

TableMapping (io.jans.orm.cloud.spanner.model.TableMapping)17 Duration (java.time.Duration)8 Instant (java.time.Instant)8 StructField (com.google.cloud.spanner.Type.StructField)5 ValueWithStructField (io.jans.orm.cloud.spanner.model.ValueWithStructField)4 AttributeData (io.jans.orm.model.AttributeData)4 SearchException (io.jans.orm.exception.operation.SearchException)3 LinkedList (java.util.LinkedList)3 Mutation (com.google.cloud.spanner.Mutation)2 WriteBuilder (com.google.cloud.spanner.Mutation.WriteBuilder)2 SpannerException (com.google.cloud.spanner.SpannerException)2 PersistenceException (io.jans.orm.exception.operation.PersistenceException)2 MessageDigest (java.security.MessageDigest)2 Column (net.sf.jsqlparser.schema.Column)2 KeySet (com.google.cloud.spanner.KeySet)1 Builder (com.google.cloud.spanner.Statement.Builder)1 KeyConversionException (io.jans.orm.exception.KeyConversionException)1 MappingException (io.jans.orm.exception.MappingException)1 EntryConvertationException (io.jans.orm.exception.operation.EntryConvertationException)1 AttributeDataModification (io.jans.orm.model.AttributeDataModification)1