Search in sources :

Example 6 with DeleteIf

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

the class DeleteStatementHandlerTest method bind_DeleteOperationWithIfGiven_ShouldBindProperly.

@Test
public void bind_DeleteOperationWithIfGiven_ShouldBindProperly() {
    // Arrange
    configureBehavior(null);
    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.bind(prepared, del);
    // Assert
    verify(bound).setString(0, ANY_TEXT_1);
    verify(bound).setString(1, ANY_TEXT_2);
    verify(bound).setInt(2, ANY_INT_1);
    verify(bound).setString(3, ANY_TEXT_3);
}
Also used : TextValue(com.scalar.db.io.TextValue) 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 7 with DeleteIf

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

the class OperationCheckerTest method whenCheckingDeleteOperationWithAllValidArguments_shouldNotThrowAnyException.

@Test
public void whenCheckingDeleteOperationWithAllValidArguments_shouldNotThrowAnyException() {
    // Arrange
    Key partitionKey = new Key(PKEY1, 1, PKEY2, "val1");
    Key clusteringKey = new Key(CKEY1, 2, CKEY2, "val1");
    MutationCondition condition = new DeleteIf(new ConditionalExpression(COL1, new IntValue(1), ConditionalExpression.Operator.EQ));
    Delete delete = new Delete(partitionKey, clusteringKey).withCondition(condition).forNamespace(NAMESPACE).forTable(TABLE_NAME);
    // Act Assert
    assertThatCode(() -> operationChecker.check(delete)).doesNotThrowAnyException();
}
Also used : Delete(com.scalar.db.api.Delete) MutationCondition(com.scalar.db.api.MutationCondition) ConditionalExpression(com.scalar.db.api.ConditionalExpression) IntValue(com.scalar.db.io.IntValue) Key(com.scalar.db.io.Key) DeleteIf(com.scalar.db.api.DeleteIf) Test(org.junit.jupiter.api.Test)

Example 8 with DeleteIf

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

the class ConditionalQueryBuilderTest method visit_DeleteIfAcceptCalled_ShouldCallWhere.

@Test
public void visit_DeleteIfAcceptCalled_ShouldCallWhere() {
    // Arrange
    DeleteIf condition = new DeleteIf(new ConditionalExpression(ANY_NAME_1, ANY_INT_VALUE, Operator.EQ), new ConditionalExpression(ANY_NAME_2, ANY_INT_VALUE, Operator.GT));
    ConditionalQueryBuilder builder = new ConditionalQueryBuilder(select);
    // Act
    condition.accept(builder);
    // Assert
    verify(select).and(DSL.field("r.values[\"" + ANY_NAME_1 + "\"]").equal(ANY_INT));
    verify(select).and(DSL.field("r.values[\"" + ANY_NAME_2 + "\"]").greaterThan(ANY_INT));
}
Also used : ConditionalExpression(com.scalar.db.api.ConditionalExpression) DeleteIf(com.scalar.db.api.DeleteIf) Test(org.junit.jupiter.api.Test)

Example 9 with DeleteIf

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

the class JdbcServiceTest method whenDeleteOperationWithDeleteIfConditionExecuted_shouldReturnTrueAndCallQueryBuilder.

@Test
public void whenDeleteOperationWithDeleteIfConditionExecuted_shouldReturnTrueAndCallQueryBuilder() throws Exception {
    // Arrange
    when(queryBuilder.deleteFrom(any(), any(), any())).thenReturn(deleteQueryBuilder);
    when(deleteQueryBuilder.where(any(), any(), any())).thenReturn(deleteQueryBuilder);
    when(deleteQueryBuilder.build()).thenReturn(deleteQuery);
    when(connection.prepareStatement(any())).thenReturn(preparedStatement);
    when(preparedStatement.executeUpdate()).thenReturn(1);
    // Act
    Delete delete = new Delete(new Key("p1", "val1")).withCondition(new DeleteIf(new ConditionalExpression("v1", new TextValue("val2"), ConditionalExpression.Operator.EQ))).forNamespace(NAMESPACE).forTable(TABLE);
    boolean ret = jdbcService.delete(delete, connection);
    // Assert
    assertThat(ret).isTrue();
    verify(operationChecker).check(any(Delete.class));
    verify(queryBuilder).deleteFrom(any(), any(), any());
}
Also used : Delete(com.scalar.db.api.Delete) TextValue(com.scalar.db.io.TextValue) ConditionalExpression(com.scalar.db.api.ConditionalExpression) Key(com.scalar.db.io.Key) DeleteIf(com.scalar.db.api.DeleteIf) Test(org.junit.jupiter.api.Test)

Example 10 with DeleteIf

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

the class ConditionExpressionBuilderTest method visit_DeleteIfAcceptCalled_ShouldCallWhere.

@Test
public void visit_DeleteIfAcceptCalled_ShouldCallWhere() {
    // Arrange
    DeleteIf condition = ConditionBuilder.deleteIf(ConditionBuilder.column(ANY_NAME_1).isEqualToInt(ANY_INT_VALUE.getAsInt())).and(ConditionBuilder.column(ANY_NAME_2).isGreaterThanInt(ANY_INT_VALUE.getAsInt())).and(ConditionBuilder.column(ANY_NAME_3).isNullInt()).and(ConditionBuilder.column(ANY_NAME_4).isNotNullInt()).build();
    ConditionExpressionBuilder builder = new ConditionExpressionBuilder(DynamoOperation.CONDITION_COLUMN_NAME_ALIAS, DynamoOperation.CONDITION_VALUE_ALIAS);
    // Act
    condition.accept(builder);
    String actual = builder.build();
    // Assert
    assertThat(actual).isEqualTo("#ccol0 = :cval0 AND #ccol1 > :cval1 " + "AND (attribute_not_exists(#ccol2) OR #ccol2 = :cval2) " + "AND (attribute_exists(#ccol3) AND NOT #ccol3 = :cval3)");
}
Also used : 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