Search in sources :

Example 51 with ConditionalExpression

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

the class StorageIntegrationTestBase method delete_DeleteWithIfGivenWhenNoSuchRecord_ShouldThrowNoMutationException.

@Test
public void delete_DeleteWithIfGivenWhenNoSuchRecord_ShouldThrowNoMutationException() 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(Integer.MAX_VALUE)), ConditionalExpression.Operator.EQ)));
    assertThatThrownBy(() -> storage.delete(delete)).isInstanceOf(NoMutationException.class);
    // Assert
    Optional<Result> actual = storage.get(new Get(partitionKey, clusteringKey));
    assertThat(actual.isPresent()).isTrue();
}
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 52 with ConditionalExpression

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

the class StorageIntegrationTestBase method put_PutWithIfGivenWhenSuchRecordExists_ShouldUpdateRecord.

@Test
public void put_PutWithIfGivenWhenSuchRecordExists_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(COL_NAME3, new IntValue(pKey + cKey), ConditionalExpression.Operator.EQ)));
    puts.get(0).withValue(COL_NAME3, 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(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, 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)

Example 53 with ConditionalExpression

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

the class StorageWithReservedKeywordIntegrationTestBase method put_WithReservedKeywordAndMultiplePutWithDifferentConditionsGiven_ShouldStoreProperly.

@Test
public void put_WithReservedKeywordAndMultiplePutWithDifferentConditionsGiven_ShouldStoreProperly() throws IOException, ExecutionException {
    // Arrange
    List<Put> puts = preparePuts();
    storage.put(puts.get(1));
    puts.get(0).withCondition(new PutIfNotExists());
    puts.get(1).withCondition(new PutIf(new ConditionalExpression(columnName2, new TextValue("1"), ConditionalExpression.Operator.EQ)));
    // Act
    assertThatCode(() -> storage.put(Arrays.asList(puts.get(0), puts.get(1)))).doesNotThrowAnyException();
    // Assert
    List<Result> results = scanAll(new Scan(new Key(columnName1, 0)));
    assertThat(results.size()).isEqualTo(2);
    assertThat(results.get(0).getValue(columnName1).isPresent()).isTrue();
    assertThat(results.get(0).getValue(columnName1).get().getAsInt()).isEqualTo(0);
    assertThat(results.get(0).getValue(columnName4).isPresent()).isTrue();
    assertThat(results.get(0).getValue(columnName4).get().getAsInt()).isEqualTo(0);
    assertThat(results.get(1).getValue(columnName1).isPresent()).isTrue();
    assertThat(results.get(1).getValue(columnName1).get().getAsInt()).isEqualTo(0);
    assertThat(results.get(1).getValue(columnName4).isPresent()).isTrue();
    assertThat(results.get(1).getValue(columnName4).get().getAsInt()).isEqualTo(1);
}
Also used : PutIfNotExists(com.scalar.db.api.PutIfNotExists) PutIf(com.scalar.db.api.PutIf) TextValue(com.scalar.db.io.TextValue) ConditionalExpression(com.scalar.db.api.ConditionalExpression) Scan(com.scalar.db.api.Scan) Put(com.scalar.db.api.Put) Key(com.scalar.db.io.Key) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 54 with ConditionalExpression

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

the class StorageWithReservedKeywordIntegrationTestBase method delete_WithReservedKeywordAndDeleteWithIfGivenWhenSuchRecordExists_ShouldDeleteProperly.

@Test
public void delete_WithReservedKeywordAndDeleteWithIfGivenWhenSuchRecordExists_ShouldDeleteProperly() throws ExecutionException {
    // Arrange
    populateRecords();
    int pKey = 0;
    int cKey = 0;
    Key partitionKey = new Key(columnName1, pKey);
    Key clusteringKey = new Key(columnName4, cKey);
    // Act
    Delete delete = prepareDelete(pKey, cKey);
    delete.withCondition(new DeleteIf(new ConditionalExpression(columnName2, 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 55 with ConditionalExpression

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

the class QueryBuilderTest method updateQueryTest.

@Test
public void updateQueryTest() throws SQLException {
    UpdateQuery query;
    PreparedStatement preparedStatement;
    Map<String, Optional<Value<?>>> values = new HashMap<>();
    values.put("v1", Optional.of(new TextValue("v1Value")));
    values.put("v2", Optional.of(new TextValue("v2Value")));
    values.put("v3", Optional.of(new TextValue("v3Value")));
    preparedStatement = mock(PreparedStatement.class);
    query = queryBuilder.update(NAMESPACE, TABLE, TABLE_METADATA).set(values).where(new Key("p1", "p1Value"), Optional.empty()).build();
    assertThat(query.sql()).isEqualTo(encloseSql("UPDATE n1.t1 SET v1=?,v2=?,v3=? WHERE p1=?"));
    query.bind(preparedStatement);
    verify(preparedStatement).setString(1, "v1Value");
    verify(preparedStatement).setString(2, "v2Value");
    verify(preparedStatement).setString(3, "v3Value");
    verify(preparedStatement).setString(4, "p1Value");
    preparedStatement = mock(PreparedStatement.class);
    query = queryBuilder.update(NAMESPACE, TABLE, TABLE_METADATA).set(values).where(new Key("p1", "p1Value"), Optional.of(new Key("c1", "c1Value"))).build();
    assertThat(query.sql()).isEqualTo(encloseSql("UPDATE n1.t1 SET v1=?,v2=?,v3=? WHERE p1=? AND c1=?"));
    query.bind(preparedStatement);
    verify(preparedStatement).setString(1, "v1Value");
    verify(preparedStatement).setString(2, "v2Value");
    verify(preparedStatement).setString(3, "v3Value");
    verify(preparedStatement).setString(4, "p1Value");
    verify(preparedStatement).setString(5, "c1Value");
    preparedStatement = mock(PreparedStatement.class);
    query = queryBuilder.update(NAMESPACE, TABLE, TABLE_METADATA).set(values).where(new Key("p1", "p1Value", "p2", "p2Value"), Optional.of(new Key("c1", "c1Value", "c2", "c2Value"))).build();
    assertThat(query.sql()).isEqualTo(encloseSql("UPDATE n1.t1 SET v1=?,v2=?,v3=? WHERE p1=? AND p2=? AND c1=? AND c2=?"));
    query.bind(preparedStatement);
    verify(preparedStatement).setString(1, "v1Value");
    verify(preparedStatement).setString(2, "v2Value");
    verify(preparedStatement).setString(3, "v3Value");
    verify(preparedStatement).setString(4, "p1Value");
    verify(preparedStatement).setString(5, "p2Value");
    verify(preparedStatement).setString(6, "c1Value");
    verify(preparedStatement).setString(7, "c2Value");
    preparedStatement = mock(PreparedStatement.class);
    query = queryBuilder.update(NAMESPACE, TABLE, TABLE_METADATA).set(values).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("UPDATE n1.t1 SET v1=?,v2=?,v3=? WHERE p1=? AND c1=? AND v1=?"));
    query.bind(preparedStatement);
    verify(preparedStatement).setString(1, "v1Value");
    verify(preparedStatement).setString(2, "v2Value");
    verify(preparedStatement).setString(3, "v3Value");
    verify(preparedStatement).setString(4, "p1Value");
    verify(preparedStatement).setString(5, "c1Value");
    verify(preparedStatement).setString(6, "v1ConditionValue");
    preparedStatement = mock(PreparedStatement.class);
    query = queryBuilder.update(NAMESPACE, TABLE, TABLE_METADATA).set(values).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.GT), new ConditionalExpression("v3", new TextValue("v3ConditionValue"), Operator.LTE))).build();
    assertThat(query.sql()).isEqualTo(encloseSql("UPDATE n1.t1 SET v1=?,v2=?,v3=? WHERE p1=? AND c1=? AND v1<>? AND v2>? AND v3<=?"));
    query.bind(preparedStatement);
    verify(preparedStatement).setString(1, "v1Value");
    verify(preparedStatement).setString(2, "v2Value");
    verify(preparedStatement).setString(3, "v3Value");
    verify(preparedStatement).setString(4, "p1Value");
    verify(preparedStatement).setString(5, "c1Value");
    verify(preparedStatement).setString(6, "v1ConditionValue");
    verify(preparedStatement).setString(7, "v2ConditionValue");
    verify(preparedStatement).setString(8, "v3ConditionValue");
    values.put("v4", Optional.empty());
    preparedStatement = mock(PreparedStatement.class);
    query = queryBuilder.update(NAMESPACE, TABLE, TABLE_METADATA).set(values).where(new Key("p1", "p1Value"), Optional.of(new Key("c1", "c1Value"))).build();
    assertThat(query.sql()).isEqualTo(encloseSql("UPDATE n1.t1 SET v1=?,v2=?,v3=?,v4=? WHERE p1=? AND c1=?"));
    query.bind(preparedStatement);
    verify(preparedStatement).setString(1, "v1Value");
    verify(preparedStatement).setString(2, "v2Value");
    verify(preparedStatement).setString(3, "v3Value");
    verify(preparedStatement).setNull(4, Types.VARCHAR);
    verify(preparedStatement).setString(5, "p1Value");
    verify(preparedStatement).setString(6, "c1Value");
}
Also used : Optional(java.util.Optional) HashMap(java.util.HashMap) 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)

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