use of com.scalar.db.io.TextValue in project scalardb by scalar-labs.
the class ResultImplTest method getValues_ProperValuesGivenInConstructor_ShouldReturnWhatsSet.
@Test
public void getValues_ProperValuesGivenInConstructor_ShouldReturnWhatsSet() {
// Arrange
ResultImpl result = new ResultImpl(values, TABLE_METADATA);
// Act
Map<String, Value<?>> actual = result.getValues();
// Assert
assertThat(actual.get(ANY_NAME_1)).isEqualTo(new TextValue(ANY_NAME_1, ANY_TEXT_1));
assertThat(actual.get(ANY_NAME_2)).isEqualTo(new TextValue(ANY_NAME_2, ANY_TEXT_2));
assertThat(actual.get(ANY_COLUMN_NAME_1)).isEqualTo(new BooleanValue(ANY_COLUMN_NAME_1, true));
assertThat(actual.get(ANY_COLUMN_NAME_7)).isEqualTo(new BlobValue(ANY_COLUMN_NAME_7, "bytes".getBytes(StandardCharsets.UTF_8)));
}
use of com.scalar.db.io.TextValue in project scalardb by scalar-labs.
the class ResultImplTest method getValues_TryToModifyReturned_ShouldThrowException.
@Test
public void getValues_TryToModifyReturned_ShouldThrowException() {
// Arrange
ResultImpl result = new ResultImpl(values, TABLE_METADATA);
Map<String, Value<?>> values = result.getValues();
// Act Assert
assertThatThrownBy(() -> values.put("new", new TextValue(ANY_NAME_1, ANY_TEXT_1))).isInstanceOf(UnsupportedOperationException.class);
}
use of com.scalar.db.io.TextValue in project scalardb by scalar-labs.
the class QueryBuilderTest method deleteQueryTest.
@Test
public void deleteQueryTest() throws SQLException {
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=?"));
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=?"));
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=?"));
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=?"));
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<?"));
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");
}
use of com.scalar.db.io.TextValue in project scalardb by scalar-labs.
the class QueryBuilderTest method upsertQueryTest.
@Test
public void upsertQueryTest() throws SQLException {
String expectedQuery;
UpsertQuery 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);
switch(rdbEngine) {
case MYSQL:
expectedQuery = "INSERT INTO n1.t1 (p1,v1,v2,v3) VALUES (?,?,?,?)" + " ON DUPLICATE KEY UPDATE v1=?,v2=?,v3=?";
break;
case POSTGRESQL:
expectedQuery = "INSERT INTO n1.t1 (p1,v1,v2,v3) VALUES (?,?,?,?) " + "ON CONFLICT (p1) DO UPDATE SET v1=?,v2=?,v3=?";
break;
case ORACLE:
expectedQuery = "MERGE INTO n1.t1 t1 USING (SELECT ? p1 FROM DUAL) t2 ON (t1.p1=t2.p1) " + "WHEN MATCHED THEN UPDATE SET v1=?,v2=?,v3=? " + "WHEN NOT MATCHED THEN INSERT (p1,v1,v2,v3) VALUES (?,?,?,?)";
break;
case SQL_SERVER:
default:
expectedQuery = "MERGE n1.t1 t1 USING (SELECT ? p1) t2 ON (t1.p1=t2.p1) " + "WHEN MATCHED THEN UPDATE SET v1=?,v2=?,v3=? " + "WHEN NOT MATCHED THEN INSERT (p1,v1,v2,v3) VALUES (?,?,?,?);";
break;
}
query = queryBuilder.upsertInto(NAMESPACE, TABLE, TABLE_METADATA).values(new Key("p1", "p1Value"), Optional.empty(), values).build();
assertThat(query.sql()).isEqualTo(encloseSql(expectedQuery));
query.bind(preparedStatement);
switch(rdbEngine) {
case MYSQL:
case POSTGRESQL:
verify(preparedStatement).setString(1, "p1Value");
verify(preparedStatement).setString(2, "v1Value");
verify(preparedStatement).setString(3, "v2Value");
verify(preparedStatement).setString(4, "v3Value");
verify(preparedStatement).setString(5, "v1Value");
verify(preparedStatement).setString(6, "v2Value");
verify(preparedStatement).setString(7, "v3Value");
break;
case ORACLE:
case SQL_SERVER:
verify(preparedStatement).setString(1, "p1Value");
verify(preparedStatement).setString(2, "v1Value");
verify(preparedStatement).setString(3, "v2Value");
verify(preparedStatement).setString(4, "v3Value");
verify(preparedStatement).setString(5, "p1Value");
verify(preparedStatement).setString(6, "v1Value");
verify(preparedStatement).setString(7, "v2Value");
verify(preparedStatement).setString(8, "v3Value");
break;
}
preparedStatement = mock(PreparedStatement.class);
switch(rdbEngine) {
case MYSQL:
expectedQuery = "INSERT INTO n1.t1 (p1,c1,v1,v2,v3) VALUES (?,?,?,?,?)" + " ON DUPLICATE KEY UPDATE v1=?,v2=?,v3=?";
break;
case POSTGRESQL:
expectedQuery = "INSERT INTO n1.t1 (p1,c1,v1,v2,v3) VALUES (?,?,?,?,?) " + "ON CONFLICT (p1,c1) DO UPDATE SET v1=?,v2=?,v3=?";
break;
case ORACLE:
expectedQuery = "MERGE INTO n1.t1 t1 USING (SELECT ? p1,? c1 FROM DUAL) t2 " + "ON (t1.p1=t2.p1 AND t1.c1=t2.c1) " + "WHEN MATCHED THEN UPDATE SET v1=?,v2=?,v3=? " + "WHEN NOT MATCHED THEN INSERT (p1,c1,v1,v2,v3) VALUES (?,?,?,?,?)";
break;
case SQL_SERVER:
default:
expectedQuery = "MERGE n1.t1 t1 USING (SELECT ? p1,? c1) t2 " + "ON (t1.p1=t2.p1 AND t1.c1=t2.c1) " + "WHEN MATCHED THEN UPDATE SET v1=?,v2=?,v3=? " + "WHEN NOT MATCHED THEN INSERT (p1,c1,v1,v2,v3) VALUES (?,?,?,?,?);";
break;
}
query = queryBuilder.upsertInto(NAMESPACE, TABLE, TABLE_METADATA).values(new Key("p1", "p1Value"), Optional.of(new Key("c1", "c1Value")), values).build();
assertThat(query.sql()).isEqualTo(encloseSql(expectedQuery));
query.bind(preparedStatement);
switch(rdbEngine) {
case MYSQL:
case POSTGRESQL:
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");
verify(preparedStatement).setString(6, "v1Value");
verify(preparedStatement).setString(7, "v2Value");
verify(preparedStatement).setString(8, "v3Value");
break;
case ORACLE:
case SQL_SERVER:
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");
verify(preparedStatement).setString(6, "p1Value");
verify(preparedStatement).setString(7, "c1Value");
verify(preparedStatement).setString(8, "v1Value");
verify(preparedStatement).setString(9, "v2Value");
verify(preparedStatement).setString(10, "v3Value");
break;
}
values.put("v4", Optional.of(new TextValue("v4Value")));
preparedStatement = mock(PreparedStatement.class);
switch(rdbEngine) {
case MYSQL:
expectedQuery = "INSERT INTO n1.t1 (p1,p2,c1,c2,v1,v2,v3,v4) VALUES (?,?,?,?,?,?,?,?)" + " ON DUPLICATE KEY UPDATE v1=?,v2=?,v3=?,v4=?";
break;
case POSTGRESQL:
expectedQuery = "INSERT INTO n1.t1 (p1,p2,c1,c2,v1,v2,v3,v4) VALUES (?,?,?,?,?,?,?,?) " + "ON CONFLICT (p1,p2,c1,c2) DO UPDATE SET v1=?,v2=?,v3=?,v4=?";
break;
case ORACLE:
expectedQuery = "MERGE INTO n1.t1 t1 USING (SELECT ? p1,? p2,? c1,? c2 FROM DUAL) t2 " + "ON (t1.p1=t2.p1 AND t1.p2=t2.p2 AND t1.c1=t2.c1 AND t1.c2=t2.c2) " + "WHEN MATCHED THEN UPDATE SET v1=?,v2=?,v3=?,v4=? " + "WHEN NOT MATCHED THEN INSERT (p1,p2,c1,c2,v1,v2,v3,v4) " + "VALUES (?,?,?,?,?,?,?,?)";
break;
case SQL_SERVER:
default:
expectedQuery = "MERGE n1.t1 t1 USING (SELECT ? p1,? p2,? c1,? c2) t2 " + "ON (t1.p1=t2.p1 AND t1.p2=t2.p2 AND t1.c1=t2.c1 AND t1.c2=t2.c2) " + "WHEN MATCHED THEN UPDATE SET v1=?,v2=?,v3=?,v4=? " + "WHEN NOT MATCHED THEN INSERT (p1,p2,c1,c2,v1,v2,v3,v4) VALUES (?,?,?,?,?,?,?,?);";
break;
}
query = queryBuilder.upsertInto(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(expectedQuery));
query.bind(preparedStatement);
switch(rdbEngine) {
case MYSQL:
case POSTGRESQL:
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).setString(9, "v1Value");
verify(preparedStatement).setString(10, "v2Value");
verify(preparedStatement).setString(11, "v3Value");
verify(preparedStatement).setString(12, "v4Value");
break;
case ORACLE:
case SQL_SERVER:
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).setString(9, "p1Value");
verify(preparedStatement).setString(10, "p2Value");
verify(preparedStatement).setString(11, "c1Value");
verify(preparedStatement).setString(12, "c2Value");
verify(preparedStatement).setString(13, "v1Value");
verify(preparedStatement).setString(14, "v2Value");
verify(preparedStatement).setString(15, "v3Value");
verify(preparedStatement).setString(16, "v4Value");
break;
}
values.put("v5", Optional.empty());
preparedStatement = mock(PreparedStatement.class);
switch(rdbEngine) {
case MYSQL:
expectedQuery = "INSERT INTO n1.t1 (p1,p2,c1,c2,v1,v2,v3,v4,v5) VALUES (?,?,?,?,?,?,?,?,?)" + " ON DUPLICATE KEY UPDATE v1=?,v2=?,v3=?,v4=?,v5=?";
break;
case POSTGRESQL:
expectedQuery = "INSERT INTO n1.t1 (p1,p2,c1,c2,v1,v2,v3,v4,v5) VALUES (?,?,?,?,?,?,?,?,?) " + "ON CONFLICT (p1,p2,c1,c2) DO UPDATE SET v1=?,v2=?,v3=?,v4=?,v5=?";
break;
case ORACLE:
expectedQuery = "MERGE INTO n1.t1 t1 USING (SELECT ? p1,? p2,? c1,? c2 FROM DUAL) t2 " + "ON (t1.p1=t2.p1 AND t1.p2=t2.p2 AND t1.c1=t2.c1 AND t1.c2=t2.c2) " + "WHEN MATCHED THEN UPDATE SET v1=?,v2=?,v3=?,v4=?,v5=? " + "WHEN NOT MATCHED THEN INSERT (p1,p2,c1,c2,v1,v2,v3,v4,v5) " + "VALUES (?,?,?,?,?,?,?,?,?)";
break;
case SQL_SERVER:
default:
expectedQuery = "MERGE n1.t1 t1 USING (SELECT ? p1,? p2,? c1,? c2) t2 " + "ON (t1.p1=t2.p1 AND t1.p2=t2.p2 AND t1.c1=t2.c1 AND t1.c2=t2.c2) " + "WHEN MATCHED THEN UPDATE SET v1=?,v2=?,v3=?,v4=?,v5=? " + "WHEN NOT MATCHED THEN INSERT (p1,p2,c1,c2,v1,v2,v3,v4,v5) " + "VALUES (?,?,?,?,?,?,?,?,?);";
break;
}
query = queryBuilder.upsertInto(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(expectedQuery));
query.bind(preparedStatement);
switch(rdbEngine) {
case MYSQL:
case POSTGRESQL:
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);
verify(preparedStatement).setString(10, "v1Value");
verify(preparedStatement).setString(11, "v2Value");
verify(preparedStatement).setString(12, "v3Value");
verify(preparedStatement).setString(13, "v4Value");
verify(preparedStatement).setNull(14, Types.VARCHAR);
break;
case ORACLE:
case SQL_SERVER:
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);
verify(preparedStatement).setString(10, "p1Value");
verify(preparedStatement).setString(11, "p2Value");
verify(preparedStatement).setString(12, "c1Value");
verify(preparedStatement).setString(13, "c2Value");
verify(preparedStatement).setString(14, "v1Value");
verify(preparedStatement).setString(15, "v2Value");
verify(preparedStatement).setString(16, "v3Value");
verify(preparedStatement).setString(17, "v4Value");
verify(preparedStatement).setNull(18, Types.VARCHAR);
break;
}
}
use of com.scalar.db.io.TextValue in project scalardb by scalar-labs.
the class StorageWithReservedKeywordIntegrationTestBase method put_WithReservedKeywordAndSinglePutGiven_ShouldStoreProperly.
@Test
public void put_WithReservedKeywordAndSinglePutGiven_ShouldStoreProperly() throws ExecutionException {
// Arrange
int pKey = 0;
int cKey = 0;
List<Put> puts = preparePuts();
Key partitionKey = new Key(columnName1, pKey);
Key clusteringKey = new Key(columnName4, cKey);
Get get = new Get(partitionKey, clusteringKey);
// Act
storage.put(puts.get(pKey * 2 + cKey));
// Assert
Optional<Result> actual = storage.get(get);
assertThat(actual.isPresent()).isTrue();
assertThat(actual.get().getValue(columnName1)).isEqualTo(Optional.of(new IntValue(columnName1, pKey)));
assertThat(actual.get().getValue(columnName2)).isEqualTo(Optional.of(new TextValue(columnName2, Integer.toString(pKey + cKey))));
assertThat(actual.get().getValue(columnName3)).isEqualTo(Optional.of(new IntValue(columnName3, pKey + cKey)));
assertThat(actual.get().getValue(columnName4)).isEqualTo(Optional.of(new IntValue(columnName4, cKey)));
assertThat(actual.get().getValue(columnName5)).isEqualTo(Optional.of(new BooleanValue(columnName5, cKey % 2 == 0)));
}
Aggregations