Skip to content

Commit ccfe820

Browse files
committed
DATAJDBC-547 - Polishing.
Let SqlIdentifier implement Streamable<SqlIdentifier> to allow iteration of composed identifiers. Adapt dialect to iteration changes. Rewrite simple if blocks to using ternary operators. Original pull request: #221.
1 parent d3e6db2 commit ccfe820

File tree

6 files changed

+66
-34
lines changed

6 files changed

+66
-34
lines changed

spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/PostgresDialect.java

+11-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.data.relational.core.sql.IdentifierProcessing.LetterCasing;
2222
import org.springframework.data.relational.core.sql.IdentifierProcessing.Quoting;
2323
import org.springframework.data.relational.core.sql.LockOptions;
24+
import org.springframework.data.relational.core.sql.SqlIdentifier;
2425
import org.springframework.data.relational.core.sql.Table;
2526
import org.springframework.util.Assert;
2627
import org.springframework.util.ClassUtils;
@@ -132,9 +133,16 @@ public String getLock(LockOptions lockOptions) {
132133
return "";
133134
}
134135

135-
String tableName = tables.get(0) // get the first table
136-
.getName().getSimpleIdentifier() // without schema
137-
.toSql(this.identifierProcessing);
136+
// get the first table and obtain last part if the identifier is a composed one.
137+
SqlIdentifier identifier = tables.get(0).getName();
138+
SqlIdentifier last = identifier;
139+
140+
for (SqlIdentifier sqlIdentifier : identifier) {
141+
last = sqlIdentifier;
142+
}
143+
144+
// without schema
145+
String tableName = last.toSql(this.identifierProcessing);
138146

139147
switch (lockOptions.getLockMode()) {
140148

spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/DerivedSqlIdentifier.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.data.relational.core.mapping;
1717

18+
import java.util.Collections;
19+
import java.util.Iterator;
1820
import java.util.function.UnaryOperator;
1921

2022
import org.springframework.data.relational.core.sql.IdentifierProcessing;
@@ -40,6 +42,15 @@ class DerivedSqlIdentifier implements SqlIdentifier {
4042
this.quoted = quoted;
4143
}
4244

45+
/*
46+
* (non-Javadoc)
47+
* @see org.springframework.data.relational.domain.SqlIdentifier#iterator()
48+
*/
49+
@Override
50+
public Iterator<SqlIdentifier> iterator() {
51+
return Collections.<SqlIdentifier> singleton(this).iterator();
52+
}
53+
4354
/*
4455
* (non-Javadoc)
4556
* @see org.springframework.data.relational.domain.SqlIdentifier#transform(java.util.function.UnaryOperator)
@@ -106,11 +117,6 @@ public int hashCode() {
106117
*/
107118
@Override
108119
public String toString() {
109-
110-
if (quoted) {
111-
return toSql(IdentifierProcessing.ANSI);
112-
}
113-
114-
return this.name;
120+
return quoted ? toSql(IdentifierProcessing.ANSI) : this.name;
115121
}
116122
}

spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/RelationalPersistentEntityImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,6 @@ public SqlIdentifier getIdColumn() {
103103
*/
104104
@Override
105105
public String toString() {
106-
return String.format("JdbcPersistentEntityImpl<%s>", getType());
106+
return String.format("RelationalPersistentEntityImpl<%s>", getType());
107107
}
108108
}

spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/CompositeSqlIdentifier.java

+16-7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.data.relational.core.sql;
1717

18+
import java.util.Arrays;
19+
import java.util.Iterator;
1820
import java.util.StringJoiner;
1921
import java.util.function.UnaryOperator;
2022

@@ -40,6 +42,15 @@ class CompositeSqlIdentifier implements SqlIdentifier {
4042
this.parts = parts;
4143
}
4244

45+
/*
46+
* (non-Javadoc)
47+
* @see org.springframework.data.relational.domain.SqlIdentifier#iterator()
48+
*/
49+
@Override
50+
public Iterator<SqlIdentifier> iterator() {
51+
return Arrays.asList(parts).iterator();
52+
}
53+
4354
/*
4455
* (non-Javadoc)
4556
* @see org.springframework.data.relational.domain.SqlIdentifier#transform(java.util.function.UnaryOperator)
@@ -71,12 +82,7 @@ public String toSql(IdentifierProcessing processing) {
7182
*/
7283
@Override
7384
public String getReference(IdentifierProcessing processing) {
74-
throw new UnsupportedOperationException("A Composite SQL Identifiers can't be used as a reference name");
75-
}
76-
77-
@Override
78-
public SqlIdentifier getSimpleIdentifier() {
79-
return parts[parts.length - 1];
85+
throw new UnsupportedOperationException("Composite SQL Identifiers can't be used for reference name retrieval");
8086
}
8187

