Skip to content

DATAJDBC-479 - Use SqlIdentifier in SQL AST #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAJDBC-476-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data Relational Parent</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-jdbc-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAJDBC-476-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-jdbc</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAJDBC-476-SNAPSHOT</version>

<name>Spring Data JDBC</name>
<description>Spring Data module for JDBC repositories.</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAJDBC-476-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
import org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.sql.Column;
import org.springframework.data.relational.core.sql.SQL;
import org.springframework.data.relational.core.sql.Table;
import org.springframework.data.relational.core.sql.IdentifierProcessing;
import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.data.relational.core.sql.Table;

/**
* Utility to get from path to SQL DSL elements.
Expand All @@ -35,21 +33,19 @@ class SqlContext {

private final RelationalPersistentEntity<?> entity;
private final Table table;
private final IdentifierProcessing identifierProcessing;

SqlContext(RelationalPersistentEntity<?> entity, IdentifierProcessing identifierProcessing) {
SqlContext(RelationalPersistentEntity<?> entity) {

this.identifierProcessing = identifierProcessing;
this.entity = entity;
this.table = SQL.table(entity.getTableName().toSql(this.identifierProcessing));
this.table = Table.create(entity.getTableName());
}

Column getIdColumn() {
return table.column(entity.getIdColumn().toSql(identifierProcessing));
return table.column(entity.getIdColumn());
}

Column getVersionColumn() {
return table.column(entity.getRequiredVersionProperty().getColumnName().toSql(identifierProcessing));
return table.column(entity.getRequiredVersionProperty().getColumnName());
}

Table getTable() {
Expand All @@ -59,17 +55,15 @@ Table getTable() {
Table getTable(PersistentPropertyPathExtension path) {

SqlIdentifier tableAlias = path.getTableAlias();
Table table = SQL.table(path.getTableName().toSql(identifierProcessing));
return tableAlias == null ? table : table.as(tableAlias.toSql(identifierProcessing));
Table table = Table.create(path.getTableName());
return tableAlias == null ? table : table.as(tableAlias);
}

Column getColumn(PersistentPropertyPathExtension path) {
return getTable(path).column(path.getColumnName().toSql(identifierProcessing))
.as(path.getColumnAlias().toSql(identifierProcessing));
return getTable(path).column(path.getColumnName()).as(path.getColumnAlias());
}

Column getReverseColumn(PersistentPropertyPathExtension path) {
return getTable(path).column(path.getReverseColumnName().toSql(identifierProcessing))
.as(path.getReverseColumnNameAlias().toSql(identifierProcessing));
return getTable(path).column(path.getReverseColumnName()).as(path.getReverseColumnNameAlias());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
import org.springframework.data.relational.core.sql.*;
import org.springframework.data.relational.core.sql.render.NamingStrategies;
import org.springframework.data.relational.core.sql.render.RenderContext;
import org.springframework.data.relational.core.sql.render.RenderNamingStrategy;
import org.springframework.data.relational.core.sql.render.SelectRenderContext;
import org.springframework.data.relational.core.sql.render.SqlRenderer;
import org.springframework.data.relational.domain.Identifier;
import org.springframework.data.relational.core.sql.IdentifierProcessing;
import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.data.util.Lazy;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
Expand All @@ -69,6 +71,7 @@ class SqlGenerator {
private static final Pattern parameterPattern = Pattern.compile("\\W");
private final RelationalPersistentEntity<?> entity;
private final MappingContext<RelationalPersistentEntity<?>, RelationalPersistentProperty> mappingContext;
private final RenderContext renderContext;
private final IdentifierProcessing identifierProcessing;

private final SqlContext sqlContext;
Expand Down Expand Up @@ -101,8 +104,24 @@ class SqlGenerator {
this.mappingContext = mappingContext;
this.entity = entity;
this.identifierProcessing = identifierProcessing;
this.sqlContext = new SqlContext(entity, identifierProcessing);
this.sqlContext = new SqlContext(entity);
this.columns = new Columns(entity, mappingContext);
this.renderContext = new RenderContext() {
@Override
public RenderNamingStrategy getNamingStrategy() {
return NamingStrategies.asIs();
}

@Override
public IdentifierProcessing getIdentifierProcessing() {
return identifierProcessing;
}

@Override
public SelectRenderContext getSelect() {
return new SelectRenderContext() {};
}
};
}

/**
Expand All @@ -126,10 +145,9 @@ private Condition getSubselectCondition(PersistentPropertyPathExtension path,
return rootCondition.apply(filterColumn);
}

Table subSelectTable = SQL.table(parentPath.getTableName().toSql(identifierProcessing));
Column idColumn = subSelectTable.column(parentPath.getIdColumnName().toSql(identifierProcessing));
Column selectFilterColumn = subSelectTable
.column(parentPath.getEffectiveIdColumnName().toSql(identifierProcessing));
Table subSelectTable = Table.create(parentPath.getTableName());
Column idColumn = subSelectTable.column(parentPath.getIdColumnName());
Column selectFilterColumn = subSelectTable.column(parentPath.getEffectiveIdColumnName());

Condition innerCondition;

Expand Down Expand Up @@ -191,21 +209,19 @@ String getFindAllByProperty(Identifier parentIdentifier, @Nullable SqlIdentifier
Assert.isTrue(keyColumn != null || !ordered,
"If the SQL statement should be ordered a keyColumn to order by must be provided.");

Table table = getTable();

SelectBuilder.SelectWhere builder = selectBuilder( //
keyColumn == null //
? Collections.emptyList() //
: Collections.singleton(keyColumn.toSql(identifierProcessing)) //
: Collections.singleton(keyColumn) //
);

Table table = getTable();

Condition condition = buildConditionForBackReference(parentIdentifier, table);
SelectBuilder.SelectWhereAndOr withWhereClause = builder.where(condition);

Select select = ordered //
? withWhereClause
.orderBy(table.column(keyColumn.toSql(identifierProcessing)).as(keyColumn.toSql(identifierProcessing)))
.build() //
? withWhereClause.orderBy(table.column(keyColumn).as(keyColumn)).build() //
: withWhereClause.build();

return render(select);
Expand All @@ -216,8 +232,7 @@ private Condition buildConditionForBackReference(Identifier parentIdentifier, Ta
Condition condition = null;
for (SqlIdentifier backReferenceColumn : parentIdentifier.toMap().keySet()) {

Condition newCondition = table.column(backReferenceColumn.toSql(identifierProcessing))
.isEqualTo(getBindMarker(backReferenceColumn));
Condition newCondition = table.column(backReferenceColumn).isEqualTo(getBindMarker(backReferenceColumn));
condition = condition == null ? newCondition : condition.and(newCondition);
}

Expand Down Expand Up @@ -353,7 +368,7 @@ private SelectBuilder.SelectWhere selectBuilder() {
return selectBuilder(Collections.emptyList());
}

private SelectBuilder.SelectWhere selectBuilder(Collection<String> keyColumns) {
private SelectBuilder.SelectWhere selectBuilder(Collection<SqlIdentifier> keyColumns) {

Table table = getTable();

Expand All @@ -377,7 +392,7 @@ private SelectBuilder.SelectWhere selectBuilder(Collection<String> keyColumns) {
}
}

for (String keyColumn : keyColumns) {
for (SqlIdentifier keyColumn : keyColumns) {
columnExpressions.add(table.column(keyColumn).as(keyColumn));
}

Expand Down Expand Up @@ -440,8 +455,8 @@ Join getJoin(PersistentPropertyPathExtension path) {

return new Join( //
currentTable, //
currentTable.column(path.getReverseColumnName().toSql(identifierProcessing)), //
parentTable.column(idDefiningParentPath.getIdColumnName().toSql(identifierProcessing)) //
currentTable.column(path.getReverseColumnName()), //
parentTable.column(idDefiningParentPath.getIdColumnName()) //
);
}

Expand Down Expand Up @@ -481,14 +496,14 @@ private String createInsertSql(Set<SqlIdentifier> additionalColumns) {

Table table = getTable();

Set<SqlIdentifier> columnNamesForInsert = new TreeSet<>(Comparator.comparing(id -> id.toSql(identifierProcessing)));
Set<SqlIdentifier> columnNamesForInsert = new TreeSet<>(Comparator.comparing(SqlIdentifier::getReference));
columnNamesForInsert.addAll(columns.getInsertableColumns());
columnNamesForInsert.addAll(additionalColumns);

InsertBuilder.InsertIntoColumnsAndValuesWithBuild insert = Insert.builder().into(table);

for (SqlIdentifier cn : columnNamesForInsert) {
insert = insert.column(table.column(cn.toSql(identifierProcessing)));
insert = insert.column(table.column(cn));
}

InsertBuilder.InsertValuesWithBuild insertWithValues = null;
Expand Down Expand Up @@ -520,7 +535,7 @@ private UpdateBuilder.UpdateWhereAndOr createBaseUpdate() {
List<AssignValue> assignments = columns.getUpdateableColumns() //
.stream() //
.map(columnName -> Assignments.value( //
table.column(columnName.toSql(identifierProcessing)), //
table.column(columnName), //
getBindMarker(columnName))) //
.collect(Collectors.toList());

Expand Down Expand Up @@ -552,13 +567,13 @@ private DeleteBuilder.DeleteWhereAndOr createBaseDeleteById(Table table) {
private String createDeleteByPathAndCriteria(PersistentPropertyPathExtension path,
Function<Column, Condition> rootCondition) {

Table table = SQL.table(path.getTableName().toSql(identifierProcessing));
Table table = Table.create(path.getTableName());

DeleteBuilder.DeleteWhere builder = Delete.builder() //
.from(table);
Delete delete;

Column filterColumn = table.column(path.getReverseColumnName().toSql(identifierProcessing));
Column filterColumn = table.column(path.getReverseColumnName());

if (path.getLength() == 1) {

Expand Down Expand Up @@ -587,19 +602,19 @@ private String createDeleteByListSql() {
}

private String render(Select select) {
return SqlRenderer.create().render(select);
return SqlRenderer.create(renderContext).render(select);
}

private String render(Insert insert) {
return SqlRenderer.create().render(insert);
return SqlRenderer.create(renderContext).render(insert);
}

private String render(Update update) {
return SqlRenderer.create().render(update);
return SqlRenderer.create(renderContext).render(update);
}

private String render(Delete delete) {
return SqlRenderer.create().render(delete);
return SqlRenderer.create(renderContext).render(delete);
}

private Table getTable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ protected <T> RelationalPersistentEntity<T> createPersistentEntity(TypeInformati
@Override
protected RelationalPersistentProperty createPersistentProperty(Property property,
RelationalPersistentEntity<?> owner, SimpleTypeHolder simpleTypeHolder) {
return new BasicJdbcPersistentProperty(property, owner, simpleTypeHolder, this);
BasicJdbcPersistentProperty persistentProperty = new BasicJdbcPersistentProperty(property, owner, simpleTypeHolder,
this);
persistentProperty.setForceQuote(isForceQuote());
return persistentProperty;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.springframework.data.relational.core.sql.IdentifierProcessing;
import org.springframework.data.relational.core.sql.IdentifierProcessing.LetterCasing;
import org.springframework.data.relational.core.sql.IdentifierProcessing.Quoting;
import org.springframework.data.relational.core.sql.SqlIdentifier;

/**
* Unit tests for the {@link SqlGenerator} in a context of the {@link Embedded} annotation.
Expand All @@ -48,6 +49,7 @@ public class SqlGeneratorEmbeddedUnitTests {

@Before
public void setUp() {
this.context.setForceQuote(false);
this.sqlGenerator = createSqlGenerator(DummyEntity.class);
}

Expand Down Expand Up @@ -201,7 +203,8 @@ public void columnForEmbeddedProperty() {

assertThat(generatedColumn("embeddable.test", DummyEntity.class)) //
.extracting(c -> c.getName(), c -> c.getTable().getName(), c -> getAlias(c.getTable()), this::getAlias)
.containsExactly("test", "dummy_entity", null, "test");
.containsExactly(SqlIdentifier.unquoted("test"), SqlIdentifier.unquoted("dummy_entity"), null,
SqlIdentifier.unquoted("test"));
}

@Test // DATAJDBC-340
Expand All @@ -224,7 +227,8 @@ public void columnForPrefixedEmbeddedProperty() {

assertThat(generatedColumn("prefixedEmbeddable.test", DummyEntity.class)) //
.extracting(c -> c.getName(), c -> c.getTable().getName(), c -> getAlias(c.getTable()), this::getAlias)
.containsExactly("prefix_test", "dummy_entity", null, "prefix_test");
.containsExactly(SqlIdentifier.unquoted("prefix_test"), SqlIdentifier.unquoted("dummy_entity"), null,
SqlIdentifier.unquoted("prefix_test"));
}

@Test // DATAJDBC-340
Expand All @@ -240,7 +244,8 @@ public void columnForCascadedEmbeddedProperty() {

assertThat(generatedColumn("embeddable.embeddable.attr1", DummyEntity.class)) //
.extracting(c -> c.getName(), c -> c.getTable().getName(), c -> getAlias(c.getTable()), this::getAlias)
.containsExactly("attr1", "dummy_entity", null, "attr1");
.containsExactly(SqlIdentifier.unquoted("attr1"), SqlIdentifier.unquoted("dummy_entity"), null,
SqlIdentifier.unquoted("attr1"));
}

@Test // DATAJDBC-340
Expand All @@ -250,11 +255,11 @@ public void joinForEmbeddedWithReference() {

SoftAssertions.assertSoftly(softly -> {

softly.assertThat(join.getJoinTable().getName()).isEqualTo("other_entity");
softly.assertThat(join.getJoinTable().getName()).isEqualTo(SqlIdentifier.unquoted("other_entity"));
softly.assertThat(join.getJoinColumn().getTable()).isEqualTo(join.getJoinTable());
softly.assertThat(join.getJoinColumn().getName()).isEqualTo("dummy_entity2");
softly.assertThat(join.getParentId().getName()).isEqualTo("id");
softly.assertThat(join.getParentId().getTable().getName()).isEqualTo("dummy_entity2");
softly.assertThat(join.getJoinColumn().getName()).isEqualTo(SqlIdentifier.unquoted("dummy_entity2"));
softly.assertThat(join.getParentId().getName()).isEqualTo(SqlIdentifier.unquoted("id"));
softly.assertThat(join.getParentId().getTable().getName()).isEqualTo(SqlIdentifier.unquoted("dummy_entity2"));
});
}

Expand All @@ -263,15 +268,16 @@ public void columnForEmbeddedWithReferenceProperty() {

assertThat(generatedColumn("embedded.other.value", DummyEntity2.class)) //
.extracting(c -> c.getName(), c -> c.getTable().getName(), c -> getAlias(c.getTable()), this::getAlias)
.containsExactly("value", "other_entity", "prefix_other", "prefix_other_value");
.containsExactly(SqlIdentifier.unquoted("value"), SqlIdentifier.unquoted("other_entity"),
SqlIdentifier.quoted("prefix_other"), SqlIdentifier.unquoted("prefix_other_value"));
}

private SqlGenerator.Join generateJoin(String path, Class<?> type) {
return createSqlGenerator(type)
.getJoin(new PersistentPropertyPathExtension(context, PropertyPathTestingUtils.toPath(path, type, context)));
}

private String getAlias(Object maybeAliased) {
private SqlIdentifier getAlias(Object maybeAliased) {

if (maybeAliased instanceof Aliased) {
return ((Aliased) maybeAliased).getAlias();
Expand Down
Loading