use of com.scalar.db.api.ConditionalExpression in project scalardb by scalar-labs.
the class UpdateQuery method bind.
@Override
public void bind(PreparedStatement preparedStatement) throws SQLException {
PreparedStatementBinder binder = new PreparedStatementBinder(preparedStatement, tableMetadata, rdbEngine);
for (Column<?> column : columns.values()) {
column.accept(binder);
binder.throwSQLExceptionIfOccurred();
}
for (Column<?> column : partitionKey.getColumns()) {
column.accept(binder);
binder.throwSQLExceptionIfOccurred();
}
if (clusteringKey.isPresent()) {
for (Column<?> column : clusteringKey.get().getColumns()) {
column.accept(binder);
binder.throwSQLExceptionIfOccurred();
}
}
for (ConditionalExpression condition : otherConditions) {
// no need to bind for 'is null' and 'is not null' conditions
if (condition.getOperator() != Operator.IS_NULL && condition.getOperator() != Operator.IS_NOT_NULL) {
condition.getColumn().accept(binder);
binder.throwSQLExceptionIfOccurred();
}
}
}
use of com.scalar.db.api.ConditionalExpression in project scalardb by scalar-labs.
the class DynamoMutation method getConditionColumnMap.
@Nonnull
public Map<String, String> getConditionColumnMap() {
Map<String, String> ret = new HashMap<>();
Mutation mutation = (Mutation) getOperation();
if (mutation.getCondition().isPresent()) {
int index = 0;
for (ConditionalExpression expression : mutation.getCondition().get().getExpressions()) {
ret.put(CONDITION_COLUMN_NAME_ALIAS + index, expression.getName());
index++;
}
}
return ret;
}
use of com.scalar.db.api.ConditionalExpression 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.api.ConditionalExpression 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.api.ConditionalExpression 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");
}
Aggregations