Search in sources :

Example 1 with Select

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

the class FromClauseVisitorUnitTests method testRendering.

static List<Fixture> testRendering() {
    Table tabOne = Table.create("tabOne");
    Table tabTwo = Table.create("tabTwo");
    Select selectOne = Select.builder().select(Column.create("oneId", tabOne)).from(tabOne).build();
    Select selectTwo = Select.builder().select(Column.create("twoId", tabTwo)).from(tabTwo).build();
    return Arrays.asList(fixture("single table", new TestFrom(Table.create("one")), "one"), fixture("single table with alias", new TestFrom(Table.aliased("one", "one_alias")), "one one_alias"), fixture("multiple tables", new TestFrom(Table.create("one"), Table.create("two")), "one, two"), fixture("multiple tables with alias", new TestFrom(Table.aliased("one", "one_alias"), Table.aliased("two", "two_alias")), "one one_alias, two two_alias"), fixture("single inline query", new TestFrom(InlineQuery.create(selectOne, "ilAlias")), "(SELECT tabOne.oneId FROM tabOne) ilAlias"), fixture("inline query with table", new TestFrom(InlineQuery.create(selectOne, "ilAlias"), tabTwo), "(SELECT tabOne.oneId FROM tabOne) ilAlias, tabTwo"), fixture("table with inline query", new TestFrom(tabTwo, InlineQuery.create(selectOne, "ilAlias")), "tabTwo, (SELECT tabOne.oneId FROM tabOne) ilAlias"), fixture("two inline queries", new TestFrom(InlineQuery.create(selectOne, "aliasOne"), InlineQuery.create(selectTwo, "aliasTwo")), "(SELECT tabOne.oneId FROM tabOne) aliasOne, (SELECT tabTwo.twoId FROM tabTwo) aliasTwo"));
}
Also used : Table(org.springframework.data.relational.core.sql.Table) TestFrom(org.springframework.data.relational.core.sql.TestFrom) Select(org.springframework.data.relational.core.sql.Select)

Example 2 with Select

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

the class OrderByClauseVisitorUnitTests method shouldApplyNamingStrategy.

// DATAJDBC-309
@Test
void shouldApplyNamingStrategy() {
    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.toUpper()));
    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)

Example 3 with Select

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

the class OrderByClauseVisitorUnitTests method shouldRenderOrderByFullyQualifiedNameWithTableAlias.

// GH-968
@Test
void shouldRenderOrderByFullyQualifiedNameWithTableAlias() {
    Table employee = SQL.table("employee").as("emp");
    Column column = employee.column("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)

Example 4 with Select

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

the class JdbcQueryCreator method complete.

/**
 * Creates {@link ParametrizedQuery} applying the given {@link Criteria} and {@link Sort} definition.
 *
 * @param criteria {@link Criteria} to be applied to query
 * @param sort sort option to be applied to query, must not be {@literal null}.
 * @return instance of {@link ParametrizedQuery}
 */
@Override
protected ParametrizedQuery complete(@Nullable Criteria criteria, Sort sort) {
    RelationalPersistentEntity<?> entity = entityMetadata.getTableEntity();
    Table table = Table.create(entityMetadata.getTableName());
    MapSqlParameterSource parameterSource = new MapSqlParameterSource();
    SelectBuilder.SelectLimitOffset limitOffsetBuilder = createSelectClause(entity, table);
    SelectBuilder.SelectWhere whereBuilder = applyLimitAndOffset(limitOffsetBuilder);
    SelectBuilder.SelectOrdered selectOrderBuilder = applyCriteria(criteria, entity, table, parameterSource, whereBuilder);
    selectOrderBuilder = applyOrderBy(sort, entity, table, selectOrderBuilder);
    SelectBuilder.BuildSelect completedBuildSelect = selectOrderBuilder;
    if (this.lockMode.isPresent()) {
        completedBuildSelect = selectOrderBuilder.lock(this.lockMode.get().value());
    }
    Select select = completedBuildSelect.build();
    String sql = SqlRenderer.create(renderContextFactory.createRenderContext()).render(select);
    return new ParametrizedQuery(sql, parameterSource);
}
Also used : Table(org.springframework.data.relational.core.sql.Table) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) Select(org.springframework.data.relational.core.sql.Select) SelectBuilder(org.springframework.data.relational.core.sql.SelectBuilder)

Example 5 with Select

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

the class MySqlDialectRenderingUnitTests method shouldRenderSelectWithLockRead.

// DATAJDBC-498
@Test
public void shouldRenderSelectWithLockRead() {
    Table table = Table.create("foo");
    LockMode lockMode = LockMode.PESSIMISTIC_READ;
    Select select = StatementBuilder.select(table.asterisk()).from(table).lock(lockMode).build();
    String sql = SqlRenderer.create(factory.createRenderContext()).render(select);
    assertThat(sql).isEqualTo("SELECT foo.* FROM foo LOCK IN SHARE MODE");
}
Also used : Table(org.springframework.data.relational.core.sql.Table) Select(org.springframework.data.relational.core.sql.Select) LockMode(org.springframework.data.relational.core.sql.LockMode) Test(org.junit.jupiter.api.Test)

Aggregations

Select (org.springframework.data.relational.core.sql.Select)41 Table (org.springframework.data.relational.core.sql.Table)40 Test (org.junit.jupiter.api.Test)37 LockMode (org.springframework.data.relational.core.sql.LockMode)14 Column (org.springframework.data.relational.core.sql.Column)5 InlineQuery (org.springframework.data.relational.core.sql.InlineQuery)1 SelectBuilder (org.springframework.data.relational.core.sql.SelectBuilder)1 TestFrom (org.springframework.data.relational.core.sql.TestFrom)1 TestJoin (org.springframework.data.relational.core.sql.TestJoin)1 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)1