Search in sources :

Example 66 with Value

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

the class CommandRemote method close.

@Override
public void close() {
    if (session == null || session.isClosed()) {
        return;
    }
    synchronized (session) {
        session.traceOperation("COMMAND_CLOSE", id);
        for (Transfer transfer : transferList) {
            try {
                transfer.writeInt(SessionRemote.COMMAND_CLOSE).writeInt(id);
            } catch (IOException e) {
                trace.error(e, "close");
            }
        }
    }
    session = null;
    try {
        for (ParameterInterface p : parameters) {
            Value v = p.getParamValue();
            if (v != null) {
                v.remove();
            }
        }
    } catch (DbException e) {
        trace.error(e, "close");
    }
    parameters.clear();
}
Also used : ParameterInterface(org.h2.expression.ParameterInterface) Transfer(org.h2.value.Transfer) Value(org.h2.value.Value) IOException(java.io.IOException) DbException(org.h2.message.DbException)

Example 67 with Value

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

the class Parser method parseCreateConstant.

private CreateConstant parseCreateConstant() {
    boolean ifNotExists = readIfNotExists();
    String constantName = readIdentifierWithSchema();
    Schema schema = getSchema();
    if (isKeyword(constantName)) {
        throw DbException.get(ErrorCode.CONSTANT_ALREADY_EXISTS_1, constantName);
    }
    read("VALUE");
    Expression expr = readExpression();
    CreateConstant command = new CreateConstant(session, schema);
    command.setConstantName(constantName);
    command.setExpression(expr);
    command.setIfNotExists(ifNotExists);
    return command;
}
Also used : Expression(org.h2.expression.Expression) ValueExpression(org.h2.expression.ValueExpression) DropSchema(org.h2.command.ddl.DropSchema) CreateSchema(org.h2.command.ddl.CreateSchema) Schema(org.h2.schema.Schema) ValueString(org.h2.value.ValueString) CreateConstant(org.h2.command.ddl.CreateConstant)

Example 68 with Value

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

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

the class Insert method addRowImpl.

private Row addRowImpl(Value[] values) {
    Row newRow = table.getTemplateRow();
    setCurrentRowNumber(++rowNumber);
    for (int j = 0, len = columns.length; j < len; j++) {
        Column c = columns[j];
        int index = c.getColumnId();
        try {
            Value v = c.convert(values[j], session.getDatabase().getMode());
            newRow.setValue(index, v);
        } catch (DbException ex) {
            throw setRow(ex, rowNumber, getSQL(values));
        }
    }
    table.validateConvertUpdateSequence(session, newRow);
    boolean done = table.fireBeforeRow(session, null, newRow);
    if (!done) {
        table.addRow(session, newRow);
        session.log(table, UndoLogRecord.INSERT, newRow);
        table.fireAfterRow(session, null, newRow, false);
        return newRow;
    }
    return null;
}
Also used : Column(org.h2.table.Column) ExpressionColumn(org.h2.expression.ExpressionColumn) Value(org.h2.value.Value) SequenceValue(org.h2.expression.SequenceValue) Row(org.h2.result.Row) DbException(org.h2.message.DbException)

Example 70 with Value

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

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