Skip to content

Commit 61c6438

Browse files
committed
Polishing.
Simplified the code structure. Ensured backward compatibility by recreating some methods often immediately deprecating them. Moved new classes to the places where they belong, so that the package ...core.sql.render depends on ...core.dialect and not the other way round. This causes dependency cycles because dependencies in the other direction already exists. This will be properly fixed by #1105. For now the offending classes are ignored by the DependencyTests. See #777 See #1105 Polishing
1 parent 7551cbd commit 61c6438

File tree

16 files changed

+124
-104
lines changed

16 files changed

+124
-104
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlGenerator.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,12 @@ class SqlGenerator {
9393
SqlGenerator(RelationalMappingContext mappingContext, JdbcConverter converter, RelationalPersistentEntity<?> entity,
9494
Dialect dialect) {
9595

96-
final RenderContextFactory renderContextFactory = new RenderContextFactory(dialect);
97-
9896
this.mappingContext = mappingContext;
9997
this.entity = entity;
10098
this.sqlContext = new SqlContext(entity);
101-
this.sqlRenderer = SqlRenderer.create(renderContextFactory.createRenderContext());
99+
this.renderContext = new RenderContextFactory(dialect).createRenderContext();
100+
this.sqlRenderer = SqlRenderer.create(renderContext);
102101
this.columns = new Columns(entity, mappingContext, converter);
103-
this.renderContext = renderContextFactory.createRenderContext();
104102
}
105103

106104
/**

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcAggregateTemplateIntegrationTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,9 +888,11 @@ public void saveAndLoadDateTimeWithMicrosecondPrecision() {
888888
assertThat(loaded.testTime).isEqualTo(entity.testTime);
889889
}
890890

891-
@Test // DATAJDBC-557
891+
@Test // GH-777
892892
public void insertWithIdOnly() {
893+
893894
WithIdOnly entity = new WithIdOnly();
895+
894896
assertThat(template.save(entity).id).isNotNull();
895897
}
896898

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/DefaultDataAccessStrategyUnitTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public void insertWithUndefinedIdRetrievesGeneratedKeys() {
219219

220220
assertThat(generatedId).isEqualTo(GENERATED_ID);
221221

222-
verify(namedJdbcOperations).update(eq("INSERT INTO \"DUMMY_ENTITY\" VALUES ()"),
222+
verify(namedJdbcOperations).update(eq("INSERT INTO \"DUMMY_ENTITY\" VALUES (DEFAULT)"),
223223
paramSourceCaptor.capture(), any(KeyHolder.class));
224224
}
225225

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/SqlGeneratorUnitTests.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -398,18 +398,19 @@ void getInsertForEmptyColumnListPostgres() {
398398

399399
SqlGenerator sqlGenerator = createSqlGenerator(IdOnlyEntity.class, PostgresDialect.INSTANCE);
400400

401-
String insertSqlStatement = sqlGenerator.getInsert(emptySet());
401+
String insert = sqlGenerator.getInsert(emptySet());
402402

403-
assertThat(insertSqlStatement).endsWith(" VALUES (DEFAULT) ");
403+
assertThat(insert).endsWith(" VALUES (DEFAULT)");
404404
}
405405

406-
@Test //DATAJDBC-557
406+
@Test // GH-777
407407
void gerInsertForEmptyColumnListMsSqlServer() {
408+
408409
SqlGenerator sqlGenerator = createSqlGenerator(IdOnlyEntity.class, SqlServerDialect.INSTANCE);
409410

410-
String insertSqlStatement = sqlGenerator.getInsert(emptySet());
411+
String insert = sqlGenerator.getInsert(emptySet());
411412

412-
assertThat(insertSqlStatement).endsWith(" DEFAULT VALUES ");
413+
assertThat(insert).endsWith(" DEFAULT VALUES");
413414
}
414415

415416
@Test // DATAJDBC-334

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,12 @@ default Set<Class<?>> simpleTypes() {
112112
}
113113

114114
/**
115-
* @return an appropriate {@link InsertWithDefaultValues } for that specific dialect.
115+
* @return an appropriate {@link InsertRenderContext} for that specific dialect.
116116
* for most of the Dialects the default implementation will be valid, but, for
117-
* example, in case of {@link SqlServerDialect} it is not
117+
* example, in case of {@link SqlServerDialect} it is not.
118+
* @since 2.4
118119
*/
119-
default InsertWithDefaultValues getSqlInsertWithDefaultValues() {
120-
return new InsertWithDefaultValues() {};
120+
default InsertRenderContext getInsertRenderContext() {
121+
return InsertRenderContexts.DEFAULT;
121122
}
122123
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.springframework.data.relational.core.dialect;
2+
3+
import org.springframework.data.relational.core.sql.Insert;
4+
import org.springframework.data.relational.core.sql.render.RenderContext;
5+
6+
/**
7+
* This interface encapsulates the details about how to process {@link Insert} SQL statement
8+
*
9+
* @see RenderContext
10+
* @author Mikhail Polivakha
11+
* @since 2.4
12+
*/
13+
public interface InsertRenderContext {
14+
15+
String getDefaultValuesInsertPart();
16+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.springframework.data.relational.core.dialect;
2+
3+
/**
4+
* In the scope of Insert with default values SQL statement, for example {@literal INSERT INTO SCHEMA.TABLE VALUES
5+
* (DEFAULT)} this enum represents the default values part in different {@link Dialect}s
6+
*
7+
* @author Mikhail Polivakha
8+
* @since 2.4
9+
*/
10+
public enum InsertRenderContexts implements InsertRenderContext {
11+
12+
DEFAULT(" VALUES (DEFAULT)"), //
13+
MS_SQL_SERVER(" DEFAULT VALUES");
14+
15+
private final String defaultInsertPart;
16+
17+
InsertRenderContexts(String defaultInsertPart) {
18+
this.defaultInsertPart = defaultInsertPart;
19+
}
20+
21+
public String getDefaultValuesInsertPart() {
22+
return defaultInsertPart;
23+
}
24+
25+
}
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
package org.springframework.data.relational.core.dialect;
22

3-
import org.springframework.data.relational.core.mapping.InsertDefaultValues;
4-
53
/**
64
* This interface aggregates information about an Insert with default values statement.
5+
*
76
* @author Mikhail Polivakha
7+
* @since 2.4
88
*/
99
public interface InsertWithDefaultValues {
1010

11-
/**
12-
* @return the part of the sql statement, that follows after <b>INSERT INTO table</b>
13-
*/
14-
default String getDefaultInsertPart() {
15-
return InsertDefaultValues.DEFAULT.getDefaultInsertPart();
16-
}
17-
}
11+
/**
12+
* @return the part of the sql statement, that follows after <b>INSERT INTO table</b>
13+
*/
14+
String getDefaultInsertPart();
15+
}

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package org.springframework.data.relational.core.dialect;
1717

1818
import org.springframework.data.relational.core.sql.IdentifierProcessing;
19-
import org.springframework.data.relational.core.sql.render.InsertRenderContext;
2019
import org.springframework.data.relational.core.sql.render.NamingStrategies;
2120
import org.springframework.data.relational.core.sql.render.RenderContext;
2221
import org.springframework.data.relational.core.sql.render.RenderNamingStrategy;
@@ -78,19 +77,23 @@ public RenderContext createRenderContext() {
7877
static class DialectRenderContext implements RenderContext {
7978

8079
private final RenderNamingStrategy renderNamingStrategy;
81-
private final SelectRenderContext selectRenderContext;
8280
private final Dialect renderingDialect;
81+
private final SelectRenderContext selectRenderContext;
82+
private final InsertRenderContext insertRenderContext;
8383

84-
DialectRenderContext(RenderNamingStrategy renderNamingStrategy, Dialect renderingDialect, SelectRenderContext selectRenderContext) {
84+
DialectRenderContext(RenderNamingStrategy renderNamingStrategy, Dialect renderingDialect,
85+
SelectRenderContext selectRenderContext) {
8586

8687
Assert.notNull(renderNamingStrategy, "RenderNamingStrategy must not be null");
8788
Assert.notNull(renderingDialect, "renderingDialect must not be null");
88-
Assert.notNull(renderingDialect.getIdentifierProcessing(), "IdentifierProcessing of renderingDialect must not be null");
89+
Assert.notNull(renderingDialect.getIdentifierProcessing(),
90+
"IdentifierProcessing of renderingDialect must not be null");
8991
Assert.notNull(selectRenderContext, "SelectRenderContext must not be null");
9092

9193
this.renderNamingStrategy = renderNamingStrategy;
9294
this.renderingDialect = renderingDialect;
9395
this.selectRenderContext = selectRenderContext;
96+
this.insertRenderContext = renderingDialect.getInsertRenderContext();
9497
}
9598

9699
/*
@@ -111,6 +114,11 @@ public IdentifierProcessing getIdentifierProcessing() {
111114
return renderingDialect.getIdentifierProcessing();
112115
}
113116

117+
@Override
118+
public SelectRenderContext getSelect() {
119+
return getSelectRenderContext();
120+
}
121+
114122
/*
115123
* (non-Javadoc)
116124
* @see org.springframework.data.relational.core.sql.render.RenderContext#getSelect()
@@ -122,12 +130,7 @@ public SelectRenderContext getSelectRenderContext() {
122130

123131
@Override
124132
public InsertRenderContext getInsertRenderContext() {
125-
return new InsertRenderContext() {
126-
@Override
127-
public String getInsertDefaultValuesPartSQL() {
128-
return renderingDialect.getSqlInsertWithDefaultValues().getDefaultInsertPart();
129-
}
130-
};
133+
return insertRenderContext;
131134
}
132135
}
133-
}
136+
}

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.springframework.data.relational.core.dialect;
1717

18-
import org.springframework.data.relational.core.mapping.InsertDefaultValues;
1918
import org.springframework.data.relational.core.sql.IdentifierProcessing;
2019
import org.springframework.data.relational.core.sql.LockOptions;
2120
import org.springframework.data.relational.core.sql.render.SelectRenderContext;
@@ -154,12 +153,7 @@ public IdentifierProcessing getIdentifierProcessing() {
154153
}
155154

156155
@Override
157-
public InsertWithDefaultValues getSqlInsertWithDefaultValues() {
158-
return new InsertWithDefaultValues() {
159-
@Override
160-
public String getDefaultInsertPart() {
161-
return InsertDefaultValues.MS_SQL_SERVER.getDefaultInsertPart();
162-
}
163-
};
156+
public InsertRenderContext getInsertRenderContext() {
157+
return InsertRenderContexts.MS_SQL_SERVER;
164158
}
165159
}

0 commit comments

Comments
 (0)