#390 - Insert should work without giving any explicit assignment/variables.
DatabaseClient.insert() can now persist objects that consist only of an id.
This commit is contained in:
committed by
Mark Paluch
parent
7b676a93ce
commit
748c5ba993
@@ -1044,10 +1044,6 @@ class DefaultDatabaseClient implements DatabaseClient, ConnectionAccessor {
|
||||
|
||||
private <R> FetchSpec<R> exchange(BiFunction<Row, RowMetadata, R> mappingFunction) {
|
||||
|
||||
if (this.byName.isEmpty()) {
|
||||
throw new IllegalStateException("Insert fields is empty!");
|
||||
}
|
||||
|
||||
StatementMapper mapper = dataAccessStrategy.getStatementMapper();
|
||||
StatementMapper.InsertSpec insert = mapper.createInsert(this.table);
|
||||
|
||||
|
||||
@@ -159,10 +159,6 @@ class DefaultStatementMapper implements StatementMapper {
|
||||
|
||||
Bindings bindings;
|
||||
|
||||
if (boundAssignments.getAssignments().isEmpty()) {
|
||||
throw new IllegalStateException("INSERT contains no values");
|
||||
}
|
||||
|
||||
bindings = boundAssignments.getBindings();
|
||||
|
||||
InsertBuilder.InsertIntoColumnsAndValues insertBuilder = StatementBuilder.insert(table);
|
||||
|
||||
@@ -249,6 +249,22 @@ public class DefaultDatabaseClientUnitTests {
|
||||
verify(statement).bindNull(1, Integer.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void insertShouldWorkWithoutValues() {
|
||||
|
||||
Statement statement = mockStatementFor("INSERT INTO id_only VALUES ()");
|
||||
DatabaseClient databaseClient = databaseClientBuilder.build();
|
||||
|
||||
databaseClient.insert().into("id_only") //
|
||||
.then() //
|
||||
.as(StepVerifier::create) //
|
||||
.verifyComplete();
|
||||
|
||||
verify(statement).returnGeneratedValues();
|
||||
verify(statement).execute();
|
||||
verifyNoMoreInteractions(statement);
|
||||
}
|
||||
|
||||
@Test // gh-128
|
||||
public void executeShouldBindNamedValuesByIndex() {
|
||||
|
||||
|
||||
@@ -61,7 +61,7 @@ public abstract class AbstractR2dbcRepositoryIntegrationTests extends R2dbcInteg
|
||||
|
||||
@Autowired private LegoSetRepository repository;
|
||||
@Autowired private ConnectionFactory connectionFactory;
|
||||
private JdbcTemplate jdbc;
|
||||
protected JdbcTemplate jdbc;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
|
||||
@@ -16,24 +16,33 @@
|
||||
package org.springframework.data.r2dbc.repository;
|
||||
|
||||
import io.r2dbc.spi.ConnectionFactory;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan.Filter;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.FilterType;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.r2dbc.config.AbstractR2dbcConfiguration;
|
||||
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories;
|
||||
import org.springframework.data.r2dbc.repository.support.R2dbcRepositoryFactory;
|
||||
import org.springframework.data.r2dbc.testing.H2TestSupport;
|
||||
import org.springframework.data.relational.core.mapping.Table;
|
||||
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@@ -47,10 +56,11 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||
public class H2R2dbcRepositoryIntegrationTests extends AbstractR2dbcRepositoryIntegrationTests {
|
||||
|
||||
@Autowired private H2LegoSetRepository repository;
|
||||
@Autowired private IdOnlyEntityRepository idOnlyEntityRepository;
|
||||
|
||||
@Configuration
|
||||
@EnableR2dbcRepositories(considerNestedRepositories = true,
|
||||
includeFilters = @Filter(classes = H2LegoSetRepository.class, type = FilterType.ASSIGNABLE_TYPE))
|
||||
includeFilters = @Filter(classes = {H2LegoSetRepository.class, IdOnlyEntityRepository.class}, type = FilterType.ASSIGNABLE_TYPE))
|
||||
static class IntegrationTestConfiguration extends AbstractR2dbcConfiguration {
|
||||
|
||||
@Bean
|
||||
@@ -60,6 +70,17 @@ public class H2R2dbcRepositoryIntegrationTests extends AbstractR2dbcRepositoryIn
|
||||
}
|
||||
}
|
||||
|
||||
interface IdOnlyEntityRepository extends ReactiveCrudRepository<IdOnlyEntity, Integer> {
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Table("id_only")
|
||||
@NoArgsConstructor
|
||||
static class IdOnlyEntity {
|
||||
@Id Integer id;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected DataSource createDataSource() {
|
||||
return H2TestSupport.createDataSource();
|
||||
@@ -112,6 +133,18 @@ public class H2R2dbcRepositoryIntegrationTests extends AbstractR2dbcRepositoryIn
|
||||
repository.updateManualAndReturnNothing(42).as(StepVerifier::create).verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldInsertIdOnlyEntity() {
|
||||
this.jdbc.execute("CREATE TABLE ID_ONLY(id serial CONSTRAINT id_only_pk PRIMARY KEY)");
|
||||
|
||||
IdOnlyEntity entity1 = new IdOnlyEntity();
|
||||
idOnlyEntityRepository.saveAll(Arrays.asList(entity1))
|
||||
.as(StepVerifier::create) //
|
||||
.consumeNextWith( actual -> {
|
||||
assertThat(actual.getId()).isNotNull();
|
||||
}).verifyComplete();
|
||||
}
|
||||
|
||||
interface H2LegoSetRepository extends LegoSetRepository {
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user