1847644 support for aggregation operators for single table
[csql.git] / include / DataType.h
blob0cb7ec9db8986c149eafdb5a9febd8c384ded159
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.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 #include<sqlext.h>
19 #include<sqltypes.h>
20 //#include<os.h>
21 typedef int JulianRep;
24 /**
25 * @class DataType
26 * Data Types supported by the database system.
27 * <br/>
28 * @author Prabakaran Thirumalai
30 enum DataType {
31 typeInt = 0, /**<integer type*/
32 typeLong = 1,
33 typeLongLong = 2,
34 typeShort = 3,
35 typeByteInt = 4,
37 typeDouble = 10,
38 typeFloat = 11,
39 typeDecimal = 12,
41 typeDate = 20,
42 typeTime = 21,
43 typeTimeStamp = 22,
45 typeString = 30,
46 typeBinary = 31,
48 typeULong = 99,
49 typeUnknown = 100
52 /**
53 * @class ComparisionOp
54 * Comparision operators supported by the database system.
55 * <br/>
56 * @author Prabakaran Thirumalai
58 enum ComparisionOp {
59 OpEquals = 0,
60 OpNotEquals,
61 OpLessThan,
62 OpLessThanEquals,
63 OpGreaterThan,
64 OpGreaterThanEquals,
65 OpInvalidComparisionOp
68 /**
69 * @class LogicalOp
70 * Logical operators supported by the database system.
71 * <br/>
72 * @author Prabakaran Thirumalai
74 enum LogicalOp {
75 OpAnd = 0,
76 OpOr,
77 OpNot,
78 OpInvalidLogicalOp
82 class AllDataType
84 public:
85 static long size(DataType type, int length =0);
86 static char* getSQLString(DataType type);
87 static SQLSMALLINT convertToSQLType(DataType type);
88 static SQLSMALLINT convertToSQL_C_Type(DataType type);
89 static DataType convertFromSQLType(SQLSMALLINT type);
91 static void copyVal(void* dest, void *src, DataType type, int length = 0);
92 static void addVal(void* dest, void *src, DataType type);
93 static void divVal(void* dest, int src, DataType type);
95 static bool compareVal(void *src1, void *src2, ComparisionOp op,
96 DataType type, long length = 0);
97 static bool compareIntVal(void* src1, void* src2, ComparisionOp op);
98 static bool compareLongVal(void* src1, void* src2, ComparisionOp op);
99 static bool compareLongLongVal(void* src1, void* src2, ComparisionOp op);
100 static bool compareShortVal(void* src1, void* src2, ComparisionOp op);
101 static bool compareByteIntVal(void* src1, void* src2, ComparisionOp op);
102 static bool compareDoubleVal(void* src1, void* src2, ComparisionOp op);
103 static bool compareFloatVal(void* src1, void* src2, ComparisionOp op);
104 static bool compareDateVal(void* src1, void* src2, ComparisionOp op);
105 static bool compareTimeVal(void* src1, void* src2, ComparisionOp op);
106 static bool compareTimeStampVal(void* src1, void* src2, ComparisionOp op);
107 static bool compareStringVal(void* src1, void* src2, ComparisionOp op);
108 static bool compareBinaryVal(void* src1, void* src2,
109 ComparisionOp op, int length);
112 static void convert(DataType srcType, void *src, DataType destType, void *dest);
113 static void convertToInt(void* dest, void* src, DataType srcType);
114 static void convertToLong(void* dest, void* src, DataType srcType);
115 static void convertToLongLong(void* dest, void* src, DataType srcType);
116 static void convertToShort(void* dest, void* src, DataType srcType);
117 static void convertToByteInt(void* dest, void* src, DataType srcType);
118 static void convertToFloat(void* dest, void* src, DataType srcType);
119 static void convertToDouble(void* dest, void* src, DataType srcType);
120 static void convertToString(void* dest, void* src, DataType srcType);
121 static void convertToDate(void* dest, void* src, DataType srcType);
122 static void convertToTime(void* dest, void* src, DataType srcType);
123 static void convertToTimeStamp(void* dest, void* src, DataType srcType);
126 static ComparisionOp getComparisionOperator(char *str);
128 static void* alloc(DataType type, int length =0);
129 static void strToValue(void *dest, char *src, DataType type, int length=0);
130 static int printVal(void *src, DataType type, int length);
137 * @class ByteInt
138 * Represents 8 bit integer.
139 * <br/>
140 * @author Prabakaran Thirumalai
142 class ByteInt {
144 public:
145 ByteInt() { }
147 /** copy constructor
149 ByteInt(const ByteInt &v) { val = v.val; }
150 /** constructor with char
151 * @param v char value
153 ByteInt(char v) { val = v; }
154 operator int() const { return (int) val; }
155 char operator=(ByteInt v) { return val = v.val; }
156 char operator=(char v) { return val = v; }
157 char operator+=(ByteInt v) { return val += v.val; }
158 char operator+=(char v) { return val += v; }
159 char operator-=(ByteInt v) { return val -= v.val; }
160 char operator-=(char v) { return val -= v; }
161 char operator*=(ByteInt v) { return val *= v.val; }
162 char operator*=(char v) { return val *= v; }
163 char operator/=(ByteInt v) { return val /= v.val; }
164 char operator/=(char v) { return val /= v; }
165 char operator%=(ByteInt v) { return val %= v.val; }
166 char operator%=(char v) { return val %= v; }
167 char operator<<=(ByteInt v) { return val <<= v.val; }
168 char operator<<=(char v) { return val <<= v; }
169 char operator>>=(ByteInt v) { return val >>= v.val; }
170 char operator>>=(char v) { return val >>= v; }
171 char operator&=(ByteInt v) { return val &= v.val; }
172 char operator&=(char v) { return val &= v; }
173 char operator|=(ByteInt v) { return val |= v.val; }
174 char operator|=(char v) { return val |= v; }
175 char operator^=(ByteInt v) { return val ^= v.val; }
176 char operator^=(char v) { return val ^= v; }
177 char operator++() { return val++; }
178 char operator++(int) { char tmp = val; val++; return val; }
179 char operator--() { return val--; }
180 char operator--(int) { char tmp = val; val--; return val; }
182 private:
183 signed char val;
188 * @class Date
189 * Represents Date Data type.
190 * <br/>
191 * @author Prabakaran Thirumalai
193 class Date { // The class a user would declare to hold date
195 public:
196 Date() {julianDate = 0;}
197 Date(JulianRep julian) : julianDate(julian) {}
199 /** constructor with year, month, day
200 * @param year year
201 * @param month month
202 * @param day day
204 Date(int year, int month, int day);
206 Date(const Date &d2) { julianDate = d2.julianDate; }
207 Date& operator=(const Date& d2)
208 { julianDate=d2.julianDate; return *this; }
210 /** sets the date with specified year, month, day
211 * @param year year
212 * @param month month
213 * @param day day
215 int set(int year, int month, int day);
216 int set(const struct tm *tmptr);
218 /** get year, month, day of the date
219 * @param year year IN
220 * @param month month IN
221 * @param day day IN
223 int get(int &year, int &month, int &day) const;
225 /** checks for the validity of the date
227 bool isValid() const;
229 /** resets the date to zero
231 void setNull() { julianDate = 0;}
233 /** returns day of the week
235 int dayOfWeek() const;
237 /** returns day of the week.
238 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
240 const char *dayOfWeekName() const;
242 /** returns day of the week abbreviation
243 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
245 const char *dayOfWeekAbbr() const;
247 /** returns day of the week.
248 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
250 static const char *dayOfWeekName(int day); // 0--> Sunday
252 /** returns day of the week abbreviation
253 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
255 static const char *dayOfWeekAbbr(int day);
257 static int dayOfWeek(JulianRep juldate);
260 /** returns the day of the month. Values are 1 to 31
262 int dayOfMonth() const;
264 int dayOfYear() const;
266 /** returns the month Values are 1 to 12.
268 int month() const;
270 /** returns the month name
271 * values are "January", "February", "March", "April", "May", "June",
272 * "July", "August", "September", "October", "November", "December"
274 const char *monthName() const;
276 /** returns the month name abbreviation
277 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
278 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
280 const char *monthAbbr() const;
282 /** returns the month name
283 * values are "January", "February", "March", "April", "May", "June",
284 * "July", "August", "September", "October", "November", "December"
286 static const char *monthName(int month);
289 /** returns the month name abbreviation
290 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
291 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
293 static const char *monthAbbr(int month);
295 /** parses the date string passed and stores it
296 *It should of the format "mm/dd/yyyy"
298 int parseFrom(const char *s);
300 Date &operator++() { julianDate++; return *this; }
301 Date &operator--() { julianDate--; return *this; }
303 Date &operator+=(int days) { julianDate += days; return *this;}
304 Date &operator-=(int days) { julianDate -= days; return *this;}
306 /** returns the year
308 int year() const;
310 /** checks for the validity of the date
312 static bool isValidDate(int year, int month, int day);
314 friend Date operator+(const Date &d1, int days);
315 friend Date operator+(int days, const Date &d1);
316 friend Date operator-(const Date &d1, int days);
317 friend int operator-(const Date &d1, const Date & d2);
318 friend int operator<(const Date &d1 ,const Date &d2);
319 friend int operator>(const Date &d1 ,const Date &d2);
320 friend int operator<=(const Date &d1 ,const Date &d2);
321 friend int operator>=(const Date &d1 ,const Date &d2);
322 friend int operator==(const Date &d1 ,const Date &d2);
323 friend int operator!=(const Date &d1 ,const Date &d2);
325 /** checks for leap year
327 static bool isLeapYear(int year);
329 /** returns the number of days in the specified month of the year.
331 static int daysInMonth(int month, int year);
333 static int YMDToJulian(int year,int mon,int day, JulianRep &julian);
334 static int julianToYMD(JulianRep julian,int &year,int &month,int &day);
336 private:
337 JulianRep julianDate;
344 * @class Time
345 * Represents Time Data type.
346 * <br/>
347 * @author Prabakaran Thirumalai
349 class Time { // The class a user would declare to hold time
350 public:
351 Time() {timeVal = 0;}
353 /** Overloaded constructor
354 * @param hours hours
355 * @param mins mins
356 * @param secs secs
357 * @param usec usec
359 Time(int hours, int mins, int secs, int usec = 0);
360 Time(int totalSecs) : timeVal(totalSecs) {;}
361 Time(const Time &d2) { timeVal = d2.timeVal; }
362 Time& operator=(const Time& d2) { timeVal=d2.timeVal; return *this; }
364 /** sets the time with specified hours, mins, secs
365 * @param hours hours
366 * @param mins mins
367 * @param secs secs
368 * @param usec usec
370 int set(int hours, int mins, int secs, int usec = 0);
372 /** retrieves the time using IN parameters
373 * @param hours hours
374 * @param mins mins
375 * @param secs secs
377 int get(int &hours, int &mins, int &secs) const;
379 /** checks for the validity of the time
381 bool isValid() const;
383 /** resets the time
385 void setNull() { timeVal = -1;}
387 int secondsSinceMidnight() const { return timeVal/10000;}
389 /** returns the microsecs
391 int usec() const; // to nearest 100 of usec.
393 /** returns the millisecs
395 int msec() const;
397 /** returns the secs
399 int seconds() const;
401 /** returns the minutes
403 int minutes() const;
405 /** returns the hours
407 int hours() const;
410 /** sets the millisecs
412 int setMsec(int ms);
414 /** sets the microsecs
416 int setUsec(int us);
418 /** parses the time string passed and stores it
419 *It should of the format "hh:mm::ss"
421 int parseFrom(const char *s);
423 Time &operator++() { timeVal += 10000; return *this; }
424 Time &operator--() { timeVal -= 10000; return *this; }
426 Time &operator+=(int seconds) { timeVal += seconds*10000; return *this; }
427 Time &operator-=(int seconds) { timeVal -= seconds*10000; return *this; }
430 /** checks for the validity of the time specified
432 static bool isValidTime(int hours, int mins, int secs);
434 friend Time operator+(const Time &t1, int seconds);
435 friend Time operator+(int seconds, const Time &t1);
436 friend Time operator-(const Time &t1, int seconds);
437 friend int operator-(const Time &t1, const Time& t2);
438 friend int operator<(const Time &t1 ,const Time &t2 );
439 friend int operator>(const Time &t1 ,const Time &t2 );
440 friend int operator<=(const Time &t1 ,const Time &t2 );
441 friend int operator>=(const Time &t1 ,const Time &t2 );
442 friend int operator==(const Time &t1 ,const Time &t2 );
443 friend int operator!=(const Time &t1 ,const Time &t2 );
446 private:
447 int timeVal;
451 * @class TimeStamp
452 * Represents TimeStamp Data type.
453 * <br/>
454 * @author Prabakaran Thirumalai
456 class TimeStamp {
458 public:
459 TimeStamp() {}
461 /** Overloaded constructor
462 * @param year year
463 * @param month month
464 * @param day day
465 * @param hours hours
466 * @param mins mins
467 * @param secs secs
468 * @param usec usec
470 TimeStamp(int year, int month, int day, int hour, int minute, int sec, int usec = 0) :
471 date(year, month, day), time(hour, minute, sec, usec) { }
473 TimeStamp(const TimeStamp &ts)
474 { date = ts.date; time = ts.time; }
475 TimeStamp(const Date &d, Time &t) : date(d), time(t) {}
478 TimeStamp& operator=(const TimeStamp& d2)
479 { date=d2.date; time = d2.time; return *this; }
481 /** get year, month, day from the date part of the timestamp
482 * @param year year IN
483 * @param month month IN
484 * @param day day IN
486 int getDate(int &year, int &month, int &day)
487 { return date.get(year, month, day); }
489 /** get the date part of the timestamp
490 * @param Date date
492 void getDate(Date &newDate) const
493 { newDate = date; }
495 /** sets the date with specified year, month, day
496 * @param year year
497 * @param month month
498 * @param day day
500 int setDate(int year, int month, int day)
501 { return date.set(year, month, day); }
503 /** set the date part of the timestamp
504 * @param Date date
506 void setDate(const Date &newDate)
507 { date = newDate; }
510 operator Date() { return date; }
511 operator Time() { return time; }
515 /** retrieves the time using IN parameters
516 * @param hours hours
517 * @param mins mins
518 * @param secs secs
520 int getTime(int &hours, int &mins, int &secs) const
521 { return time.get(hours, mins, secs); }
522 /** retrieves the time part of the timestamp
523 * @param newTime Time
525 void getTime(Time &newTime) const
526 { newTime = time; }
528 /** sets the time with specified hours, mins, secs
529 * @param hours hours
530 * @param mins mins
531 * @param secs secs
532 * @param usec usec
534 int setTime(int hours, int mins, int secs, int usec = 0)
535 { return time.set(hours, mins, secs, usec); }
537 /** set the time part of the timestamp
538 * @param newTime Time
540 void setTime(const Time &newTime)
541 { time = newTime; }
543 /** checks for the validity of the timestamp
545 bool isValid() const { return date.isValid() && time.isValid(); }
547 /** resets the date and time */
548 void setNull() { date.setNull(); time.setNull(); }
550 /** returns day of the week. Values are 1-7
552 int dayOfWeek() const { return date.dayOfWeek(); }
554 /** returns day of the week.
555 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Satur
556 day".
558 const char *dayOfWeekName() const { return date.dayOfWeekName(); }
560 /** returns day of the week abbreviation
561 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
563 const char *dayOfWeekAbbr() const { return date.dayOfWeekAbbr(); }
565 /** returns the day of the month. Values are 1 to 31
567 int dayOfMonth() const { return date.dayOfMonth(); }
568 int dayOfYear() const { return date.dayOfYear(); }
570 /** returns the month. Values are 1 to 12.
573 int month() const { return date.month(); }
575 /** returns the month name
576 * values are "January", "February", "March", "April", "May", "June",
577 * "July", "August", "September", "October", "November", "December"
579 const char *monthName() const { return date.monthName(); }
581 /** returns the month name abbreviation
582 * Values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
583 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
585 const char *monthAbbr() const { return date.monthAbbr(); }
587 /** returns the year
589 int year() const { return date.year(); }
591 int secondsSinceMidnight() const { return time.secondsSinceMidnight(); }
592 /** returns the seconds */
593 int seconds() const { return time.seconds(); }
594 /** returns the minutes */
595 int minutes() const { return time.minutes(); }
596 /** returns the hours */
597 int hours() const { return time.hours(); }
598 /** returns the millisecs */
599 int msec() const { return time.msec(); }
600 /** returns the microsecs */
601 int usec() const { return time.usec(); }
603 /** sets the millisecs */
604 int setMsec(int ms) { return time.setMsec(ms) ; }
605 /** sets the microsecs */
606 int setUsec(int us) { return time.setUsec(us) ; }
608 /** parses the date string passed and stores it
609 *It should of the format "mm/dd/yyyy"
611 int parseDateFrom(const char *s) { return date.parseFrom(s); }
613 /** parses the time string passed and stores it
614 *It should of the format "hh:mm::ss"
617 int parseTimeFrom(const char *s) { return time.parseFrom(s); }
619 int parseFrom(const char *s);
620 friend int operator<(const TimeStamp &d1, const TimeStamp &d2);
621 friend int operator>(const TimeStamp &d1, const TimeStamp &d2);
622 friend int operator<=(const TimeStamp &d1, const TimeStamp &d2);
623 friend int operator>=(const TimeStamp &d1, const TimeStamp &d2);
624 friend int operator==(const TimeStamp &d1, const TimeStamp &d2);
625 friend int operator!=(const TimeStamp &d1, const TimeStamp &d2);
628 private:
629 Date date;
630 Time time;
634 #endif