📄 types.h
字号:
/* This file is part of libodbc++. Copyright (C) 1999-2000 Manush Dodunekov <manush@stendahls.net> This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/#ifndef __ODBCXX_TYPES_H#define __ODBCXX_TYPES_H#include <odbc++/setup.h>#include <exception>#if !defined(ODBCXX_QT)# include <string># else# include <qstring.h>#endif#include <ctime>#if defined(ODBCXX_NO_STD_TIME_T)namespace std { using ::time_t;};#endif#if defined(ODBCXX_QT)class QIODevice;#endif#if defined(ODBCXX_HAVE_ISQL_H) && defined(ODBCXX_HAVE_ISQLEXT_H)# include <isql.h># include <isqlext.h>#elif defined(ODBCXX_HAVE_SQL_H) && defined(ODBCXX_HAVE_SQLEXT_H)# include <sql.h># include <sqlext.h>#else# error "Whoops. Can not recognize the ODBC subsystem."#endif#if defined(ODBCXX_HAVE_SQLUCODE_H)# include <sqlucode.h>#endif// fixups for current iODBC, which kindly doesn't provide SQL_TRUE and// SQL_FALSE macros#if !defined(SQL_TRUE)# define SQL_TRUE 1#endif#if !defined(SQL_FALSE)# define SQL_FALSE 0#endif// MS ODBC SDK misses this in some releases#if ODBCVER >= 0x0300 && !defined(SQL_NOT_DEFERRABLE)# define SQL_NOT_DEFERRABLE 7#endif// Setup our ODBC3_C (odbc3 conditional) macro#if ODBCVER >= 0x0300# define ODBC3_C(odbc3_value,old_value) odbc3_value#else# define ODBC3_C(odbc3_value,old_value) old_value#endif// ODBC3_DC (odbc3 dynamic conditional)// Every context using this macro should provide// a this->_getDriverInfo() method returning// a const DriverInfo*#if ODBCVER >= 0x0300# define ODBC3_DC(odbc3_value,old_value) \(this->_getDriverInfo()->getMajorVersion()>=3?odbc3_value:old_value)#else# define ODBC3_DC(odbc3_value,old_value) old_value#endif#if defined(ODBCXX_HAVE_INTTYPES_H)# include <inttypes.h>#endif#include <vector>namespace odbc { // We want Long to be at least 64 bits#if defined(WIN32) typedef __int64 Long;#elif defined(ODBCXX_HAVE_INTTYPES_H) typedef int64_t Long;#else# if ODBCXX_SIZEOF_INT == 8 typedef int Long;# elif ODBCXX_SIZEOF_LONG == 8 typedef long Long;# elif ODBCXX_SIZEOF_LONG_LONG == 8 typedef long long Long;# else# error "Can't find an appropriate at-least-64-bit integer"# endif#endif //constants: //how much we try to fetch with each SQLGetData call const int GETDATA_CHUNK_SIZE=4*1024; //how much we write with each SQLPutData call const int PUTDATA_CHUNK_SIZE=GETDATA_CHUNK_SIZE; //how much we read/write in string<->stream conversion //better names for those? const int STRING_TO_STREAM_CHUNK_SIZE=1024; const int STREAM_TO_STRING_CHUNK_SIZE=STRING_TO_STREAM_CHUNK_SIZE; /** SQL type constants */ struct Types { /** Type constants */ enum SQLType { /** An SQL BIGINT */ BIGINT = SQL_BIGINT, /** An SQL BINARY (fixed length) */ BINARY = SQL_BINARY, /** An SQL BIT */ BIT = SQL_BIT, /** An SQL CHAR (fixed length) */ CHAR = SQL_CHAR, /** An SQL DATE */ DATE = ODBC3_C(SQL_TYPE_DATE,SQL_DATE), /** An SQL DECIMAL (precision,scale) */ DECIMAL = SQL_DECIMAL, /** An SQL DOUBLE */ DOUBLE = SQL_DOUBLE, /** An SQL FLOAT */ FLOAT = SQL_FLOAT, /** An SQL INTEGER */ INTEGER = SQL_INTEGER, /** An SQL LONGVARBINARY (variable length, huge) */ LONGVARBINARY = SQL_LONGVARBINARY, /** An SQL LONGVARCHAR (variable length, huge) */ LONGVARCHAR = SQL_LONGVARCHAR, /** An SQL NUMERIC (precision,scale) */ NUMERIC = SQL_NUMERIC, /** An SQL REAL */ REAL = SQL_REAL, /** An SQL SMALLINT */ SMALLINT = SQL_SMALLINT, /** An SQL TIME */ TIME = ODBC3_C(SQL_TYPE_TIME,SQL_TIME), /** An SQL TIMESTAMP */ TIMESTAMP = ODBC3_C(SQL_TYPE_TIMESTAMP,SQL_TIMESTAMP), /** An SQL TINYINT */ TINYINT = SQL_TINYINT, /** An SQL VARBINARY (variable length less than 256) */ VARBINARY = SQL_VARBINARY, /** An SQL VARCHAR (variable length less than 256) */ VARCHAR = SQL_VARCHAR#if defined(ODBCXX_HAVE_SQLUCODE_H) , /** A wide SQL CHAR (fixed length less than 256) */ WCHAR = SQL_WCHAR, /** A wide SQL VARCHAR (variable length less than 256) */ WVARCHAR = SQL_WVARCHAR, /** A wide SQL LONGVARCHAR (variable length, huge) */ WLONGVARCHAR = SQL_WLONGVARCHAR#endif }; };#if !defined(ODBCXX_QT) /** A chunk of bytes. * * Used for setting and getting binary values. * @warning This class uses reference counting. An instance that is * referred to from different threads is likely to cause trouble. */ class ODBCXX_EXPORT Bytes { private: struct Rep { ODBCXX_SIGNED_CHAR_TYPE* buf_; size_t len_; int refCount_; Rep(const ODBCXX_SIGNED_CHAR_TYPE* b, size_t l) :len_(l), refCount_(0) { if(len_>0) { buf_=new ODBCXX_SIGNED_CHAR_TYPE[len_]; memcpy((void*)buf_,(void*)b,len_); } else { buf_=NULL; } } ~Rep() { delete [] buf_; } }; Rep* rep_; public: /** Default constructor */ Bytes() :rep_(new Rep(NULL,0)) { rep_->refCount_++; } /** Constructor */ Bytes(const ODBCXX_SIGNED_CHAR_TYPE* data, size_t dataLen) :rep_(new Rep(data,dataLen)) { rep_->refCount_++; } /** Copy constructor */ Bytes(const Bytes& b) :rep_(b.rep_) { rep_->refCount_++; } /** Assignment */ Bytes& operator=(const Bytes& b) { if(--rep_->refCount_==0) { delete rep_; } rep_=b.rep_; rep_->refCount_++; return *this; } /** Comparison */ bool operator==(const Bytes& b) const { if (getSize()!=b.getSize()) return false; for(size_t i=0;i<getSize();i++) { if(*(getData()+i)!=*(b.getData()+i)) return false; } return true; } /** Destructor */ ~Bytes() { if(--rep_->refCount_==0) { delete rep_; } } /** Returns a pointer to the data */ const ODBCXX_SIGNED_CHAR_TYPE* getData() const { return rep_->buf_; } /** Returns the size of the data */ size_t getSize() const { return rep_->len_; } };#endif /** An SQL DATE */ class ODBCXX_EXPORT Date { protected: int year_; int month_; int day_; virtual void _invalid(const ODBCXX_CHAR_TYPE* what, int value); int _validateYear(int y) { return y; } int _validateMonth(int m) { if(m<1 || m>12) { this->_invalid(ODBCXX_STRING_CONST("month"),m); } return m; } int _validateDay(int d) { if(d<1 || d>31) { this->_invalid(ODBCXX_STRING_CONST("day"),d); } return d; } public: /** Constructor. */ Date(int year, int month, int day) { this->setYear(year); this->setMonth(month); this->setDay(day); } /** Constructor. * * Sets this date to today. */ explicit Date(); /** Constructor. * * Sets this date to the specified time_t value. */ Date(std::time_t t) { this->setTime(t); } /** Constructor. * * Sets this date to the specified string in the <tt>YYYY-MM-DD</tt> format. */ Date(const ODBCXX_STRING& str) { this->parse(str); } /** Copy constructor */ Date(const Date& d) :year_(d.year_), month_(d.month_), day_(d.day_) {} /** Assignment operator */ Date& operator=(const Date& d) { year_=d.year_; month_=d.month_; day_=d.day_; return *this; } /** Destructor */ virtual ~Date() {} /** Sets this date to the specified time_t value */ virtual void setTime(std::time_t t); /** Returns the time_t value of <tt>00:00:00</tt> at this date */ std::time_t getTime() const; /** Sets this date from a string in the <tt>YYYY-MM-DD</tt> format */ void parse(const ODBCXX_STRING& str); /** Gets the year of this date */ int getYear() const { return year_; } /** Gets the month of this date */ int getMonth() const { return month_; } /** Gets the monthday of this date */ int getDay() const { return day_; } /** Sets the year of this date */ void setYear(int year) { year_=this->_validateYear(year); } /** Sets the month of this date */ void setMonth(int month) { month_=this->_validateMonth(month);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -