Search in sources :

Example 6 with TableDoesNotExistException

use of herddb.model.TableDoesNotExistException in project herddb by diennea.

the class JSQLParserPlanner method buildCreateIndexStatement.

private Statement buildCreateIndexStatement(String defaultTableSpace, CreateIndex s) throws StatementExecutionException {
    try {
        String tableSpace = s.getTable().getSchemaName();
        if (tableSpace == null) {
            tableSpace = defaultTableSpace;
        }
        tableSpace = fixMySqlBackTicks(tableSpace);
        String tableName = fixMySqlBackTicks(s.getTable().getName().toLowerCase());
        String indexName = fixMySqlBackTicks(s.getIndex().getName().toLowerCase());
        boolean unique = isUnique(s.getIndex().getType());
        String indexType = convertIndexType(s.getIndex().getType());
        herddb.model.Index.Builder builder = herddb.model.Index.builder().name(indexName).uuid(UUID.randomUUID().toString()).type(indexType).unique(unique).table(tableName).tablespace(tableSpace);
        AbstractTableManager tableDefinition = manager.getTableSpaceManager(tableSpace).getTableManager(tableName);
        if (tableDefinition == null) {
            throw new TableDoesNotExistException("no such table " + tableName + " in tablespace " + tableSpace);
        }
        for (String columnName : s.getIndex().getColumnsNames()) {
            columnName = fixMySqlBackTicks(columnName.toLowerCase());
            Column column = tableDefinition.getTable().getColumn(columnName);
            if (column == null) {
                throw new StatementExecutionException("no such column " + columnName + " on table " + tableName + " in tablespace " + tableSpace);
            }
            builder.column(column.name, column.type);
        }
        CreateIndexStatement statement = new CreateIndexStatement(builder.build());
        return statement;
    } catch (IllegalArgumentException err) {
        throw new StatementExecutionException("bad index definition: " + err.getMessage(), err);
    }
}
Also used : TableDoesNotExistException(herddb.model.TableDoesNotExistException) AbstractTableManager(herddb.core.AbstractTableManager) Column(herddb.model.Column) CreateIndexStatement(herddb.model.commands.CreateIndexStatement) CreateIndex(net.sf.jsqlparser.statement.create.index.CreateIndex) ForeignKeyIndex(net.sf.jsqlparser.statement.create.table.ForeignKeyIndex) Index(net.sf.jsqlparser.statement.create.table.Index) StatementExecutionException(herddb.model.StatementExecutionException)

Example 7 with TableDoesNotExistException

use of herddb.model.TableDoesNotExistException in project herddb by diennea.

the class JSQLParserPlanner method getTable.

private Table getTable(String defaultTableSpace, net.sf.jsqlparser.schema.Table table) {
    String tableSpace = table.getSchemaName();
    if (tableSpace == null) {
        tableSpace = defaultTableSpace;
    }
    tableSpace = fixMySqlBackTicks(tableSpace);
    TableSpaceManager tableSpaceManager = getTableSpaceManager(tableSpace);
    if (tableSpaceManager == null) {
        clearCache();
        throw new StatementExecutionException("tablespace " + defaultTableSpace + " is not available");
    }
    String tableName = fixMySqlBackTicks(table.getName().toLowerCase());
    AbstractTableManager tableManager = tableSpaceManager.getTableManager(tableName);
    if (tableManager == null) {
        throw new TableDoesNotExistException("no table " + tableName + " here for " + tableSpace);
    }
    return tableManager.getTable();
}
Also used : TableDoesNotExistException(herddb.model.TableDoesNotExistException) AbstractTableManager(herddb.core.AbstractTableManager) TableSpaceManager(herddb.core.TableSpaceManager) StatementExecutionException(herddb.model.StatementExecutionException)

Example 8 with TableDoesNotExistException

use of herddb.model.TableDoesNotExistException in project herddb by diennea.

the class ShowCreateTableCalculator method calculate.

