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
|
/* This file is part of the KDE project
Copyright (C) 2001 Joseph Wenninger <[email protected]>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "KWQtSqlPowerSerialDataSource.h"
#include "KWQtSqlPowerSerialDataSource.moc"
#include "KWQtSqlMailMergeOpen.h"
#include <tqlayout.h>
#include <tqdom.h>
#include <kcombobox.h>
#include <klineedit.h>
#include <kpushbutton.h>
#include <tqsqldatabase.h>
#include <tqmessagebox.h>
#include <kpassdlg.h>
#include <tqsqlrecord.h>
#include <tqsqlcursor.h>
#include <tqdatatable.h>
#include <kdebug.h>
#define KWQtSqlBarIcon( x ) BarIcon( x, db->KWInstance() )
/******************************************************************
*
* Class: KWQtSqlSerialDataSource
*
******************************************************************/
KWQtSqlPowerSerialDataSource::KWQtSqlPowerSerialDataSource(TDEInstance *inst,TQObject *parent)
: KWQtSqlSerialDataSourceBase(inst,parent),myquery(0)
{
port=i18n("default");
}
KWQtSqlPowerSerialDataSource::~KWQtSqlPowerSerialDataSource()
{
if (myquery) delete myquery;
TQSqlDatabase::removeDatabase("KWTQTSQLPOWER");
}
void KWQtSqlPowerSerialDataSource::refresh(bool force)
{
if ((force) || (myquery==0))
{
if (myquery)
{
delete myquery;
myquery=0;
}
TQString tmp=query.upper();
if (!tmp.startsWith("SELECT")) return;
if ((!database) || (!database->isOpen()))openDatabase();
myquery=new KWMySqlCursor(query,true,database);
myquery->setMode(TQSqlCursor::ReadOnly);
}
kdDebug()<<TQString("There were %1 rows in the query").arg(myquery->size())<<endl;
}
TQString KWQtSqlPowerSerialDataSource::getValue( const TQString &name, int record ) const
{
int num=record;
if (!myquery) return name;
if ( num < 0 || num > (int)myquery->size() )
return name;
if (!myquery->seek(num,false)) return i18n(">>>Illegal position within datasource<<<");
if (!myquery->contains(name)) return i18n(">>>Field %1 is unknown in the current database query<<<").arg(name);
return (myquery->value(name)).toString();
}
void KWQtSqlPowerSerialDataSource::save( TQDomDocument &doc, TQDomElement &parent)
{
TQDomElement def=doc.createElement(TQString::fromLatin1("DEFINITION"));
parent.appendChild(def);
{
TQDomElement defEnt=doc.createElement(TQString::fromLatin1("DATABASE"));
defEnt.setAttribute(TQString::fromLatin1("hostname"),hostname);
defEnt.setAttribute(TQString::fromLatin1("port"),port);
defEnt.setAttribute(TQString::fromLatin1("driver"),driver);
defEnt.setAttribute(TQString::fromLatin1("databasename"),databasename);
defEnt.setAttribute(TQString::fromLatin1("username"),username);
def.appendChild(defEnt);
defEnt=doc.createElement(TQString::fromLatin1("QUERY"));
defEnt.setAttribute(TQString::fromLatin1("value"),query);
def.appendChild(defEnt);
TQDomElement sampleEnt=doc.createElement(TQString::fromLatin1("SAMPLERECORD"));
parent.appendChild(sampleEnt);
for (DbRecord::Iterator it=sampleRecord.begin();it!=sampleRecord.end();++it)
{
TQDomElement fieldEnt=doc.createElement(TQString::fromLatin1("FIELD"));
fieldEnt.setAttribute(TQString::fromLatin1("name"),it.key());
sampleEnt.appendChild(fieldEnt);
}
}
}
void KWQtSqlPowerSerialDataSource::load( TQDomElement& parentElem )
{
clearSampleRecord();
TQDomNode defNd=parentElem.namedItem("DEFINITION");
if (!defNd.isNull())
{
TQDomElement def=defNd.toElement();
TQDomNode dbNd=def.namedItem("DATABASE");
if (!dbNd.isNull())
{
TQDomElement dbEnt=dbNd.toElement();
if (dbEnt.tagName()==TQString::fromLatin1("DATABASE"))
{
hostname=dbEnt.attribute(TQString::fromLatin1("hostname"));
port=dbEnt.attribute(TQString::fromLatin1("port"));
driver=dbEnt.attribute(TQString::fromLatin1("driver"));
databasename=dbEnt.attribute(TQString::fromLatin1("databasename"));
username=dbEnt.attribute(TQString::fromLatin1("username"));
}
}
TQDomNode queryNd=def.namedItem("QUERY");
if (!queryNd.isNull())
{
query=queryNd.toElement().attribute(TQString::fromLatin1("value"));
}
}
defNd=parentElem.namedItem("SAMPLERECORD");
if (!defNd.isNull())
{
TQDomElement def1=defNd.toElement();
for (TQDomElement defEnt=defNd.firstChild().toElement();!defEnt.isNull();defEnt=defEnt.nextSibling().toElement())
{
addSampleRecordEntry(defEnt.attribute(TQString::fromLatin1("name")));
}
}
}
bool KWQtSqlPowerSerialDataSource::showConfigDialog(TQWidget *par,int action)
{
bool ret=false;
if (action==KWSLEdit)
{
if ((!database) || (!database->isOpen()))openDatabase();
KWQtSqlPowerMailMergeEditor *dia=new KWQtSqlPowerMailMergeEditor(par,this);
ret=dia->exec();
delete dia;
}
else ret=KWQtSqlSerialDataSourceBase::showConfigDialog(par,action);
return ret;
}
void KWQtSqlPowerSerialDataSource::clearSampleRecord() {sampleRecord.clear();}
void KWQtSqlPowerSerialDataSource::addSampleRecordEntry(TQString name)
{sampleRecord[name]=name; }//i18n("No Value");}
/******************************************************************
*
* Class: KWQtSqlMailMergeEditor
*
******************************************************************/
KWQtSqlPowerMailMergeEditor::KWQtSqlPowerMailMergeEditor( TQWidget *parent, KWQtSqlPowerSerialDataSource *db_ )
:KDialogBase( Plain, i18n( "Mail Merge - Editor" ), Ok | Cancel, Ok, parent, "", true ), db( db_ )
{
(new TQVBoxLayout(plainPage()))->setAutoAdd(true);
setMainWidget(widget=new KWQtSqlPowerWidget(plainPage()));
connect(widget->setup,TQT_SIGNAL(clicked()),this,TQT_SLOT(openSetup()));
connect(widget->tables,TQT_SIGNAL(currentChanged(TQListBoxItem*)),this,TQT_SLOT(slotTableChanged(TQListBoxItem*)));
connect(widget->execute,TQT_SIGNAL(clicked()),this,TQT_SLOT(slotExecute()));
connect(this,TQT_SIGNAL(okClicked()),this,TQT_SLOT(slotSetQuery()));
widget->query->setText(db->query);
updateDBViews();
}
void KWQtSqlPowerMailMergeEditor::slotSetQuery()
{
db->query=widget->query->text();
db->refresh(true);
}
void KWQtSqlPowerMailMergeEditor::slotExecute()
{
if (!db->database) if (!db->openDatabase()) return;
TQString tmp=widget->query->text().upper();
if (!tmp.startsWith("SELECT")) return;
KWMySqlCursor *cur=new KWMySqlCursor(widget->query->text(),true,db->database);
cur->setMode(TQSqlCursor::ReadOnly);
db->clearSampleRecord();
kdDebug()<<TQString("Fieldname count %1").arg(cur->count())<<endl;
for (uint i=0;i<cur->count();i++)
db->addSampleRecordEntry(cur->fieldName(i));
widget->queryresult->setSqlCursor(cur,true,true);
widget->queryresult->refresh(TQDataTable::RefreshAll);
}
void KWQtSqlPowerMailMergeEditor::slotTableChanged ( TQListBoxItem * item )
{
widget->fields->clear();
if (item)
{
if (!db->database) return;
TQSqlRecord rec=db->database->record(item->text());
for (uint i=0;i<rec.count();i++)
{
widget->fields->insertItem(rec.fieldName(i));
}
}
}
void KWQtSqlPowerMailMergeEditor::openSetup()
{
KWQtSqlMailMergeOpen *dia=new KWQtSqlMailMergeOpen(this,db);
if (dia->exec())
{
db->openDatabase();
updateDBViews();
}
delete dia;
}
void KWQtSqlPowerMailMergeEditor::updateDBViews()
{
widget->fields->clear();
widget->tables->clear();
if (!db->database) return;
widget->tables->insertStringList(db->database->tables());
}
KWQtSqlPowerMailMergeEditor::~KWQtSqlPowerMailMergeEditor(){;}
extern "C" {
KWORD_MAILMERGE_EXPORT KWMailMergeDataSource *create_kwmailmerge_qtsqldb_power(TDEInstance *inst,TQObject *parent)
{
return new KWQtSqlPowerSerialDataSource(inst,parent);
}
}
|