author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 06 Jul 2010 15:10:48 +0300 | |
changeset 30 | 5dc02b23752f |
parent 19 | fcece45ef507 |
child 33 | 3e2da88830cd |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtSql module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "qsql_oci.h" |
|
43 |
||
44 |
#include <qcoreapplication.h> |
|
45 |
#include <qvariant.h> |
|
46 |
#include <qdatetime.h> |
|
47 |
#include <qmetatype.h> |
|
48 |
#include <qregexp.h> |
|
49 |
#include <qshareddata.h> |
|
50 |
#include <qsqlerror.h> |
|
51 |
#include <qsqlfield.h> |
|
52 |
#include <qsqlindex.h> |
|
53 |
#include <qsqlquery.h> |
|
54 |
#include <qstringlist.h> |
|
55 |
#include <qvarlengtharray.h> |
|
56 |
#include <qvector.h> |
|
57 |
#include <qdebug.h> |
|
58 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
59 |
// This is needed for oracle oci when compiling with mingw-w64 headers |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
60 |
#if defined(__MINGW64_VERSION_MAJOR) && defined(_WIN64) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
61 |
#define _int64 __int64 |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
62 |
#endif |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
63 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
64 |
|
0 | 65 |
#include <oci.h> |
66 |
#ifdef max |
|
67 |
#undef max |
|
68 |
#endif |
|
69 |
#ifdef min |
|
70 |
#undef min |
|
71 |
#endif |
|
72 |
||
73 |
#include <stdlib.h> |
|
74 |
||
75 |
#define QOCI_DYNAMIC_CHUNK_SIZE 65535 |
|
76 |
#define QOCI_PREFETCH_MEM 10240 |
|
77 |
||
78 |
// setting this define will allow using a query from a different |
|
79 |
// thread than its database connection. |
|
80 |
// warning - this is not fully tested and can lead to race conditions |
|
81 |
#define QOCI_THREADED |
|
82 |
||
83 |
//#define QOCI_DEBUG |
|
84 |
||
85 |
Q_DECLARE_METATYPE(OCIEnv*) |
|
86 |
Q_DECLARE_METATYPE(OCIStmt*) |
|
87 |
||
88 |
QT_BEGIN_NAMESPACE |
|
89 |
||
90 |
#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN |
|
91 |
enum { QOCIEncoding = 2002 }; // AL16UTF16LE |
|
92 |
#else |
|
93 |
enum { QOCIEncoding = 2000 }; // AL16UTF16 |
|
94 |
#endif |
|
95 |
||
96 |
static const ub1 CSID_NCHAR = SQLCS_NCHAR; |
|
97 |
static const ub2 qOraCharset = OCI_UCS2ID; |
|
98 |
||
99 |
typedef QVarLengthArray<sb2, 32> IndicatorArray; |
|
100 |
typedef QVarLengthArray<ub2, 32> SizeArray; |
|
101 |
||
102 |
static QByteArray qMakeOraDate(const QDateTime& dt); |
|
103 |
static QDateTime qMakeDate(const char* oraDate); |
|
104 |
static QString qOraWarn(OCIError *err, int *errorCode = 0); |
|
105 |
#ifndef Q_CC_SUN |
|
106 |
static // for some reason, Sun CC can't use qOraWarning when it's declared static |
|
107 |
#endif |
|
108 |
void qOraWarning(const char* msg, OCIError *err); |
|
109 |
static QSqlError qMakeError(const QString& errString, QSqlError::ErrorType type, OCIError *err); |
|
110 |
||
111 |
class QOCIRowId: public QSharedData |
|
112 |
{ |
|
113 |
public: |
|
114 |
QOCIRowId(OCIEnv *env); |
|
115 |
~QOCIRowId(); |
|
116 |
||
117 |
OCIRowid *id; |
|
118 |
||
119 |
private: |
|
120 |
QOCIRowId(const QOCIRowId &other): QSharedData(other) { Q_ASSERT(false); } |
|
121 |
}; |
|
122 |
||
123 |
QOCIRowId::QOCIRowId(OCIEnv *env) |
|
124 |
: id(0) |
|
125 |
{ |
|
126 |
OCIDescriptorAlloc (env, reinterpret_cast<dvoid **>(&id), |
|
127 |
OCI_DTYPE_ROWID, 0, 0); |
|
128 |
} |
|
129 |
||
130 |
QOCIRowId::~QOCIRowId() |
|
131 |
{ |
|
132 |
if (id) |
|
133 |
OCIDescriptorFree(id, OCI_DTYPE_ROWID); |
|
134 |
} |
|
135 |
||
136 |
typedef QSharedDataPointer<QOCIRowId> QOCIRowIdPointer; |
|
137 |
QT_BEGIN_INCLUDE_NAMESPACE |
|
138 |
Q_DECLARE_METATYPE(QOCIRowIdPointer) |
|
139 |
QT_END_INCLUDE_NAMESPACE |
|
140 |
||
141 |
class QOCICols; |
|
142 |
||
143 |
struct QOCIResultPrivate |
|
144 |
{ |
|
145 |
QOCIResultPrivate(QOCIResult *result, const QOCIDriverPrivate *driver); |
|
146 |
~QOCIResultPrivate(); |
|
147 |
||
148 |
QOCICols *cols; |
|
149 |
QOCIResult *q; |
|
150 |
OCIEnv *env; |
|
151 |
OCIError *err; |
|
152 |
OCISvcCtx *&svc; |
|
153 |
OCIStmt *sql; |
|
154 |
bool transaction; |
|
155 |
int serverVersion; |
|
156 |
int prefetchRows, prefetchMem; |
|
157 |
||
158 |
void setCharset(OCIBind* hbnd); |
|
159 |
void setStatementAttributes(); |
|
160 |
int bindValue(OCIStmt *sql, OCIBind **hbnd, OCIError *err, int pos, |
|
161 |
const QVariant &val, dvoid *indPtr, ub2 *tmpSize, QList<QByteArray> &tmpStorage); |
|
162 |
int bindValues(QVector<QVariant> &values, IndicatorArray &indicators, SizeArray &tmpSizes, |
|
163 |
QList<QByteArray> &tmpStorage); |
|
164 |
void outValues(QVector<QVariant> &values, IndicatorArray &indicators, |
|
165 |
QList<QByteArray> &tmpStorage); |
|
166 |
inline bool isOutValue(int i) const |
|
167 |
{ return q->bindValueType(i) & QSql::Out; } |
|
168 |
inline bool isBinaryValue(int i) const |
|
169 |
{ return q->bindValueType(i) & QSql::Binary; } |
|
170 |
}; |
|
171 |
||
172 |
void QOCIResultPrivate::setStatementAttributes() |
|
173 |
{ |
|
174 |
Q_ASSERT(sql); |
|
175 |
||
176 |
int r = 0; |
|
177 |
||
178 |
if (prefetchRows >= 0) { |
|
179 |
r = OCIAttrSet(sql, |
|
180 |
OCI_HTYPE_STMT, |
|
181 |
&prefetchRows, |
|
182 |
0, |
|
183 |
OCI_ATTR_PREFETCH_ROWS, |
|
184 |
err); |
|
185 |
if (r != 0) |
|
186 |
qOraWarning("QOCIResultPrivate::setStatementAttributes:" |
|
187 |
" Couldn't set OCI_ATTR_PREFETCH_ROWS: ", err); |
|
188 |
} |
|
189 |
if (prefetchMem >= 0) { |
|
190 |
r = OCIAttrSet(sql, |
|
191 |
OCI_HTYPE_STMT, |
|
192 |
&prefetchMem, |
|
193 |
0, |
|
194 |
OCI_ATTR_PREFETCH_MEMORY, |
|
195 |
err); |
|
196 |
if (r != 0) |
|
197 |
qOraWarning("QOCIResultPrivate::setStatementAttributes:" |
|
198 |
" Couldn't set OCI_ATTR_PREFETCH_MEMORY: ", err); |
|
199 |
} |
|
200 |
} |
|
201 |
||
202 |
void QOCIResultPrivate::setCharset(OCIBind* hbnd) |
|
203 |
{ |
|
204 |
int r = 0; |
|
205 |
||
206 |
Q_ASSERT(hbnd); |
|
207 |
||
208 |
r = OCIAttrSet(hbnd, |
|
209 |
OCI_HTYPE_BIND, |
|
210 |
// this const cast is safe since OCI doesn't touch |
|
211 |
// the charset. |
|
212 |
const_cast<void *>(static_cast<const void *>(&qOraCharset)), |
|
213 |
0, |
|
214 |
OCI_ATTR_CHARSET_ID, |
|
215 |
err); |
|
216 |
if (r != 0) |
|
217 |
qOraWarning("QOCIResultPrivate::setCharset: Couldn't set OCI_ATTR_CHARSET_ID: ", err); |
|
218 |
} |
|
219 |
||
220 |
int QOCIResultPrivate::bindValue(OCIStmt *sql, OCIBind **hbnd, OCIError *err, int pos, |
|
221 |
const QVariant &val, dvoid *indPtr, ub2 *tmpSize, QList<QByteArray> &tmpStorage) |
|
222 |
{ |
|
223 |
int r = OCI_SUCCESS; |
|
224 |
void *data = const_cast<void *>(val.constData()); |
|
225 |
||
226 |
switch (val.type()) { |
|
227 |
case QVariant::ByteArray: |
|
228 |
r = OCIBindByPos(sql, hbnd, err, |
|
229 |
pos + 1, |
|
230 |
isOutValue(pos) |
|
231 |
? const_cast<char *>(reinterpret_cast<QByteArray *>(data)->constData()) |
|
232 |
: reinterpret_cast<QByteArray *>(data)->data(), |
|
233 |
reinterpret_cast<QByteArray *>(data)->size(), |
|
234 |
SQLT_BIN, indPtr, 0, 0, 0, 0, OCI_DEFAULT); |
|
235 |
break; |
|
236 |
case QVariant::Time: |
|
237 |
case QVariant::Date: |
|
238 |
case QVariant::DateTime: { |
|
239 |
QByteArray ba = qMakeOraDate(val.toDateTime()); |
|
240 |
r = OCIBindByPos(sql, hbnd, err, |
|
241 |
pos + 1, |
|
242 |
ba.data(), |
|
243 |
ba.size(), |
|
244 |
SQLT_DAT, indPtr, 0, 0, 0, 0, OCI_DEFAULT); |
|
245 |
tmpStorage.append(ba); |
|
246 |
break; } |
|
247 |
case QVariant::Int: |
|
248 |
r = OCIBindByPos(sql, hbnd, err, |
|
249 |
pos + 1, |
|
250 |
// if it's an out value, the data is already detached |
|
251 |
// so the const cast is safe. |
|
252 |
const_cast<void *>(data), |
|
253 |
sizeof(int), |
|
254 |
SQLT_INT, indPtr, 0, 0, 0, 0, OCI_DEFAULT); |
|
255 |
break; |
|
256 |
case QVariant::UInt: |
|
257 |
r = OCIBindByPos(sql, hbnd, err, |
|
258 |
pos + 1, |
|
259 |
// if it's an out value, the data is already detached |
|
260 |
// so the const cast is safe. |
|
261 |
const_cast<void *>(data), |
|
262 |
sizeof(uint), |
|
263 |
SQLT_UIN, indPtr, 0, 0, 0, 0, OCI_DEFAULT); |
|
264 |
break; |
|
265 |
case QVariant::Double: |
|
266 |
r = OCIBindByPos(sql, hbnd, err, |
|
267 |
pos + 1, |
|
268 |
// if it's an out value, the data is already detached |
|
269 |
// so the const cast is safe. |
|
270 |
const_cast<void *>(data), |
|
271 |
sizeof(double), |
|
272 |
SQLT_FLT, indPtr, 0, 0, 0, 0, OCI_DEFAULT); |
|
273 |
break; |
|
274 |
case QVariant::UserType: |
|
275 |
if (qVariantCanConvert<QOCIRowIdPointer>(val) && !isOutValue(pos)) { |
|
276 |
// use a const pointer to prevent a detach |
|
277 |
const QOCIRowIdPointer rptr = qVariantValue<QOCIRowIdPointer>(val); |
|
278 |
r = OCIBindByPos(sql, hbnd, err, |
|
279 |
pos + 1, |
|
280 |
// it's an IN value, so const_cast is ok |
|
281 |
const_cast<OCIRowid **>(&rptr->id), |
|
282 |
-1, |
|
283 |
SQLT_RDD, indPtr, 0, 0, 0, 0, OCI_DEFAULT); |
|
284 |
} else { |
|
285 |
qWarning("Unknown bind variable"); |
|
286 |
r = OCI_ERROR; |
|
287 |
} |
|
288 |
break; |
|
289 |
case QVariant::String: { |
|
290 |
const QString s = val.toString(); |
|
291 |
if (isBinaryValue(pos)) { |
|
292 |
r = OCIBindByPos(sql, hbnd, err, |
|
293 |
pos + 1, |
|
294 |
const_cast<ushort *>(s.utf16()), |
|
295 |
s.length() * sizeof(QChar), |
|
296 |
SQLT_LNG, indPtr, 0, 0, 0, 0, OCI_DEFAULT); |
|
297 |
break; |
|
298 |
} else if (!isOutValue(pos)) { |
|
299 |
// don't detach the string |
|
300 |
r = OCIBindByPos(sql, hbnd, err, |
|
301 |
pos + 1, |
|
302 |
// safe since oracle doesn't touch OUT values |
|
303 |
const_cast<ushort *>(s.utf16()), |
|
304 |
(s.length() + 1) * sizeof(QChar), |
|
305 |
SQLT_STR, indPtr, 0, 0, 0, 0, OCI_DEFAULT); |
|
306 |
if (r == OCI_SUCCESS) |
|
307 |
setCharset(*hbnd); |
|
308 |
break; |
|
309 |
} |
|
310 |
} // fall through for OUT values |
|
311 |
default: { |
|
312 |
const QString s = val.toString(); |
|
313 |
// create a deep-copy |
|
314 |
QByteArray ba(reinterpret_cast<const char *>(s.utf16()), (s.length() + 1) * sizeof(QChar)); |
|
315 |
if (isOutValue(pos)) { |
|
316 |
ba.reserve((s.capacity() + 1) * sizeof(QChar)); |
|
317 |
*tmpSize = ba.size(); |
|
318 |
r = OCIBindByPos(sql, hbnd, err, |
|
319 |
pos + 1, |
|
320 |
ba.data(), |
|
321 |
ba.capacity(), |
|
322 |
SQLT_STR, indPtr, tmpSize, 0, 0, 0, OCI_DEFAULT); |
|
323 |
} else { |
|
324 |
r = OCIBindByPos(sql, hbnd, err, |
|
325 |
pos + 1, |
|
326 |
ba.data(), |
|
327 |
ba.size(), |
|
328 |
SQLT_STR, indPtr, 0, 0, 0, 0, OCI_DEFAULT); |
|
329 |
} |
|
330 |
if (r == OCI_SUCCESS) |
|
331 |
setCharset(*hbnd); |
|
332 |
tmpStorage.append(ba); |
|
333 |
break; |
|
334 |
} // default case |
|
335 |
} // switch |
|
336 |
if (r != OCI_SUCCESS) |
|
337 |
qOraWarning("QOCIResultPrivate::bindValue:", err); |
|
338 |
return r; |
|
339 |
} |
|
340 |
||
341 |
int QOCIResultPrivate::bindValues(QVector<QVariant> &values, IndicatorArray &indicators, |
|
342 |
SizeArray &tmpSizes, QList<QByteArray> &tmpStorage) |
|
343 |
{ |
|
344 |
int r = OCI_SUCCESS; |
|
345 |
for (int i = 0; i < values.count(); ++i) { |
|
346 |
if (isOutValue(i)) |
|
347 |
values[i].detach(); |
|
348 |
const QVariant &val = values.at(i); |
|
349 |
||
350 |
OCIBind * hbnd = 0; // Oracle handles these automatically |
|
351 |
sb2 *indPtr = &indicators[i]; |
|
352 |
*indPtr = val.isNull() ? -1 : 0; |
|
353 |
||
354 |
bindValue(sql, &hbnd, err, i, val, indPtr, &tmpSizes[i], tmpStorage); |
|
355 |
} |
|
356 |
return r; |
|
357 |
} |
|
358 |
||
359 |
// will assign out value and remove its temp storage. |
|
360 |
static void qOraOutValue(QVariant &value, QList<QByteArray> &storage) |
|
361 |
{ |
|
362 |
switch (value.type()) { |
|
363 |
case QVariant::Time: |
|
364 |
value = qMakeDate(storage.takeFirst()).time(); |
|
365 |
break; |
|
366 |
case QVariant::Date: |
|
367 |
value = qMakeDate(storage.takeFirst()).date(); |
|
368 |
break; |
|
369 |
case QVariant::DateTime: |
|
370 |
value = qMakeDate(storage.takeFirst()); |
|
371 |
break; |
|
372 |
case QVariant::String: |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
373 |
value = QString( |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
374 |
reinterpret_cast<const QChar *>(storage.takeFirst().constData())); |
0 | 375 |
break; |
376 |
default: |
|
377 |
break; //nothing |
|
378 |
} |
|
379 |
} |
|
380 |
||
381 |
void QOCIResultPrivate::outValues(QVector<QVariant> &values, IndicatorArray &indicators, |
|
382 |
QList<QByteArray> &tmpStorage) |
|
383 |
{ |
|
384 |
for (int i = 0; i < values.count(); ++i) { |
|
385 |
||
386 |
if (!isOutValue(i)) |
|
387 |
continue; |
|
388 |
||
389 |
qOraOutValue(values[i], tmpStorage); |
|
390 |
||
391 |
QVariant::Type typ = values.at(i).type(); |
|
392 |
if (indicators[i] == -1) // NULL |
|
393 |
values[i] = QVariant(typ); |
|
394 |
else |
|
395 |
values[i] = QVariant(typ, values.at(i).constData()); |
|
396 |
} |
|
397 |
} |
|
398 |
||
399 |
||
400 |
struct QOCIDriverPrivate |
|
401 |
{ |
|
402 |
QOCIDriverPrivate(); |
|
403 |
||
404 |
OCIEnv *env; |
|
405 |
OCISvcCtx *svc; |
|
406 |
OCIServer *srvhp; |
|
407 |
OCISession *authp; |
|
408 |
OCIError *err; |
|
409 |
bool transaction; |
|
410 |
int serverVersion; |
|
411 |
ub4 prefetchRows; |
|
412 |
ub2 prefetchMem; |
|
413 |
QString user; |
|
414 |
||
415 |
void allocErrorHandle(); |
|
416 |
}; |
|
417 |
||
418 |
QOCIDriverPrivate::QOCIDriverPrivate() |
|
419 |
: env(0), svc(0), srvhp(0), authp(0), err(0), transaction(false), serverVersion(-1), |
|
420 |
prefetchRows(-1), prefetchMem(QOCI_PREFETCH_MEM) |
|
421 |
{ |
|
422 |
} |
|
423 |
||
424 |
void QOCIDriverPrivate::allocErrorHandle() |
|
425 |
{ |
|
426 |
int r = OCIHandleAlloc(env, |
|
427 |
reinterpret_cast<void **>(&err), |
|
428 |
OCI_HTYPE_ERROR, |
|
429 |
0, |
|
430 |
0); |
|
431 |
if (r != 0) |
|
432 |
qWarning("QOCIDriver: unable to allocate error handle"); |
|
433 |
} |
|
434 |
||
435 |
struct OraFieldInfo |
|
436 |
{ |
|
437 |
QString name; |
|
438 |
QVariant::Type type; |
|
439 |
ub1 oraIsNull; |
|
440 |
ub4 oraType; |
|
441 |
sb1 oraScale; |
|
442 |
ub4 oraLength; // size in bytes |
|
443 |
ub4 oraFieldLength; // amount of characters |
|
444 |
sb2 oraPrecision; |
|
445 |
}; |
|
446 |
||
447 |
QString qOraWarn(OCIError *err, int *errorCode) |
|
448 |
{ |
|
449 |
sb4 errcode; |
|
450 |
text errbuf[1024]; |
|
451 |
errbuf[0] = 0; |
|
452 |
errbuf[1] = 0; |
|
453 |
||
454 |
OCIErrorGet(err, |
|
455 |
1, |
|
456 |
0, |
|
457 |
&errcode, |
|
458 |
errbuf, |
|
459 |
sizeof(errbuf), |
|
460 |
OCI_HTYPE_ERROR); |
|
461 |
if (errorCode) |
|
462 |
*errorCode = errcode; |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
463 |
return QString(reinterpret_cast<const QChar *>(errbuf)); |
0 | 464 |
} |
465 |
||
466 |
void qOraWarning(const char* msg, OCIError *err) |
|
467 |
{ |
|
468 |
#ifdef QOCI_DEBUG |
|
469 |
qWarning("%s %s", msg, qPrintable(qOraWarn(err))); |
|
470 |
#else |
|
471 |
Q_UNUSED(msg); |
|
472 |
Q_UNUSED(err); |
|
473 |
#endif |
|
474 |
} |
|
475 |
||
476 |
static int qOraErrorNumber(OCIError *err) |
|
477 |
{ |
|
478 |
sb4 errcode; |
|
479 |
OCIErrorGet(err, |
|
480 |
1, |
|
481 |
0, |
|
482 |
&errcode, |
|
483 |
0, |
|
484 |
0, |
|
485 |
OCI_HTYPE_ERROR); |
|
486 |
return errcode; |
|
487 |
} |
|
488 |
||
489 |
QSqlError qMakeError(const QString& errString, QSqlError::ErrorType type, OCIError *err) |
|
490 |
{ |
|
491 |
int errorCode = 0; |
|
492 |
const QString oraErrorString = qOraWarn(err, &errorCode); |
|
493 |
return QSqlError(errString, oraErrorString, type, errorCode); |
|
494 |
} |
|
495 |
||
496 |
QVariant::Type qDecodeOCIType(const QString& ocitype, QSql::NumericalPrecisionPolicy precisionPolicy) |
|
497 |
{ |
|
498 |
QVariant::Type type = QVariant::Invalid; |
|
499 |
if (ocitype == QLatin1String("VARCHAR2") || ocitype == QLatin1String("VARCHAR") |
|
500 |
|| ocitype.startsWith(QLatin1String("INTERVAL")) |
|
501 |
|| ocitype == QLatin1String("CHAR") || ocitype == QLatin1String("NVARCHAR2") |
|
502 |
|| ocitype == QLatin1String("NCHAR")) |
|
503 |
type = QVariant::String; |
|
504 |
else if (ocitype == QLatin1String("NUMBER") |
|
505 |
|| ocitype == QLatin1String("FLOAT") |
|
506 |
|| ocitype == QLatin1String("BINARY_FLOAT") |
|
507 |
|| ocitype == QLatin1String("BINARY_DOUBLE")) { |
|
508 |
switch(precisionPolicy) { |
|
509 |
case QSql::LowPrecisionInt32: |
|
510 |
type = QVariant::Int; |
|
511 |
break; |
|
512 |
case QSql::LowPrecisionInt64: |
|
513 |
type = QVariant::LongLong; |
|
514 |
break; |
|
515 |
case QSql::LowPrecisionDouble: |
|
516 |
type = QVariant::Double; |
|
517 |
break; |
|
518 |
case QSql::HighPrecision: |
|
519 |
default: |
|
520 |
type = QVariant::String; |
|
521 |
break; |
|
522 |
} |
|
523 |
} |
|
524 |
else if (ocitype == QLatin1String("LONG") || ocitype == QLatin1String("NCLOB") |
|
525 |
|| ocitype == QLatin1String("CLOB")) |
|
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
526 |
type = QVariant::ByteArray; |
0 | 527 |
else if (ocitype == QLatin1String("RAW") || ocitype == QLatin1String("LONG RAW") |
528 |
|| ocitype == QLatin1String("ROWID") || ocitype == QLatin1String("BLOB") |
|
529 |
|| ocitype == QLatin1String("CFILE") || ocitype == QLatin1String("BFILE")) |
|
530 |
type = QVariant::ByteArray; |
|
531 |
else if (ocitype == QLatin1String("DATE") || ocitype.startsWith(QLatin1String("TIME"))) |
|
532 |
type = QVariant::DateTime; |
|
533 |
else if (ocitype == QLatin1String("UNDEFINED")) |
|
534 |
type = QVariant::Invalid; |
|
535 |
if (type == QVariant::Invalid) |
|
536 |
qWarning("qDecodeOCIType: unknown type: %s", ocitype.toLocal8Bit().constData()); |
|
537 |
return type; |
|
538 |
} |
|
539 |
||
540 |
QVariant::Type qDecodeOCIType(int ocitype, QSql::NumericalPrecisionPolicy precisionPolicy) |
|
541 |
{ |
|
542 |
QVariant::Type type = QVariant::Invalid; |
|
543 |
switch (ocitype) { |
|
544 |
case SQLT_STR: |
|
545 |
case SQLT_VST: |
|
546 |
case SQLT_CHR: |
|
547 |
case SQLT_AFC: |
|
548 |
case SQLT_VCS: |
|
549 |
case SQLT_AVC: |
|
550 |
case SQLT_RDD: |
|
551 |
case SQLT_LNG: |
|
552 |
#ifdef SQLT_INTERVAL_YM |
|
553 |
case SQLT_INTERVAL_YM: |
|
554 |
#endif |
|
555 |
#ifdef SQLT_INTERVAL_DS |
|
556 |
case SQLT_INTERVAL_DS: |
|
557 |
#endif |
|
558 |
type = QVariant::String; |
|
559 |
break; |
|
560 |
case SQLT_INT: |
|
561 |
type = QVariant::Int; |
|
562 |
break; |
|
563 |
case SQLT_FLT: |
|
564 |
case SQLT_NUM: |
|
565 |
case SQLT_VNU: |
|
566 |
case SQLT_UIN: |
|
567 |
switch(precisionPolicy) { |
|
568 |
case QSql::LowPrecisionInt32: |
|
569 |
type = QVariant::Int; |
|
570 |
break; |
|
571 |
case QSql::LowPrecisionInt64: |
|
572 |
type = QVariant::LongLong; |
|
573 |
break; |
|
574 |
case QSql::LowPrecisionDouble: |
|
575 |
type = QVariant::Double; |
|
576 |
break; |
|
577 |
case QSql::HighPrecision: |
|
578 |
default: |
|
579 |
type = QVariant::String; |
|
580 |
break; |
|
581 |
} |
|
582 |
break; |
|
583 |
case SQLT_VBI: |
|
584 |
case SQLT_BIN: |
|
585 |
case SQLT_LBI: |
|
586 |
case SQLT_LVC: |
|
587 |
case SQLT_LVB: |
|
588 |
case SQLT_BLOB: |
|
19
fcece45ef507
Revision: 201015
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
589 |
case SQLT_CLOB: |
0 | 590 |
case SQLT_FILE: |
591 |
case SQLT_NTY: |
|
592 |
case SQLT_REF: |
|
593 |
case SQLT_RID: |
|
594 |
type = QVariant::ByteArray; |
|
595 |
break; |
|
596 |
case SQLT_DAT: |
|
597 |
case SQLT_ODT: |
|
598 |
#ifdef SQLT_TIMESTAMP |
|
599 |
case SQLT_TIMESTAMP: |
|
600 |
case SQLT_TIMESTAMP_TZ: |
|
601 |
case SQLT_TIMESTAMP_LTZ: |
|
602 |
#endif |
|
603 |
type = QVariant::DateTime; |
|
604 |
break; |
|
605 |
default: |
|
606 |
type = QVariant::Invalid; |
|
607 |
qWarning("qDecodeOCIType: unknown OCI datatype: %d", ocitype); |
|
608 |
break; |
|
609 |
} |
|
610 |
return type; |
|
611 |
} |
|
612 |
||
613 |
static QSqlField qFromOraInf(const OraFieldInfo &ofi) |
|
614 |
{ |
|
615 |
QSqlField f(ofi.name, ofi.type); |
|
616 |
f.setRequired(ofi.oraIsNull == 0); |
|
617 |
||
618 |
if (ofi.type == QVariant::String && ofi.oraType != SQLT_NUM && ofi.oraType != SQLT_VNU) |
|
619 |
f.setLength(ofi.oraFieldLength); |
|
620 |
else |
|
621 |
f.setLength(ofi.oraPrecision == 0 ? 38 : int(ofi.oraPrecision)); |
|
622 |
||
623 |
f.setPrecision(ofi.oraScale); |
|
624 |
f.setSqlType(int(ofi.oraType)); |
|
625 |
return f; |
|
626 |
} |
|
627 |
||
628 |
/*! |
|
629 |
\internal |
|
630 |
||
631 |
Convert QDateTime to the internal Oracle DATE format NB! |
|
632 |
It does not handle BCE dates. |
|
633 |
*/ |
|
634 |
QByteArray qMakeOraDate(const QDateTime& dt) |
|
635 |
{ |
|
636 |
QByteArray ba; |
|
637 |
ba.resize(7); |
|
638 |
int year = dt.date().year(); |
|
639 |
ba[0]= (year / 100) + 100; // century |
|
640 |
ba[1]= (year % 100) + 100; // year |
|
641 |
ba[2]= dt.date().month(); |
|
642 |
ba[3]= dt.date().day(); |
|
643 |
ba[4]= dt.time().hour() + 1; |
|
644 |
ba[5]= dt.time().minute() + 1; |
|
645 |
ba[6]= dt.time().second() + 1; |
|
646 |
return ba; |
|
647 |
} |
|
648 |
||
649 |
QDateTime qMakeDate(const char* oraDate) |
|
650 |
{ |
|
651 |
int century = oraDate[0]; |
|
652 |
if(century >= 100){ |
|
653 |
int year = uchar(oraDate[1]); |
|
654 |
year = ((century-100)*100) + (year-100); |
|
655 |
int month = oraDate[2]; |
|
656 |
int day = oraDate[3]; |
|
657 |
int hour = oraDate[4] - 1; |
|
658 |
int min = oraDate[5] - 1; |
|
659 |
int sec = oraDate[6] - 1; |
|
660 |
return QDateTime(QDate(year,month,day), QTime(hour,min,sec)); |
|
661 |
} |
|
662 |
return QDateTime(); |
|
663 |
} |
|
664 |
||
665 |
class QOCICols |
|
666 |
{ |
|
667 |
public: |
|
668 |
QOCICols(int size, QOCIResultPrivate* dp); |
|
669 |
~QOCICols(); |
|
670 |
void setCharset(OCIDefine* dfn); |
|
671 |
int readPiecewise(QVector<QVariant> &values, int index = 0); |
|
672 |
int readLOBs(QVector<QVariant> &values, int index = 0); |
|
673 |
int fieldFromDefine(OCIDefine* d); |
|
674 |
void getValues(QVector<QVariant> &v, int index); |
|
675 |
inline int size() { return fieldInf.size(); } |
|
676 |
static bool execBatch(QOCIResultPrivate *d, QVector<QVariant> &boundValues, bool arrayBind); |
|
677 |
||
678 |
QSqlRecord rec; |
|
679 |
||
680 |
private: |
|
681 |
char* create(int position, int size); |
|
682 |
OCILobLocator ** createLobLocator(int position, OCIEnv* env); |
|
683 |
OraFieldInfo qMakeOraField(const QOCIResultPrivate* p, OCIParam* param) const; |
|
684 |
||
685 |
class OraFieldInf |
|
686 |
{ |
|
687 |
public: |
|
688 |
OraFieldInf(): data(0), len(0), ind(0), typ(QVariant::Invalid), oraType(0), def(0), lob(0) |
|
689 |
{} |
|
690 |
~OraFieldInf(); |
|
691 |
char *data; |
|
692 |
int len; |
|
693 |
sb2 ind; |
|
694 |
QVariant::Type typ; |
|
695 |
ub4 oraType; |
|
696 |
OCIDefine *def; |
|
697 |
OCILobLocator *lob; |
|
698 |
}; |
|
699 |
||
700 |
QVector<OraFieldInf> fieldInf; |
|
701 |
const QOCIResultPrivate *const d; |
|
702 |
}; |
|
703 |
||
704 |
QOCICols::OraFieldInf::~OraFieldInf() |
|
705 |
{ |
|
706 |
delete [] data; |
|
707 |
if (lob) { |
|
708 |
int r = OCIDescriptorFree(lob, OCI_DTYPE_LOB); |
|
709 |
if (r != 0) |
|
710 |
qWarning("QOCICols: Cannot free LOB descriptor"); |
|
711 |
} |
|
712 |
} |
|
713 |
||
714 |
QOCICols::QOCICols(int size, QOCIResultPrivate* dp) |
|
715 |
: fieldInf(size), d(dp) |
|
716 |
{ |
|
717 |
ub4 dataSize = 0; |
|
718 |
OCIDefine* dfn = 0; |
|
719 |
int r; |
|
720 |
||
721 |
OCIParam* param = 0; |
|
722 |
sb4 parmStatus = 0; |
|
723 |
ub4 count = 1; |
|
724 |
int idx = 0; |
|
725 |
parmStatus = OCIParamGet(d->sql, |
|
726 |
OCI_HTYPE_STMT, |
|
727 |
d->err, |
|
728 |
reinterpret_cast<void **>(¶m), |
|
729 |
count); |
|
730 |
||
731 |
while (parmStatus == OCI_SUCCESS) { |
|
732 |
OraFieldInfo ofi = qMakeOraField(d, param); |
|
733 |
if (ofi.oraType == SQLT_RDD) |
|
734 |
dataSize = 50; |
|
735 |
#ifdef SQLT_INTERVAL_YM |
|
736 |
#ifdef SQLT_INTERVAL_DS |
|
737 |
else if (ofi.oraType == SQLT_INTERVAL_YM || ofi.oraType == SQLT_INTERVAL_DS) |
|
738 |
// since we are binding interval datatype as string, |
|
739 |
// we are not interested in the number of bytes but characters. |
|
740 |
dataSize = 50; // magic number |
|
741 |
#endif //SQLT_INTERVAL_DS |
|
742 |
#endif //SQLT_INTERVAL_YM |
|
743 |
else if (ofi.oraType == SQLT_NUM || ofi.oraType == SQLT_VNU){ |
|
744 |
if (ofi.oraPrecision > 0) |
|
745 |
dataSize = (ofi.oraPrecision + 1) * sizeof(utext); |
|
746 |
else |
|
747 |
dataSize = (38 + 1) * sizeof(utext); |
|
748 |
} |
|
749 |
else |
|
750 |
dataSize = ofi.oraLength; |
|
751 |
||
752 |
fieldInf[idx].typ = ofi.type; |
|
753 |
fieldInf[idx].oraType = ofi.oraType; |
|
754 |
rec.append(qFromOraInf(ofi)); |
|
755 |
||
756 |
switch (ofi.type) { |
|
757 |
case QVariant::DateTime: |
|
758 |
r = OCIDefineByPos(d->sql, |
|
759 |
&dfn, |
|
760 |
d->err, |
|
761 |
count, |
|
762 |
create(idx, dataSize+1), |
|
763 |
dataSize+1, |
|
764 |
SQLT_DAT, |
|
765 |
&(fieldInf[idx].ind), |
|
766 |
0, 0, OCI_DEFAULT); |
|
767 |
break; |
|
768 |
case QVariant::Double: |
|
769 |
r = OCIDefineByPos(d->sql, |
|
770 |
&dfn, |
|
771 |
d->err, |
|
772 |
count, |
|
773 |
create(idx, sizeof(double) - 1), |
|
774 |
sizeof(double), |
|
775 |
SQLT_FLT, |
|
776 |
&(fieldInf[idx].ind), |
|
777 |
0, 0, OCI_DEFAULT); |
|
778 |
break; |
|
779 |
case QVariant::Int: |
|
780 |
r = OCIDefineByPos(d->sql, |
|
781 |
&dfn, |
|
782 |
d->err, |
|
783 |
count, |
|
784 |
create(idx, sizeof(qint32) - 1), |
|
785 |
sizeof(qint32), |
|
786 |
SQLT_INT, |
|
787 |
&(fieldInf[idx].ind), |
|
788 |
0, 0, OCI_DEFAULT); |
|
789 |
break; |
|
790 |
case QVariant::LongLong: |
|
791 |
r = OCIDefineByPos(d->sql, |
|
792 |
&dfn, |
|
793 |
d->err, |
|
794 |
count, |
|
795 |
create(idx, sizeof(OCINumber)), |
|
796 |
sizeof(OCINumber), |
|
797 |
SQLT_VNU, |
|
798 |
&(fieldInf[idx].ind), |
|
799 |
0, 0, OCI_DEFAULT); |
|
800 |
break; |
|
801 |
case QVariant::ByteArray: |
|
802 |
// RAW and LONG RAW fields can't be bound to LOB locators |
|
803 |
if (ofi.oraType == SQLT_BIN) { |
|
804 |
// qDebug("binding SQLT_BIN"); |
|
805 |
r = OCIDefineByPos(d->sql, |
|
806 |
&dfn, |
|
807 |
d->err, |
|
808 |
count, |
|
809 |
create(idx, dataSize), |
|
810 |
dataSize, |
|
811 |
SQLT_BIN, |
|
812 |
&(fieldInf[idx].ind), |
|
813 |
0, 0, OCI_DYNAMIC_FETCH); |
|
814 |
} else if (ofi.oraType == SQLT_LBI) { |
|
815 |
// qDebug("binding SQLT_LBI"); |
|
816 |
r = OCIDefineByPos(d->sql, |
|
817 |
&dfn, |
|
818 |
d->err, |
|
819 |
count, |
|
820 |
0, |
|
821 |
SB4MAXVAL, |
|
822 |
SQLT_LBI, |
|
823 |
&(fieldInf[idx].ind), |
|
824 |
0, 0, OCI_DYNAMIC_FETCH); |
|
825 |
} else if (ofi.oraType == SQLT_CLOB) { |
|
826 |
r = OCIDefineByPos(d->sql, |
|
827 |
&dfn, |
|
828 |
d->err, |
|
829 |
count, |
|
830 |
createLobLocator(idx, d->env), |
|
831 |
-1, |
|
832 |
SQLT_CLOB, |
|
833 |
&(fieldInf[idx].ind), |
|
834 |
0, 0, OCI_DEFAULT); |
|
835 |
} else { |
|
836 |
// qDebug("binding SQLT_BLOB"); |
|
837 |
r = OCIDefineByPos(d->sql, |
|
838 |
&dfn, |
|
839 |
d->err, |
|
840 |
count, |
|
841 |
createLobLocator(idx, d->env), |
|
842 |
-1, |
|
843 |
SQLT_BLOB, |
|
844 |
&(fieldInf[idx].ind), |
|
845 |
0, 0, OCI_DEFAULT); |
|
846 |
} |
|
847 |
break; |
|
848 |
case QVariant::String: |
|
849 |
if (ofi.oraType == SQLT_LNG) { |
|
850 |
r = OCIDefineByPos(d->sql, |
|
851 |
&dfn, |
|
852 |
d->err, |
|
853 |
count, |
|
854 |
0, |
|
855 |
SB4MAXVAL, |
|
856 |
SQLT_LNG, |
|
857 |
&(fieldInf[idx].ind), |
|
858 |
0, 0, OCI_DYNAMIC_FETCH); |
|
859 |
} else { |
|
860 |
dataSize += dataSize + sizeof(QChar); |
|
861 |
//qDebug("OCIDefineByPosStr(%d): %d", count, dataSize); |
|
862 |
r = OCIDefineByPos(d->sql, |
|
863 |
&dfn, |
|
864 |
d->err, |
|
865 |
count, |
|
866 |
create(idx, dataSize), |
|
867 |
dataSize, |
|
868 |
SQLT_STR, |
|
869 |
&(fieldInf[idx].ind), |
|
870 |
0, 0, OCI_DEFAULT); |
|
871 |
if (r == 0) |
|
872 |
setCharset(dfn); |
|
873 |
} |
|
874 |
break; |
|
875 |
default: |
|
876 |
// this should make enough space even with character encoding |
|
877 |
dataSize = (dataSize + 1) * sizeof(utext) ; |
|
878 |
//qDebug("OCIDefineByPosDef(%d): %d", count, dataSize); |
|
879 |
r = OCIDefineByPos(d->sql, |
|
880 |
&dfn, |
|
881 |
d->err, |
|
882 |
count, |
|
883 |
create(idx, dataSize), |
|
884 |
dataSize+1, |
|
885 |
SQLT_STR, |
|
886 |
&(fieldInf[idx].ind), |
|
887 |
0, 0, OCI_DEFAULT); |
|
888 |
break; |
|
889 |
} |
|
890 |
if (r != 0) |
|
891 |
qOraWarning("QOCICols::bind:", d->err); |
|
892 |
fieldInf[idx].def = dfn; |
|
893 |
++count; |
|
894 |
++idx; |
|
895 |
parmStatus = OCIParamGet(d->sql, |
|
896 |
OCI_HTYPE_STMT, |
|
897 |
d->err, |
|
898 |
reinterpret_cast<void **>(¶m), |
|
899 |
count); |
|
900 |
} |
|
901 |
} |
|
902 |
||
903 |
QOCICols::~QOCICols() |
|
904 |
{ |
|
905 |
} |
|
906 |
||
907 |
char* QOCICols::create(int position, int size) |
|
908 |
{ |
|
909 |
char* c = new char[size+1]; |
|
910 |
// Oracle may not fill fixed width fields |
|
911 |
memset(c, 0, size+1); |
|
912 |
fieldInf[position].data = c; |
|
913 |
fieldInf[position].len = size; |
|
914 |
return c; |
|
915 |
} |
|
916 |
||
917 |
OCILobLocator **QOCICols::createLobLocator(int position, OCIEnv* env) |
|
918 |
{ |
|
919 |
OCILobLocator *& lob = fieldInf[position].lob; |
|
920 |
int r = OCIDescriptorAlloc(env, |
|
921 |
reinterpret_cast<void **>(&lob), |
|
922 |
OCI_DTYPE_LOB, |
|
923 |
0, |
|
924 |
0); |
|
925 |
if (r != 0) { |
|
926 |
qWarning("QOCICols: Cannot create LOB locator"); |
|
927 |
lob = 0; |
|
928 |
} |
|
929 |
return &lob; |
|
930 |
} |
|
931 |
||
932 |
void QOCICols::setCharset(OCIDefine* dfn) |
|
933 |
{ |
|
934 |
int r = 0; |
|
935 |
||
936 |
Q_ASSERT(dfn); |
|
937 |
||
938 |
r = OCIAttrSet(dfn, |
|
939 |
OCI_HTYPE_DEFINE, |
|
940 |
// this const cast is safe since OCI doesn't touch |
|
941 |
// the charset. |
|
942 |
const_cast<void *>(static_cast<const void *>(&qOraCharset)), |
|
943 |
0, |
|
944 |
OCI_ATTR_CHARSET_ID, |
|
945 |
d->err); |
|
946 |
if (r != 0) |
|
947 |
qOraWarning("QOCICols::setCharset: Couldn't set OCI_ATTR_CHARSET_ID: ", d->err); |
|
948 |
} |
|
949 |
||
950 |
int QOCICols::readPiecewise(QVector<QVariant> &values, int index) |
|
951 |
{ |
|
952 |
OCIDefine* dfn; |
|
953 |
ub4 typep; |
|
954 |
ub1 in_outp; |
|
955 |
ub4 iterp; |
|
956 |
ub4 idxp; |
|
957 |
ub1 piecep; |
|
958 |
sword status; |
|
959 |
text col [QOCI_DYNAMIC_CHUNK_SIZE+1]; |
|
960 |
int fieldNum = -1; |
|
961 |
int r = 0; |
|
962 |
bool nullField; |
|
963 |
||
964 |
do { |
|
965 |
r = OCIStmtGetPieceInfo(d->sql, d->err, reinterpret_cast<void **>(&dfn), &typep, |
|
966 |
&in_outp, &iterp, &idxp, &piecep); |
|
967 |
if (r != OCI_SUCCESS) |
|
968 |
qOraWarning("OCIResultPrivate::readPiecewise: unable to get piece info:", d->err); |
|
969 |
fieldNum = fieldFromDefine(dfn); |
|
970 |
bool isStringField = fieldInf.at(fieldNum).oraType == SQLT_LNG; |
|
971 |
ub4 chunkSize = QOCI_DYNAMIC_CHUNK_SIZE; |
|
972 |
nullField = false; |
|
973 |
r = OCIStmtSetPieceInfo(dfn, OCI_HTYPE_DEFINE, |
|
974 |
d->err, col, |
|
975 |
&chunkSize, piecep, NULL, NULL); |
|
976 |
if (r != OCI_SUCCESS) |
|
977 |
qOraWarning("OCIResultPrivate::readPiecewise: unable to set piece info:", d->err); |
|
978 |
status = OCIStmtFetch (d->sql, d->err, 1, OCI_FETCH_NEXT, OCI_DEFAULT); |
|
979 |
if (status == -1) { |
|
980 |
sb4 errcode; |
|
981 |
OCIErrorGet(d->err, 1, 0, &errcode, 0, 0,OCI_HTYPE_ERROR); |
|
982 |
switch (errcode) { |
|
983 |
case 1405: /* NULL */ |
|
984 |
nullField = true; |
|
985 |
break; |
|
986 |
default: |
|
987 |
qOraWarning("OCIResultPrivate::readPiecewise: unable to fetch next:", d->err); |
|
988 |
break; |
|
989 |
} |
|
990 |
} |
|
991 |
if (status == OCI_NO_DATA) |
|
992 |
break; |
|
993 |
if (nullField || !chunkSize) { |
|
994 |
fieldInf[fieldNum].ind = -1; |
|
995 |
} else { |
|
996 |
if (isStringField) { |
|
997 |
QString str = values.at(fieldNum + index).toString(); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
998 |
str += QString(reinterpret_cast<const QChar *>(col), chunkSize / 2); |
0 | 999 |
values[fieldNum + index] = str; |
1000 |
fieldInf[fieldNum].ind = 0; |
|
1001 |
} else { |
|
1002 |
QByteArray ba = values.at(fieldNum + index).toByteArray(); |
|
1003 |
int sz = ba.size(); |
|
1004 |
ba.resize(sz + chunkSize); |
|
1005 |
memcpy(ba.data() + sz, reinterpret_cast<char *>(col), chunkSize); |
|
1006 |
values[fieldNum + index] = ba; |
|
1007 |
fieldInf[fieldNum].ind = 0; |
|
1008 |
} |
|
1009 |
} |
|
1010 |
} while (status == OCI_SUCCESS_WITH_INFO || status == OCI_NEED_DATA); |
|
1011 |
return r; |
|
1012 |
} |
|
1013 |
||
1014 |
OraFieldInfo QOCICols::qMakeOraField(const QOCIResultPrivate* p, OCIParam* param) const |
|
1015 |
{ |
|
1016 |
OraFieldInfo ofi; |
|
1017 |
ub2 colType(0); |
|
1018 |
text *colName = 0; |
|
1019 |
ub4 colNameLen(0); |
|
1020 |
sb1 colScale(0); |
|
1021 |
ub2 colLength(0); |
|
1022 |
ub2 colFieldLength(0); |
|
1023 |
sb2 colPrecision(0); |
|
1024 |
ub1 colIsNull(0); |
|
1025 |
int r(0); |
|
1026 |
QVariant::Type type(QVariant::Invalid); |
|
1027 |
||
1028 |
r = OCIAttrGet(param, |
|
1029 |
OCI_DTYPE_PARAM, |
|
1030 |
&colType, |
|
1031 |
0, |
|
1032 |
OCI_ATTR_DATA_TYPE, |
|
1033 |
p->err); |
|
1034 |
if (r != 0) |
|
1035 |
qOraWarning("qMakeOraField:", p->err); |
|
1036 |
||
1037 |
r = OCIAttrGet(param, |
|
1038 |
OCI_DTYPE_PARAM, |
|
1039 |
&colName, |
|
1040 |
&colNameLen, |
|
1041 |
OCI_ATTR_NAME, |
|
1042 |
p->err); |
|
1043 |
if (r != 0) |
|
1044 |
qOraWarning("qMakeOraField:", p->err); |
|
1045 |
||
1046 |
r = OCIAttrGet(param, |
|
1047 |
OCI_DTYPE_PARAM, |
|
1048 |
&colLength, |
|
1049 |
0, |
|
1050 |
OCI_ATTR_DATA_SIZE, /* in bytes */ |
|
1051 |
p->err); |
|
1052 |
if (r != 0) |
|
1053 |
qOraWarning("qMakeOraField:", p->err); |
|
1054 |
||
1055 |
#ifdef OCI_ATTR_CHAR_SIZE |
|
1056 |
r = OCIAttrGet(param, |
|
1057 |
OCI_DTYPE_PARAM, |
|
1058 |
&colFieldLength, |
|
1059 |
0, |
|
1060 |
OCI_ATTR_CHAR_SIZE, |
|
1061 |
p->err); |
|
1062 |
if (r != 0) |
|
1063 |
qOraWarning("qMakeOraField:", p->err); |
|
1064 |
#else |
|
1065 |
// for Oracle8. |
|
1066 |
colFieldLength = colLength; |
|
1067 |
#endif |
|
1068 |
||
1069 |
r = OCIAttrGet(param, |
|
1070 |
OCI_DTYPE_PARAM, |
|
1071 |
&colPrecision, |
|
1072 |
0, |
|
1073 |
OCI_ATTR_PRECISION, |
|
1074 |
p->err); |
|
1075 |
if (r != 0) |
|
1076 |
qOraWarning("qMakeOraField:", p->err); |
|
1077 |
||
1078 |
r = OCIAttrGet(param, |
|
1079 |
OCI_DTYPE_PARAM, |
|
1080 |
&colScale, |
|
1081 |
0, |
|
1082 |
OCI_ATTR_SCALE, |
|
1083 |
p->err); |
|
1084 |
if (r != 0) |
|
1085 |
qOraWarning("qMakeOraField:", p->err); |
|
1086 |
r = OCIAttrGet(param, |
|
1087 |
OCI_DTYPE_PARAM, |
|
1088 |
&colType, |
|
1089 |
0, |
|
1090 |
OCI_ATTR_DATA_TYPE, |
|
1091 |
p->err); |
|
1092 |
if (r != 0) |
|
1093 |
qOraWarning("qMakeOraField:", p->err); |
|
1094 |
r = OCIAttrGet(param, |
|
1095 |
OCI_DTYPE_PARAM, |
|
1096 |
&colIsNull, |
|
1097 |
0, |
|
1098 |
OCI_ATTR_IS_NULL, |
|
1099 |
p->err); |
|
1100 |
if (r != 0) |
|
1101 |
qOraWarning("qMakeOraField:", p->err); |
|
1102 |
||
1103 |
type = qDecodeOCIType(colType, p->q->numericalPrecisionPolicy()); |
|
1104 |
||
1105 |
if (type == QVariant::Int) { |
|
1106 |
if (colLength == 22 && colPrecision == 0 && colScale == 0) |
|
1107 |
type = QVariant::String; |
|
1108 |
if (colScale > 0) |
|
1109 |
type = QVariant::String; |
|
1110 |
} |
|
1111 |
||
1112 |
// bind as double if the precision policy asks for it |
|
1113 |
if (((colType == SQLT_FLT) || (colType == SQLT_NUM)) |
|
1114 |
&& (p->q->numericalPrecisionPolicy() == QSql::LowPrecisionDouble)) { |
|
1115 |
type = QVariant::Double; |
|
1116 |
} |
|
1117 |
||
1118 |
// bind as int32 or int64 if the precision policy asks for it |
|
1119 |
if ((colType == SQLT_NUM) || (colType == SQLT_VNU) || (colType == SQLT_UIN) |
|
1120 |
|| (colType == SQLT_INT)) { |
|
1121 |
if (p->q->numericalPrecisionPolicy() == QSql::LowPrecisionInt64) |
|
1122 |
type = QVariant::LongLong; |
|
1123 |
else if (p->q->numericalPrecisionPolicy() == QSql::LowPrecisionInt32) |
|
1124 |
type = QVariant::Int; |
|
1125 |
} |
|
1126 |
||
1127 |
if (colType == SQLT_BLOB) |
|
1128 |
colLength = 0; |
|
1129 |
||
1130 |
// colNameLen is length in bytes |
|
1131 |
ofi.name = QString(reinterpret_cast<const QChar*>(colName), colNameLen / 2); |
|
1132 |
ofi.type = type; |
|
1133 |
ofi.oraType = colType; |
|
1134 |
ofi.oraFieldLength = colFieldLength; |
|
1135 |
ofi.oraLength = colLength; |
|
1136 |
ofi.oraScale = colScale; |
|
1137 |
ofi.oraPrecision = colPrecision; |
|
1138 |
ofi.oraIsNull = colIsNull; |
|
1139 |
||
1140 |
return ofi; |
|
1141 |
} |
|
1142 |
||
1143 |
struct QOCIBatchColumn |
|
1144 |
{ |
|
1145 |
inline QOCIBatchColumn() |
|
1146 |
: bindh(0), bindAs(0), maxLen(0), recordCount(0), |
|
1147 |
data(0), lengths(0), indicators(0), maxarr_len(0), curelep(0) {} |
|
1148 |
||
1149 |
OCIBind* bindh; |
|
1150 |
ub2 bindAs; |
|
1151 |
ub4 maxLen; |
|
1152 |
ub4 recordCount; |
|
1153 |
char* data; |
|
1154 |
ub2* lengths; |
|
1155 |
sb2* indicators; |
|
1156 |
ub4 maxarr_len; |
|
1157 |
ub4 curelep; |
|
1158 |
}; |
|
1159 |
||
1160 |
struct QOCIBatchCleanupHandler |
|
1161 |
{ |
|
1162 |
inline QOCIBatchCleanupHandler(QVector<QOCIBatchColumn> &columns) |
|
1163 |
: col(columns) {} |
|
1164 |
||
1165 |
~QOCIBatchCleanupHandler() |
|
1166 |
{ |
|
1167 |
// deleting storage, length and indicator arrays |
|
1168 |
for ( int j = 0; j < col.count(); ++j){ |
|
1169 |
delete[] col[j].lengths; |
|
1170 |
delete[] col[j].indicators; |
|
1171 |
delete[] col[j].data; |
|
1172 |
} |
|
1173 |
} |
|
1174 |
||
1175 |
QVector<QOCIBatchColumn> &col; |
|
1176 |
}; |
|
1177 |
||
1178 |
bool QOCICols::execBatch(QOCIResultPrivate *d, QVector<QVariant> &boundValues, bool arrayBind) |
|
1179 |
{ |
|
1180 |
int columnCount = boundValues.count(); |
|
1181 |
if (boundValues.isEmpty() || columnCount == 0) |
|
1182 |
return false; |
|
1183 |
||
1184 |
#ifdef QOCI_DEBUG |
|
1185 |
qDebug() << "columnCount:" << columnCount << boundValues; |
|
1186 |
#endif |
|
1187 |
||
1188 |
int i; |
|
1189 |
sword r; |
|
1190 |
||
1191 |
QVarLengthArray<QVariant::Type> fieldTypes; |
|
1192 |
for (i = 0; i < columnCount; ++i) { |
|
1193 |
QVariant::Type tp = boundValues.at(i).type(); |
|
1194 |
fieldTypes.append(tp == QVariant::List ? boundValues.at(i).toList().value(0).type() |
|
1195 |
: tp); |
|
1196 |
} |
|
1197 |
||
1198 |
QList<QByteArray> tmpStorage; |
|
1199 |
SizeArray tmpSizes(columnCount); |
|
1200 |
QVector<QOCIBatchColumn> columns(columnCount); |
|
1201 |
QOCIBatchCleanupHandler cleaner(columns); |
|
1202 |
||
1203 |
// figuring out buffer sizes |
|
1204 |
for (i = 0; i < columnCount; ++i) { |
|
1205 |
||
1206 |
if (boundValues.at(i).type() != QVariant::List) { |
|
1207 |
||
1208 |
// not a list - create a deep-copy of the single value |
|
1209 |
QOCIBatchColumn &singleCol = columns[i]; |
|
1210 |
singleCol.indicators = new sb2[1]; |
|
1211 |
*singleCol.indicators = boundValues.at(i).isNull() ? -1 : 0; |
|
1212 |
||
1213 |
r = d->bindValue(d->sql, &singleCol.bindh, d->err, i, |
|
1214 |
boundValues.at(i), singleCol.indicators, &tmpSizes[i], tmpStorage); |
|
1215 |
||
1216 |
if (r != OCI_SUCCESS && r != OCI_SUCCESS_WITH_INFO) { |
|
1217 |
qOraWarning("QOCIPrivate::execBatch: unable to bind column:", d->err); |
|
1218 |
d->q->setLastError(qMakeError(QCoreApplication::translate("QOCIResult", |
|
1219 |
"Unable to bind column for batch execute"), |
|
1220 |
QSqlError::StatementError, d->err)); |
|
1221 |
return false; |
|
1222 |
} |
|
1223 |
continue; |
|
1224 |
} |
|
1225 |
||
1226 |
QOCIBatchColumn &col = columns[i]; |
|
1227 |
col.recordCount = boundValues.at(i).toList().count(); |
|
1228 |
||
1229 |
col.lengths = new ub2[col.recordCount]; |
|
1230 |
col.indicators = new sb2[col.recordCount]; |
|
1231 |
col.maxarr_len = col.recordCount; |
|
1232 |
col.curelep = col.recordCount; |
|
1233 |
||
1234 |
switch (fieldTypes[i]) { |
|
1235 |
case QVariant::Time: |
|
1236 |
case QVariant::Date: |
|
1237 |
case QVariant::DateTime: |
|
1238 |
col.bindAs = SQLT_DAT; |
|
1239 |
col.maxLen = 7; |
|
1240 |
break; |
|
1241 |
||
1242 |
case QVariant::Int: |
|
1243 |
col.bindAs = SQLT_INT; |
|
1244 |
col.maxLen = sizeof(int); |
|
1245 |
break; |
|
1246 |
||
1247 |
case QVariant::UInt: |
|
1248 |
col.bindAs = SQLT_UIN; |
|
1249 |
col.maxLen = sizeof(uint); |
|
1250 |
break; |
|
1251 |
||
1252 |
case QVariant::Double: |
|
1253 |
col.bindAs = SQLT_FLT; |
|
1254 |
col.maxLen = sizeof(double); |
|
1255 |
break; |
|
1256 |
||
1257 |
case QVariant::UserType: |
|
1258 |
col.bindAs = SQLT_RDD; |
|
1259 |
col.maxLen = sizeof(OCIRowid*); |
|
1260 |
break; |
|
1261 |
||
1262 |
case QVariant::String: { |
|
1263 |
col.bindAs = SQLT_STR; |
|
1264 |
for (uint j = 0; j < col.recordCount; ++j) { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1265 |
uint len; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1266 |
if(d->isOutValue(i)) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1267 |
len = boundValues.at(i).toList().at(j).toString().capacity() + 1; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1268 |
else |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1269 |
len = boundValues.at(i).toList().at(j).toString().length() + 1; |
0 | 1270 |
if (len > col.maxLen) |
1271 |
col.maxLen = len; |
|
1272 |
} |
|
1273 |
col.maxLen *= sizeof(QChar); |
|
1274 |
break; } |
|
1275 |
||
1276 |
case QVariant::ByteArray: |
|
1277 |
default: { |
|
1278 |
col.bindAs = SQLT_LBI; |
|
1279 |
for (uint j = 0; j < col.recordCount; ++j) { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1280 |
if(d->isOutValue(i)) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1281 |
col.lengths[j] = boundValues.at(i).toList().at(j).toByteArray().capacity(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1282 |
else |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1283 |
col.lengths[j] = boundValues.at(i).toList().at(j).toByteArray().size(); |
0 | 1284 |
if (col.lengths[j] > col.maxLen) |
1285 |
col.maxLen = col.lengths[j]; |
|
1286 |
} |
|
1287 |
break; } |
|
1288 |
} |
|
1289 |
||
1290 |
col.data = new char[col.maxLen * col.recordCount]; |
|
1291 |
memset(col.data, 0, col.maxLen * col.recordCount); |
|
1292 |
||
1293 |
// we may now populate column with data |
|
1294 |
for (uint row = 0; row < col.recordCount; ++row) { |
|
1295 |
const QVariant &val = boundValues.at(i).toList().at(row); |
|
1296 |
||
1297 |
if (val.isNull()){ |
|
1298 |
columns[i].indicators[row] = -1; |
|
1299 |
columns[i].lengths[row] = 0; |
|
1300 |
} else { |
|
1301 |
columns[i].indicators[row] = 0; |
|
1302 |
char *dataPtr = columns[i].data + (columns[i].maxLen * row); |
|
1303 |
switch (fieldTypes[i]) { |
|
1304 |
case QVariant::Time: |
|
1305 |
case QVariant::Date: |
|
1306 |
case QVariant::DateTime:{ |
|
1307 |
columns[i].lengths[row] = columns[i].maxLen; |
|
1308 |
const QByteArray ba = qMakeOraDate(val.toDateTime()); |
|
1309 |
Q_ASSERT(ba.size() == int(columns[i].maxLen)); |
|
1310 |
memcpy(dataPtr, ba.constData(), columns[i].maxLen); |
|
1311 |
break; |
|
1312 |
} |
|
1313 |
case QVariant::Int: |
|
1314 |
columns[i].lengths[row] = columns[i].maxLen; |
|
1315 |
*reinterpret_cast<int*>(dataPtr) = val.toInt(); |
|
1316 |
break; |
|
1317 |
||
1318 |
case QVariant::UInt: |
|
1319 |
columns[i].lengths[row] = columns[i].maxLen; |
|
1320 |
*reinterpret_cast<uint*>(dataPtr) = val.toUInt(); |
|
1321 |
break; |
|
1322 |
||
1323 |
case QVariant::Double: |
|
1324 |
columns[i].lengths[row] = columns[i].maxLen; |
|
1325 |
*reinterpret_cast<double*>(dataPtr) = val.toDouble(); |
|
1326 |
break; |
|
1327 |
||
1328 |
case QVariant::String: { |
|
1329 |
const QString s = val.toString(); |
|
1330 |
columns[i].lengths[row] = (s.length() + 1) * sizeof(QChar); |
|
1331 |
memcpy(dataPtr, s.utf16(), columns[i].lengths[row]); |
|
1332 |
break; |
|
1333 |
} |
|
1334 |
case QVariant::UserType: |
|
1335 |
if (qVariantCanConvert<QOCIRowIdPointer>(val)) { |
|
1336 |
const QOCIRowIdPointer rptr = qVariantValue<QOCIRowIdPointer>(val); |
|
1337 |
*reinterpret_cast<OCIRowid**>(dataPtr) = rptr->id; |
|
1338 |
columns[i].lengths[row] = 0; |
|
1339 |
break; |
|
1340 |
} |
|
1341 |
case QVariant::ByteArray: |
|
1342 |
default: { |
|
1343 |
const QByteArray ba = val.toByteArray(); |
|
1344 |
columns[i].lengths[row] = ba.size(); |
|
1345 |
memcpy(dataPtr, ba.constData(), ba.size()); |
|
1346 |
break; |
|
1347 |
} |
|
1348 |
} |
|
1349 |
} |
|
1350 |
} |
|
1351 |
||
1352 |
QOCIBatchColumn &bindColumn = columns[i]; |
|
1353 |
||
1354 |
#ifdef QOCI_DEBUG |
|
1355 |
qDebug("OCIBindByPos(%p, %p, %p, %d, %p, %d, %d, %p, %p, 0, %d, %p, OCI_DEFAULT)", |
|
1356 |
d->sql, &bindColumn.bindh, d->err, i + 1, bindColumn.data, |
|
1357 |
bindColumn.maxLen, bindColumn.bindAs, bindColumn.indicators, bindColumn.lengths, |
|
1358 |
arrayBind ? bindColumn.maxarr_len : 0, arrayBind ? &bindColumn.curelep : 0); |
|
1359 |
||
1360 |
for (int ii = 0; ii < (int)bindColumn.recordCount; ++ii) { |
|
1361 |
qDebug(" record %d: indicator %d, length %d", ii, bindColumn.indicators[ii], |
|
1362 |
bindColumn.lengths[ii]); |
|
1363 |
} |
|
1364 |
#endif |
|
1365 |
||
1366 |
||
1367 |
// binding the column |
|
1368 |
r = OCIBindByPos( |
|
1369 |
d->sql, &bindColumn.bindh, d->err, i + 1, |
|
1370 |
bindColumn.data, |
|
1371 |
bindColumn.maxLen, |
|
1372 |
bindColumn.bindAs, |
|
1373 |
bindColumn.indicators, |
|
1374 |
bindColumn.lengths, |
|
1375 |
0, |
|
1376 |
arrayBind ? bindColumn.maxarr_len : 0, |
|
1377 |
arrayBind ? &bindColumn.curelep : 0, |
|
1378 |
OCI_DEFAULT); |
|
1379 |
||
1380 |
#ifdef QOCI_DEBUG |
|
1381 |
qDebug("After OCIBindByPos: r = %d, bindh = %p", r, bindColumn.bindh); |
|
1382 |
#endif |
|
1383 |
||
1384 |
if (r != OCI_SUCCESS && r != OCI_SUCCESS_WITH_INFO) { |
|
1385 |
qOraWarning("QOCIPrivate::execBatch: unable to bind column:", d->err); |
|
1386 |
d->q->setLastError(qMakeError(QCoreApplication::translate("QOCIResult", |
|
1387 |
"Unable to bind column for batch execute"), |
|
1388 |
QSqlError::StatementError, d->err)); |
|
1389 |
return false; |
|
1390 |
} |
|
1391 |
||
1392 |
r = OCIBindArrayOfStruct ( |
|
1393 |
columns[i].bindh, d->err, |
|
1394 |
columns[i].maxLen, |
|
1395 |
sizeof(columns[i].indicators[0]), |
|
1396 |
sizeof(columns[i].lengths[0]), |
|
1397 |
0); |
|
1398 |
||
1399 |
if (r != OCI_SUCCESS && r != OCI_SUCCESS_WITH_INFO) { |
|
1400 |
qOraWarning("QOCIPrivate::execBatch: unable to bind column:", d->err); |
|
1401 |
d->q->setLastError(qMakeError(QCoreApplication::translate("QOCIResult", |
|
1402 |
"Unable to bind column for batch execute"), |
|
1403 |
QSqlError::StatementError, d->err)); |
|
1404 |
return false; |
|
1405 |
} |
|
1406 |
} |
|
1407 |
||
1408 |
//finaly we can execute |
|
1409 |
r = OCIStmtExecute(d->svc, d->sql, d->err, |
|
1410 |
arrayBind ? 1 : columns[0].recordCount, |
|
1411 |
0, NULL, NULL, |
|
1412 |
d->transaction ? OCI_DEFAULT : OCI_COMMIT_ON_SUCCESS); |
|
1413 |
||
1414 |
if (r != OCI_SUCCESS && r != OCI_SUCCESS_WITH_INFO) { |
|
1415 |
qOraWarning("QOCIPrivate::execBatch: unable to execute batch statement:", d->err); |
|
1416 |
d->q->setLastError(qMakeError(QCoreApplication::translate("QOCIResult", |
|
1417 |
"Unable to execute batch statement"), |
|
1418 |
QSqlError::StatementError, d->err)); |
|
1419 |
return false; |
|
1420 |
} |
|
1421 |
||
1422 |
// for out parameters we copy data back to value vector |
|
1423 |
for (i = 0; i < columnCount; ++i) { |
|
1424 |
||
1425 |
if (!d->isOutValue(i)) |
|
1426 |
continue; |
|
1427 |
||
1428 |
QVariant::Type tp = boundValues.at(i).type(); |
|
1429 |
if (tp != QVariant::List) { |
|
1430 |
qOraOutValue(boundValues[i], tmpStorage); |
|
1431 |
if (*columns[i].indicators == -1) |
|
1432 |
boundValues[i] = QVariant(tp); |
|
1433 |
continue; |
|
1434 |
} |
|
1435 |
||
1436 |
QVariantList *list = static_cast<QVariantList *>(const_cast<void*>(boundValues.at(i).data())); |
|
1437 |
||
1438 |
char* data = columns[i].data; |
|
1439 |
for (uint r = 0; r < columns[i].recordCount; ++r){ |
|
1440 |
||
1441 |
if (columns[i].indicators[r] == -1) { |
|
1442 |
(*list)[r] = QVariant(); |
|
1443 |
continue; |
|
1444 |
} |
|
1445 |
||
1446 |
switch(columns[i].bindAs) { |
|
1447 |
||
1448 |
case SQLT_DAT: |
|
1449 |
(*list)[r] = qMakeDate(data + r * columns[i].maxLen); |
|
1450 |
break; |
|
1451 |
||
1452 |
case SQLT_INT: |
|
1453 |
(*list)[r] = *reinterpret_cast<int*>(data + r * columns[i].maxLen); |
|
1454 |
break; |
|
1455 |
||
1456 |
case SQLT_UIN: |
|
1457 |
(*list)[r] = *reinterpret_cast<uint*>(data + r * columns[i].maxLen); |
|
1458 |
break; |
|
1459 |
||
1460 |
case SQLT_FLT: |
|
1461 |
(*list)[r] = *reinterpret_cast<double*>(data + r * columns[i].maxLen); |
|
1462 |
break; |
|
1463 |
||
1464 |
case SQLT_STR: |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1465 |
(*list)[r] = QString(reinterpret_cast<const QChar *>(data |
0 | 1466 |
+ r * columns[i].maxLen)); |
1467 |
break; |
|
1468 |
||
1469 |
default: |
|
1470 |
(*list)[r] = QByteArray(data + r * columns[i].maxLen, columns[i].maxLen); |
|
1471 |
break; |
|
1472 |
} |
|
1473 |
} |
|
1474 |
} |
|
1475 |
||
1476 |
d->q->setSelect(false); |
|
1477 |
d->q->setAt(QSql::BeforeFirstRow); |
|
1478 |
d->q->setActive(true); |
|
1479 |
||
1480 |
return true; |
|
1481 |
} |
|
1482 |
||
1483 |
template<class T, int sz> |
|
1484 |
int qReadLob(T &buf, const QOCIResultPrivate *d, OCILobLocator *lob) |
|
1485 |
{ |
|
1486 |
ub1 csfrm; |
|
1487 |
ub4 amount; |
|
1488 |
int r; |
|
1489 |
||
1490 |
// Read this from the database, don't assume we know what it is set to |
|
1491 |
r = OCILobCharSetForm(d->env, d->err, lob, &csfrm); |
|
1492 |
if (r != OCI_SUCCESS) { |
|
1493 |
qOraWarning("OCIResultPrivate::readLobs: Couldn't get LOB char set form: ", d->err); |
|
1494 |
csfrm = 0; |
|
1495 |
} |
|
1496 |
||
1497 |
// Get the length of the LOB (this is in characters) |
|
1498 |
r = OCILobGetLength(d->svc, d->err, lob, &amount); |
|
1499 |
if (r == OCI_SUCCESS) { |
|
1500 |
if (amount == 0) { |
|
1501 |
// Short cut for null LOBs |
|
1502 |
buf.resize(0); |
|
1503 |
return OCI_SUCCESS; |
|
1504 |
} |
|
1505 |
} else { |
|
1506 |
qOraWarning("OCIResultPrivate::readLobs: Couldn't get LOB length: ", d->err); |
|
1507 |
return r; |
|
1508 |
} |
|
1509 |
||
1510 |
// Resize the buffer to hold the LOB contents |
|
1511 |
buf.resize(amount); |
|
1512 |
||
1513 |
// Read the LOB into the buffer |
|
1514 |
r = OCILobRead(d->svc, |
|
1515 |
d->err, |
|
1516 |
lob, |
|
1517 |
&amount, |
|
1518 |
1, |
|
1519 |
buf.data(), |
|
1520 |
buf.size() * sz, // this argument is in bytes, not characters |
|
1521 |
0, |
|
1522 |
0, |
|
1523 |
// Extract the data from a CLOB in UTF-16 (ie. what QString uses internally) |
|
1524 |
sz == 1 ? ub2(0) : ub2(QOCIEncoding), |
|
1525 |
csfrm); |
|
1526 |
||
1527 |
if (r != OCI_SUCCESS) |
|
1528 |
qOraWarning("OCIResultPrivate::readLOBs: Cannot read LOB: ", d->err); |
|
1529 |
||
1530 |
return r; |
|
1531 |
} |
|
1532 |
||
1533 |
int QOCICols::readLOBs(QVector<QVariant> &values, int index) |
|
1534 |
{ |
|
1535 |
OCILobLocator *lob; |
|
1536 |
int r = OCI_SUCCESS; |
|
1537 |
||
1538 |
for (int i = 0; i < size(); ++i) { |
|
1539 |
const OraFieldInf &fi = fieldInf.at(i); |
|
1540 |
if (fi.ind == -1 || !(lob = fi.lob)) |
|
1541 |
continue; |
|
1542 |
||
1543 |
bool isClob = fi.oraType == SQLT_CLOB; |
|
1544 |
QVariant var; |
|
1545 |
||
1546 |
if (isClob) { |
|
1547 |
QString str; |
|
1548 |
r = qReadLob<QString, sizeof(QChar)>(str, d, lob); |
|
1549 |
var = str; |
|
1550 |
} else { |
|
1551 |
QByteArray buf; |
|
1552 |
r = qReadLob<QByteArray, sizeof(char)>(buf, d, lob); |
|
1553 |
var = buf; |
|
1554 |
} |
|
1555 |
if (r == OCI_SUCCESS) |
|
1556 |
values[index + i] = var; |
|
1557 |
else |
|
1558 |
break; |
|
1559 |
} |
|
1560 |
return r; |
|
1561 |
} |
|
1562 |
||
1563 |
int QOCICols::fieldFromDefine(OCIDefine* d) |
|
1564 |
{ |
|
1565 |
for (int i = 0; i < fieldInf.count(); ++i) { |
|
1566 |
if (fieldInf.at(i).def == d) |
|
1567 |
return i; |
|
1568 |
} |
|
1569 |
return -1; |
|
1570 |
} |
|
1571 |
||
1572 |
void QOCICols::getValues(QVector<QVariant> &v, int index) |
|
1573 |
{ |
|
1574 |
for (int i = 0; i < fieldInf.size(); ++i) { |
|
1575 |
const OraFieldInf &fld = fieldInf.at(i); |
|
1576 |
||
1577 |
if (fld.ind == -1) { |
|
1578 |
// got a NULL value |
|
1579 |
v[index + i] = QVariant(fld.typ); |
|
1580 |
continue; |
|
1581 |
} |
|
1582 |
||
1583 |
if (fld.oraType == SQLT_BIN || fld.oraType == SQLT_LBI || fld.oraType == SQLT_LNG) |
|
1584 |
continue; // already fetched piecewise |
|
1585 |
||
1586 |
switch (fld.typ) { |
|
1587 |
case QVariant::DateTime: |
|
1588 |
v[index + i] = QVariant(qMakeDate(fld.data)); |
|
1589 |
break; |
|
1590 |
case QVariant::Double: |
|
1591 |
case QVariant::Int: |
|
1592 |
case QVariant::LongLong: |
|
1593 |
if (d->q->numericalPrecisionPolicy() != QSql::HighPrecision) { |
|
1594 |
if ((d->q->numericalPrecisionPolicy() == QSql::LowPrecisionDouble) |
|
1595 |
&& (fld.typ == QVariant::Double)) { |
|
1596 |
v[index + i] = *reinterpret_cast<double *>(fld.data); |
|
1597 |
break; |
|
1598 |
} else if ((d->q->numericalPrecisionPolicy() == QSql::LowPrecisionInt64) |
|
1599 |
&& (fld.typ == QVariant::LongLong)) { |
|
1600 |
qint64 qll = 0; |
|
1601 |
int r = OCINumberToInt(d->err, reinterpret_cast<OCINumber *>(fld.data), sizeof(qint64), |
|
1602 |
OCI_NUMBER_SIGNED, &qll); |
|
1603 |
if(r == OCI_SUCCESS) |
|
1604 |
v[index + i] = qll; |
|
1605 |
else |
|
1606 |
v[index + i] = QVariant(); |
|
1607 |
break; |
|
1608 |
} else if ((d->q->numericalPrecisionPolicy() == QSql::LowPrecisionInt32) |
|
1609 |
&& (fld.typ == QVariant::Int)) { |
|
1610 |
v[index + i] = *reinterpret_cast<int *>(fld.data); |
|
1611 |
break; |
|
1612 |
} |
|
1613 |
} |
|
1614 |
// else fall through |
|
1615 |
case QVariant::String: |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
1616 |
v[index + i] = QString(reinterpret_cast<const QChar *>(fld.data)); |
0 | 1617 |
break; |
1618 |
case QVariant::ByteArray: |
|
1619 |
if (fld.len > 0) |
|
1620 |
v[index + i] = QByteArray(fld.data, fld.len); |
|
1621 |
else |
|
1622 |
v[index + i] = QVariant(QVariant::ByteArray); |
|
1623 |
break; |
|
1624 |
default: |
|
1625 |
qWarning("QOCICols::value: unknown data type"); |
|
1626 |
break; |
|
1627 |
} |
|
1628 |
} |
|
1629 |
} |
|
1630 |
||
1631 |
QOCIResultPrivate::QOCIResultPrivate(QOCIResult *result, const QOCIDriverPrivate *driver) |
|
1632 |
: cols(0), q(result), env(driver->env), err(0), svc(const_cast<OCISvcCtx*&>(driver->svc)), |
|
1633 |
sql(0), transaction(driver->transaction), serverVersion(driver->serverVersion), |
|
1634 |
prefetchRows(driver->prefetchRows), prefetchMem(driver->prefetchMem) |
|
1635 |
{ |
|
1636 |
int r = OCIHandleAlloc(env, |
|
1637 |
reinterpret_cast<void **>(&err), |
|
1638 |
OCI_HTYPE_ERROR, |
|
1639 |
0, |
|
1640 |
0); |
|
1641 |
if (r != 0) |
|
1642 |
qWarning("QOCIResult: unable to alloc error handle"); |
|
1643 |
} |
|
1644 |
||
1645 |
QOCIResultPrivate::~QOCIResultPrivate() |
|
1646 |
{ |
|
1647 |
delete cols; |
|
1648 |
||
1649 |
int r = OCIHandleFree(err, OCI_HTYPE_ERROR); |
|
1650 |
if (r != 0) |
|
1651 |
qWarning("~QOCIResult: unable to free statement handle"); |
|
1652 |
} |
|
1653 |
||
1654 |
||
1655 |
//////////////////////////////////////////////////////////////////////////// |
|
1656 |
||
1657 |
QOCIResult::QOCIResult(const QOCIDriver * db, const QOCIDriverPrivate* p) |
|
1658 |
: QSqlCachedResult(db) |
|
1659 |
{ |
|
1660 |
d = new QOCIResultPrivate(this, p); |
|
1661 |
} |
|
1662 |
||
1663 |
QOCIResult::~QOCIResult() |
|
1664 |
{ |
|
1665 |
if (d->sql) { |
|
1666 |
int r = OCIHandleFree(d->sql, OCI_HTYPE_STMT); |
|
1667 |
if (r != 0) |
|
1668 |
qWarning("~QOCIResult: unable to free statement handle"); |
|
1669 |
} |
|
1670 |
delete d; |
|
1671 |
} |
|
1672 |
||
1673 |
QVariant QOCIResult::handle() const |
|
1674 |
{ |
|
1675 |
return qVariantFromValue(d->sql); |
|
1676 |
} |
|
1677 |
||
1678 |
bool QOCIResult::reset (const QString& query) |
|
1679 |
{ |
|
1680 |
if (!prepare(query)) |
|
1681 |
return false; |
|
1682 |
return exec(); |
|
1683 |
} |
|
1684 |
||
1685 |
bool QOCIResult::gotoNext(QSqlCachedResult::ValueCache &values, int index) |
|
1686 |
{ |
|
1687 |
if (at() == QSql::AfterLastRow) |
|
1688 |
return false; |
|
1689 |
||
1690 |
bool piecewise = false; |
|
1691 |
int r = OCI_SUCCESS; |
|
1692 |
r = OCIStmtFetch(d->sql, d->err, 1, OCI_FETCH_NEXT, OCI_DEFAULT); |
|
1693 |
||
1694 |
if (index < 0) //not interested in values |
|
1695 |
return r == OCI_SUCCESS || r == OCI_SUCCESS_WITH_INFO; |
|
1696 |
||
1697 |
switch (r) { |
|
1698 |
case OCI_SUCCESS: |
|
1699 |
break; |
|
1700 |
case OCI_SUCCESS_WITH_INFO: |
|
1701 |
qOraWarning("QOCIResult::gotoNext: SuccessWithInfo: ", d->err); |
|
1702 |
r = OCI_SUCCESS; //ignore it |
|
1703 |
break; |
|
1704 |
case OCI_NO_DATA: |
|
1705 |
// end of rowset |
|
1706 |
return false; |
|
1707 |
case OCI_NEED_DATA: |
|
1708 |
piecewise = true; |
|
1709 |
r = OCI_SUCCESS; |
|
1710 |
break; |
|
1711 |
case OCI_ERROR: |
|
1712 |
if (qOraErrorNumber(d->err) == 1406) { |
|
1713 |
qWarning("QOCI Warning: data truncated for %s", lastQuery().toLocal8Bit().constData()); |
|
1714 |
r = OCI_SUCCESS; /* ignore it */ |
|
1715 |
break; |
|
1716 |
} |
|
1717 |
// fall through |
|
1718 |
default: |
|
1719 |
qOraWarning("QOCIResult::gotoNext: ", d->err); |
|
1720 |
setLastError(qMakeError(QCoreApplication::translate("QOCIResult", |
|
1721 |
"Unable to goto next"), |
|
1722 |
QSqlError::StatementError, d->err)); |
|
1723 |
break; |
|
1724 |
} |
|
1725 |
||
1726 |
// need to read piecewise before assigning values |
|
1727 |
if (r == OCI_SUCCESS && piecewise) |
|
1728 |
r = d->cols->readPiecewise(values, index); |
|
1729 |
||
1730 |
if (r == OCI_SUCCESS) |
|
1731 |
d->cols->getValues(values, index); |
|
1732 |
if (r == OCI_SUCCESS) |
|
1733 |
r = d->cols->readLOBs(values, index); |
|
1734 |
if (r != OCI_SUCCESS) |
|
1735 |
setAt(QSql::AfterLastRow); |
|
1736 |
return r == OCI_SUCCESS || r == OCI_SUCCESS_WITH_INFO; |
|
1737 |
} |
|
1738 |
||
1739 |
int QOCIResult::size() |
|
1740 |
{ |
|
1741 |
return -1; |
|
1742 |
} |
|
1743 |
||
1744 |
int QOCIResult::numRowsAffected() |
|
1745 |
{ |
|
1746 |
int rowCount; |
|
1747 |
OCIAttrGet(d->sql, |
|
1748 |
OCI_HTYPE_STMT, |
|
1749 |
&rowCount, |
|
1750 |
NULL, |
|
1751 |
OCI_ATTR_ROW_COUNT, |
|
1752 |
d->err); |
|
1753 |
return rowCount; |
|
1754 |
} |
|
1755 |
||
1756 |
bool QOCIResult::prepare(const QString& query) |
|
1757 |
{ |
|
1758 |
int r = 0; |
|
1759 |
QSqlResult::prepare(query); |
|
1760 |
||
1761 |
delete d->cols; |
|
1762 |
d->cols = 0; |
|
1763 |
QSqlCachedResult::cleanup(); |
|
1764 |
||
1765 |
if (d->sql) { |
|
1766 |
r = OCIHandleFree(d->sql, OCI_HTYPE_STMT); |
|
1767 |
if (r != OCI_SUCCESS) |
|
1768 |
qOraWarning("QOCIResult::prepare: unable to free statement handle:", d->err); |
|
1769 |
} |
|
1770 |
if (query.isEmpty()) |
|
1771 |
return false; |
|
1772 |
r = OCIHandleAlloc(d->env, |
|
1773 |
reinterpret_cast<void **>(&d->sql), |
|
1774 |
OCI_HTYPE_STMT, |
|
1775 |
0, |
|
1776 |
0); |
|
1777 |
if (r != OCI_SUCCESS) { |
|
1778 |
qOraWarning("QOCIResult::prepare: unable to alloc statement:", d->err); |
|
1779 |
setLastError(qMakeError(QCoreApplication::translate("QOCIResult", |
|
1780 |
"Unable to alloc statement"), QSqlError::StatementError, d->err)); |
|
1781 |
return false; |
|
1782 |
} |
|
1783 |
d->setStatementAttributes(); |
|
1784 |
const OraText *txt = reinterpret_cast<const OraText *>(query.utf16()); |
|
1785 |
const int len = query.length() * sizeof(QChar); |
|
1786 |
r = OCIStmtPrepare(d->sql, |
|
1787 |
d->err, |
|
1788 |
txt, |
|
1789 |
len, |
|
1790 |
OCI_NTV_SYNTAX, |
|
1791 |
OCI_DEFAULT); |
|
1792 |
if (r != OCI_SUCCESS) { |
|
1793 |
qOraWarning("QOCIResult::prepare: unable to prepare statement:", d->err); |
|
1794 |
setLastError(qMakeError(QCoreApplication::translate("QOCIResult", |
|
1795 |
"Unable to prepare statement"), QSqlError::StatementError, d->err)); |
|
1796 |
return false; |
|
1797 |
} |
|
1798 |
return true; |
|
1799 |
} |
|
1800 |
||
1801 |
bool QOCIResult::exec() |
|
1802 |
{ |
|
1803 |
int r = 0; |
|
1804 |
ub2 stmtType=0; |
|
1805 |
ub4 iters; |
|
1806 |
ub4 mode; |
|
1807 |
QList<QByteArray> tmpStorage; |
|
1808 |
IndicatorArray indicators(boundValueCount()); |
|
1809 |
SizeArray tmpSizes(boundValueCount()); |
|
1810 |
||
1811 |
r = OCIAttrGet(d->sql, |
|
1812 |
OCI_HTYPE_STMT, |
|
1813 |
&stmtType, |
|
1814 |
NULL, |
|
1815 |
OCI_ATTR_STMT_TYPE, |
|
1816 |
d->err); |
|
1817 |
||
1818 |
if (r != OCI_SUCCESS && r != OCI_SUCCESS_WITH_INFO) { |
|
1819 |
qOraWarning("QOCIResult::exec: Unable to get statement type:", d->err); |
|
1820 |
setLastError(qMakeError(QCoreApplication::translate("QOCIResult", |
|
1821 |
"Unable to get statement type"), QSqlError::StatementError, d->err)); |
|
1822 |
#ifdef QOCI_DEBUG |
|
1823 |
qDebug() << "lastQuery()" << lastQuery(); |
|
1824 |
#endif |
|
1825 |
return false; |
|
1826 |
} |
|
1827 |
||
1828 |
if (stmtType == OCI_STMT_SELECT) { |
|
1829 |
iters = 0; |
|
1830 |
mode = OCI_DEFAULT; |
|
1831 |
} else { |
|
1832 |
iters = 1; |
|
1833 |
mode = d->transaction ? OCI_DEFAULT : OCI_COMMIT_ON_SUCCESS; |
|
1834 |
} |
|
1835 |
||
1836 |
// bind placeholders |
|
1837 |
if (boundValueCount() > 0 |
|
1838 |
&& d->bindValues(boundValues(), indicators, tmpSizes, tmpStorage) != OCI_SUCCESS) { |
|
1839 |
qOraWarning("QOCIResult::exec: unable to bind value: ", d->err); |
|
1840 |
setLastError(qMakeError(QCoreApplication::translate("QOCIResult", "Unable to bind value"), |
|
1841 |
QSqlError::StatementError, d->err)); |
|
1842 |
#ifdef QOCI_DEBUG |
|
1843 |
qDebug() << "lastQuery()" << lastQuery(); |
|
1844 |
#endif |
|
1845 |
return false; |
|
1846 |
} |
|
1847 |
||
1848 |
// execute |
|
1849 |
r = OCIStmtExecute(d->svc, |
|
1850 |
d->sql, |
|
1851 |
d->err, |
|
1852 |
iters, |
|
1853 |
0, |
|
1854 |
0, |
|
1855 |
0, |
|
1856 |
mode); |
|
1857 |
if (r != OCI_SUCCESS && r != OCI_SUCCESS_WITH_INFO) { |
|
1858 |
qOraWarning("QOCIResult::exec: unable to execute statement:", d->err); |
|
1859 |
setLastError(qMakeError(QCoreApplication::translate("QOCIResult", |
|
1860 |
"Unable to execute statement"), QSqlError::StatementError, d->err)); |
|
1861 |
#ifdef QOCI_DEBUG |
|
1862 |
qDebug() << "lastQuery()" << lastQuery(); |
|
1863 |
#endif |
|
1864 |
return false; |
|
1865 |
} |
|
1866 |
||
1867 |
if (stmtType == OCI_STMT_SELECT) { |
|
1868 |
ub4 parmCount = 0; |
|
1869 |
int r = OCIAttrGet(d->sql, OCI_HTYPE_STMT, reinterpret_cast<void **>(&parmCount), |
|
1870 |
0, OCI_ATTR_PARAM_COUNT, d->err); |
|
1871 |
if (r == 0 && !d->cols) |
|
1872 |
d->cols = new QOCICols(parmCount, d); |
|
1873 |
setSelect(true); |
|
1874 |
QSqlCachedResult::init(parmCount); |
|
1875 |
} else { /* non-SELECT */ |
|
1876 |
setSelect(false); |
|
1877 |
} |
|
1878 |
setAt(QSql::BeforeFirstRow); |
|
1879 |
setActive(true); |
|
1880 |
||
1881 |
if (hasOutValues()) |
|
1882 |
d->outValues(boundValues(), indicators, tmpStorage); |
|
1883 |
||
1884 |
return true; |
|
1885 |
} |
|
1886 |
||
1887 |
QSqlRecord QOCIResult::record() const |
|
1888 |
{ |
|
1889 |
QSqlRecord inf; |
|
1890 |
if (!isActive() || !isSelect() || !d->cols) |
|
1891 |
return inf; |
|
1892 |
return d->cols->rec; |
|
1893 |
} |
|
1894 |
||
1895 |
QVariant QOCIResult::lastInsertId() const |
|
1896 |
{ |
|
1897 |
if (isActive()) { |
|
1898 |
QOCIRowIdPointer ptr(new QOCIRowId(d->env)); |
|
1899 |
||
1900 |
int r = OCIAttrGet(d->sql, OCI_HTYPE_STMT, ptr.constData()->id, |
|
1901 |
0, OCI_ATTR_ROWID, d->err); |
|
1902 |
if (r == OCI_SUCCESS) |
|
1903 |
return qVariantFromValue(ptr); |
|
1904 |
} |
|
1905 |
return QVariant(); |
|
1906 |
} |
|
1907 |
||
1908 |
void QOCIResult::virtual_hook(int id, void *data) |
|
1909 |
{ |
|
1910 |
Q_ASSERT(data); |
|
1911 |
||
1912 |
switch (id) { |
|
1913 |
case QSqlResult::BatchOperation: |
|
1914 |
QOCICols::execBatch(d, boundValues(), *reinterpret_cast<bool *>(data)); |
|
1915 |
break; |
|
1916 |
default: |
|
1917 |
QSqlCachedResult::virtual_hook(id, data); |
|
1918 |
} |
|
1919 |
} |
|
1920 |
||
1921 |
//////////////////////////////////////////////////////////////////////////// |
|
1922 |
||
1923 |
||
1924 |
QOCIDriver::QOCIDriver(QObject* parent) |
|
1925 |
: QSqlDriver(parent) |
|
1926 |
{ |
|
1927 |
d = new QOCIDriverPrivate(); |
|
1928 |
||
1929 |
#ifdef QOCI_THREADED |
|
1930 |
const ub4 mode = OCI_UTF16 | OCI_OBJECT | OCI_THREADED; |
|
1931 |
#else |
|
1932 |
const ub4 mode = OCI_UTF16 | OCI_OBJECT; |
|
1933 |
#endif |
|
1934 |
int r = OCIEnvCreate(&d->env, |
|
1935 |
mode, |
|
1936 |
NULL, |
|
1937 |
NULL, |
|
1938 |
NULL, |
|
1939 |
NULL, |
|
1940 |
0, |
|
1941 |
NULL); |
|
1942 |
if (r != 0) { |
|
1943 |
qWarning("QOCIDriver: unable to create environment"); |
|
1944 |
setLastError(qMakeError(tr("Unable to initialize", "QOCIDriver"), |
|
1945 |
QSqlError::ConnectionError, d->err)); |
|
1946 |
return; |
|
1947 |
} |
|
1948 |
||
1949 |
d->allocErrorHandle(); |
|
1950 |
} |
|
1951 |
||
1952 |
QOCIDriver::QOCIDriver(OCIEnv* env, OCISvcCtx* ctx, QObject* parent) |
|
1953 |
: QSqlDriver(parent) |
|
1954 |
{ |
|
1955 |
d = new QOCIDriverPrivate(); |
|
1956 |
d->env = env; |
|
1957 |
d->svc = ctx; |
|
1958 |
||
1959 |
d->allocErrorHandle(); |
|
1960 |
||
1961 |
if (env && ctx) { |
|
1962 |
setOpen(true); |
|
1963 |
setOpenError(false); |
|
1964 |
} |
|
1965 |
} |
|
1966 |
||
1967 |
QOCIDriver::~QOCIDriver() |
|
1968 |
{ |
|
1969 |
if (isOpen()) |
|
1970 |
close(); |
|
1971 |
int r = OCIHandleFree(d->err, OCI_HTYPE_ERROR); |
|
1972 |
if (r != OCI_SUCCESS) |
|
1973 |
qWarning("Unable to free Error handle: %d", r); |
|
1974 |
r = OCIHandleFree(d->env, OCI_HTYPE_ENV); |
|
1975 |
if (r != OCI_SUCCESS) |
|
1976 |
qWarning("Unable to free Environment handle: %d", r); |
|
1977 |
||
1978 |
delete d; |
|
1979 |
} |
|
1980 |
||
1981 |
bool QOCIDriver::hasFeature(DriverFeature f) const |
|
1982 |
{ |
|
1983 |
switch (f) { |
|
1984 |
case Transactions: |
|
1985 |
case LastInsertId: |
|
1986 |
case BLOB: |
|
1987 |
case PreparedQueries: |
|
1988 |
case NamedPlaceholders: |
|
1989 |
case BatchOperations: |
|
1990 |
case LowPrecisionNumbers: |
|
1991 |
return true; |
|
1992 |
case QuerySize: |
|
1993 |
case PositionalPlaceholders: |
|
1994 |
case SimpleLocking: |
|
1995 |
case EventNotifications: |
|
1996 |
case FinishQuery: |
|
1997 |
case MultipleResultSets: |
|
1998 |
return false; |
|
1999 |
case Unicode: |
|
2000 |
return d->serverVersion >= 9; |
|
2001 |
} |
|
2002 |
return false; |
|
2003 |
} |
|
2004 |
||
2005 |
static void qParseOpts(const QString &options, QOCIDriverPrivate *d) |
|
2006 |
{ |
|
2007 |
const QStringList opts(options.split(QLatin1Char(';'), QString::SkipEmptyParts)); |
|
2008 |
for (int i = 0; i < opts.count(); ++i) { |
|
2009 |
const QString tmp(opts.at(i)); |
|
2010 |
int idx; |
|
2011 |
if ((idx = tmp.indexOf(QLatin1Char('='))) == -1) { |
|
2012 |
qWarning("QOCIDriver::parseArgs: Invalid parameter: '%s'", |
|
2013 |
tmp.toLocal8Bit().constData()); |
|
2014 |
continue; |
|
2015 |
} |
|
2016 |
const QString opt = tmp.left(idx); |
|
2017 |
const QString val = tmp.mid(idx + 1).simplified(); |
|
2018 |
bool ok; |
|
2019 |
if (opt == QLatin1String("OCI_ATTR_PREFETCH_ROWS")) { |
|
2020 |
d->prefetchRows = val.toInt(&ok); |
|
2021 |
if (!ok) |
|
2022 |
d->prefetchRows = -1; |
|
2023 |
} else if (opt == QLatin1String("OCI_ATTR_PREFETCH_MEMORY")) { |
|
2024 |
d->prefetchMem = val.toInt(&ok); |
|
2025 |
if (!ok) |
|
2026 |
d->prefetchMem = -1; |
|
2027 |
} else { |
|
2028 |
qWarning ("QOCIDriver::parseArgs: Invalid parameter: '%s'", |
|
2029 |
opt.toLocal8Bit().constData()); |
|
2030 |
} |
|
2031 |
} |
|
2032 |
} |
|
2033 |
||
2034 |
bool QOCIDriver::open(const QString & db, |
|
2035 |
const QString & user, |
|
2036 |
const QString & password, |
|
2037 |
const QString & hostname, |
|
2038 |
int port, |
|
2039 |
const QString &opts) |
|
2040 |
{ |
|
2041 |
int r; |
|
2042 |
||
2043 |
if (isOpen()) |
|
2044 |
close(); |
|
2045 |
||
2046 |
qParseOpts(opts, d); |
|
2047 |
||
2048 |
// Connect without tnsnames.ora if a hostname is given |
|
2049 |
QString connectionString = db; |
|
2050 |
if (!hostname.isEmpty()) |
|
2051 |
connectionString = |
|
2052 |
QString::fromLatin1("(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=%1)(Port=%2))" |
|
2053 |
"(CONNECT_DATA=(SID=%3)))").arg(hostname).arg((port > -1 ? port : 1521)).arg(db); |
|
2054 |
||
2055 |
r = OCIHandleAlloc(d->env, reinterpret_cast<void **>(&d->srvhp), OCI_HTYPE_SERVER, 0, 0); |
|
2056 |
if (r == OCI_SUCCESS) |
|
2057 |
r = OCIServerAttach(d->srvhp, d->err, reinterpret_cast<const OraText *>(connectionString.utf16()), |
|
2058 |
connectionString.length() * sizeof(QChar), OCI_DEFAULT); |
|
2059 |
if (r == OCI_SUCCESS || r == OCI_SUCCESS_WITH_INFO) |
|
2060 |
r = OCIHandleAlloc(d->env, reinterpret_cast<void **>(&d->svc), OCI_HTYPE_SVCCTX, 0, 0); |
|
2061 |
if (r == OCI_SUCCESS) |
|
2062 |
r = OCIAttrSet(d->svc, OCI_HTYPE_SVCCTX, d->srvhp, 0, OCI_ATTR_SERVER, d->err); |
|
2063 |
if (r == OCI_SUCCESS) |
|
2064 |
r = OCIHandleAlloc(d->env, reinterpret_cast<void **>(&d->authp), OCI_HTYPE_SESSION, 0, 0); |
|
2065 |
if (r == OCI_SUCCESS) |
|
2066 |
r = OCIAttrSet(d->authp, OCI_HTYPE_SESSION, const_cast<ushort *>(user.utf16()), |
|
2067 |
user.length() * sizeof(QChar), OCI_ATTR_USERNAME, d->err); |
|
2068 |
if (r == OCI_SUCCESS) |
|
2069 |
r = OCIAttrSet(d->authp, OCI_HTYPE_SESSION, const_cast<ushort *>(password.utf16()), |
|
2070 |
password.length() * sizeof(QChar), OCI_ATTR_PASSWORD, d->err); |
|
2071 |
||
2072 |
OCITrans* trans; |
|
2073 |
if (r == OCI_SUCCESS) |
|
2074 |
r = OCIHandleAlloc(d->env, reinterpret_cast<void **>(&trans), OCI_HTYPE_TRANS, 0, 0); |
|
2075 |
if (r == OCI_SUCCESS) |
|
2076 |
r = OCIAttrSet(d->svc, OCI_HTYPE_SVCCTX, trans, 0, OCI_ATTR_TRANS, d->err); |
|
2077 |
||
2078 |
if (r == OCI_SUCCESS) { |
|
2079 |
if (user.isEmpty() && password.isEmpty()) |
|
2080 |
r = OCISessionBegin(d->svc, d->err, d->authp, OCI_CRED_EXT, OCI_DEFAULT); |
|
2081 |
else |
|
2082 |
r = OCISessionBegin(d->svc, d->err, d->authp, OCI_CRED_RDBMS, OCI_DEFAULT); |
|
2083 |
} |
|
2084 |
if (r == OCI_SUCCESS || r == OCI_SUCCESS_WITH_INFO) |
|
2085 |
r = OCIAttrSet(d->svc, OCI_HTYPE_SVCCTX, d->authp, 0, OCI_ATTR_SESSION, d->err); |
|
2086 |
||
2087 |
if (r != OCI_SUCCESS) { |
|
2088 |
setLastError(qMakeError(tr("Unable to logon"), QSqlError::ConnectionError, d->err)); |
|
2089 |
setOpenError(true); |
|
2090 |
if (d->authp) |
|
2091 |
OCIHandleFree(d->authp, OCI_HTYPE_SESSION); |
|
2092 |
d->authp = 0; |
|
2093 |
if (d->srvhp) |
|
2094 |
OCIHandleFree(d->srvhp, OCI_HTYPE_SERVER); |
|
2095 |
d->srvhp = 0; |
|
2096 |
return false; |
|
2097 |
} |
|
2098 |
||
2099 |
// get server version |
|
2100 |
char vertxt[512]; |
|
2101 |
r = OCIServerVersion(d->svc, |
|
2102 |
d->err, |
|
2103 |
reinterpret_cast<OraText *>(vertxt), |
|
2104 |
sizeof(vertxt), |
|
2105 |
OCI_HTYPE_SVCCTX); |
|
2106 |
if (r != 0) { |
|
2107 |
qWarning("QOCIDriver::open: could not get Oracle server version."); |
|
2108 |
} else { |
|
2109 |
QString versionStr; |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
19
diff
changeset
|
2110 |
versionStr = QString(reinterpret_cast<const QChar *>(vertxt)); |
0 | 2111 |
QRegExp vers(QLatin1String("([0-9]+)\\.[0-9\\.]+[0-9]")); |
2112 |
if (vers.indexIn(versionStr) >= 0) |
|
2113 |
d->serverVersion = vers.cap(1).toInt(); |
|
2114 |
if (d->serverVersion == 0) |
|
2115 |
d->serverVersion = -1; |
|
2116 |
} |
|
2117 |
||
2118 |
setOpen(true); |
|
2119 |
setOpenError(false); |
|
2120 |
d->user = user; |
|
2121 |
||
2122 |
return true; |
|
2123 |
} |
|
2124 |
||
2125 |
void QOCIDriver::close() |
|
2126 |
{ |
|
2127 |
if (!isOpen()) |
|
2128 |
return; |
|
2129 |
||
2130 |
OCISessionEnd(d->svc, d->err, d->authp, OCI_DEFAULT); |
|
2131 |
OCIServerDetach(d->srvhp, d->err, OCI_DEFAULT); |
|
2132 |
OCIHandleFree(d->authp, OCI_HTYPE_SESSION); |
|
2133 |
d->authp = 0; |
|
2134 |
OCIHandleFree(d->srvhp, OCI_HTYPE_SERVER); |
|
2135 |
d->srvhp = 0; |
|
2136 |
OCIHandleFree(d->svc, OCI_HTYPE_SVCCTX); |
|
2137 |
d->svc = 0; |
|
2138 |
setOpen(false); |
|
2139 |
setOpenError(false); |
|
2140 |
} |
|
2141 |
||
2142 |
QSqlResult *QOCIDriver::createResult() const |
|
2143 |
{ |
|
2144 |
return new QOCIResult(this, d); |
|
2145 |
} |
|
2146 |
||
2147 |
bool QOCIDriver::beginTransaction() |
|
2148 |
{ |
|
2149 |
if (!isOpen()) { |
|
2150 |
qWarning("QOCIDriver::beginTransaction: Database not open"); |
|
2151 |
return false; |
|
2152 |
} |
|
2153 |
int r = OCITransStart(d->svc, |
|
2154 |
d->err, |
|
2155 |
2, |
|
2156 |
OCI_TRANS_READWRITE); |
|
2157 |
if (r == OCI_ERROR) { |
|
2158 |
qOraWarning("QOCIDriver::beginTransaction: ", d->err); |
|
2159 |
setLastError(qMakeError(QCoreApplication::translate("QOCIDriver", |
|
2160 |
"Unable to begin transaction"), QSqlError::TransactionError, d->err)); |
|
2161 |
return false; |
|
2162 |
} |
|
2163 |
d->transaction = true; |
|
2164 |
return true; |
|
2165 |
} |
|
2166 |
||
2167 |
bool QOCIDriver::commitTransaction() |
|
2168 |
{ |
|
2169 |
if (!isOpen()) { |
|
2170 |
qWarning("QOCIDriver::commitTransaction: Database not open"); |
|
2171 |
return false; |
|
2172 |
} |
|
2173 |
int r = OCITransCommit(d->svc, |
|
2174 |
d->err, |
|
2175 |
0); |
|
2176 |
if (r == OCI_ERROR) { |
|
2177 |
qOraWarning("QOCIDriver::commitTransaction:", d->err); |
|
2178 |
setLastError(qMakeError(QCoreApplication::translate("QOCIDriver", |
|
2179 |
"Unable to commit transaction"), QSqlError::TransactionError, d->err)); |
|
2180 |
return false; |
|
2181 |
} |
|
2182 |
d->transaction = false; |
|
2183 |
return true; |
|
2184 |
} |
|
2185 |
||
2186 |
bool QOCIDriver::rollbackTransaction() |
|
2187 |
{ |
|
2188 |
if (!isOpen()) { |
|
2189 |
qWarning("QOCIDriver::rollbackTransaction: Database not open"); |
|
2190 |
return false; |
|
2191 |
} |
|
2192 |
int r = OCITransRollback(d->svc, |
|
2193 |
d->err, |
|
2194 |
0); |
|
2195 |
if (r == OCI_ERROR) { |
|
2196 |
qOraWarning("QOCIDriver::rollbackTransaction:", d->err); |
|
2197 |
setLastError(qMakeError(QCoreApplication::translate("QOCIDriver", |
|
2198 |
"Unable to rollback transaction"), QSqlError::TransactionError, d->err)); |
|
2199 |
return false; |
|
2200 |
} |
|
2201 |
d->transaction = false; |
|
2202 |
return true; |
|
2203 |
} |
|
2204 |
||
2205 |
QStringList QOCIDriver::tables(QSql::TableType type) const |
|
2206 |
{ |
|
2207 |
QStringList tl; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2208 |
QStringList sysUsers = QStringList() << QLatin1String("MDSYS") |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2209 |
<< QLatin1String("LBACSYS") |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2210 |
<< QLatin1String("SYS") |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2211 |
<< QLatin1String("SYSTEM") |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2212 |
<< QLatin1String("WKSYS") |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2213 |
<< QLatin1String("CTXSYS") |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2214 |
<< QLatin1String("WMSYS"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2215 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2216 |
QString user = d->user; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2217 |
if ( isIdentifierEscaped(user, QSqlDriver::TableName)) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2218 |
user = stripDelimiters(user, QSqlDriver::TableName); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2219 |
else |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2220 |
user = user.toUpper(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2221 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2222 |
if(sysUsers.contains(user)) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2223 |
sysUsers.removeAll(user);; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2224 |
|
0 | 2225 |
if (!isOpen()) |
2226 |
return tl; |
|
2227 |
||
2228 |
QSqlQuery t(createResult()); |
|
2229 |
t.setForwardOnly(true); |
|
2230 |
if (type & QSql::Tables) { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2231 |
QString query = QLatin1String("select owner, table_name from all_tables where "); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2232 |
QStringList whereList; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2233 |
foreach(const QString &sysUserName, sysUsers) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2234 |
whereList << QLatin1String("owner != '") + sysUserName + QLatin1String("' "); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2235 |
t.exec(query + whereList.join(QLatin1String(" and "))); |
0 | 2236 |
|
2237 |
while (t.next()) { |
|
2238 |
if (t.value(0).toString().toUpper() != user.toUpper()) |
|
2239 |
tl.append(t.value(0).toString() + QLatin1Char('.') + t.value(1).toString()); |
|
2240 |
else |
|
2241 |
tl.append(t.value(1).toString()); |
|
2242 |
} |
|
2243 |
||
2244 |
// list all table synonyms as well |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2245 |
query = QLatin1String("select owner, synonym_name from all_synonyms where "); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2246 |
t.exec(query + whereList.join(QLatin1String(" and "))); |
0 | 2247 |
while (t.next()) { |
2248 |
if (t.value(0).toString() != d->user) |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2249 |
tl.append(t.value(0).toString() + QLatin1Char('.') + t.value(1).toString()); |
0 | 2250 |
else |
2251 |
tl.append(t.value(1).toString()); |
|
2252 |
} |
|
2253 |
} |
|
2254 |
if (type & QSql::Views) { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2255 |
QString query = QLatin1String("select owner, view_name from all_views where "); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2256 |
QStringList whereList; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2257 |
foreach(const QString &sysUserName, sysUsers) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2258 |
whereList << QLatin1String("owner != '") + sysUserName + QLatin1String("' "); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2259 |
t.exec(query + whereList.join(QLatin1String(" and "))); |
0 | 2260 |
while (t.next()) { |
2261 |
if (t.value(0).toString().toUpper() != d->user.toUpper()) |
|
2262 |
tl.append(t.value(0).toString() + QLatin1Char('.') + t.value(1).toString()); |
|
2263 |
else |
|
2264 |
tl.append(t.value(1).toString()); |
|
2265 |
} |
|
2266 |
} |
|
2267 |
if (type & QSql::SystemTables) { |
|
2268 |
t.exec(QLatin1String("select table_name from dictionary")); |
|
2269 |
while (t.next()) { |
|
2270 |
tl.append(t.value(0).toString()); |
|
2271 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2272 |
QString query = QLatin1String("select owner, table_name from all_tables where "); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2273 |
QStringList whereList; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2274 |
foreach(const QString &sysUserName, sysUsers) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2275 |
whereList << QLatin1String("owner = '") + sysUserName + QLatin1String("' "); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2276 |
t.exec(query + whereList.join(QLatin1String(" or "))); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2277 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2278 |
while (t.next()) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2279 |
if (t.value(0).toString().toUpper() != user.toUpper()) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2280 |
tl.append(t.value(0).toString() + QLatin1Char('.') + t.value(1).toString()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2281 |
else |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2282 |
tl.append(t.value(1).toString()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2283 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2284 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2285 |
// list all table synonyms as well |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2286 |
query = QLatin1String("select owner, synonym_name from all_synonyms where "); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2287 |
t.exec(query + whereList.join(QLatin1String(" or "))); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2288 |
while (t.next()) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2289 |
if (t.value(0).toString() != d->user) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2290 |
tl.append(t.value(0).toString() + QLatin1String(".") + t.value(1).toString()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2291 |
else |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2292 |
tl.append(t.value(1).toString()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2293 |
} |
0 | 2294 |
} |
2295 |
return tl; |
|
2296 |
} |
|
2297 |
||
2298 |
void qSplitTableAndOwner(const QString & tname, QString * tbl, |
|
2299 |
QString * owner) |
|
2300 |
{ |
|
2301 |
int i = tname.indexOf(QLatin1Char('.')); // prefixed with owner? |
|
2302 |
if (i != -1) { |
|
2303 |
*tbl = tname.right(tname.length() - i - 1); |
|
2304 |
*owner = tname.left(i); |
|
2305 |
} else { |
|
2306 |
*tbl = tname; |
|
2307 |
} |
|
2308 |
} |
|
2309 |
||
2310 |
QSqlRecord QOCIDriver::record(const QString& tablename) const |
|
2311 |
{ |
|
2312 |
QSqlRecord fil; |
|
2313 |
if (!isOpen()) |
|
2314 |
return fil; |
|
2315 |
||
2316 |
QSqlQuery t(createResult()); |
|
2317 |
// using two separate queries for this is A LOT faster than using |
|
2318 |
// eg. a sub-query on the sys.synonyms table |
|
2319 |
QString stmt(QLatin1String("select column_name, data_type, data_length, " |
|
2320 |
"data_precision, data_scale, nullable, data_default%1" |
|
2321 |
"from all_tab_columns a " |
|
2322 |
"where a.table_name=%2")); |
|
2323 |
if (d->serverVersion >= 9) |
|
2324 |
stmt = stmt.arg(QLatin1String(", char_length ")); |
|
2325 |
else |
|
2326 |
stmt = stmt.arg(QLatin1String(" ")); |
|
2327 |
bool buildRecordInfo = false; |
|
2328 |
QString table, owner, tmpStmt; |
|
2329 |
qSplitTableAndOwner(tablename, &table, &owner); |
|
2330 |
||
2331 |
if (isIdentifierEscaped(table, QSqlDriver::TableName)) |
|
2332 |
table = stripDelimiters(table, QSqlDriver::TableName); |
|
2333 |
else |
|
2334 |
table = table.toUpper(); |
|
2335 |
||
2336 |
tmpStmt = stmt.arg(QLatin1Char('\'') + table + QLatin1Char('\'')); |
|
2337 |
if (owner.isEmpty()) { |
|
2338 |
owner = d->user; |
|
2339 |
} |
|
2340 |
||
2341 |
if (isIdentifierEscaped(owner, QSqlDriver::TableName)) |
|
2342 |
owner = stripDelimiters(owner, QSqlDriver::TableName); |
|
2343 |
else |
|
2344 |
owner = owner.toUpper(); |
|
2345 |
||
2346 |
tmpStmt += QLatin1String(" and a.owner='") + owner + QLatin1Char('\''); |
|
2347 |
t.setForwardOnly(true); |
|
2348 |
t.exec(tmpStmt); |
|
2349 |
if (!t.next()) { // try and see if the tablename is a synonym |
|
2350 |
stmt = stmt + QLatin1String(" join all_synonyms b " |
|
2351 |
"on a.owner=b.table_owner and a.table_name=b.table_name " |
|
2352 |
"where b.owner='") + owner + |
|
2353 |
QLatin1String("' and b.synonym_name='") + table + |
|
2354 |
QLatin1Char('\''); |
|
2355 |
t.setForwardOnly(true); |
|
2356 |
t.exec(stmt); |
|
2357 |
if (t.next()) |
|
2358 |
buildRecordInfo = true; |
|
2359 |
} else { |
|
2360 |
buildRecordInfo = true; |
|
2361 |
} |
|
2362 |
QStringList keywords = QStringList() << QLatin1String("NUMBER") << QLatin1String("FLOAT") << QLatin1String("BINARY_FLOAT") |
|
2363 |
<< QLatin1String("BINARY_DOUBLE"); |
|
2364 |
if (buildRecordInfo) { |
|
2365 |
do { |
|
2366 |
QVariant::Type ty = qDecodeOCIType(t.value(1).toString(), t.numericalPrecisionPolicy()); |
|
2367 |
QSqlField f(t.value(0).toString(), ty); |
|
2368 |
f.setRequired(t.value(5).toString() == QLatin1String("N")); |
|
2369 |
f.setPrecision(t.value(4).toInt()); |
|
2370 |
if (d->serverVersion >= 9 && (ty == QVariant::String) && !t.isNull(3) && !keywords.contains(t.value(1).toString())) { |
|
2371 |
// Oracle9: data_length == size in bytes, char_length == amount of characters |
|
2372 |
f.setLength(t.value(7).toInt()); |
|
2373 |
} else { |
|
2374 |
f.setLength(t.value(t.isNull(3) ? 2 : 3).toInt()); |
|
2375 |
} |
|
2376 |
f.setDefaultValue(t.value(6)); |
|
2377 |
fil.append(f); |
|
2378 |
} while (t.next()); |
|
2379 |
} |
|
2380 |
return fil; |
|
2381 |
} |
|
2382 |
||
2383 |
QSqlIndex QOCIDriver::primaryIndex(const QString& tablename) const |
|
2384 |
{ |
|
2385 |
QSqlIndex idx(tablename); |
|
2386 |
if (!isOpen()) |
|
2387 |
return idx; |
|
2388 |
QSqlQuery t(createResult()); |
|
2389 |
QString stmt(QLatin1String("select b.column_name, b.index_name, a.table_name, a.owner " |
|
2390 |
"from all_constraints a, all_ind_columns b " |
|
2391 |
"where a.constraint_type='P' " |
|
2392 |
"and b.index_name = a.constraint_name " |
|
2393 |
"and b.index_owner = a.owner")); |
|
2394 |
||
2395 |
bool buildIndex = false; |
|
2396 |
QString table, owner, tmpStmt; |
|
2397 |
qSplitTableAndOwner(tablename, &table, &owner); |
|
2398 |
||
2399 |
if (isIdentifierEscaped(table, QSqlDriver::TableName)) |
|
2400 |
table = stripDelimiters(table, QSqlDriver::TableName); |
|
2401 |
else |
|
2402 |
table = table.toUpper(); |
|
2403 |
||
2404 |
tmpStmt = stmt + QLatin1String(" and a.table_name='") + table + QLatin1Char('\''); |
|
2405 |
if (owner.isEmpty()) { |
|
2406 |
owner = d->user; |
|
2407 |
} |
|
2408 |
||
2409 |
if (isIdentifierEscaped(owner, QSqlDriver::TableName)) |
|
2410 |
owner = stripDelimiters(owner, QSqlDriver::TableName); |
|
2411 |
else |
|
2412 |
owner = owner.toUpper(); |
|
2413 |
||
2414 |
tmpStmt += QLatin1String(" and a.owner='") + owner + QLatin1Char('\''); |
|
2415 |
t.setForwardOnly(true); |
|
2416 |
t.exec(tmpStmt); |
|
2417 |
||
2418 |
if (!t.next()) { |
|
2419 |
stmt += QLatin1String(" and a.table_name=(select tname from sys.synonyms " |
|
2420 |
"where sname='") + table + QLatin1String("' and creator=a.owner)"); |
|
2421 |
t.setForwardOnly(true); |
|
2422 |
t.exec(stmt); |
|
2423 |
if (t.next()) { |
|
2424 |
owner = t.value(3).toString(); |
|
2425 |
buildIndex = true; |
|
2426 |
} |
|
2427 |
} else { |
|
2428 |
buildIndex = true; |
|
2429 |
} |
|
2430 |
if (buildIndex) { |
|
2431 |
QSqlQuery tt(createResult()); |
|
2432 |
tt.setForwardOnly(true); |
|
2433 |
idx.setName(t.value(1).toString()); |
|
2434 |
do { |
|
2435 |
tt.exec(QLatin1String("select data_type from all_tab_columns where table_name='") + |
|
2436 |
t.value(2).toString() + QLatin1String("' and column_name='") + |
|
2437 |
t.value(0).toString() + QLatin1String("' and owner='") + |
|
2438 |
owner + QLatin1Char('\'')); |
|
2439 |
if (!tt.next()) { |
|
2440 |
return QSqlIndex(); |
|
2441 |
} |
|
2442 |
QSqlField f(t.value(0).toString(), qDecodeOCIType(tt.value(0).toString(), t.numericalPrecisionPolicy())); |
|
2443 |
idx.append(f); |
|
2444 |
} while (t.next()); |
|
2445 |
return idx; |
|
2446 |
} |
|
2447 |
return QSqlIndex(); |
|
2448 |
} |
|
2449 |
||
2450 |
QString QOCIDriver::formatValue(const QSqlField &field, bool trimStrings) const |
|
2451 |
{ |
|
2452 |
switch (field.type()) { |
|
2453 |
case QVariant::DateTime: { |
|
2454 |
QDateTime datetime = field.value().toDateTime(); |
|
2455 |
QString datestring; |
|
2456 |
if (datetime.isValid()) { |
|
2457 |
datestring = QLatin1String("TO_DATE('") + QString::number(datetime.date().year()) |
|
2458 |
+ QLatin1Char('-') |
|
2459 |
+ QString::number(datetime.date().month()) + QLatin1Char('-') |
|
2460 |
+ QString::number(datetime.date().day()) + QLatin1Char(' ') |
|
2461 |
+ QString::number(datetime.time().hour()) + QLatin1Char(':') |
|
2462 |
+ QString::number(datetime.time().minute()) + QLatin1Char(':') |
|
2463 |
+ QString::number(datetime.time().second()) |
|
2464 |
+ QLatin1String("','YYYY-MM-DD HH24:MI:SS')"); |
|
2465 |
} else { |
|
2466 |
datestring = QLatin1String("NULL"); |
|
2467 |
} |
|
2468 |
return datestring; |
|
2469 |
} |
|
2470 |
case QVariant::Time: { |
|
2471 |
QDateTime datetime = field.value().toDateTime(); |
|
2472 |
QString datestring; |
|
2473 |
if (datetime.isValid()) { |
|
2474 |
datestring = QLatin1String("TO_DATE('") |
|
2475 |
+ QString::number(datetime.time().hour()) + QLatin1Char(':') |
|
2476 |
+ QString::number(datetime.time().minute()) + QLatin1Char(':') |
|
2477 |
+ QString::number(datetime.time().second()) |
|
2478 |
+ QLatin1String("','HH24:MI:SS')"); |
|
2479 |
} else { |
|
2480 |
datestring = QLatin1String("NULL"); |
|
2481 |
} |
|
2482 |
return datestring; |
|
2483 |
} |
|
2484 |
case QVariant::Date: { |
|
2485 |
QDate date = field.value().toDate(); |
|
2486 |
QString datestring; |
|
2487 |
if (date.isValid()) { |
|
2488 |
datestring = QLatin1String("TO_DATE('") + QString::number(date.year()) + |
|
2489 |
QLatin1Char('-') + |
|
2490 |
QString::number(date.month()) + QLatin1Char('-') + |
|
2491 |
QString::number(date.day()) + QLatin1String("','YYYY-MM-DD')"); |
|
2492 |
} else { |
|
2493 |
datestring = QLatin1String("NULL"); |
|
2494 |
} |
|
2495 |
return datestring; |
|
2496 |
} |
|
2497 |
default: |
|
2498 |
break; |
|
2499 |
} |
|
2500 |
return QSqlDriver::formatValue(field, trimStrings); |
|
2501 |
} |
|
2502 |
||
2503 |
QVariant QOCIDriver::handle() const |
|
2504 |
{ |
|
2505 |
return qVariantFromValue(d->env); |
|
2506 |
} |
|
2507 |
||
2508 |
QString QOCIDriver::escapeIdentifier(const QString &identifier, IdentifierType type) const |
|
2509 |
{ |
|
2510 |
QString res = identifier; |
|
2511 |
if(!identifier.isEmpty() && !isIdentifierEscaped(identifier, type)) { |
|
2512 |
res.replace(QLatin1Char('"'), QLatin1String("\"\"")); |
|
2513 |
res.prepend(QLatin1Char('"')).append(QLatin1Char('"')); |
|
2514 |
res.replace(QLatin1Char('.'), QLatin1String("\".\"")); |
|
2515 |
} |
|
2516 |
return res; |
|
2517 |
} |
|
2518 |
||
2519 |
QT_END_NAMESPACE |