Search in sources :

Example 6 with AbstractTableManager

use of herddb.core.AbstractTableManager in project herddb by diennea.

the class JSQLParserPlanner method queryConsistencyCheckStatement.

public Statement queryConsistencyCheckStatement(String defaultTablespace, String query, List<Object> parameters) {
    if (query.startsWith(TABLE_CONSISTENCY_COMMAND)) {
        query = query.substring(query.substring(0, 21).length());
        String tableSpace = defaultTablespace;
        String tableName;
        if (query.contains(".")) {
            String[] tokens = query.split("\\.");
            tableSpace = tokens[0].trim().replaceAll("\'", "");
            tableName = tokens[1].trim().replaceAll("\'", "");
        } else {
            tableName = query.trim();
        }
        TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSpace);
        if (tableSpaceManager == null) {
            throw new TableSpaceDoesNotExistException(String.format("Tablespace %s does not exist.", tableSpace));
        }
        AbstractTableManager tableManager = tableSpaceManager.getTableManager(tableName);
        if (tableManager == null || tableManager.getCreatedInTransaction() > 0) {
            throw new TableDoesNotExistException(String.format("Table %s does not exist.", tableName));
        }
        return new TableConsistencyCheckStatement(tableName, tableSpace);
    } else {
        throw new StatementExecutionException(String.format("Incorrect Syntax for tableconsistencycheck"));
    }
}
Also used : TableDoesNotExistException(herddb.model.TableDoesNotExistException) TableSpaceDoesNotExistException(herddb.model.TableSpaceDoesNotExistException) AbstractTableManager(herddb.core.AbstractTableManager) TableSpaceManager(herddb.core.TableSpaceManager) StatementExecutionException(herddb.model.StatementExecutionException) TableConsistencyCheckStatement(herddb.model.commands.TableConsistencyCheckStatement)

Example 7 with AbstractTableManager

use of herddb.core.AbstractTableManager in project herddb by diennea.

the class JSQLParserPlanner method buildUpdateStatement.

private ExecutionPlan buildUpdateStatement(String defaultTableSpace, Update update, boolean returnValues) throws StatementExecutionException {
    net.sf.jsqlparser.schema.Table table = update.getTable();
    // no alias for UPDATE!
    checkSupported(table.getAlias() == null);
    OpSchema tableSchema = getTableSchema(defaultTableSpace, table);
    TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSchema.tableSpace);
    AbstractTableManager tableManager = tableSpaceManager.getTableManager(tableSchema.name);
    Table tableImpl = tableManager.getTable();
    checkSupported(update.getSelect() == null);
    checkSupported(update.getJoins() == null);
    checkSupported(update.getOrderByElements() == null);
    checkSupported(update.getReturningExpressionList() == null);
    checkSupported(update.getStartJoins() == null || update.getStartJoins().isEmpty());
    List<Expression> projects = update.getExpressions();
    List<CompiledSQLExpression> expressions = new ArrayList<>(projects.size());
    int index = 0;
    List<String> updateColumnList = new ArrayList<>(projects.size());
    for (net.sf.jsqlparser.schema.Column column : update.getColumns()) {
        checkSupported(column.getTable() == null);
        String columnName = fixMySqlBackTicks(column.getColumnName().toLowerCase());
        checkSupported(!tableImpl.isPrimaryKeyColumn(columnName));
        updateColumnList.add(columnName);
        CompiledSQLExpression exp = SQLParserExpressionCompiler.compileExpression(projects.get(index), tableSchema);
        expressions.add(exp);
        index++;
    }
    RecordFunction function = new SQLRecordFunction(updateColumnList, tableImpl, expressions);
    Predicate where = null;
    if (update.getWhere() != null) {
        CompiledSQLExpression whereExpression = SQLParserExpressionCompiler.compileExpression(update.getWhere(), tableSchema);
        if (whereExpression != null) {
            SQLRecordPredicate sqlWhere = new SQLRecordPredicate(tableImpl, null, whereExpression);
            IndexUtils.discoverIndexOperations(tableSchema.tableSpace, whereExpression, tableImpl, sqlWhere, update, tableSpaceManager);
            where = sqlWhere;
        }
    }
    PlannerOp op = new SimpleUpdateOp(new UpdateStatement(tableSchema.tableSpace, tableSchema.name, null, function, where).setReturnValues(returnValues));
    return optimizePlan(op);
}
Also used : UpdateStatement(herddb.model.commands.UpdateStatement) Table(herddb.model.Table) ShowCreateTableCalculator.calculateShowCreateTable(herddb.sql.functions.ShowCreateTableCalculator.calculateShowCreateTable) CreateTable(net.sf.jsqlparser.statement.create.table.CreateTable) PlannerOp(herddb.model.planner.PlannerOp) ArrayList(java.util.ArrayList) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) Predicate(herddb.model.Predicate) AbstractTableManager(herddb.core.AbstractTableManager) AccessCurrentRowExpression(herddb.sql.expressions.AccessCurrentRowExpression) Expression(net.sf.jsqlparser.expression.Expression) CompiledMultiAndExpression(herddb.sql.expressions.CompiledMultiAndExpression) JdbcParameterExpression(herddb.sql.expressions.JdbcParameterExpression) AlterExpression(net.sf.jsqlparser.statement.alter.AlterExpression) TypedJdbcParameterExpression(herddb.sql.expressions.TypedJdbcParameterExpression) ConstantExpression(herddb.sql.expressions.ConstantExpression) SignedExpression(net.sf.jsqlparser.expression.SignedExpression) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) CompiledEqualsExpression(herddb.sql.expressions.CompiledEqualsExpression) TableSpaceManager(herddb.core.TableSpaceManager) RecordFunction(herddb.model.RecordFunction) AutoIncrementPrimaryKeyRecordFunction(herddb.model.AutoIncrementPrimaryKeyRecordFunction) OpSchema(herddb.sql.expressions.OpSchema) SimpleUpdateOp(herddb.model.planner.SimpleUpdateOp)

