include/DataType.h

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     static void addVal(void* dest, void *src, DataType type);
00093     static void divVal(void* dest, int src, DataType type);
00094 
00095     static bool compareVal(void *src1, void *src2, ComparisionOp op,
00096                            DataType type, long length = 0);
00097     static bool compareIntVal(void* src1, void* src2, ComparisionOp op);
00098     static bool compareLongVal(void* src1, void* src2, ComparisionOp op);
00099     static bool compareLongLongVal(void* src1, void* src2, ComparisionOp op);
00100     static bool compareShortVal(void* src1, void* src2, ComparisionOp op);
00101     static bool compareByteIntVal(void* src1, void* src2, ComparisionOp op);
00102     static bool compareDoubleVal(void* src1, void* src2, ComparisionOp op);
00103     static bool compareFloatVal(void* src1, void* src2, ComparisionOp op);
00104     static bool compareDateVal(void* src1, void* src2, ComparisionOp op);
00105     static bool compareTimeVal(void* src1, void* src2, ComparisionOp op);
00106     static bool compareTimeStampVal(void* src1, void* src2, ComparisionOp op);
00107     static bool compareStringVal(void* src1, void* src2, ComparisionOp op);
00108     static bool compareBinaryVal(void* src1, void* src2,
00109                                  ComparisionOp op, int length);
00110 
00111 
00112     static void convert(DataType srcType, void *src, DataType destType, void *dest);
00113     static void convertToInt(void* dest, void* src, DataType srcType);
00114     static void convertToLong(void* dest, void* src, DataType srcType);
00115     static void convertToLongLong(void* dest, void* src, DataType srcType);
00116     static void convertToShort(void* dest, void* src, DataType srcType);
00117     static void convertToByteInt(void* dest, void* src, DataType srcType);
00118     static void convertToFloat(void* dest, void* src, DataType srcType);
00119     static void convertToDouble(void* dest, void* src, DataType srcType);
00120     static void convertToString(void* dest, void* src, DataType srcType);
00121     static void convertToDate(void* dest, void* src, DataType srcType);
00122     static void convertToTime(void* dest, void* src, DataType srcType);
00123     static void convertToTimeStamp(void* dest, void* src, DataType srcType);
00124 
00125 
00126     static ComparisionOp getComparisionOperator(char *str);
00127 
00128     static void* alloc(DataType type, int length =0);
00129     static void strToValue(void *dest, char *src, DataType type, int length=0);
00130     static int printVal(void *src, DataType type, int length);
00131 
00132 
00133 };
00134 
00135 
00142 class ByteInt {
00143 
00144     public:
00145     ByteInt() { }
00146 
00149     ByteInt(const ByteInt &v) { val = v.val; }
00153     ByteInt(char v) { val = v; }
00154     operator int() const { return (int) val; }
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^=(ByteInt v)  { return val ^= v.val; }
00176     char operator^=(char v)         { return val ^= v; }
00177     char operator++()               { return val++; }
00178     char operator++(int)               { char tmp = val; val++; return val; }
00179     char operator--()               { return val--; }
00180     char operator--(int)               { char tmp = val; val--; return val; }
00181 
00182     private:
00183     signed char val;
00184 };
00185 
00186 
00193 class Date {  // The class a user would declare to hold date
00194 
00195     public:
00196     Date() {julianDate = 0;}
00197     Date(JulianRep julian) : julianDate(julian) {}
00198 
00204     Date(int year, int month, int day);
00205 
00206     Date(const Date &d2) { julianDate = d2.julianDate; }
00207     Date& operator=(const Date& d2)
00208         { julianDate=d2.julianDate; return *this; }
00209 
00215     int set(int year, int month, int day);
00216     int set(const struct tm *tmptr);
00217 
00223     int get(int &year, int &month, int &day) const;
00224 
00227     bool isValid() const;
00228 
00231     void setNull() { julianDate = 0;}
00232 
00235     int dayOfWeek() const;
00236 
00240     const char *dayOfWeekName() const;
00241 
00245     const char *dayOfWeekAbbr() const;
00246 
00250     static const char *dayOfWeekName(int day); // 0--> Sunday
00251 
00255     static const char *dayOfWeekAbbr(int day);
00256 
00257     static int dayOfWeek(JulianRep juldate);
00258 
00259 
00262     int dayOfMonth() const;
00263 
00264     int dayOfYear() const;
00265 
00268     int month() const;
00269 
00274     const char *monthName() const;
00275 
00280     const char *monthAbbr() const;
00281 
00286     static const char *monthName(int month);
00287 
00288 
00293     static const char *monthAbbr(int month);
00294 
00298     int parseFrom(const char *s);
00299 
00300     Date &operator++() { julianDate++; return *this; }
00301     Date &operator--() { julianDate--; return *this; }
00302 
00303     Date &operator+=(int days) { julianDate += days; return *this;}
00304     Date &operator-=(int days) { julianDate -= days; return *this;}
00305 
00308     int year() const;
00309 
00312     static bool isValidDate(int year, int month, int day);
00313 
00314     friend Date  operator+(const Date &d1, int days);
00315     friend Date  operator+(int days, const Date &d1);
00316     friend Date  operator-(const Date &d1, int days);
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     friend int  operator==(const Date &d1 ,const Date &d2);
00323     friend int  operator!=(const Date &d1 ,const Date &d2);
00324 
00327     static bool isLeapYear(int year);
00328 
00331     static int daysInMonth(int month, int year);
00332 
00333     static int YMDToJulian(int year,int mon,int day, JulianRep &julian);
00334     static int julianToYMD(JulianRep julian,int &year,int &month,int &day);
00335 
00336     private:
00337     JulianRep julianDate;
00338 
00339 
00340 
00341 };
00342 
00349 class Time {  // The class a user would declare to hold time
00350     public:
00351     Time() {timeVal = 0;}
00352 
00359     Time(int hours, int mins, int secs, int usec = 0);
00360     Time(int totalSecs) : timeVal(totalSecs) {;}
00361     Time(const Time &d2) { timeVal = d2.timeVal; }
00362     Time& operator=(const Time& d2) { timeVal=d2.timeVal; return *this; }
00363 
00370     int set(int hours, int mins, int secs, int usec = 0);
00371 
00377     int get(int &hours, int &mins, int &secs) const;
00378 
00381     bool isValid() const;
00382 
00385     void setNull() { timeVal = -1;}
00386 
00387     int secondsSinceMidnight() const { return timeVal/10000;}
00388 
00391     int usec() const; // to nearest 100 of usec.
00392 
00395     int msec() const;
00396 
00399     int seconds() const;
00400 
00403     int minutes() const;
00404 
00407     int hours() const;
00408 
00409 
00412     int setMsec(int ms);
00413 
00416     int setUsec(int us);
00417 
00421     int parseFrom(const char *s);
00422 
00423     Time &operator++() { timeVal += 10000; return *this; }
00424     Time &operator--() { timeVal -= 10000; return *this; }
00425 
00426     Time &operator+=(int seconds) { timeVal += seconds*10000; return *this; }
00427     Time &operator-=(int seconds) { timeVal -= seconds*10000; return *this; }
00428 
00429 
00432     static bool isValidTime(int hours, int mins, int secs);
00433 
00434     friend Time operator+(const Time &t1, int seconds);
00435     friend Time operator+(int seconds, const Time &t1);
00436     friend Time operator-(const Time &t1, int seconds);
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     friend int  operator==(const Time &t1 ,const Time &t2 );
00443     friend int  operator!=(const Time &t1 ,const Time &t2 );
00444 
00445 
00446     private:
00447     int timeVal;
00448 };
00449 
00456 class TimeStamp {
00457 
00458     public:
00459     TimeStamp() {}
00460 
00470     TimeStamp(int year, int month, int day, int hour, int minute, int sec, int usec = 0) :
00471         date(year, month, day), time(hour, minute, sec, usec) { }
00472 
00473     TimeStamp(const TimeStamp &ts)
00474     { date = ts.date; time = ts.time; }
00475     TimeStamp(const Date &d, Time &t) : date(d), time(t) {}
00476 
00477 
00478     TimeStamp& operator=(const TimeStamp& d2)
00479         { date=d2.date; time = d2.time; return *this; }
00480 
00486     int getDate(int &year, int &month, int &day)
00487          { return date.get(year, month, day); }
00488 
00492    void getDate(Date &newDate) const
00493          { newDate = date; }
00494 
00500     int setDate(int year, int month, int day)
00501          { return date.set(year, month, day); }
00502 
00506     void setDate(const Date &newDate)
00507          { date = newDate; }
00508 
00509 
00510     operator Date() { return date; }
00511     operator Time() { return time; }
00512 
00513 
00514 
00520     int getTime(int &hours, int &mins, int &secs) const
00521          { return time.get(hours, mins, secs); }
00525     void getTime(Time &newTime) const
00526          { newTime = time; }
00527 
00534     int setTime(int hours, int mins, int secs, int usec = 0)
00535          { return time.set(hours, mins, secs, usec); }
00536 
00540     void setTime(const Time &newTime)
00541          { time = newTime; }
00542 
00545     bool isValid() const { return date.isValid() && time.isValid(); }
00546 
00548     void setNull() { date.setNull(); time.setNull(); }
00549 
00552     int dayOfWeek() const { return date.dayOfWeek(); }
00553 
00558     const char *dayOfWeekName() const { return date.dayOfWeekName(); }
00559 
00563     const char *dayOfWeekAbbr() const { return date.dayOfWeekAbbr(); }
00564 
00567     int dayOfMonth() const { return date.dayOfMonth(); }
00568     int dayOfYear() const { return date.dayOfYear(); }
00569 
00573     int month() const { return date.month(); }
00574 
00579     const char *monthName() const { return date.monthName(); }
00580 
00585     const char *monthAbbr() const { return date.monthAbbr(); }
00586 
00589     int year() const { return date.year(); }
00590 
00591     int secondsSinceMidnight() const { return time.secondsSinceMidnight(); }
00593     int seconds() const { return time.seconds(); }
00595     int minutes() const { return time.minutes(); }
00597     int hours() const { return time.hours(); }
00599     int msec() const { return time.msec(); }
00601     int usec() const { return time.usec(); }
00602 
00604     int setMsec(int ms) { return time.setMsec(ms) ; }
00606     int setUsec(int us) { return time.setUsec(us) ; }
00607 
00611     int parseDateFrom(const char *s) { return date.parseFrom(s); }
00612 
00617     int parseTimeFrom(const char *s) { return time.parseFrom(s); }
00618 
00619     int parseFrom(const char *s);
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     friend int  operator==(const TimeStamp &d1, const TimeStamp &d2);
00625     friend int  operator!=(const TimeStamp &d1, const TimeStamp &d2);
00626 
00627 
00628     private:
00629     Date date;
00630     Time time;
00631 };
00632 
00633 
00634 #endif
00635 
00636 

CSQL Project Page