Search in sources :

Example 1 with IllegalDataAccessException

use of herddb.utils.IllegalDataAccessException in project herddb by diennea.

the class AlterTableSQLTest method dropColumn.

@Test
public void dropColumn() 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()).consume();
            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());
        table = manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable();
        assertEquals(0, table.getColumn("k1").serialPosition);
        assertEquals(1, table.getColumn("n1").serialPosition);
        assertEquals(2, table.columns.length);
        {
            List<DataAccessor> tuples = scan(manager, "SELECT * FROM tblspace1.tsql", Collections.emptyList()).consume();
            assertEquals(1, tuples.size());
            assertEquals(2, tuples.get(0).getFieldNames().length);
            try {
                assertEquals(null, tuples.get(0).get("s1"));
                fail("field does not exist anymore");
            } catch (IllegalDataAccessException ok) {
            }
        }
        execute(manager, "ALTER TABLE tblspace1.tsql add column s1 string", Collections.emptyList());
        table = manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable();
        assertEquals(0, table.getColumn("k1").serialPosition);
        assertEquals(1, table.getColumn("n1").serialPosition);
        assertEquals(3, table.getColumn("s1").serialPosition);
        {
            List<DataAccessor> tuples = scan(manager, "SELECT * FROM tblspace1.tsql", Collections.emptyList()).consume();
            assertEquals(1, tuples.size());
            assertEquals(3, tuples.get(0).getFieldNames().length);
            assertEquals(null, tuples.get(0).get("s1"));
        }
        try {
            execute(manager, "ALTER TABLE tblspace1.tsql drop column k1", Collections.emptyList());
            fail();
        } catch (StatementExecutionException error) {
            assertTrue(error.getMessage().contains("column k1 cannot be dropped because is part of the primary key"));
        }
    }
}
Also used : IllegalDataAccessException(herddb.utils.IllegalDataAccessException) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) Table(herddb.model.Table) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) List(java.util.List) RawString(herddb.utils.RawString) StatementExecutionException(herddb.model.StatementExecutionException) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 2 with IllegalDataAccessException

use of herddb.utils.IllegalDataAccessException in project herddb by diennea.

the class DeleteTest method deleteDeleteWithMulticolumnPKTest.

@Test
public void deleteDeleteWithMulticolumnPKTest() 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(), NO_TRANSACTION);
        manager.waitForTablespace("tblspace1", 10000);
        execute(manager, "CREATE TABLE tblspace1.tsql (K1 string ,s1 string,n1 int, primary key(k1,s1))", Collections.emptyList());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,s1,n1) values(?,?,?)", Arrays.asList("mykey", "a", Integer.valueOf(1234))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,s1,n1) values(?,?,?)", Arrays.asList("mykey2", "a", Integer.valueOf(1234))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,s1,n1) values(?,?,?)", Arrays.asList("mykey3", "a", Integer.valueOf(1234))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,s1,n1) values(?,?,?)", Arrays.asList("mykey4", "a", Integer.valueOf(1234))).getUpdateCount());
        try {
            executeUpdate(manager, "DELETE FROM tblspace1.tsql WHERE badfield=1234", Collections.emptyList());
            fail();
        } catch (StatementExecutionException ok) {
            ok.printStackTrace();
            assertTrue(ok.getCause() instanceof IllegalDataAccessException || ok.getMessage().contains("BADFIELD"));
        }
        assertEquals(4, executeUpdate(manager, "DELETE FROM tblspace1.tsql WHERE N1=1234", Collections.emptyList()).getUpdateCount());
        assertEquals(0, scan(manager, "SELECT * FROM tblspace1.tsql WHERE N1=1234", Collections.emptyList()).consume().size());
    }
}
Also used : IllegalDataAccessException(herddb.utils.IllegalDataAccessException) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) StatementExecutionException(herddb.model.StatementExecutionException) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Aggregations

MemoryCommitLogManager (herddb.mem.MemoryCommitLogManager)2 MemoryDataStorageManager (herddb.mem.MemoryDataStorageManager)2 MemoryMetadataStorageManager (herddb.mem.MemoryMetadataStorageManager)2 StatementExecutionException (herddb.model.StatementExecutionException)2 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)2 IllegalDataAccessException (herddb.utils.IllegalDataAccessException)2 Test (org.junit.Test)2 Table (herddb.model.Table)1 RawString (herddb.utils.RawString)1 List (java.util.List)1