use of herddb.core.DBManager in project herddb by diennea.
the class RawSQLTest method updateTest.
@Test
public void updateTest() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "CREATE TABLE tblspace1.tsql (k1 string primary key,n1 int,s1 string)", Collections.emptyList());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1,s1) values(?,?,?)", Arrays.asList("mykey", Integer.valueOf(1234), "value1")).getUpdateCount());
assertEquals(1, scan(manager, "SELECT * FROM tblspace1.tsql where s1='value1'", Collections.emptyList()).consumeAndClose().size());
assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.tsql set s1=k1 where k1=?", Arrays.asList("mykey")).getUpdateCount());
assertEquals(1, scan(manager, "SELECT * FROM tblspace1.tsql where s1='mykey'", Collections.emptyList()).consumeAndClose().size());
}
}
use of herddb.core.DBManager in project herddb by diennea.
the class RawSQLTest method simpleCountTest.
@Test
public void simpleCountTest() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "CREATE TABLE tblspace1.tsql (k1 string primary key,n1 int,s1 string)", Collections.emptyList());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey", Integer.valueOf(1))).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey2", Integer.valueOf(2))).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey3", Integer.valueOf(3))).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1) values(?)", Arrays.asList("mykey4")).getUpdateCount());
{
try (DataScanner scan1 = scan(manager, "SELECT COUNT(*) as cc FROM tblspace1.tsql", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertEquals(1, result.size());
assertEquals(Long.valueOf(4), result.get(0).get(0));
assertEquals(Long.valueOf(4), result.get(0).get("cc"));
}
}
try (DataScanner scan1 = scan(manager, "SELECT COUNT(*) FROM tblspace1.tsql", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertEquals(1, result.size());
assertEquals(Long.valueOf(4), result.get(0).get(0));
assertEquals(Long.valueOf(4), result.get(0).get("expr$0"));
}
{
try (DataScanner scan1 = scan(manager, "SELECT COUNT(*) as cc FROM tblspace1.tsql WHERE k1='mykey3'", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertEquals(1, result.size());
assertEquals(Long.valueOf(1), result.get(0).get(0));
assertEquals(Long.valueOf(1), result.get(0).get("cc"));
}
}
{
try (DataScanner scan1 = scan(manager, "SELECT COUNT(*),k1 FROM tblspace1.tsql", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
Assert.fail();
} catch (StatementExecutionException error) {
assertTrue("field k1 MUST appear in GROUP BY clause".equals(error.getMessage()) || error.getMessage().equals("From line 1, column 17 to line 1, column 18: Expression 'K1' is not being grouped"));
}
}
{
try (DataScanner scan1 = scan(manager, "SELECT COUNT(*) as cc,k1 FROM tblspace1.tsql GROUP BY k1", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertEquals(4, result.size());
for (DataAccessor t : result) {
assertEquals(Long.valueOf(1), t.get("cc"));
switch(t.get("k1") + "") {
case "mykey":
case "mykey2":
case "mykey3":
case "mykey4":
break;
default:
fail();
}
}
}
}
}
}
use of herddb.core.DBManager in project herddb by diennea.
the class RawSQLTest method simpleExitLoopTest.
@Test
public void simpleExitLoopTest() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "CREATE TABLE tblspace1.tsql (k1 string primary key,n1 int,s1 string)", Collections.emptyList());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey", Integer.valueOf(1))).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey2", Integer.valueOf(2))).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey3", Integer.valueOf(3))).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1) values(?)", Arrays.asList("mykey4")).getUpdateCount());
{
try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql LIMIT 1", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertEquals(1, result.size());
}
try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql LIMIT 2", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertEquals(2, result.size());
}
try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql LIMIT 10", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertEquals(4, result.size());
}
}
}
}
use of herddb.core.DBManager in project herddb by diennea.
the class RawSQLTest method testDMLOutOfOrderThanCreateTable.
@Test
public void testDMLOutOfOrderThanCreateTable() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "CREATE TABLE tblspace1.IP (\n" + " field0 INT NOT NULL PRIMARY KEY,\n" + " field1 VARCHAR(15) NOT NULL,\n" + " field2 VARCHAR(255) NOT NULL,\n" + " field3 INT NOT NULL,\n" + " field4 INT NOT NULL,\n" + " field5 NCLOB /* NOT NULL */\n" + ")", Collections.emptyList());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.IP (field0,field1,field3,field4,field2,field5) values(?,?,?,?,?,?)", Arrays.asList(1, "1.2.3.4", 2, 5, "localhost", "aaa")).getUpdateCount());
assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.IP set field3=?, field2=?, field4=?, field5=? WHERE field0=?", Arrays.asList(1, "localhost", 8, "aaa", 1)).getUpdateCount());
assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.IP set field3=?, field4=?, field2=?, field5=? WHERE field0=?", Arrays.asList(1, 8, "localhost", "aaa", 1)).getUpdateCount());
try (DataScanner scan = scan(manager, "SELECT field5,field4,field2,field3 FROM tblspace1.IP where field0=?", Arrays.asList(1))) {
while (scan.hasNext()) {
DataAccessor next = scan.next();
assertThat(next, instanceOf(RuntimeProjectedDataAccessor.class));
assertArrayEquals(new String[] { "field5", "field4", "field2", "field3" }, next.getFieldNames());
assertArrayEquals(new Object[] { RawString.of("aaa"), 8, RawString.of("localhost"), 1 }, next.getValues());
}
}
}
}
use of herddb.core.DBManager in project herddb by diennea.
the class RawSQLTest method caseWhenTest.
@Test
public void caseWhenTest() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "CREATE TABLE tblspace1.tsql (k1 string primary key,n1 int,s1 string,t1 timestamp)", Collections.emptyList());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1,t1) values(?,?,CURRENT_TIMESTAMP)", Arrays.asList("mykey", Integer.valueOf(1234))).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1,t1) values(?,?,CURRENT_TIMESTAMP)", Arrays.asList("mykey2", Integer.valueOf(1235))).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1,t1) values(?,?,CURRENT_TIMESTAMP)", Arrays.asList("mykey3", Integer.valueOf(1236))).getUpdateCount());
try (DataScanner scan = scan(manager, "SELECT k1, " + "CASE " + "WHEN k1='mykey' THEN 'a' " + "WHEN k1='mykey2' THEN 'b' " + "ELSE 'c' " + "END as mycase " + "FROM tblspace1.tsql " + "ORDER BY k1", Collections.emptyList())) {
List<DataAccessor> res = scan.consume();
for (DataAccessor t : res) {
System.out.println("t:" + t);
}
assertEquals(3, res.size());
assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey", "mycase", "a"))));
assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey2", "mycase", "b"))));
assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey3", "mycase", "c"))));
}
try (DataScanner scan = scan(manager, "SELECT k1, " + "CASE " + "WHEN k1='mykey' THEN 'a' " + "WHEN k1='mykey2' THEN 'b' " + "END as mycase " + "FROM tblspace1.tsql " + "ORDER BY k1", Collections.emptyList())) {
List<DataAccessor> res = scan.consume();
for (DataAccessor t : res) {
System.out.println("t:" + t);
}
assertEquals(3, res.size());
assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey", "mycase", "a"))));
assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey2", "mycase", "b"))));
assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey3", "mycase", null))));
}
if (manager.isFullSQLSupportEnabled()) {
try (DataScanner scan = scan(manager, "SELECT k1, " + "SUM(CASE " + "WHEN k1='mykey' THEN 1 " + "WHEN k1='mykey2' THEN 2 " + "ELSE 3 " + "END) as mysum " + "FROM tblspace1.tsql " + "GROUP BY k1", Collections.emptyList())) {
List<DataAccessor> res = scan.consume();
for (DataAccessor t : res) {
System.out.println("t2:" + t);
}
assertEquals(3, res.size());
assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey", "mysum", 1L))));
assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey2", "mysum", 2L))));
assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey3", "mysum", 3L))));
}
try (DataScanner scan = scan(manager, "SELECT " + "SUM(CASE " + "WHEN k1='mykey' THEN 1 " + "WHEN k1='mykey2' THEN 2 " + "ELSE 3 " + "END) as mysum " + "FROM tblspace1.tsql " + "", Collections.emptyList())) {
List<DataAccessor> res = scan.consume();
for (DataAccessor t : res) {
System.out.println("t:" + t);
}
assertEquals(1, res.size());
assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("mysum", 6L))));
}
}
}
}
Aggregations