Search in sources :

Example 81 with TextValue

use of com.scalar.db.io.TextValue in project scalardb by scalar-labs.

the class MapVisitorTest method visit_TextValueWithNullValueAcceptCalled_ShouldGetMap.

@Test
public void visit_TextValueWithNullValueAcceptCalled_ShouldGetMap() {
    // Act
    new TextValue("any_text", (String) null).accept(visitor);
    // Assert
    assertThat(visitor.get().containsKey("any_text")).isTrue();
    assertThat(visitor.get().get("any_text")).isNull();
}
Also used : TextValue(com.scalar.db.io.TextValue) Test(org.junit.Test)

Example 82 with TextValue

use of com.scalar.db.io.TextValue in project scalardb by scalar-labs.

the class ValueBinderTest method visit_TextValueWithNullValueAcceptCalled_ShouldCallSetToNull.

@Test
public void visit_TextValueWithNullValueAcceptCalled_ShouldCallSetToNull() {
    // Arrange
    TextValue value = new TextValue(ANY_NAME, (String) null);
    ValueBinder binder = new ValueBinder(bound);
    // Act
    value.accept(binder);
    // Assert
    verify(bound).setToNull(0);
}
Also used : TextValue(com.scalar.db.io.TextValue) Test(org.junit.Test)

Example 83 with TextValue

use of com.scalar.db.io.TextValue 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");
}
Also used : Optional(java.util.Optional) 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) Test(org.junit.Test)

Example 84 with TextValue

use of com.scalar.db.io.TextValue in project scalardb by scalar-labs.

the class QueryBuilderTest method insertQueryTest.

@Test
public void insertQueryTest() throws SQLException {
    InsertQuery 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.insertInto(NAMESPACE, TABLE, TABLE_METADATA).values(new Key("p1", "p1Value"), Optional.empty(), values).build();
    assertThat(query.sql()).isEqualTo(encloseSql("INSERT INTO n1.t1 (p1,v1,v2,v3) VALUES (?,?,?,?)"));
    query.bind(preparedStatement);
    verify(preparedStatement).setString(1, "p1Value");
    verify(preparedStatement).setString(2, "v1Value");
    verify(preparedStatement).setString(3, "v2Value");
    verify(preparedStatement).setString(4, "v3Value");
    preparedStatement = mock(PreparedStatement.class);
    query = queryBuilder.insertInto(NAMESPACE, TABLE, TABLE_METADATA).values(new Key("p1", "p1Value"), Optional.of(new Key("c1", "c1Value")), values).build();
    assertThat(query.sql()).isEqualTo(encloseSql("INSERT INTO n1.t1 (p1,c1,v1,v2,v3) VALUES (?,?,?,?,?)"));
    query.bind(preparedStatement);
    verify(preparedStatement).setString(1, "p1Value");
    verify(preparedStatement).setString(2, "c1Value");
    verify(preparedStatement).setString(3, "v1Value");
    verify(preparedStatement).setString(4, "v2Value");
    verify(preparedStatement).setString(5, "v3Value");
    values.put("v4", Optional.of(new TextValue("v4Value")));
    preparedStatement = mock(PreparedStatement.class);
    query = queryBuilder.insertInto(NAMESPACE, TABLE, TABLE_METADATA).values(new Key("p1", "p1Value", "p2", "p2Value"), Optional.of(new Key("c1", "c1Value", "c2", "c2Value")), values).build();
    assertThat(query.sql()).isEqualTo(encloseSql("INSERT INTO n1.t1 (p1,p2,c1,c2,v1,v2,v3,v4) VALUES (?,?,?,?,?,?,?,?)"));
    query.bind(preparedStatement);
    verify(preparedStatement).setString(1, "p1Value");
    verify(preparedStatement).setString(2, "p2Value");
    verify(preparedStatement).setString(3, "c1Value");
    verify(preparedStatement).setString(4, "c2Value");
    verify(preparedStatement).setString(5, "v1Value");
    verify(preparedStatement).setString(6, "v2Value");
    verify(preparedStatement).setString(7, "v3Value");
    verify(preparedStatement).setString(8, "v4Value");
    values.put("v5", Optional.empty());
    preparedStatement = mock(PreparedStatement.class);
    query = queryBuilder.insertInto(NAMESPACE, TABLE, TABLE_METADATA).values(new Key("p1", "p1Value", "p2", "p2Value"), Optional.of(new Key("c1", "c1Value", "c2", "c2Value")), values).build();
    assertThat(query.sql()).isEqualTo(encloseSql("INSERT INTO n1.t1 (p1,p2,c1,c2,v1,v2,v3,v4,v5) VALUES (?,?,?,?,?,?,?,?,?)"));
    query.bind(preparedStatement);
    verify(preparedStatement).setString(1, "p1Value");
    verify(preparedStatement).setString(2, "p2Value");
    verify(preparedStatement).setString(3, "c1Value");
    verify(preparedStatement).setString(4, "c2Value");
    verify(preparedStatement).setString(5, "v1Value");
    verify(preparedStatement).setString(6, "v2Value");
    verify(preparedStatement).setString(7, "v3Value");
    verify(preparedStatement).setString(8, "v4Value");
    verify(preparedStatement).setNull(9, Types.VARCHAR);
}
Also used : Optional(java.util.Optional) HashMap(java.util.HashMap) TextValue(com.scalar.db.io.TextValue) PreparedStatement(java.sql.PreparedStatement) Key(com.scalar.db.io.Key) Test(org.junit.Test)

