[go: up one dir, main page]

File: toresultparam.cpp

package info (click to toggle)
tora 1.3.23-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 15,984 kB
  • ctags: 14,460
  • sloc: cpp: 123,554; sh: 16,181; makefile: 966; xml: 69
file content (417 lines) | stat: -rw-r--r-- 14,604 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*****
*
* TOra - An Oracle Toolkit for DBA's and developers
* Copyright (C) 2003-2005 Quest Software, Inc
* Portions Copyright (C) 2005 Other Contributors
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation;  only version 2 of
* the License is valid for this program.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*
*      As a special exception, you have permission to link this program
*      with the Oracle Client libraries and distribute executables, as long
*      as you follow the requirements of the GNU GPL in regard to all of the
*      software in the executable aside from Oracle client libraries.
*
*      Specifically you are not permitted to link this program with the
*      Qt/UNIX, Qt/Windows or Qt Non Commercial products of TrollTech.
*      And you are not permitted to distribute binaries compiled against
*      these libraries without written consent from Quest Software, Inc.
*      Observe that this does not disallow linking to the Qt Free Edition.
*
*      You may link this product with any GPL'd Qt library such as Qt/Free
*
* All trademarks belong to their respective owners.
*
*****/

#include "utils.h"

#include "toconf.h"
#include "toresultparam.h"
#include "tosql.h"
#include "toresultview.h"
#include "tomemoeditor.h"
#include "toconnection.h"
#include "toresultlong.h"

#include <qlabel.h>
#include <qlineedit.h>
#include <qregexp.h>
#include <qtoolbar.h>
#include <qtoolbutton.h>
#include <qtooltip.h>

#include "toresultparam.moc"

#include "icons/database.xpm"
#include "icons/filesave.xpm"
#include "icons/scansource.xpm"
#include "icons/tocurrent.xpm"
#include "icons/trash.xpm"
#include "icons/commit.xpm"

static toSQL SQLParamsMySQL("toResultParam:ListParam",
                            "TOAD 1,2 show variables",
                            "List parameters available in the session",
                            "4.0",
                            "MySQL");

static toSQL SQLParamsGlobal("toResultParam:ListGlobal",
                             "TOAD 1,2 show global variables",
                             "List parameters available in the database",
                             "4.0",
                             "MySQL");

static toSQL SQLParams("toResultParam:ListParam",
                       "select name \"Parameter\",value \"Value\",' ' \"Changed\",\n"
                       "       description \"Description\", num \" Number\",\n"
                       "       type \" Type\",isdefault \" Default\",\n"
                       "       isses_modifiable \" Sesmod\",issys_modifiable \" Sysmod\"\n"
                       "  from v$parameter order by name",
                       "");

static toSQL SQLHiddenParams("toResultParam:ListHidden",
                             "SELECT KSPPINM \"Parameter\",\n"
                             "       KSPFTCTXVL \"Value\",\n"
                             "       ' ' \"Changed\",\n"
                             "       KSPPDESC \"Description\",\n"
                             "       KSPFTCTXPN \" Num\",\n"
                             "       ksppity \" Type\",\n"
                             "       KSPFTCTXDF \" Default\",\n"
                             "       DECODE(MOD(TRUNC(KSPPIFLG/256),2),0,'FALSE','TRUE') \" Sesmod\",\n"
                             "       DECODE(MOD(TRUNC(KSPPIFLG/65536),8),0,'FALSE','TRUE') \" Sysmod\"\n"
                             "  FROM X$KSPPI x,\n"
                             "       X$KSPPCV2 y\n"
                             " WHERE x.INDX+1=y.KSPFTCTXPN ORDER BY KSPPINM",
                             "List parameters available in the database including hidden parameters");

bool toResultParam::canHandle(toConnection &conn)
{
    return toIsOracle(conn) || toIsMySQL(conn);
}

