Search in sources :

Example 1 with CriteriaDefinition

use of org.springframework.data.relational.core.query.CriteriaDefinition in project gravitee-access-management by gravitee-io.

the class JdbcResourceRepository method findByDomainAndClient.

@Override
public Single<Page<Resource>> findByDomainAndClient(String domain, String client, int page, int size) {
    LOGGER.debug("findByDomainAndClient({}, {}, {}, {})", domain, client, page, size);
    CriteriaDefinition whereClause = from(where("domain").is(domain).and(where("client_id").is(client)));
    return findResourcePage(domain, page, size, whereClause);
}
Also used : CriteriaDefinition(org.springframework.data.relational.core.query.CriteriaDefinition)

Example 2 with CriteriaDefinition

use of org.springframework.data.relational.core.query.CriteriaDefinition in project r2dbc-dsl by SevenParadigms.

the class DefaultStatementMapper method getMappedObject.

private PreparedOperation<Delete> getMappedObject(DeleteSpec deleteSpec, @Nullable RelationalPersistentEntity<?> entity) {
    BindMarkers bindMarkers = this.dialect.getBindMarkersFactory().create();
    Table table = Table.create(toSql(deleteSpec.getTable()));
    DeleteBuilder.DeleteWhere deleteBuilder = StatementBuilder.delete(table);
    Bindings bindings = Bindings.empty();
    Delete delete;
    CriteriaDefinition criteria = deleteSpec.getCriteria();
    if (criteria != null && !criteria.isEmpty()) {
        BoundCondition boundCondition = this.updateMapper.getMappedObject(bindMarkers, deleteSpec.getCriteria(), table, entity);
        bindings = boundCondition.getBindings();
        delete = deleteBuilder.where(boundCondition.getCondition()).build();
    } else {
        delete = deleteBuilder.build();
    }
    return new DefaultPreparedOperation<>(delete, this.renderContext, bindings);
}
Also used : BoundCondition(org.springframework.data.r2dbc.query.BoundCondition) BindMarkers(org.springframework.r2dbc.core.binding.BindMarkers) Bindings(org.springframework.r2dbc.core.binding.Bindings) CriteriaDefinition(org.springframework.data.relational.core.query.CriteriaDefinition)

Example 3 with CriteriaDefinition

use of org.springframework.data.relational.core.query.CriteriaDefinition in project r2dbc-dsl by SevenParadigms.

the class R2dbcEntityTemplate method doExists.

Mono<Boolean> doExists(Query query, Class<?> entityClass, SqlIdentifier tableName) {
    StatementMapper statementMapper = dataAccessStrategy.getStatementMapper().forType(entityClass);
    SqlIdentifier columnName = SqlIdentifier.unquoted("*");
    if (isIdContains(entityClass)) {
        columnName = new DefaultSqlIdentifier(SqlField.id, false);
    } else {
        RelationalPersistentEntity<?> entity = getRequiredEntity(entityClass);
        if (entity.hasIdProperty()) {
            columnName = entity.getRequiredIdProperty().getColumnName();
        }
    }
    StatementMapper.SelectSpec selectSpec = // 
    statementMapper.createSelect(// 
    tableName).withProjection(// 
    columnName).limit(1);
    Optional<CriteriaDefinition> criteria = query.getCriteria();
    if (criteria.isPresent()) {
        selectSpec = criteria.map(selectSpec::withCriteria).orElse(selectSpec);
    }
    PreparedOperation<?> operation = statementMapper.getMappedObject(selectSpec);
    return // 
    this.databaseClient.sql(operation).map(// 
    (r, md) -> r).first().hasElement();
}
Also used : DefaultSqlIdentifier(org.springframework.data.r2dbc.repository.support.DefaultSqlIdentifier) SqlIdentifier(org.springframework.data.relational.core.sql.SqlIdentifier) DefaultSqlIdentifier(org.springframework.data.r2dbc.repository.support.DefaultSqlIdentifier) CriteriaDefinition(org.springframework.data.relational.core.query.CriteriaDefinition)

Example 4 with CriteriaDefinition

use of org.springframework.data.relational.core.query.CriteriaDefinition in project r2dbc-dsl by SevenParadigms.

the class QueryMapper method unroll.

private Condition unroll(CriteriaDefinition criteria, Table table, @Nullable RelationalPersistentEntity<?> entity, MutableBindings bindings) {
    CriteriaDefinition current = criteria;
    // reverse unroll criteria chain
    Map<CriteriaDefinition, CriteriaDefinition> forwardChain = new HashMap<>();
    while (current.hasPrevious()) {
        forwardChain.put(current.getPrevious(), current);
        current = current.getPrevious();
    }
    // perform the actual mapping
    Condition mapped = getCondition(current, bindings, table, entity);
    while (forwardChain.containsKey(current)) {
        CriteriaDefinition criterion = forwardChain.get(current);
        Condition result = null;
        Condition condition = getCondition(criterion, bindings, table, entity);
        if (condition != null) {
            result = combine(criterion, mapped, criterion.getCombinator(), condition);
        }
        if (result != null) {
            mapped = result;
        }
        current = criterion;
    }
    if (mapped == null) {
        throw new IllegalStateException("Cannot map empty Criteria");
    }
    return mapped;
}
Also used : HashMap(java.util.HashMap) CriteriaDefinition(org.springframework.data.relational.core.query.CriteriaDefinition)

Example 5 with CriteriaDefinition

use of org.springframework.data.relational.core.query.CriteriaDefinition in project spring-data-jdbc by spring-projects.

the class QueryMapper method unroll.

private Condition unroll(CriteriaDefinition criteria, Table table, @Nullable RelationalPersistentEntity<?> entity, MapSqlParameterSource parameterSource) {
    CriteriaDefinition current = criteria;
    // reverse unroll criteria chain
    Map<CriteriaDefinition, CriteriaDefinition> forwardChain = new HashMap<>();
    while (current.hasPrevious()) {
        forwardChain.put(current.getPrevious(), current);
        current = current.getPrevious();
    }
    // perform the actual mapping
    Condition mapped = getCondition(current, parameterSource, table, entity);
    while (forwardChain.containsKey(current)) {
        CriteriaDefinition criterion = forwardChain.get(current);
        Condition result = null;
        Condition condition = getCondition(criterion, parameterSource, table, entity);
        if (condition != null) {
            result = combine(mapped, criterion.getCombinator(), condition);
        }
        if (result != null) {
            mapped = result;
        }
        current = criterion;
    }
    if (mapped == null) {
        throw new IllegalStateException("Cannot map empty Criteria");
    }
    return mapped;
}
Also used : HashMap(java.util.HashMap) CriteriaDefinition(org.springframework.data.relational.core.query.CriteriaDefinition)

Aggregations

CriteriaDefinition (org.springframework.data.relational.core.query.CriteriaDefinition)10 BoundCondition (org.springframework.data.r2dbc.query.BoundCondition)3 BindMarkers (org.springframework.r2dbc.core.binding.BindMarkers)3 Bindings (org.springframework.r2dbc.core.binding.Bindings)3 HashMap (java.util.HashMap)2 Nullable (org.springframework.lang.Nullable)2 BoundAssignments (org.springframework.data.r2dbc.query.BoundAssignments)1 DefaultSqlIdentifier (org.springframework.data.r2dbc.repository.support.DefaultSqlIdentifier)1 SqlIdentifier (org.springframework.data.relational.core.sql.SqlIdentifier)1