public static String calculate(boolean showCreateIndex, String tableName, String tableSpace, AbstractTableManager tableManager) {
    Table t = tableManager.getTable();
    if (t == null) {
        throw new TableDoesNotExistException(String.format("Table %s does not exist.", tableName));
    }
    StringBuilder sb = new StringBuilder("CREATE TABLE " + tableSpace + "." + tableName);
    StringJoiner joiner = new StringJoiner(",", "(", ")");
    for (Column c : t.getColumns()) {
        joiner.add(c.name + " " + ColumnTypes.typeToString(c.type) + autoIncrementColumn(t, c) + defaultClause(c));
    }
    if (t.getPrimaryKey().length > 0) {
        joiner.add("PRIMARY KEY(" + String.join(",", t.getPrimaryKey()) + ")");
    }
    if (showCreateIndex) {
        List<Index> indexes = tableManager.getAvailableIndexes();
        if (!indexes.isEmpty()) {
            indexes.forEach(idx -> {
                if (idx.unique) {
                    joiner.add("UNIQUE KEY " + idx.name + " (" + String.join(",", idx.columnNames) + ")");
                } else {
                    joiner.add("INDEX " + idx.name + "(" + String.join(",", idx.columnNames) + ")");
                }
            });
        }
    }
    sb.append(joiner.toString());
    return sb.toString();
}
Also used : TableDoesNotExistException(herddb.model.TableDoesNotExistException) Table(herddb.model.Table) Column(herddb.model.Column) Index(herddb.model.Index) StringJoiner(java.util.StringJoiner)

Example 9 with TableDoesNotExistException

use of herddb.model.TableDoesNotExistException in project herddb by diennea.

the class DropTableSQLTest method dropTableWithTransaction.

@Test
public void dropTableWithTransaction() 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);
        long tx = ((TransactionResult) execute(manager, "EXECUTE begintransaction 'tblspace1'", Collections.emptyList())).getTransactionId();
        execute(manager, "CREATE TABLE tblspace1.tsql (k1 string primary key,n1 int,s1 string)", Collections.emptyList(), new TransactionContext(tx));
        execute(manager, "INSERT INTO tblspace1.tsql (k1) values('a')", Collections.emptyList(), new TransactionContext(tx));
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql ", Collections.emptyList())) {
            fail();
        } catch (TableDoesNotExistException ok) {
        }
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables where table_name='tsql'", Collections.emptyList())) {
            List<DataAccessor> all = scan.consume();
            assertEquals(0, all.size());
        }
        execute(manager, "EXECUTE committransaction 'tblspace1'," + tx, Collections.emptyList());
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql ", Collections.emptyList())) {
            List<DataAccessor> all = scan.consume();
            assertEquals(1, all.size());
        }
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables where table_name='tsql'", Collections.emptyList())) {
            List<DataAccessor> all = scan.consume();
            assertEquals(1, all.size());
        }
        long tx2 = ((TransactionResult) execute(manager, "EXECUTE begintransaction 'tblspace1'", Collections.emptyList())).getTransactionId();
        // name is not case sensitive
        execute(manager, "DROP TABLE tblspace1.tSQL", Collections.emptyList(), new TransactionContext(tx2));
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql ", Collections.emptyList())) {
            List<DataAccessor> all = scan.consume();
            assertEquals(1, all.size());
        }
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables where table_name='tsql'", Collections.emptyList())) {
            List<DataAccessor> all = scan.consume();
            assertEquals(1, all.size());
        }
        execute(manager, "EXECUTE committransaction 'tblspace1'," + tx2, Collections.emptyList());
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables where table_name='tsql'", Collections.emptyList())) {
            List<DataAccessor> all = scan.consume();
            assertEquals(0, all.size());
        }
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql ", Collections.emptyList())) {
            fail();
        } catch (TableDoesNotExistException ok) {
            assertTrue(manager.getPlanner() instanceof JSQLParserPlanner);
        } catch (StatementExecutionException ok) {
            assertEquals("From line 1, column 15 to line 1, column 28: Object 'TSQL' not found within 'tblspace1'", ok.getMessage());
            assertTrue(manager.getPlanner() instanceof CalcitePlanner);
        }
    }
}
Also used : JSQLParserPlanner(herddb.sql.JSQLParserPlanner) TransactionResult(herddb.model.TransactionResult) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) DataAccessor(herddb.utils.DataAccessor) StatementExecutionException(herddb.model.StatementExecutionException) TableDoesNotExistException(herddb.model.TableDoesNotExistException) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DataScanner(herddb.model.DataScanner) TransactionContext(herddb.model.TransactionContext) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) CalcitePlanner(herddb.sql.CalcitePlanner) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 10 with TableDoesNotExistException

