Search in sources :

Example 1 with ExpressionColumn

use of org.gridgain.internal.h2.expression.ExpressionColumn in project h2database by h2database.

the class Parser method readJoin.

private TableFilter readJoin(TableFilter top) {
    TableFilter last = top;
    while (true) {
        TableFilter join;
        if (readIf("RIGHT")) {
            readIf("OUTER");
            read("JOIN");
            // the right hand side is the 'inner' table usually
            join = readTableFilter();
            join = readJoin(join);
            Expression on = null;
            if (readIf("ON")) {
                on = readExpression();
            }
            addJoin(join, top, true, on);
            top = join;
        } else if (readIf("LEFT")) {
            readIf("OUTER");
            read("JOIN");
            join = readTableFilter();
            join = readJoin(join);
            Expression on = null;
            if (readIf("ON")) {
                on = readExpression();
            }
            addJoin(top, join, true, on);
        } else if (readIf("FULL")) {
            throw getSyntaxError();
        } else if (readIf("INNER")) {
            read("JOIN");
            join = readTableFilter();
            top = readJoin(top);
            Expression on = null;
            if (readIf("ON")) {
                on = readExpression();
            }
            addJoin(top, join, false, on);
        } else if (readIf("JOIN")) {
            join = readTableFilter();
            top = readJoin(top);
            Expression on = null;
            if (readIf("ON")) {
                on = readExpression();
            }
            addJoin(top, join, false, on);
        } else if (readIf("CROSS")) {
            read("JOIN");
            join = readTableFilter();
            addJoin(top, join, false, null);
        } else if (readIf("NATURAL")) {
            read("JOIN");
            join = readTableFilter();
            Column[] tableCols = last.getTable().getColumns();
            Column[] joinCols = join.getTable().getColumns();
            String tableSchema = last.getTable().getSchema().getName();
            String joinSchema = join.getTable().getSchema().getName();
            Expression on = null;
            for (Column tc : tableCols) {
                String tableColumnName = tc.getName();
                for (Column c : joinCols) {
                    String joinColumnName = c.getName();
                    if (equalsToken(tableColumnName, joinColumnName)) {
                        join.addNaturalJoinColumn(c);
                        Expression tableExpr = new ExpressionColumn(database, tableSchema, last.getTableAlias(), tableColumnName);
                        Expression joinExpr = new ExpressionColumn(database, joinSchema, join.getTableAlias(), joinColumnName);
                        Expression equal = new Comparison(session, Comparison.EQUAL, tableExpr, joinExpr);
                        if (on == null) {
                            on = equal;
                        } else {
                            on = new ConditionAndOr(ConditionAndOr.AND, on, equal);
                        }
                    }
                }
            }
            addJoin(top, join, false, on);
        } else {
            break;
        }
        last = join;
    }
    return top;
}
Also used : TableFilter(org.h2.table.TableFilter) Expression(org.h2.expression.Expression) ValueExpression(org.h2.expression.ValueExpression) AlterTableRenameColumn(org.h2.command.ddl.AlterTableRenameColumn) AlterTableAlterColumn(org.h2.command.ddl.AlterTableAlterColumn) Column(org.h2.table.Column) ExpressionColumn(org.h2.expression.ExpressionColumn) IndexColumn(org.h2.table.IndexColumn) Comparison(org.h2.expression.Comparison) ValueString(org.h2.value.ValueString) ConditionAndOr(org.h2.expression.ConditionAndOr) ExpressionColumn(org.h2.expression.ExpressionColumn)

Example 2 with ExpressionColumn

use of org.gridgain.internal.h2.expression.ExpressionColumn in project h2database by h2database.

the class Insert method prepareUpdateCondition.

private Expression prepareUpdateCondition(Index foundIndex) {
    // MVPrimaryIndex is playing fast and loose with it's implementation of
    // the Index interface.
    // It returns all of the columns in the table when we call
    // getIndexColumns() or getColumns().
    // Don't have time right now to fix that, so just special-case it.
    final Column[] indexedColumns;
    if (foundIndex instanceof MVPrimaryIndex) {
        MVPrimaryIndex foundMV = (MVPrimaryIndex) foundIndex;
        indexedColumns = new Column[] { foundMV.getIndexColumns()[foundMV.getMainIndexColumn()].column };
    } else {
        indexedColumns = foundIndex.getColumns();
    }
    Expression[] row = list.get(getCurrentRowNumber() - 1);
    Expression condition = null;
    for (Column column : indexedColumns) {
        ExpressionColumn expr = new ExpressionColumn(session.getDatabase(), table.getSchema().getName(), table.getName(), column.getName());
        for (int i = 0; i < columns.length; i++) {
            if (expr.getColumnName().equals(columns[i].getName())) {
                if (condition == null) {
                    condition = new Comparison(session, Comparison.EQUAL, expr, row[i]);
                } else {
                    condition = new ConditionAndOr(ConditionAndOr.AND, condition, new Comparison(session, Comparison.EQUAL, expr, row[i]));
                }
                break;
            }
        }
    }
    return condition;
}
Also used : Column(org.h2.table.Column) ExpressionColumn(org.h2.expression.ExpressionColumn) Expression(org.h2.expression.Expression) Comparison(org.h2.expression.Comparison) MVPrimaryIndex(org.h2.mvstore.db.MVPrimaryIndex) ConditionAndOr(org.h2.expression.ConditionAndOr) ExpressionColumn(org.h2.expression.ExpressionColumn)

