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