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();
}
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);
}
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)));
}
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");
}
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)));
}
Aggregations