Initial revision
[csql.git] / include / DataType.h
blob89a0e16694b3e84f7610a3d96b3833c27185813a
1 /***************************************************************************
2 * Copyright (C) 2007 by Prabakaran Thirumalai *
3 * praba_tuty@yahoo.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 ***************************************************************************/
16 #ifndef DATATYPE_H
17 #define DATATYPE_H
18 //USER EXPOSED
20 //#include<os.h>
21 typedef int JulianRep;
23 enum DataType {
24 typeInt = 0,
25 typeLong = 1,
26 typeLongLong = 2,
27 typeShort = 3,
28 typeByteInt = 4,
30 typeDouble = 10,
31 typeFloat = 11,
32 typeDecimal = 12,
34 typeDate = 20,
35 typeTime = 21,
36 typeTimeStamp = 22,
38 typeString = 30,
39 typeBinary = 31,
41 typeULong = 99,
42 typeUnknown = 100
45 enum ComparisionOp {
46 OpEquals = 0,
47 OpNotEquals,
48 OpLessThan,
49 OpLessThanEquals,
50 OpGreaterThan,
51 OpGreaterThanEquals
53 enum LogicalOp {
54 OpAnd = 0,
55 OpOr,
56 OpNot
58 class AllDataType
60 public:
61 static long size(DataType type);
62 static void copyVal(void* dest, void *src, DataType type);
64 static bool compareVal(void *src1, void *src2, ComparisionOp op,
65 DataType type, long length = 0);
66 static bool compareIntVal(void* src1, void* src2, ComparisionOp op);
67 static bool compareLongVal(void* src1, void* src2, ComparisionOp op);
68 static bool compareLongLongVal(void* src1, void* src2, ComparisionOp op);
69 static bool compareShortVal(void* src1, void* src2, ComparisionOp op);
70 static bool compareByteIntVal(void* src1, void* src2, ComparisionOp op);
71 static bool compareDoubleVal(void* src1, void* src2, ComparisionOp op);
72 static bool compareFloatVal(void* src1, void* src2, ComparisionOp op);
73 static bool compareDateVal(void* src1, void* src2, ComparisionOp op);
74 static bool compareTimeVal(void* src1, void* src2, ComparisionOp op);
75 static bool compareTimeStampVal(void* src1, void* src2, ComparisionOp op);
76 static bool compareStringVal(void* src1, void* src2, ComparisionOp op);
77 static bool compareBinaryVal(void* src1, void* src2,
78 ComparisionOp op, int length);
84 // 8 bit integer
85 class ByteInt {
87 public:
88 ByteInt() { }
89 ByteInt(const ByteInt &v) { val = v.val; }
90 ByteInt(char v) { val = v; }
91 operator int() const { return (int) val; }
92 char operator=(ByteInt v) { return val = v.val; }
93 char operator=(char v) { return val = v; }
94 char operator+=(ByteInt v) { return val += v.val; }
95 char operator+=(char v) { return val += v; }
96 char operator-=(ByteInt v) { return val -= v.val; }
97 char operator-=(char v) { return val -= v; }
98 char operator*=(ByteInt v) { return val *= v.val; }
99 char operator*=(char v) { return val *= v; }
100 char operator/=(ByteInt v) { return val /= v.val; }
101 char operator/=(char v) { return val /= v; }
102 char operator%=(ByteInt v) { return val %= v.val; }
103 char operator%=(char v) { return val %= v; }
104 char operator<<=(ByteInt v) { return val <<= v.val; }
105 char operator<<=(char v) { return val <<= v; }
106 char operator>>=(ByteInt v) { return val >>= v.val; }
107 char operator>>=(char v) { return val >>= v; }
108 char operator&=(ByteInt v) { return val &= v.val; }
109 char operator&=(char v) { return val &= v; }
110 char operator|=(ByteInt v) { return val |= v.val; }
111 char operator|=(char v) { return val |= v; }
112 char operator^=(ByteInt v) { return val ^= v.val; }
113 char operator^=(char v) { return val ^= v; }
114 char operator++() { return val++; }
115 char operator--() { return val--; }
117 private:
118 signed char val;
123 class Date { // The class a user would declare to hold date
125 public:
126 Date() {julianDate = 0;}
127 Date(JulianRep julian) : julianDate(julian) {}
128 Date(int year, int month, int day);
129 Date(const Date &d2) { julianDate = d2.julianDate; }
130 Date& operator=(const Date& d2)
131 { julianDate=d2.julianDate; return *this; }
133 int set(int year, int month, int day);
134 int set(const struct tm *tmptr);
135 int get(int &year, int &month, int &day) const;
137 bool isValid() const;
138 void setNull() { julianDate = 0;}
140 int dayOfWeek() const;
141 const char *dayOfWeekName() const;
142 const char *dayOfWeekAbbr() const;
143 static const char *dayOfWeekName(int day); // 0--> Sunday
144 static const char *dayOfWeekAbbr(int day);
145 static int dayOfWeek(JulianRep juldate);
147 int dayOfMonth() const;
148 int dayOfYear() const;
150 int month() const;
151 const char *monthName() const;
152 const char *monthAbbr() const;
153 static const char *monthName(int month);
154 static const char *monthAbbr(int month);
157 int parseFrom(const char *s);
159 Date &operator++() { julianDate++; return *this; }
160 Date &operator--() { julianDate--; return *this; }
162 Date &operator+=(int days) { julianDate += days; return *this;}
163 Date &operator-=(int days) { julianDate -= days; return *this;}
165 int year() const;
167 static bool isValidDate(int year, int month, int day);
169 friend Date operator+(const Date &d1, int days);
170 friend Date operator+(int days, const Date &d1);
171 friend Date operator-(const Date &d1, int days);
172 friend int operator-(const Date &d1, const Date & d2);
173 friend int operator<(const Date &d1 ,const Date &d2);
174 friend int operator>(const Date &d1 ,const Date &d2);
175 friend int operator<=(const Date &d1 ,const Date &d2);
176 friend int operator>=(const Date &d1 ,const Date &d2);
177 friend int operator==(const Date &d1 ,const Date &d2);
178 friend int operator!=(const Date &d1 ,const Date &d2);
180 static bool isLeapYear(int year);
181 static int daysInMonth(int month, int year);
182 static int YMDToJulian(int year,int mon,int day, JulianRep &julian);
183 static int julianToYMD(JulianRep julian,int &year,int &month,int &day);
185 private:
186 JulianRep julianDate;
192 class Time { // The class a user would declare to hold time
193 public:
194 Time() {timeVal = 0;}
195 Time(int hours, int mins, int secs, int usec = 0);
196 Time(int totalSecs) : timeVal(totalSecs) {;}
197 Time(const Time &d2) { timeVal = d2.timeVal; }
198 Time& operator=(const Time& d2) { timeVal=d2.timeVal; return *this; }
200 int set(int hours, int mins, int secs, int usec = 0);
201 int get(int &hours, int &mins, int &secs) const;
203 bool isValid() const;
204 void setNull() { timeVal = -1;}
206 int secondsSinceMidnight() const { return timeVal/10000;}
207 int usec() const; // to nearest 100 of usec.
208 int msec() const;
209 int seconds() const;
210 int minutes() const;
211 int hours() const;
212 int setMsec(int ms);
213 int setUsec(int us);
215 int parseFrom(const char *s);
217 Time &operator++() { timeVal += 10000; return *this; }
218 Time &operator--() { timeVal -= 10000; return *this; }
220 Time &operator+=(int seconds) { timeVal += seconds*10000; return *this; }
221 Time &operator-=(int seconds) { timeVal -= seconds*10000; return *this; }
223 static bool isValidTime(int hours, int mins, int secs);
225 friend Time operator+(const Time &t1, int seconds);
226 friend Time operator+(int seconds, const Time &t1);
227 friend Time operator-(const Time &t1, int seconds);
228 friend int operator-(const Time &t1, const Time& t2);
229 friend int operator<(const Time &t1 ,const Time &t2 );
230 friend int operator>(const Time &t1 ,const Time &t2 );
231 friend int operator<=(const Time &t1 ,const Time &t2 );
232 friend int operator>=(const Time &t1 ,const Time &t2 );
233 friend int operator==(const Time &t1 ,const Time &t2 );
234 friend int operator!=(const Time &t1 ,const Time &t2 );
237 private:
238 int timeVal;
241 class TimeStamp {
243 public:
244 TimeStamp() {}
245 TimeStamp(int year, int month, int day, int hour, int minute, int sec, int usec = 0) :
246 date(year, month, day), time(hour, minute, sec, usec) { }
247 TimeStamp(const TimeStamp &ts)
248 { date = ts.date; time = ts.time; }
249 TimeStamp(const Date &d, Time &t) : date(d), time(t) {}
252 TimeStamp& operator=(const TimeStamp& d2)
253 { date=d2.date; time = d2.time; return *this; }
256 int getDate(int &year, int &month, int &day)
257 { return date.get(year, month, day); }
258 void getDate(Date &newDate) const
259 { newDate = date; }
260 int setDate(int year, int month, int day)
261 { return date.set(year, month, day); }
262 void setDate(const Date &newDate)
263 { date = newDate; }
264 operator Date() { return date; }
265 operator Time() { return time; }
267 int getTime(int &hours, int &mins, int &secs) const
268 { return time.get(hours, mins, secs); }
269 void getTime(Time &newTime) const
270 { newTime = time; }
271 int setTime(int hours, int mins, int secs, int usec = 0)
272 { return time.set(hours, mins, secs, usec); }
273 void setTime(const Time &newTime)
274 { time = newTime; }
276 bool isValid() const { return date.isValid() && time.isValid(); }
277 void setNull() { date.setNull(); time.setNull(); }
279 int dayOfWeek() const { return date.dayOfWeek(); }
280 const char *dayOfWeekName() const { return date.dayOfWeekName(); }
281 const char *dayOfWeekAbbr() const { return date.dayOfWeekAbbr(); }
282 int dayOfMonth() const { return date.dayOfMonth(); }
283 int dayOfYear() const { return date.dayOfYear(); }
284 int month() const { return date.month(); }
285 const char *monthName() const { return date.monthName(); }
286 const char *monthAbbr() const { return date.monthAbbr(); }
287 int year() const { return date.year(); }
289 int secondsSinceMidnight() const { return time.secondsSinceMidnight(); }
290 int seconds() const { return time.seconds(); }
291 int minutes() const { return time.minutes(); }
292 int hours() const { return time.hours(); }
293 int msec() const { return time.msec(); }
294 int usec() const { return time.usec(); }
295 int setMsec(int ms) { return time.setMsec(ms) ; }
296 int setUsec(int us) { return time.setUsec(us) ; }
298 int parseDateFrom(const char *s) { return date.parseFrom(s); }
299 int parseTimeFrom(const char *s) { return time.parseFrom(s); }
301 friend int operator<(const TimeStamp &d1, const TimeStamp &d2);
302 friend int operator>(const TimeStamp &d1, const TimeStamp &d2);
303 friend int operator<=(const TimeStamp &d1, const TimeStamp &d2);
304 friend int operator>=(const TimeStamp &d1, const TimeStamp &d2);
305 friend int operator==(const TimeStamp &d1, const TimeStamp &d2);
306 friend int operator!=(const TimeStamp &d1, const TimeStamp &d2);
309 private:
310 Date date;
311 Time time;
315 #endif