Search in sources :

Example 81 with Value

use of org.h2.value.Value 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 82 with Value

use of org.h2.value.Value in project h2database by h2database.

the class Merge method merge.

/**
 * Merge the given row.
 *
 * @param row the row
 */
protected void merge(Row row) {
    ArrayList<Parameter> k = update.getParameters();
    for (int i = 0; i < columns.length; i++) {
        Column col = columns[i];
        Value v = row.getValue(col.getColumnId());
        Parameter p = k.get(i);
        p.setValue(v);
    }
    for (int i = 0; i < keys.length; i++) {
        Column col = keys[i];
        Value v = row.getValue(col.getColumnId());
        if (v == null) {
            throw DbException.get(ErrorCode.COLUMN_CONTAINS_NULL_VALUES_1, col.getSQL());
        }
        Parameter p = k.get(columns.length + i);
        p.setValue(v);
    }
    // try and update
    int count = update.update();
    // if update fails try an insert
    if (count == 0) {
        try {
            targetTable.validateConvertUpdateSequence(session, row);
            boolean done = targetTable.fireBeforeRow(session, null, row);
            if (!done) {
                targetTable.lock(session, true, false);
                targetTable.addRow(session, row);
                session.getGeneratedKeys().confirmRow(row);
                session.log(targetTable, UndoLogRecord.INSERT, row);
                targetTable.fireAfterRow(session, null, row, false);
            }
        } catch (DbException e) {
            if (e.getErrorCode() == ErrorCode.DUPLICATE_KEY_1) {
                // possibly a concurrent merge or insert
                Index index = (Index) e.getSource();
                if (index != null) {
                    // verify the index columns match the key
                    Column[] indexColumns = index.getColumns();
                    boolean indexMatchesKeys = true;
                    if (indexColumns.length <= keys.length) {
                        for (int i = 0; i < indexColumns.length; i++) {
                            if (indexColumns[i] != keys[i]) {
                                indexMatchesKeys = false;
                                break;
                            }
                        }
                    }
                    if (indexMatchesKeys) {
                        throw DbException.get(ErrorCode.CONCURRENT_UPDATE_1, targetTable.getName());
                    }
                }
            }
            throw e;
        }
    } else if (count != 1) {
        throw DbException.get(ErrorCode.DUPLICATE_KEY_1, targetTable.getSQL());
    }
}
Also used : Column(org.h2.table.Column) SequenceValue(org.h2.expression.SequenceValue) Value(org.h2.value.Value) Parameter(org.h2.expression.Parameter) Index(org.h2.index.Index) DbException(org.h2.message.DbException)

Example 83 with Value

use of org.h2.value.Value 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 84 with Value

use of org.h2.value.Value in project h2database by h2database.

the class GeneratedKeys method confirmRow.

/**
 * Saves row with generated keys if any.
 *
 * @param tableRow
 *            table row that was inserted
 */
public void confirmRow(Row tableRow) {
    if (Boolean.FALSE.equals(generatedKeysRequest)) {
        return;
    }
    int size = row.size();
    if (size > 0) {
        if (size == 1) {
            Column column = row.get(0);
            data.add(Collections.singletonMap(column, tableRow.getValue(column.getColumnId())));
            if (!allColumns.contains(column)) {
                allColumns.add(column);
            }
        } else {
            HashMap<Column, Value> map = new HashMap<>();
            for (Column column : row) {
                map.put(column, tableRow.getValue(column.getColumnId()));
                if (!allColumns.contains(column)) {
                    allColumns.add(column);
                }
            }
            data.add(map);
        }
        row.clear();
    }
}
Also used : ExpressionColumn(org.h2.expression.ExpressionColumn) Column(org.h2.table.Column) HashMap(java.util.HashMap) Value(org.h2.value.Value)

Example 85 with Value

use of org.h2.value.Value in project h2database by h2database.

the class Session method getVariable.

/**
 * Get the value of the specified user defined variable. This method always
 * returns a value; it returns ValueNull.INSTANCE if the variable doesn't
 * exist.
 *
 * @param name the variable name
 * @return the value, or NULL
 */
public Value getVariable(String name) {
    initVariables();
    Value v = variables.get(name);
    return v == null ? ValueNull.INSTANCE : v;
}
Also used : Value(org.h2.value.Value)

Aggregations

Value (org.h2.value.Value)291 SQLException (java.sql.SQLException)94 DbException (org.h2.message.DbException)91 ResultSet (java.sql.ResultSet)69 PreparedStatement (java.sql.PreparedStatement)61 Statement (java.sql.Statement)53 Column (org.h2.table.Column)44 ValueString (org.h2.value.ValueString)44 JdbcStatement (org.h2.jdbc.JdbcStatement)42 JdbcPreparedStatement (org.h2.jdbc.JdbcPreparedStatement)36 SimpleResultSet (org.h2.tools.SimpleResultSet)31 StatementBuilder (org.h2.util.StatementBuilder)28 IndexColumn (org.h2.table.IndexColumn)23 Expression (org.h2.expression.Expression)22 IOException (java.io.IOException)21 Row (org.h2.result.Row)19 SearchRow (org.h2.result.SearchRow)18 Connection (java.sql.Connection)17 ValueArray (org.h2.value.ValueArray)17 ValueTimestamp (org.h2.value.ValueTimestamp)17