Search in sources :

Example 1 with SelectBuilder

use of org.springframework.data.relational.core.sql.SelectBuilder in project spring-data-jdbc by spring-projects.

the class JdbcQueryCreator method selectBuilder.

private SelectBuilder.SelectJoin selectBuilder(Table table) {
    List<Expression> columnExpressions = new ArrayList<>();
    RelationalPersistentEntity<?> entity = entityMetadata.getTableEntity();
    SqlContext sqlContext = new SqlContext(entity);
    List<Join> joinTables = new ArrayList<>();
    for (PersistentPropertyPath<RelationalPersistentProperty> path : context.findPersistentPropertyPaths(entity.getType(), p -> true)) {
        PersistentPropertyPathExtension extPath = new PersistentPropertyPathExtension(context, path);
        if (returnedType.needsCustomConstruction()) {
            if (!returnedType.getInputProperties().contains(extPath.getRequiredPersistentPropertyPath().getBaseProperty().getName())) {
                continue;
            }
        }
        // add a join if necessary
        Join join = getJoin(sqlContext, extPath);
        if (join != null) {
            joinTables.add(join);
        }
        Column column = getColumn(sqlContext, extPath);
        if (column != null) {
            columnExpressions.add(column);
        }
    }
    SelectBuilder.SelectAndFrom selectBuilder = StatementBuilder.select(columnExpressions);
    SelectBuilder.SelectJoin baseSelect = selectBuilder.from(table);
    for (Join join : joinTables) {
        baseSelect = baseSelect.leftOuterJoin(join.joinTable).on(join.joinColumn).equals(join.parentId);
    }
    return baseSelect;
}
Also used : ArrayList(java.util.ArrayList) Expression(org.springframework.data.relational.core.sql.Expression) Column(org.springframework.data.relational.core.sql.Column) PersistentPropertyPathExtension(org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension) RelationalPersistentProperty(org.springframework.data.relational.core.mapping.RelationalPersistentProperty) SelectBuilder(org.springframework.data.relational.core.sql.SelectBuilder)

Example 2 with SelectBuilder

use of org.springframework.data.relational.core.sql.SelectBuilder in project spring-data-jdbc by spring-projects.

the class JdbcQueryCreator method createSelectClause.

SelectBuilder.SelectLimitOffset createSelectClause(RelationalPersistentEntity<?> entity, Table table) {
    SelectBuilder.SelectJoin builder;
    if (tree.isExistsProjection()) {
        Column idColumn = table.column(entity.getIdColumn());
        builder = Select.builder().select(idColumn).from(table);
    } else if (tree.isCountProjection()) {
        builder = Select.builder().select(Functions.count(Expressions.asterisk())).from(table);
    } else {
        builder = selectBuilder(table);
    }
    return (SelectBuilder.SelectLimitOffset) builder;
}
Also used : Column(org.springframework.data.relational.core.sql.Column) SelectBuilder(org.springframework.data.relational.core.sql.SelectBuilder)

Aggregations

Column (org.springframework.data.relational.core.sql.Column)2 SelectBuilder (org.springframework.data.relational.core.sql.SelectBuilder)2 ArrayList (java.util.ArrayList)1 PersistentPropertyPathExtension (org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension)1 RelationalPersistentProperty (org.springframework.data.relational.core.mapping.RelationalPersistentProperty)1 Expression (org.springframework.data.relational.core.sql.Expression)1