Search in sources :

Example 51 with StatementExecutionException

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

the class JSQLParserPlanner method buildSelectStatement.

private ExecutionPlan buildSelectStatement(String defaultTableSpace, int maxRows, Select select, boolean forceScan) throws StatementExecutionException {
    checkSupported(select.getWithItemsList() == null);
    SelectBody selectBody = select.getSelectBody();
    PlannerOp op = buildSelectBody(defaultTableSpace, maxRows, selectBody, forceScan).optimize();
    // Simplify Scan to Get
    if (!forceScan && op instanceof BindableTableScanOp) {
        ScanStatement scanStatement = op.unwrap(ScanStatement.class);
        if (scanStatement != null && scanStatement.getPredicate() != null) {
            Table tableDef = scanStatement.getTableDef();
            CompiledSQLExpression where = scanStatement.getPredicate().unwrap(CompiledSQLExpression.class);
            SQLRecordKeyFunction keyFunction = IndexUtils.findIndexAccess(where, tableDef.getPrimaryKey(), tableDef, "=", tableDef);
            if (keyFunction == null || !keyFunction.isFullPrimaryKey()) {
                throw new StatementExecutionException("unsupported GET not on PK (" + keyFunction + ")");
            }
            GetStatement get = new GetStatement(scanStatement.getTableSpace(), scanStatement.getTable(), keyFunction, scanStatement.getPredicate(), true);
            return ExecutionPlan.simple(get);
        }
    }
    return ExecutionPlan.simple(new SQLPlannedOperationStatement(op), op);
}
Also used : PlannerOp(herddb.model.planner.PlannerOp) Table(herddb.model.Table) ShowCreateTableCalculator.calculateShowCreateTable(herddb.sql.functions.ShowCreateTableCalculator.calculateShowCreateTable) CreateTable(net.sf.jsqlparser.statement.create.table.CreateTable) GetStatement(herddb.model.commands.GetStatement) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) SelectBody(net.sf.jsqlparser.statement.select.SelectBody) StatementExecutionException(herddb.model.StatementExecutionException) BindableTableScanOp(herddb.model.planner.BindableTableScanOp) ScanStatement(herddb.model.commands.ScanStatement) SQLPlannedOperationStatement(herddb.model.commands.SQLPlannedOperationStatement)

Example 52 with StatementExecutionException

use of herddb.model.StatementExecutionException 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 53 with StatementExecutionException

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

the class JSQLParserPlanner method parseStatement.

private net.sf.jsqlparser.statement.Statement parseStatement(String query) throws StatementExecutionException {
    net.sf.jsqlparser.statement.Statement stmt;
    CCJSqlParser parser = new CCJSqlParser(new StringProvider(query));
    // parser.setErrorRecovery(true);
    try {
        return parser.Statement();
    } catch (ParseException err) {
        throw new StatementExecutionException("unable to parse query " + query, err);
    }
}
Also used : StringProvider(net.sf.jsqlparser.parser.StringProvider) CCJSqlParser(net.sf.jsqlparser.parser.CCJSqlParser) ParseException(net.sf.jsqlparser.parser.ParseException) StatementExecutionException(herddb.model.StatementExecutionException)

Example 54 with StatementExecutionException

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

the class JSQLParserPlanner method parseForeignKeyIndex.

private ForeignKeyDef parseForeignKeyIndex(ForeignKeyIndex fk, Table table, String tableName, String tableSpace) throws StatementExecutionException {
    String indexName = fixMySqlBackTicks(fk.getName().toLowerCase());
    if (indexName.equals("generate_unnamed")) {
        indexName = "fk_" + tableName + "_" + System.nanoTime();
    }
    int onUpdateCascadeAction = parseForeignKeyAction(fk.getOnUpdateReferenceOption());
    int onDeleteCascadeAction = parseForeignKeyAction(fk.getOnDeleteReferenceOption());
    Table parentTableSchema = getTable(table.tablespace, fk.getTable());
    herddb.model.ForeignKeyDef.Builder builder = herddb.model.ForeignKeyDef.builder().name(indexName).parentTableId(parentTableSchema.uuid).onUpdateAction(onUpdateCascadeAction).onDeleteAction(onDeleteCascadeAction);
    for (String columnName : fk.getColumnsNames()) {
        columnName = fixMySqlBackTicks(columnName.toLowerCase());
        Column column = table.getColumn(columnName);
        if (column == null) {
            throw new StatementExecutionException("no such column " + columnName + " on table " + tableName + " in tablespace " + tableSpace);
        }
        builder.column(column.name);
    }
    for (String columnName : fk.getReferencedColumnNames()) {
        columnName = fixMySqlBackTicks(columnName.toLowerCase());
        Column column = parentTableSchema.getColumn(columnName);
        if (column == null) {
            throw new StatementExecutionException("no such column " + columnName + " on table " + parentTableSchema.name + " in tablespace " + parentTableSchema.tablespace);
        }
        builder.parentTableColumn(column.name);
    }
    ForeignKeyDef fkDef = builder.build();
    return fkDef;
}
Also used : Table(herddb.model.Table) ShowCreateTableCalculator.calculateShowCreateTable(herddb.sql.functions.ShowCreateTableCalculator.calculateShowCreateTable) CreateTable(net.sf.jsqlparser.statement.create.table.CreateTable) Column(herddb.model.Column) ForeignKeyDef(herddb.model.ForeignKeyDef) StatementExecutionException(herddb.model.StatementExecutionException)

Example 55 with StatementExecutionException

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

the class JSQLParserPlanner method buildCreateTableStatement.

