include/DataType.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by www.databasecache.com                           *
00003  *   Contact: praba_tuty@databasecache.com                                 *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015   ***************************************************************************/
00016 #ifndef DATATYPE_H
00017 #define DATATYPE_H
00018 #include<sqlext.h>
00019 #include<sqltypes.h>
00020 //#include<os.h>
00021 typedef int JulianRep;
00022 
00023 
00030 enum DataType {
00031    typeInt         =  0, 
00032    typeLong        =  1,
00033    typeLongLong    =  2,
00034    typeShort       =  3,
00035    typeByteInt     =  4,
00036 
00037    typeDouble      =  10,
00038    typeFloat       =  11,
00039    typeDecimal     =  12,
00040 
00041    typeDate        =  20,
00042    typeTime        =  21,
00043    typeTimeStamp   =  22,
00044 
00045    typeString      =  30,
00046    typeBinary      =  31,
00047 
00048    typeULong       =  99,
00049    typeUnknown     =  100
00050 };
00051 
00058 enum ComparisionOp {
00059     OpEquals = 0,
00060     OpNotEquals,
00061     OpLessThan,
00062     OpLessThanEquals,
00063     OpGreaterThan,
00064     OpGreaterThanEquals,
00065     OpInvalidComparisionOp
00066 };
00067 
00074 enum LogicalOp {
00075     OpAnd = 0,
00076     OpOr,
00077     OpNot,
00078     OpInvalidLogicalOp
00079 };
00080 
00081 
00082 class AllDataType
00083 {
00084     public:
00085     static long size(DataType type, int length =0);
00086     static char* getSQLString(DataType type);
00087     static SQLSMALLINT convertToSQLType(DataType type);
00088     static SQLSMALLINT convertToSQL_C_Type(DataType type);
00089     static DataType convertFromSQLType(SQLSMALLINT type);
00090 
00091     static void copyVal(void* dest, void *src, DataType type, int length = 0);
00092 
00093     static bool compareVal(void *src1, void *src2, ComparisionOp op,
00094                            DataType type, long length = 0);
00095     static bool compareIntVal(void* src1, void* src2, ComparisionOp op);
00096     static bool compareLongVal(void* src1, void* src2, ComparisionOp op);
00097     static bool compareLongLongVal(void* src1, void* src2, ComparisionOp op);
00098     static bool compareShortVal(void* src1, void* src2, ComparisionOp op);
00099     static bool compareByteIntVal(void* src1, void* src2, ComparisionOp op);
00100     static bool compareDoubleVal(void* src1, void* src2, ComparisionOp op);
00101     static bool compareFloatVal(void* src1, void* src2, ComparisionOp op);
00102     static bool compareDateVal(void* src1, void* src2, ComparisionOp op);
00103     static bool compareTimeVal(void* src1, void* src2, ComparisionOp op);
00104     static bool compareTimeStampVal(void* src1, void* src2, ComparisionOp op);
00105     static bool compareStringVal(void* src1, void* src2, ComparisionOp op);
00106     static bool compareBinaryVal(void* src1, void* src2,
00107                                  ComparisionOp op, int length);
00108 
00109 
00110     static void convert(DataType srcType, void *src, DataType destType, void *dest);
00111     static void convertToInt(void* dest, void* src, DataType srcType);
00112     static void convertToLong(void* dest, void* src, DataType srcType);
00113     static void convertToLongLong(void* dest, void* src, DataType srcType);
00114     static void convertToShort(void* dest, void* src, DataType srcType);
00115     static void convertToByteInt(void* dest, void* src, DataType srcType);
00116     static void convertToFloat(void* dest, void* src, DataType srcType);
00117     static void convertToDouble(void* dest, void* src, DataType srcType);
00118     static void convertToString(void* dest, void* src, DataType srcType);
00119     static void convertToDate(void* dest, void* src, DataType srcType);
00120     static void convertToTime(void* dest, void* src, DataType srcType);
00121     static void convertToTimeStamp(void* dest, void* src, DataType srcType);
00122 
00123 
00124     static ComparisionOp getComparisionOperator(char *str);
00125 
00126     static void* alloc(DataType type, int length =0);
00127     static void strToValue(void *dest, char *src, DataType type, int length=0);
00128     static int printVal(void *src, DataType type, int length);
00129 
00130 
00131 };
00132 
00133 
00140 class ByteInt {
00141 
00142     public:
00143     ByteInt() { }
00144 
00147     ByteInt(const ByteInt &v) { val = v.val; }
00151     ByteInt(char v) { val = v; }
00152     operator int() const { return (int) val; }
00153     char operator=(ByteInt v)   { return val = v.val; }
00154     char operator=(char v)          { return val = v; }
00155     char operator+=(ByteInt v)  { return val += v.val; }
00156     char operator+=(char v)         { return val += v; }
00157     char operator-=(ByteInt v)  { return val -= v.val; }
00158     char operator-=(char v)         { return val -= v; }
00159     char operator*=(ByteInt v)  { return val *= v.val; }
00160     char operator*=(char v)         { return val *= v; }
00161     char operator/=(ByteInt v)  { return val /= v.val; }
00162     char operator/=(char v)         { return val /= v; }
00163     char operator%=(ByteInt v)  { return val %= v.val; }
00164     char operator%=(char v)         { return val %= v; }
00165     char operator<<=(ByteInt v) { return val <<= v.val; }
00166     char operator<<=(char v)        { return val <<= v; }
00167     char operator>>=(ByteInt v) { return val >>= v.val; }
00168     char operator>>=(char v)        { return val >>= v; }
00169     char operator&=(ByteInt v)  { return val &= v.val; }
00170     char operator&=(char v)         { return val &= v; }
00171     char operator|=(ByteInt v)  { return val |= v.val; }
00172     char operator|=(char v)         { return val |= v; }
00173     char operator^=(ByteInt v)  { return val ^= v.val; }
00174     char operator^=(char v)         { return val ^= v; }
00175     char operator++()               { return val++; }
00176     char operator++(int)               { char tmp = val; val++; return val; }
00177     char operator--()               { return val--; }
00178     char operator--(int)               { char tmp = val; val--; return val; }
00179 
00180     private:
00181     signed char val;
00182 };
00183 
00184 
00191 class Date {  // The class a user would declare to hold date
00192 
00193     public:
00194     Date() {julianDate = 0;}
00195     Date(JulianRep julian) : julianDate(julian) {}
00196 
00202     Date(int year, int month, int day);
00203 
00204     Date(const Date &d2) { julianDate = d2.julianDate; }
00205     Date& operator=(const Date& d2)
00206         { julianDate=d2.julianDate; return *this; }
00207 
00213     int set(int year, int month, int day);
00214     int set(const struct tm *tmptr);
00215 
00221     int get(int &year, int &month, int &day) const;
00222 
00225     bool isValid() const;
00226 
00229     void setNull() { julianDate = 0;}
00230 
00233     int dayOfWeek() const;
00234 
00238     const char *dayOfWeekName() const;
00239 
00243     const char *dayOfWeekAbbr() const;
00244 
00248     static const char *dayOfWeekName(int day); // 0--> Sunday
00249 
00253     static const char *dayOfWeekAbbr(int day);
00254 
00255     static int dayOfWeek(JulianRep juldate);
00256 
00257 
00260     int dayOfMonth() const;
00261 
00262     int dayOfYear() const;
00263 
00266     int month() const;
00267 
00272     const char *monthName() const;
00273 
00278     const char *monthAbbr() const;
00279 
00284     static const char *monthName(int month);
00285 
00286 
00291     static const char *monthAbbr(int month);
00292 
00296     int parseFrom(const char *s);
00297 
00298     Date &operator++() { julianDate++; return *this; }
00299     Date &operator--() { julianDate--; return *this; }
00300 
00301     Date &operator+=(int days) { julianDate += days; return *this;}
00302     Date &operator-=(int days) { julianDate -= days; return *this;}
00303 
00306     int year() const;
00307 
00310     static bool isValidDate(int year, int month, int day);
00311 
00312     friend Date  operator+(const Date &d1, int days);
00313     friend Date  operator+(int days, const Date &d1);
00314     friend Date  operator-(const Date &d1, int days);
00315     friend int  operator-(const Date &d1, const Date & d2);
00316     friend int  operator<(const Date &d1 ,const Date &d2);
00317     friend int  operator>(const Date &d1 ,const Date &d2);
00318     friend int  operator<=(const Date &d1 ,const Date &d2);
00319     friend int  operator>=(const Date &d1 ,const Date &d2);
00320     friend int  operator==(const Date &d1 ,const Date &d2);
00321     friend int  operator!=(const Date &d1 ,const Date &d2);
00322 
00325     static bool isLeapYear(int year);
00326 
00329     static int daysInMonth(int month, int year);
00330 
00331     static int YMDToJulian(int year,int mon,int day, JulianRep &julian);
00332     static int julianToYMD(JulianRep julian,int &year,int &month,int &day);
00333 
00334     private:
00335     JulianRep julianDate;
00336 
00337 
00338 
00339 };
00340 
00347 class Time {  // The class a user would declare to hold time
00348     public:
00349     Time() {timeVal = 0;}
00350 
00357     Time(int hours, int mins, int secs, int usec = 0);
00358     Time(int totalSecs) : timeVal(totalSecs) {;}
00359     Time(const Time &d2) { timeVal = d2.timeVal; }
00360     Time& operator=(const Time& d2) { timeVal=d2.timeVal; return *this; }
00361 
00368     int set(int hours, int mins, int secs, int usec = 0);
00369 
00375     int get(int &hours, int &mins, int &secs) const;
00376 
00379     bool isValid() const;
00380 
00383     void setNull() { timeVal = -1;}
00384 
00385     int secondsSinceMidnight() const { return timeVal/10000;}
00386 
00389     int usec() const; // to nearest 100 of usec.
00390 
00393     int msec() const;
00394 
00397     int seconds() const;
00398 
00401     int minutes() const;
00402 
00405     int hours() const;
00406 
00407 
00410     int setMsec(int ms);
00411 
00414     int setUsec(int us);
00415 
00419     int parseFrom(const char *s);
00420 
00421     Time &operator++() { timeVal += 10000; return *this; }
00422     Time &operator--() { timeVal -= 10000; return *this; }
00423 
00424     Time &operator+=(int seconds) { timeVal += seconds*10000; return *this; }
00425     Time &operator-=(int seconds) { timeVal -= seconds*10000; return *this; }
00426 
00427 
00430     static bool isValidTime(int hours, int mins, int secs);
00431 
00432     friend Time operator+(const Time &t1, int seconds);
00433     friend Time operator+(int seconds, const Time &t1);
00434     friend Time operator-(const Time &t1, int seconds);
00435     friend int  operator-(const Time &t1, const Time& t2);
00436     friend int  operator<(const Time &t1 ,const Time &t2 );
00437     friend int  operator>(const Time &t1 ,const Time &t2 );
00438     friend int  operator<=(const Time &t1 ,const Time &t2 );
00439     friend int  operator>=(const Time &t1 ,const Time &t2 );
00440     friend int  operator==(const Time &t1 ,const Time &t2 );
00441     friend int  operator!=(const Time &t1 ,const Time &t2 );
00442 
00443 
00444     private:
00445     int timeVal;
00446 };
00447 
00454 class TimeStamp {
00455 
00456     public:
00457     TimeStamp() {}
00458 
00468     TimeStamp(int year, int month, int day, int hour, int minute, int sec, int usec = 0) :
00469         date(year, month, day), time(hour, minute, sec, usec) { }
00470 
00471     TimeStamp(const TimeStamp &ts)
00472     { date = ts.date; time = ts.time; }
00473     TimeStamp(const Date &d, Time &t) : date(d), time(t) {}
00474 
00475 
00476     TimeStamp& operator=(const TimeStamp& d2)
00477         { date=d2.date; time = d2.time; return *this; }
00478 
00484     int getDate(int &year, int &month, int &day)
00485          { return date.get(year, month, day); }
00486 
00490    void getDate(Date &newDate) const
00491          { newDate = date; }
00492 
00498     int setDate(int year, int month, int day)
00499          { return date.set(year, month, day); }
00500 
00504     void setDate(const Date &newDate)
00505          { date = newDate; }
00506 
00507 
00508     operator Date() { return date; }
00509     operator Time() { return time; }
00510 
00511 
00512 
00518     int getTime(int &hours, int &mins, int &secs) const
00519          { return time.get(hours, mins, secs); }
00523     void getTime(Time &newTime) const
00524          { newTime = time; }
00525 
00532     int setTime(int hours, int mins, int secs, int usec = 0)
00533          { return time.set(hours, mins, secs, usec); }
00534 
00538     void setTime(const Time &newTime)
00539          { time = newTime; }
00540 
00543     bool isValid() const { return date.isValid() && time.isValid(); }
00544 
00546     void setNull() { date.setNull(); time.setNull(); }
00547 
00550     int dayOfWeek() const { return date.dayOfWeek(); }
00551 
00556     const char *dayOfWeekName() const { return date.dayOfWeekName(); }
00557 
00561     const char *dayOfWeekAbbr() const { return date.dayOfWeekAbbr(); }
00562 
00565     int dayOfMonth() const { return date.dayOfMonth(); }
00566     int dayOfYear() const { return date.dayOfYear(); }
00567 
00571     int month() const { return date.month(); }
00572 
00577     const char *monthName() const { return date.monthName(); }
00578 
00583     const char *monthAbbr() const { return date.monthAbbr(); }
00584 
00587     int year() const { return date.year(); }
00588 
00589     int secondsSinceMidnight() const { return time.secondsSinceMidnight(); }
00591     int seconds() const { return time.seconds(); }
00593     int minutes() const { return time.minutes(); }
00595     int hours() const { return time.hours(); }
00597     int msec() const { return time.msec(); }
00599     int usec() const { return time.usec(); }
00600 
00602     int setMsec(int ms) { return time.setMsec(ms) ; }
00604     int setUsec(int us) { return time.setUsec(us) ; }
00605 
00609     int parseDateFrom(const char *s) { return date.parseFrom(s); }
00610 
00615     int parseTimeFrom(const char *s) { return time.parseFrom(s); }
00616 
00617     int parseFrom(const char *s);
00618     friend int  operator<(const TimeStamp &d1, const TimeStamp &d2);
00619     friend int  operator>(const TimeStamp &d1, const TimeStamp &d2);
00620     friend int  operator<=(const TimeStamp &d1, const TimeStamp &d2);
00621     friend int  operator>=(const TimeStamp &d1, const TimeStamp &d2);
00622     friend int  operator==(const TimeStamp &d1, const TimeStamp &d2);
00623     friend int  operator!=(const TimeStamp &d1, const TimeStamp &d2);
00624 
00625 
00626     private:
00627     Date date;
00628     Time time;
00629 };
00630 
00631 
00632 #endif
00633 
00634 

Generated on Mon Jun 9 22:37:14 2008 for csql by  doxygen 1.4.7