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
);
103 static void convert(DataType srcType
, void *src
, DataType destType
, void *dest
);
104 static void convertToInt(void* dest
, void* src
, DataType srcType
);
105 static void convertToLong(void* dest
, void* src
, DataType srcType
);
106 static void convertToLongLong(void* dest
, void* src
, DataType srcType
);
107 static void convertToShort(void* dest
, void* src
, DataType srcType
);
108 static void convertToByteInt(void* dest
, void* src
, DataType srcType
);
109 static void convertToFloat(void* dest
, void* src
, DataType srcType
);
110 static void convertToDouble(void* dest
, void* src
, DataType srcType
);
111 static void convertToString(void* dest
, void* src
, DataType srcType
);
114 static ComparisionOp
getComparisionOperator(char *str
);
116 static void* alloc(DataType type
);
117 static void strToValue(void *dest
, char *src
, DataType type
);
118 static void printVal(void *src
, DataType type
, int length
);
126 * Represents 8 bit integer.
128 * @author Prabakaran Thirumalai
137 ByteInt(const ByteInt
&v
) { val
= v
.val
; }
138 /** constructor with char
139 * @param v char value
141 ByteInt(char v
) { val
= v
; }
142 operator int() const { return (int) val
; }
143 char operator=(ByteInt v
) { return val
= v
.val
; }
144 char operator=(char v
) { return val
= v
; }
145 char operator+=(ByteInt v
) { return val
+= v
.val
; }
146 char operator+=(char v
) { return val
+= v
; }
147 char operator-=(ByteInt v
) { return val
-= v
.val
; }
148 char operator-=(char v
) { return val
-= v
; }
149 char operator*=(ByteInt v
) { return val
*= v
.val
; }
150 char operator*=(char v
) { return val
*= v
; }
151 char operator/=(ByteInt v
) { return val
/= v
.val
; }
152 char operator/=(char v
) { return val
/= v
; }
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++() { return val
++; }
166 char operator++(int) { char tmp
= val
; val
++; return val
; }
167 char operator--() { return val
--; }
168 char operator--(int) { char tmp
= val
; val
--; return val
; }
177 * Represents Date Data type.
179 * @author Prabakaran Thirumalai
181 class Date
{ // The class a user would declare to hold date
184 Date() {julianDate
= 0;}
185 Date(JulianRep julian
) : julianDate(julian
) {}
187 /** constructor with year, month, day
192 Date(int year
, int month
, int day
);
194 Date(const Date
&d2
) { julianDate
= d2
.julianDate
; }
195 Date
& operator=(const Date
& d2
)
196 { julianDate
=d2
.julianDate
; return *this; }
198 /** sets the date with specified year, month, day
203 int set(int year
, int month
, int day
);
204 int set(const struct tm
*tmptr
);
206 /** get year, month, day of the date
207 * @param year year IN
208 * @param month month IN
211 int get(int &year
, int &month
, int &day
) const;
213 /** checks for the validity of the date
215 bool isValid() const;
217 /** resets the date to zero
219 void setNull() { julianDate
= 0;}
221 /** returns day of the week
223 int dayOfWeek() const;
225 /** returns day of the week.
226 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
228 const char *dayOfWeekName() const;
230 /** returns day of the week abbreviation
231 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
233 const char *dayOfWeekAbbr() const;
235 /** returns day of the week.
236 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
238 static const char *dayOfWeekName(int day
); // 0--> Sunday
240 /** returns day of the week abbreviation
241 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
243 static const char *dayOfWeekAbbr(int day
);
245 static int dayOfWeek(JulianRep juldate
);
248 /** returns the day of the month. Values are 1 to 31
250 int dayOfMonth() const;
252 int dayOfYear() const;
254 /** returns the month Values are 1 to 12.
258 /** returns the month name
259 * values are "January", "February", "March", "April", "May", "June",
260 * "July", "August", "September", "October", "November", "December"
262 const char *monthName() const;
264 /** returns the month name abbreviation
265 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
266 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
268 const char *monthAbbr() const;
270 /** returns the month name
271 * values are "January", "February", "March", "April", "May", "June",
272 * "July", "August", "September", "October", "November", "December"
274 static const char *monthName(int month
);
277 /** returns the month name abbreviation
278 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
279 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
281 static const char *monthAbbr(int month
);
283 /** parses the date string passed and stores it
284 *It should of the format "mm/dd/yyyy"
286 int parseFrom(const char *s
);
288 Date
&operator++() { julianDate
++; return *this; }
289 Date
&operator--() { julianDate
--; return *this; }
291 Date
&operator+=(int days
) { julianDate
+= days
; return *this;}
292 Date
&operator-=(int days
) { julianDate
-= days
; return *this;}
298 /** checks for the validity of the date
300 static bool isValidDate(int year
, int month
, int day
);
302 friend Date
operator+(const Date
&d1
, int days
);
303 friend Date
operator+(int days
, const Date
&d1
);
304 friend Date
operator-(const Date
&d1
, int days
);
305 friend int operator-(const Date
&d1
, const Date
& d2
);
306 friend int operator<(const Date
&d1
,const Date
&d2
);
307 friend int operator>(const Date
&d1
,const Date
&d2
);
308 friend int operator<=(const Date
&d1
,const Date
&d2
);
309 friend int operator>=(const Date
&d1
,const Date
&d2
);
310 friend int operator==(const Date
&d1
,const Date
&d2
);
311 friend int operator!=(const Date
&d1
,const Date
&d2
);
313 /** checks for leap year
315 static bool isLeapYear(int year
);
317 /** returns the number of days in the specified month of the year.
319 static int daysInMonth(int month
, int year
);
321 static int YMDToJulian(int year
,int mon
,int day
, JulianRep
&julian
);
322 static int julianToYMD(JulianRep julian
,int &year
,int &month
,int &day
);
325 JulianRep julianDate
;
333 * Represents Time Data type.
335 * @author Prabakaran Thirumalai
337 class Time
{ // The class a user would declare to hold time
339 Time() {timeVal
= 0;}
341 /** Overloaded constructor
347 Time(int hours
, int mins
, int secs
, int usec
= 0);
348 Time(int totalSecs
) : timeVal(totalSecs
) {;}
349 Time(const Time
&d2
) { timeVal
= d2
.timeVal
; }
350 Time
& operator=(const Time
& d2
) { timeVal
=d2
.timeVal
; return *this; }
352 /** sets the time with specified hours, mins, secs
358 int set(int hours
, int mins
, int secs
, int usec
= 0);
360 /** retrieves the time using IN parameters
365 int get(int &hours
, int &mins
, int &secs
) const;
367 /** checks for the validity of the time
369 bool isValid() const;
373 void setNull() { timeVal
= -1;}
375 int secondsSinceMidnight() const { return timeVal
/10000;}
377 /** returns the microsecs
379 int usec() const; // to nearest 100 of usec.
381 /** returns the millisecs
389 /** returns the minutes
393 /** returns the hours
398 /** sets the millisecs
402 /** sets the microsecs
406 /** parses the time string passed and stores it
407 *It should of the format "hh:mm::ss"
409 int parseFrom(const char *s
);
411 Time
&operator++() { timeVal
+= 10000; return *this; }
412 Time
&operator--() { timeVal
-= 10000; return *this; }
414 Time
&operator+=(int seconds
) { timeVal
+= seconds
*10000; return *this; }
415 Time
&operator-=(int seconds
) { timeVal
-= seconds
*10000; return *this; }
418 /** checks for the validity of the time specified
420 static bool isValidTime(int hours
, int mins
, int secs
);
422 friend Time
operator+(const Time
&t1
, int seconds
);
423 friend Time
operator+(int seconds
, const Time
&t1
);
424 friend Time
operator-(const Time
&t1
, int seconds
);
425 friend int operator-(const Time
&t1
, const Time
& t2
);
426 friend int operator<(const Time
&t1
,const Time
&t2
);
427 friend int operator>(const Time
&t1
,const Time
&t2
);
428 friend int operator<=(const Time
&t1
,const Time
&t2
);
429 friend int operator>=(const Time
&t1
,const Time
&t2
);
430 friend int operator==(const Time
&t1
,const Time
&t2
);
431 friend int operator!=(const Time
&t1
,const Time
&t2
);
440 * Represents TimeStamp Data type.
442 * @author Prabakaran Thirumalai
449 /** Overloaded constructor
458 TimeStamp(int year
, int month
, int day
, int hour
, int minute
, int sec
, int usec
= 0) :
459 date(year
, month
, day
), time(hour
, minute
, sec
, usec
) { }
461 TimeStamp(const TimeStamp
&ts
)
462 { date
= ts
.date
; time
= ts
.time
; }
463 TimeStamp(const Date
&d
, Time
&t
) : date(d
), time(t
) {}
466 TimeStamp
& operator=(const TimeStamp
& d2
)
467 { date
=d2
.date
; time
= d2
.time
; return *this; }
469 /** get year, month, day from the date part of the timestamp
470 * @param year year IN
471 * @param month month IN
474 int getDate(int &year
, int &month
, int &day
)
475 { return date
.get(year
, month
, day
); }
477 /** get the date part of the timestamp
480 void getDate(Date
&newDate
) const
483 /** sets the date with specified year, month, day
488 int setDate(int year
, int month
, int day
)
489 { return date
.set(year
, month
, day
); }
491 /** set the date part of the timestamp
494 void setDate(const Date
&newDate
)
498 operator Date() { return date
; }
499 operator Time() { return time
; }
503 /** retrieves the time using IN parameters
508 int getTime(int &hours
, int &mins
, int &secs
) const
509 { return time
.get(hours
, mins
, secs
); }
510 /** retrieves the time part of the timestamp
511 * @param newTime Time
513 void getTime(Time
&newTime
) const
516 /** sets the time with specified hours, mins, secs
522 int setTime(int hours
, int mins
, int secs
, int usec
= 0)
523 { return time
.set(hours
, mins
, secs
, usec
); }
525 /** set the time part of the timestamp
526 * @param newTime Time
528 void setTime(const Time
&newTime
)
531 /** checks for the validity of the timestamp
533 bool isValid() const { return date
.isValid() && time
.isValid(); }
535 /** resets the date and time */
536 void setNull() { date
.setNull(); time
.setNull(); }
538 /** returns day of the week. Values are 1-7
540 int dayOfWeek() const { return date
.dayOfWeek(); }
542 /** returns day of the week.
543 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Satur
546 const char *dayOfWeekName() const { return date
.dayOfWeekName(); }
548 /** returns day of the week abbreviation
549 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
551 const char *dayOfWeekAbbr() const { return date
.dayOfWeekAbbr(); }
553 /** returns the day of the month. Values are 1 to 31
555 int dayOfMonth() const { return date
.dayOfMonth(); }
556 int dayOfYear() const { return date
.dayOfYear(); }
558 /** returns the month. Values are 1 to 12.
561 int month() const { return date
.month(); }
563 /** returns the month name
564 * values are "January", "February", "March", "April", "May", "June",
565 * "July", "August", "September", "October", "November", "December"
567 const char *monthName() const { return date
.monthName(); }
569 /** returns the month name abbreviation
570 * Values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
571 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
573 const char *monthAbbr() const { return date
.monthAbbr(); }
577 int year() const { return date
.year(); }
579 int secondsSinceMidnight() const { return time
.secondsSinceMidnight(); }
580 /** returns the seconds */
581 int seconds() const { return time
.seconds(); }
582 /** returns the minutes */
583 int minutes() const { return time
.minutes(); }
584 /** returns the hours */
585 int hours() const { return time
.hours(); }
586 /** returns the millisecs */
587 int msec() const { return time
.msec(); }
588 /** returns the microsecs */
589 int usec() const { return time
.usec(); }
591 /** sets the millisecs */
592 int setMsec(int ms
) { return time
.setMsec(ms
) ; }
593 /** sets the microsecs */
594 int setUsec(int us
) { return time
.setUsec(us
) ; }
596 /** parses the date string passed and stores it
597 *It should of the format "mm/dd/yyyy"
599 int parseDateFrom(const char *s
) { return date
.parseFrom(s
); }
601 /** parses the time string passed and stores it
602 *It should of the format "hh:mm::ss"
605 int parseTimeFrom(const char *s
) { return time
.parseFrom(s
); }
607 friend int operator<(const TimeStamp
&d1
, const TimeStamp
&d2
);
608 friend int operator>(const TimeStamp
&d1
, const TimeStamp
&d2
);
609 friend int operator<=(const TimeStamp
&d1
, const TimeStamp
&d2
);
610 friend int operator>=(const TimeStamp
&d1
, const TimeStamp
&d2
);
611 friend int operator==(const TimeStamp
&d1
, const TimeStamp
&d2
);
612 friend int operator!=(const TimeStamp
&d1
, const TimeStamp
&d2
);