Skip to content

Commit

Permalink
[#90] Use DmlTable column to make dml query
Browse files Browse the repository at this point in the history
  • Loading branch information
IAM20 committed Mar 24, 2023
1 parent 82b1b39 commit 6967d24
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,18 @@ public class BoardRepositoryTest {
.build()
))
.configMap(Stream.of(
Config.builder()
.configKey("board1 post1 config-key")
.configValue("board1 post1 config-value")
.build(),
Config.builder()
.configKey("board1 post1 config-key2")
.configValue("board1 post1 config-value2")
.build(),
Config.builder()
.configKey("board1 post1 config-key3")
.configValue("board1 post1 config-value3")
.build())
Config.builder()
.configKey("board1 post1 config-key")
.configValue("board1 post1 config-value")
.build(),
Config.builder()
.configKey("board1 post1 config-key2")
.configValue("board1 post1 config-value2")
.build(),
Config.builder()
.configKey("board1 post1 config-key3")
.configValue("board1 post1 config-value3")
.build())
.collect(toMap(Config::getConfigKey, config -> config)))
.audit(Audit.builder()
.name("naver1")
Expand Down Expand Up @@ -156,14 +156,14 @@ public class BoardRepositoryTest {
.build()
))
.configMap(Stream.of(
Config.builder()
.configKey("board1 config-key")
.configValue("board1 config-value")
.build(),
Config.builder()
.configKey("board1 config-key2")
.configValue("board1 config-value2")
.build())
Config.builder()
.configKey("board1 config-key")
.configValue("board1 config-value")
.build(),
Config.builder()
.configKey("board1 config-key2")
.configValue("board1 config-value2")
.build())
.collect(toMap(Config::getConfigKey, config -> config)))
.audit(Audit.builder()
.name("naver")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.sql.Aliased;
import org.springframework.data.relational.core.sql.Column;
import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.data.relational.core.sql.Table;
Expand Down Expand Up @@ -57,11 +58,29 @@ public Column getIdColumn() {
return table.column(entity.getIdColumn());
}

@Override
public Column getDmlIdColumn() {
Table dmlTable = table;
if (table instanceof Aliased) {
dmlTable = Table.create(table.getName());
}
return dmlTable.column(entity.getIdColumn());
}

@Override
public Column getVersionColumn() {
return table.column(entity.getRequiredVersionProperty().getColumnName());
}

@Override
public Column getDmlVersionColumn() {
Table dmlTable = table;
if (table instanceof Aliased) {
dmlTable = Table.create(table.getName());
}
return dmlTable.column(entity.getRequiredVersionProperty().getColumnName());
}

@Override
public Table getTable() {
return table;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ public interface SqlContexts {
*/
Column getIdColumn();

/**
* Gets dml id column for dml.
*
* @return the id column
*/
Column getDmlIdColumn();

/**
* Gets table.
*
Expand Down Expand Up @@ -65,6 +72,13 @@ public interface SqlContexts {
*/
Column getVersionColumn();

/**
* Gets version column for dml.
*
* @return the version column
*/
Column getDmlVersionColumn();

/**
* Gets reverse column.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -884,8 +884,7 @@ private String createUpdateSql() {
private String createUpdateWithVersionSql() {

Update update = createBaseUpdate() //
.and(getVersionColumn().isEqualTo(
getBindMarker(VERSION_SQL_PARAMETER))) //
.and(getDmlVersionColumn().isEqualTo(getBindMarker(VERSION_SQL_PARAMETER))) //
.build();

return render(update);
Expand All @@ -900,12 +899,12 @@ private UpdateBuilder.UpdateWhereAndOr createBaseUpdate() {
.map(columnName -> Assignments.value( //
table.column(columnName), //
getBindMarker(columnName))) //
.collect(Collectors.toList());
.toList();

return Update.builder() //
.table(table) //
.set(assignments) //
.where(getIdColumn().isEqualTo(getBindMarker(entity.getIdColumn())));
.where(getDmlIdColumn().isEqualTo(getBindMarker(entity.getIdColumn())));
}

private String createUpsertSql() {
Expand Down Expand Up @@ -947,7 +946,7 @@ private String createDeleteByIdInSql() {
private String createDeleteByIdAndVersionSql() {

Delete delete = createBaseDeleteById(getDmlTable()) //
.and(getVersionColumn().isEqualTo(
.and(getDmlVersionColumn().isEqualTo(
getBindMarker(VERSION_SQL_PARAMETER))) //
.build();

Expand All @@ -956,14 +955,14 @@ private String createDeleteByIdAndVersionSql() {

private DeleteBuilder.DeleteWhereAndOr createBaseDeleteById(Table table) {
return Delete.builder().from(table)
.where(getIdColumn().isEqualTo(
.where(getDmlIdColumn().isEqualTo(
getBindMarker(ID_SQL_PARAMETER)));
}

private DeleteBuilder.DeleteWhereAndOr createBaseDeleteByIdIn(Table table) {

return Delete.builder().from(table)
.where(getIdColumn().in(getBindMarker(IDS_SQL_PARAMETER)));
.where(getDmlIdColumn().in(getBindMarker(IDS_SQL_PARAMETER)));
}

private String createDeleteByPathAndCriteria(PersistentPropertyPathExtension path,
Expand Down Expand Up @@ -997,7 +996,7 @@ private String createDeleteByListSql() {

Delete delete = Delete.builder() //
.from(table) //
.where(getIdColumn().in(getBindMarker(IDS_SQL_PARAMETER))) //
.where(getDmlIdColumn().in(getBindMarker(IDS_SQL_PARAMETER))) //
.build();

return render(delete);
Expand Down Expand Up @@ -1036,10 +1035,18 @@ private Column getIdColumn() {
return sqlContext.getIdColumn();
}

private Column getDmlIdColumn() {
return sqlContext.getDmlIdColumn();
}

private Column getVersionColumn() {
return sqlContext.getVersionColumn();
}

private Column getDmlVersionColumn() {
return sqlContext.getDmlVersionColumn();
}

private String renderReference(SqlIdentifier identifier) {
return identifier.getReference(renderContext.getIdentifierProcessing());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
import org.springframework.data.relational.core.mapping.Table;
import org.springframework.data.relational.core.sql.Aliased;
import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.data.relational.core.sql.TableLike;

import com.navercorp.spring.data.jdbc.plus.sql.annotation.SqlTableAlias;

/**
* COPY org.springframework.data.relational.core.convert.SqlGeneratorUnitTests
*
Expand Down Expand Up @@ -753,6 +756,31 @@ public void columnForReferencedEntityWithoutId() {
quoted("child"), quoted("CHILD_PARENT_OF_NO_ID_CHILD"));
}

@Test
void updateWithAlias() {

assertThat(createSqlGenerator(DummyAliasEntity.class, NonQuotingDialect.INSTANCE).getUpdate())
.contains("WHERE dummy_entity.id1 = :id1");
}

@Test
void deleteWithAlias() {
assertThat(createSqlGenerator(DummyAliasEntity.class, NonQuotingDialect.INSTANCE).getDeleteById())
.contains("WHERE dummy_entity.id1 = :id");
}

@Test
void updateVersionWithAlias() {
assertThat(createSqlGenerator(VersionedAliasEntity.class, NonQuotingDialect.INSTANCE).getUpdateWithVersion())
.contains("WHERE versioned_entity.id1 = :id1 AND versioned_entity.x_version = :___old");
}

@Test
void deleteVersionWithAlias() {
assertThat(createSqlGenerator(VersionedAliasEntity.class, NonQuotingDialect.INSTANCE).getDeleteByIdAndVersion())
.contains("versioned_entity.id1 = :id AND versioned_entity.x_version = :___old");
}

private SqlIdentifier getAlias(Object maybeAliased) {

if (maybeAliased instanceof Aliased) {
Expand Down Expand Up @@ -787,6 +815,30 @@ static class DummyEntity {
AggregateReference<OtherAggregate, Long> other;
}

@SuppressWarnings("unused")
@Table("dummy_entity")
@SqlTableAlias("n_dummy")
static class DummyAliasEntity {

@Column("id1")
@Id
Long id;
String name;
ReferencedEntity ref;
Set<Element> elements;
Map<Integer, Element> mappedElements;
AggregateReference<OtherAggregate, Long> other;
}

@SuppressWarnings("unused")
@Table("versioned_entity")
@SqlTableAlias("n_dummy")
static class VersionedAliasEntity extends DummyAliasEntity {

@Version
Integer version;
}

static class VersionedEntity extends DummyEntity {
@Version
Integer version;
Expand Down

0 comments on commit 6967d24

Please sign in to comment.