Search in sources :

Example 1 with ConditionalExpression

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

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

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

the class StorageIntegrationTestBase method put_PutWithIfGivenWhenNoSuchRecord_ShouldThrowNoMutationException.

@Test
public void put_PutWithIfGivenWhenNoSuchRecord_ShouldThrowNoMutationException() throws ExecutionException {
    // Arrange
    int pKey = 0;
    int cKey = 0;
    List<Put> puts = preparePuts();
    Get get = prepareGet(pKey, cKey);
    // Act Assert
    storage.put(puts.get(0));
    puts.get(0).withCondition(new PutIf(new ConditionalExpression(COL_NAME3, new IntValue(pKey + cKey + 1), ConditionalExpression.Operator.EQ)));
    puts.get(0).withValue(COL_NAME3, Integer.MAX_VALUE);
    assertThatThrownBy(() -> storage.put(puts.get(0))).isInstanceOf(NoMutationException.class);
    // Assert
    Optional<Result> actual = storage.get(get);
    assertThat(actual.isPresent()).isTrue();
    Result result = actual.get();
    assertThat(result.getValue(COL_NAME1)).isEqualTo(Optional.of(new IntValue(COL_NAME1, pKey)));
    assertThat(result.getValue(COL_NAME4)).isEqualTo(Optional.of(new IntValue(COL_NAME4, cKey)));
    assertThat(result.getValue(COL_NAME3)).isEqualTo(Optional.of(new IntValue(COL_NAME3, pKey + cKey)));
}
Also used : PutIf(com.scalar.db.api.PutIf) Get(com.scalar.db.api.Get) ConditionalExpression(com.scalar.db.api.ConditionalExpression) IntValue(com.scalar.db.io.IntValue) Put(com.scalar.db.api.Put) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 4 with ConditionalExpression

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

the class QueryBuilderTest method deleteQueryTest.

@Test
public void deleteQueryTest() throws SQLException {
    DeleteQuery query;
    PreparedStatement preparedStatement;
    preparedStatement = mock(PreparedStatement.class);
    query = queryBuilder.deleteFrom(NAMESPACE, TABLE, TABLE_METADATA).where(new Key("p1", "p1Value"), Optional.empty()).build();
    assertThat(query.sql()).isEqualTo(encloseSql("DELETE FROM n1.t1 WHERE p1=?"));
    query.bind(preparedStatement);
    verify(preparedStatement).setString(1, "p1Value");
    preparedStatement = mock(PreparedStatement.class);
    query = queryBuilder.deleteFrom(NAMESPACE, TABLE, TABLE_METADATA).where(new Key("p1", "p1Value"), Optional.of(new Key("c1", "c1Value"))).build();
    assertThat(query.sql()).isEqualTo(encloseSql("DELETE FROM n1.t1 WHERE p1=? AND c1=?"));
    query.bind(preparedStatement);
    verify(preparedStatement).setString(1, "p1Value");
    verify(preparedStatement).setString(2, "c1Value");
    preparedStatement = mock(PreparedStatement.class);
    query = queryBuilder.deleteFrom(NAMESPACE, TABLE, TABLE_METADATA).where(new Key("p1", "p1Value", "p2", "p2Value"), Optional.of(new Key("c1", "c1Value", "c2", "c2Value"))).build();
    assertThat(query.sql()).isEqualTo(encloseSql("DELETE FROM n1.t1 WHERE p1=? AND p2=? AND c1=? AND c2=?"));
    query.bind(preparedStatement);
    verify(preparedStatement).setString(1, "p1Value");
    verify(preparedStatement).setString(2, "p2Value");
    verify(preparedStatement).setString(3, "c1Value");
    verify(preparedStatement).setString(4, "c2Value");
    preparedStatement = mock(PreparedStatement.class);
    query = queryBuilder.deleteFrom(NAMESPACE, TABLE, TABLE_METADATA).where(new Key("p1", "p1Value"), Optional.of(new Key("c1", "c1Value")), Collections.singletonList(new ConditionalExpression("v1", new TextValue("v1ConditionValue"), Operator.EQ))).build();
    assertThat(query.sql()).isEqualTo(encloseSql("DELETE FROM n1.t1 WHERE p1=? AND c1=? AND v1=?"));
    query.bind(preparedStatement);
    verify(preparedStatement).setString(1, "p1Value");
    verify(preparedStatement).setString(2, "c1Value");
    verify(preparedStatement).setString(3, "v1ConditionValue");
    preparedStatement = mock(PreparedStatement.class);
    query = queryBuilder.deleteFrom(NAMESPACE, TABLE, TABLE_METADATA).where(new Key("p1", "p1Value"), Optional.of(new Key("c1", "c1Value")), Arrays.asList(new ConditionalExpression("v1", new TextValue("v1ConditionValue"), Operator.NE), new ConditionalExpression("v2", new TextValue("v2ConditionValue"), Operator.GTE), new ConditionalExpression("v3", new TextValue("v3ConditionValue"), Operator.LT))).build();
    assertThat(query.sql()).isEqualTo(encloseSql("DELETE FROM n1.t1 WHERE p1=? AND c1=? AND v1<>? AND v2>=? AND v3<?"));
    query.bind(preparedStatement);
    verify(preparedStatement).setString(1, "p1Value");
    verify(preparedStatement).setString(2, "c1Value");
    verify(preparedStatement).setString(3, "v1ConditionValue");
    verify(preparedStatement).setString(4, "v2ConditionValue");
    verify(preparedStatement).setString(5, "v3ConditionValue");
}
Also used : TextValue(com.scalar.db.io.TextValue) ConditionalExpression(com.scalar.db.api.ConditionalExpression) PreparedStatement(java.sql.PreparedStatement) Key(com.scalar.db.io.Key) Test(org.junit.Test)

Example 5 with ConditionalExpression

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

the class StorageWithReservedKeywordIntegrationTestBase method put_PutWithReservedKeywordAndIfGivenWhenSuchRecordExists_ShouldUpdateRecord.

@Test
public void put_PutWithReservedKeywordAndIfGivenWhenSuchRecordExists_ShouldUpdateRecord() throws ExecutionException {
    // Arrange
    int pKey = 0;
    int cKey = 0;
    List<Put> puts = preparePuts();
    Get get = prepareGet(pKey, cKey);
    // Act Assert
    storage.put(puts.get(0));
    puts.get(0).withCondition(new PutIf(new ConditionalExpression(columnName3, new IntValue(pKey + cKey), ConditionalExpression.Operator.EQ)));
    puts.get(0).withValue(columnName3, Integer.MAX_VALUE);
    assertThatCode(() -> storage.put(puts.get(0))).doesNotThrowAnyException();
    // Assert
    Optional<Result> actual = storage.get(get);
    assertThat(actual.isPresent()).isTrue();
    Result result = actual.get();
    assertThat(result.getValue(columnName1)).isEqualTo(Optional.of(new IntValue(columnName1, pKey)));
    assertThat(result.getValue(columnName4)).isEqualTo(Optional.of(new IntValue(columnName4, cKey)));
    assertThat(result.getValue(columnName3)).isEqualTo(Optional.of(new IntValue(columnName3, Integer.MAX_VALUE)));
}
Also used : PutIf(com.scalar.db.api.PutIf) Get(com.scalar.db.api.Get) ConditionalExpression(com.scalar.db.api.ConditionalExpression) IntValue(com.scalar.db.io.IntValue) Put(com.scalar.db.api.Put) Result(com.scalar.db.api.Result) Test(org.junit.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