Though select fails, it return the tuple. This is because curTuple_ is not set in...
[csql.git] / include / DataType.h
blob834dc536d70ed82b5944d6e71d2feebd0fa1f74d
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
19 //#include<os.h>
20 typedef int JulianRep;
23 /**
24 * @class DataType
25 * Data Types supported by the database system.
26 * <br/>
27 * @author Prabakaran Thirumalai
29 enum DataType {
30 typeInt = 0, /**<integer type*/
31 typeLong = 1,
32 typeLongLong = 2,
33 typeShort = 3,
34 typeByteInt = 4,
36 typeDouble = 10,
37 typeFloat = 11,
38 typeDecimal = 12,
40 typeDate = 20,
41 typeTime = 21,
42 typeTimeStamp = 22,
44 typeString = 30,
45 typeBinary = 31,
47 typeULong = 99,
48 typeUnknown = 100
51 /**
52 * @class ComparisionOp
53 * Comparision operators supported by the database system.
54 * <br/>
55 * @author Prabakaran Thirumalai
57 enum ComparisionOp {
58 OpEquals = 0,
59 OpNotEquals,
60 OpLessThan,
61 OpLessThanEquals,
62 OpGreaterThan,
63 OpGreaterThanEquals,
64 OpInvalidComparision
67 /**
68 * @class LogicalOp
69 * Logical operators supported by the database system.
70 * <br/>
71 * @author Prabakaran Thirumalai
73 enum LogicalOp {
74 OpAnd = 0,
75 OpOr,
76 OpNot
80 class AllDataType
82 public:
83 static long size(DataType type);
84 static void copyVal(void* dest, void *src, DataType type, int length = 0);
86 static bool compareVal(void *src1, void *src2, ComparisionOp op,
87 DataType type, long length = 0);
88 static bool compareIntVal(void* src1, void* src2, ComparisionOp op);
89 static bool compareLongVal(void* src1, void* src2, ComparisionOp op);
90 static bool compareLongLongVal(void* src1, void* src2, ComparisionOp op);
91 static bool compareShortVal(void* src1, void* src2, ComparisionOp op);
92 static bool compareByteIntVal(void* src1, void* src2, ComparisionOp op);
93 static bool compareDoubleVal(void* src1, void* src2, ComparisionOp op);
94 static bool compareFloatVal(void* src1, void* src2, ComparisionOp op);
95 static bool compareDateVal(void* src1, void* src2, ComparisionOp op);
96 static bool compareTimeVal(void* src1, void* src2, ComparisionOp op);
97 static bool compareTimeStampVal(void* src1, void* src2, ComparisionOp op);
98 static bool compareStringVal(void* src1, void* src2, ComparisionOp op);
99 static bool compareBinaryVal(void* src1, void* src2,
100 ComparisionOp op, int length);
103 static void convert(DataType srcType, void *src, DataType destType, void *dest);
104 static void convertToInt(void* dest, void* src, DataType srcType);
105 static void convertToLong(void* dest, void* src, DataType srcType);
106 static void convertToLongLong(void* dest, void* src, DataType srcType);
107 static void convertToShort(void* dest, void* src, DataType srcType);
108 static void convertToByteInt(void* dest, void* src, DataType srcType);
109 static void convertToFloat(void* dest, void* src, DataType srcType);
110 static void convertToDouble(void* dest, void* src, DataType srcType);
111 static void convertToString(void* dest, void* src, DataType srcType);
114 static ComparisionOp getComparisionOperator(char *str);
116 static void* alloc(DataType type);
117 static void strToValue(void *dest, char *src, DataType type);
118 static void printVal(void *src, DataType type, int length);
125 * @class ByteInt
126 * Represents 8 bit integer.
127 * <br/>
128 * @author Prabakaran Thirumalai
130 class ByteInt {
132 public:
133 ByteInt() { }
135 /** copy constructor
137 ByteInt(const ByteInt &v) { val = v.val; }
138 /** constructor with char
139 * @param v char value
141 ByteInt(char v) { val = v; }
142 operator int() const { return (int) val; }
143 char operator=(ByteInt v) { return val = v.val; }
144 char operator=(char v) { return val = v; }
145 char operator+=(ByteInt v) { return val += v.val; }
146 char operator+=(char v) { return val += v; }
147 char operator-=(ByteInt v) { return val -= v.val; }
148 char operator-=(char v) { return val -= v; }
149 char operator*=(ByteInt v) { return val *= v.val; }
150 char operator*=(char v) { return val *= v; }
151 char operator/=(ByteInt v) { return val /= v.val; }
152 char operator/=(char v) { return val /= v; }
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++() { return val++; }
166 char operator++(int) { char tmp = val; val++; return val; }
167 char operator--() { return val--; }
168 char operator--(int) { char tmp = val; val--; return val; }
170 private:
171 signed char val;
176 * @class Date
177 * Represents Date Data type.
178 * <br/>
179 * @author Prabakaran Thirumalai
181 class Date { // The class a user would declare to hold date
183 public:
184 Date() {julianDate = 0;}
185 Date(JulianRep julian) : julianDate(julian) {}
187 /** constructor with year, month, day
188 * @param year year
189 * @param month month
190 * @param day day
192 Date(int year, int month, int day);
194 Date(const Date &d2) { julianDate = d2.julianDate; }
195 Date& operator=(const Date& d2)
196 { julianDate=d2.julianDate; return *this; }
198 /** sets the date with specified year, month, day
199 * @param year year
200 * @param month month
201 * @param day day
203 int set(int year, int month, int day);
204 int set(const struct tm *tmptr);
206 /** get year, month, day of the date
207 * @param year year IN
208 * @param month month IN
209 * @param day day IN
211 int get(int &year, int &month, int &day) const;
213 /** checks for the validity of the date
215 bool isValid() const;
217 /** resets the date to zero
219 void setNull() { julianDate = 0;}
221 /** returns day of the week
223 int dayOfWeek() const;
225 /** returns day of the week.
226 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
228 const char *dayOfWeekName() const;
230 /** returns day of the week abbreviation
231 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
233 const char *dayOfWeekAbbr() const;
235 /** returns day of the week.
236 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
238 static const char *dayOfWeekName(int day); // 0--> Sunday
240 /** returns day of the week abbreviation
241 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
243 static const char *dayOfWeekAbbr(int day);
245 static int dayOfWeek(JulianRep juldate);
248 /** returns the day of the month. Values are 1 to 31
250 int dayOfMonth() const;
252 int dayOfYear() const;
254 /** returns the month Values are 1 to 12.
256 int month() const;
258 /** returns the month name
259 * values are "January", "February", "March", "April", "May", "June",
260 * "July", "August", "September", "October", "November", "December"
262 const char *monthName() const;
264 /** returns the month name abbreviation
265 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
266 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
268 const char *monthAbbr() const;
270 /** returns the month name
271 * values are "January", "February", "March", "April", "May", "June",
272 * "July", "August", "September", "October", "November", "December"
274 static const char *monthName(int month);
277 /** returns the month name abbreviation
278 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
279 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
281 static const char *monthAbbr(int month);
283 /** parses the date string passed and stores it
284 *It should of the format "mm/dd/yyyy"
286 int parseFrom(const char *s);
288 Date &operator++() { julianDate++; return *this; }
289 Date &operator--() { julianDate--; return *this; }
291 Date &operator+=(int days) { julianDate += days; return *this;}
292 Date &operator-=(int days) { julianDate -= days; return *this;}
294 /** returns the year
296 int year() const;
298 /** checks for the validity of the date
300 static bool isValidDate(int year, int month, int day);
302 friend Date operator+(const Date &d1, int days);
303 friend Date operator+(int days, const Date &d1);
304 friend Date operator-(const Date &d1, int days);
305 friend int operator-(const Date &d1, const Date & d2);
306 friend int operator<(const Date &d1 ,const Date &d2);
307 friend int operator>(const Date &d1 ,const Date &d2);
308 friend int operator<=(const Date &d1 ,const Date &d2);
309 friend int operator>=(const Date &d1 ,const Date &d2);
310 friend int operator==(const Date &d1 ,const Date &d2);
311 friend int operator!=(const Date &d1 ,const Date &d2);
313 /** checks for leap year
315 static bool isLeapYear(int year);
317 /** returns the number of days in the specified month of the year.
319 static int daysInMonth(int month, int year);
321 static int YMDToJulian(int year,int mon,int day, JulianRep &julian);
322 static int julianToYMD(JulianRep julian,int &year,int &month,int &day);
324 private:
325 JulianRep julianDate;
332 * @class Time
333 * Represents Time Data type.
334 * <br/>
335 * @author Prabakaran Thirumalai
337 class Time { // The class a user would declare to hold time
338 public:
339 Time() {timeVal = 0;}
341 /** Overloaded constructor
342 * @param hours hours
343 * @param mins mins
344 * @param secs secs
345 * @param usec usec
347 Time(int hours, int mins, int secs, int usec = 0);
348 Time(int totalSecs) : timeVal(totalSecs) {;}
349 Time(const Time &d2) { timeVal = d2.timeVal; }
350 Time& operator=(const Time& d2) { timeVal=d2.timeVal; return *this; }
352 /** sets the time with specified hours, mins, secs
353 * @param hours hours
354 * @param mins mins
355 * @param secs secs
356 * @param usec usec
358 int set(int hours, int mins, int secs, int usec = 0);
360 /** retrieves the time using IN parameters
361 * @param hours hours
362 * @param mins mins
363 * @param secs secs
365 int get(int &hours, int &mins, int &secs) const;
367 /** checks for the validity of the time
369 bool isValid() const;
371 /** resets the time
373 void setNull() { timeVal = -1;}
375 int secondsSinceMidnight() const { return timeVal/10000;}
377 /** returns the microsecs
379 int usec() const; // to nearest 100 of usec.
381 /** returns the millisecs
383 int msec() const;
385 /** returns the secs
387 int seconds() const;
389 /** returns the minutes
391 int minutes() const;
393 /** returns the hours
395 int hours() const;
398 /** sets the millisecs
400 int setMsec(int ms);
402 /** sets the microsecs
404 int setUsec(int us);
406 /** parses the time string passed and stores it
407 *It should of the format "hh:mm::ss"
409 int parseFrom(const char *s);
411 Time &operator++() { timeVal += 10000; return *this; }
412 Time &operator--() { timeVal -= 10000; return *this; }
414 Time &operator+=(int seconds) { timeVal += seconds*10000; return *this; }
415 Time &operator-=(int seconds) { timeVal -= seconds*10000; return *this; }
418 /** checks for the validity of the time specified
420 static bool isValidTime(int hours, int mins, int secs);
422 friend Time operator+(const Time &t1, int seconds);
423 friend Time operator+(int seconds, const Time &t1);
424 friend Time operator-(const Time &t1, int seconds);
425 friend int operator-(const Time &t1, const Time& t2);
426 friend int operator<(const Time &t1 ,const Time &t2 );
427 friend int operator>(const Time &t1 ,const Time &t2 );
428 friend int operator<=(const Time &t1 ,const Time &t2 );
429 friend int operator>=(const Time &t1 ,const Time &t2 );
430 friend int operator==(const Time &t1 ,const Time &t2 );
431 friend int operator!=(const Time &t1 ,const Time &t2 );
434 private:
435 int timeVal;
439 * @class TimeStamp
440 * Represents TimeStamp Data type.
441 * <br/>
442 * @author Prabakaran Thirumalai
444 class TimeStamp {
446 public:
447 TimeStamp() {}
449 /** Overloaded constructor
450 * @param year year
451 * @param month month
452 * @param day day
453 * @param hours hours
454 * @param mins mins
455 * @param secs secs
456 * @param usec usec
458 TimeStamp(int year, int month, int day, int hour, int minute, int sec, int usec = 0) :
459 date(year, month, day), time(hour, minute, sec, usec) { }
461 TimeStamp(const TimeStamp &ts)
462 { date = ts.date; time = ts.time; }
463 TimeStamp(const Date &d, Time &t) : date(d), time(t) {}
466 TimeStamp& operator=(const TimeStamp& d2)
467 { date=d2.date; time = d2.time; return *this; }
469 /** get year, month, day from the date part of the timestamp
470 * @param year year IN
471 * @param month month IN
472 * @param day day IN
474 int getDate(int &year, int &month, int &day)
475 { return date.get(year, month, day); }
477 /** get the date part of the timestamp
478 * @param Date date
480 void getDate(Date &newDate) const
481 { newDate = date; }
483 /** sets the date with specified year, month, day
484 * @param year year
485 * @param month month
486 * @param day day
488 int setDate(int year, int month, int day)
489 { return date.set(year, month, day); }
491 /** set the date part of the timestamp
492 * @param Date date
494 void setDate(const Date &newDate)
495 { date = newDate; }
498 operator Date() { return date; }
499 operator Time() { return time; }
503 /** retrieves the time using IN parameters
504 * @param hours hours
505 * @param mins mins
506 * @param secs secs
508 int getTime(int &hours, int &mins, int &secs) const
509 { return time.get(hours, mins, secs); }
510 /** retrieves the time part of the timestamp
511 * @param newTime Time
513 void getTime(Time &newTime) const
514 { newTime = time; }
516 /** sets the time with specified hours, mins, secs
517 * @param hours hours
518 * @param mins mins
519 * @param secs secs
520 * @param usec usec
522 int setTime(int hours, int mins, int secs, int usec = 0)
523 { return time.set(hours, mins, secs, usec); }
525 /** set the time part of the timestamp
526 * @param newTime Time
528 void setTime(const Time &newTime)
529 { time = newTime; }
531 /** checks for the validity of the timestamp
533 bool isValid() const { return date.isValid() && time.isValid(); }
535 /** resets the date and time */
536 void setNull() { date.setNull(); time.setNull(); }
538 /** returns day of the week. Values are 1-7
540 int dayOfWeek() const { return date.dayOfWeek(); }
542 /** returns day of the week.
543 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Satur
544 day".
546 const char *dayOfWeekName() const { return date.dayOfWeekName(); }
548 /** returns day of the week abbreviation
549 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
551 const char *dayOfWeekAbbr() const { return date.dayOfWeekAbbr(); }
553 /** returns the day of the month. Values are 1 to 31
555 int dayOfMonth() const { return date.dayOfMonth(); }
556 int dayOfYear() const { return date.dayOfYear(); }
558 /** returns the month. Values are 1 to 12.
561 int month() const { return date.month(); }
563 /** returns the month name
564 * values are "January", "February", "March", "April", "May", "June",
565 * "July", "August", "September", "October", "November", "December"
567 const char *monthName() const { return date.monthName(); }
569 /** returns the month name abbreviation
570 * Values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
571 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
573 const char *monthAbbr() const { return date.monthAbbr(); }
575 /** returns the year
577 int year() const { return date.year(); }
579 int secondsSinceMidnight() const { return time.secondsSinceMidnight(); }
580 /** returns the seconds */
581 int seconds() const { return time.seconds(); }
582 /** returns the minutes */
583 int minutes() const { return time.minutes(); }
584 /** returns the hours */
585 int hours() const { return time.hours(); }
586 /** returns the millisecs */
587 int msec() const { return time.msec(); }
588 /** returns the microsecs */
589 int usec() const { return time.usec(); }
591 /** sets the millisecs */
592 int setMsec(int ms) { return time.setMsec(ms) ; }
593 /** sets the microsecs */
594 int setUsec(int us) { return time.setUsec(us) ; }
596 /** parses the date string passed and stores it
597 *It should of the format "mm/dd/yyyy"
599 int parseDateFrom(const char *s) { return date.parseFrom(s); }
601 /** parses the time string passed and stores it
602 *It should of the format "hh:mm::ss"
605 int parseTimeFrom(const char *s) { return time.parseFrom(s); }
607 friend int operator<(const TimeStamp &d1, const TimeStamp &d2);
608 friend int operator>(const TimeStamp &d1, const TimeStamp &d2);
609 friend int operator<=(const TimeStamp &d1, const TimeStamp &d2);
610 friend int operator>=(const TimeStamp &d1, const TimeStamp &d2);
611 friend int operator==(const TimeStamp &d1, const TimeStamp &d2);
612 friend int operator!=(const TimeStamp &d1, const TimeStamp &d2);
615 private:
616 Date date;
617 Time time;
621 #endif