Search in sources :

Example 1 with ResultInterface

use of org.gridgain.internal.h2.result.ResultInterface in project h2database by h2database.

the class CommandContainer method query.

@Override
public ResultInterface query(int maxrows) {
    recompileIfRequired();
    setProgress(DatabaseEventListener.STATE_STATEMENT_START);
    start();
    prepared.checkParameters();
    ResultInterface result = prepared.query(maxrows);
    prepared.trace(startTimeNanos, result.isLazy() ? 0 : result.getRowCount());
    setProgress(DatabaseEventListener.STATE_STATEMENT_END);
    return result;
}
Also used : ResultInterface(org.h2.result.ResultInterface)

Example 2 with ResultInterface

use of org.gridgain.internal.h2.result.ResultInterface in project h2database by h2database.

the class Analyze method analyzeTable.

/**
 * Analyze this table.
 *
 * @param session the session
 * @param table the table
 * @param sample the number of sample rows
 * @param manual whether the command was called by the user
 */
public static void analyzeTable(Session session, Table table, int sample, boolean manual) {
    if (table.getTableType() != TableType.TABLE || table.isHidden() || session == null) {
        return;
    }
    if (!manual) {
        if (session.getDatabase().isSysTableLocked()) {
            return;
        }
        if (table.hasSelectTrigger()) {
            return;
        }
    }
    if (table.isTemporary() && !table.isGlobalTemporary() && session.findLocalTempTable(table.getName()) == null) {
        return;
    }
    if (table.isLockedExclusively() && !table.isLockedExclusivelyBy(session)) {
        return;
    }
    if (!session.getUser().hasRight(table, Right.SELECT)) {
        return;
    }
    if (session.getCancel() != 0) {
        // if the connection is closed and there is something to undo
        return;
    }
    Column[] columns = table.getColumns();
    if (columns.length == 0) {
        return;
    }
    Database db = session.getDatabase();
    StatementBuilder buff = new StatementBuilder("SELECT ");
    for (Column col : columns) {
        buff.appendExceptFirst(", ");
        int type = col.getType();
        if (type == Value.BLOB || type == Value.CLOB) {
            // can not index LOB columns, so calculating
            // the selectivity is not required
            buff.append("MAX(NULL)");
        } else {
            buff.append("SELECTIVITY(").append(col.getSQL()).append(')');
        }
    }
    buff.append(" FROM ").append(table.getSQL());
    if (sample > 0) {
        buff.append(" LIMIT ? SAMPLE_SIZE ? ");
    }
    String sql = buff.toString();
    Prepared command = session.prepare(sql);
    if (sample > 0) {
        ArrayList<Parameter> params = command.getParameters();
        params.get(0).setValue(ValueInt.get(1));
        params.get(1).setValue(ValueInt.get(sample));
    }
    ResultInterface result = command.query(0);
    result.next();
    for (int j = 0; j < columns.length; j++) {
        Value v = result.currentRow()[j];
        if (v != ValueNull.INSTANCE) {
            int selectivity = v.getInt();
            columns[j].setSelectivity(selectivity);
        }
    }
    db.updateMeta(session, table);
}
Also used : ResultInterface(org.h2.result.ResultInterface) Column(org.h2.table.Column) StatementBuilder(org.h2.util.StatementBuilder) Database(org.h2.engine.Database) Prepared(org.h2.command.Prepared) Value(org.h2.value.Value) Parameter(org.h2.expression.Parameter)

Example 3 with ResultInterface

use of org.gridgain.internal.h2.result.ResultInterface in project h2database by h2database.

the class Replace method update.

@Override
public int update() {
    int count;
    session.getUser().checkRight(table, Right.INSERT);
    session.getUser().checkRight(table, Right.UPDATE);
    setCurrentRowNumber(0);
    if (!list.isEmpty()) {
        count = 0;
        for (int x = 0, size = list.size(); x < size; x++) {
            setCurrentRowNumber(x + 1);
            Expression[] expr = list.get(x);
            Row newRow = table.getTemplateRow();
            for (int i = 0, len = columns.length; i < len; i++) {
                Column c = columns[i];
                int index = c.getColumnId();
                Expression e = expr[i];
                if (e != null) {
                    // e can be null (DEFAULT)
                    try {
                        Value v = c.convert(e.getValue(session));
                        newRow.setValue(index, v);
                    } catch (DbException ex) {
                        throw setRow(ex, count, getSQL(expr));
                    }
                }
            }
            replace(newRow);
            count++;
        }
    } else {
        ResultInterface rows = query.query(0);
        count = 0;
        table.fire(session, Trigger.UPDATE | Trigger.INSERT, true);
        table.lock(session, true, false);
        while (rows.next()) {
            count++;
            Value[] r = rows.currentRow();
            Row newRow = table.getTemplateRow();
            setCurrentRowNumber(count);
            for (int j = 0; j < columns.length; j++) {
                Column c = columns[j];
                int index = c.getColumnId();
                try {
                    Value v = c.convert(r[j]);
                    newRow.setValue(index, v);
                } catch (DbException ex) {
                    throw setRow(ex, count, getSQL(r));
                }
            }
            replace(newRow);
        }
        rows.close();
        table.fire(session, Trigger.UPDATE | Trigger.INSERT, false);
    }
    return count;
}
Also used : ResultInterface(org.h2.result.ResultInterface) Expression(org.h2.expression.Expression) Column(org.h2.table.Column) Value(org.h2.value.Value) Row(org.h2.result.Row) DbException(org.h2.message.DbException)