8288
/*
@@ -86,11 +92,14 @@ public SqlIdentifier getSimpleIdentifier() {
8692
@Override
8793
public boolean equals(Object o) {
8894

89-
if (this == o)
95+
if (this == o) {
9096
return true;
97+
}
98+
9199
if (o instanceof SqlIdentifier) {
92100
return toString().equals(o.toString());
93101
}
102+
94103
return false;
95104
}
96105

spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/DefaultSqlIdentifier.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.data.relational.core.sql;
1717

18+
import java.util.Collections;
19+
import java.util.Iterator;
1820
import java.util.function.UnaryOperator;
1921

2022
import org.springframework.util.Assert;
@@ -39,6 +41,15 @@ class DefaultSqlIdentifier implements SqlIdentifier {
3941
this.quoted = quoted;
4042
}
4143

44+
/*
45+
* (non-Javadoc)
46+
* @see org.springframework.data.relational.domain.SqlIdentifier#iterator()
47+
*/
48+
@Override
49+
public Iterator<SqlIdentifier> iterator() {
50+
return Collections.<SqlIdentifier> singleton(this).iterator();
51+
}
52+
4253
/*
4354
* (non-Javadoc)
4455
* @see org.springframework.data.relational.domain.SqlIdentifier#transform(java.util.function.UnaryOperator)
@@ -102,11 +113,6 @@ public int hashCode() {
102113
*/
103114
@Override
104115
public String toString() {
105-
106-
if (quoted) {
107-
return toSql(IdentifierProcessing.ANSI);
108-
}
109-
110-
return this.name;
116+
return quoted ? toSql(IdentifierProcessing.ANSI) : this.name;
111117
}
112118
}

spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/SqlIdentifier.java

+14-11
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@
1515
*/
1616
package org.springframework.data.relational.core.sql;
1717

18+
import java.util.Collections;
19+
import java.util.Iterator;
1820
import java.util.function.UnaryOperator;
1921

22+
import org.springframework.data.util.Streamable;
23+
2024
/**
2125
* Represents a named object that exists in the database like a table name or a column name. SQL identifiers are created
2226
* from a {@link String name} with specifying whether the name should be quoted or unquoted.
@@ -28,18 +32,27 @@
2832
* <p>
2933
* {@link SqlIdentifier} objects are immutable. Calling transformational methods such as
3034
* {@link #transform(UnaryOperator)} creates a new instance.
35+
* <p>
36+
* {@link SqlIdentifier} are composable so an identifier may consist of a single identifier part or can be composed from
37+
* multiple parts. Composed identifier can be traversed with {@link #stream()} or {@link #iterator()}. The iteration
38+
* order depends on the actual composition ordering.
3139
*
3240
* @author Jens Schauder
3341
* @author Mark Paluch
3442
* @since 2.0
3543
*/
36-
public interface SqlIdentifier {
44+
public interface SqlIdentifier extends Streamable<SqlIdentifier> {
3745

3846
/**
3947
* Null-object.
4048
*/
4149
SqlIdentifier EMPTY = new SqlIdentifier() {
4250

51+
@Override
52+
public Iterator<SqlIdentifier> iterator() {
53+
return Collections.emptyIterator();
54+
}
55+
4356
@Override
4457
public SqlIdentifier transform(UnaryOperator<String> transformationFunction) {
4558
return this;
@@ -99,16 +112,6 @@ default String getReference() {
99112
*/
100113
SqlIdentifier transform(UnaryOperator<String> transformationFunction);
101114

102-
/**
103-
* Returns the last part of an identifier. For a fully qualified column name {@literal schema.table.column} it will
104-
* just return the column part. If the identifier consists of only a single part, that part is returned.
105-
*
106-
* @return Guaranteed to be not {@literal null}.
107-
*/
108-
default SqlIdentifier getSimpleIdentifier() {
109-
return this;
110-
}
111-
112115
/**
113116
* Create a new quoted identifier given {@code name}.
114117
*

0 commit comments

Comments
 (0)