use of herddb.utils.DataAccessor 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.utils.DataAccessor in project herddb by diennea.
the class RawSQLTest method multiInsert.
@Test
public void multiInsert() 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());
java.sql.Timestamp tt1 = new java.sql.Timestamp(System.currentTimeMillis());
java.sql.Timestamp tt2 = new java.sql.Timestamp(System.currentTimeMillis() + 60000);
assertEquals(2, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1,t1) values(?,?,?),(?,?,?)", Arrays.asList("mykey", Integer.valueOf(1234), tt1, "mykey2", Integer.valueOf(1235), tt2)).getUpdateCount());
try (DataScanner scan = scan(manager, "SELECT k1,n1,t1 FROM tblspace1.tsql ORDER BY n1 desc", Collections.emptyList())) {
List<DataAccessor> res = scan.consume();
assertEquals(RawString.of("mykey2"), res.get(0).get("k1"));
assertEquals(RawString.of("mykey"), res.get(1).get("k1"));
assertEquals(Integer.valueOf(1235), res.get(0).get("n1"));
assertEquals(Integer.valueOf(1234), res.get(1).get("n1"));
assertEquals(tt2, res.get(0).get("t1"));
assertEquals(tt1, res.get(1).get("t1"));
}
execute(manager, "CREATE TABLE tblspace1.tsql2 (a1 integer auto_increment primary key, k1 string ,n1 int,s1 string,t1 timestamp)", Collections.emptyList());
assertEquals(2, executeUpdate(manager, "INSERT INTO tblspace1.tsql2(k1,n1,t1) values(?,?,?),(?,?,?)", Arrays.asList("mykey", Integer.valueOf(1234), tt1, "mykey2", Integer.valueOf(1235), tt2)).getUpdateCount());
try (DataScanner scan = scan(manager, "SELECT a1,k1,n1,t1 FROM tblspace1.tsql2 ORDER BY n1 desc", Collections.emptyList())) {
List<DataAccessor> res = scan.consume();
assertEquals(RawString.of("mykey2"), res.get(0).get("k1"));
assertEquals(RawString.of("mykey"), res.get(1).get("k1"));
assertEquals(Integer.valueOf(1235), res.get(0).get("n1"));
assertEquals(Integer.valueOf(1234), res.get(1).get("n1"));
assertEquals(tt2, res.get(0).get("t1"));
assertEquals(tt1, res.get(1).get("t1"));
assertEquals(2, res.get(0).get("a1"));
assertEquals(1, res.get(1).get("a1"));
}
// auto-transaction
execute(manager, "CREATE TABLE tblspace1.tsql3 (a1 integer auto_increment primary key, k1 string ,n1 int,s1 string,t1 timestamp)", Collections.emptyList());
DMLStatementExecutionResult resInsert = executeUpdate(manager, "INSERT INTO tblspace1.tsql3(k1,n1,t1) values(?,?,?),(?,?,?)", Arrays.asList("mykey", Integer.valueOf(1234), tt1, "mykey2", Integer.valueOf(1235), tt2), TransactionContext.AUTOTRANSACTION_TRANSACTION);
assertEquals(2, resInsert.getUpdateCount());
assertTrue(resInsert.transactionId > 0);
try (DataScanner scan = scan(manager, "SELECT a1,k1,n1,t1 FROM tblspace1.tsql3 ORDER BY n1 desc", Collections.emptyList(), 0, new TransactionContext(resInsert.transactionId))) {
List<DataAccessor> res = scan.consume();
assertEquals(RawString.of("mykey2"), res.get(0).get("k1"));
assertEquals(RawString.of("mykey"), res.get(1).get("k1"));
assertEquals(Integer.valueOf(1235), res.get(0).get("n1"));
assertEquals(Integer.valueOf(1234), res.get(1).get("n1"));
assertEquals(tt2, res.get(0).get("t1"));
assertEquals(tt1, res.get(1).get("t1"));
assertEquals(2, res.get(0).get("a1"));
assertEquals(1, res.get(1).get("a1"));
}
}
}
use of herddb.utils.DataAccessor 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))));
}
}
}
}
use of herddb.utils.DataAccessor in project herddb by diennea.
the class RawSQLTest method selectWithParameters.
@Test
public void selectWithParameters() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
assumeTrue(manager.getPlanner() instanceof JSQLParserPlanner);
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(1234))).getUpdateCount());
try (DataScanner scan = scan(manager, "SELECT ? as foo, k1, n1 FROM tblspace1.tsql", Arrays.asList("test"))) {
List<DataAccessor> all = scan.consume();
assertEquals(1, all.size());
assertEquals(RawString.of("test"), all.get(0).get("foo"));
assertEquals(RawString.of("mykey"), all.get(0).get("k1"));
assertEquals(Integer.valueOf(1234), all.get(0).get("n1"));
}
Timestamp timestamp = new java.sql.Timestamp(System.currentTimeMillis());
try (DataScanner scan = scan(manager, "SELECT ? as foo, ? as bar FROM tblspace1.tsql", Arrays.asList(Long.valueOf(1), timestamp))) {
List<DataAccessor> all = scan.consume();
assertEquals(1, all.size());
assertEquals(Long.valueOf(1), all.get(0).get("foo"));
assertEquals(timestamp, all.get(0).get("bar"));
}
if (manager.isFullSQLSupportEnabled()) {
try (DataScanner scan = scan(manager, "SELECT MAX(?) as foo, MIN(?) as bar FROM tblspace1.tsql", Arrays.asList(Long.valueOf(1), timestamp))) {
List<DataAccessor> all = scan.consume();
assertEquals(1, all.size());
assertEquals(Long.valueOf(1), all.get(0).get("foo"));
assertEquals(timestamp, all.get(0).get("bar"));
}
}
executeUpdate(manager, "DELETE FROM tblspace1.tsql", Collections.emptyList());
try (DataScanner scan = scan(manager, "SELECT ? as foo, ? as bar FROM tblspace1.tsql", Arrays.asList(Long.valueOf(1), timestamp))) {
List<DataAccessor> all = scan.consume();
assertEquals(0, all.size());
}
if (manager.isFullSQLSupportEnabled()) {
try (DataScanner scan = scan(manager, "SELECT MAX(?) as foo, MIN(?) as bar FROM tblspace1.tsql", Arrays.asList(Long.valueOf(1), timestamp))) {
List<DataAccessor> all = scan.consume();
assertEquals(1, all.size());
assertNull(all.get(0).get("foo"));
assertNull(all.get(0).get("bar"));
}
}
}
}
use of herddb.utils.DataAccessor in project herddb by diennea.
the class RawSQLTest method compareToNull.
@Test
public void compareToNull() 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());
executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey", null));
executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey2", 213));
executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey3", 214));
// comparing with NULLs return NULL, not "false" or "true"
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql WHERE n1=213", Arrays.asList())) {
assertEquals(1, scan.consume().size());
}
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql WHERE n1 = NULL", Arrays.asList())) {
assertEquals(0, scan.consume().size());
}
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql WHERE n1 IS NULL", Arrays.asList())) {
assertEquals(1, scan.consume().size());
}
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql WHERE n1 IS NOT NULL", Arrays.asList())) {
assertEquals(2, scan.consume().size());
}
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql WHERE NOT n1 IS NULL", Arrays.asList())) {
assertEquals(2, scan.consume().size());
}
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql WHERE NOT n1 <= 213", Arrays.asList())) {
assertEquals(1, scan.consume().size());
}
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql WHERE n1 <= 214", Arrays.asList())) {
assertEquals(2, scan.consume().size());
}
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql WHERE n1 > 213", Arrays.asList())) {
assertEquals(1, scan.consume().size());
}
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql WHERE n1 >= 213", Arrays.asList())) {
assertEquals(2, scan.consume().size());
}
// In sorts NULL is greater than other values
try (DataScanner scan = scan(manager, "SELECT k1 FROM tblspace1.tsql ORDER BY n1 ASC NULLS LAST", Arrays.asList())) {
List<DataAccessor> consume = scan.consume();
assertEquals(3, consume.size());
int i = 0;
assertEquals(RawString.of("mykey2"), consume.get(i++).get(0));
assertEquals(RawString.of("mykey3"), consume.get(i++).get(0));
assertEquals(RawString.of("mykey"), consume.get(i++).get(0));
}
try (DataScanner scan = scan(manager, "SELECT k1 FROM tblspace1.tsql ORDER BY n1 DESC NULLS LAST", Arrays.asList())) {
List<DataAccessor> consume = scan.consume();
assertEquals(3, consume.size());
int i = 0;
assertEquals(RawString.of("mykey"), consume.get(i++).get(0));
assertEquals(RawString.of("mykey3"), consume.get(i++).get(0));
assertEquals(RawString.of("mykey2"), consume.get(i++).get(0));
}
try (DataScanner scan = scan(manager, "SELECT k1 FROM tblspace1.tsql ORDER BY n1 ASC", Arrays.asList())) {
List<DataAccessor> consume = scan.consume();
assertEquals(3, consume.size());
int i = 0;
assertEquals(RawString.of("mykey2"), consume.get(i++).get(0));
assertEquals(RawString.of("mykey3"), consume.get(i++).get(0));
assertEquals(RawString.of("mykey"), consume.get(i++).get(0));
}
try (DataScanner scan = scan(manager, "SELECT k1 FROM tblspace1.tsql ORDER BY n1 DESC", Arrays.asList())) {
List<DataAccessor> consume = scan.consume();
assertEquals(3, consume.size());
int i = 0;
assertEquals(RawString.of("mykey"), consume.get(i++).get(0));
assertEquals(RawString.of("mykey3"), consume.get(i++).get(0));
assertEquals(RawString.of("mykey2"), consume.get(i++).get(0));
}
try (DataScanner scan = scan(manager, "SELECT k1 FROM tblspace1.tsql ORDER BY n1 ASC NULLS FIRST", Arrays.asList())) {
List<DataAccessor> consume = scan.consume();
assertEquals(3, consume.size());
int i = 0;
assertEquals(RawString.of("mykey"), consume.get(i++).get(0));
assertEquals(RawString.of("mykey2"), consume.get(i++).get(0));
assertEquals(RawString.of("mykey3"), consume.get(i++).get(0));
}
}
}
Aggregations