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