Example 85 with TextValue

use of com.scalar.db.io.TextValue in project scalardb by scalar-labs.

the class ResultImplTest method getValue_ProperNullValuesGivenInConstructor_ShouldReturnWhatsSet.

@Test
public void getValue_ProperNullValuesGivenInConstructor_ShouldReturnWhatsSet() {
    // Arrange
    ResultImpl result = new ResultImpl(ImmutableMap.<String, Optional<Value<?>>>builder().put(ANY_NAME_1, Optional.of(new TextValue(ANY_NAME_1, ANY_TEXT_1))).put(ANY_NAME_2, Optional.of(new TextValue(ANY_NAME_2, ANY_TEXT_2))).put(ANY_COLUMN_NAME_1, Optional.empty()).put(ANY_COLUMN_NAME_2, Optional.empty()).put(ANY_COLUMN_NAME_3, Optional.empty()).put(ANY_COLUMN_NAME_4, Optional.empty()).put(ANY_COLUMN_NAME_5, Optional.empty()).put(ANY_COLUMN_NAME_6, Optional.empty()).put(ANY_COLUMN_NAME_7, Optional.empty()).build(), TABLE_METADATA);
    // Act Assert
    assertThat(result.getValue(ANY_NAME_1)).isEqualTo(Optional.of(new TextValue(ANY_NAME_1, ANY_TEXT_1)));
    assertThat(result.getValue(ANY_NAME_2)).isEqualTo(Optional.of(new TextValue(ANY_NAME_2, ANY_TEXT_2)));
    assertThat(result.getValue(ANY_COLUMN_NAME_1)).isEqualTo(Optional.of(new BooleanValue(ANY_COLUMN_NAME_1, false)));
    assertThat(result.getValue(ANY_COLUMN_NAME_2)).isEqualTo(Optional.of(new IntValue(ANY_COLUMN_NAME_2, 0)));
    assertThat(result.getValue(ANY_COLUMN_NAME_3)).isEqualTo(Optional.of(new BigIntValue(ANY_COLUMN_NAME_3, 0L)));
    assertThat(result.getValue(ANY_COLUMN_NAME_4)).isEqualTo(Optional.of(new FloatValue(ANY_COLUMN_NAME_4, 0.0F)));
    assertThat(result.getValue(ANY_COLUMN_NAME_5)).isEqualTo(Optional.of(new DoubleValue(ANY_COLUMN_NAME_5, 0.0D)));
    assertThat(result.getValue(ANY_COLUMN_NAME_6)).isEqualTo(Optional.of(new TextValue(ANY_COLUMN_NAME_6, (String) null)));
    assertThat(result.getValue(ANY_COLUMN_NAME_7)).isEqualTo(Optional.of(new BlobValue(ANY_COLUMN_NAME_7, (byte[]) null)));
    assertThat(result.getContainedColumnNames()).isEqualTo(new HashSet<>(Arrays.asList(ANY_NAME_1, ANY_NAME_2, ANY_COLUMN_NAME_1, ANY_COLUMN_NAME_2, ANY_COLUMN_NAME_3, ANY_COLUMN_NAME_4, ANY_COLUMN_NAME_5, ANY_COLUMN_NAME_6, ANY_COLUMN_NAME_7)));
    assertThat(result.contains(ANY_NAME_1)).isTrue();
    assertThat(result.isNull(ANY_NAME_1)).isFalse();
    assertThat(result.getText(ANY_NAME_1)).isEqualTo(ANY_TEXT_1);
    assertThat(result.getAsObject(ANY_NAME_1)).isEqualTo(ANY_TEXT_1);
    assertThat(result.contains(ANY_NAME_2)).isTrue();
    assertThat(result.isNull(ANY_NAME_2)).isFalse();
    assertThat(result.getText(ANY_NAME_2)).isEqualTo(ANY_TEXT_2);
    assertThat(result.getAsObject(ANY_NAME_2)).isEqualTo(ANY_TEXT_2);
    assertThat(result.contains(ANY_COLUMN_NAME_1)).isTrue();
    assertThat(result.isNull(ANY_COLUMN_NAME_1)).isTrue();
    assertThat(result.getBoolean(ANY_COLUMN_NAME_1)).isEqualTo(false);
    assertThat(result.getAsObject(ANY_COLUMN_NAME_1)).isNull();
    assertThat(result.contains(ANY_COLUMN_NAME_2)).isTrue();
    assertThat(result.isNull(ANY_COLUMN_NAME_2)).isTrue();
    assertThat(result.getInt(ANY_COLUMN_NAME_2)).isEqualTo(0);
    assertThat(result.getAsObject(ANY_COLUMN_NAME_2)).isNull();
    assertThat(result.contains(ANY_COLUMN_NAME_3)).isTrue();
    assertThat(result.isNull(ANY_COLUMN_NAME_3)).isTrue();
    assertThat(result.getBigInt(ANY_COLUMN_NAME_3)).isEqualTo(0L);
    assertThat(result.getAsObject(ANY_COLUMN_NAME_3)).isNull();
    assertThat(result.contains(ANY_COLUMN_NAME_4)).isTrue();
    assertThat(result.isNull(ANY_COLUMN_NAME_4)).isTrue();
    assertThat(result.getFloat(ANY_COLUMN_NAME_4)).isEqualTo(0.0F);
    assertThat(result.getAsObject(ANY_COLUMN_NAME_4)).isNull();
    assertThat(result.contains(ANY_COLUMN_NAME_5)).isTrue();
    assertThat(result.isNull(ANY_COLUMN_NAME_5)).isTrue();
    assertThat(result.getDouble(ANY_COLUMN_NAME_5)).isEqualTo(0.0D);
    assertThat(result.getAsObject(ANY_COLUMN_NAME_5)).isNull();
    assertThat(result.contains(ANY_COLUMN_NAME_6)).isTrue();
    assertThat(result.isNull(ANY_COLUMN_NAME_6)).isTrue();
    assertThat(result.getText(ANY_COLUMN_NAME_6)).isNull();
    assertThat(result.getAsObject(ANY_COLUMN_NAME_6)).isNull();
    assertThat(result.contains(ANY_COLUMN_NAME_7)).isTrue();
    assertThat(result.isNull(ANY_COLUMN_NAME_7)).isTrue();
    assertThat(result.getBlob(ANY_COLUMN_NAME_7)).isNull();
    assertThat(result.getBlobAsByteBuffer(ANY_COLUMN_NAME_7)).isNull();
    assertThat(result.getBlobAsBytes(ANY_COLUMN_NAME_7)).isNull();
    assertThat(result.getAsObject(ANY_COLUMN_NAME_7)).isNull();
}
Also used : DoubleValue(com.scalar.db.io.DoubleValue) TextValue(com.scalar.db.io.TextValue) BooleanValue(com.scalar.db.io.BooleanValue) IntValue(com.scalar.db.io.IntValue) DoubleValue(com.scalar.db.io.DoubleValue) TextValue(com.scalar.db.io.TextValue) Value(com.scalar.db.io.Value) BigIntValue(com.scalar.db.io.BigIntValue) FloatValue(com.scalar.db.io.FloatValue) BlobValue(com.scalar.db.io.BlobValue) BooleanValue(com.scalar.db.io.BooleanValue) FloatValue(com.scalar.db.io.FloatValue) IntValue(com.scalar.db.io.IntValue) BigIntValue(com.scalar.db.io.BigIntValue) BigIntValue(com.scalar.db.io.BigIntValue) BlobValue(com.scalar.db.io.BlobValue) Test(org.junit.Test)

Aggregations

TextValue (com.scalar.db.io.TextValue)89 Key (com.scalar.db.io.Key)60 IntValue (com.scalar.db.io.IntValue)51 Test (org.junit.jupiter.api.Test)47 Test (org.junit.Test)38 BooleanValue (com.scalar.db.io.BooleanValue)36 Put (com.scalar.db.api.Put)27 BigIntValue (com.scalar.db.io.BigIntValue)27 DoubleValue (com.scalar.db.io.DoubleValue)26 BlobValue (com.scalar.db.io.BlobValue)25 FloatValue (com.scalar.db.io.FloatValue)24 Result (com.scalar.db.api.Result)23 ConditionalExpression (com.scalar.db.api.ConditionalExpression)21 Get (com.scalar.db.api.Get)17 Value (com.scalar.db.io.Value)15 DeleteIf (com.scalar.db.api.DeleteIf)11 Delete (com.scalar.db.api.Delete)8 ExpectedResult (com.scalar.db.util.TestUtils.ExpectedResult)8 PutIf (com.scalar.db.api.PutIf)6 PreparedStatement (java.sql.PreparedStatement)6