use of herddb.model.TableDoesNotExistException in project herddb by diennea.

the class SimpleTransactionTest method rollbackCreateTable.

@Test
public void rollbackCreateTable() throws Exception {
    Bytes key = Bytes.from_int(1234);
    Bytes value = Bytes.from_long(8888);
    Table transacted_table = Table.builder().tablespace(tableSpace).name("t2").column("id", ColumnTypes.STRING).column("name", ColumnTypes.STRING).primaryKey("id").build();
    long tx = beginTransaction();
    CreateTableStatement st_create = new CreateTableStatement(transacted_table);
    manager.executeStatement(st_create, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx));
    InsertStatement insert = new InsertStatement(tableSpace, "t2", new Record(key, value));
    assertEquals(1, manager.executeUpdate(insert, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx)).getUpdateCount());
    GetStatement get = new GetStatement(tableSpace, "t2", key, null, false);
    GetResult result = manager.get(get, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx));
    assertTrue(result.found());
    assertEquals(key, result.getRecord().key);
    assertEquals(value, result.getRecord().value);
    RollbackTransactionStatement rollback = new RollbackTransactionStatement(tableSpace, tx);
    manager.executeStatement(rollback, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
    try {
        manager.get(new GetStatement(tableSpace, "t2", key, null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        fail();
    } catch (TableDoesNotExistException error) {
    }
}
Also used : TableDoesNotExistException(herddb.model.TableDoesNotExistException) Bytes(herddb.utils.Bytes) Table(herddb.model.Table) GetResult(herddb.model.GetResult) TransactionContext(herddb.model.TransactionContext) GetStatement(herddb.model.commands.GetStatement) CreateTableStatement(herddb.model.commands.CreateTableStatement) RollbackTransactionStatement(herddb.model.commands.RollbackTransactionStatement) Record(herddb.model.Record) InsertStatement(herddb.model.commands.InsertStatement) Test(org.junit.Test)

Aggregations

TableDoesNotExistException (herddb.model.TableDoesNotExistException)26 StatementExecutionException (herddb.model.StatementExecutionException)16 Table (herddb.model.Table)11 Test (org.junit.Test)10 AbstractTableManager (herddb.core.AbstractTableManager)7 DataScanner (herddb.model.DataScanner)7 TableSpaceDoesNotExistException (herddb.model.TableSpaceDoesNotExistException)7 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)7 DataStorageManagerException (herddb.storage.DataStorageManagerException)7 DumpedLogEntry (herddb.backup.DumpedLogEntry)6 CommitLogResult (herddb.log.CommitLogResult)6 LogEntry (herddb.log.LogEntry)6 LogNotAvailableException (herddb.log.LogNotAvailableException)6 MemoryCommitLogManager (herddb.mem.MemoryCommitLogManager)6 MemoryMetadataStorageManager (herddb.mem.MemoryMetadataStorageManager)6 DDLStatementExecutionResult (herddb.model.DDLStatementExecutionResult)6 IndexAlreadyExistsException (herddb.model.IndexAlreadyExistsException)6 IndexDoesNotExistException (herddb.model.IndexDoesNotExistException)6 TransactionContext (herddb.model.TransactionContext)6 Bytes (herddb.utils.Bytes)6