use of herddb.core.DBManager in project herddb by diennea.
the class AlterTableSQLTest method alterTableImplitlyCommitsTransaction.
@Test
public void alterTableImplitlyCommitsTransaction() 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());
Table table = manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable();
assertEquals(0, table.getColumn("k1").serialPosition);
assertEquals(1, table.getColumn("n1").serialPosition);
assertEquals(2, table.getColumn("s1").serialPosition);
long tx = TestUtils.beginTransaction(manager, "tblspace1");
execute(manager, "INSERT INTO tblspace1.tsql (k1,n1,s1) values('a',1,'b')", Collections.emptyList(), new TransactionContext(tx));
{
// record not visible outside transaction
List<DataAccessor> tuples = scan(manager, "SELECT * FROM tblspace1.tsql", Collections.emptyList(), TransactionContext.NO_TRANSACTION).consumeAndClose();
assertEquals(0, tuples.size());
}
{
// record visible inside transaction
List<DataAccessor> tuples = scan(manager, "SELECT * FROM tblspace1.tsql", Collections.emptyList(), new TransactionContext(tx)).consumeAndClose();
assertEquals(1, tuples.size());
assertEquals(3, tuples.get(0).getFieldNames().length);
assertEquals(RawString.of("b"), tuples.get(0).get("s1"));
}
execute(manager, "ALTER TABLE tblspace1.tsql drop column s1", Collections.emptyList(), new TransactionContext(tx));
table = manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable();
assertEquals(0, table.getColumn("k1").serialPosition);
assertEquals(1, table.getColumn("n1").serialPosition);
assertEquals(2, table.columns.length);
// transaction no more exists (it has been committed)
assertNull(manager.getTableSpaceManager("tblspace1").getTransaction(tx));
// record is visible out of the transaction, but with only 2 columns
{
List<DataAccessor> tuples = scan(manager, "SELECT * FROM tblspace1.tsql", Collections.emptyList(), TransactionContext.NO_TRANSACTION).consumeAndClose();
assertEquals(1, tuples.size());
assertEquals(2, tuples.get(0).getFieldNames().length);
}
}
}
use of herddb.core.DBManager in project herddb by diennea.
the class AlterTableSQLTest method renameTable.
@Test
public void renameTable() 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 int primary key auto_increment,n1 int,s1 string)", Collections.emptyList());
execute(manager, "INSERT INTO tblspace1.tsql (n1,s1) values(1,'b')", Collections.emptyList());
{
List<DataAccessor> tuples = scan(manager, "SELECT * FROM tblspace1.tsql where k1=1", Collections.emptyList()).consumeAndClose();
assertEquals(1, tuples.size());
assertEquals(3, tuples.get(0).getFieldNames().length);
}
// rename to UPPERCASE
execute(manager, "EXECUTE RENAMETABLE 'tblspace1','tsql','TSQL2'", Collections.emptyList());
{
List<DataAccessor> tuples = scan(manager, "SELECT * FROM tblspace1.tsql2 where k1=1", Collections.emptyList()).consumeAndClose();
assertEquals(1, tuples.size());
assertEquals(3, tuples.get(0).getFieldNames().length);
}
{
List<DataAccessor> tuples = scan(manager, "SELECT * FROM tblspace1.TSQL2 where k1=1", Collections.emptyList()).consumeAndClose();
assertEquals(1, tuples.size());
assertEquals(3, tuples.get(0).getFieldNames().length);
}
// rename using different case
execute(manager, "EXECUTE RENAMETABLE 'tblspace1','tsql2','tsql3'", Collections.emptyList());
{
List<DataAccessor> tuples = scan(manager, "SELECT * FROM tblspace1.tsql3 where k1=1", Collections.emptyList()).consumeAndClose();
assertEquals(1, tuples.size());
assertEquals(3, tuples.get(0).getFieldNames().length);
}
}
}
use of herddb.core.DBManager in project herddb by diennea.
the class AlterTableSQLTest method addColumn.
@Test
public void addColumn() 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());
Table table = manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable();
assertEquals(0, table.getColumn("k1").serialPosition);
assertEquals(1, table.getColumn("n1").serialPosition);
assertEquals(2, table.getColumn("s1").serialPosition);
execute(manager, "INSERT INTO tblspace1.tsql (k1,n1,s1) values('a',1,'b')", Collections.emptyList());
{
List<DataAccessor> tuples = scan(manager, "SELECT * FROM tblspace1.tsql", Collections.emptyList()).consumeAndClose();
assertEquals(1, tuples.size());
assertEquals(3, tuples.get(0).getFieldNames().length);
}
execute(manager, "ALTER TABLE tblspace1.tsql add column k2 string", Collections.emptyList());
// uppercase table name !
execute(manager, "INSERT INTO tblspace1.TSQL (k1,n1,s1,k2) values('b',1,'b','c')", Collections.emptyList());
{
List<DataAccessor> tuples = scan(manager, "SELECT * FROM tblspace1.tsql WHERE k2='c'", Collections.emptyList()).consumeAndClose();
assertEquals(1, tuples.size());
assertEquals(4, tuples.get(0).getFieldNames().length);
}
table = manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable();
assertEquals(0, table.getColumn("k1").serialPosition);
assertEquals(1, table.getColumn("n1").serialPosition);
assertEquals(2, table.getColumn("s1").serialPosition);
assertEquals(3, table.getColumn("k2").serialPosition);
// check alter table is case non sensitive about table names
execute(manager, "ALTER TABLE tblspace1.TSQL add column k10 string", Collections.emptyList());
execute(manager, "ALTER TABLE tblspace1.tSql add column k11 string", Collections.emptyList());
table = manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable();
assertEquals(ColumnTypes.STRING, table.getColumn("k11").type);
assertEquals("Found a record in table tsql that contains a NULL value for column k11 ALTER command is not possible", herddb.utils.TestUtils.expectThrows(StatementExecutionException.class, () -> {
execute(manager, "ALTER TABLE tblspace1.tSql modify column k11 string not null", Collections.emptyList());
}).getMessage());
// no effect
table = manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable();
assertEquals(ColumnTypes.STRING, table.getColumn("k11").type);
execute(manager, "TRUNCATE TABLE tblspace1.tSql", Collections.emptyList());
execute(manager, "ALTER TABLE tblspace1.tSql modify column k11 string not null", Collections.emptyList());
table = manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable();
assertEquals(ColumnTypes.NOTNULL_STRING, table.getColumn("k11").type);
}
}
use of herddb.core.DBManager in project herddb by diennea.
the class AlterTableSQLTest method modifyColumnWithDefaults.
@Test
public void modifyColumnWithDefaults() 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 int primary key auto_increment,n1 int default 1234,s1 string)", Collections.emptyList());
assertTrue(manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable().auto_increment);
Table table = manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable();
assertEquals(0, table.getColumn("k1").serialPosition);
assertEquals(1, table.getColumn("n1").serialPosition);
assertEquals(1234, table.getColumn("n1").defaultValue.to_int());
assertEquals(2, table.getColumn("s1").serialPosition);
// keep default
execute(manager, "ALTER TABLE tblspace1.tsql modify column n1 int", Collections.emptyList());
table = manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable();
assertEquals(0, table.getColumn("k1").serialPosition);
assertEquals(1, table.getColumn("n1").serialPosition);
assertEquals(1234, table.getColumn("n1").defaultValue.to_int());
assertEquals(2, table.getColumn("s1").serialPosition);
// alter default
execute(manager, "ALTER TABLE tblspace1.tsql modify column n1 int default 1255", Collections.emptyList());
table = manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable();
assertEquals(0, table.getColumn("k1").serialPosition);
assertEquals(1, table.getColumn("n1").serialPosition);
assertEquals(1255, table.getColumn("n1").defaultValue.to_int());
assertEquals(2, table.getColumn("s1").serialPosition);
// rename, shuold keep default
execute(manager, "ALTER TABLE tblspace1.tsql change column n1 n2 int", Collections.emptyList());
table = manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable();
assertEquals(0, table.getColumn("k1").serialPosition);
assertEquals(1, table.getColumn("n2").serialPosition);
assertEquals(1255, table.getColumn("n2").defaultValue.to_int());
assertEquals(2, table.getColumn("s1").serialPosition);
// drop default
execute(manager, "ALTER TABLE tblspace1.tsql modify column n2 int default null", Collections.emptyList());
table = manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable();
assertEquals(0, table.getColumn("k1").serialPosition);
assertEquals(1, table.getColumn("n2").serialPosition);
assertNull(table.getColumn("n2").defaultValue);
assertEquals(2, table.getColumn("s1").serialPosition);
execute(manager, "ALTER TABLE tblspace1.tsql add mydate timestamp default CURRENT_TIMESTAMP", Collections.emptyList());
table = manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable();
assertEquals(0, table.getColumn("k1").serialPosition);
assertEquals(1, table.getColumn("n2").serialPosition);
assertNull(table.getColumn("n2").defaultValue);
assertEquals(3, table.getColumn("mydate").serialPosition);
assertEquals(Bytes.from_string("CURRENT_TIMESTAMP"), table.getColumn("mydate").defaultValue);
}
}
use of herddb.core.DBManager in project herddb by diennea.
the class BetterExecuteSyntaxTest method betterSyntax.
@Test
public void betterSyntax() throws Exception {
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
assertTrue(manager.waitForBootOfLocalTablespaces(10000));
execute(manager, "CREATE TABLESPACE 'tblspace1'", Collections.emptyList());
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "ALTER TABLESPACE 'TBLSPACE1','expectedreplicacount:2'", Collections.emptyList());
long tx = ((TransactionResult) execute(manager, "BEGIN TRANSACTION 'tblspace1'", Collections.emptyList())).getTransactionId();
execute(manager, "COMMIT TRANSACTION 'tblspace1'," + tx, Collections.emptyList());
long tx2 = ((TransactionResult) execute(manager, "BEGIN TRANSACTION 'tblspace1'", Collections.emptyList())).getTransactionId();
execute(manager, "ROLLBACK TRANSACTION 'tblspace1'," + tx2, Collections.emptyList());
execute(manager, "DROP TABLESPACE 'tblspace1'", Collections.emptyList());
try (DataScanner scan = TestUtils.scan(manager, "SELECT COUNT(*) FROM systablespaces WHERE tablespace_name=?", Arrays.asList("tblspace1"))) {
DataAccessor first = scan.consume().get(0);
Number count = (Number) first.get(first.getFieldNames()[0]);
assertEquals(0, count.intValue());
}
}
}
Aggregations