Search in sources :

Example 6 with ExpressionColumn

use of org.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 7 with ExpressionColumn

use of org.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 8 with ExpressionColumn

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

the class Expression method getExpressionColumns.

/**
 * Extracts expression columns from ValueArray
 *
 * @param session the current session
 * @param value the value to extract columns from
 * @return array of expression columns
 */
static Expression[] getExpressionColumns(Session session, ValueArray value) {
    Value[] list = value.getList();
    ExpressionColumn[] expr = new ExpressionColumn[list.length];
    for (int i = 0, len = list.length; i < len; i++) {
        Value v = list[i];
        Column col = new Column("C" + (i + 1), v.getType(), v.getPrecision(), v.getScale(), v.getDisplaySize());
        expr[i] = new ExpressionColumn(session.getDatabase(), col);
    }
    return expr;
}
Also used : Column(org.h2.table.Column) Value(org.h2.value.Value)

Example 9 with ExpressionColumn

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

the class ExpressionList method getExpressionColumns.

@Override
public Expression[] getExpressionColumns(Session session) {
    ExpressionColumn[] expr = new ExpressionColumn[list.length];
    for (int i = 0; i < list.length; i++) {
        Expression e = list[i];
        Column col = new Column("C" + (i + 1), e.getType(), e.getPrecision(), e.getScale(), e.getDisplaySize());
        expr[i] = new ExpressionColumn(session.getDatabase(), col);
    }
    return expr;
}
Also used : Column(org.h2.table.Column)

Example 10 with ExpressionColumn

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

the class Function method getSequence.

private Sequence getSequence(Session session, Value v0, Value v1) {
    String schemaName, sequenceName;
    if (v1 == null) {
        Parser p = new Parser(session);
        String sql = v0.getString();
        Expression expr = p.parseExpression(sql);
        if (expr instanceof ExpressionColumn) {
            ExpressionColumn seq = (ExpressionColumn) expr;
            schemaName = seq.getOriginalTableAliasName();
            if (schemaName == null) {
                schemaName = session.getCurrentSchemaName();
                sequenceName = sql;
            } else {
                sequenceName = seq.getColumnName();
            }
        } else {
            throw DbException.getSyntaxError(sql, 1);
        }
    } else {
        schemaName = v0.getString();
        sequenceName = v1.getString();
    }
    Schema s = database.findSchema(schemaName);
    if (s == null) {
        schemaName = StringUtils.toUpperEnglish(schemaName);
        s = database.getSchema(schemaName);
    }
    Sequence seq = s.findSequence(sequenceName);
    if (seq == null) {
        sequenceName = StringUtils.toUpperEnglish(sequenceName);
        seq = s.getSequence(sequenceName);
    }
    return seq;
}
Also used : Schema(org.h2.schema.Schema) LinkSchema(org.h2.table.LinkSchema) ValueString(org.h2.value.ValueString) Sequence(org.h2.schema.Sequence) ToDateParser(org.h2.util.ToDateParser) Parser(org.h2.command.Parser)

Aggregations

ExpressionColumn (org.h2.expression.ExpressionColumn)21 Column (org.h2.table.Column)18 Expression (org.h2.expression.Expression)17 ValueExpression (org.h2.expression.ValueExpression)9 IndexColumn (org.h2.table.IndexColumn)9 TableFilter (org.h2.table.TableFilter)8 Query (org.h2.command.dml.Query)6 Comparison (org.h2.expression.Comparison)6 ConditionAndOr (org.h2.expression.ConditionAndOr)6 ValueString (org.h2.value.ValueString)6 AlterTableAlterColumn (org.h2.command.ddl.AlterTableAlterColumn)4 Database (org.h2.engine.Database)4 Index (org.h2.index.Index)4 Value (org.h2.value.Value)4 Parser (org.h2.command.Parser)3 LocalResult (org.h2.result.LocalResult)3 Table (org.h2.table.Table)3 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 CacheException (javax.cache.CacheException)2