1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
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. *
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. *
15 ***************************************************************************/
22 typedef int JulianRep
;
27 * Data Types supported by the database system.
32 typeInt
= 0, /**<integer type*/
55 * @class ComparisionOp
56 * Comparision operators supported by the database system.
67 OpLike
, // for Like operator
68 OpInvalidComparisionOp
73 * Logical operators supported by the database system.
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
);
134 static void memoryset(void *value
,DataType type
);
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
);
147 * Represents 8 bit integer.
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
; }
198 * Represents Date Data type.
202 class Date
{ // The class a user would declare to hold date
205 Date() {julianDate
= 0;}
206 Date(JulianRep julian
) : julianDate(julian
) {}
208 /** constructor with year, month, 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
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
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.
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;}
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
);
346 JulianRep julianDate
;
354 * Represents Time Data type.
358 class Time
{ // The class a user would declare to hold time
360 Time() {timeVal
= 0;}
362 /** Overloaded constructor
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
379 int set(int hours
, int mins
, int secs
, int usec
= 0);
381 /** retrieves the time using IN parameters
386 int get(int &hours
, int &mins
, int &secs
) const;
388 /** checks for the validity of the time
390 bool isValid() const;
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
410 /** returns the minutes
414 /** returns the hours
419 /** sets the millisecs
423 /** sets the microsecs
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
);
461 * Represents TimeStamp Data type.
470 /** Overloaded constructor
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
495 int getDate(int &year
, int &month
, int &day
)
496 { return date
.get(year
, month
, day
); }
498 /** get the date part of the timestamp
501 void getDate(Date
&newDate
) const
504 /** sets the date with specified year, month, 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
515 void setDate(const Date
&newDate
)
519 operator Date() { return date
; }
520 operator Time() { return time
; }
524 /** retrieves the time using IN parameters
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
537 /** sets the time with specified hours, mins, secs
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
)
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
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(); }
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
);