forked from FirebirdSQL/firebird-odbc-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOdbcStatement.cpp
3704 lines (3160 loc) · 92.6 KB
/
OdbcStatement.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
/*
*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed on
* an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the License for the specific
* language governing rights and limitations under the License.
*
*
* The Original Code was created by James A. Starkey
*
* Copyright (c) 1999, 2000, 2001 James A. Starkey
* All Rights Reserved.
*
*
* Changes
* 2003-03-24 OdbcStatement.cpp
* Contributed by Norbert Meyer
* o In sqlExtendedFetch() add support for
* applications which only check rowCountPointer
* o In setValue()
* Empty strings have len = 0, so test for that
* o In setParameter() and executeStatement()
* test for binding->indicatorPointer
*
* 2003-03-24 OdbcStatement.cpp
* Contributed by Carlos Guzman Alvarez
* Remove updatePreparedResultSet from OdbStatement
* and achieve the same goal in another way.
*
* 2003-03-24 OdbcStatement.cpp
* Contributed by Roger Gammans
* Fix a segv in SQLBindCol()
*
* 2002-11-21 OdbcStatement.cpp
* Contributed by C. G. Alvarez
* Improved handling of TIME datatype
*
* 2002-11-21 OdbcStatement.cpp
* Contributed by C. G. Alvarez
* Modification to OdbcStatement::sqlExtendedFetch
* to support SQL_API_SQLEXTENDEDFETCH
*
* 2002-10-11 OdbcStatement.cpp
* Contributed by C. G. Alvarez
* Extensive modifications to blob reading and writing
*
* 2002-10-11 OdbcStatement.cpp
* Contributed by C. G. Alvarez
* Added sqlNumParams()
*
* 2002-08-14 OdbcStatement.cpp
* Contributed by C. G. Alvarez
* Minor enhancements to sqlGetSmtAttr and sqlSetStmtAttr.
*
*
* 2002-08-12 OdbcStatement.cpp
* Added changes from C. G. Alvarez to so that
* sqlColAttributes() called with SQL_COLUMN_TYPE_NAME returns
* the name of the type instead of the number of the type.
* Similarly, sqlColAttribute() will return string for
* SQL_DESC_TYPE_NAME.
*
* Added sqlTablePrivileges()
*
*
* 2002-07-08 OdbcStatement.cpp
* Added changes from C. G. Alvarez to return
* SQL_DESC_UNNAMED and SQL_DESC_BASE_TABLE_NAME
* from sqlColAtrributes()
*
* 2002-06-26 OdbcStatement.cpp
* Added changes from C. G. Alvarez to provide
* better support for remote views.
*
* 2002-06-26 OdbcStatement::OdbcStatement
* Initialised numberColumns in constructor (Roger Gammans)
*
* 2002-06-17 OdbcStatement::setParameter()
* Submitted by C. G. Alvarez
* Added code to handle returning strings that are not
* null terminated.
*
* 2002-06-08 OdbcStatement.cpp
* Submitted by B. Schulte
* sqlNumResultCols().
* This fixes the bug : ' I can't edit my remote-views
* in Visual FoxPro'. If the resultSet does not exist,
* execute it, to get a valid resultSet. Foxpro calls
* this function to get all column-descriptions for
* its remote-views.
*
* 2002-06-04 OdbcdStatement.cpp
* submitted by Robert Milharcic
* Extensive changes to improve writing and
* retrieval of binary blobs
*
* 2002-05-20 Updated OdbcStatement.cpp
*
* Contributed by Pier Alberto GUIDOTTI
* o Use RM's changes to support reading
* text blobs too.
*
* 2002-05-20 Updated OdbcStatement.cpp
*
* Contributed by Robert Milharcic
* o Several changes to allow reading of binary blobs
* See code commented with //Added by RM or //From RM
*
*
* 2002-05-20 Updated OdbcStatement.cpp
*
* Contributed by Bernhard Schulte
* o Use TimeStamp instead of DateTime in setParameter().
*
*
*/
// OdbcStatement.cpp: implementation of the OdbcStatement class.
//
//////////////////////////////////////////////////////////////////////
#ifndef _WINDOWS
#include <wchar.h>
#endif
#include <memory.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "IscDbc/Connection.h"
#include "IscDbc/SQLException.h"
#include "OdbcEnv.h"
#include "OdbcConnection.h"
#include "OdbcStatement.h"
#include "OdbcError.h"
#include "DescRecord.h"
#ifdef DEBUG
#define TRACE(msg) OutputDebugString(#msg"\n");
#define TRACE02(msg,val) TraceOutput(#msg,val)
#else
#define TRACE(msg)
#define TRACE02(msg,val)
#endif
namespace OdbcJdbcLibrary {
using namespace IscDbcLibrary;
void TraceOutput(char * msg, intptr_t val)
{
char buf[80];
sprintf( buf, "\t%s = %ld : %p\n", msg, val, val );
OutputDebugString(buf);
}
// Bound Address + Binding Offset + ((Row Number – 1) x Element Size)
// *ptr = binding->pointer + bindOffsetPtr + ((1 – 1) * rowBindType); // <-- for single row
#define GETBOUNDADDRESS(binding) ( (uintptr_t)binding->dataPtr + ( applicationParamDescriptor->headBindType ? (uintptr_t)bindOffsetPtr : 0 ) );
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
OdbcStatement::OdbcStatement(OdbcConnection *connect, int statementNumber)
{
connection = connect;
resultSet = NULL;
statement = connection->connection->createInternalStatement();
bulkInsert = NULL;
execute = &OdbcStatement::executeStatement;
fetchNext = &ResultSet::nextFetch;
schemaFetchData = true;
metaData = NULL;
cancel = false;
countFetched = 0l;
enFetch = NoneFetch;
parameterNeedData = 0;
maxRows = 0;
maxLength = 0;
applicationRowDescriptor = connection->allocDescriptor (odtApplicationRow);
saveApplicationRowDescriptor = applicationRowDescriptor;
applicationParamDescriptor = connection->allocDescriptor (odtApplicationParameter);
saveApplicationParamDescriptor = applicationParamDescriptor;
implementationRowDescriptor = connection->allocDescriptor (odtImplementationRow);
implementationParamDescriptor = connection->allocDescriptor (odtImplementationParameter);
implementationGetDataDescriptor = NULL;
fetchRetData = SQL_RD_ON;
sqldataOutOffsetPtr = NULL;
numberColumns = 0;
rowNumberParamArray = 0;
registrationOutParameter = false;
isRegistrationOutParameter = false;
isResultSetFromSystemCatalog = false;
isFetchStaticCursor = false;
currency = SQL_CONCUR_READ_ONLY;
cursorType = SQL_CURSOR_FORWARD_ONLY;
cursorName.Format ("SQL_CUR%d", statementNumber);
setPreCursorName = false;
cursorScrollable = SQL_NONSCROLLABLE;
asyncEnable = false;
enableAutoIPD = SQL_TRUE;
useBookmarks = SQL_UB_OFF;
cursorSensitivity = SQL_INSENSITIVE;
fetchBookmarkPtr = NULL;
noscanSQL = SQL_NOSCAN_OFF;
bindOffsetColumnWiseBinding = 0;
bindOffsetIndColumnWiseBinding = 0;
listBindIn = new ListBindColumn;
convert = new OdbcConvert(this);
listBindOut = new ListBindColumn;
listBindGetData = NULL;
}
OdbcStatement::~OdbcStatement()
{
releaseBindings();
releaseParameters();
try
{
releaseStatement();
}
catch ( std::exception ) { }
statement->release();
delete applicationRowDescriptor;
delete applicationParamDescriptor;
delete implementationRowDescriptor;
delete implementationParamDescriptor;
delete implementationGetDataDescriptor;
delete convert;
delete listBindIn;
delete listBindOut;
delete listBindGetData;
connection->statementDeleted (this);
delete bulkInsert;
}
OdbcConnection* OdbcStatement::getConnection()
{
return connection;
}
OdbcObjectType OdbcStatement::getType()
{
return odbcTypeStatement;
}
inline StatementMetaData* OdbcStatement::getStatementMetaDataIRD()
{
return resultSet ? resultSet->getMetaData() : statement->getStatementMetaDataIRD();
}
inline void OdbcStatement::clearErrors()
{
if ( infoPosted )
OdbcObject::clearErrors();
}
SQLRETURN OdbcStatement::sqlTables(SQLCHAR * catalog, int catLength,
SQLCHAR * schema, int schemaLength,
SQLCHAR * table, int tableLength,
SQLCHAR * type, int typeLength)
{
clearErrors();
releaseStatement();
char temp [1024], *p = temp;
const char *cat = getString (&p, catalog, catLength, NULL);
const char *scheme = getString (&p, schema, schemaLength, NULL);
const char *tbl = getString (&p, table, tableLength, NULL);
const char *typeString = getString (&p, type, typeLength, "");
const char *typeVector [16];
int numberTypes = 0;
for (const char *q = typeString; *q && numberTypes < 16;)
if (*q == ' ')
++q;
else
{
typeVector [numberTypes++] = p;
if (*q == '\'')
{
for (++q; *q && *q++ != '\'';)
*p++ = q [-1];
while (*q && *q++ != ',')
;
}
else
for (char c; *q && (c = *q++) != ',';)
*p++ = c;
*p++ = 0;
}
try
{
DatabaseMetaData *metaData = connection->getMetaData();
setResultSet (metaData->getTables (cat, scheme, tbl, numberTypes, typeVector));
}
catch ( std::exception &ex )
{
SQLException &exception = (SQLException&)ex;
postError ("HY000", exception);
return SQL_ERROR;
}
return sqlSuccess();
}
SQLRETURN OdbcStatement::sqlTablePrivileges(SQLCHAR * catalog, int catLength,
SQLCHAR * schema, int schemaLength,
SQLCHAR * table, int tableLength)
{
clearErrors();
releaseStatement();
char temp [1024], *p = temp;
const char *cat = getString (&p, catalog, catLength, NULL);
const char *scheme = getString (&p, schema, schemaLength, NULL);
const char *tbl = getString (&p, table, tableLength, NULL);
try
{
DatabaseMetaData *metaData = connection->getMetaData();
setResultSet (metaData->getTablePrivileges (cat, scheme, tbl));
}
catch ( std::exception &ex )
{
SQLException &exception = (SQLException&)ex;
postError ("HY000", exception);
return SQL_ERROR;
}
return sqlSuccess();
}
SQLRETURN OdbcStatement::sqlColumnPrivileges(SQLCHAR * catalog, int catLength,
SQLCHAR * schema, int schemaLength,
SQLCHAR * table, int tableLength,
SQLCHAR * column, int columnLength)
{
clearErrors();
releaseStatement();
char temp [1024], *p = temp;
const char *cat = getString (&p, catalog, catLength, NULL);
const char *scheme = getString (&p, schema, schemaLength, NULL);
const char *tbl = getString (&p, table, tableLength, NULL);
const char *col = getString (&p, column, columnLength, NULL);
try
{
DatabaseMetaData *metaData = connection->getMetaData();
setResultSet (metaData->getColumnPrivileges (cat, scheme, tbl, col));
}
catch ( std::exception &ex )
{
SQLException &exception = (SQLException&)ex;
postError ("HY000", exception);
return SQL_ERROR;
}
return sqlSuccess();
}
SQLRETURN OdbcStatement::sqlPrepare(SQLCHAR * sql, int sqlLength)
{
clearErrors();
releaseStatement();
int retNativeSQL = 0;
JString temp, tempNative;
const char *string = (const char*) sql;
if (sqlLength != SQL_NTS)
{
temp = JString ((const char*) sql, sqlLength);
string = temp;
}
#ifdef DEBUG
{
char tempDebugStr [8196];
sprintf (tempDebugStr, "Preparing statement:\n\t%.8170s\n", string);
OutputDebugString (tempDebugStr);
}
#endif
try
{
if ( noscanSQL == SQL_NOSCAN_OFF )
{
int lenstrSQL = (int)strlen(string);
int lennewstrSQL = lenstrSQL + 4096;
retNativeSQL = connection->connection->getNativeSql( string, lenstrSQL, tempNative. getBuffer( lennewstrSQL ), lennewstrSQL, &lenstrSQL );
if ( retNativeSQL > 0 )
{
retNativeSQL = 0;
string = tempNative;
}
}
#ifdef DEBUG
{
char tempDebugStr [8196];
sprintf (tempDebugStr, "Preparing statement:\n\t%.8170s\n", string);
OutputDebugString (tempDebugStr);
}
#endif
sqlPrepareString = string;
implementationParamDescriptor->releasePrepared();
if ( !retNativeSQL )
{
statement->prepareStatement (string);
if ( statement->isActiveSelect() )
execute = &OdbcStatement::executeStatement;
else if ( statement->isActiveProcedure() )
execute = &OdbcStatement::executeProcedure;
else if ( statement->isActiveModify() && applicationParamDescriptor->headArraySize > 1 )
execute = &OdbcStatement::executeStatementParamArray;
else
execute = &OdbcStatement::executeStatement;
registrationOutParameter = false;
listBindIn->removeAll();
listBindOut->removeAll();
implementationRowDescriptor->setDefaultImplDesc (statement->getStatementMetaDataIRD());
implementationParamDescriptor->setDefaultImplDesc (statement->getStatementMetaDataIRD(), statement->getStatementMetaDataIPD());
applicationRowDescriptor->clearPrepared();
rebindColumn();
numberColumns = statement->getStatementMetaDataIRD()->getColumnCount();
implementationParamDescriptor->updateDefinedIn();
applicationParamDescriptor->clearPrepared();
if ( enableAutoIPD == SQL_TRUE )
rebindParam();
}
else
{
switch ( retNativeSQL )
{
case -1: // replace commit
execute = &OdbcStatement::executeCommit;
break;
case -2: // replace rollback
execute = &OdbcStatement::executeRollback;
break;
case -3: // create database
execute = &OdbcStatement::executeCreateDatabase;
break;
case -4: // use local statement param Transaction
statement->setActiveLocalParamTransaction();
execute = &OdbcStatement::executeNone;
break;
case -7: // declare local statement param Transaction
statement->declareLocalParamTransaction();
execute = &OdbcStatement::executeNone;
break;
case -5: // use connect param Transaction
case -6: // use all connections param Transaction
statement->delActiveLocalParamTransaction();
execute = &OdbcStatement::executeNone;
break;
}
}
}
catch ( std::exception &ex )
{
SQLException &exception = (SQLException&)ex;
postError ("HY000", exception);
return SQL_ERROR;
}
return sqlSuccess();
}
void OdbcStatement::releaseStatement()
{
eof = false;
cancel = false;
numberColumns = 0;
releaseResultSet();
statement->drop();
}
void OdbcStatement::releaseResultSet()
{
if (resultSet)
{
resultSet->release();
resultSet = NULL;
metaData = NULL;
sqldataOutOffsetPtr = NULL;
implementationRowDescriptor->clearDefined();
implementationParamDescriptor->clearDefined();
}
lastRowsetSize = 0;
countFetched = 0;
isResultSetFromSystemCatalog = false;
if ( implementationGetDataDescriptor )
{
delete implementationGetDataDescriptor;
implementationGetDataDescriptor = NULL;
delete listBindGetData;
listBindGetData = NULL;
}
if ( bulkInsert )
{
delete bulkInsert;
bulkInsert = NULL;
}
}
void OdbcStatement::setResultSet(ResultSet * results, bool fromSystemCatalog)
{
execute = &OdbcStatement::executeStatement;
fetchNext = &ResultSet::nextFetch;
resultSet = results;
isResultSetFromSystemCatalog = fromSystemCatalog;
metaData = resultSet->getMetaData();
sqldataOutOffsetPtr = (SQLLEN*) resultSet->getSqlDataOffsetPtr();
if ( !statement->isActive() )
{
listBindOut->removeAll();
implementationRowDescriptor->setDefaultImplDesc (metaData);
applicationRowDescriptor->clearPrepared();
rebindColumn();
}
else
implementationRowDescriptor->updateDefinedOut();
convert->setBindOffsetPtrFrom(sqldataOutOffsetPtr, NULL);
numberColumns = resultSet->getColumnCount();
enFetch = NoneFetch;
eof = false;
cancel = false;
countFetched = 0;
rowNumber = 0;
indicatorRowNumber = 0;
lastRowsetSize = 0;
rowNumberParamArray = 0;
if ( fromSystemCatalog )
{
setCursorRowCount(resultSet->getCountRowsStaticCursor());
}
}
void OdbcStatement::rebindColumn()
{
if ( !implementationRowDescriptor->headCount )
return;
int nCount = implementationRowDescriptor->headCount;
int nCountApp = applicationRowDescriptor->headCount;
DescRecord * record = applicationRowDescriptor->getDescRecord (0);
if ( !record->isPrepared && record->isDefined )
{ // set column 0
DescRecord *imprec = implementationRowDescriptor->getDescRecord (0);
imprec->dataPtr = &rowNumber;
imprec->indicatorPtr = &indicatorRowNumber;
record->initZeroColumn();
bindOutputColumn ( 0, record );
}
for (int column = 1, columnApp = 1; column <= nCount && columnApp <= nCountApp; ++column, ++columnApp)
{
record = applicationRowDescriptor->getDescRecord ( columnApp );
if ( !record->isPrepared && record->isDefined )
{
SQLINTEGER bufferLength = record->length;
bindOutputColumn ( columnApp, record);
record->length = bufferLength;
}
}
}
void OdbcStatement::addBindColumn(int column, DescRecord * recordFrom, DescRecord * recordTo)
{
CBindColumn bindCol(column, recordFrom, recordTo);
int j = listBindOut->SearchAndInsert( &bindCol );
if( j < 0 )
(*listBindOut)[~j] = bindCol;
else
(*listBindOut)[j] = bindCol;
}
void OdbcStatement::delBindColumn(int column)
{
}
SQLRETURN OdbcStatement::sqlBindCol(int column, int targetType, SQLPOINTER targetValuePtr, SQLLEN bufferLength, SQLLEN * indPtr)
{
clearErrors();
if (column < 0)
return sqlReturn (SQL_ERROR, "07009", "Invalid descriptor index");
try
{
switch (targetType)
{
case SQL_C_CHAR:
case SQL_C_WCHAR:
case SQL_C_SHORT:
case SQL_C_SSHORT:
case SQL_C_USHORT:
case SQL_C_LONG:
case SQL_C_SLONG:
case SQL_C_ULONG: // case SQL_C_BOOKMARK:
case SQL_C_FLOAT:
case SQL_C_DOUBLE:
case SQL_C_BIT:
case SQL_C_TINYINT:
case SQL_C_STINYINT:
case SQL_C_UTINYINT:
case SQL_C_SBIGINT:
case SQL_C_UBIGINT:
case SQL_C_BINARY: // case SQL_C_VARBOOKMARK:
case SQL_C_DATE:
case SQL_C_TIME:
case SQL_C_TIMESTAMP:
case SQL_C_NUMERIC:
case SQL_DECIMAL:
case SQL_TYPE_DATE:
case SQL_TYPE_TIME:
case SQL_TYPE_TIMESTAMP:
case SQL_C_DEFAULT:
case SQL_C_INTERVAL_YEAR:
case SQL_C_INTERVAL_MONTH:
case SQL_C_INTERVAL_DAY:
case SQL_C_INTERVAL_HOUR:
case SQL_C_INTERVAL_MINUTE:
case SQL_C_INTERVAL_SECOND:
case SQL_C_INTERVAL_YEAR_TO_MONTH:
case SQL_C_INTERVAL_DAY_TO_HOUR:
case SQL_C_INTERVAL_DAY_TO_MINUTE:
case SQL_C_INTERVAL_DAY_TO_SECOND:
case SQL_C_INTERVAL_HOUR_TO_MINUTE:
case SQL_C_INTERVAL_HOUR_TO_SECOND:
case SQL_C_INTERVAL_MINUTE_TO_SECOND:
case SQL_C_GUID:
break;
default:
{
JString msg;
msg.Format ("Invalid application buffer type (%d)", targetType);
LOG_MSG ((const char*) msg);
LOG_MSG ("\n");
return sqlReturn( SQL_ERROR, "HY003", (const char*)msg );
}
}
DescRecord *record = applicationRowDescriptor->getDescRecord (column);
record->parameterType = SQL_PARAM_OUTPUT;
record->type = targetType;
record->conciseType = targetType;
record->dataPtr = targetValuePtr;
record->indicatorPtr = indPtr;
record->length = bufferLength;
record->scale = 0;
record->isDefined = true;
record->isPrepared = false;
record->sizeColumnExtendedFetch = bufferLength;
if ( implementationRowDescriptor->isDefined() )
{
if ( column > implementationRowDescriptor->headCount )
return sqlReturn (SQL_ERROR, "07009", "Invalid descriptor index");
if ( !column )
{
DescRecord *imprec = implementationRowDescriptor->getDescRecord (column);
imprec->dataPtr = &rowNumber;
imprec->indicatorPtr = &indicatorRowNumber;
record->initZeroColumn();
}
bindOutputColumn ( column, record);
record->length = bufferLength;
}
if ( bulkInsert )
{
delete bulkInsert;
bulkInsert = NULL;
}
}
catch ( std::exception &ex )
{
SQLException &exception = (SQLException&)ex;
postError ("HY000", exception);
return SQL_ERROR;
}
return sqlSuccess();
}
inline
void OdbcStatement::setZeroColumn(int column)
{
CBindColumn * bindCol = listBindOut->GetHeadPosition();
if ( bindCol && !bindCol->column )
convert->setZeroColumn(bindCol->appRecord, column);
}
inline
SQLRETURN OdbcStatement::fetchData()
{
SQLULEN rowCount = 0;
SQLULEN *rowCountPt = implementationRowDescriptor->headRowsProcessedPtr ? implementationRowDescriptor->headRowsProcessedPtr
: &rowCount;
SQLUSMALLINT *statusPtr = implementationRowDescriptor->headArrayStatusPtr ? implementationRowDescriptor->headArrayStatusPtr
: NULL;
int nCountRow = applicationRowDescriptor->headArraySize;
SQLLEN *&bindOffsetPtr = applicationRowDescriptor->headBindOffsetPtr;
SQLLEN *bindOffsetPtrSave = bindOffsetPtr;
try
{
int nRow = 0;
if ( !eof )
{
int rowBindType = applicationRowDescriptor->headBindType;
SQLLEN bindOffsetPtrTmp = bindOffsetPtr ? *bindOffsetPtr : 0;
bindOffsetPtr = &bindOffsetPtrTmp;
if ( schemaFetchData )
{
convert->setBindOffsetPtrTo(bindOffsetPtr, bindOffsetPtr);
while ( nRow < nCountRow && (resultSet->*fetchNext)() )
{
++countFetched;
++rowNumber; // Should stand only here!!!
if ( fetchRetData == SQL_RD_ON )
returnData();
bindOffsetPtrTmp += rowBindType;
++nRow;
}
if ( statusPtr && nRow )
memset(statusPtr, SQL_ROW_SUCCESS, sizeof(*statusPtr) * nRow);
}
else // if ( schemaExtendedFetchData )
{
SQLLEN bindOffsetPtrData = 0;
SQLLEN bindOffsetPtrInd = 0;
convert->setBindOffsetPtrTo(&bindOffsetPtrData, &bindOffsetPtrInd);
while ( nRow < nCountRow && (resultSet->*fetchNext)() )
{
++countFetched;
++rowNumber; // Should stand only here!!!
if ( fetchRetData == SQL_RD_ON )
returnDataFromExtendedFetch();
bindOffsetPtrInd += sizeof(SQLLEN);
++bindOffsetPtrTmp;
++nRow;
if ( maxRows && nRow == maxRows )
break;
}
if ( statusPtr && nRow )
memset(statusPtr, SQL_ROW_SUCCESS, sizeof(*statusPtr) * nRow);
}
*rowCountPt = nRow;
setZeroColumn(rowNumber);
bindOffsetPtr = bindOffsetPtrSave;
if( !nRow || nRow < nCountRow )
{
eof = true;
if( nRow && statusPtr )
{
SQLUSMALLINT * pt = statusPtr + nRow;
SQLUSMALLINT * ptEnd = statusPtr + nCountRow;
while ( pt < ptEnd )
*pt++ = SQL_ROW_NOROW;
}
else if ( !nRow )
return SQL_NO_DATA;
}
}
else
{
*rowCountPt = 0;
return SQL_NO_DATA;
}
}
catch ( std::exception &ex )
{
SQLException &exception = (SQLException&)ex;
bindOffsetPtr = bindOffsetPtrSave;
OdbcError *error = postError ("HY000", exception);
error->setRowNumber (rowNumber);
return SQL_ERROR;
}
return sqlSuccess();
}
SQLRETURN OdbcStatement::sqlFetch()
{
clearErrors();
if (!resultSet)
return sqlReturn (SQL_ERROR, "24000", "Invalid cursor state");
if (cancel)
{
releaseResultSet();
return sqlReturn (SQL_ERROR, "S1008", "Operation canceled");
}
if( enFetch == NoneFetch )
{
enFetch = Fetch;
schemaFetchData = getSchemaFetchData();
rebindColumn();
convert->setBindOffsetPtrFrom(sqldataOutOffsetPtr, NULL);
isFetchStaticCursor = isStaticCursor();
}
if ( isFetchStaticCursor )
return sqlFetchScrollCursorStatic ( SQL_FETCH_NEXT, 1);
return fetchData();
}
#ifdef DEBUG
char *strDebOrientFetch[]=
{
"",
"SQL_FETCH_NEXT",
"SQL_FETCH_FIRST",
"SQL_FETCH_LAST",
"SQL_FETCH_PRIOR",
"SQL_FETCH_ABSOLUTE",
"SQL_FETCH_RELATIVE",
"",
"SQL_FETCH_BOOKMARK",
""
};
#endif
SQLRETURN OdbcStatement::sqlFetchScrollCursorStatic(int orientation, int offset)
{
SQLLEN *&bindOffsetPtr = applicationRowDescriptor->headBindOffsetPtr;
int rowsetSize = applicationRowDescriptor->headArraySize;
bool bFetchAbsolute;
SQLULEN rowCount;
SQLULEN *rowCountPt = implementationRowDescriptor->headRowsProcessedPtr ? implementationRowDescriptor->headRowsProcessedPtr
: &rowCount;
rowNumber = resultSet->getPosRowInSet();
switch(orientation)
{
case SQL_FETCH_RELATIVE:
if ( resultSet->isCurrRowsetStart() )
{
if ( !rowNumber && offset < 0 )
{
resultSet->beforeFirst();
resultSet->setPosRowInSet(0);
return SQL_NO_DATA;
}
int checkRow = rowNumber + offset;
if ( rowNumber > 0 && checkRow < 0 )
{
if ( abs( offset ) > rowsetSize )
{
resultSet->beforeFirst();
resultSet->setPosRowInSet(0);
return SQL_NO_DATA;
}
rowNumber = 0;
postError( "01S06", "Attempt to fetch before the result set returned the first rowset" );
}
else if ( checkRow > sqlDiagCursorRowCount )
{
resultSet->afterLast();
resultSet->setPosRowInSet(sqlDiagCursorRowCount ? sqlDiagCursorRowCount - 1 : 0);
return SQL_NO_DATA;
}
rowNumber = checkRow;
break;
}
bFetchAbsolute = ( resultSet->isBeforeFirst() && offset > 0 ) || ( resultSet->isAfterLast() && offset < 0 );
if ( !bFetchAbsolute )
{
if ( resultSet->isBeforeFirst() )
{
if ( offset <= 0 )
{
resultSet->beforeFirst();
resultSet->setPosRowInSet(0);
return SQL_NO_DATA;
}
}
else if ( resultSet->isAfterLast() )
{
if ( offset >= 0 )
{
resultSet->afterLast();
resultSet->setPosRowInSet(sqlDiagCursorRowCount ? sqlDiagCursorRowCount - 1 : 0);
return SQL_NO_DATA;
}
}
rowNumber += offset;
break;
}
case SQL_FETCH_ABSOLUTE:
if ( offset > 0 )
rowNumber = offset - 1;
else if( offset == -1 )
{
rowNumber = sqlDiagCursorRowCount - rowsetSize;
if ( rowNumber < 0 )
rowNumber = 0;
}
else if ( offset < 0 )
{
if( abs(offset) > sqlDiagCursorRowCount )
{
if ( abs(offset) > rowsetSize )
{
resultSet->beforeFirst();
resultSet->setPosRowInSet(0);
return SQL_NO_DATA;
}
rowNumber = 0;
postError( "01S06", "Attempt to fetch before the result set returned the first rowset" );
}
else
rowNumber = sqlDiagCursorRowCount + offset;
}
else if( !offset )
{
resultSet->beforeFirst();
resultSet->setPosRowInSet(0);
return SQL_NO_DATA;
}
else // if( offset > sqlDiagCursorRowCount )
{
resultSet->afterLast();
resultSet->setPosRowInSet(sqlDiagCursorRowCount ? sqlDiagCursorRowCount - 1 : 0);
return SQL_NO_DATA;
}
break;
case SQL_FETCH_NEXT:
if ( eof && rowNumber == sqlDiagCursorRowCount - 1 )
return SQL_NO_DATA;
rowNumber += lastRowsetSize;
break;
case SQL_FETCH_LAST:
if( sqlDiagCursorRowCount )
{
rowNumber = sqlDiagCursorRowCount - rowsetSize;
if ( rowNumber < 0 )
rowNumber = 0;