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);
93 static bool compareVal(void *src1
, void *src2
, ComparisionOp op
,
94 DataType type
, long length
= 0);
95 static bool compareIntVal(void* src1
, void* src2
, ComparisionOp op
);
96 static bool compareLongVal(void* src1
, void* src2
, ComparisionOp op
);
97 static bool compareLongLongVal(void* src1
, void* src2
, ComparisionOp op
);
98 static bool compareShortVal(void* src1
, void* src2
, ComparisionOp op
);
99 static bool compareByteIntVal(void* src1
, void* src2
, ComparisionOp op
);
100 static bool compareDoubleVal(void* src1
, void* src2
, ComparisionOp op
);
101 static bool compareFloatVal(void* src1
, void* src2
, ComparisionOp op
);
102 static bool compareDateVal(void* src1
, void* src2
, ComparisionOp op
);
103 static bool compareTimeVal(void* src1
, void* src2
, ComparisionOp op
);
104 static bool compareTimeStampVal(void* src1
, void* src2
, ComparisionOp op
);
105 static bool compareStringVal(void* src1
, void* src2
, ComparisionOp op
);
106 static bool compareBinaryVal(void* src1
, void* src2
,
107 ComparisionOp op
, int length
);
110 static void convert(DataType srcType
, void *src
, DataType destType
, void *dest
);
111 static void convertToInt(void* dest
, void* src
, DataType srcType
);
112 static void convertToLong(void* dest
, void* src
, DataType srcType
);
113 static void convertToLongLong(void* dest
, void* src
, DataType srcType
);
114 static void convertToShort(void* dest
, void* src
, DataType srcType
);
115 static void convertToByteInt(void* dest
, void* src
, DataType srcType
);
116 static void convertToFloat(void* dest
, void* src
, DataType srcType
);
117 static void convertToDouble(void* dest
, void* src
, DataType srcType
);
118 static void convertToString(void* dest
, void* src
, DataType srcType
);
119 static void convertToDate(void* dest
, void* src
, DataType srcType
);
120 static void convertToTime(void* dest
, void* src
, DataType srcType
);
121 static void convertToTimeStamp(void* dest
, void* src
, DataType srcType
);
124 static ComparisionOp
getComparisionOperator(char *str
);
126 static void* alloc(DataType type
, int length
=0);
127 static void strToValue(void *dest
, char *src
, DataType type
, int length
=0);
128 static void printVal(void *src
, DataType type
, int length
);
136 * Represents 8 bit integer.
138 * @author Prabakaran Thirumalai
147 ByteInt(const ByteInt
&v
) { val
= v
.val
; }
148 /** constructor with char
149 * @param v char value
151 ByteInt(char v
) { val
= v
; }
152 operator int() const { return (int) val
; }
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<<=(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++() { return val
++; }
176 char operator++(int) { char tmp
= val
; val
++; return val
; }
177 char operator--() { return val
--; }
178 char operator--(int) { char tmp
= val
; val
--; return val
; }
187 * Represents Date Data type.
189 * @author Prabakaran Thirumalai
191 class Date
{ // The class a user would declare to hold date
194 Date() {julianDate
= 0;}
195 Date(JulianRep julian
) : julianDate(julian
) {}
197 /** constructor with year, month, day
202 Date(int year
, int month
, int day
);
204 Date(const Date
&d2
) { julianDate
= d2
.julianDate
; }
205 Date
& operator=(const Date
& d2
)
206 { julianDate
=d2
.julianDate
; return *this; }
208 /** sets the date with specified year, month, day
213 int set(int year
, int month
, int day
);
214 int set(const struct tm
*tmptr
);
216 /** get year, month, day of the date
217 * @param year year IN
218 * @param month month IN
221 int get(int &year
, int &month
, int &day
) const;
223 /** checks for the validity of the date
225 bool isValid() const;
227 /** resets the date to zero
229 void setNull() { julianDate
= 0;}
231 /** returns day of the week
233 int dayOfWeek() const;
235 /** returns day of the week.
236 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
238 const char *dayOfWeekName() const;
240 /** returns day of the week abbreviation
241 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
243 const char *dayOfWeekAbbr() const;
245 /** returns day of the week.
246 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
248 static const char *dayOfWeekName(int day
); // 0--> Sunday
250 /** returns day of the week abbreviation
251 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
253 static const char *dayOfWeekAbbr(int day
);
255 static int dayOfWeek(JulianRep juldate
);
258 /** returns the day of the month. Values are 1 to 31
260 int dayOfMonth() const;
262 int dayOfYear() const;
264 /** returns the month Values are 1 to 12.
268 /** returns the month name
269 * values are "January", "February", "March", "April", "May", "June",
270 * "July", "August", "September", "October", "November", "December"
272 const char *monthName() const;
274 /** returns the month name abbreviation
275 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
276 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
278 const char *monthAbbr() const;
280 /** returns the month name
281 * values are "January", "February", "March", "April", "May", "June",
282 * "July", "August", "September", "October", "November", "December"
284 static const char *monthName(int month
);
287 /** returns the month name abbreviation
288 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
289 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
291 static const char *monthAbbr(int month
);
293 /** parses the date string passed and stores it
294 *It should of the format "mm/dd/yyyy"
296 int parseFrom(const char *s
);
298 Date
&operator++() { julianDate
++; return *this; }
299 Date
&operator--() { julianDate
--; return *this; }
301 Date
&operator+=(int days
) { julianDate
+= days
; return *this;}
302 Date
&operator-=(int days
) { julianDate
-= days
; return *this;}
308 /** checks for the validity of the date
310 static bool isValidDate(int year
, int month
, int day
);
312 friend Date
operator+(const Date
&d1
, int days
);
313 friend Date
operator+(int days
, const Date
&d1
);
314 friend Date
operator-(const Date
&d1
, int days
);
315 friend int operator-(const Date
&d1
, const Date
& d2
);
316 friend int operator<(const Date
&d1
,const Date
&d2
);
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
);
323 /** checks for leap year
325 static bool isLeapYear(int year
);
327 /** returns the number of days in the specified month of the year.
329 static int daysInMonth(int month
, int year
);
331 static int YMDToJulian(int year
,int mon
,int day
, JulianRep
&julian
);
332 static int julianToYMD(JulianRep julian
,int &year
,int &month
,int &day
);
335 JulianRep julianDate
;
343 * Represents Time Data type.
345 * @author Prabakaran Thirumalai
347 class Time
{ // The class a user would declare to hold time
349 Time() {timeVal
= 0;}
351 /** Overloaded constructor
357 Time(int hours
, int mins
, int secs
, int usec
= 0);
358 Time(int totalSecs
) : timeVal(totalSecs
) {;}
359 Time(const Time
&d2
) { timeVal
= d2
.timeVal
; }
360 Time
& operator=(const Time
& d2
) { timeVal
=d2
.timeVal
; return *this; }
362 /** sets the time with specified hours, mins, secs
368 int set(int hours
, int mins
, int secs
, int usec
= 0);
370 /** retrieves the time using IN parameters
375 int get(int &hours
, int &mins
, int &secs
) const;
377 /** checks for the validity of the time
379 bool isValid() const;
383 void setNull() { timeVal
= -1;}
385 int secondsSinceMidnight() const { return timeVal
/10000;}
387 /** returns the microsecs
389 int usec() const; // to nearest 100 of usec.
391 /** returns the millisecs
399 /** returns the minutes
403 /** returns the hours
408 /** sets the millisecs
412 /** sets the microsecs
416 /** parses the time string passed and stores it
417 *It should of the format "hh:mm::ss"
419 int parseFrom(const char *s
);
421 Time
&operator++() { timeVal
+= 10000; return *this; }
422 Time
&operator--() { timeVal
-= 10000; return *this; }
424 Time
&operator+=(int seconds
) { timeVal
+= seconds
*10000; return *this; }
425 Time
&operator-=(int seconds
) { timeVal
-= seconds
*10000; return *this; }
428 /** checks for the validity of the time specified
430 static bool isValidTime(int hours
, int mins
, int secs
);
432 friend Time
operator+(const Time
&t1
, int seconds
);
433 friend Time
operator+(int seconds
, const Time
&t1
);
434 friend Time
operator-(const Time
&t1
, int seconds
);
435 friend int operator-(const Time
&t1
, const Time
& t2
);
436 friend int operator<(const Time
&t1
,const Time
&t2
);
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
);
450 * Represents TimeStamp Data type.
452 * @author Prabakaran Thirumalai
459 /** Overloaded constructor
468 TimeStamp(int year
, int month
, int day
, int hour
, int minute
, int sec
, int usec
= 0) :
469 date(year
, month
, day
), time(hour
, minute
, sec
, usec
) { }
471 TimeStamp(const TimeStamp
&ts
)
472 { date
= ts
.date
; time
= ts
.time
; }
473 TimeStamp(const Date
&d
, Time
&t
) : date(d
), time(t
) {}
476 TimeStamp
& operator=(const TimeStamp
& d2
)
477 { date
=d2
.date
; time
= d2
.time
; return *this; }
479 /** get year, month, day from the date part of the timestamp
480 * @param year year IN
481 * @param month month IN
484 int getDate(int &year
, int &month
, int &day
)
485 { return date
.get(year
, month
, day
); }
487 /** get the date part of the timestamp
490 void getDate(Date
&newDate
) const
493 /** sets the date with specified year, month, day
498 int setDate(int year
, int month
, int day
)
499 { return date
.set(year
, month
, day
); }
501 /** set the date part of the timestamp
504 void setDate(const Date
&newDate
)
508 operator Date() { return date
; }
509 operator Time() { return time
; }
513 /** retrieves the time using IN parameters
518 int getTime(int &hours
, int &mins
, int &secs
) const
519 { return time
.get(hours
, mins
, secs
); }
520 /** retrieves the time part of the timestamp
521 * @param newTime Time
523 void getTime(Time
&newTime
) const
526 /** sets the time with specified hours, mins, secs
532 int setTime(int hours
, int mins
, int secs
, int usec
= 0)
533 { return time
.set(hours
, mins
, secs
, usec
); }
535 /** set the time part of the timestamp
536 * @param newTime Time
538 void setTime(const Time
&newTime
)
541 /** checks for the validity of the timestamp
543 bool isValid() const { return date
.isValid() && time
.isValid(); }
545 /** resets the date and time */
546 void setNull() { date
.setNull(); time
.setNull(); }
548 /** returns day of the week. Values are 1-7
550 int dayOfWeek() const { return date
.dayOfWeek(); }
552 /** returns day of the week.
553 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Satur
556 const char *dayOfWeekName() const { return date
.dayOfWeekName(); }
558 /** returns day of the week abbreviation
559 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
561 const char *dayOfWeekAbbr() const { return date
.dayOfWeekAbbr(); }
563 /** returns the day of the month. Values are 1 to 31
565 int dayOfMonth() const { return date
.dayOfMonth(); }
566 int dayOfYear() const { return date
.dayOfYear(); }
568 /** returns the month. Values are 1 to 12.
571 int month() const { return date
.month(); }
573 /** returns the month name
574 * values are "January", "February", "March", "April", "May", "June",
575 * "July", "August", "September", "October", "November", "December"
577 const char *monthName() const { return date
.monthName(); }
579 /** returns the month name abbreviation
580 * Values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
581 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
583 const char *monthAbbr() const { return date
.monthAbbr(); }
587 int year() const { return date
.year(); }
589 int secondsSinceMidnight() const { return time
.secondsSinceMidnight(); }
590 /** returns the seconds */
591 int seconds() const { return time
.seconds(); }
592 /** returns the minutes */
593 int minutes() const { return time
.minutes(); }
594 /** returns the hours */
595 int hours() const { return time
.hours(); }
596 /** returns the millisecs */
597 int msec() const { return time
.msec(); }
598 /** returns the microsecs */
599 int usec() const { return time
.usec(); }
601 /** sets the millisecs */
602 int setMsec(int ms
) { return time
.setMsec(ms
) ; }
603 /** sets the microsecs */
604 int setUsec(int us
) { return time
.setUsec(us
) ; }
606 /** parses the date string passed and stores it
607 *It should of the format "mm/dd/yyyy"
609 int parseDateFrom(const char *s
) { return date
.parseFrom(s
); }
611 /** parses the time string passed and stores it
612 *It should of the format "hh:mm::ss"
615 int parseTimeFrom(const char *s
) { return time
.parseFrom(s
); }
617 int parseFrom(const char *s
);
618 friend int operator<(const TimeStamp
&d1
, const TimeStamp
&d2
);
619 friend int operator>(const TimeStamp
&d1
, const TimeStamp
&d2
);
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
);