toResultParam::toResultParam(QWidget *parent, const char *name)
        : QVBox(parent, name)
{
    QToolBar *toolbar = toAllocBar(this, tr("Parameter editor"));
    Toggle = new QToolButton(toolbar);
    Toggle->setToggleButton(true);
    if (toIsOracle(connection()))
    {
        Toggle->setIconSet(QIconSet(QPixmap(const_cast<const char**>(scansource_xpm))));
        connect(Toggle, SIGNAL(toggled(bool)), this, SLOT(showHidden(bool)));
        QToolTip::add
            (Toggle, tr("Display hidden parameters. This will only word if you are logged in as the sys user."));
    }
    else
    {
        QIconSet iconset(QPixmap(const_cast<const char**>(tocurrent_xpm)));
        iconset.setPixmap(QPixmap(const_cast<const char**>(database_xpm)), QIconSet::Automatic, QIconSet::Normal, QIconSet::On);
        Toggle->setIconSet(iconset);
        connect(Toggle, SIGNAL(toggled(bool)), this, SLOT(showGlobal(bool)));
        QToolTip::add
            (Toggle, tr("Switch between global and session variables to show."));
    }
    toolbar->addSeparator();

    new QToolButton(QPixmap(const_cast<const char**>(filesave_xpm)),
                    tr("Generate configuration file"),
                    tr("Generate configuration file"),
                    this, SLOT(generateFile()), toolbar);
    toolbar->addSeparator();
    if (toIsOracle(connection()))
    {
        new QToolButton(QPixmap(const_cast<const char**>(database_xpm)),
                        tr("Apply changes to system"),
                        tr("Apply changes to system"),
                        this, SLOT(applySystem()), toolbar);
        new QToolButton(QPixmap(const_cast<const char**>(tocurrent_xpm)),
                        tr("Apply changes to session"),
                        tr("Apply changes to session"),
                        this, SLOT(applySession()), toolbar);
    }
    else
    {
        new QToolButton(QPixmap(const_cast<const char**>(commit_xpm)),
                        tr("Apply changes"),
                        tr("Apply changes"),
                        this, SLOT(applyChanges()), toolbar);
    }
    if (toIsOracle(connection()))
    {
        toolbar->addSeparator();
        new QToolButton(QPixmap(const_cast<const char**>(trash_xpm)),
                        tr("Drop current changes"),
                        tr("Drop current changes"),
                        this, SLOT(dropChanges()), toolbar);
    }
    toolbar->setStretchableWidget(new QLabel(toolbar, TO_TOOLBAR_WIDGET_NAME));

    Params = new toResultLong(false, false, toQuery::Background, this);
    Params->setSQL(SQLParams);
    Params->setReadAll(true);
    Params->setSelectionMode(QListView::Single);
    connect(Params, SIGNAL(selectionChanged()), this, SLOT(changeItem()));
    connect(Params, SIGNAL(done()), this, SLOT(done()));
    Value = new QLineEdit(this);
    Value->setEnabled(false);
    refresh();
}

void toResultParam::showHidden(bool hid)
{
    if (hid)
        Params->setSQL(SQLHiddenParams);
    else
        Params->setSQL(SQLParams);
    refresh();
}

void toResultParam::showGlobal(bool glb)
{
    if (glb)
        Params->setSQL(SQLParamsGlobal);
    else
        Params->setSQL(SQLParams);
    refresh();
}

void toResultParam::query(const QString &, const toQList &)
{
    saveChange();
    LastItem = QString::null;

    Params->refresh();
}

void toResultParam::dropChanges(void)
{
    NewValues.clear();
    refresh();
}

void toResultParam::done()
{
    for (QListViewItem *item = Params->firstChild();item;item = item->nextSibling())
    {
        std::map<QString, QString>::iterator i = NewValues.find(item->text(0));
        if (i != NewValues.end())
        {
            item->setText(1, (*i).second);
            item->setText(6, QString::fromLatin1("FALSE"));
            item->setText(2, tr("Changed"));
        }
    }
}

void toResultParam::saveChange()
{
    if (!LastItem.isEmpty() && LastValue != Value->text())
    {
        NewValues[LastItem] = Value->text();
        LastValue = Value->text();
        for (QListViewItem *item = Params->firstChild();item;item = item->nextSibling())
        {
            if (item->text(0) == LastItem)
            {
                item->setText(1, LastValue);
                item->setText(6, QString::fromLatin1("FALSE"));
                item->setText(2, tr("Changed"));
                break;
            }
        }
    }
}

