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
|
/* This file is part of the KDE project
Copyright (C) 2003-2004 Jaroslaw Staniek <js@iidea.pl>
This program 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 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <kexidb/driver.h>
#include <kexidb/driver_p.h>
#include <kexidb/drivermanager.h>
#include <kexidb/drivermanager_p.h>
#include "error.h"
#include "drivermanager.h"
#include "connection.h"
#include "connectiondata.h"
#include "admin.h"
#include <tqfileinfo.h>
#include <tdelocale.h>
#include <kdebug.h>
#include <assert.h>
using namespace KexiDB;
/*! used when we do not have Driver instance yet,
or when we cannot get one */
TQValueVector<TQString> dflt_typeNames;
//---------------------------------------------
DriverBehaviour::DriverBehaviour()
: UNSIGNED_TYPE_KEYWORD("UNSIGNED")
, AUTO_INCREMENT_FIELD_OPTION("AUTO_INCREMENT")
, AUTO_INCREMENT_PK_FIELD_OPTION("AUTO_INCREMENT PRIMARY KEY")
, SPECIAL_AUTO_INCREMENT_DEF(false)
, AUTO_INCREMENT_REQUIRES_PK(false)
, ROW_ID_FIELD_RETURNS_LAST_AUTOINCREMENTED_VALUE(false)
, QUOTATION_MARKS_FOR_IDENTIFIER('"')
, USING_DATABASE_REQUIRED_TO_CONNECT(true)
, _1ST_ROW_READ_AHEAD_REQUIRED_TO_KNOW_IF_THE_RESULT_IS_EMPTY(false)
, SELECT_1_SUBQUERY_SUPPORTED(false)
, SQL_KEYWORDS(0)
{
}
//---------------------------------------------
Driver::Info::Info()
: fileBased(false)
, allowImportingTo(true)
{
}
//---------------------------------------------
Driver::Driver( TQObject *parent, const char *name, const TQStringList & )
: TQObject( parent, name )
, Object()
, beh( new DriverBehaviour() )
, d( new DriverPrivate() )
{
d->connections.setAutoDelete(false);
//TODO: reasonable size
d->connections.resize(101);
d->typeNames.resize(Field::LastType + 1);
d->initKexiKeywords();
}
Driver::~Driver()
{
DriverManagerInternal::self()->aboutDelete( this );
// KexiDBDbg << "Driver::~Driver()" << endl;
TQPtrDictIterator<Connection> it( d->connections );
Connection *conn;
while ( (conn = it.toFirst()) ) {
delete conn;
}
delete beh;
delete d;
// KexiDBDbg << "Driver::~Driver() ok" << endl;
}
bool Driver::isValid()
{
clearError();
if (KexiDB::version().major != version().major
|| KexiDB::version().minor != version().minor)
{
setError(ERR_INCOMPAT_DRIVER_VERSION,
i18n("Incompatible database driver's \"%1\" version: found version %2, expected version %3.")
.arg(name())
.arg(TQString("%1.%2").arg(version().major).arg(version().minor))
.arg(TQString("%1.%2").arg(KexiDB::version().major).arg(KexiDB::version().minor)));
return false;
}
TQString inv_impl = i18n("Invalid database driver's \"%1\" implementation:\n").arg(name());
TQString not_init = i18n("Value of \"%1\" is not initialized for the driver.");
if (beh->ROW_ID_FIELD_NAME.isEmpty()) {
setError(ERR_INVALID_DRIVER_IMPL, inv_impl + not_init.arg("DriverBehaviour::ROW_ID_FIELD_NAME"));
return false;
}
return true;
}
const TQPtrList<Connection> Driver::connectionsList() const
{
TQPtrList<Connection> clist;
TQPtrDictIterator<Connection> it( d->connections );
for( ; it.current(); ++it )
clist.append( &(*it) );
return clist;
}
TQString Driver::fileDBDriverMimeType() const
{ return d->fileDBDriverMimeType; }
TQString Driver::defaultFileBasedDriverMimeType()
{ return TQString::fromLatin1("application/x-kexiproject-sqlite3"); }
TQString Driver::defaultFileBasedDriverName()
{
DriverManager dm;
return dm.lookupByMime(Driver::defaultFileBasedDriverMimeType()).lower();
}
const KService* Driver::service() const
{ return d->service; }
bool Driver::isFileDriver() const
{ return d->isFileDriver; }
int Driver::features() const
{ return d->features; }
bool Driver::transactionsSupported() const
{ return d->features & (SingleTransactions | MultipleTransactions); }
AdminTools& Driver::adminTools() const
{
if (!d->adminTools)
d->adminTools = drv_createAdminTools();
return *d->adminTools;
}
AdminTools* Driver::drv_createAdminTools() const
{
return new AdminTools(); //empty impl.
}
TQString Driver::sqlTypeName(int id_t, int /*p*/) const
{
if (id_t > Field::InvalidType && id_t <= Field::LastType)
return d->typeNames[(id_t>0 && id_t<=Field::LastType) ? id_t : Field::InvalidType /*sanity*/];
return d->typeNames[Field::InvalidType];
}
Connection *Driver::createConnection( ConnectionData &conn_data, int options )
{
clearError();
if (!isValid())
return 0;
if (d->isFileDriver) {
if (conn_data.fileName().isEmpty()) {
setError(ERR_MISSING_DB_LOCATION, i18n("File name expected for file-based database driver.") );
return 0;
}
}
// Connection *conn = new Connection( this, conn_data );
Connection *conn = drv_createConnection( conn_data );
conn->setReadOnly(options & ReadOnlyConnection);
conn_data.driverName = name();
d->connections.insert( conn, conn );
return conn;
}
Connection* Driver::removeConnection( Connection *conn )
{
clearError();
return d->connections.take( conn );
}
TQString Driver::defaultSQLTypeName(int id_t)
{
if (id_t>=Field::Null)
return "Null";
if (dflt_typeNames.isEmpty()) {
dflt_typeNames.resize(Field::LastType + 1);
dflt_typeNames[Field::InvalidType]="InvalidType";
dflt_typeNames[Field::Byte]="Byte";
dflt_typeNames[Field::ShortInteger]="ShortInteger";
dflt_typeNames[Field::Integer]="Integer";
dflt_typeNames[Field::BigInteger]="BigInteger";
dflt_typeNames[Field::Boolean]="Boolean";
dflt_typeNames[Field::Date]="Date";
dflt_typeNames[Field::DateTime]="DateTime";
dflt_typeNames[Field::Time]="Time";
dflt_typeNames[Field::Float]="Float";
dflt_typeNames[Field::Double]="Double";
dflt_typeNames[Field::Text]="Text";
dflt_typeNames[Field::LongText]="LongText";
dflt_typeNames[Field::BLOB]="BLOB";
}
return dflt_typeNames[id_t];
}
bool Driver::isSystemObjectName( const TQString& n ) const
{
return Driver::isKexiDBSystemObjectName(n);
}
bool Driver::isKexiDBSystemObjectName( const TQString& n )
{
if (!n.lower().startsWith("kexi__"))
return false;
const TQStringList list( Connection::kexiDBSystemTableNames() );
return list.find(n.lower())!=list.constEnd();
}
bool Driver::isSystemFieldName( const TQString& n ) const
{
if (!beh->ROW_ID_FIELD_NAME.isEmpty() && n.lower()==beh->ROW_ID_FIELD_NAME.lower())
return true;
return drv_isSystemFieldName(n);
}
TQString Driver::valueToSQL( uint ftype, const TQVariant& v ) const
{
if (v.isNull())
return "NULL";
switch (ftype) {
case Field::Text:
case Field::LongText: {
TQString s = v.toString();
return escapeString(s); //TQString("'")+s.replace( '"', "\\\"" ) + "'";
}
case Field::Byte:
case Field::ShortInteger:
case Field::Integer:
case Field::BigInteger:
return v.toString();
case Field::Float:
case Field::Double: {
if (v.type()==TQVariant::String) {
//workaround for values stored as string that should be casted to floating-point
TQString s(v.toString());
return s.replace(',', ".");
}
return v.toString();
}
//TODO: here special encoding method needed
case Field::Boolean:
return TQString::number(v.toInt()?1:0); //0 or 1
case Field::Time:
return TQString("\'")+v.toTime().toString(TQt::ISODate)+"\'";
case Field::Date:
return TQString("\'")+v.toDate().toString(TQt::ISODate)+"\'";
case Field::DateTime:
return dateTimeToSQL( v.toDateTime() );
case Field::BLOB: {
if (v.toByteArray().isEmpty())
return TQString::fromLatin1("NULL");
if (v.type()==TQVariant::String)
return escapeBLOB(v.toString().utf8());
return escapeBLOB(v.toByteArray());
}
case Field::InvalidType:
return "!INVALIDTYPE!";
default:
KexiDBDbg << "Driver::valueToSQL(): UNKNOWN!" << endl;
return TQString();
}
return TQString();
}
TQVariant Driver::propertyValue( const TQCString& propName ) const
{
return d->properties[propName.lower()];
}
TQString Driver::propertyCaption( const TQCString& propName ) const
{
return d->propertyCaptions[propName.lower()];
}
TQValueList<TQCString> Driver::propertyNames() const
{
TQValueList<TQCString> names = d->properties.keys();
qHeapSort(names);
return names;
}
TQString Driver::escapeIdentifier(const TQString& str, int options) const
{
TQCString cstr = str.latin1();
return TQString(escapeIdentifier(cstr, options));
}
TQCString Driver::escapeIdentifier(const TQCString& str, int options) const
{
bool needOuterQuotes = false;
// Need to use quotes if ...
// ... we have been told to, or ...
if(options & EscapeAlways)
needOuterQuotes = true;
// ... or if the driver does not have a list of keywords,
else if(!d->driverSQLDict)
needOuterQuotes = true;
// ... or if it's a keyword in Kexi's SQL dialect,
else if(d->kexiSQLDict->find(str))
needOuterQuotes = true;
// ... or if it's a keyword in the backends SQL dialect,
// (have already checked !d->driverSQLDict)
else if((options & EscapeDriver) && d->driverSQLDict->find(str))
needOuterQuotes = true;
// ... or if the identifier has a space in it...
else if(str.find(' ') != -1)
needOuterQuotes = true;
if(needOuterQuotes && (options & EscapeKexi)) {
const char quote = '"';
return quote + TQCString(str).replace( quote, "\"\"" ) + quote;
}
else if (needOuterQuotes) {
const char quote = beh->QUOTATION_MARKS_FOR_IDENTIFIER.latin1();
return quote + drv_escapeIdentifier(str) + quote;
} else {
return drv_escapeIdentifier(str);
}
}
void Driver::initSQLKeywords(int hashSize) {
if(!d->driverSQLDict && beh->SQL_KEYWORDS != 0) {
d->initDriverKeywords(beh->SQL_KEYWORDS, hashSize);
}
}
#include "driver.moc"
|