Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
types.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file types.h
1 /*
2  This file is part of libodbc++.
3 
4  Copyright (C) 1999-2000 Manush Dodunekov <manush@stendahls.net>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING. If not, write to
18  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  Boston, MA 02111-1307, USA.
20 */
21 
22 #ifndef __ODBCXX_TYPES_H
23 #define __ODBCXX_TYPES_H
24 
25 #include <RDBC/odbc++/setup.h>
26 
27 #include <exception>
28 #if !defined(ODBCXX_QT)
29 # include <string>
30 # else
31 # include <qstring.h>
32 #endif
33 #include <ctime>
34 
35 #if defined(ODBCXX_QT)
36 class QIODevice;
37 #endif
38 
39 #if defined(ODBCXX_HAVE_ISQL_H) && defined(ODBCXX_HAVE_ISQLEXT_H)
40 # include <isql.h>
41 # include <isqlext.h>
42 #elif defined(ODBCXX_HAVE_SQL_H) && defined(ODBCXX_HAVE_SQLEXT_H)
43 # include <sql.h>
44 # include <sqlext.h>
45 #else
46 # error "Whoops. Can not recognize the ODBC subsystem."
47 #endif
48 
49 // fixups for current iODBC, which kindly doesn't provide SQL_TRUE and
50 // SQL_FALSE macros
51 
52 #if !defined(SQL_TRUE)
53 # define SQL_TRUE 1
54 #endif
55 
56 #if !defined(SQL_FALSE)
57 # define SQL_FALSE 0
58 #endif
59 
60 // MS ODBC SDK misses this in some releases
61 #if ODBCVER >= 0x0300 && !defined(SQL_NOT_DEFERRABLE)
62 # define SQL_NOT_DEFERRABLE 7
63 #endif
64 
65 
66 // Setup our ODBC3_C (odbc3 conditional) macro
67 #if ODBCVER >= 0x0300
68 
69 # define ODBC3_C(odbc3_value,old_value) odbc3_value
70 
71 #else
72 
73 # define ODBC3_C(odbc3_value,old_value) old_value
74 
75 #endif
76 
77 
78 // ODBC3_DC (odbc3 dynamic conditional)
79 // Every context using this macro should provide
80 // a this->_getDriverInfo() method returning
81 // a const DriverInfo*
82 
83 #if ODBCVER >= 0x0300
84 
85 # define ODBC3_DC(odbc3_value,old_value) \
86 (this->_getDriverInfo()->getMajorVersion()>=3?odbc3_value:old_value)
87 
88 #else
89 
90 # define ODBC3_DC(odbc3_value,old_value) old_value
91 
92 #endif
93 
94 #if defined(ODBCXX_HAVE_INTTYPES_H)
95 # include <inttypes.h>
96 #endif
97 
98 #include <vector>
99 
100 
101 namespace odbc {
102 
103  // We want Long to be at least 64 bits
104 
105 #if defined(WIN32)
106 
107  typedef __int64 Long;
108 
109 #elif defined(ODBCXX_HAVE_INTTYPES_H)
110 
111  typedef int64_t Long;
112 
113 #else
114 
115 # if ODBCXX_SIZEOF_INT == 8
116 
117  typedef int Long;
118 
119 # elif ODBCXX_SIZEOF_LONG == 8
120 
121  typedef long Long;
122 
123 # elif ODBCXX_SIZEOF_LONG_LONG == 8
124 
125  typedef long long Long;
126 
127 # else
128 
129 # error "Can't find an appropriate at-least-64-bit integer"
130 
131 # endif
132 
133 #endif
134 
135 
136  //constants:
137  //how much we try to fetch with each SQLGetData call
138  const int GETDATA_CHUNK_SIZE=4*1024;
139  //how much we write with each SQLPutData call
141 
142  //how much we read/write in string<->stream conversion
143  //better names for those?
146 
147 
148 
149 
150 
153  struct Types {
156  enum SQLType {
158  BIGINT = SQL_BIGINT,
160  BINARY = SQL_BINARY,
162  BIT = SQL_BIT,
164  CHAR = SQL_CHAR,
166  DATE = ODBC3_C(SQL_TYPE_DATE,SQL_DATE),
168  DECIMAL = SQL_DECIMAL,
170  DOUBLE = SQL_DOUBLE,
172  FLOAT = SQL_FLOAT,
174  INTEGER = SQL_INTEGER,
176  LONGVARBINARY = SQL_LONGVARBINARY,
178  LONGVARCHAR = SQL_LONGVARCHAR,
180  NUMERIC = SQL_NUMERIC,
182  REAL = SQL_REAL,
184  SMALLINT = SQL_SMALLINT,
186  TIME = ODBC3_C(SQL_TYPE_TIME,SQL_TIME),
188  TIMESTAMP = ODBC3_C(SQL_TYPE_TIMESTAMP,SQL_TIMESTAMP),
190  TINYINT = SQL_TINYINT,
192  VARBINARY = SQL_VARBINARY,
194  VARCHAR = SQL_VARCHAR
195  };
196  };
197 
198 
199 #if !defined(ODBCXX_QT)
200 
207  private:
208  struct Rep {
209  signed char* buf_;
210  size_t len_;
212  Rep(const signed char* b, size_t l)
213  :len_(l), refCount_(0) {
214  if(len_>0) {
215  buf_=new signed char[len_];
216  memcpy((void*)buf_,(void*)b,len_);
217  } else {
218  buf_=NULL;
219  }
220  }
221  ~Rep() {
222  delete buf_;
223  }
224  };
225 
227  public:
230  :rep_(new Rep(NULL,0)) {
231  rep_->refCount_++;
232  }
233 
235  Bytes(const signed char* data, size_t dataLen)
236  :rep_(new Rep(data,dataLen)) {
237  rep_->refCount_++;
238  }
239 
241  Bytes(const Bytes& b)
242  :rep_(b.rep_) {
243  rep_->refCount_++;
244  }
245 
247  Bytes& operator=(const Bytes& b) {
248  if(--rep_->refCount_==0) {
249  delete rep_;
250  }
251  rep_=b.rep_;
252  rep_->refCount_++;
253  return *this;
254  }
255 
257  ~Bytes() {
258  if(--rep_->refCount_==0) {
259  delete rep_;
260  }
261  }
262 
264  const signed char* getData() const {
265  return rep_->buf_;
266  }
267 
269  size_t getSize() const {
270  return rep_->len_;
271  }
272  };
273 #endif
274 
277  protected:
278  int year_;
279  int month_;
280  int day_;
281 
282  virtual void _invalid(const char* what, int value);
283 
284  int _validateYear(int y) {
285  return y;
286  }
287 
288  int _validateMonth(int m) {
289  if(m<1 || m>12) {
290  this->_invalid("month",m);
291  }
292  return m;
293  }
294 
295  int _validateDay(int d) {
296  if(d<1 || d>31) {
297  this->_invalid("day",d);
298  }
299  return d;
300  }
301 
302  public:
305  Date(int year, int month, int day) {
306  this->setYear(year);
307  this->setMonth(month);
308  this->setDay(day);
309  }
310 
315  explicit Date();
316 
321  Date(time_t t) {
322  this->setTime(t);
323  }
324 
330  this->parse(str);
331  }
332 
334  Date(const Date& d)
335  :year_(d.year_),
336  month_(d.month_),
337  day_(d.day_) {}
338 
340  Date& operator=(const Date& d) {
341  year_=d.year_;
342  month_=d.month_;
343  day_=d.day_;
344  return *this;
345  }
346 
348  virtual ~Date() {}
349 
351  virtual void setTime(time_t t);
352 
354  time_t getTime() const;
355 
357  void parse(const ODBCXX_STRING& str);
358 
360  int getYear() const {
361  return year_;
362  }
363 
365  int getMonth() const {
366  return month_;
367  }
368 
370  int getDay() const {
371  return day_;
372  }
373 
375  void setYear(int year) {
376  year_=this->_validateYear(year);
377  }
378 
380  void setMonth(int month) {
381  month_=this->_validateMonth(month);
382  }
383 
385  void setDay(int day) {
386  day_=this->_validateDay(day);
387  }
388 
390  virtual ODBCXX_STRING toString() const;
391  };
392 
395  protected:
396  int hour_;
397  int minute_;
398  int second_;
399 
400  virtual void _invalid(const char* what, int value);
401 
402  int _validateHour(int h) {
403  if(h<0 || h>23) {
404  this->_invalid("hour",h);
405  }
406  return h;
407  }
408 
409  int _validateMinute(int m) {
410  if(m<0 || m>59) {
411  this->_invalid("minute",m);
412  }
413  return m;
414  }
415 
416  int _validateSecond(int s) {
417  if(s<0 || s>61) {
418  this->_invalid("second",s);
419  }
420  return s;
421  }
422 
423  public:
425  Time(int hour, int minute, int second) {
426  this->setHour(hour);
427  this->setMinute(minute);
428  this->setSecond(second);
429  }
430 
435  explicit Time();
436 
441  Time(time_t t) {
442  this->setTime(t);
443  }
444 
450  this->parse(str);
451  }
452 
454  Time(const Time& t)
455  :hour_(t.hour_),
456  minute_(t.minute_),
457  second_(t.second_) {}
458 
460  Time& operator=(const Time& t) {
461  hour_=t.hour_;
462  minute_=t.minute_;
463  second_=t.second_;
464  return *this;
465  }
466 
468  virtual ~Time() {}
469 
471  virtual void setTime(time_t t);
472 
474  time_t getTime() const;
475 
477  void parse(const ODBCXX_STRING& str);
478 
480  int getHour() const {
481  return hour_;
482  }
483 
485  int getMinute() const {
486  return minute_;
487  }
488 
490  int getSecond() const {
491  return second_;
492  }
493 
495  void setHour(int h) {
496  hour_=this->_validateHour(h);
497  }
498 
500  void setMinute(int m) {
501  minute_=this->_validateMinute(m);
502  }
503 
505  void setSecond(int s) {
506  second_=this->_validateSecond(s);
507  }
508 
509  virtual ODBCXX_STRING toString() const;
510  };
511 
512 
515  class ODBCXX_EXPORT Timestamp : public Date, public Time {
516  private:
517  int nanos_;
518 
519  virtual void _invalid(const char* what, int value);
520 
521  int _validateNanos(int n) {
522  if(n<0) {
523  this->_invalid("nanoseconds",n);
524  }
525  return n;
526  }
527 
528  public:
530  Timestamp(int year, int month, int day,
531  int hour, int minute, int second,
532  int nanos =0)
533  :Date(year,month,day), Time(hour,minute,second) {
534  this->setNanos(nanos);
535  }
536 
541  explicit Timestamp();
542 
547  Timestamp(time_t t) {
548  this->setTime(t);
549  }
550 
556  this->parse(s);
557  }
558 
559 
562  :Date(t),Time(t),nanos_(t.nanos_) {}
563 
566  Date::operator=(t);
567  Time::operator=(t);
568  nanos_=t.nanos_;
569  return *this;
570  }
571 
573  virtual ~Timestamp() {}
574 
576  virtual void setTime(time_t t);
577 
579  virtual time_t getTime() {
580  return Date::getTime()+Time::getTime();
581  }
582 
585  void parse(const ODBCXX_STRING& s);
586 
588  int getNanos() const {
589  return nanos_;
590  }
591 
593  void setNanos(int nanos) {
594  nanos_=this->_validateNanos(nanos);
595  }
596 
597  virtual ODBCXX_STRING toString() const;
598  };
599 
600 
601  //this is used for several 'lists of stuff' below
602  //expects T to be a pointer-to-something, and
603  //the contents will get deleted when the vector
604  //itself is deleted
605  template <class T> class CleanVector : public std::vector<T> {
606  private:
607  CleanVector(const CleanVector<T>&); //forbid
608  CleanVector<T>& operator=(const CleanVector<T>&); //forbid
609 
610  public:
611  explicit CleanVector() {}
612  virtual ~CleanVector() {
613  typename std::vector<T>::iterator i=this->begin();
614  typename std::vector<T>::iterator end=this->end();
615  while(i!=end) {
616  delete *i;
617  ++i;
618  }
619  this->clear();
620  }
621  };
622 
623 
627  friend class ErrorHandler;
628 
629  private:
630  char state_[SQL_SQLSTATE_SIZE+1];
631  char description_[SQL_MAX_MESSAGE_LENGTH];
632  SQLINTEGER nativeCode_;
633 
635 
636  public:
637  virtual ~DriverMessage() {}
638 
639  const char* getSQLState() const {
640  return state_;
641  }
642 
643  const char* getDescription() const {
644  return description_;
645  }
646 
647  int getNativeCode() const {
648  return nativeCode_;
649  }
650  };
651 
652 
655  class SQLException : public std::exception {
656  private:
660 #if defined(ODBCXX_QT)
661  QCString reason8_;
662 #endif
663 
664  public:
666  SQLException(const ODBCXX_STRING& reason ="",
667  const ODBCXX_STRING& sqlState ="",
668  int vendorCode =0)
669  :reason_(reason),
670  sqlState_(sqlState),
671  errorCode_(vendorCode)
672 #if defined(ODBCXX_QT)
673  ,reason8_(reason.local8Bit())
674 #endif
675 {}
676 
679  :reason_(dm.getDescription()),
680  sqlState_(dm.getSQLState()),
681  errorCode_(dm.getNativeCode()) {}
682 
684  virtual ~SQLException() throw() {}
685 
687  int getErrorCode() const {
688  return errorCode_;
689  }
690 
695  const ODBCXX_STRING& getSQLState() const {
696  return sqlState_;
697  }
698 
700  const ODBCXX_STRING& getMessage() const {
701  return reason_;
702  }
703 
704 
706  virtual const char* what() const throw() {
707  // the conversion from QString involves a temporary, which
708  // doesn't survive this scope. So here, we do a conditional
709 #if defined(ODBCXX_QT)
710  return reason8_.data();
711 #else
712  return reason_.c_str();
713 #endif
714  }
715  };
716 
717 
722  class SQLWarning : public SQLException {
723 
724  SQLWarning(const SQLWarning&); //forbid
725  SQLWarning& operator=(const SQLWarning&); //forbid
726 
727  public:
729  SQLWarning(const ODBCXX_STRING& reason ="",
730  const ODBCXX_STRING& sqlState ="",
731  int vendorCode =0)
732  :SQLException(reason,sqlState,vendorCode) {}
733 
736  :SQLException(dm) {}
737 
739  virtual ~SQLWarning() throw() {}
740  };
741 
743 
744 
745  template <class T> class Deleter {
746  private:
747  T* ptr_;
748  bool isArray_;
749 
750  Deleter(const Deleter<T>&);
752 
753  public:
754  explicit Deleter(T* ptr, bool isArray =false)
755  :ptr_(ptr), isArray_(isArray) {}
757  if(!isArray_) {
758  delete ptr_;
759  } else {
760  delete[] ptr_;
761  }
762  }
763  };
764 
765 }; // namespace odbc
766 
767 
768 #endif // __ODBCXX_TYPES_H