1 /***************************************************************************
3 * Copyright (C) Lakshya Solutions Ltd. All rights reserved. *
5 ***************************************************************************/
14 typedef int JulianRep
;
19 * Data Types supported by the database system.
24 typeInt
= 0, /**<integer type*/
48 * @class ComparisionOp
49 * Comparision operators supported by the database system.
60 OpLike
, // for Like operator
62 OpInvalidComparisionOp
64 static char CompOpNames
[][20] =
66 "Equals", "NotEquals", "LessThan", "LessThanEquals", "GreaterThan",
67 "GreaterThanEquals", "Like", "isNULL", "Invalid"
73 * Logical operators supported by the database system.
84 static char LogOpNames
[][10] =
86 "AND", "OR", "NOT", "Invalid"
92 inline static long size(DataType type
, int length
=0);
93 static char* getSQLString(DataType type
);
94 static SQLSMALLINT
convertToSQLType(DataType type
);
95 static SQLSMALLINT
convertToCSQLSQLType(DataType type
);
96 static SQLSMALLINT
convertToSQL_C_Type(DataType type
,TDBInfo tdbname
=mysql
);
97 static DataType
convertFromSQLType(SQLSMALLINT type
,int length
=0, int scale
=0, TDBInfo tdbname
=mysql
);
99 inline static void copyVal(void* dest
, void *src
, DataType type
, int length
= 0,int dbFlag
=0);
100 inline static void copyZeroVal(void* dest
, DataType type
, int length
= 0);
101 inline static void cachecopyVal(void* dest
, void *src
, DataType type
, int length
= 0,int dbFlag
=0);
102 inline static void addVal(void* dest
, void *src
, DataType type
);
103 static void divVal(double* dest
, int src
, DataType type
);
104 static void subVal(void* dest
, void *src
, DataType type
);
105 static void mulVal(void* dest
, void *src
, DataType type
);
106 static void mudVal(void* dest
, void *src
, DataType type
);
107 static void divVal(void* dest
, void *src
, DataType type
);
108 static void increment(void* dest
, void *src
, DataType type
);
109 static bool isValueZero(void* dest
, DataType type
);
111 inline static bool compareVal(void *src1
, void *src2
, ComparisionOp op
,
112 DataType type
, long length
= 0);
113 static bool compareIntVal(void* src1
, void* src2
, ComparisionOp op
);
114 static bool compareLongVal(void* src1
, void* src2
, ComparisionOp op
);
115 static bool compareLongLongVal(void* src1
, void* src2
, ComparisionOp op
);
116 static bool compareShortVal(void* src1
, void* src2
, ComparisionOp op
);
117 static bool compareByteIntVal(void* src1
, void* src2
, ComparisionOp op
);
118 static bool compareDoubleVal(void* src1
, void* src2
, ComparisionOp op
);
119 static bool compareFloatVal(void* src1
, void* src2
, ComparisionOp op
);
120 static bool compareDateVal(void* src1
, void* src2
, ComparisionOp op
);
121 static bool compareTimeVal(void* src1
, void* src2
, ComparisionOp op
);
122 static bool compareTimeStampVal(void* src1
, void* src2
, ComparisionOp op
);
123 static bool compareStringVal(void* src1
, void* src2
, ComparisionOp op
);
124 static bool compareBinaryVal(void* src1
, void* src2
,
125 ComparisionOp op
, int length
);
128 static void convert(DataType srcType
, void *src
, DataType destType
, void *dest
, int length
=0);
129 static void convertToInt(void* dest
, void* src
, DataType srcType
);
130 static void convertToLong(void* dest
, void* src
, DataType srcType
);
131 static void convertToLongLong(void* dest
, void* src
, DataType srcType
);
132 static void convertToShort(void* dest
, void* src
, DataType srcType
);
133 static void convertToByteInt(void* dest
, void* src
, DataType srcType
);
134 static void convertToFloat(void* dest
, void* src
, DataType srcType
);
135 static void convertToDouble(void* dest
, void* src
, DataType srcType
);
136 static void convertToString(void* dest
, void* src
, DataType srcType
, int length
=0,TDBInfo tdbname
=mysql
);
137 static void convertToDate(void* dest
, void* src
, DataType srcType
);
138 static void convertToTime(void* dest
, void* src
, DataType srcType
);
139 static void convertToTimeStamp(void* dest
, void* src
, DataType srcType
);
140 static void convertToBinary(void* dest
, void* src
, DataType srcType
, int length
);
141 static DataType
getCsqlTypeFromFunctionType(FunctionType type
);
142 static DataType
getCsqlTypeFromFunctionTypeForComparision(FunctionType type
);
143 static void memoryset(void *value
,DataType type
);
144 inline static ComparisionOp
getComparisionOperator(char *str
);
146 static void* alloc(DataType type
, int length
=0);
147 static DbRetVal
strToValue(void *dest
, char *src
, DataType type
, int length
=0);
148 static int printVal(void *src
, DataType type
, int length
,int dbFlag
=0);
149 inline static bool isValidFieldForAvg(DataType type
);
155 * Represents 8 bit integer.
166 ByteInt(const ByteInt
&v
) { val
= v
.val
; }
167 /** constructor with char
168 * @param v char value
170 ByteInt(char v
) { val
= v
; }
171 operator int() const { return (int) val
; }
172 char operator=(ByteInt v
) { return val
= v
.val
; }
173 char operator=(char v
) { return val
= v
; }
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++() { return val
++; }
195 char operator++(int) { char tmp
= val
; val
++; return val
; }
196 char operator--() { return val
--; }
197 char operator--(int) { char tmp
= val
; val
--; return val
; }
206 * Represents Date Data type.
210 class Date
{ // The class a user would declare to hold date
213 Date() {julianDate
= 0;}
214 Date(JulianRep julian
) : julianDate(julian
) {}
216 /** constructor with year, month, day
221 Date(int year
, int month
, int day
);
223 Date(const Date
&d2
) { julianDate
= d2
.julianDate
; }
224 Date
& operator=(const Date
& d2
)
225 { julianDate
=d2
.julianDate
; return *this; }
227 /** sets the date with specified year, month, day
232 int set(int year
, int month
, int day
);
233 int set(const struct tm
*tmptr
);
235 /** get year, month, day of the date
236 * @param year year IN
237 * @param month month IN
240 int get(int &year
, int &month
, int &day
) const;
242 /** checks for the validity of the date
244 bool isValid() const;
246 /** resets the date to zero
248 void setNull() { julianDate
= 0;}
250 /** returns day of the week
252 int dayOfWeek() const;
254 /** returns day of the week.
255 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
257 const char *dayOfWeekName() const;
259 /** returns day of the week abbreviation
260 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
262 const char *dayOfWeekAbbr() const;
264 /** returns day of the week.
265 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
267 static const char *dayOfWeekName(int day
); // 0--> Sunday
269 /** returns day of the week abbreviation
270 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
272 static const char *dayOfWeekAbbr(int day
);
274 static int dayOfWeek(JulianRep juldate
);
277 void addDay(int noOfDays
);
278 void subDay(int noOfDays
);
279 void addMonth(int noOfMons
);
280 void subMonth(int noOfMons
);
281 void addYear(int noOfYrs
);
282 void subYear(int noOfYrs
);
284 /** returns the day of the month. Values are 1 to 31
286 int dayOfMonth() const;
288 int dayOfYear() const;
290 /** returns the month Values are 1 to 12.
294 /** returns the month name
295 * values are "January", "February", "March", "April", "May", "June",
296 * "July", "August", "September", "October", "November", "December"
298 const char *monthName() const;
300 /** returns the month name abbreviation
301 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
302 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
304 const char *monthAbbr() const;
306 /** returns the month name
307 * values are "January", "February", "March", "April", "May", "June",
308 * "July", "August", "September", "October", "November", "December"
310 static const char *monthName(int month
);
313 /** returns the month name abbreviation
314 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
315 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
317 static const char *monthAbbr(int month
);
319 /** parses the date string passed and stores it
320 *It should of the format "mm/dd/yyyy"
322 int parseFrom(const char *s
);
324 int getCalDay() { return julianDate
;}
325 static void changeToCsqlFormat(char *src
);
328 Date
&operator++() { julianDate
++; return *this; }
329 Date
&operator--() { julianDate
--; return *this; }
331 Date
&operator+=(int days
) { julianDate
+= days
; return *this;}
332 Date
&operator-=(int days
) { julianDate
-= days
; return *this;}
338 /** checks for the validity of the date
340 static bool isValidDate(int year
, int month
, int day
);
342 friend Date
operator+(const Date
&d1
, int days
);
343 friend Date
operator+(int days
, const Date
&d1
);
344 friend Date
operator-(const Date
&d1
, int days
);
345 friend int operator-(const Date
&d1
, const Date
& d2
);
346 friend int operator<(const Date
&d1
,const Date
&d2
);
347 friend int operator>(const Date
&d1
,const Date
&d2
);
348 friend int operator<=(const Date
&d1
,const Date
&d2
);
349 friend int operator>=(const Date
&d1
,const Date
&d2
);
350 friend int operator==(const Date
&d1
,const Date
&d2
);
351 friend int operator!=(const Date
&d1
,const Date
&d2
);
353 /** checks for leap year
355 static bool isLeapYear(int year
);
357 /** returns the number of days in the specified month of the year.
359 static int daysInMonth(int month
, int year
);
361 static int YMDToJulian(int year
,int mon
,int day
, JulianRep
&julian
);
362 static int julianToYMD(JulianRep julian
,int &year
,int &month
,int &day
);
365 JulianRep julianDate
;
373 * Represents Time Data type.
377 class Time
{ // The class a user would declare to hold time
379 Time() {timeVal
= 0;}
381 /** Overloaded constructor
387 Time(int hours
, int mins
, int secs
, int usec
= 0);
388 Time(int totalSecs
) : timeVal(totalSecs
) {;}
389 Time(const Time
&d2
) { timeVal
= d2
.timeVal
; }
390 Time
& operator=(const Time
& d2
) { timeVal
=d2
.timeVal
; return *this; }
392 /** sets the time with specified hours, mins, secs
398 int set(int hours
, int mins
, int secs
, int usec
= 0);
400 /** retrieves the time using IN parameters
405 int get(int &hours
, int &mins
, int &secs
) const;
410 int getCalSec(){ return timeVal
/1000; }
411 /** checks for the validity of the time
413 bool isValid() const;
417 void setNull() { timeVal
= -1;}
419 int secondsSinceMidnight() const { return timeVal
/10000;}
421 /** returns the microsecs
423 int usec() const; // to nearest 100 of usec.
425 /** returns the millisecs
433 /** returns the minutes
437 /** returns the hours
442 int addSec(int secs
);
443 int subSec(int secs
);
444 int addMin(int mins
);
445 int subMin(int mins
);
446 int addHour(int hours
);
447 int subHour(int hours
);
449 /** sets the millisecs
453 /** sets the microsecs
457 /** parses the time string passed and stores it
458 *It should of the format "hh:mm::ss"
460 int parseFrom(const char *s
);
462 Time
&operator++() { timeVal
+= 10000; return *this; }
463 Time
&operator--() { timeVal
-= 10000; return *this; }
465 Time
&operator+=(int seconds
) { timeVal
+= seconds
*10000; return *this; }
466 Time
&operator-=(int seconds
) { timeVal
-= seconds
*10000; return *this; }
469 /** checks for the validity of the time specified
471 static bool isValidTime(int hours
, int mins
, int secs
);
473 friend Time
operator+(const Time
&t1
, int seconds
);
474 friend Time
operator+(int seconds
, const Time
&t1
);
475 friend Time
operator-(const Time
&t1
, int seconds
);
476 friend int operator-(const Time
&t1
, const Time
& t2
);
477 friend int operator<(const Time
&t1
,const Time
&t2
);
478 friend int operator>(const Time
&t1
,const Time
&t2
);
479 friend int operator<=(const Time
&t1
,const Time
&t2
);
480 friend int operator>=(const Time
&t1
,const Time
&t2
);
481 friend int operator==(const Time
&t1
,const Time
&t2
);
482 friend int operator!=(const Time
&t1
,const Time
&t2
);
491 * Represents TimeStamp Data type.
500 /** Overloaded constructor
509 TimeStamp(int year
, int month
, int day
, int hour
, int minute
, int sec
, int usec
= 0) :
510 date(year
, month
, day
), time(hour
, minute
, sec
, usec
) { }
512 TimeStamp(const TimeStamp
&ts
)
513 { date
= ts
.date
; time
= ts
.time
; }
514 TimeStamp(const Date
&d
, Time
&t
) : date(d
), time(t
) {}
517 TimeStamp
& operator=(const TimeStamp
& d2
)
518 { date
=d2
.date
; time
= d2
.time
; return *this; }
520 /** get year, month, day from the date part of the timestamp
521 * @param year year IN
522 * @param month month IN
525 int getDate(int &year
, int &month
, int &day
)
526 { return date
.get(year
, month
, day
); }
528 /** get the date part of the timestamp
531 void getDate(Date
&newDate
) const
534 /** sets the date with specified year, month, day
539 int setDate(int year
, int month
, int day
)
540 { return date
.set(year
, month
, day
); }
542 /** set the date part of the timestamp
545 void setDate(const Date
&newDate
)
549 operator Date() { return date
; }
550 operator Time() { return time
; }
554 /** retrieves the time using IN parameters
559 int getTime(int &hours
, int &mins
, int &secs
) const
560 { return time
.get(hours
, mins
, secs
); }
561 /** retrieves the time part of the timestamp
562 * @param newTime Time
564 void getTime(Time
&newTime
) const
567 /** sets the time with specified hours, mins, secs
573 int setTime(int hours
, int mins
, int secs
, int usec
= 0)
574 { return time
.set(hours
, mins
, secs
, usec
); }
576 /** set the time part of the timestamp
577 * @param newTime Time
579 void setTime(const Time
&newTime
)
582 /** checks for the validity of the timestamp
584 bool isValid() const { return date
.isValid() && time
.isValid(); }
586 /** resets the date and time */
587 void setNull() { date
.setNull(); time
.setNull(); }
589 /** returns day of the week. Values are 1-7
591 int dayOfWeek() const { return date
.dayOfWeek(); }
593 /** returns day of the week.
594 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Satur
597 const char *dayOfWeekName() const { return date
.dayOfWeekName(); }
599 /** returns day of the week abbreviation
600 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
602 const char *dayOfWeekAbbr() const { return date
.dayOfWeekAbbr(); }
604 /** returns the day of the month. Values are 1 to 31
606 int dayOfMonth() const { return date
.dayOfMonth(); }
607 int dayOfYear() const { return date
.dayOfYear(); }
609 /** returns the month. Values are 1 to 12.
612 int month() const { return date
.month(); }
614 /** returns the month name
615 * values are "January", "February", "March", "April", "May", "June",
616 * "July", "August", "September", "October", "November", "December"
618 const char *monthName() const { return date
.monthName(); }
620 /** returns the month name abbreviation
621 * Values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
622 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
624 const char *monthAbbr() const { return date
.monthAbbr(); }
628 int year() const { return date
.year(); }
630 int secondsSinceMidnight() const { return time
.secondsSinceMidnight(); }
631 /** returns the seconds */
632 int seconds() const { return time
.seconds(); }
633 /** returns the minutes */
634 int minutes() const { return time
.minutes(); }
635 /** returns the hours */
636 int hours() const { return time
.hours(); }
637 /** returns the millisecs */
638 int msec() const { return time
.msec(); }
639 /** returns the microsecs */
640 int usec() const { return time
.usec(); }
642 /** sets the millisecs */
643 int setMsec(int ms
) { return time
.setMsec(ms
) ; }
644 /** sets the microsecs */
645 int setUsec(int us
) { return time
.setUsec(us
) ; }
647 /** parses the date string passed and stores it
648 *It should of the format "mm/dd/yyyy"
650 int parseDateFrom(const char *s
) { return date
.parseFrom(s
); }
652 /** parses the time string passed and stores it
653 *It should of the format "hh:mm::ss"
656 int parseTimeFrom(const char *s
) { return time
.parseFrom(s
); }
658 static void changeToCsqlFormat(char *src
);
660 int parseFrom(const char *s
);
661 friend int operator<(const TimeStamp
&d1
, const TimeStamp
&d2
);
662 friend int operator>(const TimeStamp
&d1
, const TimeStamp
&d2
);
663 friend int operator<=(const TimeStamp
&d1
, const TimeStamp
&d2
);
664 friend int operator>=(const TimeStamp
&d1
, const TimeStamp
&d2
);
665 friend int operator==(const TimeStamp
&d1
, const TimeStamp
&d2
);
666 friend int operator!=(const TimeStamp
&d1
, const TimeStamp
&d2
);
667 void addYear(int noOfYrs
){ date
.addYear(noOfYrs
);}
669 void subYear(int noOfYrs
) { date
.subYear(noOfYrs
); }
671 void addMonth(int noOfMons
) { date
.addMonth(noOfMons
);}
673 void subMonth(int noOfMons
) { date
.subMonth(noOfMons
);}
675 void addDay(int noOfDays
) { date
.addDay(noOfDays
);}
677 void subDay(int noOfDays
) { date
.subDay(noOfDays
); }
679 void addHour(int hours
);
681 void subHour(int hours
);
683 void addMin(int noOfMins
);
685 void subMin(int noOfMins
);
687 void addSec(int noOfSecs
);
689 void subSec(int noOfSecs
);
691 int secDiff(TimeStamp
&ts
);
692 int minDiff(TimeStamp
&ts
);
693 int hourDiff(TimeStamp
&ts
);
694 int dayDiff(TimeStamp
&ts
);
695 int monthDiff(TimeStamp
&ts
);
696 int yearDiff(TimeStamp
&ts
);
703 #include<DataTypeInline.h>