Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TOracleStatement.cxx
Go to the documentation of this file.
1// @(#)root/oracle:$Id$
2// Author: Sergey Linev 6/02/2006
3
4
5/*************************************************************************
6 * Copyright (C) 1995-2006, Rene Brun and Fons Rademakers. *
7 * All rights reserved. *
8 * *
9 * For the licensing terms see $ROOTSYS/LICENSE. *
10 * For the list of contributors see $ROOTSYS/README/CREDITS. *
11 *************************************************************************/
12
13//////////////////////////////////////////////////////////////////////////
14// //
15// SQL statement class for Oracle //
16// //
17// See TSQLStatement class documentation for more details. //
18// //
19//////////////////////////////////////////////////////////////////////////
20
21#include "TOracleStatement.h"
22#include "TOracleServer.h"
23#include "TDataType.h"
24#include "snprintf.h"
25#include <cstdlib>
26
27#include <occi.h>
28
30
31using namespace oracle::occi;
32
33
34////////////////////////////////////////////////////////////////////////////////
35/// Normal constructor of TOracleStatement class
36/// On creation time specifies buffer length, which should be
37/// used in data fetching or data inserting
38
39TOracleStatement::TOracleStatement(Environment* env, Connection* conn, Statement* stmt, Int_t niter, Bool_t errout) :
40 TSQLStatement(errout),
41 fEnv(env),
42 fConn(conn),
43 fStmt(stmt),
44 fNumIterations(niter),
45 fTimeFmt(TOracleServer::GetDatimeFormat())
46{
47 if (fStmt) {
48 fStmt->setPrefetchMemorySize(1000000);
49 fStmt->setPrefetchRowCount(niter);
50 fStmt->setMaxIterations(niter);
51 }
52}
53
54////////////////////////////////////////////////////////////////////////////////
55/// Destructor of TOracleStatement clas
56
58{
59 Close();
60}
61
62////////////////////////////////////////////////////////////////////////////////
63/// Close Oracle statement
64/// Removes and destroys all buffers and metainfo
65
67{
68
69 if (fFieldInfo)
70 delete fFieldInfo;
71
72 if (fResult && fStmt)
73 fStmt->closeResultSet(fResult);
74
75 if (fConn && fStmt)
76 fConn->terminateStatement(fStmt);
77
79
80 fConn = nullptr;
81 fStmt = nullptr;
82 fResult = nullptr;
83 fFieldInfo = nullptr;
84 fIterCounter = 0;
85}
86
87// Check that statement is ready for use
88#define CheckStatement(method, res) \
89 { \
90 ClearError(); \
91 if (fStmt==0) { \
92 SetError(-1,"Statement is not correctly initialized",method); \
93 return res; \
94 } \
95 }
96
97// Check that parameter can be set for statement
98#define CheckSetPar(method) \
99 { \
100 CheckStatement(method, kFALSE); \
101 if (!IsParSettMode()) { \
102 SetError(-1,"Parameters cannot be set for this statement", method); \
103 return kFALSE; \
104 } \
105 if (npar<0) { \
106 TString errmsg("Invalid parameter number "); \
107 errmsg+= npar; \
108 SetError(-1,errmsg.Data(),method); \
109 return kFALSE; \
110 } \
111 }
112
113#define CheckGetField(method, defres) \
114 { \
115 ClearError(); \
116 if (!IsResultSet()) { \
117 SetError(-1,"There is no result set for statement", method); \
118 return defres; \
119 } \
120 if ((npar<0) || (npar>=fBufferSize)) { \
121 TString errmsg("Invalid parameter number "); \
122 errmsg+= npar; \
123 SetError(-1,errmsg.Data(),method); \
124 return defres; \
125 } \
126 }
127
128////////////////////////////////////////////////////////////////////////////////
129/// Set buffer size, which is used to keep string values of
130/// currently fetched column.
131
133{
134 CloseBuffer();
135 if (size<=0) return;
137 fBuffer = new TBufferRec[size];
138 for (Int_t n=0;n<fBufferSize;n++) {
139 fBuffer[n].membuf = nullptr;
140 fBuffer[n].bufsize = -1;
141 }
142}
143
144////////////////////////////////////////////////////////////////////////////////
145/// Destroy buffers, used in data fetching
146
148{
149 if (fBuffer) {
150 for (Int_t n=0;n<fBufferSize;n++) {
151 if (fBuffer[n].membuf)
152 free(fBuffer[n].membuf);
153 }
154
155 delete[] fBuffer;
156 }
157 fBuffer = nullptr;
158 fBufferSize = 0;
159}
160
161////////////////////////////////////////////////////////////////////////////////
162/// Process SQL statement
163
165{
166 CheckStatement("Process", kFALSE);
167
168 try {
169
170 if (IsParSettMode()) {
171 fStmt->executeUpdate();
172 fWorkingMode = 0;
173 } else {
174 fStmt->execute();
175 }
176
177 return kTRUE;
178 } catch (SQLException &oraex) {
179 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "Process");
180 }
181
182 return kFALSE;
183}
184
185////////////////////////////////////////////////////////////////////////////////
186/// Return number of affected rows after statement Process() was called
187/// Make sense for queries like SELECT, INSERT, UPDATE
188
190{
191 CheckStatement("GetNumAffectedRows", -1);
192
193 try {
194 return fStmt->getUpdateCount();
195 } catch (SQLException &oraex) {
196 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetNumAffectedRows");
197 }
198 return -1;
199}
200
201
202////////////////////////////////////////////////////////////////////////////////
203/// Return number of parameters in statement
204/// Not yet implemented for Oracle
205
207{
208 CheckStatement("GetNumParameters", -1);
209
210 Info("GetParametersNumber","Not implemented");
211
212 return 0;
213}
214
215////////////////////////////////////////////////////////////////////////////////
216/// Set NULL as value of parameter npar
217
219{
220 CheckSetPar("SetNull");
221
222 try {
223 fStmt->setNull(npar+1, OCCIINT);
224
225 return kTRUE;
226 } catch (SQLException &oraex) {
227 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetNull");
228 }
229
230 return kFALSE;
231}
232
233
234////////////////////////////////////////////////////////////////////////////////
235/// Set integer value for parameter npar
236
238{
239 CheckSetPar("SetInt");
240
241 try {
242 fStmt->setInt(npar+1, value);
243
244 return kTRUE;
245 } catch (SQLException &oraex) {
246 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetInt");
247 }
248
249 return kFALSE;
250}
251
252////////////////////////////////////////////////////////////////////////////////
253/// Set unsigned integer value for parameter npar
254
256{
257 CheckSetPar("SetUInt");
258
259 try {
260 fStmt->setUInt(npar+1, value);
261 return kTRUE;
262 } catch (SQLException &oraex) {
263 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetUInt");
264 }
265
266 return kFALSE;
267}
268
269////////////////////////////////////////////////////////////////////////////////
270/// Set long integer value for parameter npar
271
273{
274 CheckSetPar("SetLong");
275
276 try {
277 fStmt->setNumber(npar+1, Number(value));
278 return kTRUE;
279 } catch (SQLException &oraex) {
280 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetLong");
281 }
282 return kFALSE;
283}
284
285////////////////////////////////////////////////////////////////////////////////
286/// Set 64-bit integer value for parameter npar
287
289{
290 CheckSetPar("SetLong64");
291
292 try {
293 fStmt->setNumber(npar+1, Number((long double)value));
294 return kTRUE;
295 } catch (SQLException &oraex) {
296 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetLong64");
297 }
298 return kFALSE;
299}
300
301////////////////////////////////////////////////////////////////////////////////
302/// Set unsigned 64-bit integer value for parameter npar
303
305{
306 CheckSetPar("SetULong64");
307
308 try {
309 fStmt->setNumber(npar+1, Number((long double)value));
310 return kTRUE;
311 } catch (SQLException &oraex) {
312 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetULong64");
313 }
314 return kFALSE;
315}
316
317////////////////////////////////////////////////////////////////////////////////
318/// Set double value for parameter npar
319
321{
322 CheckSetPar("SetDouble");
323
324 try {
325 fStmt->setDouble(npar+1, value);
326 return kTRUE;
327 } catch (SQLException &oraex) {
328 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetDouble");
329 }
330 return kFALSE;
331}
332
333////////////////////////////////////////////////////////////////////////////////
334/// Set string value for parameter npar
335
336Bool_t TOracleStatement::SetString(Int_t npar, const char* value, Int_t maxsize)
337{
338 CheckSetPar("SetString");
339
340 try {
341
342 // this is when NextIteration is called first time
343 if (fIterCounter==1) {
344 fStmt->setDatabaseNCHARParam(npar+1, true);
345 fStmt->setMaxParamSize(npar+1, maxsize);
346 }
347
348 fStmt->setString(npar+1, value);
349 return kTRUE;
350 } catch (SQLException &oraex) {
351 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetString");
352 }
353 return kFALSE;
354}
355
356////////////////////////////////////////////////////////////////////////////////
357/// set parameter value as binary data
358
360{
361 CheckSetPar("SetBinary");
362
363 try {
364
365 // this is when NextIteration is called first time
366 if (fIterCounter==1)
367 fStmt->setMaxParamSize(npar+1, maxsize);
368
369 Bytes buf((unsigned char*) mem, size);
370
371 fStmt->setBytes(npar+1, buf);
372
373 return kTRUE;
374
375 } catch (SQLException &oraex) {
376 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetBinary");
377 }
378 return kFALSE;
379}
380
381////////////////////////////////////////////////////////////////////////////////
382/// Set date value for parameter npar
383
385{
386 CheckSetPar("SetDate");
387
388 try {
389 Date tm = fStmt->getDate(npar+1);
390 int o_year;
391 unsigned int o_month, o_day, o_hour, o_minute, o_second;
392 tm.getDate(o_year, o_month, o_day, o_hour, o_minute, o_second);
393 tm.setDate(year, month, day, o_hour, o_minute, o_second);
394 fStmt->setDate(npar+1, tm);
395 return kTRUE;
396 } catch (SQLException &oraex) {
397 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetDate");
398 }
399
400 return kFALSE;
401}
402
403////////////////////////////////////////////////////////////////////////////////
404/// Set time value for parameter npar
405
407{
408 CheckSetPar("SetTime");
409
410 try {
411 Date tm = fStmt->getDate(npar+1);
412 int o_year;
413 unsigned int o_month, o_day, o_hour, o_minute, o_second;
414 tm.getDate(o_year, o_month, o_day, o_hour, o_minute, o_second);
415 tm.setDate(o_year, o_month, o_day, hour, min, sec);
416 fStmt->setDate(npar+1, tm);
417 return kTRUE;
418 } catch (SQLException &oraex) {
419 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetTime");
420 }
421
422 return kFALSE;
423}
424
425////////////////////////////////////////////////////////////////////////////////
426/// Set date & time value for parameter npar
427
428Bool_t TOracleStatement::SetDatime(Int_t npar, Int_t year, Int_t month, Int_t day, Int_t hour, Int_t min, Int_t sec)
429{
430 CheckSetPar("SetDatime");
431
432 try {
433 Date tm(fEnv, year, month, day, hour, min, sec);
434 fStmt->setDate(npar+1, tm);
435 return kTRUE;
436 } catch (SQLException &oraex) {
437 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetDatime");
438 }
439
440 return kFALSE;
441}
442
443////////////////////////////////////////////////////////////////////////////////
444/// Set date & time value for parameter npar
445
446Bool_t TOracleStatement::SetTimestamp(Int_t npar, Int_t year, Int_t month, Int_t day, Int_t hour, Int_t min, Int_t sec, Int_t frac)
447{
448 CheckSetPar("SetTimestamp");
449
450 try {
451 Timestamp tm(fEnv, year, month, day, hour, min, sec, frac);
452 fStmt->setTimestamp(npar+1, tm);
453 return kTRUE;
454 } catch (SQLException &oraex) {
455 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetTimestamp");
456 }
457
458 return kFALSE;
459}
460
461////////////////////////////////////////////////////////////////////////////////
462/// Set vector of integer values for parameter npar
463
464Bool_t TOracleStatement::SetVInt(Int_t npar, const std::vector<Int_t> value, const char* schemaName, const char* typeName)
465{
466 CheckSetPar("SetVInt");
467
468 try {
469 setVector(fStmt, npar+1, value, schemaName, typeName);
470 return kTRUE;
471 } catch (SQLException &oraex) {
472 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetVInt");
473 }
474
475 return kFALSE;
476}
477
478////////////////////////////////////////////////////////////////////////////////
479/// Set vector of unsigned integer values for parameter npar
480
481Bool_t TOracleStatement::SetVUInt(Int_t npar, const std::vector<UInt_t> value, const char* schemaName, const char* typeName)
482{
483 CheckSetPar("SetVUInt");
484
485 try {
486 setVector(fStmt, npar+1, value, schemaName, typeName);
487 return kTRUE;
488 } catch (SQLException &oraex) {
489 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetVUInt");
490 }
491
492 return kFALSE;
493}
494
495////////////////////////////////////////////////////////////////////////////////
496/// Set vector of long integer values for parameter npar
497
498Bool_t TOracleStatement::SetVLong(Int_t npar, const std::vector<Long_t> value, const char* schemaName, const char* typeName)
499{
500 CheckSetPar("SetVLong");
501
502 try {
503 std::vector<Number> nvec;
504 for (std::vector<Long_t>::const_iterator it = value.begin();
505 it != value.end();
506 ++it) {
507 nvec.push_back(Number(*it));
508 }
509 setVector(fStmt, npar+1, nvec, schemaName, typeName);
510 return kTRUE;
511 } catch (SQLException &oraex) {
512 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetVLong");
513 }
514 return kFALSE;
515}
516
517////////////////////////////////////////////////////////////////////////////////
518/// Set vector of 64-bit integer values for parameter npar
519
520Bool_t TOracleStatement::SetVLong64(Int_t npar, const std::vector<Long64_t> value, const char* schemaName, const char* typeName)
521{
522 CheckSetPar("SetVLong64");
523
524 try {
525 std::vector<Number> nvec;
526 for (std::vector<Long64_t>::const_iterator it = value.begin();
527 it != value.end();
528 ++it) {
529 nvec.push_back(Number((long double)*it));
530 }
531 setVector(fStmt, npar+1, nvec, schemaName, typeName);
532 return kTRUE;
533 } catch (SQLException &oraex) {
534 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetVLong64");
535 }
536 return kFALSE;
537}
538
539////////////////////////////////////////////////////////////////////////////////
540/// Set vector of unsigned 64-bit integer values for parameter npar
541
542Bool_t TOracleStatement::SetVULong64(Int_t npar, std::vector<ULong64_t> value, const char* schemaName, const char* typeName)
543{
544 CheckSetPar("SetVULong64");
545
546 try {
547 std::vector<Number> nvec;
548 for (std::vector<ULong64_t>::const_iterator it = value.begin();
549 it != value.end();
550 ++it) {
551 nvec.push_back(Number((long double)*it));
552 }
553 setVector(fStmt, npar+1, nvec, schemaName, typeName);
554 return kTRUE;
555 } catch (SQLException &oraex) {
556 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetVULong64");
557 }
558 return kFALSE;
559}
560
561////////////////////////////////////////////////////////////////////////////////
562/// Set vector of double values for parameter npar
563
564Bool_t TOracleStatement::SetVDouble(Int_t npar, const std::vector<Double_t> value, const char* schemaName, const char* typeName)
565{
566 CheckSetPar("SetVDouble");
567
568 try {
569 setVector(fStmt, npar+1, value, schemaName, typeName);
570 return kTRUE;
571 } catch (SQLException &oraex) {
572 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetVDouble");
573 }
574 return kFALSE;
575}
576
577////////////////////////////////////////////////////////////////////////////////
578/// Add next iteration for statement with parameters
579
581{
582 CheckStatement("NextIteration", kFALSE);
583
584 try {
585 fWorkingMode=1;
586 // if number of iterations achievs limit, execute it and continue to fill
587 if ((fIterCounter % fNumIterations == 0) && (fIterCounter>0)) {
588 fStmt->executeUpdate();
589 }
590
591 if (fIterCounter % fNumIterations != 0) {
592 fStmt->addIteration();
593 }
594
595 fIterCounter++;
596
597 return kTRUE;
598 } catch (SQLException &oraex) {
599 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "NextIteration");
600 }
601 return kFALSE;
602}
603
604////////////////////////////////////////////////////////////////////////////////
605/// Store result of statement processing.
606/// Required to access results of SELECT queries
607
609{
610 CheckStatement("StoreResult", kFALSE);
611
612 try {
613 if (fStmt->status() == Statement::RESULT_SET_AVAILABLE) {
614 fResult = fStmt->getResultSet();
615 fFieldInfo = (fResult==0) ? 0 : new std::vector<MetaData>(fResult->getColumnListMetaData());
616 Int_t count = (fFieldInfo==0) ? 0 : fFieldInfo->size();
617 SetBufferSize(count);
618 if ((fResult!=0) && (count>0)) fWorkingMode = 2;
619
620 return IsResultSet();
621 }
622 } catch (SQLException &oraex) {
623 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "StoreResult");
624 }
625 return kFALSE;
626}
627
628////////////////////////////////////////////////////////////////////////////////
629/// Defines maximum size for field which must be used for read or write operation
630/// Some Oracle types as LONG (long binary continer) requires this call
631/// before any data can be read from database. Call it once before first call to NextResultRow()
632
634{
635 CheckStatement("SetMaxFieldSize", kFALSE);
636
637 try {
638 if (fResult)
639 fResult->setMaxColumnSize(nfield+1, maxsize);
640 else
641 fStmt->setMaxParamSize(nfield+1, maxsize);
642 return kTRUE;
643 } catch (SQLException &oraex) {
644 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "SetMaxFieldSize");
645 }
646
647 return kFALSE;
648}
649
650////////////////////////////////////////////////////////////////////////////////
651/// Returns number of fields in result set
652
654{
655 return IsResultSet() ? fBufferSize : -1;
656}
657
658////////////////////////////////////////////////////////////////////////////////
659/// Return field name in result set
660
662{
663 CheckGetField("GetFieldName", 0);
664
665 if (!IsResultSet() || (npar<0) || (npar>=fBufferSize)) return nullptr;
666
667 if (fBuffer[npar].namebuf.empty())
668 fBuffer[npar].namebuf = (*fFieldInfo)[npar].getString(MetaData::ATTR_NAME);
669
670 return fBuffer[npar].namebuf.empty() ? nullptr : fBuffer[npar].namebuf.c_str();
671}
672
673////////////////////////////////////////////////////////////////////////////////
674/// Move cursor to next row in result set.
675/// For Oracle it may lead to additional request to database
676
678{
679 ClearError();
680
681 if (fResult==0) {
682 SetError(-1,"There is no result set for statement", "NextResultRow");
683 return kFALSE;
684 }
685
686 if (fResult==0) return kFALSE;
687
688 try {
689 for (int n=0;n<fBufferSize;n++) {
690 if (fBuffer[n].membuf) {
691 free(fBuffer[n].membuf);
692 fBuffer[n].membuf = nullptr;
693 }
694 fBuffer[n].bufsize = -1;
695 }
696 if (fResult->next() == oracle::occi::ResultSet::END_OF_FETCH) {
697 fWorkingMode = 0;
698 CloseBuffer();
699 return kFALSE;
700 }
701 return kTRUE;
702 } catch (SQLException &oraex) {
703 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "NextResultRow");
704
705 if (oraex.getErrorCode()==32108)
706 Info("NextResultRow", "Use TSQLStatement::SetMaxFieldSize() to solve a problem");
707
708 }
709
710 return kFALSE;
711}
712
713////////////////////////////////////////////////////////////////////////////////
714/// Checks if fieled value in result set is NULL
715
717{
718 CheckGetField("IsNull", kFALSE);
719
720 try {
721 return fResult->isNull(npar+1);
722 } catch (SQLException &oraex) {
723 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "IsNull");
724 }
725
726 return kTRUE;
727}
728
729////////////////////////////////////////////////////////////////////////////////
730/// return field value as integer
731
733{
734 CheckGetField("GetInt", 0);
735
736 Int_t res = 0;
737
738 try {
739 if (!fResult->isNull(npar+1))
740 res = fResult->getInt(npar+1);
741 } catch (SQLException &oraex) {
742 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetInt");
743 }
744
745 return res;
746}
747
748////////////////////////////////////////////////////////////////////////////////
749/// return field value as unsigned integer
750
752{
753 CheckGetField("GetUInt", 0);
754
755 UInt_t res = 0;
756
757 try {
758 if (!fResult->isNull(npar+1))
759 res = fResult->getUInt(npar+1);
760 } catch (SQLException &oraex) {
761 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetUInt");
762 }
763
764 return res;
765}
766
767
768////////////////////////////////////////////////////////////////////////////////
769/// return field value as long integer
770
772{
773 CheckGetField("GetLong", 0);
774
775 Long_t res = 0;
776
777 try {
778 if (!fResult->isNull(npar+1))
779 res = (Long_t) fResult->getNumber(npar+1);
780 } catch (SQLException &oraex) {
781 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetLong");
782 }
783
784 return res;
785}
786
787////////////////////////////////////////////////////////////////////////////////
788/// return field value as 64-bit integer
789
791{
792 CheckGetField("GetLong64", 0);
793
794 Long64_t res = 0;
795
796 try {
797 if (!fResult->isNull(npar+1))
798 res = (Long64_t) (long double) fResult->getNumber(npar+1);
799 } catch (SQLException &oraex) {
800 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetLong64");
801 }
802
803 return res;
804}
805
806////////////////////////////////////////////////////////////////////////////////
807/// return field value as unsigned 64-bit integer
808
810{
811 CheckGetField("GetULong64", 0);
812
813 ULong64_t res = 0;
814
815 try {
816 if (!fResult->isNull(npar+1))
817 res = (ULong64_t) (long double) fResult->getNumber(npar+1);
818 } catch (SQLException &oraex) {
819 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetULong64");
820 }
821
822 return res;
823}
824
825////////////////////////////////////////////////////////////////////////////////
826/// return field value as double
827
829{
830 CheckGetField("GetDouble", 0.);
831
832 Double_t res = 0;
833
834 try {
835 if (!fResult->isNull(npar+1))
836 res = fResult->getDouble(npar+1);
837 } catch (SQLException &oraex) {
838 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetDouble");
839 }
840
841 return res;
842}
843
844////////////////////////////////////////////////////////////////////////////////
845/// return field value as string
846
848{
849 CheckGetField("GetString", 0);
850
851 if (fBuffer[npar].membuf)
852 return (const char *) fBuffer[npar].membuf;
853
854 try {
855 if (fResult->isNull(npar+1)) return 0;
856
857 int datatype = (*fFieldInfo)[npar].getInt(MetaData::ATTR_DATA_TYPE);
858
859 std::string res;
860
861 switch (datatype) {
862 case SQLT_NUM: { // oracle numeric NUMBER
863 int prec = (*fFieldInfo)[npar].getInt(MetaData::ATTR_PRECISION);
864 int scale = (*fFieldInfo)[npar].getInt(MetaData::ATTR_SCALE);
865
866 if ((scale == 0) || (prec == 0)) {
867 res = fResult->getString(npar+1);
868 } else {
869 double double_val = fResult->getDouble(npar+1);
870 char str_number[50];
871 snprintf(str_number, sizeof(str_number), TSQLServer::GetFloatFormat(), double_val);
872 res = str_number;
873 }
874 break;
875 }
876 case SQLT_CHR: // character string
877 case SQLT_VCS: // variable character string
878 case SQLT_AFC: // ansi fixed char
879 case SQLT_AVC: // ansi var char
880 res = fResult->getString(npar+1);
881 break;
882 case SQLT_DAT: // Oracle native DATE type
883 res = (fResult->getDate(npar+1)).toText(fTimeFmt.Data());
884 break;
885 case SQLT_TIMESTAMP: // TIMESTAMP
886 case SQLT_TIMESTAMP_TZ: // TIMESTAMP WITH TIMEZONE
887 case SQLT_TIMESTAMP_LTZ: // TIMESTAMP WITH LOCAL TIMEZONE
888 res = (fResult->getTimestamp(npar+1)).toText(fTimeFmt.Data(), 0);
889 break;
890 default:
891 res = fResult->getString(npar+1);
892 Info("getString","Type %d may not be supported", datatype);
893 }
894
895 int len = res.length();
896
897 if (len > 0) {
898 fBuffer[npar].membuf = malloc(len+1);
899 fBuffer[npar].bufsize = len+1;
900 strncpy((char *) fBuffer[npar].membuf, res.c_str(), len+1);
901 }
902
903 return (const char *)fBuffer[npar].membuf;
904
905 } catch (SQLException &oraex) {
906 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetString");
907 }
908
909 return 0;
910}
911
912////////////////////////////////////////////////////////////////////////////////
913/// Return field value as binary array
914/// Supports LONG, BLOB, CLOB, BFILE, CFILE types of columns
915/// Reads complete content of the column, therefore not suitable for
916/// big structures
917
919{
920 mem = 0;
921 size = 0;
922
923 CheckGetField("GetBinary", kFALSE);
924
925 if (fBuffer[npar].bufsize >= 0) {
926 mem = fBuffer[npar].membuf;
927 size = fBuffer[npar].bufsize;
928 return kTRUE;
929 }
930
931 try {
932 if (fResult->isNull(npar+1)) return kTRUE;
933
934 int datatype = (*fFieldInfo)[npar].getInt(MetaData::ATTR_DATA_TYPE);
935
936 switch (datatype) {
937 case SQLT_LNG: {
938 Bytes parbytes = fResult->getBytes(npar+1);
939
940 size = parbytes.length();
941
942 fBuffer[npar].bufsize = size;
943
944 if (size > 0) {
945 mem = malloc(size);
946
947 fBuffer[npar].membuf = mem;
948
949 parbytes.getBytes((unsigned char *) mem, size);
950 }
951
952 break;
953 }
954
955 case SQLT_BLOB: {
956 Blob parblob = fResult->getBlob(npar+1);
957
958 size = parblob.length();
959
960 fBuffer[npar].bufsize = size;
961
962 if (size > 0) {
963 mem = malloc(size);
964
965 fBuffer[npar].membuf = mem;
966
967 parblob.read(size, (unsigned char *) mem, size);
968 }
969
970 break;
971 }
972
973 case SQLT_CLOB: {
974 Clob parclob = fResult->getClob(npar+1);
975
976 size = parclob.length();
977
978 fBuffer[npar].bufsize = size;
979
980 if (size > 0) {
981 mem = malloc(size);
982
983 fBuffer[npar].membuf = mem;
984
985 parclob.read(size, (unsigned char *) mem, size);
986 }
987
988 break;
989 }
990
991 case SQLT_BFILEE:
992 case SQLT_CFILEE: {
993
994 Bfile parbfile = fResult->getBfile(npar+1);
995
996 size = parbfile.length();
997
998 fBuffer[npar].bufsize = size;
999
1000 if (size>0) {
1001 mem = malloc(size);
1002
1003 fBuffer[npar].membuf = mem;
1004
1005 parbfile.read(size, (unsigned char *) mem, size);
1006 }
1007
1008 break;
1009 }
1010
1011 default:
1012 Error("GetBinary", "Oracle data type %d not supported", datatype);
1013 SetError(-1, "Unsupported type for binary convertion", "GetBinary");
1014 return false;
1015 }
1016
1017 return kTRUE;
1018
1019 } catch (SQLException &oraex) {
1020 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetBinary");
1021 }
1022
1023 return kFALSE;
1024}
1025
1026
1027////////////////////////////////////////////////////////////////////////////////
1028/// return field value as date
1029
1031{
1032 Int_t hour, min, sec;
1033
1034 return GetDatime(npar, year, month, day, hour, min, sec);
1035}
1036
1037////////////////////////////////////////////////////////////////////////////////
1038/// return field value as time
1039
1041{
1042 Int_t year, month, day;
1043
1044 return GetDatime(npar, year, month, day, hour, min, sec);
1045}
1046
1047////////////////////////////////////////////////////////////////////////////////
1048/// return field value as date & time
1049
1050Bool_t TOracleStatement::GetDatime(Int_t npar, Int_t& year, Int_t& month, Int_t& day, Int_t& hour, Int_t& min, Int_t& sec)
1051{
1052 CheckGetField("GetDatime", kFALSE);
1053
1054 try {
1055 if (!fResult->isNull(npar+1)) {
1056 int datatype = (*fFieldInfo)[npar].getInt(MetaData::ATTR_DATA_TYPE);
1057
1058 if (datatype!=SQLT_DAT) return kFALSE;
1059
1060 Date tm = fResult->getDate(npar+1);
1061 int o_year;
1062 unsigned int o_month, o_day, o_hour, o_minute, o_second;
1063 tm.getDate(o_year, o_month, o_day, o_hour, o_minute, o_second);
1064 year = (Int_t) o_year;
1065 month = (Int_t) o_month;
1066 day = (Int_t) o_day;
1067 hour = (Int_t) o_hour;
1068 min = (Int_t) o_minute;
1069 sec = (Int_t) o_second;
1070 return kTRUE;
1071 }
1072 } catch (SQLException &oraex) {
1073 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetDatime");
1074 }
1075
1076 return kFALSE;
1077}
1078
1079////////////////////////////////////////////////////////////////////////////////
1080/// return field value as date & time
1081
1082Bool_t TOracleStatement::GetTimestamp(Int_t npar, Int_t& year, Int_t& month, Int_t& day, Int_t& hour, Int_t& min, Int_t& sec, Int_t& frac)
1083{
1084 CheckGetField("GetTimestamp", kFALSE);
1085
1086 try {
1087 if (!fResult->isNull(npar+1)) {
1088 int datatype = (*fFieldInfo)[npar].getInt(MetaData::ATTR_DATA_TYPE);
1089
1090 if ((datatype!=SQLT_TIMESTAMP) &&
1091 (datatype!=SQLT_TIMESTAMP_TZ) &&
1092 (datatype!=SQLT_TIMESTAMP_LTZ)) return kFALSE;
1093
1094 Timestamp tm = fResult->getTimestamp(npar+1);
1095 int o_year;
1096 unsigned int o_month, o_day, o_hour, o_minute, o_second, o_frac;
1097 tm.getDate(o_year, o_month, o_day);
1098 tm.getTime(o_hour, o_minute, o_second, o_frac);
1099 year = (Int_t) o_year;
1100 month = (Int_t) o_month;
1101 day = (Int_t) o_day;
1102 hour = (Int_t) o_hour;
1103 min = (Int_t) o_minute;
1104 sec = (Int_t) o_second;
1105 frac = (Int_t) o_frac;
1106 return kTRUE;
1107 }
1108 } catch (SQLException &oraex) {
1109 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetTimestamp");
1110 }
1111
1112 return kFALSE;
1113}
1114
1115////////////////////////////////////////////////////////////////////////////////
1116/// return field value as vector of integers
1117
1118Bool_t TOracleStatement::GetVInt(Int_t npar, std::vector<Int_t> &value)
1119{
1120 CheckGetField("GetVInt", kFALSE);
1121 try {
1122 if (!fResult->isNull(npar+1))
1123 getVector(fResult, npar+1, value);
1124 return kTRUE;
1125 } catch (SQLException &oraex) {
1126 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetVInt");
1127 }
1128 return kFALSE;
1129}
1130
1131////////////////////////////////////////////////////////////////////////////////
1132/// return field value as vector of unsigned integers
1133
1134Bool_t TOracleStatement::GetVUInt(Int_t npar, std::vector<UInt_t> &value)
1135{
1136 CheckGetField("GetVUInt", kFALSE);
1137 try {
1138 if (!fResult->isNull(npar+1))
1139 getVector(fResult, npar+1, value);
1140 return kTRUE;
1141 } catch (SQLException &oraex) {
1142 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetVUInt");
1143 }
1144 return kFALSE;
1145}
1146
1147
1148////////////////////////////////////////////////////////////////////////////////
1149/// return field value as vector of long integers
1150
1151Bool_t TOracleStatement::GetVLong(Int_t npar, std::vector<Long_t> &value)
1152{
1153 CheckGetField("GetVLong", kFALSE);
1154 try {
1155 std::vector<Number> res;
1156 if (!fResult->isNull(npar+1))
1157 getVector(fResult, npar+1, res);
1158 for (std::vector<Number>::const_iterator it = res.begin();
1159 it != res.end();
1160 ++it ) {
1161 value.push_back((Long_t)*it);
1162 }
1163 return kTRUE;
1164 } catch (SQLException &oraex) {
1165 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetVLong");
1166 }
1167 return kFALSE;
1168}
1169
1170////////////////////////////////////////////////////////////////////////////////
1171/// return field value as vector of 64-bit integers
1172
1173Bool_t TOracleStatement::GetVLong64(Int_t npar, std::vector<Long64_t> &value)
1174{
1175 CheckGetField("GetVLong64", kFALSE);
1176 try {
1177 std::vector<Number> res;
1178 if (!fResult->isNull(npar+1))
1179 getVector(fResult, npar+1, res);
1180 for (std::vector<Number>::const_iterator it = res.begin();
1181 it != res.end();
1182 ++it ) {
1183 value.push_back((Long_t)*it);
1184 }
1185 return kTRUE;
1186 } catch (SQLException &oraex) {
1187 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetVLong64");
1188 }
1189 return kFALSE;
1190}
1191
1192////////////////////////////////////////////////////////////////////////////////
1193/// return field value as vector of unsigned 64-bit integers
1194
1195Bool_t TOracleStatement::GetVULong64(Int_t npar, std::vector<ULong64_t> &value)
1196{
1197 CheckGetField("GetVULong64", kFALSE);
1198 try {
1199 std::vector<Number> res;
1200 if (!fResult->isNull(npar+1))
1201 getVector(fResult, npar+1, res);
1202 for (std::vector<Number>::const_iterator it = res.begin();
1203 it != res.end();
1204 ++it ) {
1205 value.push_back((Long_t)(long double)*it);
1206 }
1207 return kTRUE;
1208 } catch (SQLException &oraex) {
1209 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetVULong64");
1210 }
1211 return kFALSE;
1212}
1213
1214////////////////////////////////////////////////////////////////////////////////
1215/// return field value as vector of doubles
1216
1217Bool_t TOracleStatement::GetVDouble(Int_t npar, std::vector<Double_t> &value)
1218{
1219 CheckGetField("GetVDouble", kFALSE);
1220 try {
1221 if (!fResult->isNull(npar+1))
1222 getVector(fResult, npar+1, value);
1223 return kTRUE;
1224 } catch (SQLException &oraex) {
1225 SetError(oraex.getErrorCode(), oraex.getMessage().c_str(), "GetVDouble");
1226 }
1227 return kFALSE;
1228}
1229
double
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
int Int_t
Definition RtypesCore.h:45
const Bool_t kFALSE
Definition RtypesCore.h:101
long Long_t
Definition RtypesCore.h:54
bool Bool_t
Definition RtypesCore.h:63
long long Long64_t
Definition RtypesCore.h:80
unsigned long long ULong64_t
Definition RtypesCore.h:81
const Bool_t kTRUE
Definition RtypesCore.h:100
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:364
#define CheckStatement(method, res)
#define CheckSetPar(method)
#define CheckGetField(method, defres)
#define free
Definition civetweb.c:1539
#define snprintf
Definition civetweb.c:1540
#define malloc
Definition civetweb.c:1536
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:963
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition TObject.cxx:937
TOracleStatement(const TOracleStatement &)=delete
Bool_t SetVInt(Int_t npar, const std::vector< Int_t > value, const char *schemaName, const char *typeName) final
Set vector of integer values for parameter npar.
oracle::occi::Statement * fStmt
UInt_t GetUInt(Int_t npar) final
return field value as unsigned integer
Double_t GetDouble(Int_t npar) final
return field value as double
void SetBufferSize(Int_t size)
Set buffer size, which is used to keep string values of currently fetched column.
Long64_t GetLong64(Int_t npar) final
return field value as 64-bit integer
const char * GetString(Int_t npar) final
return field value as string
Bool_t SetDouble(Int_t npar, Double_t value) final
Set double value for parameter npar.
ULong64_t GetULong64(Int_t npar) final
return field value as unsigned 64-bit integer
Bool_t SetLong64(Int_t npar, Long64_t value) final
Set 64-bit integer value for parameter npar.
Bool_t SetVLong64(Int_t npar, const std::vector< Long64_t > value, const char *schemaName, const char *typeName) final
Set vector of 64-bit integer values for parameter npar.
Bool_t SetUInt(Int_t npar, UInt_t value) final
Set unsigned integer value for parameter npar.
oracle::occi::Environment * fEnv
Bool_t GetVDouble(Int_t npar, std::vector< Double_t > &value) final
return field value as vector of doubles
Bool_t GetVULong64(Int_t npar, std::vector< ULong64_t > &value) final
return field value as vector of unsigned 64-bit integers
Bool_t NextResultRow() final
Move cursor to next row in result set.
Bool_t SetBinary(Int_t npar, void *mem, Long_t size, Long_t maxsize=0x1000) final
set parameter value as binary data
virtual ~TOracleStatement()
Destructor of TOracleStatement clas.
Int_t GetNumFields() final
Returns number of fields in result set.
Bool_t SetTimestamp(Int_t npar, Int_t year, Int_t month, Int_t day, Int_t hour, Int_t min, Int_t sec, Int_t frac=0) final
Set date & time value for parameter npar.
const char * GetFieldName(Int_t nfield) final
Return field name in result set.
Bool_t SetDate(Int_t npar, Int_t year, Int_t month, Int_t day) final
Set date value for parameter npar.
Bool_t GetTime(Int_t npar, Int_t &hour, Int_t &min, Int_t &sec) final
return field value as time
Bool_t GetVLong(Int_t npar, std::vector< Long_t > &value) final
return field value as vector of long integers
TBufferRec * fBuffer
Bool_t SetString(Int_t npar, const char *value, Int_t maxsize=256) final
Set string value for parameter npar.
Bool_t SetVUInt(Int_t npar, const std::vector< UInt_t > value, const char *schemaName, const char *typeName) final
Set vector of unsigned integer values for parameter npar.
Bool_t SetMaxFieldSize(Int_t nfield, Long_t maxsize) final
Defines maximum size for field which must be used for read or write operation Some Oracle types as LO...
void CloseBuffer()
Destroy buffers, used in data fetching.
Bool_t SetVULong64(Int_t npar, const std::vector< ULong64_t > value, const char *schemaName, const char *typeName) final
Set vector of unsigned 64-bit integer values for parameter npar.
Bool_t GetVUInt(Int_t npar, std::vector< UInt_t > &value) final
return field value as vector of unsigned integers
Bool_t SetULong64(Int_t npar, ULong64_t value) final
Set unsigned 64-bit integer value for parameter npar.
Bool_t SetLong(Int_t npar, Long_t value) final
Set long integer value for parameter npar.
Bool_t SetTime(Int_t npar, Int_t hour, Int_t min, Int_t sec) final
Set time value for parameter npar.
Bool_t NextIteration() final
Add next iteration for statement with parameters.
Int_t GetInt(Int_t npar) final
return field value as integer
Bool_t IsNull(Int_t) final
Checks if fieled value in result set is NULL.
Bool_t IsParSettMode() const
Bool_t GetVInt(Int_t npar, std::vector< Int_t > &value) final
return field value as vector of integers
Bool_t Process() final
Process SQL statement.
Long_t GetLong(Int_t npar) final
return field value as long integer
Bool_t GetTimestamp(Int_t npar, Int_t &year, Int_t &month, Int_t &day, Int_t &hour, Int_t &min, Int_t &sec, Int_t &frac) final
return field value as date & time
Bool_t SetNull(Int_t npar) final
Set NULL as value of parameter npar.
Int_t GetNumParameters() final
Return number of parameters in statement Not yet implemented for Oracle.
Bool_t SetDatime(Int_t npar, Int_t year, Int_t month, Int_t day, Int_t hour, Int_t min, Int_t sec) final
Set date & time value for parameter npar.
Bool_t GetBinary(Int_t npar, void *&mem, Long_t &size) final
Return field value as binary array Supports LONG, BLOB, CLOB, BFILE, CFILE types of columns Reads com...
void Close(Option_t *="") final
Close Oracle statement Removes and destroys all buffers and metainfo.
oracle::occi::Connection * fConn
Bool_t SetVLong(Int_t npar, const std::vector< Long_t > value, const char *schemaName, const char *typeName) final
Set vector of long integer values for parameter npar.
Bool_t SetVDouble(Int_t npar, const std::vector< Double_t > value, const char *schemaName, const char *typeName) final
Set vector of double values for parameter npar.
Bool_t IsResultSet() const
Bool_t GetDatime(Int_t npar, Int_t &year, Int_t &month, Int_t &day, Int_t &hour, Int_t &min, Int_t &sec) final
return field value as date & time
Bool_t StoreResult() final
Store result of statement processing.
Bool_t GetVLong64(Int_t npar, std::vector< Long64_t > &value) final
return field value as vector of 64-bit integers
Bool_t SetInt(Int_t npar, Int_t value) final
Set integer value for parameter npar.
Int_t GetNumAffectedRows() final
Return number of affected rows after statement Process() was called Make sense for queries like SELEC...
Bool_t GetDate(Int_t npar, Int_t &year, Int_t &month, Int_t &day) final
return field value as date
oracle::occi::ResultSet * fResult
std::vector< oracle::occi::MetaData > * fFieldInfo
static const char * GetFloatFormat()
return current printf format for float/double members, default "%e"
void SetError(Int_t code, const char *msg, const char *method=nullptr)
set new values for error fields if method specified, displays error message
void ClearError()
reset error fields
const char * Data() const
Definition TString.h:369
const Int_t n
Definition legend1.C:16