test case 6 from wiki page for csqldump tool
[csql.git] / include / DataType.h
blob13a1f0e9dc041d48839212157ab375702a4c6458
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);
93 static bool compareVal(void *src1, void *src2, ComparisionOp op,
94 DataType type, long length = 0);
95 static bool compareIntVal(void* src1, void* src2, ComparisionOp op);
96 static bool compareLongVal(void* src1, void* src2, ComparisionOp op);
97 static bool compareLongLongVal(void* src1, void* src2, ComparisionOp op);
98 static bool compareShortVal(void* src1, void* src2, ComparisionOp op);
99 static bool compareByteIntVal(void* src1, void* src2, ComparisionOp op);
100 static bool compareDoubleVal(void* src1, void* src2, ComparisionOp op);
101 static bool compareFloatVal(void* src1, void* src2, ComparisionOp op);
102 static bool compareDateVal(void* src1, void* src2, ComparisionOp op);
103 static bool compareTimeVal(void* src1, void* src2, ComparisionOp op);
104 static bool compareTimeStampVal(void* src1, void* src2, ComparisionOp op);
105 static bool compareStringVal(void* src1, void* src2, ComparisionOp op);
106 static bool compareBinaryVal(void* src1, void* src2,
107 ComparisionOp op, int length);
110 static void convert(DataType srcType, void *src, DataType destType, void *dest);
111 static void convertToInt(void* dest, void* src, DataType srcType);
112 static void convertToLong(void* dest, void* src, DataType srcType);
113 static void convertToLongLong(void* dest, void* src, DataType srcType);
114 static void convertToShort(void* dest, void* src, DataType srcType);
115 static void convertToByteInt(void* dest, void* src, DataType srcType);
116 static void convertToFloat(void* dest, void* src, DataType srcType);
117 static void convertToDouble(void* dest, void* src, DataType srcType);
118 static void convertToString(void* dest, void* src, DataType srcType);
119 static void convertToDate(void* dest, void* src, DataType srcType);
120 static void convertToTime(void* dest, void* src, DataType srcType);
121 static void convertToTimeStamp(void* dest, void* src, DataType srcType);
124 static ComparisionOp getComparisionOperator(char *str);
126 static void* alloc(DataType type, int length =0);
127 static void strToValue(void *dest, char *src, DataType type, int length=0);
128 static void printVal(void *src, DataType type, int length);
135 * @class ByteInt
136 * Represents 8 bit integer.
137 * <br/>
138 * @author Prabakaran Thirumalai
140 class ByteInt {
142 public:
143 ByteInt() { }
145 /** copy constructor
147 ByteInt(const ByteInt &v) { val = v.val; }
148 /** constructor with char
149 * @param v char value
151 ByteInt(char v) { val = v; }
152 operator int() const { return (int) val; }
153 char operator=(ByteInt v) { return val = v.val; }
154 char operator=(char v) { return val = v; }
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++() { return val++; }
176 char operator++(int) { char tmp = val; val++; return val; }
177 char operator--() { return val--; }
178 char operator--(int) { char tmp = val; val--; return val; }
180 private:
181 signed char val;
186 * @class Date
187 * Represents Date Data type.
188 * <br/>
189 * @author Prabakaran Thirumalai
191 class Date { // The class a user would declare to hold date
193 public:
194 Date() {julianDate = 0;}
195 Date(JulianRep julian) : julianDate(julian) {}
197 /** constructor with year, month, day
198 * @param year year
199 * @param month month
200 * @param day day
202 Date(int year, int month, int day);
204 Date(const Date &d2) { julianDate = d2.julianDate; }
205 Date& operator=(const Date& d2)
206 { julianDate=d2.julianDate; return *this; }
208 /** sets the date with specified year, month, day
209 * @param year year
210 * @param month month
211 * @param day day
213 int set(int year, int month, int day);
214 int set(const struct tm *tmptr);
216 /** get year, month, day of the date
217 * @param year year IN
218 * @param month month IN
219 * @param day day IN
221 int get(int &year, int &month, int &day) const;
223 /** checks for the validity of the date
225 bool isValid() const;
227 /** resets the date to zero
229 void setNull() { julianDate = 0;}
231 /** returns day of the week
233 int dayOfWeek() const;
235 /** returns day of the week.
236 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
238 const char *dayOfWeekName() const;
240 /** returns day of the week abbreviation
241 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
243 const char *dayOfWeekAbbr() const;
245 /** returns day of the week.
246 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
248 static const char *dayOfWeekName(int day); // 0--> Sunday
250 /** returns day of the week abbreviation
251 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
253 static const char *dayOfWeekAbbr(int day);
255 static int dayOfWeek(JulianRep juldate);
258 /** returns the day of the month. Values are 1 to 31
260 int dayOfMonth() const;
262 int dayOfYear() const;
264 /** returns the month Values are 1 to 12.
266 int month() const;
268 /** returns the month name
269 * values are "January", "February", "March", "April", "May", "June",
270 * "July", "August", "September", "October", "November", "December"
272 const char *monthName() const;
274 /** returns the month name abbreviation
275 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
276 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
278 const char *monthAbbr() const;
280 /** returns the month name
281 * values are "January", "February", "March", "April", "May", "June",
282 * "July", "August", "September", "October", "November", "December"
284 static const char *monthName(int month);
287 /** returns the month name abbreviation
288 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
289 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
291 static const char *monthAbbr(int month);
293 /** parses the date string passed and stores it
294 *It should of the format "mm/dd/yyyy"
296 int parseFrom(const char *s);
298 Date &operator++() { julianDate++; return *this; }
299 Date &operator--() { julianDate--; return *this; }
301 Date &operator+=(int days) { julianDate += days; return *this;}
302 Date &operator-=(int days) { julianDate -= days; return *this;}
304 /** returns the year
306 int year() const;
308 /** checks for the validity of the date
310 static bool isValidDate(int year, int month, int day);
312 friend Date operator+(const Date &d1, int days);
313 friend Date operator+(int days, const Date &d1);
314 friend Date operator-(const Date &d1, int days);
315 friend int operator-(const Date &d1, const Date & d2);
316 friend int operator<(const Date &d1 ,const Date &d2);
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);
323 /** checks for leap year
325 static bool isLeapYear(int year);
327 /** returns the number of days in the specified month of the year.
329 static int daysInMonth(int month, int year);
331 static int YMDToJulian(int year,int mon,int day, JulianRep &julian);
332 static int julianToYMD(JulianRep julian,int &year,int &month,int &day);
334 private:
335 JulianRep julianDate;
342 * @class Time
343 * Represents Time Data type.
344 * <br/>
345 * @author Prabakaran Thirumalai
347 class Time { // The class a user would declare to hold time
348 public:
349 Time() {timeVal = 0;}
351 /** Overloaded constructor
352 * @param hours hours
353 * @param mins mins
354 * @param secs secs
355 * @param usec usec
357 Time(int hours, int mins, int secs, int usec = 0);
358 Time(int totalSecs) : timeVal(totalSecs) {;}
359 Time(const Time &d2) { timeVal = d2.timeVal; }
360 Time& operator=(const Time& d2) { timeVal=d2.timeVal; return *this; }
362 /** sets the time with specified hours, mins, secs
363 * @param hours hours
364 * @param mins mins
365 * @param secs secs
366 * @param usec usec
368 int set(int hours, int mins, int secs, int usec = 0);
370 /** retrieves the time using IN parameters
371 * @param hours hours
372 * @param mins mins
373 * @param secs secs
375 int get(int &hours, int &mins, int &secs) const;
377 /** checks for the validity of the time
379 bool isValid() const;
381 /** resets the time
383 void setNull() { timeVal = -1;}
385 int secondsSinceMidnight() const { return timeVal/10000;}
387 /** returns the microsecs
389 int usec() const; // to nearest 100 of usec.
391 /** returns the millisecs
393 int msec() const;
395 /** returns the secs
397 int seconds() const;
399 /** returns the minutes
401 int minutes() const;
403 /** returns the hours
405 int hours() const;
408 /** sets the millisecs
410 int setMsec(int ms);
412 /** sets the microsecs
414 int setUsec(int us);
416 /** parses the time string passed and stores it
417 *It should of the format "hh:mm::ss"
419 int parseFrom(const char *s);
421 Time &operator++() { timeVal += 10000; return *this; }
422 Time &operator--() { timeVal -= 10000; return *this; }
424 Time &operator+=(int seconds) { timeVal += seconds*10000; return *this; }
425 Time &operator-=(int seconds) { timeVal -= seconds*10000; return *this; }
428 /** checks for the validity of the time specified
430 static bool isValidTime(int hours, int mins, int secs);
432 friend Time operator+(const Time &t1, int seconds);
433 friend Time operator+(int seconds, const Time &t1);
434 friend Time operator-(const Time &t1, int seconds);
435 friend int operator-(const Time &t1, const Time& t2);
436 friend int operator<(const Time &t1 ,const Time &t2 );
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 );
444 private:
445 int timeVal;
449 * @class TimeStamp
450 * Represents TimeStamp Data type.
451 * <br/>
452 * @author Prabakaran Thirumalai
454 class TimeStamp {
456 public:
457 TimeStamp() {}
459 /** Overloaded constructor
460 * @param year year
461 * @param month month
462 * @param day day
463 * @param hours hours
464 * @param mins mins
465 * @param secs secs
466 * @param usec usec
468 TimeStamp(int year, int month, int day, int hour, int minute, int sec, int usec = 0) :
469 date(year, month, day), time(hour, minute, sec, usec) { }
471 TimeStamp(const TimeStamp &ts)
472 { date = ts.date; time = ts.time; }
473 TimeStamp(const Date &d, Time &t) : date(d), time(t) {}
476 TimeStamp& operator=(const TimeStamp& d2)
477 { date=d2.date; time = d2.time; return *this; }
479 /** get year, month, day from the date part of the timestamp
480 * @param year year IN
481 * @param month month IN
482 * @param day day IN
484 int getDate(int &year, int &month, int &day)
485 { return date.get(year, month, day); }
487 /** get the date part of the timestamp
488 * @param Date date
490 void getDate(Date &newDate) const
491 { newDate = date; }
493 /** sets the date with specified year, month, day
494 * @param year year
495 * @param month month
496 * @param day day
498 int setDate(int year, int month, int day)
499 { return date.set(year, month, day); }
501 /** set the date part of the timestamp
502 * @param Date date
504 void setDate(const Date &newDate)
505 { date = newDate; }
508 operator Date() { return date; }
509 operator Time() { return time; }
513 /** retrieves the time using IN parameters
514 * @param hours hours
515 * @param mins mins
516 * @param secs secs
518 int getTime(int &hours, int &mins, int &secs) const
519 { return time.get(hours, mins, secs); }
520 /** retrieves the time part of the timestamp
521 * @param newTime Time
523 void getTime(Time &newTime) const
524 { newTime = time; }
526 /** sets the time with specified hours, mins, secs
527 * @param hours hours
528 * @param mins mins
529 * @param secs secs
530 * @param usec usec
532 int setTime(int hours, int mins, int secs, int usec = 0)
533 { return time.set(hours, mins, secs, usec); }
535 /** set the time part of the timestamp
536 * @param newTime Time
538 void setTime(const Time &newTime)
539 { time = newTime; }
541 /** checks for the validity of the timestamp
543 bool isValid() const { return date.isValid() && time.isValid(); }
545 /** resets the date and time */
546 void setNull() { date.setNull(); time.setNull(); }
548 /** returns day of the week. Values are 1-7
550 int dayOfWeek() const { return date.dayOfWeek(); }
552 /** returns day of the week.
553 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Satur
554 day".
556 const char *dayOfWeekName() const { return date.dayOfWeekName(); }
558 /** returns day of the week abbreviation
559 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
561 const char *dayOfWeekAbbr() const { return date.dayOfWeekAbbr(); }
563 /** returns the day of the month. Values are 1 to 31
565 int dayOfMonth() const { return date.dayOfMonth(); }
566 int dayOfYear() const { return date.dayOfYear(); }
568 /** returns the month. Values are 1 to 12.
571 int month() const { return date.month(); }
573 /** returns the month name
574 * values are "January", "February", "March", "April", "May", "June",
575 * "July", "August", "September", "October", "November", "December"
577 const char *monthName() const { return date.monthName(); }
579 /** returns the month name abbreviation
580 * Values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
581 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
583 const char *monthAbbr() const { return date.monthAbbr(); }
585 /** returns the year
587 int year() const { return date.year(); }
589 int secondsSinceMidnight() const { return time.secondsSinceMidnight(); }
590 /** returns the seconds */
591 int seconds() const { return time.seconds(); }
592 /** returns the minutes */
593 int minutes() const { return time.minutes(); }
594 /** returns the hours */
595 int hours() const { return time.hours(); }
596 /** returns the millisecs */
597 int msec() const { return time.msec(); }
598 /** returns the microsecs */
599 int usec() const { return time.usec(); }
601 /** sets the millisecs */
602 int setMsec(int ms) { return time.setMsec(ms) ; }
603 /** sets the microsecs */
604 int setUsec(int us) { return time.setUsec(us) ; }
606 /** parses the date string passed and stores it
607 *It should of the format "mm/dd/yyyy"
609 int parseDateFrom(const char *s) { return date.parseFrom(s); }
611 /** parses the time string passed and stores it
612 *It should of the format "hh:mm::ss"
615 int parseTimeFrom(const char *s) { return time.parseFrom(s); }
617 int parseFrom(const char *s);
618 friend int operator<(const TimeStamp &d1, const TimeStamp &d2);
619 friend int operator>(const TimeStamp &d1, const TimeStamp &d2);
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);
626 private:
627 Date date;
628 Time time;
632 #endif