Search in sources :

Example 36 with Table

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

the class JdbcQueryCreator method getJoin.

@Nullable
Join getJoin(SqlContext sqlContext, PersistentPropertyPathExtension path) {
    if (!path.isEntity() || path.isEmbedded() || path.isMultiValued()) {
        return null;
    }
    Table currentTable = sqlContext.getTable(path);
    PersistentPropertyPathExtension idDefiningParentPath = path.getIdDefiningParentPath();
    Table parentTable = sqlContext.getTable(idDefiningParentPath);
    return new // 
    Join(// 
    currentTable, // 
    currentTable.column(path.getReverseColumnName()), // 
    parentTable.column(idDefiningParentPath.getIdColumnName()));
}
Also used : Table(org.springframework.data.relational.core.sql.Table) PersistentPropertyPathExtension(org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension) Nullable(org.springframework.lang.Nullable)

Example 37 with Table

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

the class SqlContext method getTable.

Table getTable(PersistentPropertyPathExtension path) {
    SqlIdentifier tableAlias = path.getTableAlias();
    Table table = Table.create(path.getTableName());
    return tableAlias == null ? table : table.as(tableAlias);
}
Also used : Table(org.springframework.data.relational.core.sql.Table) SqlIdentifier(org.springframework.data.relational.core.sql.SqlIdentifier)

Example 38 with Table

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

the class SqlGeneratorUnitTests method joinForOneToOneWithoutId.

// DATAJDBC-340
@Test
void joinForOneToOneWithoutId() {
    SqlGenerator.Join join = generateJoin("child", ParentOfNoIdChild.class);
    Table joinTable = join.getJoinTable();
    assertSoftly(softly -> {
        softly.assertThat(joinTable.getName()).isEqualTo(SqlIdentifier.quoted("NO_ID_CHILD"));
        softly.assertThat(joinTable).isInstanceOf(Aliased.class);
        softly.assertThat(((Aliased) joinTable).getAlias()).isEqualTo(SqlIdentifier.quoted("child"));
        softly.assertThat(join.getJoinColumn().getTable()).isEqualTo(joinTable);
        softly.assertThat(join.getJoinColumn().getName()).isEqualTo(SqlIdentifier.quoted("PARENT_OF_NO_ID_CHILD"));
        softly.assertThat(join.getParentId().getName()).isEqualTo(SqlIdentifier.quoted("X_ID"));
        softly.assertThat(join.getParentId().getTable().getName()).isEqualTo(SqlIdentifier.quoted("PARENT_OF_NO_ID_CHILD"));
    });
}
Also used : Table(org.springframework.data.relational.core.sql.Table) Aliased(org.springframework.data.relational.core.sql.Aliased) Test(org.junit.jupiter.api.Test)

Example 39 with Table

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

the class DeleteRendererUnitTests method shouldConsiderTableAlias.

// DATAJDBC-335
@Test
public void shouldConsiderTableAlias() {
    Table table = Table.create("bar").as("my_bar");
    Delete delete = // 
    Delete.builder().from(table).where(// 
    table.column("foo").isEqualTo(table.column("baz"))).build();
    assertThat(SqlRenderer.toString(delete)).isEqualTo("DELETE FROM bar my_bar WHERE my_bar.foo = my_bar.baz");
}
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 40 with Table

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

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