Search in sources :

Example 31 with ConditionalExpression

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();
        }
    }
}
Also used : ConditionalExpression(com.scalar.db.api.ConditionalExpression)

Example 32 with ConditionalExpression

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;
}
Also used : HashMap(java.util.HashMap) ConditionalExpression(com.scalar.db.api.ConditionalExpression) Mutation(com.scalar.db.api.Mutation) Nonnull(javax.annotation.Nonnull)

Example 33 with ConditionalExpression

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());
}
Also used : Delete(com.scalar.db.api.Delete) TextValue(com.scalar.db.io.TextValue) ConditionalExpression(com.scalar.db.api.ConditionalExpression) Key(com.scalar.db.io.Key) DeleteIf(com.scalar.db.api.DeleteIf) Test(org.junit.jupiter.api.Test)

Example 34 with ConditionalExpression

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");
}
Also used : TextColumn(com.scalar.db.io.TextColumn) Column(com.scalar.db.io.Column) 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) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 35 with ConditionalExpression

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");
}
Also used : TextValue(com.scalar.db.io.TextValue) ConditionalExpression(com.scalar.db.api.ConditionalExpression) PreparedStatement(java.sql.PreparedStatement) Key(com.scalar.db.io.Key) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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