Search in sources :

Example 6 with Column

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

the class ExpressionVisitorUnitTests method considersNamingStrategy.

// GH-1003
@Test
void considersNamingStrategy() {
    ExpressionVisitor visitor = new ExpressionVisitor(new SimpleRenderContext(NamingStrategies.toUpper()));
    Column expression = Column.create("col", Table.create("tab"));
    expression.visit(visitor);
    assertThat(visitor.getRenderedPart().toString()).isEqualTo("TAB.COL");
}
Also used : Column(org.springframework.data.relational.core.sql.Column) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 7 with Column

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

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

Example 9 with Column

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

the class JoinVisitorTestsUnitTest method renderJoins.

static List<Fixture> renderJoins() {
    Column colOne = Column.create("colOne", Table.create("tabOne"));
    Table tabTwo = Table.create("tabTwo");
    Column colTwo = Column.create("colTwo", tabTwo);
    Column renamed = colOne.as("renamed");
    Select select = Select.builder().select(renamed).from(colOne.getTable()).build();
    InlineQuery inlineQuery = InlineQuery.create(select, "inline");
    return Arrays.asList(fixture("simple join", new TestJoin(Join.JoinType.JOIN, tabTwo, colOne.isEqualTo(colTwo)), "JOIN tabTwo ON tabOne.colOne = tabTwo.colTwo"), fixture("inlineQuery", new TestJoin(Join.JoinType.JOIN, inlineQuery, colTwo.isEqualTo(inlineQuery.column("renamed"))), "JOIN (SELECT tabOne.colOne AS renamed FROM tabOne) inline ON tabTwo.colTwo = inline.renamed"));
}
Also used : Table(org.springframework.data.relational.core.sql.Table) Column(org.springframework.data.relational.core.sql.Column) TestJoin(org.springframework.data.relational.core.sql.TestJoin) Select(org.springframework.data.relational.core.sql.Select) InlineQuery(org.springframework.data.relational.core.sql.InlineQuery)

Example 10 with Column

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

the class OrderByClauseVisitorUnitTests method shouldRenderOrderByAlias.

// DATAJDBC-309
@Test
void shouldRenderOrderByAlias() {
    Table employee = SQL.table("employee").as("emp");
    Column column = employee.column("name").as("emp_name");
    Select select = Select.builder().select(column).from(employee).orderBy(OrderByField.from(column).asc()).build();
    OrderByClauseVisitor visitor = new OrderByClauseVisitor(new SimpleRenderContext(NamingStrategies.asIs()));
    select.visit(visitor);
    assertThat(visitor.getRenderedPart().toString()).isEqualTo("emp_name ASC");
}
Also used : Table(org.springframework.data.relational.core.sql.Table) Column(org.springframework.data.relational.core.sql.Column) Select(org.springframework.data.relational.core.sql.Select) Test(org.junit.jupiter.api.Test)

Aggregations

Column (org.springframework.data.relational.core.sql.Column)17 Test (org.junit.jupiter.api.Test)14 Table (org.springframework.data.relational.core.sql.Table)9 Select (org.springframework.data.relational.core.sql.Select)5 Update (org.springframework.data.relational.core.sql.Update)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 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 InlineQuery (org.springframework.data.relational.core.sql.InlineQuery)1 TestJoin (org.springframework.data.relational.core.sql.TestJoin)1