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 ***************************************************************************/
21 typedef int JulianRep
;
26 * Data Types supported by the database system.
28 * @author Prabakaran Thirumalai
31 typeInt
= 0, /**<integer type*/
53 * @class ComparisionOp
54 * Comparision operators supported by the database system.
56 * @author Prabakaran Thirumalai
65 OpInvalidComparisionOp
70 * Logical operators supported by the database system.
72 * @author Prabakaran Thirumalai
85 static long size(DataType type
, int length
=0);
86 static char* getSQLString(DataType type
);
87 static SQLSMALLINT
convertToSQLType(DataType type
);
88 static SQLSMALLINT
convertToSQL_C_Type(DataType type
);
89 static DataType
convertFromSQLType(SQLSMALLINT type
);
91 static void copyVal(void* dest
, void *src
, DataType type
, int length
= 0);
92 static void addVal(void* dest
, void *src
, DataType type
);
93 static void divVal(void* dest
, int src
, DataType type
);
95 static bool compareVal(void *src1
, void *src2
, ComparisionOp op
,
96 DataType type
, long length
= 0);
97 static bool compareIntVal(void* src1
, void* src2
, ComparisionOp op
);
98 static bool compareLongVal(void* src1
, void* src2
, ComparisionOp op
);
99 static bool compareLongLongVal(void* src1
, void* src2
, ComparisionOp op
);
100 static bool compareShortVal(void* src1
, void* src2
, ComparisionOp op
);
101 static bool compareByteIntVal(void* src1
, void* src2
, ComparisionOp op
);
102 static bool compareDoubleVal(void* src1
, void* src2
, ComparisionOp op
);
103 static bool compareFloatVal(void* src1
, void* src2
, ComparisionOp op
);
104 static bool compareDateVal(void* src1
, void* src2
, ComparisionOp op
);
105 static bool compareTimeVal(void* src1
, void* src2
, ComparisionOp op
);
106 static bool compareTimeStampVal(void* src1
, void* src2
, ComparisionOp op
);
107 static bool compareStringVal(void* src1
, void* src2
, ComparisionOp op
);
108 static bool compareBinaryVal(void* src1
, void* src2
,
109 ComparisionOp op
, int length
);
112 static void convert(DataType srcType
, void *src
, DataType destType
, void *dest
);
113 static void convertToInt(void* dest
, void* src
, DataType srcType
);
114 static void convertToLong(void* dest
, void* src
, DataType srcType
);
115 static void convertToLongLong(void* dest
, void* src
, DataType srcType
);
116 static void convertToShort(void* dest
, void* src
, DataType srcType
);
117 static void convertToByteInt(void* dest
, void* src
, DataType srcType
);
118 static void convertToFloat(void* dest
, void* src
, DataType srcType
);
119 static void convertToDouble(void* dest
, void* src
, DataType srcType
);
120 static void convertToString(void* dest
, void* src
, DataType srcType
);
121 static void convertToDate(void* dest
, void* src
, DataType srcType
);
122 static void convertToTime(void* dest
, void* src
, DataType srcType
);
123 static void convertToTimeStamp(void* dest
, void* src
, DataType srcType
);
126 static ComparisionOp
getComparisionOperator(char *str
);
128 static void* alloc(DataType type
, int length
=0);
129 static void strToValue(void *dest
, char *src
, DataType type
, int length
=0);
130 static int printVal(void *src
, DataType type
, int length
);
138 * Represents 8 bit integer.
140 * @author Prabakaran Thirumalai
149 ByteInt(const ByteInt
&v
) { val
= v
.val
; }
150 /** constructor with char
151 * @param v char value
153 ByteInt(char v
) { val
= v
; }
154 operator int() const { return (int) val
; }
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%=(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++() { return val
++; }
178 char operator++(int) { char tmp
= val
; val
++; return val
; }
179 char operator--() { return val
--; }
180 char operator--(int) { char tmp
= val
; val
--; return val
; }
189 * Represents Date Data type.
191 * @author Prabakaran Thirumalai
193 class Date
{ // The class a user would declare to hold date
196 Date() {julianDate
= 0;}
197 Date(JulianRep julian
) : julianDate(julian
) {}
199 /** constructor with year, month, day
204 Date(int year
, int month
, int day
);
206 Date(const Date
&d2
) { julianDate
= d2
.julianDate
; }
207 Date
& operator=(const Date
& d2
)
208 { julianDate
=d2
.julianDate
; return *this; }
210 /** sets the date with specified year, month, day
215 int set(int year
, int month
, int day
);
216 int set(const struct tm
*tmptr
);
218 /** get year, month, day of the date
219 * @param year year IN
220 * @param month month IN
223 int get(int &year
, int &month
, int &day
) const;
225 /** checks for the validity of the date
227 bool isValid() const;
229 /** resets the date to zero
231 void setNull() { julianDate
= 0;}
233 /** returns day of the week
235 int dayOfWeek() const;
237 /** returns day of the week.
238 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
240 const char *dayOfWeekName() const;
242 /** returns day of the week abbreviation
243 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
245 const char *dayOfWeekAbbr() const;
247 /** returns day of the week.
248 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
250 static const char *dayOfWeekName(int day
); // 0--> Sunday
252 /** returns day of the week abbreviation
253 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
255 static const char *dayOfWeekAbbr(int day
);
257 static int dayOfWeek(JulianRep juldate
);
260 /** returns the day of the month. Values are 1 to 31
262 int dayOfMonth() const;
264 int dayOfYear() const;
266 /** returns the month Values are 1 to 12.
270 /** returns the month name
271 * values are "January", "February", "March", "April", "May", "June",
272 * "July", "August", "September", "October", "November", "December"
274 const char *monthName() const;
276 /** returns the month name abbreviation
277 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
278 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
280 const char *monthAbbr() const;
282 /** returns the month name
283 * values are "January", "February", "March", "April", "May", "June",
284 * "July", "August", "September", "October", "November", "December"
286 static const char *monthName(int month
);
289 /** returns the month name abbreviation
290 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
291 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
293 static const char *monthAbbr(int month
);
295 /** parses the date string passed and stores it
296 *It should of the format "mm/dd/yyyy"
298 int parseFrom(const char *s
);
300 Date
&operator++() { julianDate
++; return *this; }
301 Date
&operator--() { julianDate
--; return *this; }
303 Date
&operator+=(int days
) { julianDate
+= days
; return *this;}
304 Date
&operator-=(int days
) { julianDate
-= days
; return *this;}
310 /** checks for the validity of the date
312 static bool isValidDate(int year
, int month
, int day
);
314 friend Date
operator+(const Date
&d1
, int days
);
315 friend Date
operator+(int days
, const Date
&d1
);
316 friend Date
operator-(const Date
&d1
, int days
);
317 friend int operator-(const Date
&d1
, const Date
& d2
);
318 friend int operator<(const Date
&d1
,const Date
&d2
);
319 friend int operator>(const Date
&d1
,const Date
&d2
);
320 friend int operator<=(const Date
&d1
,const Date
&d2
);
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
);
325 /** checks for leap year
327 static bool isLeapYear(int year
);
329 /** returns the number of days in the specified month of the year.
331 static int daysInMonth(int month
, int year
);
333 static int YMDToJulian(int year
,int mon
,int day
, JulianRep
&julian
);
334 static int julianToYMD(JulianRep julian
,int &year
,int &month
,int &day
);
337 JulianRep julianDate
;
345 * Represents Time Data type.
347 * @author Prabakaran Thirumalai
349 class Time
{ // The class a user would declare to hold time
351 Time() {timeVal
= 0;}
353 /** Overloaded constructor
359 Time(int hours
, int mins
, int secs
, int usec
= 0);
360 Time(int totalSecs
) : timeVal(totalSecs
) {;}
361 Time(const Time
&d2
) { timeVal
= d2
.timeVal
; }
362 Time
& operator=(const Time
& d2
) { timeVal
=d2
.timeVal
; return *this; }
364 /** sets the time with specified hours, mins, secs
370 int set(int hours
, int mins
, int secs
, int usec
= 0);
372 /** retrieves the time using IN parameters
377 int get(int &hours
, int &mins
, int &secs
) const;
379 /** checks for the validity of the time
381 bool isValid() const;
385 void setNull() { timeVal
= -1;}
387 int secondsSinceMidnight() const { return timeVal
/10000;}
389 /** returns the microsecs
391 int usec() const; // to nearest 100 of usec.
393 /** returns the millisecs
401 /** returns the minutes
405 /** returns the hours
410 /** sets the millisecs
414 /** sets the microsecs
418 /** parses the time string passed and stores it
419 *It should of the format "hh:mm::ss"
421 int parseFrom(const char *s
);
423 Time
&operator++() { timeVal
+= 10000; return *this; }
424 Time
&operator--() { timeVal
-= 10000; return *this; }
426 Time
&operator+=(int seconds
) { timeVal
+= seconds
*10000; return *this; }
427 Time
&operator-=(int seconds
) { timeVal
-= seconds
*10000; return *this; }
430 /** checks for the validity of the time specified
432 static bool isValidTime(int hours
, int mins
, int secs
);
434 friend Time
operator+(const Time
&t1
, int seconds
);
435 friend Time
operator+(int seconds
, const Time
&t1
);
436 friend Time
operator-(const Time
&t1
, int seconds
);
437 friend int operator-(const Time
&t1
, const Time
& t2
);
438 friend int operator<(const Time
&t1
,const Time
&t2
);
439 friend int operator>(const Time
&t1
,const Time
&t2
);
440 friend int operator<=(const Time
&t1
,const Time
&t2
);
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
);
452 * Represents TimeStamp Data type.
454 * @author Prabakaran Thirumalai
461 /** Overloaded constructor
470 TimeStamp(int year
, int month
, int day
, int hour
, int minute
, int sec
, int usec
= 0) :
471 date(year
, month
, day
), time(hour
, minute
, sec
, usec
) { }
473 TimeStamp(const TimeStamp
&ts
)
474 { date
= ts
.date
; time
= ts
.time
; }
475 TimeStamp(const Date
&d
, Time
&t
) : date(d
), time(t
) {}
478 TimeStamp
& operator=(const TimeStamp
& d2
)
479 { date
=d2
.date
; time
= d2
.time
; return *this; }
481 /** get year, month, day from the date part of the timestamp
482 * @param year year IN
483 * @param month month IN
486 int getDate(int &year
, int &month
, int &day
)
487 { return date
.get(year
, month
, day
); }
489 /** get the date part of the timestamp
492 void getDate(Date
&newDate
) const
495 /** sets the date with specified year, month, day
500 int setDate(int year
, int month
, int day
)
501 { return date
.set(year
, month
, day
); }
503 /** set the date part of the timestamp
506 void setDate(const Date
&newDate
)
510 operator Date() { return date
; }
511 operator Time() { return time
; }
515 /** retrieves the time using IN parameters
520 int getTime(int &hours
, int &mins
, int &secs
) const
521 { return time
.get(hours
, mins
, secs
); }
522 /** retrieves the time part of the timestamp
523 * @param newTime Time
525 void getTime(Time
&newTime
) const
528 /** sets the time with specified hours, mins, secs
534 int setTime(int hours
, int mins
, int secs
, int usec
= 0)
535 { return time
.set(hours
, mins
, secs
, usec
); }
537 /** set the time part of the timestamp
538 * @param newTime Time
540 void setTime(const Time
&newTime
)
543 /** checks for the validity of the timestamp
545 bool isValid() const { return date
.isValid() && time
.isValid(); }
547 /** resets the date and time */
548 void setNull() { date
.setNull(); time
.setNull(); }
550 /** returns day of the week. Values are 1-7
552 int dayOfWeek() const { return date
.dayOfWeek(); }
554 /** returns day of the week.
555 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Satur
558 const char *dayOfWeekName() const { return date
.dayOfWeekName(); }
560 /** returns day of the week abbreviation
561 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
563 const char *dayOfWeekAbbr() const { return date
.dayOfWeekAbbr(); }
565 /** returns the day of the month. Values are 1 to 31
567 int dayOfMonth() const { return date
.dayOfMonth(); }
568 int dayOfYear() const { return date
.dayOfYear(); }
570 /** returns the month. Values are 1 to 12.
573 int month() const { return date
.month(); }
575 /** returns the month name
576 * values are "January", "February", "March", "April", "May", "June",
577 * "July", "August", "September", "October", "November", "December"
579 const char *monthName() const { return date
.monthName(); }
581 /** returns the month name abbreviation
582 * Values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
583 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
585 const char *monthAbbr() const { return date
.monthAbbr(); }
589 int year() const { return date
.year(); }
591 int secondsSinceMidnight() const { return time
.secondsSinceMidnight(); }
592 /** returns the seconds */
593 int seconds() const { return time
.seconds(); }
594 /** returns the minutes */
595 int minutes() const { return time
.minutes(); }
596 /** returns the hours */
597 int hours() const { return time
.hours(); }
598 /** returns the millisecs */
599 int msec() const { return time
.msec(); }
600 /** returns the microsecs */
601 int usec() const { return time
.usec(); }
603 /** sets the millisecs */
604 int setMsec(int ms
) { return time
.setMsec(ms
) ; }
605 /** sets the microsecs */
606 int setUsec(int us
) { return time
.setUsec(us
) ; }
608 /** parses the date string passed and stores it
609 *It should of the format "mm/dd/yyyy"
611 int parseDateFrom(const char *s
) { return date
.parseFrom(s
); }
613 /** parses the time string passed and stores it
614 *It should of the format "hh:mm::ss"
617 int parseTimeFrom(const char *s
) { return time
.parseFrom(s
); }
619 int parseFrom(const char *s
);
620 friend int operator<(const TimeStamp
&d1
, const TimeStamp
&d2
);
621 friend int operator>(const TimeStamp
&d1
, const TimeStamp
&d2
);
622 friend int operator<=(const TimeStamp
&d1
, const TimeStamp
&d2
);
623 friend int operator>=(const TimeStamp
&d1
, const TimeStamp
&d2
);
624 friend int operator==(const TimeStamp
&d1
, const TimeStamp
&d2
);
625 friend int operator!=(const TimeStamp
&d1
, const TimeStamp
&d2
);