Search in sources :

Example 1 with Expression

use of org.springframework.data.relational.core.sql.Expression 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 Expression

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

the class QueryMapperUnitTests method shouldMapCountFunction.

// DATAJDBC-318
@Test
public void shouldMapCountFunction() {
    Table table = Table.create("my_table").as("my_aliased_table");
    Expression mappedObject = mapper.getMappedObject(Functions.count(table.column("alternative")), context.getRequiredPersistentEntity(Person.class));
    assertThat(mappedObject).hasToString("COUNT(my_aliased_table.\"another_name\")");
}
Also used : Table(org.springframework.data.relational.core.sql.Table) Expression(org.springframework.data.relational.core.sql.Expression) Test(org.junit.jupiter.api.Test)

Example 3 with Expression

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

the class QueryMapperUnitTests method shouldMapExpression.

// DATAJDBC-318
@Test
public void shouldMapExpression() {
    Table table = Table.create("my_table").as("my_aliased_table");
    Expression mappedObject = mapper.getMappedObject(table.column("alternative").as("my_aliased_col"), context.getRequiredPersistentEntity(Person.class));
    assertThat(mappedObject).hasToString("my_aliased_table.\"another_name\" AS my_aliased_col");
}
Also used : Table(org.springframework.data.relational.core.sql.Table) Expression(org.springframework.data.relational.core.sql.Expression) Test(org.junit.jupiter.api.Test)

Example 4 with Expression

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

the class QueryMapperUnitTests method shouldMapExpressionWithoutEntity.

// DATAJDBC-318
@Test
public void shouldMapExpressionWithoutEntity() {
    Table table = Table.create("my_table").as("my_aliased_table");
    Expression mappedObject = mapper.getMappedObject(table.column("my_col").as("my_aliased_col"), null);
    assertThat(mappedObject).hasToString("my_aliased_table.my_col AS my_aliased_col");
}
Also used : Table(org.springframework.data.relational.core.sql.Table) Expression(org.springframework.data.relational.core.sql.Expression) Test(org.junit.jupiter.api.Test)

Example 5 with Expression

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

the class QueryMapperUnitTests method shouldMapExpressionToUnknownColumn.

// DATAJDBC-318
@Test
public void shouldMapExpressionToUnknownColumn() {
    Table table = Table.create("my_table").as("my_aliased_table");
    Expression mappedObject = mapper.getMappedObject(table.column("unknown").as("my_aliased_col"), context.getRequiredPersistentEntity(Person.class));
    assertThat(mappedObject).hasToString("my_aliased_table.unknown AS my_aliased_col");
}
Also used : Table(org.springframework.data.relational.core.sql.Table) Expression(org.springframework.data.relational.core.sql.Expression) Test(org.junit.jupiter.api.Test)

Aggregations

Expression (org.springframework.data.relational.core.sql.Expression)6 Test (org.junit.jupiter.api.Test)5 Table (org.springframework.data.relational.core.sql.Table)4 ArrayList (java.util.ArrayList)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 PersistentPropertyPathExtension (org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension)1 RelationalPersistentProperty (org.springframework.data.relational.core.mapping.RelationalPersistentProperty)1 Column (org.springframework.data.relational.core.sql.Column)1 SelectBuilder (org.springframework.data.relational.core.sql.SelectBuilder)1