forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkODBCQuery.cxx
1902 lines (1615 loc) · 52.4 KB
/
vtkODBCQuery.cxx
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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkODBCQuery.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
/*
* Microsoft's own version of sqltypes.h relies on some typedefs and
* macros in windows.h. This next fragment tells VTK to include the
* whole thing without any of its usual #defines to keep the size
* manageable. No WIN32_LEAN_AND_MEAN for us!
*/
#if defined(_WIN32) && !defined(__CYGWIN__)
# define VTK_WINDOWS_FULL
# include <vtkWindows.h>
#endif
#include "vtkODBCQuery.h"
#include "vtkODBCDatabase.h"
#include "vtkODBCInternals.h"
#include <vtkBitArray.h>
#include <vtkObjectFactory.h>
#include <vtkStringArray.h>
#include <vtkVariant.h>
#include <vtkVariantArray.h>
#include <assert.h>
#include <vtksys/ios/sstream>
#include <vtksys/stl/vector>
#include <sql.h>
#include <sqlext.h>
// ----------------------------------------------------------------------
vtkStandardNewMacro(vtkODBCQuery);
static vtkStdString GetErrorMessage(SQLSMALLINT handleType, SQLHANDLE handle, int *code=0);
/*
* Bound Parameters and ODBC
*
* ODBC handles bound parameters by requiring that the user pass in
* buffers containing data for each parameter to be bound. These are
* bound to the statement using SQLBindParam. The statement must have
* been prepared using SQLPrepare. Those buffers need to be freed
* when they're no longer needed.
*
* I'm going to handle this by using my own class
* (vtkODBCBoundParameter) to hold all the information the user passes
* in. This is the same sort of trick I use for the MySQL bound
* parameter support. At execution time I'll take the parameters and
* attach them all to the statement. The vtkODBCBoundParameter
* instances will each own the buffesr for their data.
*
* This is slightly inefficient in that it will generate
* a few tiny little new[] requests. If this ever becomes a problem,
* we can allocate a fixed-size buffer (8 or 16 bytes) inside
* vtkODBCBoundParameter and use that for the data storage by
* default. That will still require special-case handling for blobs
* and strings.
*
* The vtkODBCQueryInternals class will handle the bookkeeping for
* which parameters are and aren't bound at any given time.
*/
// ----------------------------------------------------------------------
class vtkODBCBoundParameter
{
public:
vtkODBCBoundParameter() :
Data(NULL), DataLength(0), BufferSize(0), DataTypeC(0), DataTypeSQL(0)
{
}
~vtkODBCBoundParameter()
{
delete [] this->Data;
}
void SetData(const char *data, unsigned long size)
{
delete [] this->Data;
this->BufferSize = size;
this->DataLength = size;
this->Data = new char[size];
memcpy(this->Data, data, size);
}
public:
char *Data; // Buffer holding actual data
unsigned long DataLength;
SQLLEN BufferSize; // will be at least as large as DataLength
SQLSMALLINT DataTypeC;
SQLSMALLINT DataTypeSQL;
};
// ----------------------------------------------------------------------
class vtkODBCQueryInternals
{
public:
vtkODBCQueryInternals()
{
this->Statement = NULL;
this->CurrentRow = vtkVariantArray::New();
this->ColumnNames = vtkStringArray::New();
this->ColumnIsSigned = vtkBitArray::New();
this->ColumnTypes = NULL;
this->NullPermitted = vtkBitArray::New();
}
~vtkODBCQueryInternals()
{
this->FreeUserParameterList();
if (this->Statement != NULL)
{
SQLFreeHandle(SQL_HANDLE_STMT, this->Statement);
}
this->CurrentRow->Delete();
this->ColumnNames->Delete();
this->ColumnIsSigned->Delete();
this->NullPermitted->Delete();
if (this->ColumnTypes)
{
delete [] this->ColumnTypes;
}
}
void FreeStatement();
void FreeUserParameterList();
void ClearBoundParameters();
bool PrepareQuery(const char *queryString, SQLHANDLE connection, vtkStdString &error_message);
bool SetBoundParameter(int index, vtkODBCBoundParameter *param);
bool BindParametersToStatement();
public:
SQLHANDLE Statement;
vtkStdString Name;
vtkVariantArray *CurrentRow;
vtkStringArray *ColumnNames;
vtkBitArray *ColumnIsSigned;
vtkBitArray *NullPermitted;
SQLSMALLINT *ColumnTypes;
typedef vtksys_stl::vector<vtkODBCBoundParameter *> ParameterList;
ParameterList UserParameterList;
};
// ----------------------------------------------------------------------
void
vtkODBCQueryInternals::FreeStatement()
{
if (this->Statement)
{
SQLRETURN status;
status = SQLCloseCursor(this->Statement);
if (status != SQL_SUCCESS && status != SQL_SUCCESS_WITH_INFO)
{
vtksys_ios::ostringstream errorBuf;
errorBuf << "vtkODBCQuery: Unable to close SQL cursor. Error: "
<< GetErrorMessage(SQL_HANDLE_STMT, this->Statement);
cerr << errorBuf.str() << "\n";
}
status = SQLFreeHandle(SQL_HANDLE_STMT, this->Statement);
if (status != SQL_SUCCESS && status != SQL_SUCCESS_WITH_INFO)
{
vtksys_ios::ostringstream errorBuf;
errorBuf << "Unable to free statement handle. Memory leak will occur. Error: "
<< GetErrorMessage(SQL_HANDLE_STMT, this->Statement);
cerr << errorBuf.str() << "\n";
}
this->Statement = 0;
}
}
// ----------------------------------------------------------------------
#define VTK_ODBC_C_TYPENAME_MACRO(type,return_type) \
SQLSMALLINT vtkODBCTypeNameC(type) \
{ return return_type; }
VTK_ODBC_C_TYPENAME_MACRO(signed char, SQL_C_STINYINT);
VTK_ODBC_C_TYPENAME_MACRO(unsigned char, SQL_C_UTINYINT);
VTK_ODBC_C_TYPENAME_MACRO(signed short, SQL_C_SSHORT);
VTK_ODBC_C_TYPENAME_MACRO(unsigned short, SQL_C_USHORT);
VTK_ODBC_C_TYPENAME_MACRO(signed int, SQL_C_SLONG);
VTK_ODBC_C_TYPENAME_MACRO(unsigned int, SQL_C_ULONG);
VTK_ODBC_C_TYPENAME_MACRO(signed long, SQL_C_SLONG);
VTK_ODBC_C_TYPENAME_MACRO(unsigned long, SQL_C_ULONG);
VTK_ODBC_C_TYPENAME_MACRO(float, SQL_C_FLOAT);
VTK_ODBC_C_TYPENAME_MACRO(double, SQL_C_DOUBLE);
VTK_ODBC_C_TYPENAME_MACRO(vtkTypeInt64, SQL_C_SBIGINT);
VTK_ODBC_C_TYPENAME_MACRO(vtkTypeUInt64, SQL_C_UBIGINT);
VTK_ODBC_C_TYPENAME_MACRO(const char *, SQL_C_CHAR);
VTK_ODBC_C_TYPENAME_MACRO(char *, SQL_C_CHAR);
VTK_ODBC_C_TYPENAME_MACRO(unsigned char *, SQL_C_CHAR);
VTK_ODBC_C_TYPENAME_MACRO(void *, SQL_C_BINARY);
#define VTK_ODBC_SQL_TYPENAME_MACRO(type,return_type) \
SQLSMALLINT vtkODBCTypeNameSQL(type) \
{ return return_type; }
VTK_ODBC_SQL_TYPENAME_MACRO(signed char, SQL_TINYINT);
VTK_ODBC_SQL_TYPENAME_MACRO(unsigned char, SQL_TINYINT);
VTK_ODBC_SQL_TYPENAME_MACRO(signed short, SQL_SMALLINT);
VTK_ODBC_SQL_TYPENAME_MACRO(unsigned short, SQL_SMALLINT);
VTK_ODBC_SQL_TYPENAME_MACRO(signed int, SQL_INTEGER);
VTK_ODBC_SQL_TYPENAME_MACRO(unsigned int, SQL_INTEGER);
VTK_ODBC_SQL_TYPENAME_MACRO(signed long, SQL_INTEGER);
VTK_ODBC_SQL_TYPENAME_MACRO(unsigned long, SQL_INTEGER);
VTK_ODBC_SQL_TYPENAME_MACRO(float, SQL_REAL);
VTK_ODBC_SQL_TYPENAME_MACRO(double, SQL_DOUBLE);
VTK_ODBC_SQL_TYPENAME_MACRO(vtkTypeInt64, SQL_BIGINT);
VTK_ODBC_SQL_TYPENAME_MACRO(vtkTypeUInt64, SQL_BIGINT);
VTK_ODBC_SQL_TYPENAME_MACRO(const char *, SQL_VARCHAR);
VTK_ODBC_SQL_TYPENAME_MACRO(char *, SQL_VARCHAR);
VTK_ODBC_SQL_TYPENAME_MACRO(unsigned char *, SQL_VARCHAR);
VTK_ODBC_SQL_TYPENAME_MACRO(void *, SQL_VARBINARY);
// ----------------------------------------------------------------------
// Description:
// This function will build and populate a vtkODBCBoundParameter
// struct. The default implementation works for POD data types (char,
// int, long, etc). I'll need to special-case strings and blobs.
template<typename T>
vtkODBCBoundParameter *vtkBuildODBCBoundParameter(T data_value)
{
vtkODBCBoundParameter *param = new vtkODBCBoundParameter;
param->DataTypeC = vtkODBCTypeNameC(data_value);
param->DataTypeSQL = vtkODBCTypeNameSQL(data_value);
param->BufferSize = sizeof(T);
param->DataLength = sizeof(T);
param->SetData(reinterpret_cast<const char *>(&data_value),
sizeof(T));
return param;
}
// Description:
// Specialization of vtkBuildBoundParameter for NULL-terminated
// strings (i.e. CHAR and VARCHAR fields)
template<>
vtkODBCBoundParameter *vtkBuildODBCBoundParameter<const char *>(const char *data_value)
{
vtkODBCBoundParameter *param = new vtkODBCBoundParameter;
param->DataTypeC = SQL_C_CHAR;
param->DataTypeSQL = SQL_VARCHAR;
param->BufferSize = strlen(data_value);
param->DataLength = strlen(data_value);
param->SetData(data_value, strlen(data_value));
return param;
}
// Description:
// Alternate signature for vtkBuildBoundParameter to handle blobs
vtkODBCBoundParameter *vtkBuildODBCBoundParameter(const char *data,
unsigned long length,
bool is_blob)
{
vtkODBCBoundParameter *param = new vtkODBCBoundParameter;
param->DataTypeC = SQL_C_CHAR;
if (is_blob)
{
param->DataTypeSQL = SQL_VARBINARY;
}
else
{
param->DataTypeSQL = SQL_VARCHAR;
}
param->BufferSize = length;
param->DataLength = length;
param->SetData(data, length);
return param;
}
// ----------------------------------------------------------------------
bool vtkODBCQueryInternals::PrepareQuery(const char *queryString,
SQLHANDLE dbConnection,
vtkStdString &error_message)
{
this->FreeStatement();
this->FreeUserParameterList();
// ODBC requires that drivers either support query preparation or
// emulate it to the greatest extent possible. It says nothing
// about what queries may or may not be prepared. I'm going to
// close my eyes and pretend that all SQL is valid for preparation
// even if bound parameters don't make sense. If I'm wrong the
// error messages will certainly tell me so.
SQLRETURN status;
status = SQLAllocHandle(SQL_HANDLE_STMT,
dbConnection,
&(this->Statement));
if (status != SQL_SUCCESS && status != SQL_SUCCESS_WITH_INFO)
{
vtksys_ios::ostringstream errorBuf;
errorBuf << "Unable to allocate new statement handle. Error: "
<< GetErrorMessage(SQL_HANDLE_DBC, dbConnection);
error_message = errorBuf.str();
return false;
}
// Queries in VTK currently only support scrolling forward through
// the results, not forward/backward/randomly.
status = SQLSetStmtAttr(this->Statement,
SQL_ATTR_CURSOR_TYPE,
static_cast<SQLPOINTER>(SQL_CURSOR_FORWARD_ONLY),
SQL_IS_UINTEGER);
if (status != SQL_SUCCESS && status != SQL_SUCCESS_WITH_INFO)
{
error_message = vtkStdString(GetErrorMessage(SQL_HANDLE_STMT,
this->Statement));
return false;
}
// ugh, I hate having to use const_cast
status = SQLPrepare(this->Statement,
reinterpret_cast<SQLCHAR *>(const_cast<char *>(queryString)),
strlen(queryString));
if (status != SQL_SUCCESS)
{
vtksys_ios::ostringstream errorBuf;
errorBuf << "Unable to prepare query for execution: "
<< GetErrorMessage(SQL_HANDLE_STMT, this->Statement);
error_message = errorBuf.str();
return false;
}
else
{
error_message = vtkStdString();
SQLSMALLINT paramCount;
status = SQLNumParams(this->Statement, ¶mCount);
if (status != SQL_SUCCESS)
{
error_message = vtkStdString(GetErrorMessage(SQL_HANDLE_STMT,
this->Statement));
return false;
}
else
{
this->UserParameterList.resize(paramCount, NULL);
return true;
}
}
}
// ----------------------------------------------------------------------
void vtkODBCQueryInternals::FreeUserParameterList()
{
for (unsigned int i = 0; i < this->UserParameterList.size(); ++i)
{
if (this->UserParameterList[i] != NULL)
{
delete this->UserParameterList[i];
this->UserParameterList[i] = NULL;
}
}
this->UserParameterList.clear();
}
// ----------------------------------------------------------------------
bool vtkODBCQueryInternals::SetBoundParameter(int index, vtkODBCBoundParameter *param)
{
if (index >= static_cast<int>(this->UserParameterList.size()))
{
vtkGenericWarningMacro(<<"ERROR: Illegal parameter index "
<<index << ". Did you forget to set the query?");
return false;
}
else
{
if (this->UserParameterList[index] != NULL)
{
delete this->UserParameterList[index];
}
this->UserParameterList[index] = param;
return true;
}
}
// ----------------------------------------------------------------------
void vtkODBCQueryInternals::ClearBoundParameters()
{
if (this->Statement)
{
SQLFreeStmt(this->Statement, SQL_RESET_PARAMS);
}
}
// ----------------------------------------------------------------------
bool vtkODBCQueryInternals::BindParametersToStatement()
{
if (this->Statement == NULL)
{
vtkGenericWarningMacro(<<"BindParametersToStatement: No prepared statement available");
return false;
}
this->ClearBoundParameters();
unsigned long numParams = this->UserParameterList.size();
for (SQLUSMALLINT i = 0; i < numParams; ++i)
{
if (this->UserParameterList[i])
{
SQLRETURN status = SQLBindParameter(this->Statement,
i+1, // parameter indexing starts at 1
SQL_PARAM_INPUT,
this->UserParameterList[i]->DataTypeC,
this->UserParameterList[i]->DataTypeSQL,
0, // column size is irrelevant
0, // decimal digits are irrelevant
reinterpret_cast<SQLPOINTER>(this->UserParameterList[i]->Data),
this->UserParameterList[i]->DataLength,
&(this->UserParameterList[i]->BufferSize));
if (status != SQL_SUCCESS)
{
vtkGenericWarningMacro(<<"Unable to bind parameter "
<< i << " to SQL statement! Return code: "
<< status);
return false;
}
}
}
return true;
}
// ----------------------------------------------------------------------
static vtkStdString
GetErrorMessage(SQLSMALLINT handleType, SQLHANDLE handle, int *code)
{
SQLINTEGER sqlNativeCode = 0;
SQLSMALLINT messageLength = 0;
SQLRETURN status;
SQLCHAR state[SQL_SQLSTATE_SIZE + 1];
SQLCHAR description[SQL_MAX_MESSAGE_LENGTH + 1];
vtkStdString finalResult;
int i = 1;
// There may be several error messages queued up so we need to loop
// until we've got everything.
vtksys_ios::ostringstream messagebuf;
do
{
status = SQLGetDiagRec(handleType, handle,
i,
state,
&sqlNativeCode,
description,
SQL_MAX_MESSAGE_LENGTH,
&messageLength);
description[SQL_MAX_MESSAGE_LENGTH] = 0;
if (status == SQL_SUCCESS || status == SQL_SUCCESS_WITH_INFO)
{
if (code)
{
*code = sqlNativeCode;
}
if (i > 1)
{
messagebuf << ", "
;
}
messagebuf << state << ' ' << description;
}
else if (status == SQL_ERROR || status == SQL_INVALID_HANDLE)
{
return vtkStdString(messagebuf.str());
}
++i;
} while (status != SQL_NO_DATA);
return vtkStdString(messagebuf.str());
}
// ----------------------------------------------------------------------
vtkODBCQuery::vtkODBCQuery()
{
this->Internals = new vtkODBCQueryInternals;
this->InitialFetch = true;
this->LastErrorText = NULL;
this->QueryText = NULL;
}
// ----------------------------------------------------------------------
vtkODBCQuery::~vtkODBCQuery()
{
this->SetLastErrorText(NULL);
this->SetQueryText(NULL);
delete this->Internals;
}
// ----------------------------------------------------------------------
void
vtkODBCQuery::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
// ----------------------------------------------------------------------
bool
vtkODBCQuery::SetQuery(const char *newQuery)
{
this->Active = false;
this->SetQueryText(newQuery);
vtkODBCDatabase *db = vtkODBCDatabase::SafeDownCast(this->Database);
if (db == NULL)
{
vtkErrorMacro(<<"SHOULDN'T HAPPEN: SetQuery called with null database. This can only happen when you instantiate vtkODBCQuery directly. You should always call vtkODBCDatabase::GetQueryInstance to make a query object.");
return false;
}
vtkStdString error;
bool prepareStatus = this->Internals->PrepareQuery(newQuery, db->Internals->Connection, error);
if (prepareStatus)
{
this->SetLastErrorText(NULL);
return true;
}
else
{
vtkErrorMacro(<< error.c_str());
this->SetLastErrorText(error.c_str());
return false;
}
}
// ----------------------------------------------------------------------
const char *
vtkODBCQuery::GetQuery()
{
return this->GetQueryText();
}
// ----------------------------------------------------------------------
bool
vtkODBCQuery::Execute()
{
// It's possible to call this function while a cursor is still open.
// This is not an error, but we do need to close out the previous
// cursor before opening up a new one.
this->Active = false;
SQLFreeStmt(this->Internals->Statement, SQL_CLOSE);
this->Internals->BindParametersToStatement();
SQLRETURN status = SQLExecute(this->Internals->Statement);
if (status != SQL_SUCCESS && status != SQL_SUCCESS_WITH_INFO)
{
vtksys_ios::ostringstream errorBuf;
errorBuf << "Unable to execute statement: "
<< GetErrorMessage(SQL_HANDLE_STMT, this->Internals->Statement);
this->SetLastErrorText(errorBuf.str().c_str());
this->Active = false;
return false;
}
else
{
vtkDebugMacro(<<"SQLExecute succeeded.");
this->Active = true;
this->Internals->ColumnNames->Reset();
this->Internals->CurrentRow->Reset();
this->Internals->ColumnIsSigned->Reset();
this->Internals->NullPermitted->Reset();
if (this->Internals->ColumnTypes)
{
delete [] this->Internals->ColumnTypes;
this->Internals->ColumnTypes = NULL;
}
// Populate the result information now, all at once, rather than
// making a whole bunch of calls later and duplicating
// (potentially expensive) operations.
int numColumns = this->GetNumberOfFields();
if (numColumns)
{
this->Internals->ColumnTypes = new SQLSMALLINT[numColumns];
this->Internals->NullPermitted->SetNumberOfTuples(numColumns);
this->Internals->CurrentRow->SetNumberOfTuples(numColumns);
this->Internals->ColumnNames->SetNumberOfTuples(numColumns);
this->Internals->ColumnIsSigned->SetNumberOfTuples(numColumns);
for (int i = 0; i < numColumns; ++i)
{
SQLCHAR name[1024];
SQLSMALLINT nameLength;
SQLSMALLINT dataType;
SQLULEN columnSize;
SQLSMALLINT decimalDigits;
SQLSMALLINT nullable;
SQLLEN unsignedFlag = SQL_FALSE;
status = SQLDescribeCol(this->Internals->Statement,
i + 1, // 1-indexed, not 0
name,
1024,
&nameLength,
&dataType,
&columnSize,
&decimalDigits,
&nullable);
if (status != SQL_SUCCESS && status != SQL_SUCCESS_WITH_INFO)
{
vtksys_ios::ostringstream errbuf;
errbuf << "During vtkODBCQuery::Execute while looking up column "
<< i << ": "
<< GetErrorMessage(SQL_HANDLE_STMT, this->Internals->Statement);
this->SetLastErrorText(errbuf.str().c_str());
vtkErrorMacro(<< errbuf.str().c_str());
}
status = SQLColAttribute(this->Internals->Statement,
i+1,
SQL_DESC_UNSIGNED,
0,
0,
0,
&unsignedFlag);
if (status != SQL_SUCCESS && status != SQL_SUCCESS_WITH_INFO)
{
vtksys_ios::ostringstream errbuf;
errbuf << "vtkODBCQuery::Execute: Unable to get unsigned flag for column "
<< i << ": "
<< GetErrorMessage(SQL_HANDLE_STMT,
this->Internals->Statement);
this->SetLastErrorText(errbuf.str().c_str());
vtkErrorMacro(<< errbuf.str().c_str());
}
this->Internals->ColumnNames->SetValue(i,reinterpret_cast<const char *>(name));
this->Internals->ColumnIsSigned->SetValue(i, (unsignedFlag == SQL_FALSE));
this->Internals->ColumnTypes[i] = dataType;
this->Internals->NullPermitted->SetValue(i, nullable);
} // done populating column information
}
this->SetLastErrorText(NULL);
return true;
}
}
// ----------------------------------------------------------------------
int
vtkODBCQuery::GetNumberOfFields()
{
if (!this->Active)
{
return 0;
}
SQLSMALLINT count;
SQLRETURN status;
status = SQLNumResultCols(this->Internals->Statement, &count);
if (status != SQL_SUCCESS &&
status != SQL_SUCCESS_WITH_INFO)
{
vtksys_ios::ostringstream errbuf;
errbuf << "During vtkODBCQuery::GetNumberOfFields: "
<< GetErrorMessage(SQL_HANDLE_STMT, this->Internals->Statement);
this->SetLastErrorText(errbuf.str().c_str());
return 0;
}
this->SetLastErrorText(NULL);
return count;
}
// ----------------------------------------------------------------------
const char *
vtkODBCQuery::GetFieldName(int column)
{
if (! this->Active)
{
vtkErrorMacro(<<"GetFieldName(): Query is not active!");
return NULL;
}
else if (column < 0 || column >= this->GetNumberOfFields())
{
vtkErrorMacro(<<"GetFieldName(): Illegal field index "
<< column);
return NULL;
}
else
{
return this->Internals->ColumnNames->GetValue(column).c_str();
}
}
// ----------------------------------------------------------------------
int
vtkODBCQuery::GetFieldType(int column)
{
if (! this->Active)
{
vtkErrorMacro(<<"GetFieldType(): Query is not active!");
return VTK_VOID;
}
else if (column < 0 || column >= this->GetNumberOfFields())
{
vtkErrorMacro(<<"GetFieldType(): Illegal field index "
<< column);
return VTK_VOID;
}
else
{
switch (this->Internals->ColumnTypes[column])
{
case SQL_CHAR:
case SQL_VARCHAR:
case SQL_LONGVARCHAR:
case SQL_WCHAR:
case SQL_WVARCHAR:
case SQL_WLONGVARCHAR:
return VTK_STRING;
case SQL_INTEGER:
case SQL_NUMERIC:
if (this->Internals->ColumnIsSigned->GetValue(column))
{
return VTK_INT;
}
else
{
return VTK_UNSIGNED_INT;
}
case SQL_TINYINT:
if (this->Internals->ColumnIsSigned->GetValue(column))
{
return VTK_SIGNED_CHAR;
}
else
{
return VTK_UNSIGNED_CHAR;
}
case SQL_SMALLINT:
if (this->Internals->ColumnIsSigned->GetValue(column))
{
return VTK_SHORT;
}
else
{
return VTK_UNSIGNED_SHORT;
}
case SQL_BIT:
return VTK_BIT;
case SQL_REAL:
case SQL_FLOAT:
return VTK_FLOAT;
case SQL_DOUBLE:
return VTK_DOUBLE;
case SQL_BINARY:
case SQL_VARBINARY:
case SQL_LONGVARBINARY:
return VTK_STRING;
case SQL_BIGINT:
case SQL_DECIMAL:
return VTK_TYPE_INT64;
case SQL_TYPE_TIMESTAMP:
// case SQL_TYPE_UTCDATETIME:
// case SQL_TYPE_UTCTIME:
case SQL_TYPE_DATE:
case SQL_TYPE_TIME:
return VTK_TYPE_UINT64;
case SQL_INTERVAL_MONTH:
case SQL_INTERVAL_YEAR:
case SQL_INTERVAL_DAY:
case SQL_INTERVAL_HOUR:
case SQL_INTERVAL_MINUTE:
case SQL_INTERVAL_SECOND:
return VTK_TYPE_UINT64;
// unhandled: SQL_INTERVAL_YEAR_TO_MONTH,
// SQL_INTERVAL_DAY_TO_HOUR, SQL_INTERVAL_DAY_TO_MINUTE,
// SQL_INTERVAL_DAY_TO_SECOND, SQL_INTERVAL_HOUR_TO_MINUTE,
// SQL_INTERVAL_HOUR_TO_SECOND, SQL_INTERVAL_MINUTE_TO_SECOND,
// SQL_GUID
default:
{
vtkWarningMacro(<<"Unknown type " << this->Internals->ColumnTypes[column]
<<" returned from SQLDescribeCol");
return VTK_VOID;
}
}
}
}
// ----------------------------------------------------------------------
bool
vtkODBCQuery::NextRow()
{
if (! this->IsActive())
{
vtkErrorMacro(<<"NextRow(): Query is not active!");
return false;
}
this->ClearCurrentRow();
SQLRETURN status = SQLFetch(this->Internals->Statement);
if (status == SQL_SUCCESS)
{
this->SetLastErrorText(NULL);
return this->CacheCurrentRow();
}
else if (status == SQL_NO_DATA)
{
this->SetLastErrorText(NULL);
return false;
}
else
{
return false;
}
}
// ----------------------------------------------------------------------
vtkVariant
vtkODBCQuery::DataValue(vtkIdType column)
{
if (this->IsActive() == false)
{
vtkWarningMacro(<<"DataValue() called on inactive query");
return vtkVariant();
}
else if (column < 0 || column >= this->GetNumberOfFields())
{
vtkWarningMacro(<<"DataValue() called with out-of-range column index "
<< column);
return vtkVariant();
}
else
{
return this->Internals->CurrentRow->GetValue(column);
}
}
// ----------------------------------------------------------------------
void
vtkODBCQuery::ClearCurrentRow()
{
for (vtkIdType i = 0;
i < this->Internals->CurrentRow->GetNumberOfTuples();
++i)
{
this->Internals->CurrentRow->SetValue(i, vtkVariant());
}
}
// ----------------------------------------------------------------------
bool
vtkODBCQuery::CacheCurrentRow()
{
int column;
bool status = true;
for (column = 0; column < this->GetNumberOfFields(); ++column)
{
switch (this->Internals->ColumnTypes[column])
{
case SQL_CHAR:
case SQL_VARCHAR:
case SQL_LONGVARCHAR:
status = status & this->CacheStringColumn(column);
break;
case SQL_WCHAR:
case SQL_WVARCHAR:
case SQL_WLONGVARCHAR:
status = status & this->CacheWideStringColumn(column);
break;
case SQL_DECIMAL:
status = status & this->CacheDecimalColumn(column);
break;
case SQL_NUMERIC:
status = status & this->CacheNumericColumn(column);
break;
case SQL_SMALLINT:
case SQL_INTEGER:
status = status & this->CacheIntColumn(column);
break;
case SQL_REAL:
case SQL_FLOAT:
status = status & this->CacheFloatColumn(column);
break;
case SQL_DOUBLE:
status = status & this->CacheDoubleColumn(column);
break;
case SQL_BIT:
status = status & this->CacheBooleanColumn(column);
break;
case SQL_TINYINT:
status = status & this->CacheCharColumn(column);
break;
case SQL_BIGINT:
status = status & this->CacheLongLongColumn(column);
break;
case SQL_BINARY:
case SQL_VARBINARY:
case SQL_LONGVARBINARY:
status = status & this->CacheBinaryColumn(column);
break;
case SQL_TYPE_DATE:
case SQL_TYPE_TIME:
case SQL_TYPE_TIMESTAMP:
// case SQL_TYPE_UTCDATETIME:
// case SQL_TYPE_UTCTIME:
status = status & this->CacheTimeColumn(column);
break;
case SQL_INTERVAL_MONTH:
case SQL_INTERVAL_YEAR:
case SQL_INTERVAL_DAY:
case SQL_INTERVAL_HOUR:
case SQL_INTERVAL_MINUTE:
case SQL_INTERVAL_SECOND:
status = status & this->CacheIntervalColumn(column);
break;