using tree index
[csql.git] / include / DataType.h
blobaaba740bd19290c0c1b923f0e28a17b42a5f37aa
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<ErrorType.h>
21 //#include<os.h>
22 typedef int JulianRep;
25 /**
26 * @class DataType
27 * Data Types supported by the database system.
28 * <br/>
31 enum DataType {
32 typeInt = 0, /**<integer type*/
33 typeLong = 1,
34 typeLongLong = 2,
35 typeShort = 3,
36 typeByteInt = 4,
38 typeDouble = 10,
39 typeFloat = 11,
40 typeDecimal = 12,
42 typeDate = 20,
43 typeTime = 21,
44 typeTimeStamp = 22,
46 typeString = 30,
47 typeBinary = 31,
49 typeComposite = 98,
50 typeULong = 99,
51 typeUnknown = 100
54 /**
55 * @class ComparisionOp
56 * Comparision operators supported by the database system.
57 * <br/>
60 enum ComparisionOp {
61 OpEquals = 0,
62 OpNotEquals,
63 OpLessThan,
64 OpLessThanEquals,
65 OpGreaterThan,
66 OpGreaterThanEquals,
67 OpLike, // for Like operator
68 OpInvalidComparisionOp
71 /**
72 * @class LogicalOp
73 * Logical operators supported by the database system.
74 * <br/>
77 enum LogicalOp {
78 OpAnd = 0,
79 OpOr,
80 OpNot,
81 OpInvalidLogicalOp
85 class AllDataType
87 public:
88 static long size(DataType type, int length =0);
89 static char* getSQLString(DataType type);
90 static SQLSMALLINT convertToSQLType(DataType type);
91 static SQLSMALLINT convertToSQL_C_Type(DataType type);
92 static DataType convertFromSQLType(SQLSMALLINT type);
94 static void copyVal(void* dest, void *src, DataType type, int length = 0);
95 static void addVal(void* dest, void *src, DataType type);
96 static void divVal(void* dest, int src, DataType type);
98 static bool compareVal(void *src1, void *src2, ComparisionOp op,
99 DataType type, long length = 0);
100 static bool compareIntVal(void* src1, void* src2, ComparisionOp op);
101 static bool compareLongVal(void* src1, void* src2, ComparisionOp op);
102 static bool compareLongLongVal(void* src1, void* src2, ComparisionOp op);
103 static bool compareShortVal(void* src1, void* src2, ComparisionOp op);
104 static bool compareByteIntVal(void* src1, void* src2, ComparisionOp op);
105 static bool compareDoubleVal(void* src1, void* src2, ComparisionOp op);
106 static bool compareFloatVal(void* src1, void* src2, ComparisionOp op);
107 static bool compareDateVal(void* src1, void* src2, ComparisionOp op);
108 static bool compareTimeVal(void* src1, void* src2, ComparisionOp op);
109 static bool compareTimeStampVal(void* src1, void* src2, ComparisionOp op);
110 static bool compareStringVal(void* src1, void* src2, ComparisionOp op);
111 static bool compareBinaryVal(void* src1, void* src2,
112 ComparisionOp op, int length);
115 static void convert(DataType srcType, void *src, DataType destType, void *dest, int length=0);
116 static void convertToInt(void* dest, void* src, DataType srcType);
117 static void convertToLong(void* dest, void* src, DataType srcType);
118 static void convertToLongLong(void* dest, void* src, DataType srcType);
119 static void convertToShort(void* dest, void* src, DataType srcType);
120 static void convertToByteInt(void* dest, void* src, DataType srcType);
121 static void convertToFloat(void* dest, void* src, DataType srcType);
122 static void convertToDouble(void* dest, void* src, DataType srcType);
123 static void convertToString(void* dest, void* src, DataType srcType, int length=0);
124 static void convertToDate(void* dest, void* src, DataType srcType);
125 static void convertToTime(void* dest, void* src, DataType srcType);
126 static void convertToTimeStamp(void* dest, void* src, DataType srcType);
127 static void convertToBinary(void* dest, void* src, DataType srcType, int length);
130 static ComparisionOp getComparisionOperator(char *str);
132 static void* alloc(DataType type, int length =0);
133 static DbRetVal strToValue(void *dest, char *src, DataType type, int length=0);
134 static int printVal(void *src, DataType type, int length);
141 * @class ByteInt
142 * Represents 8 bit integer.
143 * <br/>
146 class ByteInt {
148 public:
149 ByteInt() { }
151 /** copy constructor
153 ByteInt(const ByteInt &v) { val = v.val; }
154 /** constructor with char
155 * @param v char value
157 ByteInt(char v) { val = v; }
158 operator int() const { return (int) val; }
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|=(ByteInt v) { return val |= v.val; }
178 char operator|=(char v) { return val |= v; }
179 char operator^=(ByteInt v) { return val ^= v.val; }
180 char operator^=(char v) { return val ^= v; }
181 char operator++() { return val++; }
182 char operator++(int) { char tmp = val; val++; return val; }
183 char operator--() { return val--; }
184 char operator--(int) { char tmp = val; val--; return val; }
186 private:
187 signed char val;
192 * @class Date
193 * Represents Date Data type.
194 * <br/>
197 class Date { // The class a user would declare to hold date
199 public:
200 Date() {julianDate = 0;}
201 Date(JulianRep julian) : julianDate(julian) {}
203 /** constructor with year, month, day
204 * @param year year
205 * @param month month
206 * @param day day
208 Date(int year, int month, int day);
210 Date(const Date &d2) { julianDate = d2.julianDate; }
211 Date& operator=(const Date& d2)
212 { julianDate=d2.julianDate; return *this; }
214 /** sets the date with specified year, month, day
215 * @param year year
216 * @param month month
217 * @param day day
219 int set(int year, int month, int day);
220 int set(const struct tm *tmptr);
222 /** get year, month, day of the date
223 * @param year year IN
224 * @param month month IN
225 * @param day day IN
227 int get(int &year, int &month, int &day) const;
229 /** checks for the validity of the date
231 bool isValid() const;
233 /** resets the date to zero
235 void setNull() { julianDate = 0;}
237 /** returns day of the week
239 int dayOfWeek() const;
241 /** returns day of the week.
242 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
244 const char *dayOfWeekName() const;
246 /** returns day of the week abbreviation
247 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
249 const char *dayOfWeekAbbr() const;
251 /** returns day of the week.
252 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
254 static const char *dayOfWeekName(int day); // 0--> Sunday
256 /** returns day of the week abbreviation
257 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
259 static const char *dayOfWeekAbbr(int day);
261 static int dayOfWeek(JulianRep juldate);
264 /** returns the day of the month. Values are 1 to 31
266 int dayOfMonth() const;
268 int dayOfYear() const;
270 /** returns the month Values are 1 to 12.
272 int month() const;
274 /** returns the month name
275 * values are "January", "February", "March", "April", "May", "June",
276 * "July", "August", "September", "October", "November", "December"
278 const char *monthName() const;
280 /** returns the month name abbreviation
281 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
282 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
284 const char *monthAbbr() const;
286 /** returns the month name
287 * values are "January", "February", "March", "April", "May", "June",
288 * "July", "August", "September", "October", "November", "December"
290 static const char *monthName(int month);
293 /** returns the month name abbreviation
294 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
295 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
297 static const char *monthAbbr(int month);
299 /** parses the date string passed and stores it
300 *It should of the format "mm/dd/yyyy"
302 int parseFrom(const char *s);
304 Date &operator++() { julianDate++; return *this; }
305 Date &operator--() { julianDate--; return *this; }
307 Date &operator+=(int days) { julianDate += days; return *this;}
308 Date &operator-=(int days) { julianDate -= days; return *this;}
310 /** returns the year
312 int year() const;
314 /** checks for the validity of the date
316 static bool isValidDate(int year, int month, int day);
318 friend Date operator+(const Date &d1, int days);
319 friend Date operator+(int days, const Date &d1);
320 friend Date operator-(const Date &d1, int days);
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);
324 friend int operator<=(const Date &d1 ,const Date &d2);
325 friend int operator>=(const Date &d1 ,const Date &d2);
326 friend int operator==(const Date &d1 ,const Date &d2);
327 friend int operator!=(const Date &d1 ,const Date &d2);
329 /** checks for leap year
331 static bool isLeapYear(int year);
333 /** returns the number of days in the specified month of the year.
335 static int daysInMonth(int month, int year);
337 static int YMDToJulian(int year,int mon,int day, JulianRep &julian);
338 static int julianToYMD(JulianRep julian,int &year,int &month,int &day);
340 private:
341 JulianRep julianDate;
348 * @class Time
349 * Represents Time Data type.
350 * <br/>
353 class Time { // The class a user would declare to hold time
354 public:
355 Time() {timeVal = 0;}
357 /** Overloaded constructor
358 * @param hours hours
359 * @param mins mins
360 * @param secs secs
361 * @param usec usec
363 Time(int hours, int mins, int secs, int usec = 0);
364 Time(int totalSecs) : timeVal(totalSecs) {;}
365 Time(const Time &d2) { timeVal = d2.timeVal; }
366 Time& operator=(const Time& d2) { timeVal=d2.timeVal; return *this; }
368 /** sets the time with specified hours, mins, secs
369 * @param hours hours
370 * @param mins mins
371 * @param secs secs
372 * @param usec usec
374 int set(int hours, int mins, int secs, int usec = 0);
376 /** retrieves the time using IN parameters
377 * @param hours hours
378 * @param mins mins
379 * @param secs secs
381 int get(int &hours, int &mins, int &secs) const;
383 /** checks for the validity of the time
385 bool isValid() const;
387 /** resets the time
389 void setNull() { timeVal = -1;}
391 int secondsSinceMidnight() const { return timeVal/10000;}
393 /** returns the microsecs
395 int usec() const; // to nearest 100 of usec.
397 /** returns the millisecs
399 int msec() const;
401 /** returns the secs
403 int seconds() const;
405 /** returns the minutes
407 int minutes() const;
409 /** returns the hours
411 int hours() const;
414 /** sets the millisecs
416 int setMsec(int ms);
418 /** sets the microsecs
420 int setUsec(int us);
422 /** parses the time string passed and stores it
423 *It should of the format "hh:mm::ss"
425 int parseFrom(const char *s);
427 Time &operator++() { timeVal += 10000; return *this; }
428 Time &operator--() { timeVal -= 10000; return *this; }
430 Time &operator+=(int seconds) { timeVal += seconds*10000; return *this; }
431 Time &operator-=(int seconds) { timeVal -= seconds*10000; return *this; }
434 /** checks for the validity of the time specified
436 static bool isValidTime(int hours, int mins, int secs);
438 friend Time operator+(const Time &t1, int seconds);
439 friend Time operator+(int seconds, const Time &t1);
440 friend Time operator-(const Time &t1, int seconds);
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 );
444 friend int operator<=(const Time &t1 ,const Time &t2 );
445 friend int operator>=(const Time &t1 ,const Time &t2 );
446 friend int operator==(const Time &t1 ,const Time &t2 );
447 friend int operator!=(const Time &t1 ,const Time &t2 );
450 private:
451 int timeVal;
455 * @class TimeStamp
456 * Represents TimeStamp Data type.
457 * <br/>
460 class TimeStamp {
462 public:
463 TimeStamp() {}
465 /** Overloaded constructor
466 * @param year year
467 * @param month month
468 * @param day day
469 * @param hours hours
470 * @param mins mins
471 * @param secs secs
472 * @param usec usec
474 TimeStamp(int year, int month, int day, int hour, int minute, int sec, int usec = 0) :
475 date(year, month, day), time(hour, minute, sec, usec) { }
477 TimeStamp(const TimeStamp &ts)
478 { date = ts.date; time = ts.time; }
479 TimeStamp(const Date &d, Time &t) : date(d), time(t) {}
482 TimeStamp& operator=(const TimeStamp& d2)
483 { date=d2.date; time = d2.time; return *this; }
485 /** get year, month, day from the date part of the timestamp
486 * @param year year IN
487 * @param month month IN
488 * @param day day IN
490 int getDate(int &year, int &month, int &day)
491 { return date.get(year, month, day); }
493 /** get the date part of the timestamp
494 * @param Date date
496 void getDate(Date &newDate) const
497 { newDate = date; }
499 /** sets the date with specified year, month, day
500 * @param year year
501 * @param month month
502 * @param day day
504 int setDate(int year, int month, int day)
505 { return date.set(year, month, day); }
507 /** set the date part of the timestamp
508 * @param Date date
510 void setDate(const Date &newDate)
511 { date = newDate; }
514 operator Date() { return date; }
515 operator Time() { return time; }
519 /** retrieves the time using IN parameters
520 * @param hours hours
521 * @param mins mins
522 * @param secs secs
524 int getTime(int &hours, int &mins, int &secs) const
525 { return time.get(hours, mins, secs); }
526 /** retrieves the time part of the timestamp
527 * @param newTime Time
529 void getTime(Time &newTime) const
530 { newTime = time; }
532 /** sets the time with specified hours, mins, secs
533 * @param hours hours
534 * @param mins mins
535 * @param secs secs
536 * @param usec usec
538 int setTime(int hours, int mins, int secs, int usec = 0)
539 { return time.set(hours, mins, secs, usec); }
541 /** set the time part of the timestamp
542 * @param newTime Time
544 void setTime(const Time &newTime)
545 { time = newTime; }
547 /** checks for the validity of the timestamp
549 bool isValid() const { return date.isValid() && time.isValid(); }
551 /** resets the date and time */
552 void setNull() { date.setNull(); time.setNull(); }
554 /** returns day of the week. Values are 1-7
556 int dayOfWeek() const { return date.dayOfWeek(); }
558 /** returns day of the week.
559 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Satur
560 day".
562 const char *dayOfWeekName() const { return date.dayOfWeekName(); }
564 /** returns day of the week abbreviation
565 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
567 const char *dayOfWeekAbbr() const { return date.dayOfWeekAbbr(); }
569 /** returns the day of the month. Values are 1 to 31
571 int dayOfMonth() const { return date.dayOfMonth(); }
572 int dayOfYear() const { return date.dayOfYear(); }
574 /** returns the month. Values are 1 to 12.
577 int month() const { return date.month(); }
579 /** returns the month name
580 * values are "January", "February", "March", "April", "May", "June",
581 * "July", "August", "September", "October", "November", "December"
583 const char *monthName() const { return date.monthName(); }
585 /** returns the month name abbreviation
586 * Values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
587 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
589 const char *monthAbbr() const { return date.monthAbbr(); }
591 /** returns the year
593 int year() const { return date.year(); }
595 int secondsSinceMidnight() const { return time.secondsSinceMidnight(); }
596 /** returns the seconds */
597 int seconds() const { return time.seconds(); }
598 /** returns the minutes */
599 int minutes() const { return time.minutes(); }
600 /** returns the hours */
601 int hours() const { return time.hours(); }
602 /** returns the millisecs */
603 int msec() const { return time.msec(); }
604 /** returns the microsecs */
605 int usec() const { return time.usec(); }
607 /** sets the millisecs */
608 int setMsec(int ms) { return time.setMsec(ms) ; }
609 /** sets the microsecs */
610 int setUsec(int us) { return time.setUsec(us) ; }
612 /** parses the date string passed and stores it
613 *It should of the format "mm/dd/yyyy"
615 int parseDateFrom(const char *s) { return date.parseFrom(s); }
617 /** parses the time string passed and stores it
618 *It should of the format "hh:mm::ss"
621 int parseTimeFrom(const char *s) { return time.parseFrom(s); }
623 int parseFrom(const char *s);
624 friend int operator<(const TimeStamp &d1, const TimeStamp &d2);
625 friend int operator>(const TimeStamp &d1, const TimeStamp &d2);
626 friend int operator<=(const TimeStamp &d1, const TimeStamp &d2);
627 friend int operator>=(const TimeStamp &d1, const TimeStamp &d2);
628 friend int operator==(const TimeStamp &d1, const TimeStamp &d2);
629 friend int operator!=(const TimeStamp &d1, const TimeStamp &d2);
632 private:
633 Date date;
634 Time time;
638 #endif