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

« back to all changes in this revision

Viewing changes to src/toresultlong.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Albin Tonnerre
  • Date: 2007-05-29 13:13:36 UTC
  • mfrom: (1.2.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070529131336-85ygaddivvmkd3xc
Tags: 1.3.21pre22-1ubuntu1
* Merge from Debian unstable. Remaining Ubuntu changes:
  - debian/rules: call dh_iconcache
  - Remove g++ build dependency
* Modify Maintainer value to match Debian-Maintainer-Field Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****
 
2
*
 
3
* TOra - An Oracle Toolkit for DBA's and developers
 
4
* Copyright (C) 2003-2005 Quest Software, Inc
 
5
* Portions Copyright (C) 2005 Other Contributors
 
6
 
7
* This program is free software; you can redistribute it and/or
 
8
* modify it under the terms of the GNU General Public License
 
9
* as published by the Free Software Foundation;  only version 2 of
 
10
* the License is valid for this program.
 
11
 
12
* This program is distributed in the hope that it will be useful,
 
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
* GNU General Public License for more details.
 
16
 
17
* You should have received a copy of the GNU General Public License
 
18
* along with this program; if not, write to the Free Software
 
19
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
20
*
 
21
*      As a special exception, you have permission to link this program
 
22
*      with the Oracle Client libraries and distribute executables, as long
 
23
*      as you follow the requirements of the GNU GPL in regard to all of the
 
24
*      software in the executable aside from Oracle client libraries.
 
25
*
 
26
*      Specifically you are not permitted to link this program with the
 
27
*      Qt/UNIX, Qt/Windows or Qt Non Commercial products of TrollTech.
 
28
*      And you are not permitted to distribute binaries compiled against
 
29
*      these libraries without written consent from Quest Software, Inc.
 
30
*      Observe that this does not disallow linking to the Qt Free Edition.
 
31
*
 
32
*      You may link this product with any GPL'd Qt library such as Qt/Free
 
33
*
 
34
* All trademarks belong to their respective owners.
 
35
*
 
36
*****/
 
37
 
 
38
#include "utils.h"
 
39
 
 
40
#include "toresultlong.h"
 
41
#include "toresultlong.moc"
 
42
#include "toconf.h"
 
43
#include "totool.h"
 
44
#include "tonoblockquery.h"
 
45
 
 
46
#include <stdio.h>
 
47
 
 
48
#include <qregexp.h>
 
49
#include <qclipboard.h>
 
50
#include <qheader.h>
 
51
#include <qtooltip.h>
 
52
 
 
53
toResultLong::toResultLong(bool readable, bool dispCol, toQuery::queryMode mode,
 
54
                           QWidget *parent, const char *name, WFlags f)
 
55
        : toResultView(readable, dispCol, parent, name, f)
 
56
{
 
57
    Query = NULL;
 
58
    Statistics = NULL;
 
59
    Mode = mode;
 
60
    connect(&Timer, SIGNAL(timeout(void)), this, SLOT(addItem(void)));
 
61
}
 
62
 
 
63
toResultLong::toResultLong(QWidget *parent, const char *name, WFlags f)
 
64
        : toResultView(parent, name, f)
 
65
{
 
66
    Query = NULL;
 
67
    Statistics = NULL;
 
68
    Mode = toQuery::Long;
 
69
    connect(&Timer, SIGNAL(timeout(void)), this, SLOT(addItem(void)));
 
70
}
 
71
 
 
72
void toResultLong::query(const QString &sql, const toQList &param)
 
73
{
 
74
    if (!setSQLParams(sql, param))
 
75
    {
 
76
        emit firstResult(toResult::sql(),
 
77
                         toConnection::exception(tr("Will not reexecute same query")), false);
 
78
        emit done();
 
79
        return ;
 
80
    }
 
81
    stop();
 
82
    Query = NULL;
 
83
    LastItem = NULL;
 
84
    RowNumber = 0;
 
85
    First = true;
 
86
 
 
87
    clear();
 
88
    while (columns() > 0)
 
89
    {
 
90
        removeColumn(0);
 
91
    }
 
92
    HasHeaders = false;
 
93
    setSorting( -1);
 
94
 
 
95
    if (NumberColumn)
 
96
    {
 
97
        addColumn(QString::fromLatin1("#"));
 
98
        setColumnAlignment(0, AlignRight);
 
99
    }
 
100
 
 
101
    if (Filter)
 
102
        Filter->startingQuery();
 
103
 
 
104
    try
 
105
    {
 
106
        Query = new toNoBlockQuery(connection(), Mode, sql, param, Statistics);
 
107
 
 
108
        if (ReadAll)
 
109
            MaxNumber = -1;
 
110
        else
 
111
            MaxNumber = toConfigurationSingle::Instance().globalConfig(CONF_MAX_NUMBER, DEFAULT_MAX_NUMBER).toInt();
 
112
        addItem();
 
113
    }
 
114
    catch (const toConnection::exception &str)
 
115
    {
 
116
        First = false;
 
117
        emit firstResult(toResult::sql(), str, true);
 
118
        emit done();
 
119
        if (Mode != toQuery::Long)
 
120
            toStatusMessage(str);
 
121
    }
 
122
    catch (const QString &str)
 
123
    {
 
124
        First = false;
 
125
        emit firstResult(toResult::sql(), str, true);
 
126
        emit done();
 
127
        if (Mode != toQuery::Long)
 
128
            toStatusMessage(str);
 
129
    }
 
130
    updateContents();
 
131
}
 
132
 
 
133
#define TO_POLL_CHECK 100
 
134
 
 
135
void toResultLong::editReadAll(void)
 
136
{
 
137
    if (Query && !Query->eof())
 
138
    {
 
139
        MaxNumber = -1;
 
140
        Timer.start(TO_POLL_CHECK);
 
141
    }
 
142
}
 
143
 
 
144
void toResultLong::addItem(void)
 
145
{
 
146
    try
 
147
    {
 
148
        if (!toCheckModal(this))
 
149
            return ;
 
150
        if (Query)
 
151
        {
 
152
            if (Query->poll())
 
153
            {
 
154
                bool em = false;
 
155
                QString buffer;
 
156
                if (First)
 
157
                {
 
158
                    QString tmp = sql().simplifyWhiteSpace().mid(0, 10).lower();
 
159
                    if (tmp.startsWith(QString::fromLatin1("update")) ||
 
160
                            tmp.startsWith(QString::fromLatin1("delete")) ||
 
161
                            tmp.startsWith(QString::fromLatin1("insert")))
 
162
                        buffer = tr("%1 rows processed").arg(Query->rowsProcessed());
 
163
                    else if (tmp.startsWith(QString::fromLatin1("select")))
 
164
                        buffer = tr("Query executed");
 
165
                    else
 
166
                        buffer = tr("Statement executed");
 
167
                    em = true;
 
168
                }
 
169
                if (!HasHeaders)
 
170
                {
 
171
                    Description = Query->describe();
 
172
                    bool hidden = false;
 
173
 
 
174
                    for (toQDescList::iterator i = Description.begin();i != Description.end();i++)
 
175
                    {
 
176
                        QString name = (*i).Name;
 
177
                        if (ReadableColumns)
 
178
                            toReadableColumn(name);
 
179
                        if (name.length() > 0 && name[0].latin1() != ' ')
 
180
                        {
 
181
                            if (hidden)
 
182
                                throw tr("Can only hide last column in query");
 
183
                            if (name[0].latin1() == '-')
 
184
                            {
 
185
                                addColumn(toTranslateMayby(sqlName(), name.right(name.length() - 1)));
 
186
                                setColumnAlignment(columns() - 1, AlignRight);
 
187
                            }
 
188
                            else
 
189
                            {
 
190
                                addColumn(toTranslateMayby(sqlName(), name));
 
191
                                if ((*i).AlignRight)
 
192
                                    setColumnAlignment(columns() - 1, AlignRight);
 
193
                            }
 
194
                        }
 
195
                        else
 
196
                            hidden = true;
 
197
                    }
 
198
                    HasHeaders = true;
 
199
 
 
200
                    if (resizeMode() != QListView::NoColumn)
 
201
                        setResizeMode(resizeMode());
 
202
 
 
203
                    if (sortColumn() < 0)
 
204
                    {
 
205
                        if (NumberColumn)
 
206
                            setSorting(0);
 
207
                        else
 
208
                            setSorting(Description.size());
 
209
                    }
 
210
                }
 
211
 
 
212
                if (!Query->eof())
 
213
                {
 
214
                    int disp = 0;
 
215
                    unsigned int cols = Description.size();
 
216
                    if (NumberColumn)
 
217
                    {
 
218
                        disp = 1;
 
219
                    }
 
220
                    do
 
221
                    {
 
222
                        QListViewItem *last = LastItem;
 
223
                        LastItem = createItem(LastItem, QString::null);
 
224
                        if (NumberColumn)
 
225
                            LastItem->setText(0, QString::number(RowNumber + 1));
 
226
                        else
 
227
                            LastItem->setText(cols, QString::number(RowNumber + 1));
 
228
                        toResultViewItem *ri = dynamic_cast<toResultViewItem *>(LastItem);
 
229
                        toResultViewCheck *ci = dynamic_cast<toResultViewCheck *>(LastItem);
 
230
                        for (unsigned int j = 0;(j < cols || j == 0) && !Query->eof();j++)
 
231
                        {
 
232
                            if (ri)
 
233
                                ri->setText(j + disp, Query->readValue());
 
234
                            else if (ci)
 
235
                                ci->setText(j + disp, Query->readValue());
 
236
                            else
 
237
                                LastItem->setText(j + disp, Query->readValue());
 
238
                        }
 
239
                        if (Filter && !Filter->check(LastItem))
 
240
                        {
 
241
                            delete LastItem;
 
242
                            LastItem = last;
 
243
                        }
 
244
                        else
 
245
                            RowNumber++;
 
246
                    }
 
247
                    while (Query->poll() && !Query->eof() && (MaxNumber < 0 || MaxNumber > RowNumber));
 
248
                }
 
249
                if (em)
 
250
                {
 
251
                    First = false;
 
252
                    emit firstResult(sql(), toConnection::exception(buffer), false);
 
253
                }
 
254
                if (Query->eof())
 
255
                {
 
256
                    cleanup();
 
257
                    return ;
 
258
                }
 
259
                if (MaxNumber < 0 || MaxNumber > RowNumber)
 
260
                {
 
261
                    if (!Timer.isActive())
 
262
                        Timer.start(1); // Must use timer, would mean really long recursion otherwise
 
263
                    else
 
264
                        Timer.start(TO_POLL_CHECK);
 
265
                }
 
266
                else
 
267
                    Timer.stop();
 
268
            }
 
269
            else
 
270
            {
 
271
                if (Query->eof())
 
272
                {
 
273
                    cleanup();
 
274
                    return ;
 
275
                }
 
276
                else if (!Timer.isActive())
 
277
                    Timer.start(TO_POLL_CHECK);
 
278
            }
 
279
        }
 
280
    }
 
281
    catch (const toConnection::exception &str)
 
282
    {
 
283
        if (First)
 
284
        {
 
285
            First = false;
 
286
            emit firstResult(sql(), str, true);
 
287
            if (Mode != toQuery::Long)
 
288
                toStatusMessage(str);
 
289
        }
 
290
        else
 
291
            toStatusMessage(str);
 
292
        cleanup();
 
293
    }
 
294
    catch (const QString &str)
 
295
    {
 
296
        if (First)
 
297
        {
 
298
            First = false;
 
299
            emit firstResult(sql(), str, true);
 
300
            if (Mode != toQuery::Long)
 
301
                toStatusMessage(str);
 
302
        }
 
303
        else
 
304
            toStatusMessage(str);
 
305
        cleanup();
 
306
    }
 
307
}
 
308
 
 
309
void toResultLong::cleanup(void)
 
310
{
 
311
    delete Query;
 
312
    Query = NULL;
 
313
    emit done();
 
314
    Timer.stop();
 
315
}
 
316
 
 
317
bool toResultLong::eof(void)
 
318
{
 
319
    return !Query || Query->eof();
 
320
}
 
321
 
 
322
void toResultLong::stop(void)
 
323
{
 
324
    if (Query)
 
325
    {
 
326
        delete Query;
 
327
        Query = NULL;
 
328
        Timer.stop();
 
329
        emit done();
 
330
    }
 
331
}
 
332
 
 
333
toResultLong::~toResultLong()
 
334
{
 
335
    if (Query)
 
336
    {
 
337
        delete Query;
 
338
        Query = NULL;
 
339
    }
 
340
}
 
341