Example 3 with ExpressionColumn

use of org.gridgain.internal.h2.expression.ExpressionColumn in project h2database by h2database.

the class Explain method query.

@Override
public ResultInterface query(int maxrows) {
    Column column = new Column("PLAN", Value.STRING);
    Database db = session.getDatabase();
    ExpressionColumn expr = new ExpressionColumn(db, column);
    Expression[] expressions = { expr };
    result = new LocalResult(session, expressions, 1);
    if (maxrows >= 0) {
        String plan;
        if (executeCommand) {
            PageStore store = null;
            Store mvStore = null;
            if (db.isPersistent()) {
                store = db.getPageStore();
                if (store != null) {
                    store.statisticsStart();
                }
                mvStore = db.getMvStore();
                if (mvStore != null) {
                    mvStore.statisticsStart();
                }
            }
            if (command.isQuery()) {
                command.query(maxrows);
            } else {
                command.update();
            }
            plan = command.getPlanSQL();
            Map<String, Integer> statistics = null;
            if (store != null) {
                statistics = store.statisticsEnd();
            } else if (mvStore != null) {
                statistics = mvStore.statisticsEnd();
            }
            if (statistics != null) {
                int total = 0;
                for (Entry<String, Integer> e : statistics.entrySet()) {
                    total += e.getValue();
                }
                if (total > 0) {
                    statistics = new TreeMap<>(statistics);
                    StringBuilder buff = new StringBuilder();
                    if (statistics.size() > 1) {
                        buff.append("total: ").append(total).append('\n');
                    }
                    for (Entry<String, Integer> e : statistics.entrySet()) {
                        int value = e.getValue();
                        int percent = (int) (100L * value / total);
                        buff.append(e.getKey()).append(": ").append(value);
                        if (statistics.size() > 1) {
                            buff.append(" (").append(percent).append("%)");
                        }
                        buff.append('\n');
                    }
                    plan += "\n/*\n" + buff.toString() + "*/";
                }
            }
        } else {
            plan = command.getPlanSQL();
        }
        add(plan);
    }
    result.done();
    return result;
}
Also used : Store(org.h2.mvstore.db.MVTableEngine.Store) PageStore(org.h2.store.PageStore) PageStore(org.h2.store.PageStore) ValueString(org.h2.value.ValueString) ExpressionColumn(org.h2.expression.ExpressionColumn) LocalResult(org.h2.result.LocalResult) ExpressionColumn(org.h2.expression.ExpressionColumn) Column(org.h2.table.Column) Expression(org.h2.expression.Expression) Database(org.h2.engine.Database)

Example 4 with ExpressionColumn

use of org.gridgain.internal.h2.expression.ExpressionColumn in project h2database by h2database.

the class GeneratedKeys method getKeys.

/**
 * Returns generated keys.
 *
 * @param session
 *            session
 * @return local result with generated keys
 */
