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 #define SmallestValJulDate (1721426)
22 Date::Date(int year
, int month
, int day
)
23 { YMDToJulian(year
, month
, day
, julianDate
); }
24 int Date::set(int year
, int month
, int day
)
25 { return YMDToJulian(year
,month
,day
,julianDate
); }
26 int Date::get(int &year
, int &month
, int &day
) const
27 { return julianToYMD(julianDate
,year
,month
,day
); }
29 int Date::parseFrom(const char *s
) {
32 count
= sscanf(s
,"%d/%d/%d",&year
,&month
,&day
);
33 if (count
< 3) return -1;
35 if (year
< 100) year
+= 1900;
37 if (!isValidDate(year
, month
, day
))
39 return set(year
,month
,day
);
42 int Date::dayOfMonth() const {
47 int Date::month() const {
52 int Date::year() const {
58 int Date::dayOfWeek() const { return dayOfWeek(julianDate
); }
60 const char *Date::dayOfWeekName() const
61 { return dayOfWeekName(dayOfWeek(julianDate
)); }
63 const char *Date::dayOfWeekAbbr() const
64 { return dayOfWeekAbbr(dayOfWeek(julianDate
)); }
66 static const char *day_names
[] = {
67 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
69 static const char *day_abbrs
[] = {
70 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
73 static const char *month_names
[] = {
74 "January", "February", "March", "April", "May", "June",
75 "July", "August", "September", "October", "November", "December"
77 static const char *month_abbrs
[] = {
78 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
79 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
83 static int daysBeforeMonth
[] = {
84 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365
86 static int days_per_month
[] = {
87 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
90 const char * Date::dayOfWeekName(int day
)
91 { return (day
< 1 || day
> 7) ? 0 : day_names
[day
-1]; }
93 const char * Date::dayOfWeekAbbr(int day
)
94 { return (day
< 1 || day
> 7) ? 0 : day_abbrs
[day
-1]; }
96 const char * Date::monthName() const { return monthName(month()); }
97 const char * Date::monthAbbr() const { return monthAbbr(month()); }
98 const char * Date::monthName(int month
)
99 { return (month
< 1 || month
> 12) ? 0 : month_names
[month
-1]; }
100 const char * Date::monthAbbr(int month
)
101 { return (month
< 1 || month
> 12) ? 0 : month_abbrs
[month
-1]; }
102 Date
operator+(const Date
&d1
, int days
)
103 { return Date(d1
.julianDate
+ days
); }
104 Date
operator+(int days
, const Date
&d1
)
105 { return Date(d1
.julianDate
+ days
); }
106 Date
operator-(const Date
&d1
, int days
)
107 { return Date(d1
.julianDate
- days
); }
109 int operator-(const Date
&d1
, const Date
& d2
)
110 { return d1
.julianDate
- d2
.julianDate
; }
112 int operator<(const Date
&d1
,const Date
&d2
)
113 { return d1
.julianDate
< d2
.julianDate
; }
114 int operator>(const Date
&d1
,const Date
&d2
)
115 { return d1
.julianDate
> d2
.julianDate
; }
116 int operator<=(const Date
&d1
,const Date
&d2
)
117 { return d1
.julianDate
<= d2
.julianDate
; }
118 int operator>=(const Date
&d1
,const Date
&d2
)
119 { return d1
.julianDate
>= d2
.julianDate
; }
120 int operator==(const Date
&d1
,const Date
&d2
)
121 { return d1
.julianDate
== d2
.julianDate
; }
122 int operator!=(const Date
&d1
,const Date
&d2
)
123 { return d1
.julianDate
!= d2
.julianDate
; }
125 bool Date::isValid() const
126 { return julianDate
>= SmallestValJulDate
; }
128 bool Date::isLeapYear(int year
)
130 return (year
% 400 == 0) || ((year
% 4 == 0) && !(year
% 100 == 0));
133 int Date::dayOfYear() const {
136 int tmp
= daysBeforeMonth
[month
-1];
137 if (month
>= 3 && isLeapYear(year
))
142 int Date::daysInMonth(int month
, int year
) {
144 int tmp
= days_per_month
[month
];
145 if (month
== 1 && isLeapYear(year
)) tmp
++;
149 bool Date::isValidDate(int year
, int month
, int day
) {
150 if (year
< 1 || year
> 10000) return false;
151 if (month
< 1 || month
> 12) return false;
152 return (day
>= 1) && (day
<= daysInMonth(month
,year
));
155 // Algorithm Author: Robert G. Tantzen
156 int Date::YMDToJulian(int year
, int mon
, int day
, JulianRep
&jul
) {
157 if (!isValidDate(year
, mon
, day
))
158 { jul
= (JulianRep
)0; return -1; }
160 // year, month, day are assumed to be valid
161 int m
= mon
, d
= day
, y
= year
;
165 else { m
+= 9; --y
; }
168 j
= (146097*c
)/4 + (1461*ya
)/4 + (153*m
+2)/5 + d
+ 1721119;
173 // Julian date converter. Takes a julian date (the number of days since some
174 // distant epoch or other), and returns month, day of month, and year in
175 // integer references.
176 // Algorithm Author: Robert G. Tantzen
177 int Date::dayOfWeek(JulianRep jul
) {
178 return (int)((jul
+1)%7+1);
181 int Date::julianToYMD(JulianRep jul
, int &year
, int &month
, int &day
) {
185 if (j
< SmallestValJulDate
)
186 { year
= month
= day
= 0; return -1; }
189 y
= (4 * j
- 1)/146097;
190 j
= 4 * j
- 1 - 146097 * y
;
192 j
= (4 * d
+ 3)/1461;
193 d
= 4 * d
+ 3 - 1461 * j
;
196 d
= 5 * d
- 3 - 153 * m
;
200 else { m
-= 9; ++y
; }
207 #define MAX_VALID_SECONDS (60 * 60 * 24 -1)
208 Time::Time(int hours
, int mins
, int secs
, int usec
)
209 { set(hours
,mins
,secs
, usec
); }
211 int Time::set(int hours
, int mins
, int secs
, int usec
) {
212 if((hours
| mins
| secs
| usec
) < 0) { timeVal
= -1; return -1; }
213 if(hours
>= 24 | mins
>= 60 | secs
>= 62)
214 { timeVal
= -1; return -1; }
215 timeVal
= secs
+ mins
* 60 + hours
* 3600;
217 if(usec
) timeVal
+= usec
/100;
221 int Time::get(int &hours
, int &mins
, int &secs
) const {
222 if (timeVal
< 0) return -1;
223 int s
= timeVal
/10000;
232 int Time::seconds() const { return (timeVal
/10000) % 60; }
233 int Time::minutes() const { return (timeVal
/(60*10000)) % 60; }
234 int Time::hours() const { return timeVal
/ (3600*10000); }
235 int Time::msec() const { return (timeVal
% 10000) / 10; }
236 int Time::usec() const { return (timeVal
% 10000) * 100; }
238 int Time::setMsec(int ms
) {
239 if(ms
< 0 || ms
>= 1000) return -1;
240 timeVal
= timeVal
+(10*ms
);
243 int Time::setUsec(int us
) {
244 if(us
< 0 || us
>= 1000000) return -1;
245 timeVal
= timeVal
+us
/100;
249 bool Time::isValid() const
250 { return timeVal
>= 0 && timeVal
<= (10000*(MAX_VALID_SECONDS
+1)-1); }
252 bool Time::isValidTime(int hours
, int mins
, int secs
) {
253 return (hours
>= 0 && hours
< 24) &&
254 (mins
>= 0 && mins
< 60) &&
255 (secs
>= 0 && secs
< 60);
258 Time
operator+(const Time
&t1
, int seconds
)
259 { return Time(t1
.timeVal
+ seconds
*10000); }
260 Time
operator+(int seconds
, const Time
&t1
)
261 { return Time(t1
.timeVal
+ seconds
*10000); }
262 Time
operator-(const Time
&t1
, int seconds
)
263 { return Time(t1
.timeVal
- seconds
*10000); }
265 int operator-(const Time
&t1
, const Time
& t2
)
266 { return (t1
.timeVal
- t2
.timeVal
)/10000; }
268 int operator<(const Time
&t1
,const Time
&t2
)
269 { return t1
.timeVal
< t2
.timeVal
; }
270 int operator>(const Time
&t1
,const Time
&t2
)
271 { return t1
.timeVal
> t2
.timeVal
; }
272 int operator<=(const Time
&t1
,const Time
&t2
)
273 { return t1
.timeVal
<= t2
.timeVal
; }
274 int operator>=(const Time
&t1
,const Time
&t2
)
275 { return t1
.timeVal
>= t2
.timeVal
; }
276 int operator==(const Time
&t1
,const Time
&t2
)
277 { return t1
.timeVal
== t2
.timeVal
; }
278 int operator!=(const Time
&t1
,const Time
&t2
)
279 { return t1
.timeVal
!= t2
.timeVal
; }
281 int Time::parseFrom(const char *s
) {
284 count
= sscanf(s
,"%d:%d:%d",&hours
,&mins
,&secs
);
285 if (count
< 2) return -1;
286 if (count
== 2) secs
= 0;
288 if (!isValidTime(hours
,mins
,secs
))
290 return set(hours
,mins
,secs
);
292 int TimeStamp::parseFrom(const char *s
) {
296 count
= sscanf(s
,"%d/%d/%d %d:%d:%d",&year
,&month
,&day
, &hours
, &mins
, &secs
);
297 if (count
< 5) return -1;
298 if (count
== 5) secs
= 0;
300 if (year
< 100) year
+= 1900;
302 if (!date
.isValidDate(year
, month
, day
))
305 setDate(year
,month
,day
);
308 if (!time
.isValidTime(hours
,mins
,secs
))
310 return setTime(hours
,mins
,secs
);
313 int operator< (const TimeStamp
&d1
, const TimeStamp
&d2
)
314 { return (d1
.date
!= d2
.date
) ? d1
.date
< d2
.date
: d1
.time
< d2
.time
; }
315 int operator> (const TimeStamp
&d1
, const TimeStamp
&d2
)
316 { return (d1
.date
!= d2
.date
) ? d1
.date
> d2
.date
: d1
.time
> d2
.time
; }
317 int operator<=(const TimeStamp
&d1
, const TimeStamp
&d2
)
318 { return (d1
.date
!= d2
.date
) ? d1
.date
< d2
.date
: d1
.time
<= d2
.time
; }
319 int operator>=(const TimeStamp
&d1
, const TimeStamp
&d2
)
320 { return (d1
.date
!= d2
.date
) ? d1
.date
> d2
.date
: d1
.time
>= d2
.time
; }
321 int operator==(const TimeStamp
&d1
, const TimeStamp
&d2
)
322 { return d1
.date
== d2
.date
&& d1
.time
== d2
.time
; }
323 int operator!=(const TimeStamp
&d1
, const TimeStamp
&d2
)
324 { return d1
.date
!= d2
.date
&& d1
.time
!= d2
.time
; }
327 long AllDataType::size(DataType type
, int length
)
329 if (type
== typeInt
) return sizeof(int);
330 else if (type
== typeString
) return length
;
341 size
= sizeof(long long);
344 size
= sizeof(short);
350 size
= sizeof(double);
353 size
= sizeof(float);
357 //fldDef.length_ = sizeof(long double);
366 size
= sizeof(TimeStamp
);
378 char* AllDataType::getSQLString(DataType type
)
382 case typeInt
: return "INT";
383 case typeLong
: return "INT";
384 case typeLongLong
: return "BIGINT";
385 case typeShort
: return "SMALLINT";
386 case typeByteInt
: return "TINYINT";
387 case typeDouble
: return "REAL";
388 case typeFloat
: return "FLOAT";
389 case typeDate
: return "DATE";
390 case typeTime
: return "TIME";
391 case typeTimeStamp
: return "TIMESTAMP";
392 case typeString
: return "CHAR";
393 case typeBinary
: return "BINARY";
394 default: return "UNKNOWN";
399 SQLSMALLINT
AllDataType::convertToSQLType(DataType type
)
423 return SQL_TYPE_DATE
;
425 return SQL_TYPE_TIME
;
427 return SQL_TYPE_TIMESTAMP
;
435 SQLSMALLINT
AllDataType::convertToSQL_C_Type(DataType type
)
444 return SQL_C_SBIGINT
;
448 return SQL_C_STINYINT
;
457 return SQL_C_TYPE_DATE
;
459 return SQL_C_TYPE_TIME
;
461 return SQL_C_TYPE_TIMESTAMP
;
470 DataType
AllDataType::convertFromSQLType(SQLSMALLINT type
)
487 case SQL_TYPE_TIMESTAMP
:
488 return typeTimeStamp
;
498 void AllDataType::copyVal(void* dest
, void *src
, DataType type
, int length
)
503 *(int*)dest
= *(int*)src
;
506 *(long*)dest
= *(long*)src
;
509 *(long long*)dest
= *(long long*)src
;
512 *(short*)dest
= *(short*)src
;
515 *(char*)dest
= *(char*)src
;
518 *(double*)dest
= *(double*)src
;
521 *(float*)dest
= *(float*)src
;
526 *(Date
*)dest
= *(Date
*)src
;
529 *(Time
*)dest
= *(Time
*)src
;
532 *(TimeStamp
*)dest
= *(TimeStamp
*)src
;
536 strncpy((char*)dest
, (char*)src
, length
);
537 char *d
=(char*)dest
;
542 os::memcpy(dest
, src
, length
);
548 void AllDataType::addVal(void* dest
, void *src
, DataType type
)
553 *(int*)dest
= *(int*)dest
+ *(int*)src
;
556 *(long*)dest
= *(long*)dest
+ *(long*)src
;
559 *(long long*)dest
= *(long long*)dest
+ *(long long*)src
;
562 *(short*)dest
= *(short*)dest
+ *(short*)src
;
565 *(char*)dest
= *(char*)dest
+ *(char*)src
;
568 *(double*)dest
= *(double*)dest
+ *(double*)src
;
571 *(float*)dest
= *(float*)dest
+ *(float*)src
;
585 void AllDataType::divVal(void* dest
, int src
, DataType type
)
590 *(int*)dest
= *(int*)dest
/ src
;
593 *(long*)dest
= *(long*)dest
/ src
;
596 *(long long*)dest
= *(long long*)dest
/ src
;
599 *(short*)dest
= *(short*)dest
/ src
;
602 *(char*)dest
= *(char*)dest
/ src
;
605 *(double*)dest
= *(double*)dest
/ src
;
608 *(float*)dest
= *(float*)dest
/ src
;
625 bool AllDataType::compareVal(void *val1
, void *val2
, ComparisionOp op
,
626 DataType type
, long length
)
632 result
= AllDataType::compareIntVal(val1
, val2
, op
);
635 result
= AllDataType::compareLongVal(val1
, val2
, op
);
638 result
= AllDataType::compareLongLongVal(val1
, val2
, op
);
641 result
= AllDataType::compareShortVal(val1
, val2
, op
);
644 result
= AllDataType::compareByteIntVal(val1
, val2
, op
);
647 result
= AllDataType::compareDoubleVal(val1
, val2
, op
);
650 result
= AllDataType::compareFloatVal(val1
, val2
, op
);
656 result
= AllDataType::compareDateVal(val1
, val2
, op
);
659 result
= AllDataType::compareTimeVal(val1
, val2
, op
);
662 result
= AllDataType::compareTimeStampVal(val1
, val2
, op
);
665 result
= AllDataType::compareStringVal(val1
, val2
, op
);
669 result
= AllDataType::compareBinaryVal(val1
, val2
, op
, length
);
675 bool AllDataType::compareIntVal(void* src1
, void *src2
, ComparisionOp op
)
677 if (src1
== NULL
) printf("src1 is null\n");
678 if (src2
== NULL
) printf("src2 is null\n");
683 if (*(int*)src1
== *(int*)src2
) result
= true;
687 if (*(int*)src1
!= *(int*)src2
) result
= true;
691 if (*(int*)src1
< *(int*)src2
) result
= true;
694 case OpLessThanEquals
:
695 if (*(int*)src1
<= *(int*)src2
) result
= true;
699 if (*(int*)src1
> *(int*)src2
) result
= true;
702 case OpGreaterThanEquals
:
703 if (*(int*)src1
>= *(int*)src2
) result
= true;
710 bool AllDataType::compareLongVal(void* src1
, void *src2
, ComparisionOp op
)
716 if (*(long*)src1
== *(long*)src2
) result
= true;
720 if (*(long*)src1
!= *(long*)src2
) result
= true;
724 if (*(long*)src1
< *(long*)src2
) result
= true;
727 case OpLessThanEquals
:
728 if (*(long*)src1
<= *(long*)src2
) result
= true;
732 if (*(long*)src1
> *(long*)src2
) result
= true;
735 case OpGreaterThanEquals
:
736 if (*(long*)src1
>= *(long*)src2
) result
= true;
743 bool AllDataType::compareLongLongVal(void* src1
, void *src2
,
750 if (*(long long*)src1
== *(long long*)src2
) result
= true;
754 if (*(long long*)src1
!= *(long long*)src2
) result
= true;
758 if (*(long long*)src1
< *(long long*)src2
) result
= true;
761 case OpLessThanEquals
:
762 if (*(long long*)src1
<= *(long long*)src2
) result
= true;
766 if (*(long long*)src1
> *(long long*)src2
) result
= true;
769 case OpGreaterThanEquals
:
770 if (*(long long*)src1
>= *(long long*)src2
) result
= true;
777 bool AllDataType::compareShortVal(void* src1
, void *src2
, ComparisionOp op
)
783 if (*(short*)src1
== *(short*)src2
) result
= true;
787 if (*(short*)src1
!= *(short*)src2
) result
= true;
791 if (*(short*)src1
< *(short*)src2
) result
= true;
794 case OpLessThanEquals
:
795 if (*(short*)src1
<= *(short*)src2
) result
= true;
799 if (*(short*)src1
> *(short*)src2
) result
= true;
802 case OpGreaterThanEquals
:
803 if (*(short*)src1
>= *(short*)src2
) result
= true;
810 bool AllDataType::compareByteIntVal(void* src1
, void *src2
, ComparisionOp op
)
816 if (*(ByteInt
*)src1
== *(ByteInt
*)src2
) result
= true;
820 if (*(ByteInt
*)src1
!= *(ByteInt
*)src2
) result
= true;
824 if (*(ByteInt
*)src1
< *(ByteInt
*)src2
) result
= true;
827 case OpLessThanEquals
:
828 if (*(ByteInt
*)src1
<= *(ByteInt
*)src2
) result
= true;
832 if (*(ByteInt
*)src1
> *(ByteInt
*)src2
) result
= true;
835 case OpGreaterThanEquals
:
836 if (*(ByteInt
*)src1
>= *(ByteInt
*)src2
) result
= true;
843 bool AllDataType::compareDoubleVal(void* src1
, void *src2
, ComparisionOp op
)
849 if (*(double*)src1
== *(double*)src2
) result
= true;
853 if (*(double*)src1
!= *(double*)src2
) result
= true;
857 if (*(double*)src1
< *(double*)src2
) result
= true;
860 case OpLessThanEquals
:
861 if (*(double*)src1
<= *(double*)src2
) result
= true;
865 if (*(double*)src1
> *(double*)src2
) result
= true;
868 case OpGreaterThanEquals
:
869 if (*(double*)src1
>= *(double*)src2
) result
= true;
876 bool AllDataType::compareFloatVal(void* src1
, void *src2
, ComparisionOp op
)
882 if (*(float*)src1
== *(float*)src2
) result
= true;
886 if (*(float*)src1
!= *(float*)src2
) result
= true;
890 if (*(float*)src1
< *(float*)src2
) result
= true;
893 case OpLessThanEquals
:
894 if (*(float*)src1
<= *(float*)src2
) result
= true;
898 if (*(float*)src1
> *(float*)src2
) result
= true;
901 case OpGreaterThanEquals
:
902 if (*(float*)src1
>= *(float*)src2
) result
= true;
909 bool AllDataType::compareDateVal(void* src1
, void *src2
, ComparisionOp op
)
915 if (*(Date
*)src1
== *(Date
*)src2
) result
= true;
919 if (*(Date
*)src1
!= *(Date
*)src2
) result
= true;
923 if (*(Date
*)src1
< *(Date
*)src2
) result
= true;
926 case OpLessThanEquals
:
927 if (*(Date
*)src1
<= *(Date
*)src2
) result
= true;
931 if (*(Date
*)src1
> *(Date
*)src2
) result
= true;
934 case OpGreaterThanEquals
:
935 if (*(Date
*)src1
>= *(Date
*)src2
) result
= true;
942 bool AllDataType::compareTimeVal(void* src1
, void *src2
, ComparisionOp op
)
948 if (*(Time
*)src1
== *(Time
*)src2
) result
= true;
952 if (*(Time
*)src1
!= *(Time
*)src2
) result
= true;
956 if (*(Time
*)src1
< *(Time
*)src2
) result
= true;
959 case OpLessThanEquals
:
960 if (*(Time
*)src1
<= *(Time
*)src2
) result
= true;
964 if (*(Time
*)src1
> *(Time
*)src2
) result
= true;
967 case OpGreaterThanEquals
:
968 if (*(Time
*)src1
>= *(Time
*)src2
) result
= true;
975 bool AllDataType::compareTimeStampVal(void* src1
, void *src2
, ComparisionOp op
)
981 if (*(TimeStamp
*)src1
== *(TimeStamp
*)src2
) result
= true;
985 if (*(TimeStamp
*)src1
!= *(TimeStamp
*)src2
) result
= true;
989 if (*(TimeStamp
*)src1
< *(TimeStamp
*)src2
) result
= true;
992 case OpLessThanEquals
:
993 if (*(TimeStamp
*)src1
<= *(TimeStamp
*)src2
) result
= true;
997 if (*(TimeStamp
*)src1
> *(TimeStamp
*)src2
) result
= true;
1000 case OpGreaterThanEquals
:
1001 if (*(TimeStamp
*)src1
>= *(TimeStamp
*)src2
) result
= true;
1002 else result
= false;
1008 bool AllDataType::compareStringVal(void* src1
, void *src2
, ComparisionOp op
)
1010 bool result
= false;
1011 int ret
= strcmp((char*)src1
, (char*)src2
);
1015 if (ret
== 0 ) result
= true; else result
= false;
1018 if (ret
!= 0 ) result
= true; else result
= false;
1021 if (ret
< 0 ) result
= true; else result
= false;
1023 case OpLessThanEquals
:
1024 printf("Illegal Operator:Not Supported for String\n");
1027 if (ret
> 0 ) result
= true; else result
= false;
1029 case OpGreaterThanEquals
:
1030 printf("Illegal Operator:Not Supported for String\n");
1036 bool AllDataType::compareBinaryVal(void* src1
, void *src2
,
1037 ComparisionOp op
, int length
)
1039 bool result
= false;
1040 int ret
= os::memcmp(src1
, src2
, length
);
1044 if (ret
== 0 ) result
= true; else result
= false;
1047 if (ret
!= 0 ) result
= true; else result
= false;
1050 if (ret
< 0 ) result
= true; else result
= false;
1052 case OpLessThanEquals
:
1053 printf("Illegal Operator:Not Supported for Binary\n");
1056 if (ret
> 0 ) result
= true; else result
= false;
1058 case OpGreaterThanEquals
:
1059 printf("Illegal Operator:Not Supported for Binary\n");
1067 ComparisionOp
AllDataType::getComparisionOperator(char *str
)
1070 if (strcmp(str
, "<=") == 0)
1071 op
= OpLessThanEquals
;
1072 else if (strcmp(str
, ">=") == 0)
1073 op
= OpGreaterThanEquals
;
1074 else if (strcmp(str
, "<") == 0)
1076 else if (strcmp(str
, ">") == 0)
1078 else if (strcmp(str
, "=") == 0)
1080 else if (strcmp(str
, "!=") == 0 || strcmp(str
, "<>") == 0 )
1082 else if (strcasecmp(str
, "LIKE") == 0 )
1085 op
= OpInvalidComparisionOp
;
1089 void* AllDataType::alloc(DataType type
, int length
)
1095 dest
= malloc(sizeof(int));
1098 dest
= malloc(sizeof(long));
1101 dest
= malloc(sizeof(long long));
1104 dest
= malloc(sizeof(short));
1107 dest
= malloc(sizeof(char));
1110 dest
= malloc(sizeof(double));
1113 dest
= malloc(sizeof(float));
1117 //fldDef.length_ = sizeof(long double);
1120 if (length
== 0 ) return NULL
;
1121 dest
= malloc(length
);
1124 if (length
== 0 || length
> 256 ) return NULL
;
1125 dest
= malloc(length
);
1126 memset(dest
, 0, length
);
1129 dest
= malloc(sizeof(Date
));
1132 dest
= malloc(sizeof(Time
));
1135 dest
= malloc(sizeof(TimeStamp
));
1140 DbRetVal
AllDataType::strToValue(void* dest
, char *src
, DataType type
, int length
)
1146 sscanf( src
, "%d", &val
);
1151 sscanf( src
, "%ld", &val
);
1154 case typeLongLong
: {
1156 sscanf( src
, "%lld", &val
);
1157 *(long long*)dest
= val
;
1161 sscanf( src
, "%hd", &val
);
1162 *(short*)dest
= val
;
1166 sscanf( src
, "%c", &val
);
1171 sscanf( src
, "%lg", &val
);
1172 *(double*)dest
= val
;
1176 sscanf( src
, "%f", &val
);
1177 *(float*)dest
= val
;
1182 strncpy((char*)dest
, (char*)src
, length
);
1183 char *d
=(char*)dest
;
1188 res
= sscanf( src
, "%d-%d-%d", &y
, &m
, &d
);
1190 res
= sscanf( src
, "%d/%d/%d", &y
, &m
, &d
);
1193 fprintf(stderr
,"Error reading date. yyyy{-/}mm{-/}dd is the valid format.");
1196 Date
dateObj(y
,m
,d
);
1197 *(Date
*)dest
= dateObj
;
1201 res
= sscanf( src
, "%d:%d:%d", &h
, &m
, &s
);
1204 fprintf(stderr
, "Error reading time, hh:mm:ss is the valid format.");
1207 Time
timeObj(h
,m
,s
);
1208 *(Time
*)dest
= timeObj
;
1210 case typeTimeStamp
: {
1211 int d
,m
,y
, h
,mn
,s
, res
=0;
1212 res
= sscanf( src
, "%d-%d-%d %d:%d:%d", &y
, &m
, &d
, &h
, &mn
, &s
);
1214 res
= sscanf( src
, "%d-%d-%d, %d:%d:%d", &y
, &m
, &d
, &h
, &mn
, &s
);
1216 res
= sscanf( src
, "%d/%d/%d %d:%d:%d", &y
, &m
, &d
, &h
, &mn
, &s
);
1218 res
= sscanf( src
, "%d/%d/%d, %d:%d:%d", &y
, &m
, &d
, &h
, &mn
, &s
);
1221 fprintf(stderr
, "Error reading timestamp, yyyy{-/}mm{-/}dd[,] hh:mm:ss is the valid format.");
1224 TimeStamp
timeStampObj(y
,m
,d
,h
,mn
,s
);
1225 *(TimeStamp
*)dest
= timeStampObj
;
1228 memset ((void *) dest
, 0, length
* 2);
1229 unsigned char c
= 0;
1230 const char *str
= (const char *)src
;
1231 unsigned char *val
= (unsigned char *)dest
;
1233 while (i
< length
* 2) {
1235 if (c
== '\0') { *val
= *val
| c
; break; }
1236 if (!isxdigit((int)c
)) {
1237 printError(ErrBadArg
, "Invalid hexadecimal value");
1240 if (c
<= '9') c
-= '0';
1241 else if (c
>= 'a') c
= c
- 'a' + 10;
1242 else c
= c
- 'A' + 10;
1243 if (i
% 2) { *val
= c
; *val
<<= 4; }
1244 else { *val
= *val
| c
; val
++; }
1255 void AllDataType::convert(DataType srcType
, void *src
,
1256 DataType destType
, void *dest
, int length
)
1258 switch ((DataType
) destType
)
1260 case typeInt
: convertToInt(dest
, src
, srcType
); break;
1261 case typeLong
: convertToLong(dest
, src
, srcType
); break;
1262 case typeLongLong
: convertToLongLong(dest
, src
, srcType
); break;
1263 case typeShort
: convertToShort(dest
, src
, srcType
); break;
1264 case typeByteInt
: convertToByteInt(dest
, src
, srcType
); break;
1266 case typeFloat
: convertToFloat(dest
, src
, srcType
); break;
1267 case typeDouble
: convertToDouble(dest
, src
, srcType
); break;
1270 case typeDecimal
: convertToDouble(dest
, src
, srcType
); break;
1272 case typeString
: convertToString(dest
, src
, srcType
); break;
1273 case typeBinary
: convertToBinary(dest
, src
, srcType
,length
); break;
1274 case typeDate
: convertToDate(dest
, src
, srcType
); break;
1275 case typeTime
: convertToTime(dest
, src
, srcType
); break;
1276 case typeTimeStamp
: convertToTimeStamp(dest
, src
, srcType
); break;
1281 void AllDataType::convertToInt( void* dest
, void* src
, DataType srcType
)
1285 case typeInt
: *(int *)dest
= *(int *)src
; break;
1286 case typeLong
: *(int *)dest
=(int) *(long *)src
; break;
1287 case typeLongLong
: *(int *)dest
=(int) *(long long *)src
; break;
1288 case typeShort
: *(int *)dest
=(int) *(short *)src
; break;
1289 case typeByteInt
: *(int *)dest
=(int) *(char *)src
; break;
1291 case typeFloat
: *(int *)dest
= (int) *(float *)src
; break;
1292 case typeDouble
: *(int *)dest
=(int) *(double *)src
; break;
1294 case typeString
: sscanf((const char*)src
, "%d", (int*) dest
); break;
1300 default: *(int *)dest
= (int) 0;
1304 void AllDataType::convertToLong( void* dest
, void* src
, DataType srcType
)
1308 case typeInt
: *(long *)dest
=(long) *(int *)src
; break;
1309 case typeLong
: *(long *)dest
= *(long *)src
; break;
1310 case typeLongLong
: *(long *)dest
=(long) *(long long *)src
; break;
1311 case typeShort
: *(long *)dest
=(long) *(short *)src
; break;
1312 case typeByteInt
: *(long *)dest
=(long) *(char *)src
; break;
1314 case typeFloat
: *(long *)dest
= (long) *(float *)src
; break;
1315 case typeDouble
: *(long *)dest
=(long) *(double *)src
; break;
1317 case typeString
: sscanf((const char*)src
, "%ld", (long*) dest
); break;
1323 default: *(long *)dest
= (long) 0;
1328 void AllDataType::convertToLongLong( void* dest
, void* src
, DataType srcType
)
1332 case typeInt
: *(long long *)dest
=(long long) *(int *)src
; break;
1333 case typeLong
: *(long long *)dest
= (long long) *(long *)src
; break;
1334 case typeLongLong
: *(long long *)dest
= *(long long *)src
; break;
1335 case typeShort
: *(long long *)dest
=(long long) *(short *)src
; break;
1336 case typeByteInt
: *(long long *)dest
=(long long) *(char *)src
; break;
1338 case typeFloat
: *(long long *)dest
= (long long) *(float *)src
; break;
1339 case typeDouble
: *(long long *)dest
=(long long) *(double *)src
; break;
1341 case typeString
: sscanf((const char*)src
, "%lld", (long long*) dest
); break;
1347 default: *(long long *)dest
= (long long) 0;
1351 void AllDataType::convertToShort( void* dest
, void* src
, DataType srcType
)
1355 case typeInt
: *(short*)dest
=(short) *(int*)src
; break;
1356 case typeLong
: *(short*)dest
= (short) *(long*)src
; break;
1357 case typeLongLong
: *(short*)dest
= (short) *(long long*)src
; break;
1358 case typeShort
: *(short*)dest
= *(short*)src
; break;
1359 case typeByteInt
: *(short*)dest
=(short) *(char *)src
; break;
1361 case typeFloat
: *(short*)dest
= (short) *(float *)src
; break;
1362 case typeDouble
: *(short*)dest
=(short) *(double *)src
; break;
1364 case typeString
: sscanf((const char*)src
, "%hd", (short*) dest
); break;
1370 default: *(short*)dest
= (short) 0;
1374 void AllDataType::convertToByteInt( void* dest
, void* src
, DataType srcType
)
1378 case typeInt
: *(char*)dest
= (char) *(int*)src
; break;
1379 case typeLong
: *(char*)dest
= (char) *(long*)src
; break;
1380 case typeLongLong
: *(char*)dest
= (char) *(long long*)src
; break;
1381 case typeShort
: *(char*)dest
= (char) *(short*)src
; break;
1382 case typeByteInt
: *(char*)dest
= *(char *)src
; break;
1384 case typeFloat
: *(char*)dest
= (char) *(float *)src
; break;
1385 case typeDouble
: *(char*)dest
=(char) *(double *)src
; break;
1387 case typeString
: sscanf((const char*)src
, "%c", (char*) dest
); break;
1393 default: *(char*)dest
= (char) 0;
1397 void AllDataType::convertToFloat( void* dest
, void* src
, DataType srcType
)
1401 case typeInt
: *(float *)dest
=(float) *(int *)src
; break;
1402 case typeLong
: *(float *)dest
=(float) *(long *)src
; break;
1403 case typeLongLong
: *(float *)dest
=(float) *(long long *)src
; break;
1404 case typeShort
: *(float *)dest
=(float) *(short *)src
; break;
1405 case typeByteInt
: *(float *)dest
=(float) *(char *)src
; break;
1407 case typeFloat
: *(float *)dest
= *(float *)src
; break;
1408 case typeDouble
: *(float *)dest
=(float) *(double *)src
; break;
1410 case typeString
: sscanf((const char*)src
, "%f", (float*) dest
); break;
1416 default: *(float *)dest
= (float) 0;
1420 void AllDataType::convertToDouble( void* dest
, void* src
, DataType srcType
)
1424 case typeInt
: *(double *)dest
=(double) *(int *)src
; break;
1425 case typeLong
: *(double *)dest
=(double) *(long *)src
; break;
1426 case typeLongLong
: *(double *)dest
=(double) *(long long *)src
; break;
1427 case typeShort
: *(double *)dest
=(double) *(short *)src
; break;
1428 case typeByteInt
: *(double *)dest
=(double) *(char *)src
; break;
1430 case typeFloat
: *(double *)dest
=(double) *(float *)src
; break;
1431 case typeDouble
: *(double *)dest
= *(double *)src
; break;
1433 case typeString
: sscanf((const char*)src
, "%lf", (double*) dest
); break;
1439 default: *(double *)dest
= (double) 0;
1443 void AllDataType::convertToString( void* dest
, void* src
, DataType srcType
, int length
)
1449 sprintf ((char *)dest
, "%d", *(int *)src
);
1454 sprintf ((char *)dest
, "%ld", *(long *)src
);
1459 sprintf ((char *)dest
, "%lld", *(long long *)src
);
1464 sprintf ((char *)dest
, "%hd", *(short *)src
);
1469 sprintf ((char *)dest
, "%hd", *(char *)src
);
1475 sprintf ((char *)dest
, "%f", *(float *)src
);
1480 sprintf ((char *) dest
, "%lf", *(double *)src
);
1486 strcpy((char*)dest
, (char*)src
);
1491 Date
* dt
= (Date
*)src
;
1492 sprintf((char*) dest
, "%d/%d/%d", dt
->year(),
1493 dt
->month(), dt
->dayOfMonth());
1498 Time
* tm
= (Time
*)src
;
1499 sprintf((char*)dest
,"%d:%d:%d.%d", tm
->hours(), tm
->minutes(), tm
->seconds(), 0);
1504 TimeStamp
* tm
= (TimeStamp
*)src
;
1505 sprintf((char*)dest
, "%d/%d/%d %d:%d:%d.%d", tm
->year(),
1506 tm
->month(), tm
->dayOfMonth(), tm
->hours(),
1507 tm
->minutes(), tm
->seconds(), 0 );
1512 unsigned char *c
= (unsigned char *) src
;
1513 unsigned char *str
= (unsigned char *) dest
;
1514 unsigned char p
= 0;
1516 while (i
< length
) {
1518 if (p
< 10) sprintf ((char *)str
++, "%c", '0' + p
);
1519 else sprintf((char *)str
++, "%c", 'A' + p
- 10);
1521 if (p
< 10) sprintf ((char *)str
++, "%c", '0' + p
);
1522 else sprintf((char *)str
++, "%c", 'A' + p
- 10);
1528 default: ((char*)dest
)[0] = '\0';
1532 void AllDataType::convertToDate( void* dest
, void* src
, DataType srcType
)
1548 Date
*dt
= (Date
*) dest
;
1549 dt
->parseFrom((char*)src
);
1552 default: ((char*)dest
)[0] = '\0';
1556 void AllDataType::convertToTime( void* dest
, void* src
, DataType srcType
)
1572 Time
*dt
= (Time
*) dest
;
1573 dt
->parseFrom((char*)src
);
1576 default: ((char*)dest
)[0] = '\0';
1580 void AllDataType::convertToTimeStamp( void* dest
, void* src
, DataType srcType
)
1596 TimeStamp
*dt
= (TimeStamp
*) dest
;
1597 dt
->parseFrom((char*)src
);
1600 default: ((char*)dest
)[0] = '\0';
1604 void AllDataType::convertToBinary(void *dest
, void *src
, DataType srcType
, int length
)
1610 unsigned char c
= 0;
1611 const char *str
= (const char *)src
;
1612 unsigned char *val
= (unsigned char *)dest
;
1614 while (i
< length
* 2) {
1616 if (c
== '\0') { *val
= *val
| c
; break; }
1617 if (c
<= '9') c
-= '0';
1618 else if (c
>= 'a') c
= c
- 'a' + 10;
1619 else c
= c
- 'A' + 10;
1620 if (i
% 2) { *val
= c
; *val
<<= 4; }
1621 else { *val
= *val
| c
; val
++; }
1628 int AllDataType::printVal(void* src
, DataType srcType
, int length
)
1635 count
= printf ("%d", *(int *)src
);
1640 count
= printf ("%ld", *(long *)src
);
1645 count
= printf ("%lld", *(long long *)src
);
1650 count
= printf("%hd", *(short *)src
);
1655 count
= printf("%hd", *(char *)src
);
1661 count
= printf("%f", *(float *)src
);
1666 count
= printf("%lf", *(double *)src
);
1672 count
= printf("%s", (char*)src
);
1677 Date
* dt
= (Date
*)src
;
1678 count
= printf("%d/%d/%d", dt
->year(),
1679 dt
->month(), dt
->dayOfMonth());
1684 Time
* tm
= (Time
*)src
;
1685 count
= printf("%d:%d:%d.%d", tm
->hours(), tm
->minutes(), tm
->seconds(), 0);
1690 TimeStamp
* tm
= (TimeStamp
*)src
;
1691 count
= printf("%d/%d/%d %d:%d:%d.%d", tm
->year(),
1692 tm
->month(), tm
->dayOfMonth(), tm
->hours(),
1693 tm
->minutes(), tm
->seconds(), 0 );
1698 unsigned char *c
= (unsigned char *) src
;
1699 unsigned char p
= 0;
1701 while (i
< length
) {
1703 if (p
< 10) printf ("%c", '0' + p
);
1704 else printf("%c", 'A' + p
- 10);
1706 if (p
< 10) printf ("%c", '0' + p
);
1707 else printf("%c", 'A' + p
- 10);
1713 default: { printf("DataType not supported\n"); break; }