Search in sources :

Example 51 with Command

use of org.h2.command.Command in project h2database by h2database.

the class ConstraintReferential method setWhere.

private void setWhere(Prepared command, int pos, Row row) {
    for (int i = 0, len = refColumns.length; i < len; i++) {
        int idx = refColumns[i].column.getColumnId();
        Value v = row.getValue(idx);
        ArrayList<Parameter> params = command.getParameters();
        Parameter param = params.get(pos + i);
        param.setValue(v);
    }
}
Also used : Value(org.h2.value.Value) Parameter(org.h2.expression.Parameter)

Example 52 with Command

use of org.h2.command.Command in project h2database by h2database.

the class ConstraintReferential method prepare.

private Prepared prepare(Session session, String sql, ConstraintActionType action) {
    Prepared command = session.prepare(sql);
    if (action != ConstraintActionType.CASCADE) {
        ArrayList<Parameter> params = command.getParameters();
        for (int i = 0, len = columns.length; i < len; i++) {
            Column column = columns[i].column;
            Parameter param = params.get(i);
            Value value;
            if (action == ConstraintActionType.SET_NULL) {
                value = ValueNull.INSTANCE;
            } else {
                Expression expr = column.getDefaultExpression();
                if (expr == null) {
                    throw DbException.get(ErrorCode.NO_DEFAULT_SET_1, column.getName());
                }
                value = expr.getValue(session);
            }
            param.setValue(value);
        }
    }
    return command;
}
Also used : Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn) Expression(org.h2.expression.Expression) Prepared(org.h2.command.Prepared) Value(org.h2.value.Value) Parameter(org.h2.expression.Parameter)

Example 53 with Command

use of org.h2.command.Command in project h2database by h2database.

the class Session method commit.

/**
 * Commit the current transaction. If the statement was not a data
 * definition statement, and if there are temporary tables that should be
 * dropped or truncated at commit, this is done as well.
 *
 * @param ddl if the statement was a data definition statement
 */
public void commit(boolean ddl) {
    checkCommitRollback();
    currentTransactionName = null;
    transactionStart = 0;
    if (transaction != null) {
        // TODO should not rely on locking
        if (!locks.isEmpty()) {
            for (Table t : locks) {
                if (t instanceof MVTable) {
                    ((MVTable) t).commit();
                }
            }
        }
        transaction.commit();
        transaction = null;
    }
    if (containsUncommitted()) {
        // need to commit even if rollback is not possible
        // (create/drop table and so on)
        database.commit(this);
    }
    removeTemporaryLobs(true);
    if (undoLog.size() > 0) {
        // commit the rows when using MVCC
        if (database.isMultiVersion()) {
            ArrayList<Row> rows = New.arrayList();
            synchronized (database) {
                while (undoLog.size() > 0) {
                    UndoLogRecord entry = undoLog.getLast();
                    entry.commit();
                    rows.add(entry.getRow());
                    undoLog.removeLast(false);
                }
                for (Row r : rows) {
                    r.commit();
                }
            }
        }
        undoLog.clear();
    }
    if (!ddl) {
        // do not clean the temp tables if the last command was a
        // create/drop
        cleanTempTables(false);
        if (autoCommitAtTransactionEnd) {
            autoCommit = true;
            autoCommitAtTransactionEnd = false;
        }
    }
    int rows = getDatabase().getSettings().analyzeSample / 10;
    if (tablesToAnalyze != null) {
        for (Table table : tablesToAnalyze) {
            Analyze.analyzeTable(this, table, rows, false);
        }
        // analyze can lock the meta
        database.unlockMeta(this);
    }
    tablesToAnalyze = null;
    endTransaction();
}
Also used : MVTable(org.h2.mvstore.db.MVTable) Table(org.h2.table.Table) MVTable(org.h2.mvstore.db.MVTable) Row(org.h2.result.Row) Constraint(org.h2.constraint.Constraint)

Example 54 with Command

use of org.h2.command.Command in project h2database by h2database.

the class TcpServerThread method cancelStatement.

/**
 * Cancel a running statement.
 *
 * @param targetSessionId the session id
 * @param statementId the statement to cancel
 */
void cancelStatement(String targetSessionId, int statementId) {
    if (Objects.equals(targetSessionId, this.sessionId)) {
        Command cmd = (Command) cache.getObject(statementId, false);
        cmd.cancel();
    }
}
Also used : Command(org.h2.command.Command)

Example 55 with Command

use of org.h2.command.Command in project h2database by h2database.

the class TcpServerThread method run.