public LocalResult getKeys(Session session) {
    Database db = session == null ? null : session.getDatabase();
    if (Boolean.FALSE.equals(generatedKeysRequest)) {
        clear(null);
        return new LocalResult();
    }
    ArrayList<ExpressionColumn> expressionColumns;
    if (Boolean.TRUE.equals(generatedKeysRequest)) {
        expressionColumns = new ArrayList<>(allColumns.size());
        for (Column column : allColumns) {
            expressionColumns.add(new ExpressionColumn(db, column));
        }
    } else if (generatedKeysRequest instanceof int[]) {
        if (table != null) {
            int[] indices = (int[]) generatedKeysRequest;
            Column[] columns = table.getColumns();
            int cnt = columns.length;
            allColumns.clear();
            expressionColumns = new ArrayList<>(indices.length);
            for (int idx : indices) {
                if (idx >= 1 && idx <= cnt) {
                    Column column = columns[idx - 1];
                    expressionColumns.add(new ExpressionColumn(db, column));
                    allColumns.add(column);
                }
            }
        } else {
            clear(null);
            return new LocalResult();
        }
    } else if (generatedKeysRequest instanceof String[]) {
        if (table != null) {
            String[] names = (String[]) generatedKeysRequest;
            allColumns.clear();
            expressionColumns = new ArrayList<>(names.length);
            for (String name : names) {
                Column column;
                search: if (table.doesColumnExist(name)) {
                    column = table.getColumn(name);
                } else {
                    name = StringUtils.toUpperEnglish(name);
                    if (table.doesColumnExist(name)) {
                        column = table.getColumn(name);
                    } else {
                        for (Column c : table.getColumns()) {
                            if (c.getName().equalsIgnoreCase(name)) {
                                column = c;
                                break search;
                            }
                        }
                        continue;
                    }
                }
                expressionColumns.add(new ExpressionColumn(db, column));
                allColumns.add(column);
            }
        } else {
            clear(null);
            return new LocalResult();
        }
    } else {
        clear(null);
        return new LocalResult();
    }
    int columnCount = expressionColumns.size();
    if (columnCount == 0) {
        clear(null);
        return new LocalResult();
    }
    LocalResult result = new LocalResult(session, expressionColumns.toArray(new Expression[0]), columnCount);
    for (Map<Column, Value> map : data) {
        Value[] row = new Value[columnCount];
        for (Map.Entry<Column, Value> entry : map.entrySet()) {
            int idx = allColumns.indexOf(entry.getKey());
            if (idx >= 0) {
                row[idx] = entry.getValue();
            }
        }
        for (int i = 0; i < columnCount; i++) {
            if (row[i] == null) {
                row[i] = ValueNull.INSTANCE;
            }
        }
        result.addRow(row);
    }
    clear(null);
    return result;
}
Also used : ArrayList(java.util.ArrayList) ExpressionColumn(org.h2.expression.ExpressionColumn) LocalResult(org.h2.result.LocalResult) ExpressionColumn(org.h2.expression.ExpressionColumn) Column(org.h2.table.Column) Expression(org.h2.expression.Expression) Value(org.h2.value.Value) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with ExpressionColumn

use of org.gridgain.internal.h2.expression.ExpressionColumn in project h2database by h2database.

the class FullText method addColumnData.

private static void addColumnData(ArrayList<String> columns, ArrayList<String> data, Expression expr) {
    if (expr instanceof ConditionAndOr) {
        ConditionAndOr and = (ConditionAndOr) expr;
        Expression left = and.getExpression(true);
        Expression right = and.getExpression(false);
        addColumnData(columns, data, left);
        addColumnData(columns, data, right);
    } else {
        Comparison comp = (Comparison) expr;
        ExpressionColumn ec = (ExpressionColumn) comp.getExpression(true);
        ValueExpression ev = (ValueExpression) comp.getExpression(false);
        String columnName = ec.getColumnName();
        columns.add(columnName);
        if (ev == null) {
            data.add(null);
        } else {
            data.add(ev.getValue(null).getString());
        }
    }
}
Also used : ValueExpression(org.h2.expression.ValueExpression) Expression(org.h2.expression.Expression) Comparison(org.h2.expression.Comparison) ValueExpression(org.h2.expression.ValueExpression) ConditionAndOr(org.h2.expression.ConditionAndOr) ExpressionColumn(org.h2.expression.ExpressionColumn)

Aggregations

ExpressionColumn (org.h2.expression.ExpressionColumn)86 Expression (org.h2.expression.Expression)59 Column (org.h2.table.Column)43 ExpressionColumn (org.gridgain.internal.h2.expression.ExpressionColumn)35 ValueExpression (org.h2.expression.ValueExpression)30 Expression (org.gridgain.internal.h2.expression.Expression)23 Database (org.h2.engine.Database)22 Column (org.gridgain.internal.h2.table.Column)20 TableFilter (org.h2.table.TableFilter)20 TypedValueExpression (org.h2.expression.TypedValueExpression)16 IndexColumn (org.h2.table.IndexColumn)16 ValueExpression (org.gridgain.internal.h2.expression.ValueExpression)15 TypeInfo (org.h2.value.TypeInfo)15 ArrayList (java.util.ArrayList)14 IndexColumn (org.gridgain.internal.h2.table.IndexColumn)10 TableFilter (org.gridgain.internal.h2.table.TableFilter)10 Alias (org.h2.expression.Alias)10 Value (org.h2.value.Value)10 ValueString (org.gridgain.internal.h2.value.ValueString)9 Index (org.h2.index.Index)9