Search in sources :

Example 96 with StatementExecutionException

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

the class SQLPlanner method buildCreateIndexStatement.

private Statement buildCreateIndexStatement(String defaultTableSpace, CreateIndex s) throws StatementExecutionException {
    try {
        String tableSpace = s.getTable().getSchemaName();
        if (tableSpace == null) {
            tableSpace = defaultTableSpace;
        }
        String tableName = s.getTable().getName();
        String indexName = s.getIndex().getName().toLowerCase();
        String indexType = convertIndexType(s.getIndex().getType());
        herddb.model.Index.Builder builder = herddb.model.Index.builder().name(indexName).uuid(UUID.randomUUID().toString()).type(indexType).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 = 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) Index(net.sf.jsqlparser.statement.create.table.Index) StatementExecutionException(herddb.model.StatementExecutionException)

Example 97 with StatementExecutionException

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

the class SQLPlanner method buildUpdateStatement.

private ExecutionPlan buildUpdateStatement(String defaultTableSpace, Update s, boolean returnValues) throws StatementExecutionException {
    if (s.getTables().size() != 1) {
        throw new StatementExecutionException("unsupported multi-table update " + s);
    }
    net.sf.jsqlparser.schema.Table fromTable = s.getTables().get(0);
    String tableSpace = fromTable.getSchemaName();
    String tableName = fromTable.getName();
    if (tableSpace == null) {
        tableSpace = defaultTableSpace;
    }
    TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSpace);
    if (tableSpaceManager == null) {
        throw new StatementExecutionException("no such tablespace " + tableSpace + " here at " + manager.getNodeId());
    }
    AbstractTableManager tableManager = tableSpaceManager.getTableManager(tableName);
    if (tableManager == null) {
        throw new StatementExecutionException("no such table " + tableName + " in tablespace " + tableSpace);
    }
    Table table = tableManager.getTable();
    for (net.sf.jsqlparser.schema.Column c : s.getColumns()) {
        Column column = table.getColumn(c.getColumnName());
        if (column == null) {
            throw new StatementExecutionException("no such column " + c.getColumnName() + " in table " + tableName + " in tablespace " + tableSpace);
        }
        if (table.isPrimaryKeyColumn(c.getColumnName())) {
            throw new StatementExecutionException("updates of fields on the PK (" + Arrays.toString(table.primaryKey) + ") are not supported. Please perform a DELETE and than an INSERT");
        }
    }
    List<CompiledSQLExpression> compiledSQLExpressions = new ArrayList<>();
    for (Expression e : s.getExpressions()) {
        compiledSQLExpressions.add(SQLExpressionCompiler.compileExpression(null, e));
    }
    RecordFunction function = new SQLRecordFunction(table, s.getColumns(), compiledSQLExpressions);
    // Perform a scan and then update each row
    SQLRecordPredicate where = s.getWhere() != null ? new SQLRecordPredicate(table, table.name, s.getWhere()) : null;
    if (where != null) {
        Expression expressionWhere = s.getWhere();
        discoverIndexOperations(expressionWhere, table, table.name, where, tableSpaceManager);
    }
    DMLStatement st = new UpdateStatement(tableSpace, tableName, null, function, where).setReturnValues(returnValues);
    return ExecutionPlan.simple(st);
}
Also used : UpdateStatement(herddb.model.commands.UpdateStatement) Table(herddb.model.Table) CreateTable(net.sf.jsqlparser.statement.create.table.CreateTable) ArrayList(java.util.ArrayList) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) StatementExecutionException(herddb.model.StatementExecutionException) AbstractTableManager(herddb.core.AbstractTableManager) Column(herddb.model.Column) Expression(net.sf.jsqlparser.expression.Expression) AlterExpression(net.sf.jsqlparser.statement.alter.AlterExpression) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) SignedExpression(net.sf.jsqlparser.expression.SignedExpression) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) TableSpaceManager(herddb.core.TableSpaceManager) RecordFunction(herddb.model.RecordFunction) AutoIncrementPrimaryKeyRecordFunction(herddb.model.AutoIncrementPrimaryKeyRecordFunction) DMLStatement(herddb.model.DMLStatement)

