Search in sources :

Example 21 with Select

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

the class SqlServerDialectRenderingUnitTests method shouldRenderSelectWithLimitOffsetAndOrderByWithLockWrite.

// DATAJDBC-498
@Test
public void shouldRenderSelectWithLimitOffsetAndOrderByWithLockWrite() {
    Table table = Table.create("foo");
    LockMode lockMode = LockMode.PESSIMISTIC_WRITE;
    Select select = StatementBuilder.select(table.asterisk()).from(table).orderBy(table.column("column_1")).limit(10).offset(20).lock(lockMode).build();
    String sql = SqlRenderer.create(factory.createRenderContext()).render(select);
    assertThat(sql).isEqualTo("SELECT foo.* FROM foo WITH (UPDLOCK, ROWLOCK) ORDER BY foo.column_1 OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY");
}
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)

Example 22 with Select

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

the class SqlServerDialectRenderingUnitTests method shouldRenderSelectWithLimitOffset.

// DATAJDBC-278
@Test
public void shouldRenderSelectWithLimitOffset() {
    Table table = Table.create("foo");
    Select select = StatementBuilder.select(table.asterisk()).from(table).limit(10).offset(20).build();
    String sql = SqlRenderer.create(factory.createRenderContext()).render(select);
    assertThat(sql).isEqualTo("SELECT foo.*, ROW_NUMBER() over (ORDER BY (SELECT 1)) AS __relational_row_number__ FROM foo ORDER BY __relational_row_number__ OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY");
}
Also used : Table(org.springframework.data.relational.core.sql.Table) Select(org.springframework.data.relational.core.sql.Select) Test(org.junit.jupiter.api.Test)

Example 23 with Select

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

the class SqlServerDialectRenderingUnitTests method shouldRenderSelectOrderByIgnoringNullHandling.

// GH-821
@Test
void shouldRenderSelectOrderByIgnoringNullHandling() {
    Table table = Table.create("foo");
    Select select = StatementBuilder.select(table.asterisk()).from(table).orderBy(OrderByField.from(Column.create("bar", table)).withNullHandling(Sort.NullHandling.NULLS_FIRST)).build();
    String sql = SqlRenderer.create(factory.createRenderContext()).render(select);
    assertThat(sql).isEqualTo("SELECT foo.* FROM foo ORDER BY foo.bar");
}
Also used : Table(org.springframework.data.relational.core.sql.Table) Select(org.springframework.data.relational.core.sql.Select) Test(org.junit.jupiter.api.Test)

Example 24 with Select

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

the class SelectStatementVisitor method doLeave.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.relational.core.sql.render.DelegatingVisitor#doLeave(org.springframework.data.relational.core.sql.Visitable)
	 */
@Override
public Delegation doLeave(Visitable segment) {
    if (segment instanceof Select) {
        Select select = (Select) segment;
        builder.append("SELECT ");
        if (select.isDistinct()) {
            builder.append("DISTINCT ");
        }
        builder.append(selectList);
        builder.append(selectRenderContext.afterSelectList().apply(select));
        if (from.length() != 0) {
            builder.append(" FROM ").append(from);
        }
        builder.append(selectRenderContext.afterFromTable().apply(select));
        if (join.length() != 0) {
            builder.append(' ').append(join);
        }
        if (where.length() != 0) {
            builder.append(" WHERE ").append(where);
        }
        CharSequence orderBy = orderByClauseVisitor.getRenderedPart();
        if (orderBy.length() != 0) {
            builder.append(" ORDER BY ").append(orderBy);
        }
        builder.append(selectRenderContext.afterOrderBy(orderBy.length() != 0).apply(select));
        return Delegation.leave();
    }
    return Delegation.retain();
}
Also used : Select(org.springframework.data.relational.core.sql.Select)

Example 25 with Select

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

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