/
qsql_ocispatial.cpp
4407 lines (3933 loc) · 130 KB
/
qsql_ocispatial.cpp
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
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtSql module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
** Oracle Spatial support: (C) 2012-2013 Juergen E. Fischer < jef at norbit dot de >, norBIT GmbH
**
****************************************************************************/
// #define QOCISPATIAL_DEBUG
#ifndef QOCISPATIAL_DEBUG
#define QT_NO_DEBUG_OUTPUT
#endif
#if __cplusplus >= 201500
#define FALLTHROUGH [[fallthrough]];
#elif defined(__clang__)
#define FALLTHROUGH [[clang::fallthrough]];
#elif defined(__GNUC__) && __GNUC__ >= 7
#define FALLTHROUGH [[gnu::fallthrough]];
#else
#define FALLTHROUGH
#endif
#include "qsql_ocispatial.h"
#include "wkbptr.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 <QtSql/private/qsqlcachedresult_p.h>
#include <QtSql/private/qsqldriver_p.h>
#include <qstringlist.h>
#include <qvarlengtharray.h>
#include <qvector.h>
#include <qdebug.h>
#include <cmath>
#ifdef Q_OS_WIN
#include <winsock.h>
#else
#include <netinet/in.h>
#endif
// This is needed for oracle oci when compiling with mingw-w64 headers
#if defined(__MINGW64_VERSION_MAJOR) && defined(_WIN64)
#define _int64 __int64
#endif
#include <oci.h>
#ifdef max
#undef max
#endif
#ifdef min
#undef min
#endif
#include <cstdlib>
#define QOCISPATIAL_DYNAMIC_CHUNK_SIZE 65535
#define QOCISPATIAL_PREFETCH_ROWS 10000
#define QOCISPATIAL_PREFETCH_MEM 8388608 // 8MB
// 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 QOCISPATIAL_THREADED
Q_DECLARE_OPAQUE_POINTER( OCIEnv * )
Q_DECLARE_OPAQUE_POINTER( OCIStmt * )
Q_DECLARE_METATYPE( OCIEnv * )
Q_DECLARE_METATYPE( OCIStmt * )
QT_BEGIN_NAMESPACE
#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
enum { QOCISpatialEncoding = 2002 }; // AL16UTF16LE
#else
enum { QOCISpatialEncoding = 2000 }; // AL16UTF16
#endif
#ifdef OCI_ATTR_CHARSET_FORM
// Always set the OCI_ATTR_CHARSET_FORM to SQLCS_NCHAR is safe
// because Oracle server will deal with the implicit Conversion
// Between CHAR and NCHAR.
// see: http://download.oracle.com/docs/cd/A91202_01/901_doc/appdev.901/a89857/oci05bnd.htm#422705
static const ub1 qOraCharsetForm = SQLCS_NCHAR;
#endif
#if defined (OCI_UTF16ID)
static const ub2 qOraCharset = OCI_UTF16ID;
#else
static const ub2 qOraCharset = OCI_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 qMakeOCINumber( const qlonglong &ll, OCIError *err );
static QByteArray qMakeOCINumber( const qulonglong &ull, OCIError *err );
static qlonglong qMakeLongLong( const char *ociNumber, OCIError *err );
static qulonglong qMakeULongLong( const char *ociNumber, OCIError *err );
static QString qOraWarn( OCIError *err, int *errorCode = nullptr );
#ifndef Q_CC_SUN
static // for some reason, Sun CC can't use qOraWarning when it's declared static
#endif
void qOraWarningAt( const char *msg, OCIError *err, const char *function, const char *file, int line );
static QSqlError qMakeError( const QString &errString, QSqlError::ErrorType type, OCIError *err );
#ifndef _MSC_VER
#define qOraWarning(msg,err) qOraWarningAt(msg,err,__PRETTY_FUNCTION__,__FILE__,__LINE__)
#define OCI_VERIFY(x) do { oci_verify(__PRETTY_FUNCTION__, __FILE__, __LINE__, x, #x); } while(0)
#define OCI_VERIFY_E(e,x) do { oci_verify(e,__PRETTY_FUNCTION__, __FILE__, __LINE__, x, #x); } while(0)
#else
#define qOraWarning(msg,err) qOraWarningAt(msg,err,__FUNCTION__,__FILE__,__LINE__)
#define OCI_VERIFY(x) do { oci_verify(__FUNCTION__, __FILE__, __LINE__, x, #x); } while(0)
#define OCI_VERIFY_E(e,x) do { oci_verify(e,__FUNCTION__, __FILE__, __LINE__, x, #x); } while(0)
#endif
void oci_verify( OCIError *err, const char *function, const char *file, int line, int result, const char *expression )
{
if ( result == OCI_SUCCESS || result == OCI_SUCCESS_WITH_INFO )
return;
qWarning( "%s:%d (%s) OCI error %s = %d [%s]", file, line, function, expression, result, qPrintable( qOraWarn( err ) ) );
throw result;
}
void oci_verify( const char *function, const char *file, int line, int result, const char *expression )
{
if ( result == OCI_SUCCESS || result == OCI_SUCCESS_WITH_INFO )
return;
qWarning( "%s:%d (%s) OCI error %s = %d", file, line, function, expression, result );
throw result;
}
#ifdef QOCISPATIAL_DEBUG
class enter
{
const char *mFunction = nullptr;
const char *mFile = nullptr;
int mLine;
static int level;
public:
enter( const char *function, const char *file, int line )
: mFunction( function ), mFile( file ), mLine( line )
{
qDebug( "+%*sEntering %s at %s:%d", level, "", mFunction, mFile, mLine );
level++;
}
~enter()
{
level--;
qDebug( "-%*sLeaving %s", level, "", mFunction );
}
};
#ifdef _MSC_VER
#define ENTER enter here(__FUNCTION__,__FILE__,__LINE__);
#else
#define ENTER enter here(__PRETTY_FUNCTION__,__FILE__,__LINE__);
#endif
int enter::level = 0;
#else
#define ENTER
#endif
enum WKBType
{
WKBUnknown = 0,
WKBPoint = 1,
WKBLineString,
WKBPolygon,
WKBMultiPoint,
WKBMultiLineString,
WKBMultiPolygon,
WKBCircularString = 8,
WKBCompoundCurve = 9,
WKBCurvePolygon = 10,
WKBMultiCurve = 11,
WKBMultiSurface = 12,
WKBNoGeometry = 100, //attributes only
WKBCircularStringZ = 1008,
WKBCompoundCurveZ = 1009,
WKBCurvePolygonZ = 1010,
WKBMultiCurveZ = 1011,
WKBMultiSurfaceZ = 1012,
WKBPoint25D = 0x80000001,
WKBLineString25D,
WKBPolygon25D,
WKBMultiPoint25D,
WKBMultiLineString25D,
WKBMultiPolygon25D,
#if 0
WKBGeometry = 0,
WKBPoint = 1,
WKBLineString = 2,
WKBPolygon = 3,
WKBMultiPoint = 4,
WKBMultiLineString = 5,
WKBMultiPolygon = 6,
WKBGeometryCollection = 7,
WKBPolyhedralSurface = 15,
WKBTIN = 16,
WKBTriangle = 17,
WKBGeometryZ = 1000,
WKBPointZ = 1001,
WKBLineStringZ = 1002,
WKBPolygonZ = 1003,
WKBMultiPointZ = 1004,
WKBMultiLineStringZ = 1005,
WKBMultiPolygonZ = 1006,
WKBGeometryCollectionZ = 1007,
WKBPolyhedralSurfaceZ = 1015,
WKBTINZ = 1016,
WKBTriangleZ = 1017,
WKBGeometryM = 2000,
WKBPointM = 2001,
WKBLineStringM = 2002,
WKBPolygonM = 2003,
WKBMultiPointM = 2004,
WKBMultiLineStringM = 2005,
WKBMultiPolygonM = 2006,
WKBGeometryCollectionM = 2007,
WKBPolyhedralSurfaceM = 2015,
WKBTINM = 2016,
WKBTriangleM = 2017,
WKBGeometryZM = 3000,
WKBPointZM = 3001,
WKBLineStringZM = 3002,
WKBPolygonZM = 3003,
WKBMultiPointZM = 3004,
WKBMultiLineStringZM = 3005,
WKBMultiPolygonZM = 3006,
WKBGeometryCollectionZM = 3007,
WKBPolyhedralSurfaceZM = 3015,
WKBTINZM = 3016,
WKBTriangleZM = 3017,
#endif
};
class QOCISpatialRowId: public QSharedData
{
public:
explicit QOCISpatialRowId( OCIEnv *env );
~QOCISpatialRowId();
OCIRowid *id = nullptr;
private:
QOCISpatialRowId( const QOCISpatialRowId &other ): QSharedData( other ) { Q_ASSERT( false ); }
QOCISpatialRowId &operator= ( const QOCISpatialRowId & ) = delete;
};
QOCISpatialRowId::QOCISpatialRowId( OCIEnv *env )
{
OCIDescriptorAlloc( env, reinterpret_cast<dvoid **>( &id ),
OCI_DTYPE_ROWID, 0, nullptr );
}
QOCISpatialRowId::~QOCISpatialRowId()
{
if ( id )
OCIDescriptorFree( id, OCI_DTYPE_ROWID );
}
typedef QSharedDataPointer<QOCISpatialRowId> QOCISpatialRowIdPointer;
QT_BEGIN_INCLUDE_NAMESPACE
Q_DECLARE_METATYPE( QOCISpatialRowIdPointer )
QT_END_INCLUDE_NAMESPACE
class QOCISpatialDriverPrivate : public QSqlDriverPrivate
{
Q_DECLARE_PUBLIC( QOCISpatialDriver )
public:
QOCISpatialDriverPrivate();
OCIEnv *env = nullptr;
OCISvcCtx *svc = nullptr;
OCIServer *srvhp = nullptr;
OCISession *authp = nullptr;
OCIError *err = nullptr;
bool transaction = false;
int serverVersion = -1;
ub4 prefetchRows = QOCISPATIAL_PREFETCH_ROWS;
ub4 prefetchMem = QOCISPATIAL_PREFETCH_MEM;
bool commitOnSuccess = true;
QString user;
OCIType *geometryTDO = nullptr;
void allocErrorHandle();
OCIType *tdo( QString type );
};
class QOCISpatialCols;
class QOCISpatialResultPrivate;
class QOCISpatialResult: public QSqlCachedResult
{
Q_DECLARE_PRIVATE( QOCISpatialResult )
friend class QOCISpatialDriver;
friend class QOCISpatialCols;
public:
QOCISpatialResult( const QOCISpatialDriver *db );
~QOCISpatialResult() override;
bool prepare( const QString &query ) override;
bool exec() override;
QVariant handle() const override;
protected:
bool gotoNext( ValueCache &values, int index ) override;
bool reset( const QString &query ) override;
int size() override;
int numRowsAffected() override;
QSqlRecord record() const override;
QVariant lastInsertId() const override;
bool execBatch( bool arrayBind = false ) override;
void virtual_hook( int id, void *data ) override;
};
struct QOCISDOPointObj
{
OCINumber x;
OCINumber y;
OCINumber z;
};
struct QOCISDOGeometryObj
{
OCINumber gtype;
OCINumber srid;
QOCISDOPointObj point;
OCIArray *elem_info = nullptr;
OCIArray *ordinates = nullptr;
};
struct QOCISDOPointInd
{
OCIInd _atomic;
OCIInd x;
OCIInd y;
OCIInd z;
};
struct QOCISDOGeometryInd
{
OCIInd _atomic;
OCIInd gtype;
OCIInd srid;
QOCISDOPointInd point;
OCIInd elem_info;
OCIInd ordinates;
};
class QOCISpatialResultPrivate: public QSqlCachedResultPrivate
{
public:
Q_DECLARE_PUBLIC( QOCISpatialResult )
Q_DECLARE_SQLDRIVER_PRIVATE( QOCISpatialDriver )
QOCISpatialResultPrivate( QOCISpatialResult *q, const QOCISpatialDriver *drv );
~QOCISpatialResultPrivate() override;
QOCISpatialCols *cols = nullptr;
OCIEnv *env = nullptr;
OCIError *err = nullptr;
OCISvcCtx *&svc;
OCIStmt *sql = nullptr;
QList<QOCISDOGeometryObj *> sdoobj;
QList<QOCISDOGeometryInd *> sdoind;
bool transaction;
bool commitOnSuccess = true;
int serverVersion;
ub4 prefetchRows, prefetchMem;
OCIType *geometryTDO = nullptr;
QOCISDOGeometryObj *geometryObj = nullptr;
QOCISDOGeometryInd *geometryInd = nullptr;
void setStatementAttributes();
int bindValue( OCIStmt *sql, OCIBind **hbnd, OCIError *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
{ Q_Q( const QOCISpatialResult ); return q->bindValueType( i ) & QSql::Out; }
inline bool isBinaryValue( int i ) const
{ Q_Q( const QOCISpatialResult ); return q->bindValueType( i ) & QSql::Binary; }
void setCharset( dvoid *handle, ub4 type ) const
{
int r = OCI_SUCCESS;
Q_ASSERT( handle );
#ifdef OCI_ATTR_CHARSET_FORM
r = OCIAttrSet( handle,
type,
// this const cast is safe since OCI doesn't touch
// the charset.
const_cast<void *>( static_cast<const void *>( &qOraCharsetForm ) ),
0,
OCI_ATTR_CHARSET_FORM,
//Strange Oracle bug: some Oracle servers crash the server process with non-zero error handle (mostly for 10g).
//So ignore the error message here.
nullptr );
#ifdef QOCISPATIAL_DEBUG
if ( r != OCI_SUCCESS )
qWarning( "QOCISpatialResultPrivate::setCharset: Couldn't set OCI_ATTR_CHARSET_FORM." );
#endif
#endif
r = OCIAttrSet( handle,
type,
// this const cast is safe since OCI doesn't touch
// the charset.
const_cast<void *>( static_cast<const void *>( &qOraCharset ) ),
0,
OCI_ATTR_CHARSET_ID,
err );
if ( r != OCI_SUCCESS )
qOraWarning( "Couldn't set OCI_ATTR_CHARSET_ID: ", err );
}
};
void QOCISpatialResultPrivate::setStatementAttributes()
{
ENTER
Q_ASSERT( sql );
int r = OCI_SUCCESS;
r = OCIAttrSet( sql,
OCI_HTYPE_STMT,
&prefetchRows,
0,
OCI_ATTR_PREFETCH_ROWS,
err );
if ( r != OCI_SUCCESS )
qOraWarning( "Couldn't set OCI_ATTR_PREFETCH_ROWS: ", err );
r = OCIAttrSet( sql,
OCI_HTYPE_STMT,
&prefetchMem,
0,
OCI_ATTR_PREFETCH_MEMORY,
err );
if ( r != OCI_SUCCESS )
qOraWarning( "QOCISpatialResultPrivate::setStatementAttributes:"
" Couldn't set OCI_ATTR_PREFETCH_MEMORY: ", err );
}
int QOCISpatialResultPrivate::bindValue( OCIStmt *sql, OCIBind **hbnd, OCIError *err, int pos,
const QVariant &val, dvoid *indPtr, ub2 *tmpSize, QList<QByteArray> &tmpStorage )
{
ENTER
int r = OCI_SUCCESS;
void *data = const_cast<void *>( val.constData() );
switch ( val.type() )
{
case QVariant::ByteArray:
r = OCIBindByPos( 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, nullptr, nullptr, 0, nullptr, OCI_DEFAULT );
qDebug() << "inout" << isOutValue( pos ) << "bytearray size" << reinterpret_cast<QByteArray *>( data )->size() << "r" << r;
break;
case QVariant::Time:
case QVariant::Date:
case QVariant::DateTime:
{
QByteArray ba = qMakeOraDate( val.toDateTime() );
r = OCIBindByPos( sql, hbnd, err,
pos + 1,
ba.data(),
ba.size(),
SQLT_DAT, indPtr, nullptr, nullptr, 0, nullptr, OCI_DEFAULT );
tmpStorage.append( ba );
break;
}
case QVariant::Int:
r = OCIBindByPos( 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, nullptr, nullptr, 0, nullptr, OCI_DEFAULT );
break;
case QVariant::UInt:
r = OCIBindByPos( 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, nullptr, nullptr, 0, nullptr, OCI_DEFAULT );
break;
case QVariant::LongLong:
{
QByteArray ba = qMakeOCINumber( val.toLongLong(), err );
r = OCIBindByPos( sql, hbnd, err,
pos + 1,
ba.data(),
ba.size(),
SQLT_VNU, indPtr, nullptr, nullptr, 0, nullptr, OCI_DEFAULT );
tmpStorage.append( ba );
break;
}
case QVariant::ULongLong:
{
QByteArray ba = qMakeOCINumber( val.toULongLong(), err );
r = OCIBindByPos( sql, hbnd, err,
pos + 1,
ba.data(),
ba.size(),
SQLT_VNU, indPtr, nullptr, nullptr, 0, nullptr, OCI_DEFAULT );
tmpStorage.append( ba );
break;
}
case QVariant::Double:
r = OCIBindByPos( 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, nullptr, nullptr, 0, nullptr, OCI_DEFAULT );
break;
case QVariant::UserType:
if ( val.canConvert<QOCISpatialGeometry>() && !isOutValue( pos ) )
{
try
{
if ( !geometryObj )
{
OCI_VERIFY_E( err, OCIObjectNew( env, err, svc, OCI_TYPECODE_OBJECT, geometryTDO, ( dvoid * ) nullptr, OCI_DURATION_SESSION, 1, ( dvoid ** ) &geometryObj ) );
if ( !geometryObj )
{
throw OCI_ERROR;
}
OCI_VERIFY_E( err, OCIObjectGetInd( env, err, geometryObj, ( void ** ) &geometryInd ) );
if ( !geometryInd )
{
throw OCI_ERROR;
}
}
OCI_VERIFY_E( err, OCIBindByPos( sql, hbnd, err, pos + 1, nullptr, 0, SQLT_NTY, indPtr, nullptr, nullptr, 0, nullptr, OCI_DEFAULT ) );
OCI_VERIFY_E( err, OCIBindObject( *hbnd, err, geometryTDO, ( dvoid ** )&geometryObj, nullptr, ( dvoid ** ) &geometryInd, nullptr ) );
const QOCISpatialGeometry &g = qvariant_cast<QOCISpatialGeometry>( val );
int n;
OCI_VERIFY_E( err, OCICollSize( env, err, geometryObj->elem_info, &n ) );
OCI_VERIFY_E( err, OCICollTrim( env, err, n, geometryObj->elem_info ) );
OCI_VERIFY_E( err, OCICollSize( env, err, geometryObj->ordinates, &n ) );
OCI_VERIFY_E( err, OCICollTrim( env, err, n, geometryObj->ordinates ) );
if ( g.isNull )
{
geometryInd->_atomic = OCI_IND_NULL;
}
else
{
geometryInd->_atomic = OCI_IND_NOTNULL;
geometryInd->gtype = g.gtype < 0 ? OCI_IND_NULL : OCI_IND_NOTNULL;
geometryInd->srid = g.srid < 0 ? OCI_IND_NULL : OCI_IND_NOTNULL;
OCI_VERIFY_E( err, OCINumberFromInt( err, &g.gtype, sizeof( int ), OCI_NUMBER_SIGNED, &geometryObj->gtype ) );
OCI_VERIFY_E( err, OCINumberFromInt( err, &g.srid, sizeof( int ), OCI_NUMBER_SIGNED, &geometryObj->srid ) );
if ( SDO_GTYPE_TT( g.gtype ) == GtPoint )
{
geometryInd->point._atomic = OCI_IND_NOTNULL;
geometryInd->point.x = OCI_IND_NOTNULL;
geometryInd->point.y = OCI_IND_NOTNULL;
geometryInd->point.z = OCI_IND_NOTNULL;
geometryInd->elem_info = OCI_IND_NULL;
geometryInd->ordinates = OCI_IND_NULL;
OCI_VERIFY_E( err, OCINumberFromReal( err, &g.x, sizeof( double ), &geometryObj->point.x ) );
OCI_VERIFY_E( err, OCINumberFromReal( err, &g.y, sizeof( double ), &geometryObj->point.y ) );
OCI_VERIFY_E( err, OCINumberFromReal( err, &g.z, sizeof( double ), &geometryObj->point.z ) );
}
else
{
geometryInd->point._atomic = OCI_IND_NULL;
geometryInd->elem_info = g.eleminfo.size() == 0 ? OCI_IND_NULL : OCI_IND_NOTNULL;
geometryInd->ordinates = g.ordinates.size() == 0 ? OCI_IND_NULL : OCI_IND_NOTNULL;
for ( int e : g.eleminfo )
{
OCINumber n;
OCI_VERIFY_E( err, OCINumberFromInt( err, &e, sizeof( int ), OCI_NUMBER_SIGNED, &n ) );
OCI_VERIFY_E( err, OCICollAppend( env, err, &n, nullptr, geometryObj->elem_info ) );
}
for ( double o : g.ordinates )
{
OCINumber n;
OCI_VERIFY_E( err, OCINumberFromReal( err, &o, sizeof( double ), &n ) );
OCI_VERIFY_E( err, OCICollAppend( env, err, &n, nullptr, geometryObj->ordinates ) );
}
}
}
}
catch ( int e )
{
r = e;
}
}
else if ( val.canConvert<QOCISpatialRowIdPointer>() && !isOutValue( pos ) )
{
// use a const pointer to prevent a detach
const QOCISpatialRowIdPointer rptr = qvariant_cast<QOCISpatialRowIdPointer>( val );
r = OCIBindByPos( sql, hbnd, err,
pos + 1,
// it's an IN value, so const_cast is OK
const_cast<OCIRowid **>( &rptr->id ),
-1,
SQLT_RDD, indPtr, nullptr, nullptr, 0, nullptr, OCI_DEFAULT );
}
else
{
qWarning( "Unknown bind variable" );
r = OCI_ERROR;
}
break;
case QVariant::String:
{
const QString s = val.toString();
if ( isBinaryValue( pos ) )
{
r = OCIBindByPos( sql, hbnd, err,
pos + 1,
const_cast<ushort *>( s.utf16() ),
s.length() * sizeof( QChar ),
SQLT_LNG, indPtr, nullptr, nullptr, 0, nullptr, OCI_DEFAULT );
break;
}
else if ( !isOutValue( pos ) )
{
// don't detach the string
r = OCIBindByPos( sql, hbnd, err,
pos + 1,
// safe since oracle doesn't touch OUT values
const_cast<ushort *>( s.utf16() ),
( s.length() + 1 ) * sizeof( QChar ),
SQLT_STR, indPtr, nullptr, nullptr, 0, nullptr, OCI_DEFAULT );
if ( r == OCI_SUCCESS )
setCharset( *hbnd, OCI_HTYPE_BIND );
break;
}
}
FALLTHROUGH
default:
{
const QString s = val.toString();
// create a deep-copy
QByteArray ba( reinterpret_cast<const char *>( s.utf16() ), ( s.length() + 1 ) * sizeof( QChar ) );
if ( isOutValue( pos ) )
{
ba.reserve( ( s.capacity() + 1 ) * sizeof( QChar ) );
*tmpSize = ba.size();
r = OCIBindByPos( sql, hbnd, err,
pos + 1,
ba.data(),
ba.capacity(),
SQLT_STR, indPtr, tmpSize, nullptr, 0, nullptr, OCI_DEFAULT );
tmpStorage.append( ba );
}
else
{
r = OCIBindByPos( sql, hbnd, err,
pos + 1,
ba.data(),
ba.size(),
SQLT_STR, indPtr, nullptr, nullptr, 0, nullptr, OCI_DEFAULT );
}
if ( r == OCI_SUCCESS )
setCharset( *hbnd, OCI_HTYPE_BIND );
break;
} // default case
} // switch
if ( r != OCI_SUCCESS )
qOraWarning( "QOCISpatialResultPrivate::bindValue:", err );
return r;
}
int QOCISpatialResultPrivate::bindValues( QVector<QVariant> &values, IndicatorArray &indicators,
SizeArray &tmpSizes, QList<QByteArray> &tmpStorage )
{
ENTER
int r = OCI_SUCCESS;
for ( int i = 0; i < values.count(); ++i )
{
if ( isOutValue( i ) )
values[i].detach();
const QVariant &val = values.at( i );
OCIBind *hbnd = nullptr; // 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, OCIError *err )
{
ENTER
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() ) );
break;
default:
break; //nothing
}
}
void QOCISpatialResultPrivate::outValues( QVector<QVariant> &values, IndicatorArray &indicators,
QList<QByteArray> &tmpStorage )
{
ENTER
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() );
}
}
QOCISpatialDriverPrivate::QOCISpatialDriverPrivate()
{
ENTER
}
void QOCISpatialDriverPrivate::allocErrorHandle()
{
ENTER
int r = OCIHandleAlloc( env,
reinterpret_cast<void **>( &err ),
OCI_HTYPE_ERROR,
0,
nullptr );
if ( r != OCI_SUCCESS )
qWarning( "QOCISpatialDriver: unable to allocate error handle" );
}
OCIType *QOCISpatialDriverPrivate::tdo( QString type )
{
OCIParam *paramp = nullptr;
OCIRef *type_ref = nullptr;
OCIType *tdo = nullptr;
OCIDescribe *dschp = nullptr;
try
{
OCI_VERIFY( OCIHandleAlloc( env, ( void ** ) & dschp, OCI_HTYPE_DESCRIBE, 0, nullptr ) );
OCI_VERIFY_E( err, OCIDescribeAny( svc, err, ( dvoid * ) type.utf16(), type.length() * sizeof( QChar ), OCI_OTYPE_NAME, OCI_DEFAULT, OCI_PTYPE_TYPE, dschp ) );
OCI_VERIFY_E( err, OCIAttrGet( dschp, OCI_HTYPE_DESCRIBE, ¶mp, nullptr, OCI_ATTR_PARAM, err ) );
OCI_VERIFY_E( err, OCIAttrGet( paramp, OCI_DTYPE_PARAM, &type_ref, nullptr, OCI_ATTR_REF_TDO, err ) );
OCI_VERIFY_E( err, OCIObjectPin( env, err, type_ref, nullptr, OCI_PIN_ANY, OCI_DURATION_SESSION, OCI_LOCK_NONE, ( dvoid ** ) &tdo ) );
}
catch ( int r )
{
Q_UNUSED( r )
return nullptr;
}
return tdo;
}
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 oraTypeName;
OCIType *oraOCIType = nullptr;
};
QString qOraWarn( OCIError *err, int *errorCode )
{
sb4 errcode;
text errbuf[1024];
errbuf[0] = 0;
errbuf[1] = 0;
OCIErrorGet( err,
1,
nullptr,
&errcode,
errbuf,
sizeof( errbuf ),
OCI_HTYPE_ERROR );
if ( errorCode )
*errorCode = errcode;
return QString( reinterpret_cast<const QChar *>( errbuf ) );
}
void qOraWarningAt( const char *msg, OCIError *err, const char *function, const char *file, int line )
{
qWarning( "%s: %d: (%s) %s [%s]", file, line, function, msg, qPrintable( qOraWarn( err ) ) );
}
static int qOraErrorNumber( OCIError *err )
{
ENTER
sb4 errcode;
OCIErrorGet( err,
1,
nullptr,
&errcode,
nullptr,
0,
OCI_HTYPE_ERROR );
return errcode;
}
QSqlError qMakeError( const QString &errString, QSqlError::ErrorType type, OCIError *err )
{
ENTER
int errorCode = 0;
const QString oraErrorString = qOraWarn( err, &errorCode );
return QSqlError( errString, oraErrorString, type, QString::number( errorCode ) );
}
QVariant::Type qDecodeOCIType( const QString &ocitype, QSql::NumericalPrecisionPolicy precisionPolicy )
{
ENTER
QVariant::Type type = QVariant::Invalid;
qDebug( "qDecodeOCIType(ocitype=%s, precisionPolicy=%d)\n", ocitype.toLocal8Bit().constData(), precisionPolicy );
if ( ocitype == QLatin1String( "VARCHAR2" ) || ocitype == QLatin1String( "VARCHAR" )
|| ocitype.startsWith( QLatin1String( "INTERVAL" ) )
|| ocitype == QLatin1String( "CHAR" ) || ocitype == QLatin1String( "NVARCHAR2" )
|| ocitype == QLatin1String( "NCHAR" ) )
type = QVariant::String;
else if ( ocitype == QLatin1String( "NUMBER" )
|| ocitype == QLatin1String( "FLOAT" )
|| ocitype == QLatin1String( "BINARY_FLOAT" )
|| ocitype == 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 ( ocitype == QLatin1String( "LONG" ) || ocitype == QLatin1String( "NCLOB" )
|| ocitype == QLatin1String( "CLOB" ) )
type = QVariant::ByteArray;
else if ( ocitype == QLatin1String( "RAW" ) || ocitype == QLatin1String( "LONG RAW" )
|| ocitype == QLatin1String( "ROWID" ) || ocitype == QLatin1String( "BLOB" )
|| ocitype == QLatin1String( "CFILE" ) || ocitype == QLatin1String( "BFILE" ) )
type = QVariant::ByteArray;
else if ( ocitype == QLatin1String( "DATE" ) || ocitype.startsWith( QLatin1String( "TIME" ) ) )
type = QVariant::DateTime;
else if ( ocitype == QLatin1String( "UNDEFINED" ) )
type = QVariant::Invalid;
if ( type == QVariant::Invalid )
qWarning( "qDecodeOCIType: unknown type: %s", ocitype.toLocal8Bit().constData() );
return type;
}
QVariant::Type qDecodeOCIType( int ocitype, QSql::NumericalPrecisionPolicy precisionPolicy )
{
ENTER
QVariant::Type type = QVariant::Invalid;
qDebug( "qDecodeOCIType(ocitype=%d, precisionPolicy=%d)\n", ocitype, precisionPolicy );
switch ( ocitype )
{
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