@Override
public void run() {
    try {
        transfer.init();
        trace("Connect");
        // and a list of allowed clients
        try {
            Socket socket = transfer.getSocket();
            if (socket == null) {
                // the transfer is already closed, prevent NPE in TcpServer#allow(Socket)
                return;
            }
            if (!server.allow(transfer.getSocket())) {
                throw DbException.get(ErrorCode.REMOTE_CONNECTION_NOT_ALLOWED);
            }
            int minClientVersion = transfer.readInt();
            int maxClientVersion = transfer.readInt();
            if (maxClientVersion < Constants.TCP_PROTOCOL_VERSION_MIN_SUPPORTED) {
                throw DbException.get(ErrorCode.DRIVER_VERSION_ERROR_2, "" + clientVersion, "" + Constants.TCP_PROTOCOL_VERSION_MIN_SUPPORTED);
            } else if (minClientVersion > Constants.TCP_PROTOCOL_VERSION_MAX_SUPPORTED) {
                throw DbException.get(ErrorCode.DRIVER_VERSION_ERROR_2, "" + clientVersion, "" + Constants.TCP_PROTOCOL_VERSION_MAX_SUPPORTED);
            }
            if (maxClientVersion >= Constants.TCP_PROTOCOL_VERSION_MAX_SUPPORTED) {
                clientVersion = Constants.TCP_PROTOCOL_VERSION_MAX_SUPPORTED;
            } else {
                clientVersion = maxClientVersion;
            }
            transfer.setVersion(clientVersion);
            String db = transfer.readString();
            String originalURL = transfer.readString();
            if (db == null && originalURL == null) {
                String targetSessionId = transfer.readString();
                int command = transfer.readInt();
                stop = true;
                if (command == SessionRemote.SESSION_CANCEL_STATEMENT) {
                    // cancel a running statement
                    int statementId = transfer.readInt();
                    server.cancelStatement(targetSessionId, statementId);
                } else if (command == SessionRemote.SESSION_CHECK_KEY) {
                    // check if this is the correct server
                    db = server.checkKeyAndGetDatabaseName(targetSessionId);
                    if (!targetSessionId.equals(db)) {
                        transfer.writeInt(SessionRemote.STATUS_OK);
                    } else {
                        transfer.writeInt(SessionRemote.STATUS_ERROR);
                    }
                }
            }
            String baseDir = server.getBaseDir();
            if (baseDir == null) {
                baseDir = SysProperties.getBaseDir();
            }
            db = server.checkKeyAndGetDatabaseName(db);
            ConnectionInfo ci = new ConnectionInfo(db);
            ci.setOriginalURL(originalURL);
            ci.setUserName(transfer.readString());
            ci.setUserPasswordHash(transfer.readBytes());
            ci.setFilePasswordHash(transfer.readBytes());
            int len = transfer.readInt();
            for (int i = 0; i < len; i++) {
                ci.setProperty(transfer.readString(), transfer.readString());
            }
            // override client's requested properties with server settings
            if (baseDir != null) {
                ci.setBaseDir(baseDir);
            }
            if (server.getIfExists()) {
                ci.setProperty("IFEXISTS", "TRUE");
            }
            transfer.writeInt(SessionRemote.STATUS_OK);
            transfer.writeInt(clientVersion);
            transfer.flush();
            if (clientVersion >= Constants.TCP_PROTOCOL_VERSION_13) {
                if (ci.getFilePasswordHash() != null) {
                    ci.setFileEncryptionKey(transfer.readBytes());
                }
            }
            session = Engine.getInstance().createSession(ci);
            transfer.setSession(session);
            server.addConnection(threadId, originalURL, ci.getUserName());
            trace("Connected");
        } catch (Throwable e) {
            sendError(e);
            stop = true;
        }
        while (!stop) {
            try {
                process();
            } catch (Throwable e) {
                sendError(e);
            }
        }
        trace("Disconnect");
    } catch (Throwable e) {
        server.traceError(e);
    } finally {
        close();
    }
}
Also used : ConnectionInfo(org.h2.engine.ConnectionInfo) Socket(java.net.Socket)

Aggregations

ValueString (org.h2.value.ValueString)37 Expression (org.h2.expression.Expression)20 ValueExpression (org.h2.expression.ValueExpression)19 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)17 AlterTableDropConstraint (org.h2.command.ddl.AlterTableDropConstraint)16 AlterTableRenameConstraint (org.h2.command.ddl.AlterTableRenameConstraint)16 Column (org.h2.table.Column)16 IndexColumn (org.h2.table.IndexColumn)13 AlterTableAlterColumn (org.h2.command.ddl.AlterTableAlterColumn)12 ExpressionColumn (org.h2.expression.ExpressionColumn)12 Table (org.h2.table.Table)12 AlterTableRenameColumn (org.h2.command.ddl.AlterTableRenameColumn)11 Connection (java.sql.Connection)10 CreateLinkedTable (org.h2.command.ddl.CreateLinkedTable)10 CreateSchema (org.h2.command.ddl.CreateSchema)10 CreateTable (org.h2.command.ddl.CreateTable)10 DropSchema (org.h2.command.ddl.DropSchema)10 DropTable (org.h2.command.ddl.DropTable)10 Schema (org.h2.schema.Schema)10 TruncateTable (org.h2.command.ddl.TruncateTable)9