Example 98 with StatementExecutionException

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

the class SQLPlanner method buildDeleteStatement.

private ExecutionPlan buildDeleteStatement(String defaultTableSpace, Delete s, boolean returnValues) throws StatementExecutionException {
    net.sf.jsqlparser.schema.Table fromTable = s.getTable();
    String tableSpace = fromTable.getSchemaName();
    String tableName = fromTable.getName();
    if (tableSpace == null) {
        tableSpace = defaultTableSpace;
    }
    TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSpace);
    if (tableSpaceManager == null) {
        throw new StatementExecutionException("no such tablespace " + tableSpace + " here at " + manager.getNodeId());
    }
    AbstractTableManager tableManager = tableSpaceManager.getTableManager(tableName);
    if (tableManager == null) {
        throw new StatementExecutionException("no such table " + tableName + " in tablespace " + tableSpace);
    }
    Table table = tableManager.getTable();
    // Perform a scan and then delete each row
    SQLRecordPredicate where = s.getWhere() != null ? new SQLRecordPredicate(table, table.name, s.getWhere()) : null;
    if (where != null) {
        Expression expressionWhere = s.getWhere();
        discoverIndexOperations(expressionWhere, table, table.name, where, tableSpaceManager);
    }
    DMLStatement st = new DeleteStatement(tableSpace, tableName, null, where).setReturnValues(returnValues);
    return ExecutionPlan.simple(st);
}
Also used : Table(herddb.model.Table) CreateTable(net.sf.jsqlparser.statement.create.table.CreateTable) AbstractTableManager(herddb.core.AbstractTableManager) Expression(net.sf.jsqlparser.expression.Expression) AlterExpression(net.sf.jsqlparser.statement.alter.AlterExpression) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) SignedExpression(net.sf.jsqlparser.expression.SignedExpression) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) TableSpaceManager(herddb.core.TableSpaceManager) DMLStatement(herddb.model.DMLStatement) DeleteStatement(herddb.model.commands.DeleteStatement) StatementExecutionException(herddb.model.StatementExecutionException)

Example 99 with StatementExecutionException

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

the class SQLProjection method addExpressionsForFunctionArguments.

private static void addExpressionsForFunctionArguments(ColumnReferencesDiscovery discovery, List<OutputColumn> output, Table table) throws StatementExecutionException {
    List<net.sf.jsqlparser.schema.Column> columns = discovery.getColumnsByTable().get(table.name);
    if (columns != null) {
        for (Expression e : columns) {
            net.sf.jsqlparser.schema.Column c = (net.sf.jsqlparser.schema.Column) e;
            String columnName = c.getColumnName();
            boolean found = false;
            for (OutputColumn outputColumn : output) {
                if (columnName.equalsIgnoreCase(outputColumn.column.name)) {
                    found = true;
                    break;
                } else if (outputColumn.directColumnReference != null && outputColumn.directColumnReference.getColumnName().equalsIgnoreCase(columnName)) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                Column column = table.getColumn(c.getColumnName());
                if (column == null) {
                    throw new StatementExecutionException("invalid column name " + c.getColumnName());
                }
                output.add(new OutputColumn(null, Column.column(columnName, column.type), c, null));
            }
        }
    }
}
Also used : Column(herddb.model.Column) CaseExpression(net.sf.jsqlparser.expression.CaseExpression) Expression(net.sf.jsqlparser.expression.Expression) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) TimeKeyExpression(net.sf.jsqlparser.expression.TimeKeyExpression) StatementExecutionException(herddb.model.StatementExecutionException)

Example 100 with StatementExecutionException

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

the class AlterTableSQLTest method modifyColumn.