private Statement buildCreateTableStatement(String defaultTableSpace, CreateTable s) throws StatementExecutionException {
    String tableSpace = fixMySqlBackTicks(s.getTable().getSchemaName());
    String tableName = fixMySqlBackTicks(s.getTable().getName());
    if (tableSpace == null) {
        tableSpace = defaultTableSpace;
    }
    if (s.getColumnDefinitions() == null) {
        throw new StatementExecutionException("A table must have at least 1 column");
    }
    final boolean isNotExsists = s.isIfNotExists();
    try {
        boolean foundPk = false;
        Table.Builder tablebuilder = Table.builder().uuid(UUID.randomUUID().toString()).name(tableName).tablespace(tableSpace);
        Set<String> primaryKey = new HashSet<>();
        Set<String> simpleUniqueFields = new HashSet<>();
        if (s.getIndexes() != null) {
            for (Index index : s.getIndexes()) {
                if (index.getType().equalsIgnoreCase("PRIMARY KEY")) {
                    for (String n : index.getColumnsNames()) {
                        n = fixMySqlBackTicks(n.toLowerCase());
                        tablebuilder.primaryKey(n);
                        primaryKey.add(n);
                        foundPk = true;
                    }
                }
            }
        }
        int position = 0;
        for (ColumnDefinition cf : s.getColumnDefinitions()) {
            String columnName = fixMySqlBackTicks(cf.getColumnName().toLowerCase());
            int type;
            String dataType = cf.getColDataType().getDataType();
            List<String> columnSpecs = decodeColumnSpecs(cf.getColumnSpecs());
            type = sqlDataTypeToColumnType(dataType, cf.getColDataType().getArgumentsStringList(), columnSpecs);
            Bytes defaultValue = decodeDefaultValue(cf, type);
            if (!columnSpecs.isEmpty()) {
                boolean auto_increment = decodeAutoIncrement(columnSpecs);
                if (columnSpecs.contains("PRIMARY")) {
                    foundPk = true;
                    tablebuilder.primaryKey(columnName, auto_increment);
                }
                if (auto_increment && primaryKey.contains(columnName)) {
                    tablebuilder.primaryKey(columnName, auto_increment);
                }
                boolean isUnique = columnSpecs.contains("UNIQUE");
                if (isUnique) {
                    simpleUniqueFields.add(columnName);
                }
            }
            tablebuilder.column(columnName, type, position++, defaultValue);
        }
        if (!foundPk) {
            tablebuilder.column("_pk", ColumnTypes.LONG, position++, null);
            tablebuilder.primaryKey("_pk", true);
        }
        Table table = tablebuilder.build();
        List<herddb.model.Index> otherIndexes = new ArrayList<>();
        List<herddb.model.ForeignKeyDef> foreignKeys = new ArrayList<>();
        if (s.getIndexes() != null) {
            for (Index index : s.getIndexes()) {
                if (index.getType().equalsIgnoreCase("PRIMARY KEY")) {
                } else if (index.getType().equalsIgnoreCase("INDEX") || index.getType().equalsIgnoreCase("KEY") || index.getType().equalsIgnoreCase("UNIQUE KEY")) {
                    String indexName = fixMySqlBackTicks(index.getName().toLowerCase());
                    String indexType = convertIndexType(null);
                    boolean unique = index.getType().equalsIgnoreCase("UNIQUE KEY");
                    herddb.model.Index.Builder builder = herddb.model.Index.builder().onTable(table).name(indexName).unique(unique).type(indexType).uuid(UUID.randomUUID().toString());
                    for (String columnName : index.getColumnsNames()) {
                        columnName = fixMySqlBackTicks(columnName.toLowerCase());
                        Column column = table.getColumn(columnName);
                        if (column == null) {
                            throw new StatementExecutionException("no such column " + columnName + " on table " + tableName + " in tablespace " + tableSpace);
                        }
                        builder.column(column.name, column.type);
                    }
                    otherIndexes.add(builder.build());
                } else if (index.getType().equals("FOREIGN KEY")) {
                    ForeignKeyIndex fk = (ForeignKeyIndex) index;
                    ForeignKeyDef fkDef = parseForeignKeyIndex(fk, table, tableName, tableSpace);
                    foreignKeys.add(fkDef);
                } else {
                    throw new StatementExecutionException("Unsupported index type " + index.getType());
                }
            }
        }
        for (String col : simpleUniqueFields) {
            herddb.model.Index.Builder builder = herddb.model.Index.builder().onTable(table).name(table.name + "_unique_" + col).unique(true).type(herddb.model.Index.TYPE_BRIN).uuid(UUID.randomUUID().toString()).column(col, table.getColumn(col).type);
            otherIndexes.add(builder.build());
        }
        if (!foreignKeys.isEmpty()) {
            table = table.withForeignKeys(foreignKeys.toArray(new ForeignKeyDef[0]));
        }
        CreateTableStatement statement = new CreateTableStatement(table, otherIndexes, isNotExsists);
        return statement;
    } catch (IllegalArgumentException err) {
        throw new StatementExecutionException("bad table definition: " + err.getMessage(), err);
    }
}
Also used : Table(herddb.model.Table) ShowCreateTableCalculator.calculateShowCreateTable(herddb.sql.functions.ShowCreateTableCalculator.calculateShowCreateTable) CreateTable(net.sf.jsqlparser.statement.create.table.CreateTable) CreateTableStatement(herddb.model.commands.CreateTableStatement) ArrayList(java.util.ArrayList) 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) ColumnDefinition(net.sf.jsqlparser.statement.create.table.ColumnDefinition) Bytes(herddb.utils.Bytes) Column(herddb.model.Column) ForeignKeyDef(herddb.model.ForeignKeyDef) ForeignKeyIndex(net.sf.jsqlparser.statement.create.table.ForeignKeyIndex) HashSet(java.util.HashSet)

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