#410 - Make it possible to write delete/update operations without using matching.

Fluent operations now allow statement execution without the need to specify a Query object allowing for shorter statements.
This commit is contained in:
Mark Paluch
2020-07-23 15:32:55 +02:00
parent c216d9b904
commit 07be001e54
4 changed files with 80 additions and 6 deletions

View File

@@ -56,7 +56,7 @@ public interface ReactiveDeleteOperation {
/**
* Table override (optional).
*/
interface DeleteWithTable {
interface DeleteWithTable extends TerminatingDelete {
/**
* Explicitly set the {@link String name} of the table on which to perform the delete.
@@ -88,7 +88,7 @@ public interface ReactiveDeleteOperation {
/**
* Required {@link Query filter}.
*/
interface DeleteWithQuery {
interface DeleteWithQuery extends TerminatingDelete {
/**
* Define the {@link Query} used to filter elements in the delete.

View File

@@ -60,7 +60,7 @@ public interface ReactiveUpdateOperation {
/**
* Table override (optional).
*/
interface UpdateWithTable {
interface UpdateWithTable extends TerminatingUpdate {
/**
* Explicitly set the {@link String name} of the table on which to perform the update.
@@ -92,7 +92,7 @@ public interface ReactiveUpdateOperation {
/**
* Define a {@link Query} used as the filter for the {@link Update}.
*/
interface UpdateWithQuery {
interface UpdateWithQuery extends TerminatingUpdate {
/**
* Filter rows to update by the given {@link Query}.

View File

@@ -51,13 +51,49 @@ public class ReactiveDeleteOperationUnitTests {
entityTemplate = new R2dbcEntityTemplate(client);
}
@Test // gh-220
@Test // gh-410
public void shouldDelete() {
MockResult result = MockResult.builder().rowsUpdated(1).build();
recorder.addStubbing(s -> s.startsWith("DELETE"), result);
entityTemplate.delete(Person.class) //
.all() //
.as(StepVerifier::create) //
.expectNext(1) //
.verifyComplete();
StatementRecorder.RecordedStatement statement = recorder.getCreatedStatement(s -> s.startsWith("DELETE"));
assertThat(statement.getSql()).isEqualTo("DELETE FROM person");
}
@Test // gh-410
public void shouldDeleteWithTable() {
MockResult result = MockResult.builder().rowsUpdated(1).build();
recorder.addStubbing(s -> s.startsWith("DELETE"), result);
entityTemplate.delete(Person.class) //
.from("table").all() //
.as(StepVerifier::create) //
.expectNext(1) //
.verifyComplete();
StatementRecorder.RecordedStatement statement = recorder.getCreatedStatement(s -> s.startsWith("DELETE"));
assertThat(statement.getSql()).isEqualTo("DELETE FROM table");
}
@Test // gh-220
public void shouldDeleteWithQuery() {
MockResult result = MockResult.builder().rowsUpdated(1).build();
recorder.addStubbing(s -> s.startsWith("DELETE"), result);
entityTemplate.delete(Person.class) //
.matching(query(where("name").is("Walter"))) //
.all() //

View File

@@ -52,13 +52,51 @@ public class ReactiveUpdateOperationUnitTests {
entityTemplate = new R2dbcEntityTemplate(client);
}
@Test // gh-220
@Test // gh-410
public void shouldUpdate() {
MockResult result = MockResult.builder().rowsUpdated(1).build();
recorder.addStubbing(s -> s.startsWith("UPDATE"), result);
entityTemplate.update(Person.class) //
.apply(Update.update("name", "Heisenberg")) //
.as(StepVerifier::create) //
.expectNext(1) //
.verifyComplete();
StatementRecorder.RecordedStatement statement = recorder.getCreatedStatement(s -> s.startsWith("UPDATE"));
assertThat(statement.getSql()).isEqualTo("UPDATE person SET THE_NAME = $1");
assertThat(statement.getBindings()).hasSize(1).containsEntry(0, SettableValue.from("Heisenberg"));
}
@Test // gh-410
public void shouldUpdateWithTable() {
MockResult result = MockResult.builder().rowsUpdated(1).build();
recorder.addStubbing(s -> s.startsWith("UPDATE"), result);
entityTemplate.update(Person.class) //
.inTable("table").apply(Update.update("name", "Heisenberg")) //
.as(StepVerifier::create) //
.expectNext(1) //
.verifyComplete();
StatementRecorder.RecordedStatement statement = recorder.getCreatedStatement(s -> s.startsWith("UPDATE"));
assertThat(statement.getSql()).isEqualTo("UPDATE table SET THE_NAME = $1");
assertThat(statement.getBindings()).hasSize(1).containsEntry(0, SettableValue.from("Heisenberg"));
}
@Test // gh-220
public void shouldUpdateWithQuery() {
MockResult result = MockResult.builder().rowsUpdated(1).build();
recorder.addStubbing(s -> s.startsWith("UPDATE"), result);
entityTemplate.update(Person.class) //
.matching(query(where("name").is("Walter"))) //
.apply(Update.update("name", "Heisenberg")) //