Skip to content

Insert should work without giving any explicit assignment/variables #390

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
Closed
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -1044,10 +1044,6 @@ public Mono<Void> then() {

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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,6 @@ private PreparedOperation<Insert> getMappedObject(InsertSpec insertSpec,

Bindings bindings;

if (boundAssignments.getAssignments().isEmpty()) {
throw new IllegalStateException("INSERT contains no values");
}

bindings = boundAssignments.getBindings();

InsertBuilder.InsertIntoColumnsAndValues insertBuilder = StatementBuilder.insert(table);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,22 @@ public void insertShouldAcceptSettableValue() {
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() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -47,10 +56,11 @@
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
Expand All @@ -60,6 +70,17 @@ public ConnectionFactory connectionFactory() {
}
}

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();
Expand Down Expand Up @@ -112,6 +133,18 @@ public void shouldNotReturnUpdateCount() {
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
Expand Down