@Test
public void modifyColumn() 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());
        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(2, table.getColumn("s1").serialPosition);
        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()).consume();
            assertEquals(1, tuples.size());
            assertEquals(3, tuples.get(0).getFieldNames().length);
        }
        execute(manager, "ALTER TABLE tblspace1.tsql modify column k1 int", Collections.emptyList());
        execute(manager, "INSERT INTO tblspace1.tsql (k1,n1,s1) values(2, 2,'b')", Collections.emptyList());
        {
            List<DataAccessor> tuples = scan(manager, "SELECT * FROM tblspace1.tsql", Collections.emptyList()).consume();
            assertEquals(2, tuples.size());
            assertEquals(3, tuples.get(0).getFieldNames().length);
        }
        {
            List<DataAccessor> tuples = scan(manager, "SELECT * FROM tblspace1.tsql where k1=2", Collections.emptyList()).consume();
            assertEquals(1, tuples.size());
            assertEquals(3, tuples.get(0).getFieldNames().length);
        }
        assertFalse(manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable().auto_increment);
        execute(manager, "ALTER TABLE tblspace1.tsql modify column k1 int auto_increment", Collections.emptyList());
        assertTrue(manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable().auto_increment);
        execute(manager, "INSERT INTO tblspace1.tsql (n1,s1) values(1,'b')", Collections.emptyList());
        {
            List<DataAccessor> tuples = scan(manager, "SELECT * FROM tblspace1.tsql", Collections.emptyList()).consume();
            assertEquals(3, tuples.size());
            assertEquals(3, tuples.get(0).getFieldNames().length);
        }
        try {
            execute(manager, "ALTER TABLE tblspace1.tsql modify column k1 string", Collections.emptyList());
            fail();
        } catch (StatementExecutionException error) {
            assertTrue(error.getMessage().contains("cannot change datatype"));
        }
        try {
            execute(manager, "ALTER TABLE tblspace1.tsql modify column badcol string", Collections.emptyList());
            fail();
        } catch (StatementExecutionException error) {
            assertTrue(error.getMessage().contains("bad column badcol in table tsql"));
        }
        execute(manager, "ALTER TABLE tblspace1.tsql MODIFY COLUMN k1 int RENAME TO l2", Collections.emptyList());
        assertFalse(manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable().auto_increment);
        {
            List<DataAccessor> tuples = scan(manager, "SELECT * FROM tblspace1.tsql " + "where l2=1", Collections.emptyList()).consume();
            assertEquals(1, tuples.size());
            assertEquals(Arrays.toString(tuples.get(0).getFieldNames()), 3, tuples.get(0).getFieldNames().length);
            assertEquals("l2", tuples.get(0).getFieldNames()[0]);
            assertEquals("n1", tuples.get(0).getFieldNames()[1]);
            assertEquals("s1", tuples.get(0).getFieldNames()[2]);
        }
    }
}
Also used : 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)

Aggregations

StatementExecutionException (herddb.model.StatementExecutionException)163 Table (herddb.model.Table)69 ArrayList (java.util.ArrayList)57 DataScanner (herddb.model.DataScanner)49 TransactionContext (herddb.model.TransactionContext)47 DataStorageManagerException (herddb.storage.DataStorageManagerException)40 List (java.util.List)40 DataScannerException (herddb.model.DataScannerException)39 StatementExecutionResult (herddb.model.StatementExecutionResult)39 Column (herddb.model.Column)37 DataAccessor (herddb.utils.DataAccessor)36 LogNotAvailableException (herddb.log.LogNotAvailableException)35 LogEntry (herddb.log.LogEntry)34 Test (org.junit.Test)34 InsertStatement (herddb.model.commands.InsertStatement)32 Bytes (herddb.utils.Bytes)32 CommitLogResult (herddb.log.CommitLogResult)31 Record (herddb.model.Record)31 DMLStatementExecutionResult (herddb.model.DMLStatementExecutionResult)30 Map (java.util.Map)30