Search in sources :

Example 1 with DeleteIf

use of com.scalar.db.api.DeleteIf in project scalardb by scalar-labs.

the class StorageIntegrationTestBase method delete_DeleteWithIfGivenWhenSuchRecordExists_ShouldDeleteProperly.

@Test
public void delete_DeleteWithIfGivenWhenSuchRecordExists_ShouldDeleteProperly() throws ExecutionException {
    // Arrange
    populateRecords();
    int pKey = 0;
    int cKey = 0;
    Key partitionKey = new Key(COL_NAME1, pKey);
    Key clusteringKey = new Key(COL_NAME4, cKey);
    // Act
    Delete delete = prepareDelete(pKey, cKey);
    delete.withCondition(new DeleteIf(new ConditionalExpression(COL_NAME2, new TextValue(Integer.toString(pKey)), ConditionalExpression.Operator.EQ)));
    assertThatCode(() -> storage.delete(delete)).doesNotThrowAnyException();
    // Assert
    Optional<Result> actual = storage.get(new Get(partitionKey, clusteringKey));
    assertThat(actual.isPresent()).isFalse();
}
Also used : Delete(com.scalar.db.api.Delete) TextValue(com.scalar.db.io.TextValue) ConditionalExpression(com.scalar.db.api.ConditionalExpression) Get(com.scalar.db.api.Get) Key(com.scalar.db.io.Key) DeleteIf(com.scalar.db.api.DeleteIf) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 2 with DeleteIf

use of com.scalar.db.api.DeleteIf in project scalardb by scalar-labs.

the class StorageIntegrationTestBase method delete_MultipleDeleteWithDifferentConditionsGiven_ShouldDeleteProperly.

@Test
public void delete_MultipleDeleteWithDifferentConditionsGiven_ShouldDeleteProperly() throws IOException, ExecutionException {
    // Arrange
    List<Put> puts = preparePuts();
    List<Delete> deletes = prepareDeletes();
    storage.mutate(Arrays.asList(puts.get(0), puts.get(1), puts.get(2)));
    deletes.get(0).withCondition(new DeleteIfExists());
    deletes.get(1).withCondition(new DeleteIf(new ConditionalExpression(COL_NAME2, new TextValue("1"), ConditionalExpression.Operator.EQ)));
    // Act
    assertThatCode(() -> storage.delete(Arrays.asList(deletes.get(0), deletes.get(1), deletes.get(2)))).doesNotThrowAnyException();
    // Assert
    List<Result> results = scanAll(new Scan(new Key(COL_NAME1, 0)));
    assertThat(results.size()).isEqualTo(0);
}
Also used : Delete(com.scalar.db.api.Delete) TextValue(com.scalar.db.io.TextValue) ConditionalExpression(com.scalar.db.api.ConditionalExpression) Scan(com.scalar.db.api.Scan) Put(com.scalar.db.api.Put) DeleteIfExists(com.scalar.db.api.DeleteIfExists) DeleteIf(com.scalar.db.api.DeleteIf) Key(com.scalar.db.io.Key) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 3 with DeleteIf

use of com.scalar.db.api.DeleteIf in project scalardb by scalar-labs.

the class StorageWithReservedKeywordIntegrationTestBase method delete_WithReservedKeywordAndMultipleDeleteWithDifferentConditionsGiven_ShouldDeleteProperly.

@Test
public void delete_WithReservedKeywordAndMultipleDeleteWithDifferentConditionsGiven_ShouldDeleteProperly() throws IOException, ExecutionException {
    // Arrange
    List<Put> puts = preparePuts();
    List<Delete> deletes = prepareDeletes();
    storage.mutate(Arrays.asList(puts.get(0), puts.get(1), puts.get(2)));
    deletes.get(0).withCondition(new DeleteIfExists());
    deletes.get(1).withCondition(new DeleteIf(new ConditionalExpression(columnName2, new TextValue("1"), ConditionalExpression.Operator.EQ)));
    // Act
    assertThatCode(() -> storage.delete(Arrays.asList(deletes.get(0), deletes.get(1), deletes.get(2)))).doesNotThrowAnyException();
    // Assert
    List<Result> results = scanAll(new Scan(new Key(columnName1, 0)));
    assertThat(results.size()).isEqualTo(0);
}
Also used : Delete(com.scalar.db.api.Delete) TextValue(com.scalar.db.io.TextValue) ConditionalExpression(com.scalar.db.api.ConditionalExpression) Scan(com.scalar.db.api.Scan) Put(com.scalar.db.api.Put) DeleteIfExists(com.scalar.db.api.DeleteIfExists) DeleteIf(com.scalar.db.api.DeleteIf) Key(com.scalar.db.io.Key) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 4 with DeleteIf