void toResultParam::generateFile(void)
{
    saveChange();
    QString str = tr("# Generated by TOra version %1\n\n").arg(QString::fromLatin1(TOVERSION));
    QRegExp comma(QString::fromLatin1("\\s*,\\s+"));
    for (QListViewItem *item = Params->firstChild();item;item = item->nextSibling())
    {
        if (toIsOracle(connection()))
        {
            if (item->text(6) == QString::fromLatin1("FALSE"))
            {
                str += item->text(0);
                str += QString::fromLatin1(" = ");
                if (item->text(5) == QString::fromLatin1("2"))
                {
                    QStringList lst = QStringList::split(comma, item->text(1));
                    if (lst.count() > 1)
                        str += QString::fromLatin1("( ");
                    for (unsigned int i = 0;i < lst.count();i++)
                    {
                        if (i > 0)
                            str += QString::fromLatin1(", ");
                        str += QString::fromLatin1("\"") + lst[i] + QString::fromLatin1("\"");
                    }
                    if (lst.count() > 1)
                        str += QString::fromLatin1(" )");
                }
                else
                    str += item->text(1);
                str += QString::fromLatin1("\n");
            }
        }
        else
        {
            str += item->text(0) + " = '" + item->text(1) + "'\n";
        }
    }
    connect(new toMemoEditor(this, str, 0, 0), SIGNAL(changeData(int, int, const QString &)),
            this, SLOT(changedData(int, int, const QString &)));
}

void toResultParam::applySession(void)
{
    try
    {
        saveChange();
        toConnection &conn = connection();
        for (QListViewItem *item = Params->firstChild();item;item = item->nextSibling())
        {
            if (item->text(2) == tr("Changed"))
            {
                try
                {
                    if (item->text(7) != QString::fromLatin1("FALSE"))
                    {
                        QString str = QString::fromLatin1("ALTER SESSION SET ");
                        str += item->text(0);
                        str += QString::fromLatin1(" = ");
                        if (item->text(5) == QString::fromLatin1("2"))
                        {
                            str += QString::fromLatin1("'");
                            str += item->text(1);
                            str += QString::fromLatin1("'");
                        }
                        else
                            str += item->text(1);
                        conn.allExecute(str);
                        std::map<QString, QString>::iterator i = NewValues.find(item->text(0));
                        if (i != NewValues.end())
                            NewValues.erase(i);
                    }
                }
                TOCATCH
            }
        }
    }
    TOCATCH
    refresh();
}

void toResultParam::applySystem(void)
{
    try
    {
        saveChange();
        toConnection &conn = connection();
        for (QListViewItem *item = Params->firstChild();item;item = item->nextSibling())
        {
            if (item->text(2) == tr("Changed"))
            {
                try
                {
                    if (item->text(8) != QString::fromLatin1("FALSE"))
                    {
                        QString str = QString::fromLatin1("ALTER SYSTEM SET ");
                        str += item->text(0);
                        str += QString::fromLatin1(" = ");
                        if (item->text(5) == QString::fromLatin1("2"))
                        {
                            str += QString::fromLatin1("'");
                            str += item->text(1);
                            str += QString::fromLatin1("'");
                        }
                        else
                            str += item->text(1);
                        conn.execute(str);
                        std::map<QString, QString>::iterator i = NewValues.find(item->text(0));
                        if (i != NewValues.end())
                            NewValues.erase(i);
                    }
                }
                TOCATCH
            }
        }
    }
    TOCATCH
    refresh();
}

// Only used by MySQL

void toResultParam::applyChanges(void)
{
    try
    {
        saveChange();
        toConnection &conn = connection();
        for (QListViewItem *item = Params->firstChild();item;item = item->nextSibling())
        {
            if (item->text(2) == tr("Changed"))
            {
                try
                {
                    QString str = "SET ";
                    if (!Toggle->isOn())
                        str += "GLOBAL ";
                    else
                        str += "SESSION ";
                    str += item->text(0);
                    str += " = ";
                    str += QString::fromLatin1("'");
                    str += item->text(1);
                    str += QString::fromLatin1("'");
                    conn.execute(str);
                    std::map<QString, QString>::iterator i = NewValues.find(item->text(0));
                    if (i != NewValues.end())
                        NewValues.erase(i);
                }
                TOCATCH
            }
        }
    }
    TOCATCH
    refresh();
}

void toResultParam::changeItem(void)
{
    saveChange();

    QListViewItem *item = Params->selectedItem();
    if (item)
    {
        LastItem = item->text(0);
        LastValue = item->text(1);
        Value->setText(LastValue);
        Value->setEnabled(true);
    }
    else
    {
        LastItem = QString::null;
        Value->setEnabled(false);
    }
}

void toResultParam::changedData(int, int, const QString &data)
{
    QString file = toSaveFilename(QString::null, toIsOracle(connection()) ? "*.pfile" : "*.conf", this);
    if (!file.isEmpty())
        toWriteFile(file, data);
}