Skip to content

Commit 84de41a

Browse files
committed
DATAJDBC-264 - Polishing.
Added a test. Added an author tag. Minor formatting. Original pull request: #88.
1 parent 8434f6e commit 84de41a

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/main/java/org/springframework/data/jdbc/core/SqlGenerator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* Generates SQL statements to be used by {@link SimpleJdbcRepository}
4040
*
4141
* @author Jens Schauder
42+
* @author Yoichi Imai
4243
*/
4344
class SqlGenerator {
4445

@@ -260,6 +261,7 @@ private String createInsertSql(Set<String> additionalColumns) {
260261
columnNamesForInsert.addAll(additionalColumns);
261262

262263
String tableColumns = String.join(", ", columnNamesForInsert);
264+
263265
String parameterNames = columnNamesForInsert.stream()//
264266
.map(n -> String.format(":%s", n))//
265267
.collect(Collectors.joining(", "));

src/test/java/org/springframework/data/jdbc/core/SqlGeneratorUnitTests.java

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

18+
import static java.util.Collections.*;
1819
import static org.assertj.core.api.Assertions.*;
1920

2021
import java.util.Map;
@@ -26,11 +27,10 @@
2627
import org.springframework.data.annotation.Id;
2728
import org.springframework.data.jdbc.core.mapping.PersistentPropertyPathTestUtils;
2829
import org.springframework.data.mapping.PersistentPropertyPath;
29-
import org.springframework.data.mapping.PropertyPath;
30+
import org.springframework.data.relational.core.mapping.NamingStrategy;
3031
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
3132
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
3233
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
33-
import org.springframework.data.relational.core.mapping.NamingStrategy;
3434

3535
/**
3636
* Unit tests for the {@link SqlGenerator}.
@@ -46,10 +46,16 @@ public class SqlGeneratorUnitTests {
4646
@Before
4747
public void setUp() {
4848

49+
this.sqlGenerator = createSqlGenerator(DummyEntity.class);
50+
}
51+
52+
SqlGenerator createSqlGenerator(Class<?> type) {
53+
4954
NamingStrategy namingStrategy = new PrefixingNamingStrategy();
5055
RelationalMappingContext context = new RelationalMappingContext(namingStrategy);
51-
RelationalPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(DummyEntity.class);
52-
this.sqlGenerator = new SqlGenerator(context, persistentEntity, new SqlGeneratorSource(context));
56+
RelationalPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(type);
57+
58+
return new SqlGenerator(context, persistentEntity, new SqlGeneratorSource(context));
5359
}
5460

5561
@Test // DATAJDBC-112
@@ -170,10 +176,20 @@ public void findAllByPropertyWithKeyOrdered() {
170176
+ "WHERE back-ref = :back-ref " + "ORDER BY key-column");
171177
}
172178

179+
@Test // DATAJDBC-264
180+
public void getInsertForEmptyColumnList() {
181+
182+
SqlGenerator sqlGenerator = createSqlGenerator(IdOnlyEntity.class);
183+
184+
String insert = sqlGenerator.getInsert(emptySet());
185+
186+
assertThat(insert).endsWith("()");
187+
}
173188

174189
private PersistentPropertyPath<RelationalPersistentProperty> getPath(String path, Class<?> base) {
175190
return PersistentPropertyPathTestUtils.getPath(context, path, base);
176191
}
192+
177193
@SuppressWarnings("unused")
178194
static class DummyEntity {
179195

@@ -212,4 +228,11 @@ public String getColumnName(RelationalPersistentProperty property) {
212228
}
213229

214230
}
231+
232+
@SuppressWarnings("unused")
233+
static class IdOnlyEntity {
234+
235+
@Id Long id;
236+
}
237+
215238
}

0 commit comments

Comments
 (0)