use of com.scalar.db.api.DeleteIf in project scalardb by scalar-labs.

the class DeleteStatementHandlerTest method prepare_DeleteOperationWithIfGiven_ShouldCallAccept.

@Test
public void prepare_DeleteOperationWithIfGiven_ShouldCallAccept() {
    // Arrange
    configureBehavior(null);
    del = prepareDeleteWithClusteringKey();
    DeleteIf deleteIf = spy(new DeleteIf(new ConditionalExpression(ANY_NAME_4, new IntValue(ANY_INT_2), Operator.EQ)));
    del.withCondition(deleteIf);
    // Act
    handler.prepare(del);
    // Assert
    verify(deleteIf).accept(any(ConditionSetter.class));
}
Also used : ConditionalExpression(com.scalar.db.api.ConditionalExpression) IntValue(com.scalar.db.io.IntValue) DeleteIf(com.scalar.db.api.DeleteIf) Test(org.junit.jupiter.api.Test)

Example 5 with DeleteIf

use of com.scalar.db.api.DeleteIf in project scalardb by scalar-labs.

the class DeleteStatementHandlerTest method prepare_DeleteOperationWithIfGiven_ShouldPrepareProperQuery.

@Test
public void prepare_DeleteOperationWithIfGiven_ShouldPrepareProperQuery() {
    // Arrange
    String expected = Joiner.on(" ").skipNulls().join(new String[] { "DELETE", "FROM", ANY_NAMESPACE_NAME + "." + ANY_TABLE_NAME, "WHERE", ANY_NAME_1 + "=?", "AND", ANY_NAME_2 + "=?", "IF", ANY_NAME_3 + "=?", "AND", ANY_NAME_4 + "=?;" });
    configureBehavior(expected);
    del = prepareDeleteWithClusteringKey();
    del.withCondition(new DeleteIf(new ConditionalExpression(ANY_NAME_3, new IntValue(ANY_INT_1), Operator.EQ), new ConditionalExpression(ANY_NAME_4, new TextValue(ANY_TEXT_3), Operator.EQ)));
    // Act
    handler.prepare(del);
    // Assert
    verify(session).prepare(expected);
}
Also used : TextValue(com.scalar.db.io.TextValue) ConditionalExpression(com.scalar.db.api.ConditionalExpression) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) IntValue(com.scalar.db.io.IntValue) DeleteIf(com.scalar.db.api.DeleteIf) Test(org.junit.jupiter.api.Test)

Aggregations

DeleteIf (com.scalar.db.api.DeleteIf)20 ConditionalExpression (com.scalar.db.api.ConditionalExpression)18 Test (org.junit.jupiter.api.Test)15 Delete (com.scalar.db.api.Delete)13 TextValue (com.scalar.db.io.TextValue)12 Key (com.scalar.db.io.Key)10 IntValue (com.scalar.db.io.IntValue)6 Get (com.scalar.db.api.Get)5 Result (com.scalar.db.api.Result)5 Test (org.junit.Test)5 MutationCondition (com.scalar.db.api.MutationCondition)3 Put (com.scalar.db.api.Put)3 Scan (com.scalar.db.api.Scan)3 DeleteIfExists (com.scalar.db.api.DeleteIfExists)2 BooleanValue (com.scalar.db.io.BooleanValue)1 DoubleValue (com.scalar.db.io.DoubleValue)1 Value (com.scalar.db.io.Value)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1