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 ***************************************************************************/
20 typedef int JulianRep
;
25 * Data Types supported by the database system.
27 * @author Prabakaran Thirumalai
30 typeInt
= 0, /**<integer type*/
52 * @class ComparisionOp
53 * Comparision operators supported by the database system.
55 * @author Prabakaran Thirumalai
69 * Logical operators supported by the database system.
71 * @author Prabakaran Thirumalai
83 static long size(DataType type
);
84 static void copyVal(void* dest
, void *src
, DataType type
, int length
= 0);
86 static bool compareVal(void *src1
, void *src2
, ComparisionOp op
,
87 DataType type
, long length
= 0);
88 static bool compareIntVal(void* src1
, void* src2
, ComparisionOp op
);
89 static bool compareLongVal(void* src1
, void* src2
, ComparisionOp op
);
90 static bool compareLongLongVal(void* src1
, void* src2
, ComparisionOp op
);
91 static bool compareShortVal(void* src1
, void* src2
, ComparisionOp op
);
92 static bool compareByteIntVal(void* src1
, void* src2
, ComparisionOp op
);
93 static bool compareDoubleVal(void* src1
, void* src2
, ComparisionOp op
);
94 static bool compareFloatVal(void* src1
, void* src2
, ComparisionOp op
);
95 static bool compareDateVal(void* src1
, void* src2
, ComparisionOp op
);
96 static bool compareTimeVal(void* src1
, void* src2
, ComparisionOp op
);
97 static bool compareTimeStampVal(void* src1
, void* src2
, ComparisionOp op
);
98 static bool compareStringVal(void* src1
, void* src2
, ComparisionOp op
);
99 static bool compareBinaryVal(void* src1
, void* src2
,
100 ComparisionOp op
, int length
);
102 static ComparisionOp
getComparisionOperator(char *str
);
104 static void* alloc(DataType type
);
105 static void strToValue(void *dest
, char *src
, DataType type
);
113 * Represents 8 bit integer.
115 * @author Prabakaran Thirumalai
124 ByteInt(const ByteInt
&v
) { val
= v
.val
; }
125 /** constructor with char
126 * @param v char value
128 ByteInt(char v
) { val
= v
; }
129 operator int() const { return (int) val
; }
130 char operator=(ByteInt v
) { return val
= v
.val
; }
131 char operator=(char v
) { return val
= v
; }
132 char operator+=(ByteInt v
) { return val
+= v
.val
; }
133 char operator+=(char v
) { return val
+= v
; }
134 char operator-=(ByteInt v
) { return val
-= v
.val
; }
135 char operator-=(char v
) { return val
-= v
; }
136 char operator*=(ByteInt v
) { return val
*= v
.val
; }
137 char operator*=(char v
) { return val
*= v
; }
138 char operator/=(ByteInt v
) { return val
/= v
.val
; }
139 char operator/=(char v
) { return val
/= v
; }
140 char operator%=(ByteInt v
) { return val
%= v
.val
; }
141 char operator%=(char v
) { return val
%= v
; }
142 char operator<<=(ByteInt v
) { return val
<<= v
.val
; }
143 char operator<<=(char v
) { return val
<<= v
; }
144 char operator>>=(ByteInt v
) { return val
>>= v
.val
; }
145 char operator>>=(char v
) { return val
>>= v
; }
146 char operator&=(ByteInt v
) { return val
&= v
.val
; }
147 char operator&=(char v
) { return val
&= v
; }
148 char operator|=(ByteInt v
) { return val
|= v
.val
; }
149 char operator|=(char v
) { return val
|= v
; }
150 char operator^=(ByteInt v
) { return val
^= v
.val
; }
151 char operator^=(char v
) { return val
^= v
; }
152 char operator++() { return val
++; }
153 char operator--() { return val
--; }
162 * Represents Date Data type.
164 * @author Prabakaran Thirumalai
166 class Date
{ // The class a user would declare to hold date
169 Date() {julianDate
= 0;}
170 Date(JulianRep julian
) : julianDate(julian
) {}
172 /** constructor with year, month, day
177 Date(int year
, int month
, int day
);
179 Date(const Date
&d2
) { julianDate
= d2
.julianDate
; }
180 Date
& operator=(const Date
& d2
)
181 { julianDate
=d2
.julianDate
; return *this; }
183 /** sets the date with specified year, month, day
188 int set(int year
, int month
, int day
);
189 int set(const struct tm
*tmptr
);
191 /** get year, month, day of the date
192 * @param year year IN
193 * @param month month IN
196 int get(int &year
, int &month
, int &day
) const;
198 /** checks for the validity of the date
200 bool isValid() const;
202 /** resets the date to zero
204 void setNull() { julianDate
= 0;}
206 /** returns day of the week
208 int dayOfWeek() const;
210 /** returns day of the week.
211 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
213 const char *dayOfWeekName() const;
215 /** returns day of the week abbreviation
216 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
218 const char *dayOfWeekAbbr() const;
220 /** returns day of the week.
221 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
223 static const char *dayOfWeekName(int day
); // 0--> Sunday
225 /** returns day of the week abbreviation
226 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
228 static const char *dayOfWeekAbbr(int day
);
230 static int dayOfWeek(JulianRep juldate
);
233 /** returns the day of the month. Values are 1 to 31
235 int dayOfMonth() const;
237 int dayOfYear() const;
239 /** returns the month Values are 1 to 12.
243 /** returns the month name
244 * values are "January", "February", "March", "April", "May", "June",
245 * "July", "August", "September", "October", "November", "December"
247 const char *monthName() const;
249 /** returns the month name abbreviation
250 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
251 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
253 const char *monthAbbr() const;
255 /** returns the month name
256 * values are "January", "February", "March", "April", "May", "June",
257 * "July", "August", "September", "October", "November", "December"
259 static const char *monthName(int month
);
262 /** returns the month name abbreviation
263 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
264 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
266 static const char *monthAbbr(int month
);
268 /** parses the date string passed and stores it
269 *It should of the format "mm/dd/yyyy"
271 int parseFrom(const char *s
);
273 Date
&operator++() { julianDate
++; return *this; }
274 Date
&operator--() { julianDate
--; return *this; }
276 Date
&operator+=(int days
) { julianDate
+= days
; return *this;}
277 Date
&operator-=(int days
) { julianDate
-= days
; return *this;}
283 /** checks for the validity of the date
285 static bool isValidDate(int year
, int month
, int day
);
287 friend Date
operator+(const Date
&d1
, int days
);
288 friend Date
operator+(int days
, const Date
&d1
);
289 friend Date
operator-(const Date
&d1
, int days
);
290 friend int operator-(const Date
&d1
, const Date
& d2
);
291 friend int operator<(const Date
&d1
,const Date
&d2
);
292 friend int operator>(const Date
&d1
,const Date
&d2
);
293 friend int operator<=(const Date
&d1
,const Date
&d2
);
294 friend int operator>=(const Date
&d1
,const Date
&d2
);
295 friend int operator==(const Date
&d1
,const Date
&d2
);
296 friend int operator!=(const Date
&d1
,const Date
&d2
);
298 /** checks for leap year
300 static bool isLeapYear(int year
);
302 /** returns the number of days in the specified month of the year.
304 static int daysInMonth(int month
, int year
);
306 static int YMDToJulian(int year
,int mon
,int day
, JulianRep
&julian
);
307 static int julianToYMD(JulianRep julian
,int &year
,int &month
,int &day
);
310 JulianRep julianDate
;
318 * Represents Time Data type.
320 * @author Prabakaran Thirumalai
322 class Time
{ // The class a user would declare to hold time
324 Time() {timeVal
= 0;}
326 /** Overloaded constructor
332 Time(int hours
, int mins
, int secs
, int usec
= 0);
333 Time(int totalSecs
) : timeVal(totalSecs
) {;}
334 Time(const Time
&d2
) { timeVal
= d2
.timeVal
; }
335 Time
& operator=(const Time
& d2
) { timeVal
=d2
.timeVal
; return *this; }
337 /** sets the time with specified hours, mins, secs
343 int set(int hours
, int mins
, int secs
, int usec
= 0);
345 /** retrieves the time using IN parameters
350 int get(int &hours
, int &mins
, int &secs
) const;
352 /** checks for the validity of the time
354 bool isValid() const;
358 void setNull() { timeVal
= -1;}
360 int secondsSinceMidnight() const { return timeVal
/10000;}
362 /** returns the microsecs
364 int usec() const; // to nearest 100 of usec.
366 /** returns the millisecs
374 /** returns the minutes
378 /** returns the hours
383 /** sets the millisecs
387 /** sets the microsecs
391 /** parses the time string passed and stores it
392 *It should of the format "hh:mm::ss"
394 int parseFrom(const char *s
);
396 Time
&operator++() { timeVal
+= 10000; return *this; }
397 Time
&operator--() { timeVal
-= 10000; return *this; }
399 Time
&operator+=(int seconds
) { timeVal
+= seconds
*10000; return *this; }
400 Time
&operator-=(int seconds
) { timeVal
-= seconds
*10000; return *this; }
403 /** checks for the validity of the time specified
405 static bool isValidTime(int hours
, int mins
, int secs
);
407 friend Time
operator+(const Time
&t1
, int seconds
);
408 friend Time
operator+(int seconds
, const Time
&t1
);
409 friend Time
operator-(const Time
&t1
, int seconds
);
410 friend int operator-(const Time
&t1
, const Time
& t2
);
411 friend int operator<(const Time
&t1
,const Time
&t2
);
412 friend int operator>(const Time
&t1
,const Time
&t2
);
413 friend int operator<=(const Time
&t1
,const Time
&t2
);
414 friend int operator>=(const Time
&t1
,const Time
&t2
);
415 friend int operator==(const Time
&t1
,const Time
&t2
);
416 friend int operator!=(const Time
&t1
,const Time
&t2
);
425 * Represents TimeStamp Data type.
427 * @author Prabakaran Thirumalai
434 /** Overloaded constructor
443 TimeStamp(int year
, int month
, int day
, int hour
, int minute
, int sec
, int usec
= 0) :
444 date(year
, month
, day
), time(hour
, minute
, sec
, usec
) { }
446 TimeStamp(const TimeStamp
&ts
)
447 { date
= ts
.date
; time
= ts
.time
; }
448 TimeStamp(const Date
&d
, Time
&t
) : date(d
), time(t
) {}
451 TimeStamp
& operator=(const TimeStamp
& d2
)
452 { date
=d2
.date
; time
= d2
.time
; return *this; }
454 /** get year, month, day from the date part of the timestamp
455 * @param year year IN
456 * @param month month IN
459 int getDate(int &year
, int &month
, int &day
)
460 { return date
.get(year
, month
, day
); }
462 /** get the date part of the timestamp
465 void getDate(Date
&newDate
) const
468 /** sets the date with specified year, month, day
473 int setDate(int year
, int month
, int day
)
474 { return date
.set(year
, month
, day
); }
476 /** set the date part of the timestamp
479 void setDate(const Date
&newDate
)
483 operator Date() { return date
; }
484 operator Time() { return time
; }
488 /** retrieves the time using IN parameters
493 int getTime(int &hours
, int &mins
, int &secs
) const
494 { return time
.get(hours
, mins
, secs
); }
495 /** retrieves the time part of the timestamp
496 * @param newTime Time
498 void getTime(Time
&newTime
) const
501 /** sets the time with specified hours, mins, secs
507 int setTime(int hours
, int mins
, int secs
, int usec
= 0)
508 { return time
.set(hours
, mins
, secs
, usec
); }
510 /** set the time part of the timestamp
511 * @param newTime Time
513 void setTime(const Time
&newTime
)
516 /** checks for the validity of the timestamp
518 bool isValid() const { return date
.isValid() && time
.isValid(); }
520 /** resets the date and time */
521 void setNull() { date
.setNull(); time
.setNull(); }
523 /** returns day of the week. Values are 1-7
525 int dayOfWeek() const { return date
.dayOfWeek(); }
527 /** returns day of the week.
528 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Satur
531 const char *dayOfWeekName() const { return date
.dayOfWeekName(); }
533 /** returns day of the week abbreviation
534 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
536 const char *dayOfWeekAbbr() const { return date
.dayOfWeekAbbr(); }
538 /** returns the day of the month. Values are 1 to 31
540 int dayOfMonth() const { return date
.dayOfMonth(); }
541 int dayOfYear() const { return date
.dayOfYear(); }
543 /** returns the month. Values are 1 to 12.
546 int month() const { return date
.month(); }
548 /** returns the month name
549 * values are "January", "February", "March", "April", "May", "June",
550 * "July", "August", "September", "October", "November", "December"
552 const char *monthName() const { return date
.monthName(); }
554 /** returns the month name abbreviation
555 * Values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
556 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
558 const char *monthAbbr() const { return date
.monthAbbr(); }
562 int year() const { return date
.year(); }
564 int secondsSinceMidnight() const { return time
.secondsSinceMidnight(); }
565 /** returns the seconds */
566 int seconds() const { return time
.seconds(); }
567 /** returns the minutes */
568 int minutes() const { return time
.minutes(); }
569 /** returns the hours */
570 int hours() const { return time
.hours(); }
571 /** returns the millisecs */
572 int msec() const { return time
.msec(); }
573 /** returns the microsecs */
574 int usec() const { return time
.usec(); }
576 /** sets the millisecs */
577 int setMsec(int ms
) { return time
.setMsec(ms
) ; }
578 /** sets the microsecs */
579 int setUsec(int us
) { return time
.setUsec(us
) ; }
581 /** parses the date string passed and stores it
582 *It should of the format "mm/dd/yyyy"
584 int parseDateFrom(const char *s
) { return date
.parseFrom(s
); }
586 /** parses the time string passed and stores it
587 *It should of the format "hh:mm::ss"
590 int parseTimeFrom(const char *s
) { return time
.parseFrom(s
); }
592 friend int operator<(const TimeStamp
&d1
, const TimeStamp
&d2
);
593 friend int operator>(const TimeStamp
&d1
, const TimeStamp
&d2
);
594 friend int operator<=(const TimeStamp
&d1
, const TimeStamp
&d2
);
595 friend int operator>=(const TimeStamp
&d1
, const TimeStamp
&d2
);
596 friend int operator==(const TimeStamp
&d1
, const TimeStamp
&d2
);
597 friend int operator!=(const TimeStamp
&d1
, const TimeStamp
&d2
);