1 /***************************************************************************
3 * Copyright (C) Lakshya Solutions Ltd. All rights reserved. *
5 ***************************************************************************/
12 //#include<sqltypes.h>
18 typedef int JulianRep
;
23 * Data Types supported by the database system.
28 typeInt
= 0, /**<integer type*/
52 * @class ComparisionOp
53 * Comparision operators supported by the database system.
64 OpLike
, // for Like operator
66 OpInvalidComparisionOp
68 static char CompOpNames
[][20] =
70 "Equals", "NotEquals", "LessThan", "LessThanEquals", "GreaterThan",
71 "GreaterThanEquals", "Like", "isNULL", "Invalid"
77 * Logical operators supported by the database system.
88 static char LogOpNames
[][10] =
90 "AND", "OR", "NOT", "Invalid"
93 class DllExport AllDataType
96 inline static long size(DataType type
, int length
=0);
97 static char* getSQLString(DataType type
);
98 static short convertToSQLType(DataType type
);
99 static short convertToCSQLSQLType(DataType type
);
100 static short convertToSQL_C_Type(DataType type
,TDBInfo tdbname
=mysql
);
101 static DataType
convertFromSQLType(short type
,int length
=0, int scale
=0, TDBInfo tdbname
=mysql
);
103 inline static void copyVal(void* dest
, void *src
, DataType type
, int length
= 0,int dbFlag
=0);
104 inline static void copyZeroVal(void* dest
, DataType type
, int length
= 0);
105 inline static void cachecopyVal(void* dest
, void *src
, DataType type
, int length
= 0,int dbFlag
=0);
106 inline static void addVal(void* dest
, void *src
, DataType type
);
107 static void divVal(double* dest
, int src
, DataType type
);
108 static void subVal(void* dest
, void *src
, DataType type
);
109 static void mulVal(void* dest
, void *src
, DataType type
);
110 static void mudVal(void* dest
, void *src
, DataType type
);
111 static void divVal(void* dest
, void *src
, DataType type
);
112 static void increment(void* dest
, void *src
, DataType type
);
113 static bool isValueZero(void* dest
, DataType type
);
115 inline static bool compareVal(void *src1
, void *src2
, ComparisionOp op
,
116 DataType type
, long length
= 0);
117 static bool compareIntVal(void* src1
, void* src2
, ComparisionOp op
);
118 static bool compareLongVal(void* src1
, void* src2
, ComparisionOp op
);
119 static bool compareLongLongVal(void* src1
, void* src2
, ComparisionOp op
);
120 static bool compareShortVal(void* src1
, void* src2
, ComparisionOp op
);
121 static bool compareByteIntVal(void* src1
, void* src2
, ComparisionOp op
);
122 static bool compareDoubleVal(void* src1
, void* src2
, ComparisionOp op
);
123 static bool compareFloatVal(void* src1
, void* src2
, ComparisionOp op
);
124 static bool compareDateVal(void* src1
, void* src2
, ComparisionOp op
);
125 static bool compareTimeVal(void* src1
, void* src2
, ComparisionOp op
);
126 static bool compareTimeStampVal(void* src1
, void* src2
, ComparisionOp op
);
127 static bool compareStringVal(void* src1
, void* src2
, ComparisionOp op
);
128 static bool compareBinaryVal(void* src1
, void* src2
,
129 ComparisionOp op
, int length
);
132 static void convert(DataType srcType
, void *src
, DataType destType
, void *dest
, int length
=0);
133 static void convertToInt(void* dest
, void* src
, DataType srcType
);
134 static void convertToLong(void* dest
, void* src
, DataType srcType
);
135 static void convertToLongLong(void* dest
, void* src
, DataType srcType
);
136 static void convertToShort(void* dest
, void* src
, DataType srcType
);
137 static void convertToByteInt(void* dest
, void* src
, DataType srcType
);
138 static void convertToFloat(void* dest
, void* src
, DataType srcType
);
139 static void convertToDouble(void* dest
, void* src
, DataType srcType
);
140 static void convertToString(void* dest
, void* src
, DataType srcType
, int length
=0,TDBInfo tdbname
=mysql
);
141 static void convertToDate(void* dest
, void* src
, DataType srcType
);
142 static void convertToTime(void* dest
, void* src
, DataType srcType
);
143 static void convertToTimeStamp(void* dest
, void* src
, DataType srcType
);
144 static void convertToBinary(void* dest
, void* src
, DataType srcType
, int length
);
145 static DataType
getCsqlTypeFromFunctionType(FunctionType type
);
146 static DataType
getCsqlTypeFromFunctionTypeForComparision(FunctionType type
);
147 static void memoryset(void *value
,DataType type
);
148 inline static ComparisionOp
getComparisionOperator(char *str
);
150 static void* alloc(DataType type
, int length
=0);
151 static DbRetVal
strToValue(void *dest
, char *src
, DataType type
, int length
=0);
152 static int printVal(void *src
, DataType type
, int length
,int dbFlag
=0);
153 inline static bool isValidFieldForAvg(DataType type
);
159 * Represents 8 bit integer.
163 class DllExport ByteInt
{
170 ByteInt(const ByteInt
&v
) { val
= v
.val
; }
171 /** constructor with char
172 * @param v char value
174 ByteInt(char v
) { val
= v
; }
175 operator int() const { return (int) val
; }
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^=(ByteInt v
) { return val
^= v
.val
; }
197 char operator^=(char v
) { return val
^= v
; }
198 char operator++() { return val
++; }
199 char operator++(int) { char tmp
= val
; val
++; return val
; }
200 char operator--() { return val
--; }
201 char operator--(int) { char tmp
= val
; val
--; return val
; }
210 * Represents Date Data type.
214 class DllExport Date
{ // The class a user would declare to hold date
217 Date() {julianDate
= 0;}
218 Date(JulianRep julian
) : julianDate(julian
) {}
220 /** constructor with year, month, day
225 Date(int year
, int month
, int day
);
227 Date(const Date
&d2
) { julianDate
= d2
.julianDate
; }
228 Date
& operator=(const Date
& d2
)
229 { julianDate
=d2
.julianDate
; return *this; }
231 /** sets the date with specified year, month, day
236 int set(int year
, int month
, int day
);
237 int set(const struct tm
*tmptr
);
239 /** get year, month, day of the date
240 * @param year year IN
241 * @param month month IN
244 int get(int &year
, int &month
, int &day
) const;
246 /** checks for the validity of the date
248 bool isValid() const;
250 /** resets the date to zero
252 void setNull() { julianDate
= 0;}
254 /** returns day of the week
256 int dayOfWeek() const;
258 /** returns day of the week.
259 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
261 const char *dayOfWeekName() const;
263 /** returns day of the week abbreviation
264 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
266 const char *dayOfWeekAbbr() const;
268 /** returns day of the week.
269 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
271 static const char *dayOfWeekName(int day
); // 0--> Sunday
273 /** returns day of the week abbreviation
274 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
276 static const char *dayOfWeekAbbr(int day
);
278 static int dayOfWeek(JulianRep juldate
);
281 void addDay(int noOfDays
);
282 void subDay(int noOfDays
);
283 void addMonth(int noOfMons
);
284 void subMonth(int noOfMons
);
285 void addYear(int noOfYrs
);
286 void subYear(int noOfYrs
);
288 /** returns the day of the month. Values are 1 to 31
290 int dayOfMonth() const;
292 int dayOfYear() const;
294 /** returns the month Values are 1 to 12.
298 /** returns the month name
299 * values are "January", "February", "March", "April", "May", "June",
300 * "July", "August", "September", "October", "November", "December"
302 const char *monthName() const;
304 /** returns the month name abbreviation
305 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
306 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
308 const char *monthAbbr() const;
310 /** returns the month name
311 * values are "January", "February", "March", "April", "May", "June",
312 * "July", "August", "September", "October", "November", "December"
314 static const char *monthName(int month
);
317 /** returns the month name abbreviation
318 * values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
319 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
321 static const char *monthAbbr(int month
);
323 /** parses the date string passed and stores it
324 *It should of the format "mm/dd/yyyy"
326 int parseFrom(const char *s
);
328 int getCalDay() { return julianDate
;}
329 static void changeToCsqlFormat(char *src
);
332 Date
&operator++() { julianDate
++; return *this; }
333 Date
&operator--() { julianDate
--; return *this; }
335 Date
&operator+=(int days
) { julianDate
+= days
; return *this;}
336 Date
&operator-=(int days
) { julianDate
-= days
; return *this;}
342 /** checks for the validity of the date
344 static bool isValidDate(int year
, int month
, int day
);
346 friend Date
operator+(const Date
&d1
, int days
);
347 friend Date
operator+(int days
, const Date
&d1
);
348 friend Date
operator-(const Date
&d1
, int days
);
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
);
352 friend int operator<=(const Date
&d1
,const Date
&d2
);
353 friend int operator>=(const Date
&d1
,const Date
&d2
);
354 friend int operator==(const Date
&d1
,const Date
&d2
);
355 friend int operator!=(const Date
&d1
,const Date
&d2
);
357 /** checks for leap year
359 static bool isLeapYear(int year
);
361 /** returns the number of days in the specified month of the year.
363 static int daysInMonth(int month
, int year
);
365 static int YMDToJulian(int year
,int mon
,int day
, JulianRep
&julian
);
366 static int julianToYMD(JulianRep julian
,int &year
,int &month
,int &day
);
369 JulianRep julianDate
;
377 * Represents Time Data type.
381 class DllExport Time
{ // The class a user would declare to hold time
383 Time() {timeVal
= 0;}
385 /** Overloaded constructor
391 Time(int hours
, int mins
, int secs
, int usec
= 0);
392 Time(int totalSecs
) : timeVal(totalSecs
) {;}
393 Time(const Time
&d2
) { timeVal
= d2
.timeVal
; }
394 Time
& operator=(const Time
& d2
) { timeVal
=d2
.timeVal
; return *this; }
396 /** sets the time with specified hours, mins, secs
402 int set(int hours
, int mins
, int secs
, int usec
= 0);
404 /** retrieves the time using IN parameters
409 int get(int &hours
, int &mins
, int &secs
) const;
414 int getCalSec(){ return timeVal
/10000; }
415 /** checks for the validity of the time
417 bool isValid() const;
421 void setNull() { timeVal
= -1;}
423 int secondsSinceMidnight() const { return timeVal
/10000;}
425 /** returns the microsecs
427 int usec() const; // to nearest 100 of usec.
429 /** returns the millisecs
437 /** returns the minutes
441 /** returns the hours
446 int addSec(int secs
);
447 int subSec(int secs
);
448 int addMin(int mins
);
449 int subMin(int mins
);
450 int addHour(int hours
);
451 int subHour(int hours
);
453 /** sets the millisecs
457 /** sets the microsecs
461 /** parses the time string passed and stores it
462 *It should of the format "hh:mm::ss"
464 int parseFrom(const char *s
);
466 Time
&operator++() { timeVal
+= 10000; return *this; }
467 Time
&operator--() { timeVal
-= 10000; return *this; }
469 Time
&operator+=(int seconds
) { timeVal
+= seconds
*10000; return *this; }
470 Time
&operator-=(int seconds
) { timeVal
-= seconds
*10000; return *this; }
473 /** checks for the validity of the time specified
475 static bool isValidTime(int hours
, int mins
, int secs
);
477 friend Time
operator+(const Time
&t1
, int seconds
);
478 friend Time
operator+(int seconds
, const Time
&t1
);
479 friend Time
operator-(const Time
&t1
, int seconds
);
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
);
483 friend int operator<=(const Time
&t1
,const Time
&t2
);
484 friend int operator>=(const Time
&t1
,const Time
&t2
);
485 friend int operator==(const Time
&t1
,const Time
&t2
);
486 friend int operator!=(const Time
&t1
,const Time
&t2
);
495 * Represents TimeStamp Data type.
499 class DllExport TimeStamp
{
504 /** Overloaded constructor
513 TimeStamp(int year
, int month
, int day
, int hour
, int minute
, int sec
, int usec
= 0) :
514 date(year
, month
, day
), time(hour
, minute
, sec
, usec
) { }
516 TimeStamp(const TimeStamp
&ts
)
517 { date
= ts
.date
; time
= ts
.time
; }
518 TimeStamp(const Date
&d
, Time
&t
) : date(d
), time(t
) {}
521 TimeStamp
& operator=(const TimeStamp
& d2
)
522 { date
=d2
.date
; time
= d2
.time
; return *this; }
524 /** get year, month, day from the date part of the timestamp
525 * @param year year IN
526 * @param month month IN
529 int getDate(int &year
, int &month
, int &day
)
530 { return date
.get(year
, month
, day
); }
532 /** get the date part of the timestamp
535 void getDate(Date
&newDate
) const
538 /** sets the date with specified year, month, day
543 int setDate(int year
, int month
, int day
)
544 { return date
.set(year
, month
, day
); }
546 /** set the date part of the timestamp
549 void setDate(const Date
&newDate
)
553 operator Date() { return date
; }
554 operator Time() { return time
; }
558 /** retrieves the time using IN parameters
563 int getTime(int &hours
, int &mins
, int &secs
) const
564 { return time
.get(hours
, mins
, secs
); }
565 /** retrieves the time part of the timestamp
566 * @param newTime Time
568 void getTime(Time
&newTime
) const
571 /** sets the time with specified hours, mins, secs
577 int setTime(int hours
, int mins
, int secs
, int usec
= 0)
578 { return time
.set(hours
, mins
, secs
, usec
); }
580 /** set the time part of the timestamp
581 * @param newTime Time
583 void setTime(const Time
&newTime
)
586 /** checks for the validity of the timestamp
588 bool isValid() const { return date
.isValid() && time
.isValid(); }
590 /** resets the date and time */
591 void setNull() { date
.setNull(); time
.setNull(); }
593 /** returns day of the week. Values are 1-7
595 int dayOfWeek() const { return date
.dayOfWeek(); }
597 /** returns day of the week.
598 * values are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Satur
601 const char *dayOfWeekName() const { return date
.dayOfWeekName(); }
603 /** returns day of the week abbreviation
604 * values are "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
606 const char *dayOfWeekAbbr() const { return date
.dayOfWeekAbbr(); }
608 /** returns the day of the month. Values are 1 to 31
610 int dayOfMonth() const { return date
.dayOfMonth(); }
611 int dayOfYear() const { return date
.dayOfYear(); }
613 /** returns the month. Values are 1 to 12.
616 int month() const { return date
.month(); }
618 /** returns the month name
619 * values are "January", "February", "March", "April", "May", "June",
620 * "July", "August", "September", "October", "November", "December"
622 const char *monthName() const { return date
.monthName(); }
624 /** returns the month name abbreviation
625 * Values are "Jan", "Feb", "Mar", "Apr", "May", "Jun",
626 * "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
628 const char *monthAbbr() const { return date
.monthAbbr(); }
632 int year() const { return date
.year(); }
634 int secondsSinceMidnight() const { return time
.secondsSinceMidnight(); }
635 /** returns the seconds */
636 int seconds() const { return time
.seconds(); }
637 /** returns the minutes */
638 int minutes() const { return time
.minutes(); }
639 /** returns the hours */
640 int hours() const { return time
.hours(); }
641 /** returns the millisecs */
642 int msec() const { return time
.msec(); }
643 /** returns the microsecs */
644 int usec() const { return time
.usec(); }
646 /** sets the millisecs */
647 int setMsec(int ms
) { return time
.setMsec(ms
) ; }
648 /** sets the microsecs */
649 int setUsec(int us
) { return time
.setUsec(us
) ; }
651 /** parses the date string passed and stores it
652 *It should of the format "mm/dd/yyyy"
654 int parseDateFrom(const char *s
) { return date
.parseFrom(s
); }
656 /** parses the time string passed and stores it
657 *It should of the format "hh:mm::ss"
660 int parseTimeFrom(const char *s
) { return time
.parseFrom(s
); }
662 static void changeToCsqlFormat(char *src
);
664 int parseFrom(const char *s
);
665 friend int operator<(const TimeStamp
&d1
, const TimeStamp
&d2
);
666 friend int operator>(const TimeStamp
&d1
, const TimeStamp
&d2
);
667 friend int operator<=(const TimeStamp
&d1
, const TimeStamp
&d2
);
668 friend int operator>=(const TimeStamp
&d1
, const TimeStamp
&d2
);
669 friend int operator==(const TimeStamp
&d1
, const TimeStamp
&d2
);
670 friend int operator!=(const TimeStamp
&d1
, const TimeStamp
&d2
);
671 void addYear(int noOfYrs
){ date
.addYear(noOfYrs
);}
673 void subYear(int noOfYrs
) { date
.subYear(noOfYrs
); }
675 void addMonth(int noOfMons
) { date
.addMonth(noOfMons
);}
677 void subMonth(int noOfMons
) { date
.subMonth(noOfMons
);}
679 void addDay(int noOfDays
) { date
.addDay(noOfDays
);}
681 void subDay(int noOfDays
) { date
.subDay(noOfDays
); }
683 void addHour(int hours
);
685 void subHour(int hours
);
687 void addMin(int noOfMins
);
689 void subMin(int noOfMins
);
691 void addSec(int noOfSecs
);
693 void subSec(int noOfSecs
);
695 int secDiff(TimeStamp
&ts
);
696 int minDiff(TimeStamp
&ts
);
697 int hourDiff(TimeStamp
&ts
);
698 int dayDiff(TimeStamp
&ts
);
699 int monthDiff(TimeStamp
&ts
);
700 int yearDiff(TimeStamp
&ts
);
707 #include<DataTypeInline.h>