Example 4 with ResultInterface

use of org.gridgain.internal.h2.result.ResultInterface in project h2database by h2database.

the class ConstraintReferential method checkExistingData.

@Override
public void checkExistingData(Session session) {
    if (session.getDatabase().isStarting()) {
        // don't check at startup
        return;
    }
    session.startStatementWithinTransaction();
    StatementBuilder buff = new StatementBuilder("SELECT 1 FROM (SELECT ");
    for (IndexColumn c : columns) {
        buff.appendExceptFirst(", ");
        buff.append(c.getSQL());
    }
    buff.append(" FROM ").append(table.getSQL()).append(" WHERE ");
    buff.resetCount();
    for (IndexColumn c : columns) {
        buff.appendExceptFirst(" AND ");
        buff.append(c.getSQL()).append(" IS NOT NULL ");
    }
    buff.append(" ORDER BY ");
    buff.resetCount();
    for (IndexColumn c : columns) {
        buff.appendExceptFirst(", ");
        buff.append(c.getSQL());
    }
    buff.append(") C WHERE NOT EXISTS(SELECT 1 FROM ").append(refTable.getSQL()).append(" P WHERE ");
    buff.resetCount();
    int i = 0;
    for (IndexColumn c : columns) {
        buff.appendExceptFirst(" AND ");
        buff.append("C.").append(c.getSQL()).append('=').append("P.").append(refColumns[i++].getSQL());
    }
    buff.append(')');
    String sql = buff.toString();
    ResultInterface r = session.prepare(sql).query(1);
    if (r.next()) {
        throw DbException.get(ErrorCode.REFERENTIAL_INTEGRITY_VIOLATED_PARENT_MISSING_1, getShortDescription(null, null));
    }
}
Also used : ResultInterface(org.h2.result.ResultInterface) StatementBuilder(org.h2.util.StatementBuilder) IndexColumn(org.h2.table.IndexColumn)

Example 5 with ResultInterface

use of org.gridgain.internal.h2.result.ResultInterface in project h2database by h2database.

the class ConstraintCheck method checkExistingData.

@Override
public void checkExistingData(Session session) {
    if (session.getDatabase().isStarting()) {
        // don't check at startup
        return;
    }
    String sql = "SELECT 1 FROM " + filter.getTable().getSQL() + " WHERE NOT(" + expr.getSQL() + ")";
    ResultInterface r = session.prepare(sql).query(1);
    if (r.next()) {
        throw DbException.get(ErrorCode.CHECK_CONSTRAINT_VIOLATED_1, getName());
    }
}
Also used : ResultInterface(org.h2.result.ResultInterface)

Aggregations

ResultInterface (org.h2.result.ResultInterface)164 SQLException (java.sql.SQLException)46 DbException (org.h2.message.DbException)43 ResultInterface (org.gridgain.internal.h2.result.ResultInterface)41 Value (org.h2.value.Value)38 CommandInterface (org.h2.command.CommandInterface)36 Value (org.gridgain.internal.h2.value.Value)20 ValueBigint (org.h2.value.ValueBigint)15 ValueSmallint (org.h2.value.ValueSmallint)15 SQLClientInfoException (java.sql.SQLClientInfoException)14 IOException (java.io.IOException)12 ResultWithGeneratedKeys (org.h2.result.ResultWithGeneratedKeys)12 Column (org.h2.table.Column)12 ValueTinyint (org.h2.value.ValueTinyint)12 DbException (org.gridgain.internal.h2.message.DbException)11 LocalResult (org.gridgain.internal.h2.result.LocalResult)11 Expression (org.h2.expression.Expression)11 ArrayList (java.util.ArrayList)10 Row (org.h2.result.Row)9 Database (org.h2.engine.Database)8