Search in sources :

Example 11 with ConditionalExpression

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

the class UpdateStatementHandlerTest method prepare_PutOperationWithIfGiven_ShouldPrepareProperQuery.

@Test
public void prepare_PutOperationWithIfGiven_ShouldPrepareProperQuery() {
    // Arrange
    String expected = Joiner.on(" ").skipNulls().join(new String[] { "UPDATE", ANY_NAMESPACE_NAME + "." + ANY_TABLE_NAME, "SET", ANY_NAME_3 + "=?", "WHERE", ANY_NAME_1 + "=?", "AND", ANY_NAME_2 + "=?", "IF", ANY_NAME_4 + "=?", "AND", ANY_NAME_4 + "!=?", "AND", ANY_NAME_4 + ">?", "AND", ANY_NAME_4 + ">=?", "AND", ANY_NAME_4 + "<?", "AND", ANY_NAME_4 + "<=?;" });
    configureBehavior(expected);
    put = preparePutWithClusteringKey();
    put.withCondition(new PutIf(new ConditionalExpression(ANY_NAME_4, new IntValue(ANY_INT_2), Operator.EQ), new ConditionalExpression(ANY_NAME_4, new IntValue(ANY_INT_2), Operator.NE), new ConditionalExpression(ANY_NAME_4, new IntValue(ANY_INT_2), Operator.GT), new ConditionalExpression(ANY_NAME_4, new IntValue(ANY_INT_2), Operator.GTE), new ConditionalExpression(ANY_NAME_4, new IntValue(ANY_INT_2), Operator.LT), new ConditionalExpression(ANY_NAME_4, new IntValue(ANY_INT_2), Operator.LTE)));
    // Act
    handler.prepare(put);
    // Assert
    verify(session).prepare(expected);
}
Also used : PutIf(com.scalar.db.api.PutIf) ConditionalExpression(com.scalar.db.api.ConditionalExpression) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) IntValue(com.scalar.db.io.IntValue) Test(org.junit.jupiter.api.Test)

Example 12 with ConditionalExpression

use of com.scalar.db.api.ConditionalExpression 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 13 with ConditionalExpression

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

the class OperationCheckerTest method whenCheckingPutOperationWithInvalidPutIfCondition_shouldThrowIllegalArgumentException.

@Test
public void whenCheckingPutOperationWithInvalidPutIfCondition_shouldThrowIllegalArgumentException() {
    // Arrange
    Key partitionKey = new Key(PKEY1, 1, PKEY2, "val1");
    Key clusteringKey = new Key(CKEY1, 2, CKEY2, "val1");
    List<Value<?>> values = Arrays.asList(new IntValue(COL1, 1), new DoubleValue(COL2, 0.1), new BooleanValue(COL3, true));
    MutationCondition condition = new PutIf(new ConditionalExpression(COL1, new TextValue("1"), ConditionalExpression.Operator.EQ));
    Put put = new Put(partitionKey, clusteringKey).withValues(values).withCondition(condition).forNamespace(NAMESPACE).forTable(TABLE_NAME);
    // Act Assert
    assertThatThrownBy(() -> operationChecker.check(put)).isInstanceOf(IllegalArgumentException.class);
}
Also used : PutIf(com.scalar.db.api.PutIf) DoubleValue(com.scalar.db.io.DoubleValue) MutationCondition(com.scalar.db.api.MutationCondition) TextValue(com.scalar.db.io.TextValue) BooleanValue(com.scalar.db.io.BooleanValue) ConditionalExpression(com.scalar.db.api.ConditionalExpression) IntValue(com.scalar.db.io.IntValue) DoubleValue(com.scalar.db.io.DoubleValue) TextValue(com.scalar.db.io.TextValue) Value(com.scalar.db.io.Value) BooleanValue(com.scalar.db.io.BooleanValue) IntValue(com.scalar.db.io.IntValue) Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put) Test(org.junit.jupiter.api.Test)

Example 14 with ConditionalExpression

use of com.scalar.db.api.ConditionalExpression 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 15 with ConditionalExpression

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

the class JdbcServiceTest method whenPutOperationWithPutIfConditionFails_shouldReturnFalseAndCallQueryBuilder.

@Test
public void whenPutOperationWithPutIfConditionFails_shouldReturnFalseAndCallQueryBuilder() throws Exception {
    // Arrange
    when(queryBuilder.update(any(), any(), any())).thenReturn(updateQueryBuilder);
    when(updateQueryBuilder.set(any())).thenReturn(updateQueryBuilder);
    when(updateQueryBuilder.where(any(), any(), any())).thenReturn(updateQueryBuilder);
    when(updateQueryBuilder.build()).thenReturn(updateQuery);
    when(connection.prepareStatement(any())).thenReturn(preparedStatement);
    when(preparedStatement.executeUpdate()).thenReturn(0);
    // Act
    Put put = new Put(new Key("p1", "val1")).withValue("v1", "val2").withCondition(new PutIf(new ConditionalExpression("v1", new TextValue("val2"), ConditionalExpression.Operator.EQ))).forNamespace(NAMESPACE).forTable(TABLE);
    boolean ret = jdbcService.put(put, connection);
    // Assert
    assertThat(ret).isFalse();
    verify(operationChecker).check(any(Put.class));
    verify(queryBuilder).update(any(), any(), any());
}
Also used : PutIf(com.scalar.db.api.PutIf) TextValue(com.scalar.db.io.TextValue) ConditionalExpression(com.scalar.db.api.ConditionalExpression) Put(com.scalar.db.api.Put) Key(com.scalar.db.io.Key) Test(org.junit.jupiter.api.Test)

Aggregations

ConditionalExpression (com.scalar.db.api.ConditionalExpression)55 Test (org.junit.jupiter.api.Test)35 PutIf (com.scalar.db.api.PutIf)30 Put (com.scalar.db.api.Put)27 TextValue (com.scalar.db.io.TextValue)22 DeleteIf (com.scalar.db.api.DeleteIf)18 Key (com.scalar.db.io.Key)18 Delete (com.scalar.db.api.Delete)14 Get (com.scalar.db.api.Get)13 IntValue (com.scalar.db.io.IntValue)13 Test (org.junit.Test)12 Result (com.scalar.db.api.Result)10 Scan (com.scalar.db.api.Scan)7 HashMap (java.util.HashMap)6 PutIfNotExists (com.scalar.db.api.PutIfNotExists)4 PreparedStatement (java.sql.PreparedStatement)4 MutationCondition (com.scalar.db.api.MutationCondition)3 Value (com.scalar.db.io.Value)3 DeleteIfExists (com.scalar.db.api.DeleteIfExists)2 Attribute.toIdValue (com.scalar.db.transaction.consensuscommit.Attribute.toIdValue)2