-
Notifications
You must be signed in to change notification settings - Fork 0
/
qsql_aci.cc
2705 lines (2422 loc) · 80 KB
/
qsql_aci.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "qsql_aci.h"
#include <qcoreapplication.h>
#include <qvariant.h>
#include <qdatetime.h>
#include <qmetatype.h>
#include <qregexp.h>
#include <qshareddata.h>
#include <qsqlerror.h>
#include <qsqlfield.h>
#include <qsqlindex.h>
#include <qsqlquery.h>
#include <qstringlist.h>
#include <qvarlengtharray.h>
#include <qvector.h>
#include <qdebug.h>
#include "QSqlCachedResult.h"
#include <aci.h>
#ifdef max
#undef max
#endif
#ifdef min
#undef min
#endif
#ifdef _DEBUG
#define QACI_DEBUG
#endif
#include <stdlib.h>
#define QACI_DYNAMIC_CHUNK_SIZE 65535
#define QACI_PREFETCH_MEM 10240
// setting this define will allow using a query from a different
// thread than its database connection.
// warning - this is not fully tested and can lead to race conditions
#define QACI_THREADED
#ifndef ACI_USE_UTF8
#define ACI_USE_UTF8 // this client driver user utf8
#endif
#ifndef ACE_UTF8ID
#define ACE_UTF8ID 871
#endif
Q_DECLARE_OPAQUE_POINTER(ACIEnv*);
Q_DECLARE_METATYPE(ACIEnv*)
Q_DECLARE_OPAQUE_POINTER(ACIStmt*);
Q_DECLARE_METATYPE(ACIStmt*)
QT_BEGIN_NAMESPACE
#if defined (ACI_USE_UTF8)
static const ub2 qOraCharset = ACE_UTF8ID;
#else
static const ub2 qOraCharset = ACI_UCS2ID;
#endif
typedef QVarLengthArray<sb2, 32> IndicatorArray;
typedef QVarLengthArray<ub2, 32> SizeArray;
static QByteArray qMakeOraDate(const QDateTime& dt);
static QDateTime qMakeDate(const char* oraDate);
static QByteArray qMakeACINumber(const qlonglong &ll, ACIError *err);
static QByteArray qMakeACINumber(const qulonglong& ull, ACIError* err);
static qlonglong qMakeLongLong(const char* ACINumber, ACIError* err);
static qulonglong qMakeULongLong(const char* ACINumber, ACIError* err);
static QString qOraWarn(ACIError *err, int *errorCode = 0);
#ifndef Q_CC_SUN
static // for some reason, Sun CC can't use qOraWarning when it's declared static
#endif
void qOraWarning(const char* msg, ACIError *err);
static QSqlError qMakeError(const QString& errString, QSqlError::ErrorType type, ACIError *err);
class QACIRowId : public QSharedData
{
public:
QACIRowId(ACIEnv *env);
~QACIRowId();
ACIRowid *id;
private:
QACIRowId(const QACIRowId &other) : QSharedData(other) { Q_ASSERT(false); }
};
QACIRowId::QACIRowId(ACIEnv *env)
: id(0)
{
ACIDescriptorAlloc(env, reinterpret_cast<dvoid **>(&id),
ACI_DTYPE_ROWID, 0, 0);
}
QACIRowId::~QACIRowId()
{
if (id)
ACIDescriptorFree(id, ACI_DTYPE_ROWID);
}
typedef QSharedDataPointer<QACIRowId> QACIRowIdPointer;
QT_BEGIN_INCLUDE_NAMESPACE
Q_DECLARE_METATYPE(QACIRowIdPointer)
QT_END_INCLUDE_NAMESPACE
class QACICols;
struct QACIResultPrivate;
class QACIResult : public QSqlCachedResult
{
friend class QACIDriver;
friend struct QACIResultPrivate;
friend class QACICols;
public:
QACIResult(const QACIDriver * db, const QACIDriverPrivate* p);
~QACIResult();
bool prepare(const QString& query);
bool exec();
QVariant handle() const;
protected:
bool gotoNext(ValueCache &values, int index);
bool reset(const QString& query);
int size();
int numRowsAffected();
QSqlRecord record() const;
QVariant lastInsertId() const;
bool execBatch(bool arrayBind = false);
void virtual_hook(int id, void *data);
private:
QACIResultPrivate * d;
};
struct QACIResultPrivate
{
QACIResultPrivate(QACIResult *result, const QACIDriverPrivate *driver);
~QACIResultPrivate();
QACICols *cols;
QACIResult *q;
ACIEnv *env;
ACIError *err;
ACISvcCtx *&svc;
ACIStmt *sql;
bool transaction;
int serverVersion;
int prefetchRows, prefetchMem;
void setStatementAttributes();
int bindValue(ACIStmt *sql, ACIBind **hbnd, ACIError *err, int pos,
const QVariant &val, dvoid *indPtr, ub2 *tmpSize, QList<QByteArray> &tmpStorage);
int bindValues(QVector<QVariant> &values, IndicatorArray &indicators, SizeArray &tmpSizes,
QList<QByteArray> &tmpStorage);
void outValues(QVector<QVariant> &values, IndicatorArray &indicators,
QList<QByteArray> &tmpStorage);
inline bool isOutValue(int i) const
{
return q->bindValueType(i) & QSql::Out;
}
inline bool isBinaryValue(int i) const
{
return q->bindValueType(i) & QSql::Binary;
}
void setCharset(dvoid* handle, ub4 type) const
{
int r = 0;
Q_ASSERT(handle);
r = ACIAttrSet(handle,
type,
// this const cast is safe since ACI doesn't touch
// the charset.
const_cast<void *>(static_cast<const void *>(&qOraCharset)),
0,
ACI_ATTR_CHARSET_ID,
err);
if (r != 0)
qOraWarning("QACIResultPrivate::setCharsetI Couldn't set ACI_ATTR_CHARSET_ID: ", err);
}
};
void QACIResultPrivate::setStatementAttributes()
{
Q_ASSERT(sql);
int r = 0;
if (prefetchRows >= 0) {
r = ACIAttrSet(sql,
ACI_HTYPE_STMT,
&prefetchRows,
0,
ACI_ATTR_PREFETCH_ROWS,
err);
if (r != 0)
qOraWarning("QACIResultPrivate::setStatementAttributes:"
" Couldn't set ACI_ATTR_PREFETCH_ROWS: ", err);
}
if (prefetchMem >= 0) {
r = ACIAttrSet(sql,
ACI_HTYPE_STMT,
&prefetchMem,
0,
ACI_ATTR_PREFETCH_MEMORY,
err);
if (r != 0)
qOraWarning("QACIResultPrivate::setStatementAttributes:"
" Couldn't set ACI_ATTR_PREFETCH_MEMORY: ", err);
}
}
int QACIResultPrivate::bindValue(ACIStmt *sql, ACIBind **hbnd, ACIError *err, int pos,
const QVariant &val, dvoid *indPtr, ub2 *tmpSize, QList<QByteArray> &tmpStorage)
{
int r = ACI_SUCCESS;
void *data = const_cast<void *>(val.constData());
switch (val.type()) {
case QVariant::ByteArray:
r = ACIBindByPos(sql, hbnd, err,
pos + 1,
isOutValue(pos)
? const_cast<char *>(reinterpret_cast<QByteArray *>(data)->constData())
: reinterpret_cast<QByteArray *>(data)->data(),
reinterpret_cast<QByteArray *>(data)->size(),
SQLT_BIN, indPtr, 0, 0, 0, 0, ACI_DEFAULT);
break;
case QVariant::Time:
case QVariant::Date:
case QVariant::DateTime: {
QByteArray ba = qMakeOraDate(val.toDateTime());
r = ACIBindByPos(sql, hbnd, err,
pos + 1,
ba.data(),
ba.size(),
SQLT_DAT, indPtr, 0, 0, 0, 0, ACI_DEFAULT);
tmpStorage.append(ba);
break; }
case QVariant::Int:
r = ACIBindByPos(sql, hbnd, err,
pos + 1,
// if it's an out value, the data is already detached
// so the const cast is safe.
const_cast<void *>(data),
sizeof(int),
SQLT_INT, indPtr, 0, 0, 0, 0, ACI_DEFAULT);
break;
case QVariant::UInt:
r = ACIBindByPos(sql, hbnd, err,
pos + 1,
// if it's an out value, the data is already detached
// so the const cast is safe.
const_cast<void *>(data),
sizeof(uint),
SQLT_UIN, indPtr, 0, 0, 0, 0, ACI_DEFAULT);
break;
case QVariant::LongLong:
{
QByteArray ba = qMakeACINumber(val.toLongLong(), err);
r = ACIBindByPos(sql, hbnd, err,
pos + 1,
ba.data(),
ba.size(),
SQLT_VNU, indPtr, 0, 0, 0, 0, ACI_DEFAULT);
tmpStorage.append(ba);
break;
}
case QVariant::ULongLong:
{
QByteArray ba = qMakeACINumber(val.toULongLong(), err);
r = ACIBindByPos(sql, hbnd, err,
pos + 1,
ba.data(),
ba.size(),
SQLT_VNU, indPtr, 0, 0, 0, 0, ACI_DEFAULT);
tmpStorage.append(ba);
break;
}
case QVariant::Double:
r = ACIBindByPos(sql, hbnd, err,
pos + 1,
// if it's an out value, the data is already detached
// so the const cast is safe.
const_cast<void *>(data),
sizeof(double),
SQLT_FLT, indPtr, 0, 0, 0, 0, ACI_DEFAULT);
break;
case QVariant::UserType:
if (val.canConvert<QACIRowIdPointer>() && !isOutValue(pos)) {
// use a const pointer to prevent a detach
const QACIRowIdPointer rptr = qvariant_cast<QACIRowIdPointer>(val);
r = ACIBindByPos(sql, hbnd, err,
pos + 1,
// it's an IN value, so const_cast is ok
const_cast<ACIRowid **>(&rptr->id),
-1,
SQLT_RDD, indPtr, 0, 0, 0, 0, ACI_DEFAULT);
}
else {
qWarning("Unknown bind variable");
r = ACI_ERROR;
}
break;
case QVariant::String: {
const QString s = val.toString();
QByteArray utf8 = s.toUtf8();
if (isBinaryValue(pos)) {
r = ACIBindByPos(sql, hbnd, err,
pos + 1,
utf8.data(),
utf8.size(),
SQLT_LNG, indPtr, 0, 0, 0, 0, ACI_DEFAULT);
tmpStorage.append(utf8);
break;
}
else if (!isOutValue(pos)) {
// don't detach the string
r = ACIBindByPos(sql, hbnd, err,
pos + 1,
// safe since oracle doesn't touch OUT values
utf8.data(),
utf8.size()+1,
SQLT_STR, indPtr, 0, 0, 0, 0, ACI_DEFAULT);
tmpStorage.append(utf8);
if (r == ACI_SUCCESS)
setCharset(*hbnd, ACI_HTYPE_BIND);
break;
}
} // fall through for OUT values
default: {
const QString s = val.toString();
// create a deep-copy
QByteArray utf8 = s.toUtf8();
QByteArray ba(utf8, utf8.size() + 1);
if (isOutValue(pos)) {
ba.reserve((s.capacity() + 1) * sizeof(QChar));
*tmpSize = ba.size();
r = ACIBindByPos(sql, hbnd, err,
pos + 1,
ba.data(),
ba.capacity(),
SQLT_STR, indPtr, tmpSize, 0, 0, 0, OCI_DEFAULT);
}
else {
r = ACIBindByPos(sql, hbnd, err,
pos + 1,
ba.data(),
ba.size(),
SQLT_STR, indPtr, 0, 0, 0, 0, OCI_DEFAULT);
}
if (r == OCI_SUCCESS)
setCharset(*hbnd, OCI_HTYPE_BIND);
tmpStorage.append(ba);
break;
} // default case
} // switch
if (r != ACI_SUCCESS)
qOraWarning("QACIResultPrivate::bindValue:", err);
return r;
}
int QACIResultPrivate::bindValues(QVector<QVariant> &values, IndicatorArray &indicators,
SizeArray &tmpSizes, QList<QByteArray> &tmpStorage)
{
int r = ACI_SUCCESS;
for (int i = 0; i < values.count(); ++i) {
if (isOutValue(i))
values[i].detach();
const QVariant &val = values.at(i);
ACIBind * hbnd = 0; // Oracle handles these automatically
sb2 *indPtr = &indicators[i];
*indPtr = val.isNull() ? -1 : 0;
bindValue(sql, &hbnd, err, i, val, indPtr, &tmpSizes[i], tmpStorage);
}
return r;
}
// will assign out value and remove its temp storage.
static void qOraOutValue(QVariant &value, QList<QByteArray> &storage, ACIError* err)
{
switch (value.type()) {
case QVariant::Time:
value = qMakeDate(storage.takeFirst()).time();
break;
case QVariant::Date:
value = qMakeDate(storage.takeFirst()).date();
break;
case QVariant::DateTime:
value = qMakeDate(storage.takeFirst());
break;
case QVariant::LongLong:
value = qMakeLongLong(storage.takeFirst(), err);
break;
case QVariant::ULongLong:
value = qMakeULongLong(storage.takeFirst(), err);
break;
case QVariant::String:
/*value = QString(
reinterpret_cast<const QChar *>(storage.takeFirst().constData()));*/
// TODO zhaoj
value = QString::fromUtf8(storage.takeFirst().constData());
break;
default:
break; //nothing
}
}
void QACIResultPrivate::outValues(QVector<QVariant> &values, IndicatorArray &indicators,
QList<QByteArray> &tmpStorage)
{
for (int i = 0; i < values.count(); ++i) {
if (!isOutValue(i))
continue;
qOraOutValue(values[i], tmpStorage, err);
QVariant::Type typ = values.at(i).type();
if (indicators[i] == -1) // NULL
values[i] = QVariant(typ);
else
values[i] = QVariant(typ, values.at(i).constData());
}
}
class QACIDriverPrivate
{
public:
QACIDriverPrivate();
ACIEnv *env;
ACISvcCtx *svc;
ACIServer *srvhp;
ACISession *authp;
ACIError *err;
bool transaction;
int serverVersion;
ub4 prefetchRows;
ub2 prefetchMem;
static QString user;
void allocErrorHandle();
};
QString QACIDriverPrivate::user = "";
QACIDriverPrivate::QACIDriverPrivate()
: env(0), svc(0), srvhp(0), authp(0), err(0), transaction(false),
serverVersion(-1), prefetchRows(-1), prefetchMem(QACI_PREFETCH_MEM)
{
}
void QACIDriverPrivate::allocErrorHandle()
{
int r = ACIHandleAlloc(env,
reinterpret_cast<void **>(&err),
ACI_HTYPE_ERROR,
0,
0);
if (r != 0)
qWarning("QACIDriver: unable to allocate error handle");
}
struct OraFieldInfo
{
QString name;
QVariant::Type type;
ub1 oraIsNull;
ub4 oraType;
sb1 oraScale;
ub4 oraLength; // size in bytes
ub4 oraFieldLength; // amount of characters
sb2 oraPrecision;
};
QString qOraWarn(ACIError *err, int *errorCode)
{
sb4 errcode;
text errbuf[1024];
errbuf[0] = 0;
errbuf[1] = 0;
ACIErrorGet(err,
1,
0,
&errcode,
errbuf,
sizeof(errbuf),
ACI_HTYPE_ERROR);
if (errorCode)
*errorCode = errcode;
return QString::fromLocal8Bit((const char*)errbuf);
}
void qOraWarning(const char* msg, ACIError *err)
{
#ifdef QACI_DEBUG
qWarning("%s %s", msg, qUtf8Printable(qOraWarn(err)));
#else
Q_UNUSED(msg);
Q_UNUSED(err);
#endif
}
static int qOraErrorNumber(ACIError *err)
{
sb4 errcode;
ACIErrorGet(err,
1,
0,
&errcode,
0,
0,
ACI_HTYPE_ERROR);
return errcode;
}
QSqlError qMakeError(const QString& errString, QSqlError::ErrorType type, ACIError *err)
{
int errorCode = 0;
const QString oraErrorString = qOraWarn(err, &errorCode);
return QSqlError(errString, oraErrorString, type, errorCode);
}
QVariant::Type qDecodeACIType(const QString& ACItype, QSql::NumericalPrecisionPolicy precisionPolicy)
{
QVariant::Type type = QVariant::Invalid;
if (ACItype == QLatin1String("VARCHAR2") || ACItype == QLatin1String("VARCHAR")
|| ACItype.startsWith(QLatin1String("INTERVAL"))
|| ACItype == QLatin1String("CHAR") || ACItype == QLatin1String("NVARCHAR2")
|| ACItype == QLatin1String("NCHAR"))
type = QVariant::String;
else if (ACItype == QLatin1String("NUMBER")
|| ACItype == QLatin1String("FLOAT")
|| ACItype == QLatin1String("BINARY_FLOAT")
|| ACItype == QLatin1String("BINARY_DOUBLE")) {
switch (precisionPolicy) {
case QSql::LowPrecisionInt32:
type = QVariant::Int;
break;
case QSql::LowPrecisionInt64:
type = QVariant::LongLong;
break;
case QSql::LowPrecisionDouble:
type = QVariant::Double;
break;
case QSql::HighPrecision:
default:
type = QVariant::String;
break;
}
}
else if (ACItype == QLatin1String("LONG") || ACItype == QLatin1String("NCLOB")
|| ACItype == QLatin1String("CLOB"))
type = QVariant::ByteArray;
else if (ACItype == QLatin1String("RAW") || ACItype == QLatin1String("LONG RAW")
|| ACItype == QLatin1String("ROWID") || ACItype == QLatin1String("BLOB")
|| ACItype == QLatin1String("CFILE") || ACItype == QLatin1String("BFILE"))
type = QVariant::ByteArray;
else if (ACItype == QLatin1String("DATE") || ACItype.startsWith(QLatin1String("TIME")))
type = QVariant::DateTime;
else if (ACItype == QLatin1String("UNDEFINED"))
type = QVariant::Invalid;
if (type == QVariant::Invalid)
qWarning("qDecodeACIType: unknown type: %s", ACItype.toLocal8Bit().constData());
return type;
}
QVariant::Type qDecodeACIType(int ACItype, QSql::NumericalPrecisionPolicy precisionPolicy)
{
QVariant::Type type = QVariant::Invalid;
switch (ACItype) {
case SQLT_STR:
case SQLT_VST:
case SQLT_CHR:
case SQLT_AFC:
case SQLT_VCS:
case SQLT_AVC:
case SQLT_RDD:
case SQLT_LNG:
#ifdef SQLT_INTERVAL_YM
case SQLT_INTERVAL_YM:
#endif
#ifdef SQLT_INTERVAL_DS
case SQLT_INTERVAL_DS:
#endif
type = QVariant::String;
break;
case SQLT_INT:
type = QVariant::Int;
break;
case SQLT_FLT:
case SQLT_NUM:
case SQLT_VNU:
case SQLT_UIN:
switch (precisionPolicy) {
case QSql::LowPrecisionInt32:
type = QVariant::Int;
break;
case QSql::LowPrecisionInt64:
type = QVariant::LongLong;
break;
case QSql::LowPrecisionDouble:
type = QVariant::Double;
break;
case QSql::HighPrecision:
default:
type = QVariant::String;
break;
}
break;
case SQLT_VBI:
case SQLT_BIN:
case SQLT_LBI:
case SQLT_LVC:
case SQLT_LVB:
case SQLT_BLOB:
case SQLT_CLOB:
case SQLT_FILE:
case SQLT_NTY:
case SQLT_REF:
case SQLT_RID:
type = QVariant::ByteArray;
break;
case SQLT_DAT:
case SQLT_ODT:
#ifdef SQLT_TIMESTAMP
case SQLT_TIMESTAMP:
case SQLT_TIMESTAMP_TZ:
case SQLT_TIMESTAMP_LTZ:
#endif
type = QVariant::DateTime;
break;
default:
type = QVariant::Invalid;
qWarning("qDecodeACIType: unknown ACI datatype: %d", ACItype);
break;
}
return type;
}
static QSqlField qFromOraInf(const OraFieldInfo &ofi)
{
QSqlField f(ofi.name, ofi.type);
f.setRequired(ofi.oraIsNull == 0);
if (ofi.type == QVariant::String && ofi.oraType != SQLT_NUM && ofi.oraType != SQLT_VNU)
f.setLength(ofi.oraFieldLength);
else
f.setLength(ofi.oraPrecision == 0 ? 38 : int(ofi.oraPrecision));
f.setPrecision(ofi.oraScale);
f.setSqlType(int(ofi.oraType));
return f;
}
/*!
\internal
Convert QDateTime to the internal Oracle DATE format NB!
It does not handle BCE dates.
*/
QByteArray qMakeOraDate(const QDateTime& dt)
{
QByteArray ba;
ba.resize(7);
int year = dt.date().year();
ba[0] = (year / 100) + 100; // century
ba[1] = (year % 100) + 100; // year
ba[2] = dt.date().month();
ba[3] = dt.date().day();
ba[4] = dt.time().hour() + 1;
ba[5] = dt.time().minute() + 1;
ba[6] = dt.time().second() + 1;
return ba;
}
/*!
\internal
Convert qlonglong to the internal Oracle ACINumber format.
*/
QByteArray qMakeACINumber(const qlonglong& ll, ACIError* err)
{
QByteArray ba(sizeof(ACINumber), 0);
ACINumberFromInt(err,
&ll,
sizeof(qlonglong),
ACI_NUMBER_SIGNED,
reinterpret_cast<ACINumber*>(ba.data()));
return ba;
}
/*!
\internal
Convert qulonglong to the internal Oracle ACINumber format.
*/
QByteArray qMakeACINumber(const qulonglong& ull, ACIError* err)
{
QByteArray ba(sizeof(ACINumber), 0);
ACINumberFromInt(err,
&ull,
sizeof(qlonglong),
ACI_NUMBER_UNSIGNED,
reinterpret_cast<ACINumber*>(ba.data()));
return ba;
}
qlonglong qMakeLongLong(const char* acinummber, ACIError* err)
{
qlonglong qll = 0;
ACINumberToInt(err, reinterpret_cast<const ACINumber *>(acinummber), sizeof(qlonglong),
ACI_NUMBER_SIGNED, &qll);
return qll;
}
qulonglong qMakeULongLong(const char* acinummber, ACIError* err)
{
qulonglong qull = 0;
ACINumberToInt(err, reinterpret_cast<const ACINumber *>(acinummber), sizeof(qulonglong),
ACI_NUMBER_UNSIGNED, &qull);
return qull;
}
QDateTime qMakeDate(const char* oraDate)
{
qDebug() << strlen(oraDate);
int century = uchar(oraDate[0]);
if (century >= 100) {
int year = uchar(oraDate[1]);
year = ((century - 100) * 100) + (year - 100);
int month = oraDate[2];
int day = oraDate[3];
int hour = oraDate[4] - 1;
int min = oraDate[5] - 1;
int sec = oraDate[6] - 1;
return QDateTime(QDate(year, month, day), QTime(hour, min, sec));
}
return QDateTime();
}
class QACICols
{
public:
QACICols(int size, QACIResultPrivate* dp);
~QACICols();
int readPiecewise(QVector<QVariant> &values, int index = 0);
int readLOBs(QVector<QVariant> &values, int index = 0);
int fieldFromDefine(ACIDefine* d);
void getValues(QVector<QVariant> &v, int index);
inline int size() { return fieldInf.size(); }
static bool execBatch(QACIResultPrivate *d, QVector<QVariant> &boundValues, bool arrayBind);
QSqlRecord rec;
private:
char* create(int position, int size);
ACILobLocator ** createLobLocator(int position, ACIEnv* env);
OraFieldInfo qMakeOraField(const QACIResultPrivate* p, ACIParam* param) const;
class OraFieldInf
{
public:
OraFieldInf() : data(0), len(0), ind(0), typ(QVariant::Invalid), oraType(0), def(0), lob(0)
{}
~OraFieldInf();
char *data;
int len;
sb2 ind;
QVariant::Type typ;
ub4 oraType;
ACIDefine *def;
ACILobLocator *lob;
};
QVector<OraFieldInf> fieldInf;
const QACIResultPrivate *const d;
};
QACICols::OraFieldInf::~OraFieldInf()
{
delete[] data;
data = nullptr;
if (lob) {
int r = ACIDescriptorFree(lob, ACI_DTYPE_LOB);
if (r != 0)
qWarning("QACICols: Cannot free LOB descriptor");
}
}
QACICols::QACICols(int size, QACIResultPrivate* dp)
: fieldInf(size), d(dp)
{
ub4 dataSize = 0;
ACIDefine* dfn = 0;
int r;
ACIParam* param = 0;
sb4 parmStatus = 0;
ub4 count = 1;
int idx = 0;
parmStatus = ACIParamGet(d->sql,
ACI_HTYPE_STMT,
d->err,
reinterpret_cast<void **>(¶m),
count);
while (parmStatus == ACI_SUCCESS) {
OraFieldInfo ofi = qMakeOraField(d, param);
if (ofi.oraType == SQLT_RDD)
dataSize = 50;
#ifdef SQLT_INTERVAL_YM
#ifdef SQLT_INTERVAL_DS
else if (ofi.oraType == SQLT_INTERVAL_YM || ofi.oraType == SQLT_INTERVAL_DS)
// since we are binding interval datatype as string,
// we are not interested in the number of bytes but characters.
dataSize = 50; // magic number
#endif //SQLT_INTERVAL_DS
#endif //SQLT_INTERVAL_YM
else if (ofi.oraType == SQLT_NUM || ofi.oraType == SQLT_VNU) {
if (ofi.oraPrecision > 0)
dataSize = (ofi.oraPrecision + 1) * sizeof(utext);
else
dataSize = (38 + 1) * sizeof(utext);
}
else
dataSize = ofi.oraLength;
fieldInf[idx].typ = ofi.type;
fieldInf[idx].oraType = ofi.oraType;
rec.append(qFromOraInf(ofi));
switch (ofi.type) {
case QVariant::DateTime:
r = ACIDefineByPos(d->sql,
&dfn,
d->err,
count,
create(idx, dataSize + 1),
dataSize + 1,
SQLT_DAT,
&(fieldInf[idx].ind),
0, 0, ACI_DEFAULT);
break;
case QVariant::Double:
r = ACIDefineByPos(d->sql,
&dfn,
d->err,
count,
create(idx, sizeof(double) - 1),
sizeof(double),
SQLT_FLT,
&(fieldInf[idx].ind),
0, 0, ACI_DEFAULT);
break;
case QVariant::Int:
r = ACIDefineByPos(d->sql,
&dfn,
d->err,
count,
create(idx, sizeof(qint32) - 1),
sizeof(qint32),
SQLT_INT,
&(fieldInf[idx].ind),
0, 0, ACI_DEFAULT);
break;
case QVariant::LongLong:
r = ACIDefineByPos(d->sql,
&dfn,
d->err,
count,
create(idx, sizeof(ACINumber)),
sizeof(ACINumber),
SQLT_VNU,
&(fieldInf[idx].ind),
0, 0, ACI_DEFAULT);
break;
case QVariant::ByteArray:
// RAW and LONG RAW fields can't be bound to LOB locators
if (ofi.oraType == SQLT_BIN) {
// qDebug("binding SQLT_BIN");
r = ACIDefineByPos(d->sql,
&dfn,
d->err,
count,
create(idx, dataSize),
dataSize,
SQLT_BIN,
&(fieldInf[idx].ind),
0, 0, ACI_DYNAMIC_FETCH);
}
else if (ofi.oraType == SQLT_LBI) {
// qDebug("binding SQLT_LBI");
r = ACIDefineByPos(d->sql,
&dfn,
d->err,
count,
0,
SB4MAXVAL,
SQLT_LBI,
&(fieldInf[idx].ind),
0, 0, ACI_DYNAMIC_FETCH);
}
else if (ofi.oraType == SQLT_CLOB) {
r = ACIDefineByPos(d->sql,
&dfn,
d->err,
count,
createLobLocator(idx, d->env),
-1,
SQLT_CLOB,
&(fieldInf[idx].ind),
0, 0, ACI_DEFAULT);
}
else {
// qDebug("binding SQLT_BLOB");
r = ACIDefineByPos(d->sql,
&dfn,
d->err,
count,
createLobLocator(idx, d->env),
-1,
SQLT_BLOB,
&(fieldInf[idx].ind),
0, 0, ACI_DEFAULT);
}
break;
case QVariant::String:
if (ofi.oraType == SQLT_LNG) {
r = ACIDefineByPos(d->sql,
&dfn,
d->err,
count,
0,
SB4MAXVAL,
SQLT_LNG,
&(fieldInf[idx].ind),
0, 0, ACI_DYNAMIC_FETCH);
}
else {
dataSize += dataSize + sizeof(char);
// TODO zhaoj
//qDebug("ACIDefineByPosStr(%d): %d", count, dataSize);
r = ACIDefineByPos(d->sql,
&dfn,
d->err,
count,
create(idx, dataSize),
dataSize,
SQLT_STR,
&(fieldInf[idx].ind),
0, 0, ACI_DEFAULT);
if (r == 0)
d->setCharset(dfn, ACI_HTYPE_DEFINE);
}
break;
default:
// this should make enough space even with character encoding
dataSize = (dataSize + 1) * sizeof(utext);
//qDebug("ACIDefineByPosDef(%d): %d", count, dataSize);
r = ACIDefineByPos(d->sql,
&dfn,
d->err,
count,
create(idx, dataSize),
dataSize + 1,
SQLT_STR,
&(fieldInf[idx].ind),
0, 0, ACI_DEFAULT);
break;
}
if (r != 0)
qOraWarning("QACICols::bind:", d->err);
fieldInf[idx].def = dfn;
++count;
++idx;
parmStatus = ACIParamGet(d->sql,
ACI_HTYPE_STMT,