performance fixes for JDBC wisc benchmark
[csql.git] / include / DataType.h
blob1da43578f2ed4d884c03dfa11698bd829de39530
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
70 static char CompOpNames[][20] =
72 "Equals", "NotEquals", "LessThan", "LessThanEquals", "GreaterThan",
73 "GreaterThanEquals", "Like", "Invalid"
77 /**
78 * @class LogicalOp
79 * Logical operators supported by the database system.
80 * <br/>
83 enum LogicalOp {
84 OpAnd = 0,
85 OpOr,
86 OpNot,
87 OpInvalidLogicalOp
90 static char LogOpNames[][10] =
92 "AND", "OR", "NOT", "Invalid"
95 class AllDataType
97 public:
98 static long size(DataType type, int length =0);
99 static char* getSQLString(DataType type);
100 static SQLSMALLINT convertToSQLType(DataType type);
101 static SQLSMALLINT convertToSQL_C_Type(DataType type);
102 static DataType convertFromSQLType(SQLSMALLINT type);
104 static void copyVal(void* dest, void *src, DataType type, int length = 0);
105 static void addVal(void* dest, void *src, DataType type);
106 static void divVal(void* dest, int src, DataType type);
107 static void subVal(void* dest, void *src, DataType type);
108 static void mulVal(void* dest, void *src, DataType type);
109 static void mudVal(void* dest, void *src, DataType type);
110 static void divVal(void* dest, void *src, DataType type);
113 static bool compareVal(void *src1, void *src2, ComparisionOp op,
114 DataType type, long length = 0);
115 static bool compareIntVal(void* src1, void* src2, ComparisionOp op);
116 static bool compareLongVal(void* src1, void* src2, ComparisionOp op);
117 static bool compareLongLongVal(void* src1, void* src2, ComparisionOp op);
118 static bool compareShortVal(void* src1, void* src2, ComparisionOp op);
119 static bool compareByteIntVal(void* src1, void* src2, ComparisionOp op);
120 static bool compareDoubleVal(void* src1, void* src2, ComparisionOp op);
121 static bool compareFloatVal(void* src1, void* src2, ComparisionOp op);
122 static bool compareDateVal(void* src1, void* src2, ComparisionOp op);
123 static bool compareTimeVal(void* src1, void* src2, ComparisionOp op);
124 static bool compareTimeStampVal(void* src1, void* src2, ComparisionOp op);
125 static bool compareStringVal(void* src1, void* src2, ComparisionOp op);
126 static bool compareBinaryVal(void* src1, void* src2,
127 ComparisionOp op, int length);
130 static void convert(DataType srcType, void *src, DataType destType, void *dest, int length=0);
131 static void convertToInt(void* dest, void* src, DataType srcType);
132 static void convertToLong(void* dest, void* src, DataType srcType);
133 static void convertToLongLong(void* dest, void* src, DataType srcType);
134 static void convertToShort(void* dest, void* src, DataType srcType);
135 static void convertToByteInt(void* dest, void* src, DataType srcType);
136 static void convertToFloat(void* dest, void* src, DataType srcType);
137 static void convertToDouble(void* dest, void* src, DataType srcType);
138 static void convertToString(void* dest, void* src, DataType srcType, int length=0);
139 static void convertToDate(void* dest, void* src, DataType srcType);
140 static void convertToTime(void* dest, void* src, DataType srcType);
141 static void convertToTimeStamp(void* dest, void* src, DataType srcType);
142 static void convertToBinary(void* dest, void* src, DataType srcType, int length);
144 static void memoryset(void *value,DataType type);
145 static ComparisionOp getComparisionOperator(char *str);
147 static void* alloc(DataType type, int length =0);
148 static DbRetVal strToValue(void *dest, char *src, DataType type, int length=0);
149 static int printVal(void *src, DataType type, int length);
156 * @class ByteInt
157 * Represents 8 bit integer.
158 * <br/>
161 class ByteInt {
163 public:
164 ByteInt() { }
166 /** copy constructor
168 ByteInt(const ByteInt &v) { val = v.val; }
169 /** constructor with char
170 * @param v char value
172 ByteInt(char v) { val = v; }
173 operator int() const { return (int) val; }
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<<=(ByteInt v) { return val <<= v.val; }
187 char operator<<=(char v) { return val <<= v; }
188 char operator>>=(ByteInt v) { return val >>= v.val; }
189 char operator>>=(char v) { return val >>= v; }
190 char operator&=(ByteInt v) { return val &= v.val; }
191 char operator&=(char v) { return val &= v; }
192 char operator|=(ByteInt v) { return val |= v.val; }
193 char operator|=(char v) { return val |= v; }
194 char operator^=(ByteInt v) { return val ^= v.val; }
195 char operator^=(char v) { return val ^= v; }
196 char operator++() { return val++; }
197 char operator++(int) { char tmp = val; val++; return val; }
198 char operator--() { return val--; }
199 char operator--(int) { char tmp = val; val--; return val; }
201 private:
202 signed char val;
207 * @class Date
208 * Represents Date Data type.
209 * <br/>
212 class Date { // The class a user would declare to hold date
214 public:
215 Date() {julianDate = 0;}
216 Date(JulianRep julian) : julianDate(julian) {}
218 /** constructor with year, month, day
219 * @param year year
220 * @param month month
221 * @param day day
223 Date(int year, int month, int day);
225 Date(const Date &d2) { julianDate = d2.julianDate; }
226 Date& operator=(const Date& d2)
227 { julianDate=d2.julianDate; return *this; }
229 /** sets the date with specified year, month, day
230 * @param year year
231 * @param month month
232 * @param day day
234 int set(int year, int month, int day);
235 int set(const struct tm *tmptr);
237 /** get year, month, day of the date
238 * @param year year IN
239 * @param month month IN
240 * @param day day IN
242 int get(int &year, int &month, int &day) const;
244 /** checks for the validity of the date
246 bool isValid() const;
248 /** resets the date to zero
250 void setNull() { julianDate = 0;}
252 /** returns day of the week
254 int dayOfWeek() const;
256 /** returns day of the week.
257 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
259 const char *dayOfWeekName() const;
261 /** returns day of the week abbreviation
262 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
264 const char *dayOfWeekAbbr() const;
266 /** returns day of the week.
267 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
269 static const char *dayOfWeekName(int day); // 0--> Sunday
271 /** returns day of the week abbreviation
272 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
274 static const char *dayOfWeekAbbr(int day);
276 static int dayOfWeek(JulianRep juldate);
279 /** returns the day of the month. Values are 1 to 31
281 int dayOfMonth() const;
283 int dayOfYear() const;
285 /** returns the month Values are 1 to 12.
287 int month() const;
289 /** returns the month name
290 * values are "January", "February", "March", "April", "May", "June",
291 * "July", "August", "September", "October", "November", "December"
293 const char *monthName() const;
295 /** returns the month name abbreviation
296 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
297 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
299 const char *monthAbbr() const;
301 /** returns the month name
302 * values are "January", "February", "March", "April", "May", "June",
303 * "July", "August", "September", "October", "November", "December"
305 static const char *monthName(int month);
308 /** returns the month name abbreviation
309 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
310 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
312 static const char *monthAbbr(int month);
314 /** parses the date string passed and stores it
315 *It should of the format "mm/dd/yyyy"
317 int parseFrom(const char *s);
319 Date &operator++() { julianDate++; return *this; }
320 Date &operator--() { julianDate--; return *this; }
322 Date &operator+=(int days) { julianDate += days; return *this;}
323 Date &operator-=(int days) { julianDate -= days; return *this;}
325 /** returns the year
327 int year() const;
329 /** checks for the validity of the date
331 static bool isValidDate(int year, int month, int day);
333 friend Date operator+(const Date &d1, int days);
334 friend Date operator+(int days, const Date &d1);
335 friend Date operator-(const Date &d1, int days);
336 friend int operator-(const Date &d1, const Date & d2);
337 friend int operator<(const Date &d1 ,const Date &d2);
338 friend int operator>(const Date &d1 ,const Date &d2);
339 friend int operator<=(const Date &d1 ,const Date &d2);
340 friend int operator>=(const Date &d1 ,const Date &d2);
341 friend int operator==(const Date &d1 ,const Date &d2);
342 friend int operator!=(const Date &d1 ,const Date &d2);
344 /** checks for leap year
346 static bool isLeapYear(int year);
348 /** returns the number of days in the specified month of the year.
350 static int daysInMonth(int month, int year);
352 static int YMDToJulian(int year,int mon,int day, JulianRep &julian);
353 static int julianToYMD(JulianRep julian,int &year,int &month,int &day);
355 private:
356 JulianRep julianDate;
363 * @class Time
364 * Represents Time Data type.
365 * <br/>
368 class Time { // The class a user would declare to hold time
369 public:
370 Time() {timeVal = 0;}
372 /** Overloaded constructor
373 * @param hours hours
374 * @param mins mins
375 * @param secs secs
376 * @param usec usec
378 Time(int hours, int mins, int secs, int usec = 0);
379 Time(int totalSecs) : timeVal(totalSecs) {;}
380 Time(const Time &d2) { timeVal = d2.timeVal; }
381 Time& operator=(const Time& d2) { timeVal=d2.timeVal; return *this; }
383 /** sets the time with specified hours, mins, secs
384 * @param hours hours
385 * @param mins mins
386 * @param secs secs
387 * @param usec usec
389 int set(int hours, int mins, int secs, int usec = 0);
391 /** retrieves the time using IN parameters
392 * @param hours hours
393 * @param mins mins
394 * @param secs secs
396 int get(int &hours, int &mins, int &secs) const;
398 /** checks for the validity of the time
400 bool isValid() const;
402 /** resets the time
404 void setNull() { timeVal = -1;}
406 int secondsSinceMidnight() const { return timeVal/10000;}
408 /** returns the microsecs
410 int usec() const; // to nearest 100 of usec.
412 /** returns the millisecs
414 int msec() const;
416 /** returns the secs
418 int seconds() const;
420 /** returns the minutes
422 int minutes() const;
424 /** returns the hours
426 int hours() const;
429 /** sets the millisecs
431 int setMsec(int ms);
433 /** sets the microsecs
435 int setUsec(int us);
437 /** parses the time string passed and stores it
438 *It should of the format "hh:mm::ss"
440 int parseFrom(const char *s);
442 Time &operator++() { timeVal += 10000; return *this; }
443 Time &operator--() { timeVal -= 10000; return *this; }
445 Time &operator+=(int seconds) { timeVal += seconds*10000; return *this; }
446 Time &operator-=(int seconds) { timeVal -= seconds*10000; return *this; }
449 /** checks for the validity of the time specified
451 static bool isValidTime(int hours, int mins, int secs);
453 friend Time operator+(const Time &t1, int seconds);
454 friend Time operator+(int seconds, const Time &t1);
455 friend Time operator-(const Time &t1, int seconds);
456 friend int operator-(const Time &t1, const Time& t2);
457 friend int operator<(const Time &t1 ,const Time &t2 );
458 friend int operator>(const Time &t1 ,const Time &t2 );
459 friend int operator<=(const Time &t1 ,const Time &t2 );
460 friend int operator>=(const Time &t1 ,const Time &t2 );
461 friend int operator==(const Time &t1 ,const Time &t2 );
462 friend int operator!=(const Time &t1 ,const Time &t2 );
465 private:
466 int timeVal;
470 * @class TimeStamp
471 * Represents TimeStamp Data type.
472 * <br/>
475 class TimeStamp {
477 public:
478 TimeStamp() {}
480 /** Overloaded constructor
481 * @param year year
482 * @param month month
483 * @param day day
484 * @param hours hours
485 * @param mins mins
486 * @param secs secs
487 * @param usec usec
489 TimeStamp(int year, int month, int day, int hour, int minute, int sec, int usec = 0) :
490 date(year, month, day), time(hour, minute, sec, usec) { }
492 TimeStamp(const TimeStamp &ts)
493 { date = ts.date; time = ts.time; }
494 TimeStamp(const Date &d, Time &t) : date(d), time(t) {}
497 TimeStamp& operator=(const TimeStamp& d2)
498 { date=d2.date; time = d2.time; return *this; }
500 /** get year, month, day from the date part of the timestamp
501 * @param year year IN
502 * @param month month IN
503 * @param day day IN
505 int getDate(int &year, int &month, int &day)
506 { return date.get(year, month, day); }
508 /** get the date part of the timestamp
509 * @param Date date
511 void getDate(Date &newDate) const
512 { newDate = date; }
514 /** sets the date with specified year, month, day
515 * @param year year
516 * @param month month
517 * @param day day
519 int setDate(int year, int month, int day)
520 { return date.set(year, month, day); }
522 /** set the date part of the timestamp
523 * @param Date date
525 void setDate(const Date &newDate)
526 { date = newDate; }
529 operator Date() { return date; }
530 operator Time() { return time; }
534 /** retrieves the time using IN parameters
535 * @param hours hours
536 * @param mins mins
537 * @param secs secs
539 int getTime(int &hours, int &mins, int &secs) const
540 { return time.get(hours, mins, secs); }
541 /** retrieves the time part of the timestamp
542 * @param newTime Time
544 void getTime(Time &newTime) const
545 { newTime = time; }
547 /** sets the time with specified hours, mins, secs
548 * @param hours hours
549 * @param mins mins
550 * @param secs secs
551 * @param usec usec
553 int setTime(int hours, int mins, int secs, int usec = 0)
554 { return time.set(hours, mins, secs, usec); }
556 /** set the time part of the timestamp
557 * @param newTime Time
559 void setTime(const Time &newTime)
560 { time = newTime; }
562 /** checks for the validity of the timestamp
564 bool isValid() const { return date.isValid() && time.isValid(); }
566 /** resets the date and time */
567 void setNull() { date.setNull(); time.setNull(); }
569 /** returns day of the week. Values are 1-7
571 int dayOfWeek() const { return date.dayOfWeek(); }
573 /** returns day of the week.
574 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Satur
575 day".
577 const char *dayOfWeekName() const { return date.dayOfWeekName(); }
579 /** returns day of the week abbreviation
580 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
582 const char *dayOfWeekAbbr() const { return date.dayOfWeekAbbr(); }
584 /** returns the day of the month. Values are 1 to 31
586 int dayOfMonth() const { return date.dayOfMonth(); }
587 int dayOfYear() const { return date.dayOfYear(); }
589 /** returns the month. Values are 1 to 12.
592 int month() const { return date.month(); }
594 /** returns the month name
595 * values are "January", "February", "March", "April", "May", "June",
596 * "July", "August", "September", "October", "November", "December"
598 const char *monthName() const { return date.monthName(); }
600 /** returns the month name abbreviation
601 * Values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
602 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
604 const char *monthAbbr() const { return date.monthAbbr(); }
606 /** returns the year
608 int year() const { return date.year(); }
610 int secondsSinceMidnight() const { return time.secondsSinceMidnight(); }
611 /** returns the seconds */
612 int seconds() const { return time.seconds(); }
613 /** returns the minutes */
614 int minutes() const { return time.minutes(); }
615 /** returns the hours */
616 int hours() const { return time.hours(); }
617 /** returns the millisecs */
618 int msec() const { return time.msec(); }
619 /** returns the microsecs */
620 int usec() const { return time.usec(); }
622 /** sets the millisecs */
623 int setMsec(int ms) { return time.setMsec(ms) ; }
624 /** sets the microsecs */
625 int setUsec(int us) { return time.setUsec(us) ; }
627 /** parses the date string passed and stores it
628 *It should of the format "mm/dd/yyyy"
630 int parseDateFrom(const char *s) { return date.parseFrom(s); }
632 /** parses the time string passed and stores it
633 *It should of the format "hh:mm::ss"
636 int parseTimeFrom(const char *s) { return time.parseFrom(s); }
638 int parseFrom(const char *s);
639 friend int operator<(const TimeStamp &d1, const TimeStamp &d2);
640 friend int operator>(const TimeStamp &d1, const TimeStamp &d2);
641 friend int operator<=(const TimeStamp &d1, const TimeStamp &d2);
642 friend int operator>=(const TimeStamp &d1, const TimeStamp &d2);
643 friend int operator==(const TimeStamp &d1, const TimeStamp &d2);
644 friend int operator!=(const TimeStamp &d1, const TimeStamp &d2);
647 private:
648 Date date;
649 Time time;
653 #endif