*** empty log message ***
[csql.git] / include / DataType.h
blob196f75e3b43cfaf068011f9c6ae980025ab455b1
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);
97 static void subVal(void* dest, void *src, DataType type);
98 static void mulVal(void* dest, void *src, DataType type);
99 static void mudVal(void* dest, void *src, DataType type);
100 static void divVal(void* dest, void *src, DataType type);
103 static bool compareVal(void *src1, void *src2, ComparisionOp op,
104 DataType type, long length = 0);
105 static bool compareIntVal(void* src1, void* src2, ComparisionOp op);
106 static bool compareLongVal(void* src1, void* src2, ComparisionOp op);
107 static bool compareLongLongVal(void* src1, void* src2, ComparisionOp op);
108 static bool compareShortVal(void* src1, void* src2, ComparisionOp op);
109 static bool compareByteIntVal(void* src1, void* src2, ComparisionOp op);
110 static bool compareDoubleVal(void* src1, void* src2, ComparisionOp op);
111 static bool compareFloatVal(void* src1, void* src2, ComparisionOp op);
112 static bool compareDateVal(void* src1, void* src2, ComparisionOp op);
113 static bool compareTimeVal(void* src1, void* src2, ComparisionOp op);
114 static bool compareTimeStampVal(void* src1, void* src2, ComparisionOp op);
115 static bool compareStringVal(void* src1, void* src2, ComparisionOp op);
116 static bool compareBinaryVal(void* src1, void* src2,
117 ComparisionOp op, int length);
120 static void convert(DataType srcType, void *src, DataType destType, void *dest, int length=0);
121 static void convertToInt(void* dest, void* src, DataType srcType);
122 static void convertToLong(void* dest, void* src, DataType srcType);
123 static void convertToLongLong(void* dest, void* src, DataType srcType);
124 static void convertToShort(void* dest, void* src, DataType srcType);
125 static void convertToByteInt(void* dest, void* src, DataType srcType);
126 static void convertToFloat(void* dest, void* src, DataType srcType);
127 static void convertToDouble(void* dest, void* src, DataType srcType);
128 static void convertToString(void* dest, void* src, DataType srcType, int length=0);
129 static void convertToDate(void* dest, void* src, DataType srcType);
130 static void convertToTime(void* dest, void* src, DataType srcType);
131 static void convertToTimeStamp(void* dest, void* src, DataType srcType);
132 static void convertToBinary(void* dest, void* src, DataType srcType, int length);
135 static ComparisionOp getComparisionOperator(char *str);
137 static void* alloc(DataType type, int length =0);
138 static DbRetVal strToValue(void *dest, char *src, DataType type, int length=0);
139 static int printVal(void *src, DataType type, int length);
146 * @class ByteInt
147 * Represents 8 bit integer.
148 * <br/>
151 class ByteInt {
153 public:
154 ByteInt() { }
156 /** copy constructor
158 ByteInt(const ByteInt &v) { val = v.val; }
159 /** constructor with char
160 * @param v char value
162 ByteInt(char v) { val = v; }
163 operator int() const { return (int) val; }
164 char operator=(ByteInt v) { return val = v.val; }
165 char operator=(char v) { return val = v; }
166 char operator+=(ByteInt v) { return val += v.val; }
167 char operator+=(char v) { return val += v; }
168 char operator-=(ByteInt v) { return val -= v.val; }
169 char operator-=(char v) { return val -= v; }
170 char operator*=(ByteInt v) { return val *= v.val; }
171 char operator*=(char v) { return val *= v; }
172 char operator/=(ByteInt v) { return val /= v.val; }
173 char operator/=(char v) { return val /= v; }
174 char operator%=(ByteInt v) { return val %= v.val; }
175 char operator%=(char v) { return val %= v; }
176 char operator<<=(ByteInt v) { return val <<= v.val; }
177 char operator<<=(char v) { return val <<= v; }
178 char operator>>=(ByteInt v) { return val >>= v.val; }
179 char operator>>=(char v) { return val >>= v; }
180 char operator&=(ByteInt v) { return val &= v.val; }
181 char operator&=(char v) { return val &= v; }
182 char operator|=(ByteInt v) { return val |= v.val; }
183 char operator|=(char v) { return val |= v; }
184 char operator^=(ByteInt v) { return val ^= v.val; }
185 char operator^=(char v) { return val ^= v; }
186 char operator++() { return val++; }
187 char operator++(int) { char tmp = val; val++; return val; }
188 char operator--() { return val--; }
189 char operator--(int) { char tmp = val; val--; return val; }
191 private:
192 signed char val;
197 * @class Date
198 * Represents Date Data type.
199 * <br/>
202 class Date { // The class a user would declare to hold date
204 public:
205 Date() {julianDate = 0;}
206 Date(JulianRep julian) : julianDate(julian) {}
208 /** constructor with year, month, day
209 * @param year year
210 * @param month month
211 * @param day day
213 Date(int year, int month, int day);
215 Date(const Date &d2) { julianDate = d2.julianDate; }
216 Date& operator=(const Date& d2)
217 { julianDate=d2.julianDate; return *this; }
219 /** sets the date with specified year, month, day
220 * @param year year
221 * @param month month
222 * @param day day
224 int set(int year, int month, int day);
225 int set(const struct tm *tmptr);
227 /** get year, month, day of the date
228 * @param year year IN
229 * @param month month IN
230 * @param day day IN
232 int get(int &year, int &month, int &day) const;
234 /** checks for the validity of the date
236 bool isValid() const;
238 /** resets the date to zero
240 void setNull() { julianDate = 0;}
242 /** returns day of the week
244 int dayOfWeek() const;
246 /** returns day of the week.
247 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
249 const char *dayOfWeekName() const;
251 /** returns day of the week abbreviation
252 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
254 const char *dayOfWeekAbbr() const;
256 /** returns day of the week.
257 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
259 static const char *dayOfWeekName(int day); // 0--> Sunday
261 /** returns day of the week abbreviation
262 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
264 static const char *dayOfWeekAbbr(int day);
266 static int dayOfWeek(JulianRep juldate);
269 /** returns the day of the month. Values are 1 to 31
271 int dayOfMonth() const;
273 int dayOfYear() const;
275 /** returns the month Values are 1 to 12.
277 int month() const;
279 /** returns the month name
280 * values are "January", "February", "March", "April", "May", "June",
281 * "July", "August", "September", "October", "November", "December"
283 const char *monthName() const;
285 /** returns the month name abbreviation
286 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
287 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
289 const char *monthAbbr() const;
291 /** returns the month name
292 * values are "January", "February", "March", "April", "May", "June",
293 * "July", "August", "September", "October", "November", "December"
295 static const char *monthName(int month);
298 /** returns the month name abbreviation
299 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
300 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
302 static const char *monthAbbr(int month);
304 /** parses the date string passed and stores it
305 *It should of the format "mm/dd/yyyy"
307 int parseFrom(const char *s);
309 Date &operator++() { julianDate++; return *this; }
310 Date &operator--() { julianDate--; return *this; }
312 Date &operator+=(int days) { julianDate += days; return *this;}
313 Date &operator-=(int days) { julianDate -= days; return *this;}
315 /** returns the year
317 int year() const;
319 /** checks for the validity of the date
321 static bool isValidDate(int year, int month, int day);
323 friend Date operator+(const Date &d1, int days);
324 friend Date operator+(int days, const Date &d1);
325 friend Date operator-(const Date &d1, int days);
326 friend int operator-(const Date &d1, const Date & d2);
327 friend int operator<(const Date &d1 ,const Date &d2);
328 friend int operator>(const Date &d1 ,const Date &d2);
329 friend int operator<=(const Date &d1 ,const Date &d2);
330 friend int operator>=(const Date &d1 ,const Date &d2);
331 friend int operator==(const Date &d1 ,const Date &d2);
332 friend int operator!=(const Date &d1 ,const Date &d2);
334 /** checks for leap year
336 static bool isLeapYear(int year);
338 /** returns the number of days in the specified month of the year.
340 static int daysInMonth(int month, int year);
342 static int YMDToJulian(int year,int mon,int day, JulianRep &julian);
343 static int julianToYMD(JulianRep julian,int &year,int &month,int &day);
345 private:
346 JulianRep julianDate;
353 * @class Time
354 * Represents Time Data type.
355 * <br/>
358 class Time { // The class a user would declare to hold time
359 public:
360 Time() {timeVal = 0;}
362 /** Overloaded constructor
363 * @param hours hours
364 * @param mins mins
365 * @param secs secs
366 * @param usec usec
368 Time(int hours, int mins, int secs, int usec = 0);
369 Time(int totalSecs) : timeVal(totalSecs) {;}
370 Time(const Time &d2) { timeVal = d2.timeVal; }
371 Time& operator=(const Time& d2) { timeVal=d2.timeVal; return *this; }
373 /** sets the time with specified hours, mins, secs
374 * @param hours hours
375 * @param mins mins
376 * @param secs secs
377 * @param usec usec
379 int set(int hours, int mins, int secs, int usec = 0);
381 /** retrieves the time using IN parameters
382 * @param hours hours
383 * @param mins mins
384 * @param secs secs
386 int get(int &hours, int &mins, int &secs) const;
388 /** checks for the validity of the time
390 bool isValid() const;
392 /** resets the time
394 void setNull() { timeVal = -1;}
396 int secondsSinceMidnight() const { return timeVal/10000;}
398 /** returns the microsecs
400 int usec() const; // to nearest 100 of usec.
402 /** returns the millisecs
404 int msec() const;
406 /** returns the secs
408 int seconds() const;
410 /** returns the minutes
412 int minutes() const;
414 /** returns the hours
416 int hours() const;
419 /** sets the millisecs
421 int setMsec(int ms);
423 /** sets the microsecs
425 int setUsec(int us);
427 /** parses the time string passed and stores it
428 *It should of the format "hh:mm::ss"
430 int parseFrom(const char *s);
432 Time &operator++() { timeVal += 10000; return *this; }
433 Time &operator--() { timeVal -= 10000; return *this; }
435 Time &operator+=(int seconds) { timeVal += seconds*10000; return *this; }
436 Time &operator-=(int seconds) { timeVal -= seconds*10000; return *this; }
439 /** checks for the validity of the time specified
441 static bool isValidTime(int hours, int mins, int secs);
443 friend Time operator+(const Time &t1, int seconds);
444 friend Time operator+(int seconds, const Time &t1);
445 friend Time operator-(const Time &t1, int seconds);
446 friend int operator-(const Time &t1, const Time& t2);
447 friend int operator<(const Time &t1 ,const Time &t2 );
448 friend int operator>(const Time &t1 ,const Time &t2 );
449 friend int operator<=(const Time &t1 ,const Time &t2 );
450 friend int operator>=(const Time &t1 ,const Time &t2 );
451 friend int operator==(const Time &t1 ,const Time &t2 );
452 friend int operator!=(const Time &t1 ,const Time &t2 );
455 private:
456 int timeVal;
460 * @class TimeStamp
461 * Represents TimeStamp Data type.
462 * <br/>
465 class TimeStamp {
467 public:
468 TimeStamp() {}
470 /** Overloaded constructor
471 * @param year year
472 * @param month month
473 * @param day day
474 * @param hours hours
475 * @param mins mins
476 * @param secs secs
477 * @param usec usec
479 TimeStamp(int year, int month, int day, int hour, int minute, int sec, int usec = 0) :
480 date(year, month, day), time(hour, minute, sec, usec) { }
482 TimeStamp(const TimeStamp &ts)
483 { date = ts.date; time = ts.time; }
484 TimeStamp(const Date &d, Time &t) : date(d), time(t) {}
487 TimeStamp& operator=(const TimeStamp& d2)
488 { date=d2.date; time = d2.time; return *this; }
490 /** get year, month, day from the date part of the timestamp
491 * @param year year IN
492 * @param month month IN
493 * @param day day IN
495 int getDate(int &year, int &month, int &day)
496 { return date.get(year, month, day); }
498 /** get the date part of the timestamp
499 * @param Date date
501 void getDate(Date &newDate) const
502 { newDate = date; }
504 /** sets the date with specified year, month, day
505 * @param year year
506 * @param month month
507 * @param day day
509 int setDate(int year, int month, int day)
510 { return date.set(year, month, day); }
512 /** set the date part of the timestamp
513 * @param Date date
515 void setDate(const Date &newDate)
516 { date = newDate; }
519 operator Date() { return date; }
520 operator Time() { return time; }
524 /** retrieves the time using IN parameters
525 * @param hours hours
526 * @param mins mins
527 * @param secs secs
529 int getTime(int &hours, int &mins, int &secs) const
530 { return time.get(hours, mins, secs); }
531 /** retrieves the time part of the timestamp
532 * @param newTime Time
534 void getTime(Time &newTime) const
535 { newTime = time; }
537 /** sets the time with specified hours, mins, secs
538 * @param hours hours
539 * @param mins mins
540 * @param secs secs
541 * @param usec usec
543 int setTime(int hours, int mins, int secs, int usec = 0)
544 { return time.set(hours, mins, secs, usec); }
546 /** set the time part of the timestamp
547 * @param newTime Time
549 void setTime(const Time &newTime)
550 { time = newTime; }
552 /** checks for the validity of the timestamp
554 bool isValid() const { return date.isValid() && time.isValid(); }
556 /** resets the date and time */
557 void setNull() { date.setNull(); time.setNull(); }
559 /** returns day of the week. Values are 1-7
561 int dayOfWeek() const { return date.dayOfWeek(); }
563 /** returns day of the week.
564 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Satur
565 day".
567 const char *dayOfWeekName() const { return date.dayOfWeekName(); }
569 /** returns day of the week abbreviation
570 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
572 const char *dayOfWeekAbbr() const { return date.dayOfWeekAbbr(); }
574 /** returns the day of the month. Values are 1 to 31
576 int dayOfMonth() const { return date.dayOfMonth(); }
577 int dayOfYear() const { return date.dayOfYear(); }
579 /** returns the month. Values are 1 to 12.
582 int month() const { return date.month(); }
584 /** returns the month name
585 * values are "January", "February", "March", "April", "May", "June",
586 * "July", "August", "September", "October", "November", "December"
588 const char *monthName() const { return date.monthName(); }
590 /** returns the month name abbreviation
591 * Values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
592 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
594 const char *monthAbbr() const { return date.monthAbbr(); }
596 /** returns the year
598 int year() const { return date.year(); }
600 int secondsSinceMidnight() const { return time.secondsSinceMidnight(); }
601 /** returns the seconds */
602 int seconds() const { return time.seconds(); }
603 /** returns the minutes */
604 int minutes() const { return time.minutes(); }
605 /** returns the hours */
606 int hours() const { return time.hours(); }
607 /** returns the millisecs */
608 int msec() const { return time.msec(); }
609 /** returns the microsecs */
610 int usec() const { return time.usec(); }
612 /** sets the millisecs */
613 int setMsec(int ms) { return time.setMsec(ms) ; }
614 /** sets the microsecs */
615 int setUsec(int us) { return time.setUsec(us) ; }
617 /** parses the date string passed and stores it
618 *It should of the format "mm/dd/yyyy"
620 int parseDateFrom(const char *s) { return date.parseFrom(s); }
622 /** parses the time string passed and stores it
623 *It should of the format "hh:mm::ss"
626 int parseTimeFrom(const char *s) { return time.parseFrom(s); }
628 int parseFrom(const char *s);
629 friend int operator<(const TimeStamp &d1, const TimeStamp &d2);
630 friend int operator>(const TimeStamp &d1, const TimeStamp &d2);
631 friend int operator<=(const TimeStamp &d1, const TimeStamp &d2);
632 friend int operator>=(const TimeStamp &d1, const TimeStamp &d2);
633 friend int operator==(const TimeStamp &d1, const TimeStamp &d2);
634 friend int operator!=(const TimeStamp &d1, const TimeStamp &d2);
637 private:
638 Date date;
639 Time time;
643 #endif