use of com.scalar.db.io.TextValue in project scalardb by scalar-labs.
the class DistributedStorageIntegrationTestBase method mutate_SinglePutGiven_ShouldStoreProperly.
@Test
public void mutate_SinglePutGiven_ShouldStoreProperly() throws ExecutionException {
// Arrange
int pKey = 0;
int cKey = 0;
List<Put> puts = preparePuts();
Key partitionKey = new Key(COL_NAME1, pKey);
Key clusteringKey = new Key(COL_NAME4, cKey);
Get get = new Get(partitionKey, clusteringKey);
// Act
storage.mutate(Collections.singletonList(puts.get(pKey * 2 + cKey)));
// Assert
Optional<Result> actual = storage.get(get);
assertThat(actual.isPresent()).isTrue();
assertThat(actual.get().getValue(COL_NAME1)).isEqualTo(Optional.of(new IntValue(COL_NAME1, pKey)));
assertThat(actual.get().getValue(COL_NAME2)).isEqualTo(Optional.of(new TextValue(COL_NAME2, Integer.toString(pKey + cKey))));
assertThat(actual.get().getValue(COL_NAME3)).isEqualTo(Optional.of(new IntValue(COL_NAME3, pKey + cKey)));
assertThat(actual.get().getValue(COL_NAME4)).isEqualTo(Optional.of(new IntValue(COL_NAME4, cKey)));
assertThat(actual.get().getValue(COL_NAME5)).isEqualTo(Optional.of(new BooleanValue(COL_NAME5, cKey % 2 == 0)));
}
use of com.scalar.db.io.TextValue in project scalardb by scalar-labs.
the class JdbcServiceTest method whenDeleteOperationWithDeleteIfConditionFails_shouldReturnFalseAndCallQueryBuilder.
@Test
public void whenDeleteOperationWithDeleteIfConditionFails_shouldReturnFalseAndCallQueryBuilder() throws Exception {
// Arrange
when(queryBuilder.deleteFrom(any(), any(), any())).thenReturn(deleteQueryBuilder);
when(deleteQueryBuilder.where(any(), any(), any())).thenReturn(deleteQueryBuilder);
when(deleteQueryBuilder.build()).thenReturn(deleteQuery);
when(connection.prepareStatement(any())).thenReturn(preparedStatement);
when(preparedStatement.executeUpdate()).thenReturn(0);
// Act
Delete delete = new Delete(new Key("p1", "val1")).withCondition(new DeleteIf(new ConditionalExpression("v1", new TextValue("val2"), ConditionalExpression.Operator.EQ))).forNamespace(NAMESPACE).forTable(TABLE);
boolean ret = jdbcService.delete(delete, connection);
// Assert
assertThat(ret).isFalse();
verify(operationChecker).check(any(Delete.class));
verify(queryBuilder).deleteFrom(any(), any(), any());
}
use of com.scalar.db.io.TextValue in project scalardb by scalar-labs.
the class QueryBuilderTest method updateQueryTest.
@ParameterizedTest
@EnumSource(RdbEngine.class)
public void updateQueryTest(RdbEngine rdbEngine) throws SQLException {
QueryBuilder queryBuilder = new QueryBuilder(rdbEngine);
UpdateQuery query;
PreparedStatement preparedStatement;
Map<String, Column<?>> columns = new HashMap<>();
columns.put("v1", TextColumn.of("v1", "v1Value"));
columns.put("v2", TextColumn.of("v2", "v2Value"));
columns.put("v3", TextColumn.of("v3", "v3Value"));
preparedStatement = mock(PreparedStatement.class);
query = queryBuilder.update(NAMESPACE, TABLE, TABLE_METADATA).set(columns).where(new Key("p1", "p1Value"), Optional.empty()).build();
assertThat(query.sql()).isEqualTo(encloseSql("UPDATE n1.t1 SET v1=?,v2=?,v3=? WHERE p1=?", rdbEngine));
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(columns).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=?", rdbEngine));
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(columns).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=?", rdbEngine));
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(columns).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=?", rdbEngine));
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(columns).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<=?", rdbEngine));
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");
preparedStatement = mock(PreparedStatement.class);
query = queryBuilder.update(NAMESPACE, TABLE, TABLE_METADATA).set(columns).where(new Key("p1", "p1Value"), Optional.of(new Key("c1", "c1Value")), Arrays.asList(ConditionBuilder.column("v1").isNullText(), ConditionBuilder.column("v2").isNotNullText())).build();
assertThat(query.sql()).isEqualTo(encloseSql("UPDATE n1.t1 SET v1=?,v2=?,v3=? WHERE p1=? AND c1=? AND v1 IS NULL AND v2 IS NOT NULL", rdbEngine));
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");
columns.put("v4", TextColumn.ofNull("v4"));
preparedStatement = mock(PreparedStatement.class);
query = queryBuilder.update(NAMESPACE, TABLE, TABLE_METADATA).set(columns).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=?", rdbEngine));
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");
}
use of com.scalar.db.io.TextValue in project scalardb by scalar-labs.
the class QueryBuilderTest method deleteQueryTest.
@ParameterizedTest
@EnumSource(RdbEngine.class)
public void deleteQueryTest(RdbEngine rdbEngine) throws SQLException {
QueryBuilder queryBuilder = new QueryBuilder(rdbEngine);
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=?", rdbEngine));
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=?", rdbEngine));
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=?", rdbEngine));
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=?", rdbEngine));
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<?", rdbEngine));
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");
preparedStatement = mock(PreparedStatement.class);
query = queryBuilder.deleteFrom(NAMESPACE, TABLE, TABLE_METADATA).where(new Key("p1", "p1Value"), Optional.of(new Key("c1", "c1Value")), Arrays.asList(ConditionBuilder.column("v1").isNullText(), ConditionBuilder.column("v2").isNotNullText())).build();
assertThat(query.sql()).isEqualTo(encloseSql("DELETE FROM n1.t1 WHERE p1=? AND c1=? AND v1 IS NULL AND v2 IS NOT NULL", rdbEngine));
query.bind(preparedStatement);
verify(preparedStatement).setString(1, "p1Value");
verify(preparedStatement).setString(2, "c1Value");
}
use of com.scalar.db.io.TextValue in project scalardb by scalar-labs.
the class SnapshotTest method get_KeyGivenContainedInWriteSetAndReadSet_ShouldReturnMergedResult.
@Test
public void get_KeyGivenContainedInWriteSetAndReadSet_ShouldReturnMergedResult() throws CrudException {
// Arrange
snapshot = prepareSnapshot(Isolation.SNAPSHOT);
Key partitionKey = new Key(ANY_NAME_1, ANY_TEXT_1);
Key clusteringKey = new Key(ANY_NAME_2, ANY_TEXT_2);
Put put = new Put(partitionKey, clusteringKey).withConsistency(Consistency.LINEARIZABLE).forNamespace(ANY_NAMESPACE_NAME).forTable(ANY_TABLE_NAME).withValue(ANY_NAME_3, ANY_TEXT_5).withTextValue(ANY_NAME_4, null);
Snapshot.Key key = new Snapshot.Key(prepareGet());
TransactionResult result = prepareResult(ANY_ID);
snapshot.put(key, Optional.of(result));
snapshot.put(key, put);
// Act
Optional<TransactionResult> actual = snapshot.get(key);
// Assert
assertThat(actual).isPresent();
assertThat(actual.get().getValues()).isEqualTo(ImmutableMap.<String, Value<?>>builder().put(ANY_NAME_1, new TextValue(ANY_NAME_1, ANY_TEXT_1)).put(ANY_NAME_2, new TextValue(ANY_NAME_2, ANY_TEXT_2)).put(ANY_NAME_3, new TextValue(ANY_NAME_3, ANY_TEXT_5)).put(ANY_NAME_4, new TextValue(ANY_NAME_4, (String) null)).put(Attribute.ID, Attribute.toIdValue(ANY_ID)).put(Attribute.VERSION, Attribute.toVersionValue(ANY_VERSION)).build());
assertThat(actual.get().getValue(ANY_NAME_1).isPresent()).isTrue();
assertThat(actual.get().getValue(ANY_NAME_1).get()).isEqualTo(new TextValue(ANY_NAME_1, ANY_TEXT_1));
assertThat(actual.get().getValue(ANY_NAME_2).isPresent()).isTrue();
assertThat(actual.get().getValue(ANY_NAME_2).get()).isEqualTo(new TextValue(ANY_NAME_2, ANY_TEXT_2));
assertThat(actual.get().getValue(ANY_NAME_3).isPresent()).isTrue();
assertThat(actual.get().getValue(ANY_NAME_3).get()).isEqualTo(new TextValue(ANY_NAME_3, ANY_TEXT_5));
assertThat(actual.get().getValue(ANY_NAME_4).isPresent()).isTrue();
assertThat(actual.get().getValue(ANY_NAME_4).get()).isEqualTo(new TextValue(ANY_NAME_4, (String) null));
assertThat(actual.get().getValue(Attribute.ID).isPresent()).isTrue();
assertThat(actual.get().getValue(Attribute.ID).get()).isEqualTo(Attribute.toIdValue(ANY_ID));
assertThat(actual.get().getValue(Attribute.VERSION).isPresent()).isTrue();
assertThat(actual.get().getValue(Attribute.VERSION).get()).isEqualTo(Attribute.toVersionValue(ANY_VERSION));
assertThat(actual.get().getContainedColumnNames()).isEqualTo(new HashSet<>(Arrays.asList(ANY_NAME_1, ANY_NAME_2, ANY_NAME_3, ANY_NAME_4, Attribute.ID, Attribute.VERSION)));
assertThat(actual.get().contains(ANY_NAME_1)).isTrue();
assertThat(actual.get().isNull(ANY_NAME_1)).isFalse();
assertThat(actual.get().getText(ANY_NAME_1)).isEqualTo(ANY_TEXT_1);
assertThat(actual.get().getAsObject(ANY_NAME_1)).isEqualTo(ANY_TEXT_1);
assertThat(actual.get().contains(ANY_NAME_2)).isTrue();
assertThat(actual.get().isNull(ANY_NAME_2)).isFalse();
assertThat(actual.get().getText(ANY_NAME_2)).isEqualTo(ANY_TEXT_2);
assertThat(actual.get().getAsObject(ANY_NAME_2)).isEqualTo(ANY_TEXT_2);
assertThat(actual.get().contains(ANY_NAME_3)).isTrue();
assertThat(actual.get().isNull(ANY_NAME_3)).isFalse();
assertThat(actual.get().getText(ANY_NAME_3)).isEqualTo(ANY_TEXT_5);
assertThat(actual.get().getAsObject(ANY_NAME_3)).isEqualTo(ANY_TEXT_5);
assertThat(actual.get().contains(ANY_NAME_4)).isTrue();
assertThat(actual.get().isNull(ANY_NAME_4)).isTrue();
assertThat(actual.get().getText(ANY_NAME_4)).isNull();
assertThat(actual.get().getAsObject(ANY_NAME_4)).isNull();
assertThat(actual.get().contains(Attribute.ID)).isTrue();
assertThat(actual.get().isNull(Attribute.ID)).isFalse();
assertThat(actual.get().getText(Attribute.ID)).isEqualTo(ANY_ID);
assertThat(actual.get().getAsObject(Attribute.ID)).isEqualTo(ANY_ID);
assertThat(actual.get().contains(Attribute.VERSION)).isTrue();
assertThat(actual.get().isNull(Attribute.VERSION)).isFalse();
assertThat(actual.get().getInt(Attribute.VERSION)).isEqualTo(ANY_VERSION);
assertThat(actual.get().getAsObject(Attribute.VERSION)).isEqualTo(ANY_VERSION);
}
Aggregations