Search in sources :

Example 81 with Expression

use of net.sf.jsqlparser.expression.Expression 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 82 with Expression

use of net.sf.jsqlparser.expression.Expression 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 83 with Expression

use of net.sf.jsqlparser.expression.Expression in project herddb by diennea.

the class SQLPlanner method findConstraintOnColumn.

private static Expression findConstraintOnColumn(Expression where, String columnName, String tableAlias, Class<? extends BinaryExpression> expressionType) throws StatementExecutionException {
    if (where instanceof AndExpression) {
        AndExpression and = (AndExpression) where;
        Expression keyOnLeft = findConstraintOnColumn(and.getLeftExpression(), columnName, tableAlias, expressionType);
        if (keyOnLeft != null) {
            return keyOnLeft;
        }
        Expression keyOnRight = findConstraintOnColumn(and.getRightExpression(), columnName, tableAlias, expressionType);
        if (keyOnRight != null) {
            return keyOnRight;
        }
    } else if (expressionType.isAssignableFrom(where.getClass())) {
        Expression keyDirect = validateColumnConstaintToExpression(where, columnName, tableAlias, expressionType);
        if (keyDirect != null) {
            return keyDirect;
        }
    }
    return null;
}
Also used : AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) 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)

Example 84 with Expression

use of net.sf.jsqlparser.expression.Expression 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 85 with Expression

use of net.sf.jsqlparser.expression.Expression in project herddb by diennea.

the class CompiledInExpression method create.

public static CompiledInExpression create(InExpression in, String validatedTableAlias) {
    if (in.getLeftItemsList() != null) {
        throw new StatementExecutionException("Unsupported operand " + in.getClass() + " with a non-expression left argument (" + in + ")");
    }
    CompiledSQLExpression left = compileExpression(validatedTableAlias, in.getLeftExpression());
    if (left == null) {
        return null;
    }
    if (in.getRightItemsList() instanceof ExpressionList) {
        List<CompiledSQLExpression> expList = new ArrayList<>();
        ExpressionList exps = (ExpressionList) in.getRightItemsList();
        for (Expression exp : exps.getExpressions()) {
            CompiledSQLExpression newExp = compileExpression(validatedTableAlias, exp);
            if (newExp == null) {
                return null;
            }
            expList.add(newExp);
        }
        return new CompiledInExpression(in.isNot(), left, expList, null);
    }
    if (in.getRightItemsList() instanceof SubSelect) {
        SubSelect ss = (SubSelect) in.getRightItemsList();
        if (!(ss.getSelectBody() instanceof PlainSelect)) {
            throw new StatementExecutionException("unsupported operand " + in.getClass() + " with subquery of type " + ss.getClass() + "(" + ss + ")");
        }
        return new CompiledInExpression(in.isNot(), left, null, ss);
    }
    throw new StatementExecutionException("unsupported operand " + in.getClass() + " with argument of type " + in.getRightItemsList().getClass() + "(" + in + ")");
}
Also used : Expression(net.sf.jsqlparser.expression.Expression) InExpression(net.sf.jsqlparser.expression.operators.relational.InExpression) SQLExpressionCompiler.compileExpression(herddb.sql.expressions.SQLExpressionCompiler.compileExpression) ArrayList(java.util.ArrayList) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) SubSelect(net.sf.jsqlparser.statement.select.SubSelect) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) StatementExecutionException(herddb.model.StatementExecutionException)

Aggregations

Expression (net.sf.jsqlparser.expression.Expression)92 AndExpression (net.sf.jsqlparser.expression.operators.conditional.AndExpression)30 Test (org.junit.Test)30 ArrayList (java.util.ArrayList)26 BinaryExpression (net.sf.jsqlparser.expression.BinaryExpression)25 Column (net.sf.jsqlparser.schema.Column)19 SignedExpression (net.sf.jsqlparser.expression.SignedExpression)18 ExpressionList (net.sf.jsqlparser.expression.operators.relational.ExpressionList)17 Table (net.sf.jsqlparser.schema.Table)14 CompiledSQLExpression (herddb.sql.expressions.CompiledSQLExpression)12 LikeExpression (net.sf.jsqlparser.expression.operators.relational.LikeExpression)12 AlterExpression (net.sf.jsqlparser.statement.alter.AlterExpression)12 NotExpression (net.sf.jsqlparser.expression.NotExpression)11 StatementExecutionException (herddb.model.StatementExecutionException)10 Select (net.sf.jsqlparser.statement.select.Select)10 AnalyticExpression (net.sf.jsqlparser.expression.AnalyticExpression)9 CaseExpression (net.sf.jsqlparser.expression.CaseExpression)9 KeepExpression (net.sf.jsqlparser.expression.KeepExpression)9 OrExpression (net.sf.jsqlparser.expression.operators.conditional.OrExpression)9 PlainSelect (net.sf.jsqlparser.statement.select.PlainSelect)9