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
70 static char CompOpNames
[][20] =
72 "Equals", "NotEquals", "LessThan", "LessThanEquals", "GreaterThan",
73 "GreaterThanEquals", "Like", "Invalid"
79 * Logical operators supported by the database system.
90 static char LogOpNames
[][10] =
92 "AND", "OR", "NOT", "Invalid"
98 inline static long size(DataType type
, int length
=0);
99 static char* getSQLString(DataType type
);
100 static SQLSMALLINT
convertToSQLType(DataType type
);
101 static SQLSMALLINT
convertToSQL_C_Type(DataType type
);
102 static DataType
convertFromSQLType(SQLSMALLINT type
);
104 inline static void copyVal(void* dest
, void *src
, DataType type
, int length
= 0);
105 inline static void addVal(void* dest
, void *src
, DataType type
);
106 static void divVal(void* dest
, int src
, DataType type
);
107 static void subVal(void* dest
, void *src
, DataType type
);
108 static void mulVal(void* dest
, void *src
, DataType type
);
109 static void mudVal(void* dest
, void *src
, DataType type
);
110 static void divVal(void* dest
, void *src
, DataType type
);
113 inline static bool compareVal(void *src1
, void *src2
, ComparisionOp op
,
114 DataType type
, long length
= 0);
115 static bool compareIntVal(void* src1
, void* src2
, ComparisionOp op
);
116 static bool compareLongVal(void* src1
, void* src2
, ComparisionOp op
);
117 static bool compareLongLongVal(void* src1
, void* src2
, ComparisionOp op
);
118 static bool compareShortVal(void* src1
, void* src2
, ComparisionOp op
);
119 static bool compareByteIntVal(void* src1
, void* src2
, ComparisionOp op
);
120 static bool compareDoubleVal(void* src1
, void* src2
, ComparisionOp op
);
121 static bool compareFloatVal(void* src1
, void* src2
, ComparisionOp op
);
122 static bool compareDateVal(void* src1
, void* src2
, ComparisionOp op
);
123 static bool compareTimeVal(void* src1
, void* src2
, ComparisionOp op
);
124 static bool compareTimeStampVal(void* src1
, void* src2
, ComparisionOp op
);
125 static bool compareStringVal(void* src1
, void* src2
, ComparisionOp op
);
126 static bool compareBinaryVal(void* src1
, void* src2
,
127 ComparisionOp op
, int length
);
130 static void convert(DataType srcType
, void *src
, DataType destType
, void *dest
, int length
=0);
131 static void convertToInt(void* dest
, void* src
, DataType srcType
);
132 static void convertToLong(void* dest
, void* src
, DataType srcType
);
133 static void convertToLongLong(void* dest
, void* src
, DataType srcType
);
134 static void convertToShort(void* dest
, void* src
, DataType srcType
);
135 static void convertToByteInt(void* dest
, void* src
, DataType srcType
);
136 static void convertToFloat(void* dest
, void* src
, DataType srcType
);
137 static void convertToDouble(void* dest
, void* src
, DataType srcType
);
138 static void convertToString(void* dest
, void* src
, DataType srcType
, int length
=0);
139 static void convertToDate(void* dest
, void* src
, DataType srcType
);
140 static void convertToTime(void* dest
, void* src
, DataType srcType
);
141 static void convertToTimeStamp(void* dest
, void* src
, DataType srcType
);
142 static void convertToBinary(void* dest
, void* src
, DataType srcType
, int length
);
144 static void memoryset(void *value
,DataType type
);
145 inline static ComparisionOp
getComparisionOperator(char *str
);
147 static void* alloc(DataType type
, int length
=0);
148 static DbRetVal
strToValue(void *dest
, char *src
, DataType type
, int length
=0);
149 static int printVal(void *src
, DataType type
, int length
);
157 * Represents 8 bit integer.
168 ByteInt(const ByteInt
&v
) { val
= v
.val
; }
169 /** constructor with char
170 * @param v char value
172 ByteInt(char v
) { val
= v
; }
173 operator int() const { return (int) val
; }
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<<=(ByteInt v
) { return val
<<= v
.val
; }
187 char operator<<=(char v
) { return val
<<= v
; }
188 char operator>>=(ByteInt v
) { return val
>>= v
.val
; }
189 char operator>>=(char v
) { return val
>>= v
; }
190 char operator&=(ByteInt v
) { return val
&= v
.val
; }
191 char operator&=(char v
) { return val
&= v
; }
192 char operator|=(ByteInt v
) { return val
|= v
.val
; }
193 char operator|=(char v
) { return val
|= v
; }
194 char operator^=(ByteInt v
) { return val
^= v
.val
; }
195 char operator^=(char v
) { return val
^= v
; }
196 char operator++() { return val
++; }
197 char operator++(int) { char tmp
= val
; val
++; return val
; }
198 char operator--() { return val
--; }
199 char operator--(int) { char tmp
= val
; val
--; return val
; }
208 * Represents Date Data type.
212 class Date
{ // The class a user would declare to hold date
215 Date() {julianDate
= 0;}
216 Date(JulianRep julian
) : julianDate(julian
) {}
218 /** constructor with year, month, day
223 Date(int year
, int month
, int day
);
225 Date(const Date
&d2
) { julianDate
= d2
.julianDate
; }
226 Date
& operator=(const Date
& d2
)
227 { julianDate
=d2
.julianDate
; return *this; }
229 /** sets the date with specified year, month, day
234 int set(int year
, int month
, int day
);
235 int set(const struct tm
*tmptr
);
237 /** get year, month, day of the date
238 * @param year year IN
239 * @param month month IN
242 int get(int &year
, int &month
, int &day
) const;
244 /** checks for the validity of the date
246 bool isValid() const;
248 /** resets the date to zero
250 void setNull() { julianDate
= 0;}
252 /** returns day of the week
254 int dayOfWeek() const;
256 /** returns day of the week.
257 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
259 const char *dayOfWeekName() const;
261 /** returns day of the week abbreviation
262 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
264 const char *dayOfWeekAbbr() const;
266 /** returns day of the week.
267 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
269 static const char *dayOfWeekName(int day
); // 0--> Sunday
271 /** returns day of the week abbreviation
272 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
274 static const char *dayOfWeekAbbr(int day
);
276 static int dayOfWeek(JulianRep juldate
);
279 /** returns the day of the month. Values are 1 to 31
281 int dayOfMonth() const;
283 int dayOfYear() const;
285 /** returns the month Values are 1 to 12.
289 /** returns the month name
290 * values are "January", "February", "March", "April", "May", "June",
291 * "July", "August", "September", "October", "November", "December"
293 const char *monthName() const;
295 /** returns the month name abbreviation
296 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
297 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
299 const char *monthAbbr() const;
301 /** returns the month name
302 * values are "January", "February", "March", "April", "May", "June",
303 * "July", "August", "September", "October", "November", "December"
305 static const char *monthName(int month
);
308 /** returns the month name abbreviation
309 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
310 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
312 static const char *monthAbbr(int month
);
314 /** parses the date string passed and stores it
315 *It should of the format "mm/dd/yyyy"
317 int parseFrom(const char *s
);
319 Date
&operator++() { julianDate
++; return *this; }
320 Date
&operator--() { julianDate
--; return *this; }
322 Date
&operator+=(int days
) { julianDate
+= days
; return *this;}
323 Date
&operator-=(int days
) { julianDate
-= days
; return *this;}
329 /** checks for the validity of the date
331 static bool isValidDate(int year
, int month
, int day
);
333 friend Date
operator+(const Date
&d1
, int days
);
334 friend Date
operator+(int days
, const Date
&d1
);
335 friend Date
operator-(const Date
&d1
, int days
);
336 friend int operator-(const Date
&d1
, const Date
& d2
);
337 friend int operator<(const Date
&d1
,const Date
&d2
);
338 friend int operator>(const Date
&d1
,const Date
&d2
);
339 friend int operator<=(const Date
&d1
,const Date
&d2
);
340 friend int operator>=(const Date
&d1
,const Date
&d2
);
341 friend int operator==(const Date
&d1
,const Date
&d2
);
342 friend int operator!=(const Date
&d1
,const Date
&d2
);
344 /** checks for leap year
346 static bool isLeapYear(int year
);
348 /** returns the number of days in the specified month of the year.
350 static int daysInMonth(int month
, int year
);
352 static int YMDToJulian(int year
,int mon
,int day
, JulianRep
&julian
);
353 static int julianToYMD(JulianRep julian
,int &year
,int &month
,int &day
);
356 JulianRep julianDate
;
364 * Represents Time Data type.
368 class Time
{ // The class a user would declare to hold time
370 Time() {timeVal
= 0;}
372 /** Overloaded constructor
378 Time(int hours
, int mins
, int secs
, int usec
= 0);
379 Time(int totalSecs
) : timeVal(totalSecs
) {;}
380 Time(const Time
&d2
) { timeVal
= d2
.timeVal
; }
381 Time
& operator=(const Time
& d2
) { timeVal
=d2
.timeVal
; return *this; }
383 /** sets the time with specified hours, mins, secs
389 int set(int hours
, int mins
, int secs
, int usec
= 0);
391 /** retrieves the time using IN parameters
396 int get(int &hours
, int &mins
, int &secs
) const;
398 /** checks for the validity of the time
400 bool isValid() const;
404 void setNull() { timeVal
= -1;}
406 int secondsSinceMidnight() const { return timeVal
/10000;}
408 /** returns the microsecs
410 int usec() const; // to nearest 100 of usec.
412 /** returns the millisecs
420 /** returns the minutes
424 /** returns the hours
429 /** sets the millisecs
433 /** sets the microsecs
437 /** parses the time string passed and stores it
438 *It should of the format "hh:mm::ss"
440 int parseFrom(const char *s
);
442 Time
&operator++() { timeVal
+= 10000; return *this; }
443 Time
&operator--() { timeVal
-= 10000; return *this; }
445 Time
&operator+=(int seconds
) { timeVal
+= seconds
*10000; return *this; }
446 Time
&operator-=(int seconds
) { timeVal
-= seconds
*10000; return *this; }
449 /** checks for the validity of the time specified
451 static bool isValidTime(int hours
, int mins
, int secs
);
453 friend Time
operator+(const Time
&t1
, int seconds
);
454 friend Time
operator+(int seconds
, const Time
&t1
);
455 friend Time
operator-(const Time
&t1
, int seconds
);
456 friend int operator-(const Time
&t1
, const Time
& t2
);
457 friend int operator<(const Time
&t1
,const Time
&t2
);
458 friend int operator>(const Time
&t1
,const Time
&t2
);
459 friend int operator<=(const Time
&t1
,const Time
&t2
);
460 friend int operator>=(const Time
&t1
,const Time
&t2
);
461 friend int operator==(const Time
&t1
,const Time
&t2
);
462 friend int operator!=(const Time
&t1
,const Time
&t2
);
471 * Represents TimeStamp Data type.
480 /** Overloaded constructor
489 TimeStamp(int year
, int month
, int day
, int hour
, int minute
, int sec
, int usec
= 0) :
490 date(year
, month
, day
), time(hour
, minute
, sec
, usec
) { }
492 TimeStamp(const TimeStamp
&ts
)
493 { date
= ts
.date
; time
= ts
.time
; }
494 TimeStamp(const Date
&d
, Time
&t
) : date(d
), time(t
) {}
497 TimeStamp
& operator=(const TimeStamp
& d2
)
498 { date
=d2
.date
; time
= d2
.time
; return *this; }
500 /** get year, month, day from the date part of the timestamp
501 * @param year year IN
502 * @param month month IN
505 int getDate(int &year
, int &month
, int &day
)
506 { return date
.get(year
, month
, day
); }
508 /** get the date part of the timestamp
511 void getDate(Date
&newDate
) const
514 /** sets the date with specified year, month, day
519 int setDate(int year
, int month
, int day
)
520 { return date
.set(year
, month
, day
); }
522 /** set the date part of the timestamp
525 void setDate(const Date
&newDate
)
529 operator Date() { return date
; }
530 operator Time() { return time
; }
534 /** retrieves the time using IN parameters
539 int getTime(int &hours
, int &mins
, int &secs
) const
540 { return time
.get(hours
, mins
, secs
); }
541 /** retrieves the time part of the timestamp
542 * @param newTime Time
544 void getTime(Time
&newTime
) const
547 /** sets the time with specified hours, mins, secs
553 int setTime(int hours
, int mins
, int secs
, int usec
= 0)
554 { return time
.set(hours
, mins
, secs
, usec
); }
556 /** set the time part of the timestamp
557 * @param newTime Time
559 void setTime(const Time
&newTime
)
562 /** checks for the validity of the timestamp
564 bool isValid() const { return date
.isValid() && time
.isValid(); }
566 /** resets the date and time */
567 void setNull() { date
.setNull(); time
.setNull(); }
569 /** returns day of the week. Values are 1-7
571 int dayOfWeek() const { return date
.dayOfWeek(); }
573 /** returns day of the week.
574 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Satur
577 const char *dayOfWeekName() const { return date
.dayOfWeekName(); }
579 /** returns day of the week abbreviation
580 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
582 const char *dayOfWeekAbbr() const { return date
.dayOfWeekAbbr(); }
584 /** returns the day of the month. Values are 1 to 31
586 int dayOfMonth() const { return date
.dayOfMonth(); }
587 int dayOfYear() const { return date
.dayOfYear(); }
589 /** returns the month. Values are 1 to 12.
592 int month() const { return date
.month(); }
594 /** returns the month name
595 * values are "January", "February", "March", "April", "May", "June",
596 * "July", "August", "September", "October", "November", "December"
598 const char *monthName() const { return date
.monthName(); }
600 /** returns the month name abbreviation
601 * Values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
602 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
604 const char *monthAbbr() const { return date
.monthAbbr(); }
608 int year() const { return date
.year(); }
610 int secondsSinceMidnight() const { return time
.secondsSinceMidnight(); }
611 /** returns the seconds */
612 int seconds() const { return time
.seconds(); }
613 /** returns the minutes */
614 int minutes() const { return time
.minutes(); }
615 /** returns the hours */
616 int hours() const { return time
.hours(); }
617 /** returns the millisecs */
618 int msec() const { return time
.msec(); }
619 /** returns the microsecs */
620 int usec() const { return time
.usec(); }
622 /** sets the millisecs */
623 int setMsec(int ms
) { return time
.setMsec(ms
) ; }
624 /** sets the microsecs */
625 int setUsec(int us
) { return time
.setUsec(us
) ; }
627 /** parses the date string passed and stores it
628 *It should of the format "mm/dd/yyyy"
630 int parseDateFrom(const char *s
) { return date
.parseFrom(s
); }
632 /** parses the time string passed and stores it
633 *It should of the format "hh:mm::ss"
636 int parseTimeFrom(const char *s
) { return time
.parseFrom(s
); }
638 int parseFrom(const char *s
);
639 friend int operator<(const TimeStamp
&d1
, const TimeStamp
&d2
);
640 friend int operator>(const TimeStamp
&d1
, const TimeStamp
&d2
);
641 friend int operator<=(const TimeStamp
&d1
, const TimeStamp
&d2
);
642 friend int operator>=(const TimeStamp
&d1
, const TimeStamp
&d2
);
643 friend int operator==(const TimeStamp
&d1
, const TimeStamp
&d2
);
644 friend int operator!=(const TimeStamp
&d1
, const TimeStamp
&d2
);
652 #include<DataTypeInline.h>