QMMSessionInfo.java 8.5 KB
Newer Older
J
jurgen 已提交
1
/*
J
jurgen 已提交
2
 * DBeaver - Universal Database Manager
3
 * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org)
J
jurgen 已提交
4
 *
5 6 7
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
J
jurgen 已提交
8
 *
9
 *     http://www.apache.org/licenses/LICENSE-2.0
J
jurgen 已提交
10
 *
11 12 13 14 15
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
J
jurgen 已提交
16
 */
J
jurgen 已提交
17
package org.jkiss.dbeaver.model.qm.meta;
J
jurgen 已提交
18

19
import org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration;
S
serge-rider 已提交
20
import org.jkiss.dbeaver.model.exec.*;
21 22
import org.jkiss.dbeaver.model.sql.SQLDataSource;
import org.jkiss.dbeaver.model.sql.SQLDialect;
J
jurgen 已提交
23 24 25 26 27 28

/**
 * Data source information
 */
public class QMMSessionInfo extends QMMObject {

29 30 31 32 33
    private final String containerId;
    private final String containerName;
    private final String driverId;
    private final DBPConnectionConfiguration connectionConfiguration;
    private final String contextName;
34
    private SQLDialect sqlDialect;
J
jurgen 已提交
35 36 37 38 39
    private boolean transactional;

    private QMMStatementInfo statementStack;
    private QMMStatementExecuteInfo executionStack;
    private QMMTransactionInfo transaction;
S
serge-rider 已提交
40
    //private Throwable stack;
J
jurgen 已提交
41

42
    public QMMSessionInfo(DBCExecutionContext context, boolean transactional)
J
jurgen 已提交
43
    {
J
jurgen 已提交
44
        this.containerId = context.getDataSource().getContainer().getId();
45 46 47 48 49 50 51
        this.containerName = context.getDataSource().getContainer().getName();
        this.driverId = context.getDataSource().getContainer().getDriver().getId();
        this.connectionConfiguration = context.getDataSource().getContainer().getConnectionConfiguration();
        this.contextName = context.getContextName();
        if (context.getDataSource() instanceof SQLDataSource) {
            this.sqlDialect = ((SQLDataSource) context.getDataSource()).getSQLDialect();
        }
J
jurgen 已提交
52
        this.transactional = transactional;
J
jurgen 已提交
53 54 55
        if (transactional) {
            this.transaction = new QMMTransactionInfo(this, null);
        }
S
serge-rider 已提交
56
        //stack = new RuntimeException();
J
jurgen 已提交
57 58 59
    }