Example 8 with AbstractTableManager

use of herddb.core.AbstractTableManager in project herddb by diennea.

the class JSQLParserPlanner method buildDeleteStatement.

private ExecutionPlan buildDeleteStatement(String defaultTableSpace, Delete delete) throws StatementExecutionException {
    net.sf.jsqlparser.schema.Table table = delete.getTable();
    // no alias for DELETE!
    checkSupported(table.getAlias() == null);
    OpSchema tableSchema = getTableSchema(defaultTableSpace, table);
    TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSchema.tableSpace);
    AbstractTableManager tableManager = tableSpaceManager.getTableManager(tableSchema.name);
    Table tableImpl = tableManager.getTable();
    checkSupported(delete.getLimit() == null);
    checkSupported(delete.getJoins() == null);
    checkSupported(delete.getOrderByElements() == null);
    checkSupported(delete.getTables() == null || delete.getTables().isEmpty());
    Predicate where = null;
    if (delete.getWhere() != null) {
        CompiledSQLExpression whereExpression = SQLParserExpressionCompiler.compileExpression(delete.getWhere(), tableSchema);
        if (whereExpression != null) {
            SQLRecordPredicate sqlWhere = new SQLRecordPredicate(tableImpl, null, whereExpression);
            IndexUtils.discoverIndexOperations(tableSchema.tableSpace, whereExpression, tableImpl, sqlWhere, delete, tableSpaceManager);
            where = sqlWhere;
        }
    }
    PlannerOp op = new SimpleDeleteOp(new DeleteStatement(tableSchema.tableSpace, tableSchema.name, null, where));
    return optimizePlan(op);
}
Also used : Table(herddb.model.Table) ShowCreateTableCalculator.calculateShowCreateTable(herddb.sql.functions.ShowCreateTableCalculator.calculateShowCreateTable) CreateTable(net.sf.jsqlparser.statement.create.table.CreateTable) PlannerOp(herddb.model.planner.PlannerOp) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) DeleteStatement(herddb.model.commands.DeleteStatement) Predicate(herddb.model.Predicate) AbstractTableManager(herddb.core.AbstractTableManager) SimpleDeleteOp(herddb.model.planner.SimpleDeleteOp) TableSpaceManager(herddb.core.TableSpaceManager) OpSchema(herddb.sql.expressions.OpSchema)

Example 9 with AbstractTableManager

use of herddb.core.AbstractTableManager 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 10 with AbstractTableManager

use of herddb.core.AbstractTableManager 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)

Aggregations

AbstractTableManager (herddb.core.AbstractTableManager)24 Table (herddb.model.Table)18 TableSpaceManager (herddb.core.TableSpaceManager)16 StatementExecutionException (herddb.model.StatementExecutionException)12 Column (herddb.model.Column)10 ArrayList (java.util.ArrayList)10 CreateTable (net.sf.jsqlparser.statement.create.table.CreateTable)10 CompiledSQLExpression (herddb.sql.expressions.CompiledSQLExpression)8 AlterExpression (net.sf.jsqlparser.statement.alter.AlterExpression)8 TableDoesNotExistException (herddb.model.TableDoesNotExistException)7 Expression (net.sf.jsqlparser.expression.Expression)7 SignedExpression (net.sf.jsqlparser.expression.SignedExpression)7 ShowCreateTableCalculator.calculateShowCreateTable (herddb.sql.functions.ShowCreateTableCalculator.calculateShowCreateTable)6 ScanStatement (herddb.model.commands.ScanStatement)5 OpSchema (herddb.sql.expressions.OpSchema)5 AutoIncrementPrimaryKeyRecordFunction (herddb.model.AutoIncrementPrimaryKeyRecordFunction)4 DataScanner (herddb.model.DataScanner)4 RecordFunction (herddb.model.RecordFunction)4 InsertStatement (herddb.model.commands.InsertStatement)4 PlannerOp (herddb.model.planner.PlannerOp)4