~ubuntu-branches/ubuntu/wily/tora/wily-proposed

« back to all changes in this revision

Viewing changes to src/toeventquerytask.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Meskes
  • Date: 2009-04-07 13:16:05 UTC
  • mfrom: (1.2.7 upstream) (3.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090407131605-u422yigfv7jgg0l0
Tags: 2.0.0-3
* Cleaned up packaging a little bit.
* Added homepage information to control file.
* Bumped Standards-Version to 3.8.1.
* Released to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/* BEGIN_COMMON_COPYRIGHT_HEADER
 
3
 *
 
4
 * TOra - An Oracle Toolkit for DBA's and developers
 
5
 * 
 
6
 * Shared/mixed copyright is held throughout files in this product
 
7
 * 
 
8
 * Portions Copyright (C) 2000-2001 Underscore AB
 
9
 * Portions Copyright (C) 2003-2005 Quest Software, Inc.
 
10
 * Portions Copyright (C) 2004-2008 Numerous Other Contributors
 
11
 * 
 
12
 * This program is free software; you can redistribute it and/or
 
13
 * modify it under the terms of the GNU General Public License
 
14
 * as published by the Free Software Foundation;  only version 2 of
 
15
 * the License is valid for this program.
 
16
 * 
 
17
 * This program is distributed in the hope that it will be useful,
 
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
20
 * GNU General Public License for more details.
 
21
 * 
 
22
 * You should have received a copy of the GNU General Public License
 
23
 * along with this program; if not, write to the Free Software
 
24
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
25
 * 
 
26
 *      As a special exception, you have permission to link this program
 
27
 *      with the Oracle Client libraries and distribute executables, as long
 
28
 *      as you follow the requirements of the GNU GPL in regard to all of the
 
29
 *      software in the executable aside from Oracle client libraries.
 
30
 * 
 
31
 *      Specifically you are not permitted to link this program with the
 
32
 *      Qt/UNIX, Qt/Windows or Qt Non Commercial products of TrollTech.
 
33
 *      And you are not permitted to distribute binaries compiled against
 
34
 *      these libraries. 
 
35
 * 
 
36
 *      You may link this product with any GPL'd Qt library.
 
37
 * 
 
38
 * All trademarks belong to their respective owners.
 
39
 *
 
40
 * END_COMMON_COPYRIGHT_HEADER */
 
41
 
 
42
#include "utils.h"
 
43
 
 
44
#include "toconf.h"
 
45
#include "toeventquerytask.h"
 
46
#include "toresultstats.h"
 
47
#include "totool.h"
 
48
#include "toresultstats.h"
 
49
#include "toconfiguration.h"
 
50
 
 
51
#include <QApplication>
 
52
#include <QMutexLocker>
 
53
#include <QTimer>
 
54
 
 
55
 
 
56
static const int FIREWALL_TIMEOUT = 240000;
 
57
 
 
58
 
 
59
#define CATCH_ALL                                   \
 
60
    catch(const toConnection::exception &str) {     \
 
61
        if(!Closed) {                               \
 
62
            emit error(str);                        \
 
63
            close();                                \
 
64
        }                                           \
 
65
    }                                               \
 
66
    catch(const QString &str) {                     \
 
67
        if(!Closed) {                               \
 
68
            emit error(str);                        \
 
69
            close();                                \
 
70
        }                                           \
 
71
    }                                               \
 
72
    catch(...) {                                    \
 
73
        if(!Closed) {                               \
 
74
            emit error(tr("Unknown exception"));    \
 
75
            close();                                \
 
76
        }                                           \
 
77
    }
 
78
 
 
79
 
 
80
toEventQueryTask::toEventQueryTask(QObject *parent,
 
81
                                   toConnection &conn,
 
82
                                   const QString &sql,
 
83
                                   const toQList &param,
 
84
                                   toResultStats *stats)
 
85
    : toRunnable(),
 
86
      SQL(sql),
 
87
      Params(param),
 
88
      Statistics(stats) {
 
89
    Query      = 0;
 
90
    Columns    = 0;
 
91
    Connection = &conn;
 
92
    Closed     = false;
 
93
    setObjectName("toEventQueryTask");
 
94
}
 
95
 
 
96
 
 
97
void toEventQueryTask::run(void) {
 
98
    try {
 
99
        Query = new toQuery(*Connection);
 
100
        Query->execute(SQL, Params);
 
101
 
 
102
        connect(this,
 
103
                SIGNAL(readRequested(bool)),
 
104
                this,
 
105
                SLOT(pread(bool)),
 
106
                Qt::QueuedConnection);
 
107
 
 
108
        toQDescList desc = Query->describe();
 
109
        Columns = Query->columns();
 
110
        emit headers(desc, Columns);
 
111
 
 
112
        if(Query->eof()) {
 
113
            // emit empty result
 
114
            ValuesList values;
 
115
            emit data(values);
 
116
        }
 
117
        else {
 
118
            read();
 
119
 
 
120
            if(toConfigurationSingle::Instance().firewallMode())
 
121
                QTimer::singleShot(FIREWALL_TIMEOUT, this, SLOT(timeout()));
 
122
 
 
123
            // begin thread's event loop
 
124
            thread()->exec();
 
125
        }
 
126
    }
 
127
    CATCH_ALL;
 
128
 
 
129
    close();
 
130
 
 
131
    try {
 
132
        if(Statistics)
 
133
            Statistics->changeSession(*Query);
 
134
    }
 
135
    catch(...) {
 
136
        // ignored
 
137
    }
 
138
 
 
139
    try {
 
140
        QMutexLocker lock(&CloseLock);
 
141
        Closed = true;
 
142
        delete Query;
 
143
        Query = 0;
 
144
    }
 
145
    catch(...) {
 
146
        // ignored
 
147
    }
 
148
 
 
149
    ThreadAlive.lock();
 
150
}
 
151
 
 
152
 
 
153
toEventQueryTask::~toEventQueryTask() {
 
154
}
 
155
 
 
156
 
 
157
void toEventQueryTask::close() {
 
158
    try {
 
159
        emit done();
 
160
 
 
161
        disconnect(this, 0, 0, 0);
 
162
 
 
163
        QMutexLocker lock(&CloseLock);
 
164
        if(Query && !Closed)
 
165
            Query->cancel();
 
166
    }
 
167
    catch(...) {
 
168
        // noop
 
169
    }
 
170
 
 
171
    Closed = true;
 
172
 
 
173
    // exit thread event loop. safe to call before event loop starts.
 
174
    if(thread())
 
175
        thread()->exit();
 
176
}
 
177
 
 
178
 
 
179
void toEventQueryTask::read(bool all) {
 
180
    emit readRequested(all);
 
181
}
 
182
 
 
183
 
 
184
void toEventQueryTask::pread(bool all) {
 
185
    if(!Query || Columns < 1) {
 
186
        close();
 
187
        return;
 
188
    }
 
189
 
 
190
    int maxRead = toConfigurationSingle::Instance().maxNumber();
 
191
 
 
192
    try {
 
193
        do {
 
194
            ValuesList values;
 
195
 
 
196
            for(int row = 0; row < maxRead; row++) {
 
197
                for(int i = 0; i < Columns && !Query->eof(); i++)
 
198
                    values.append(Query->readValueNull());
 
199
            }
 
200
 
 
201
            if(values.size() > 0)
 
202
                emit data(values);    // must not access after this line
 
203
        } while(all && !Query->eof());
 
204
    }
 
205
    CATCH_ALL;
 
206
 
 
207
    try {
 
208
        if(!Query || Query->eof())
 
209
            close();
 
210
    }
 
211
    CATCH_ALL;
 
212
}
 
213
 
 
214
 
 
215
void toEventQueryTask::timeout()
 
216
{
 
217
    if(Closed)
 
218
        return;
 
219
 
 
220
    pread(false);
 
221
 
 
222
    // check again in case config changes
 
223
    if(toConfigurationSingle::Instance().firewallMode())
 
224
        QTimer::singleShot(FIREWALL_TIMEOUT, this, SLOT(timeout()));
 
225
}