    @Override
J
jurgen 已提交
60
    public void close()
J
jurgen 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    {
        if (transaction != null) {
            transaction.rollback(null);
        }
        for (QMMStatementInfo stat = statementStack; stat != null; stat = stat.getPrevious()) {
            if (!stat.isClosed()) {
                DBCStatement statRef = stat.getReference();
                String query = statRef == null ? "?" : statRef.getQueryString();
                log.warn("Statement " + stat.getObjectId() + " (" + query + ") is not closed");
                stat.close();
            }
        }
        super.close();
    }

J
jurgen 已提交
76
    public QMMTransactionInfo changeTransactional(boolean transactional)
J
jurgen 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90
    {
        if (this.transactional == transactional) {
            return null;
        }
        this.transactional = transactional;
        if (this.transaction != null) {
            // Commit current transaction
            this.transaction.commit();
        }
        // start new transaction
        this.transaction = new QMMTransactionInfo(this, this.transaction);
        return this.transaction.getPrevious();
    }

J
jurgen 已提交
91
    public QMMTransactionInfo commit()
J
jurgen 已提交
92 93 94 95 96 97 98 99 100 101 102
    {
        if (this.transactional) {
            if (this.transaction != null) {
                this.transaction.commit();
            }
            this.transaction = new QMMTransactionInfo(this, this.transaction);
            return this.transaction.getPrevious();
        }
        return null;
    }

J
jurgen 已提交
103
    public QMMObject rollback(DBCSavepoint savepoint)
J
jurgen 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    {
        if (this.transactional) {
            if (this.transaction != null) {
                this.transaction.rollback(savepoint);
            }
            if (savepoint == null) {
                this.transaction = new QMMTransactionInfo(this, this.transaction);
                return this.transaction.getPrevious();
            } else {
                if (this.transaction != null) {
                    return this.transaction.getSavepoint(savepoint);
                }
            }
        }
        return null;
    }

J
jurgen 已提交
121
    public QMMStatementInfo openStatement(DBCStatement statement)
J
jurgen 已提交
122
    {
J
jurgen 已提交
123
        return this.statementStack = new QMMStatementInfo(this, statement, this.statementStack);
J
jurgen 已提交
124 125
    }

J
jurgen 已提交
126
    public QMMStatementInfo closeStatement(DBCStatement statement, long rows)
J
jurgen 已提交
127
    {
J
jurgen 已提交
128 129 130 131 132 133
        QMMStatementExecuteInfo execution = getExecution(statement);
        if (execution != null) {
            if (execution.getRowCount() == -1) {
                execution.close(rows, null);
            }
        }
J
jurgen 已提交
134 135 136 137 138 139 140 141 142 143
        for (QMMStatementInfo stat = this.statementStack; stat != null; stat = stat.getPrevious()) {
            if (stat.getReference() == statement) {
                stat.close();
                return stat;
            }
        }
        log.warn("Statement " + statement + " meta info not found");
        return null;
    }

J
jurgen 已提交
144
    public QMMStatementInfo getStatement(DBCStatement statement)
J
jurgen 已提交
145 146 147 148 149 150 151 152 153 154
    {
        for (QMMStatementInfo stat = this.statementStack; stat != null; stat = stat.getPrevious()) {
            if (stat.getReference() == statement) {
                return stat;
            }
        }
        log.warn("Statement " + statement + " meta info not found");
        return null;
    }

J
jurgen 已提交
155
    public QMMStatementExecuteInfo getExecution(DBCStatement statement)
J
jurgen 已提交
156 157 158 159 160 161 162 163 164
    {
        for (QMMStatementExecuteInfo exec = this.executionStack; exec != null; exec = exec.getPrevious()) {
            if (exec.getStatement().getReference() == statement) {
                return exec;
            }
        }
        return null;
    }

J
jurgen 已提交
165
    public QMMStatementExecuteInfo beginExecution(DBCStatement statement)
J
jurgen 已提交
166 167 168
    {
        QMMStatementInfo stat = getStatement(statement);
        if (stat != null) {
S
serge-rider 已提交
169 170
            String queryString = statement instanceof DBCParameterizedStatement ?
                ((DBCParameterizedStatement) statement).getFormattedQuery() : statement.getQueryString();
J
jurgen 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183
            final QMMTransactionSavepointInfo savepoint =
                isTransactional() && getTransaction() != null ?
                    getTransaction().getCurrentSavepoint() : null;
            return this.executionStack = new QMMStatementExecuteInfo(
                stat,
                savepoint,
                queryString,
                this.executionStack);
        } else {
            return null;
        }
    }

J
jurgen 已提交
184
    public QMMStatementExecuteInfo endExecution(DBCStatement statement, long rowCount, Throwable error)
J
jurgen 已提交
185 186 187 188 189 190 191 192
    {
        QMMStatementExecuteInfo exec = getExecution(statement);
        if (exec != null) {
            exec.close(rowCount, error);
        }
        return exec;
    }

J
jurgen 已提交
193
    public QMMStatementExecuteInfo beginFetch(DBCResultSet resultSet)
J
jurgen 已提交
194
    {
195
        QMMStatementExecuteInfo exec = getExecution(resultSet.getSourceStatement());
J
jurgen 已提交
196
        if (exec == null) {
197
            exec = beginExecution(resultSet.getSourceStatement());
J
jurgen 已提交
198
        }
J
jurgen 已提交
199 200 201
        if (exec != null) {
            exec.beginFetch();
        }
J
jurgen 已提交
202 203 204
        return exec;
    }

J
jurgen 已提交
205
    public QMMStatementExecuteInfo endFetch(DBCResultSet resultSet, long rowCount)
J
jurgen 已提交
206
    {
207
        QMMStatementExecuteInfo exec = getExecution(resultSet.getSourceStatement());
J
jurgen 已提交
208 209 210 211 212 213 214 215 216 217 218
        if (exec != null) {
            exec.endFetch(rowCount);
        }
        return exec;
    }

    public String getContainerId()
    {
        return containerId;
    }

219 220
    public String getContainerName() {
        return containerName;
J
jurgen 已提交
221 222
    }

223 224 225 226 227 228 229 230 231 232
    public String getDriverId() {
        return driverId;
    }

    public DBPConnectionConfiguration getConnectionConfiguration() {
        return connectionConfiguration;
    }

    public String getContextName() {
        return contextName;
J
jurgen 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
    }

    public QMMStatementInfo getStatementStack()
    {
        return statementStack;
    }

    public QMMTransactionInfo getTransaction()
    {
        return transaction;
    }

    public boolean isTransactional()
    {
        return transactional;
    }

    @Override
    public String toString()
    {
253
        return "SESSION " + containerName + " [" + contextName + "]";
J
jurgen 已提交
254 255
    }

256 257 258
    public SQLDialect getSQLDialect() {
        return sqlDialect;
    }
J
jurgen 已提交
259
}