Search in sources :

Example 1 with Table

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

the class DeleteRendererUnitTests method shouldRenderWithCondition.

// DATAJDBC-335
@Test
public void shouldRenderWithCondition() {
    Table table = Table.create("bar");
    Delete delete = // 
    Delete.builder().from(table).where(// 
    table.column("foo").isEqualTo(table.column("baz"))).and(table.column("doe").isNull()).build();
    assertThat(SqlRenderer.toString(delete)).isEqualTo("DELETE FROM bar WHERE bar.foo = bar.baz AND bar.doe IS NULL");
}
Also used : Delete(org.springframework.data.relational.core.sql.Delete) Table(org.springframework.data.relational.core.sql.Table) Test(org.junit.jupiter.api.Test)

Example 2 with Table

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

the class DeleteRendererUnitTests method shouldRenderWithoutWhere.

// DATAJDBC-335
@Test
public void shouldRenderWithoutWhere() {
    Table bar = SQL.table("bar");
    Delete delete = Delete.builder().from(bar).build();
    assertThat(SqlRenderer.toString(delete)).isEqualTo("DELETE FROM bar");
}
Also used : Delete(org.springframework.data.relational.core.sql.Delete) Table(org.springframework.data.relational.core.sql.Table) Test(org.junit.jupiter.api.Test)

Example 3 with Table

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

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

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

Aggregations

Table (org.springframework.data.relational.core.sql.Table)59 Test (org.junit.jupiter.api.Test)53 Select (org.springframework.data.relational.core.sql.Select)40 LockMode (org.springframework.data.relational.core.sql.LockMode)14 Column (org.springframework.data.relational.core.sql.Column)9 Expression (org.springframework.data.relational.core.sql.Expression)4 Insert (org.springframework.data.relational.core.sql.Insert)4 Update (org.springframework.data.relational.core.sql.Update)4 Delete (org.springframework.data.relational.core.sql.Delete)3 SqlIdentifier (org.springframework.data.relational.core.sql.SqlIdentifier)2 PersistentPropertyPathExtension (org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension)1 Aliased (org.springframework.data.relational.core.sql.Aliased)1 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 Nullable (org.springframework.lang.Nullable)1