code reorg
[csql.git] / src / server / DataType.cxx
blob8e5839ad296790d311e51983289c1891dc661a15
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
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. *
9 * *
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. *
14 * *
15 ***************************************************************************/
16 #include<os.h>
17 #include<DataType.h>
18 #include<Debug.h>
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) {
30 int month,day,year;
31 int count;
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))
38 return -1;
39 return set(year,month,day);
42 int Date::dayOfMonth() const {
43 int year, month, day;
44 get(year,month,day);
45 return day;
47 int Date::month() const {
48 int year, month, day;
49 get(year,month,day);
50 return month;
52 int Date::year() const {
53 int year, month, day;
54 get(year,month,day);
55 return year;
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 {
134 int year,month,day;
135 get(year,month,day);
136 int tmp = daysBeforeMonth[month-1];
137 if (month >= 3 && isLeapYear(year))
138 tmp++;
139 return tmp + day;
142 int Date::daysInMonth(int month, int year) {
143 --month;
144 int tmp = days_per_month[month];
145 if (month == 1 && isLeapYear(year)) tmp++;
146 return 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;
162 int c, ya, j;
164 if (m > 2) m -= 3;
165 else { m += 9; --y; }
166 c = y/100;
167 ya = y - (100 * c);
168 j = (146097*c)/4 + (1461*ya)/4 + (153*m+2)/5 + d + 1721119;
169 jul = (JulianRep)j;
170 return 0;
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) {
182 int j = jul;
183 int d, m, y;
185 if (j < SmallestValJulDate)
186 { year = month = day = 0; return -1; }
188 j -= 1721119;
189 y = (4 * j - 1)/146097;
190 j = 4 * j - 1 - 146097 * y;
191 d = j/4;
192 j = (4 * d + 3)/1461;
193 d = 4 * d + 3 - 1461 * j;
194 d = (d + 4)/4;
195 m = (5 * d - 3)/153;
196 d = 5 * d - 3 - 153 * m;
197 d = (d + 5) / 5;
198 y = 100 * y + j;
199 if (m < 10) m += 3;
200 else { m -= 9; ++y; }
201 month = m;
202 day = d;
203 year = y;
204 return 0;
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;
216 timeVal *= 10000;
217 if(usec) timeVal += usec/100;
218 return 0;
221 int Time::get(int &hours, int &mins, int &secs) const {
222 if (timeVal < 0) return -1;
223 int s = timeVal/10000;
224 secs = s % 60;
225 s /= 60;
226 mins = s % 60;
227 s /= 60;
228 hours = s;
229 return 0;
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);
241 return 0;
243 int Time::setUsec(int us) {
244 if(us < 0 || us >= 1000000) return -1;
245 timeVal = timeVal +us/100;
246 return 0;
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) {
282 int hours,mins,secs;
283 int count;
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))
289 return -1;
290 return set(hours,mins,secs);
292 int TimeStamp::parseFrom(const char *s) {
293 int hours,mins,secs;
294 int month,day,year;
295 int count;
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))
303 return -1;
305 setDate(year,month,day);
308 if (!time.isValidTime(hours,mins,secs))
309 return -1;
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;
331 long size = 0;
332 switch(type)
334 case typeInt:
335 size = sizeof(int);
336 break;
337 case typeLong:
338 size = sizeof(long);
339 break;
340 case typeLongLong:
341 size = sizeof(long long);
342 break;
343 case typeShort:
344 size = sizeof(short);
345 break;
346 case typeByteInt:
347 size = sizeof(char);
348 break;
349 case typeDouble:
350 size = sizeof(double);
351 break;
352 case typeFloat:
353 size = sizeof(float);
354 break;
355 case typeDecimal:
356 //TODO::for porting
357 //fldDef.length_ = sizeof(long double);
358 break;
359 case typeDate:
360 size = sizeof(Date);
361 break;
362 case typeTime:
363 size = sizeof(Time);
364 break;
365 case typeTimeStamp:
366 size = sizeof(TimeStamp);
367 break;
368 case typeString:
369 case typeBinary:
370 size = length;
371 break;
372 default:
373 size = 0;
374 break;
376 return size;
378 char* AllDataType::getSQLString(DataType type)
380 switch(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)
401 switch(type)
403 case typeInt:
404 return SQL_INTEGER;
405 case typeLong:
406 return SQL_INTEGER;
407 case typeLongLong:
408 //TODO
409 return SQL_INTEGER;
410 case typeShort:
411 return SQL_SMALLINT;
412 case typeByteInt:
413 //TODO
414 return SQL_INTEGER;
415 case typeDouble:
416 return SQL_DOUBLE;
417 case typeFloat:
418 return SQL_REAL;
419 case typeDecimal:
420 //TODO
421 return SQL_INTEGER;
422 case typeDate:
423 return SQL_TYPE_DATE;
424 case typeTime:
425 return SQL_TYPE_TIME;
426 case typeTimeStamp:
427 return SQL_TYPE_TIMESTAMP;
428 case typeString:
429 return SQL_CHAR;
430 case typeBinary:
431 return SQL_BINARY;
433 return SQL_INTEGER;
435 SQLSMALLINT AllDataType::convertToSQL_C_Type(DataType type)
437 switch(type)
439 case typeInt:
440 return SQL_C_SLONG;
441 case typeLong:
442 return SQL_C_SLONG;
443 case typeLongLong:
444 return SQL_C_SBIGINT;
445 case typeShort:
446 return SQL_C_SSHORT;
447 case typeByteInt:
448 return SQL_C_STINYINT;
449 case typeDouble:
450 return SQL_C_DOUBLE;
451 case typeFloat:
452 return SQL_C_FLOAT;
453 case typeDecimal:
454 //TODO
455 return SQL_INTEGER;
456 case typeDate:
457 return SQL_C_TYPE_DATE;
458 case typeTime:
459 return SQL_C_TYPE_TIME;
460 case typeTimeStamp:
461 return SQL_C_TYPE_TIMESTAMP;
462 case typeString:
463 return SQL_C_CHAR;
464 case typeBinary:
465 return SQL_C_BINARY;
467 return SQL_C_SLONG;
470 DataType AllDataType::convertFromSQLType(SQLSMALLINT type)
472 switch(type)
474 case SQL_INTEGER :
475 return typeInt;
476 case SQL_SMALLINT:
477 return typeShort;
478 case SQL_DOUBLE:
479 return typeDouble;
480 case SQL_FLOAT:
481 case SQL_REAL:
482 return typeFloat;
483 case SQL_TYPE_DATE:
484 return typeDate;
485 case SQL_TYPE_TIME :
486 return typeTime;
487 case SQL_TYPE_TIMESTAMP :
488 return typeTimeStamp;
489 case SQL_CHAR:
490 return typeString;
491 case SQL_VARCHAR:
492 return typeString;
493 case SQL_BINARY:
494 return typeBinary;
496 return typeInt;
498 void AllDataType::copyVal(void* dest, void *src, DataType type, int length)
500 switch(type)
502 case typeInt:
503 *(int*)dest = *(int*)src;
504 break;
505 case typeLong:
506 *(long*)dest = *(long*)src;
507 break;
508 case typeLongLong:
509 *(long long*)dest = *(long long*)src;
510 break;
511 case typeShort:
512 *(short*)dest = *(short*)src;
513 break;
514 case typeByteInt:
515 *(char*)dest = *(char*)src;
516 break;
517 case typeDouble:
518 *(double*)dest = *(double*)src;
519 break;
520 case typeFloat:
521 *(float*)dest = *(float*)src;
522 break;
523 case typeDecimal:
524 //TODO::for porting
525 case typeDate:
526 *(Date*)dest = *(Date*)src;
527 break;
528 case typeTime:
529 *(Time*)dest = *(Time*)src;
530 break;
531 case typeTimeStamp:
532 *(TimeStamp*)dest = *(TimeStamp*)src;
533 break;
534 case typeString:
536 strncpy((char*)dest, (char*)src, length);
537 char *d =(char*)dest;
538 d[length-1] = '\0';
539 break;
541 case typeBinary:
542 os::memcpy(dest, src, length);
543 break;
544 default:
545 break;
548 void AllDataType::addVal(void* dest, void *src, DataType type)
550 switch(type)
552 case typeInt:
553 *(int*)dest = *(int*)dest + *(int*)src;
554 break;
555 case typeLong:
556 *(long*)dest = *(long*)dest + *(long*)src;
557 break;
558 case typeLongLong:
559 *(long long*)dest = *(long long*)dest + *(long long*)src;
560 break;
561 case typeShort:
562 *(short*)dest = *(short*)dest + *(short*)src;
563 break;
564 case typeByteInt:
565 *(char*)dest = *(char*)dest + *(char*)src;
566 break;
567 case typeDouble:
568 *(double*)dest = *(double*)dest + *(double*)src;
569 break;
570 case typeFloat:
571 *(float*)dest = *(float*)dest + *(float*)src;
572 break;
573 case typeDecimal:
574 //TODO::for porting
575 case typeDate:
576 case typeTime:
577 case typeTimeStamp:
578 case typeBinary:
579 default:
580 break;
582 return;
585 void AllDataType::divVal(void* dest, int src, DataType type)
587 switch(type)
589 case typeInt:
590 *(int*)dest = *(int*)dest / src;
591 break;
592 case typeLong:
593 *(long*)dest = *(long*)dest / src;
594 break;
595 case typeLongLong:
596 *(long long*)dest = *(long long*)dest / src;
597 break;
598 case typeShort:
599 *(short*)dest = *(short*)dest / src;
600 break;
601 case typeByteInt:
602 *(char*)dest = *(char*)dest / src;
603 break;
604 case typeDouble:
605 *(double*)dest = *(double*)dest / src;
606 break;
607 case typeFloat:
608 *(float*)dest = *(float*)dest / src;
609 break;
610 case typeDecimal:
611 //TODO::for porting
612 case typeDate:
613 case typeTime:
614 case typeTimeStamp:
615 case typeBinary:
616 default:
617 break;
619 return;
625 bool AllDataType::compareVal(void *val1, void *val2, ComparisionOp op,
626 DataType type, long length)
628 bool result = false;
629 switch(type)
631 case typeInt:
632 result = AllDataType::compareIntVal(val1, val2, op );
633 break;
634 case typeLong:
635 result = AllDataType::compareLongVal(val1, val2, op);
636 break;
637 case typeLongLong:
638 result = AllDataType::compareLongLongVal(val1, val2, op);
639 break;
640 case typeShort:
641 result = AllDataType::compareShortVal(val1, val2, op);
642 break;
643 case typeByteInt:
644 result = AllDataType::compareByteIntVal(val1, val2, op);
645 break;
646 case typeDouble:
647 result = AllDataType::compareDoubleVal(val1, val2, op);
648 break;
649 case typeFloat:
650 result = AllDataType::compareFloatVal(val1, val2, op);
651 break;
652 case typeDecimal:
653 //TODO::for porting
654 break;
655 case typeDate:
656 result = AllDataType::compareDateVal(val1, val2, op);
657 break;
658 case typeTime:
659 result = AllDataType::compareTimeVal(val1, val2, op);
660 break;
661 case typeTimeStamp:
662 result = AllDataType::compareTimeStampVal(val1, val2, op);
663 break;
664 case typeString:
665 result = AllDataType::compareStringVal(val1, val2, op);
666 break;
667 case typeComposite:
668 case typeBinary:
669 result = AllDataType::compareBinaryVal(val1, val2, op, length);
670 break;
672 return result;
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");
679 bool result = false;
680 switch(op)
682 case OpEquals:
683 if (*(int*)src1 == *(int*)src2) result = true;
684 else result = false;
685 break;
686 case OpNotEquals:
687 if (*(int*)src1 != *(int*)src2) result = true;
688 else result = false;
689 break;
690 case OpLessThan:
691 if (*(int*)src1 < *(int*)src2) result = true;
692 else result = false;
693 break;
694 case OpLessThanEquals:
695 if (*(int*)src1 <= *(int*)src2) result = true;
696 else result = false;
697 break;
698 case OpGreaterThan:
699 if (*(int*)src1 > *(int*)src2) result = true;
700 else result = false;
701 break;
702 case OpGreaterThanEquals:
703 if (*(int*)src1 >= *(int*)src2) result = true;
704 else result = false;
705 break;
707 return result;
710 bool AllDataType::compareLongVal(void* src1, void *src2, ComparisionOp op)
712 bool result = false;
713 switch(op)
715 case OpEquals:
716 if (*(long*)src1 == *(long*)src2) result = true;
717 else result = false;
718 break;
719 case OpNotEquals:
720 if (*(long*)src1 != *(long*)src2) result = true;
721 else result = false;
722 break;
723 case OpLessThan:
724 if (*(long*)src1 < *(long*)src2) result = true;
725 else result = false;
726 break;
727 case OpLessThanEquals:
728 if (*(long*)src1 <= *(long*)src2) result = true;
729 else result = false;
730 break;
731 case OpGreaterThan:
732 if (*(long*)src1 > *(long*)src2) result = true;
733 else result = false;
734 break;
735 case OpGreaterThanEquals:
736 if (*(long*)src1 >= *(long*)src2) result = true;
737 else result = false;
738 break;
740 return result;
743 bool AllDataType::compareLongLongVal(void* src1, void *src2,
744 ComparisionOp op)
746 bool result = false;
747 switch(op)
749 case OpEquals:
750 if (*(long long*)src1 == *(long long*)src2) result = true;
751 else result = false;
752 break;
753 case OpNotEquals:
754 if (*(long long*)src1 != *(long long*)src2) result = true;
755 else result = false;
756 break;
757 case OpLessThan:
758 if (*(long long*)src1 < *(long long*)src2) result = true;
759 else result = false;
760 break;
761 case OpLessThanEquals:
762 if (*(long long*)src1 <= *(long long*)src2) result = true;
763 else result = false;
764 break;
765 case OpGreaterThan:
766 if (*(long long*)src1 > *(long long*)src2) result = true;
767 else result = false;
768 break;
769 case OpGreaterThanEquals:
770 if (*(long long*)src1 >= *(long long*)src2) result = true;
771 else result = false;
772 break;
774 return result;
777 bool AllDataType::compareShortVal(void* src1, void *src2, ComparisionOp op)
779 bool result = false;
780 switch(op)
782 case OpEquals:
783 if (*(short*)src1 == *(short*)src2) result = true;
784 else result = false;
785 break;
786 case OpNotEquals:
787 if (*(short*)src1 != *(short*)src2) result = true;
788 else result = false;
789 break;
790 case OpLessThan:
791 if (*(short*)src1 < *(short*)src2) result = true;
792 else result = false;
793 break;
794 case OpLessThanEquals:
795 if (*(short*)src1 <= *(short*)src2) result = true;
796 else result = false;
797 break;
798 case OpGreaterThan:
799 if (*(short*)src1 > *(short*)src2) result = true;
800 else result = false;
801 break;
802 case OpGreaterThanEquals:
803 if (*(short*)src1 >= *(short*)src2) result = true;
804 else result = false;
805 break;
807 return result;
810 bool AllDataType::compareByteIntVal(void* src1, void *src2, ComparisionOp op)
812 bool result = false;
813 switch(op)
815 case OpEquals:
816 if (*(ByteInt*)src1 == *(ByteInt*)src2) result = true;
817 else result = false;
818 break;
819 case OpNotEquals:
820 if (*(ByteInt*)src1 != *(ByteInt*)src2) result = true;
821 else result = false;
822 break;
823 case OpLessThan:
824 if (*(ByteInt*)src1 < *(ByteInt*)src2) result = true;
825 else result = false;
826 break;
827 case OpLessThanEquals:
828 if (*(ByteInt*)src1 <= *(ByteInt*)src2) result = true;
829 else result = false;
830 break;
831 case OpGreaterThan:
832 if (*(ByteInt*)src1 > *(ByteInt*)src2) result = true;
833 else result = false;
834 break;
835 case OpGreaterThanEquals:
836 if (*(ByteInt*)src1 >= *(ByteInt*)src2) result = true;
837 else result = false;
838 break;
840 return result;
843 bool AllDataType::compareDoubleVal(void* src1, void *src2, ComparisionOp op)
845 bool result = false;
846 switch(op)
848 case OpEquals:
849 if (*(double*)src1 == *(double*)src2) result = true;
850 else result = false;
851 break;
852 case OpNotEquals:
853 if (*(double*)src1 != *(double*)src2) result = true;
854 else result = false;
855 break;
856 case OpLessThan:
857 if (*(double*)src1 < *(double*)src2) result = true;
858 else result = false;
859 break;
860 case OpLessThanEquals:
861 if (*(double*)src1 <= *(double*)src2) result = true;
862 else result = false;
863 break;
864 case OpGreaterThan:
865 if (*(double*)src1 > *(double*)src2) result = true;
866 else result = false;
867 break;
868 case OpGreaterThanEquals:
869 if (*(double*)src1 >= *(double*)src2) result = true;
870 else result = false;
871 break;
873 return result;
876 bool AllDataType::compareFloatVal(void* src1, void *src2, ComparisionOp op)
878 bool result = false;
879 switch(op)
881 case OpEquals:
882 if (*(float*)src1 == *(float*)src2) result = true;
883 else result = false;
884 break;
885 case OpNotEquals:
886 if (*(float*)src1 != *(float*)src2) result = true;
887 else result = false;
888 break;
889 case OpLessThan:
890 if (*(float*)src1 < *(float*)src2) result = true;
891 else result = false;
892 break;
893 case OpLessThanEquals:
894 if (*(float*)src1 <= *(float*)src2) result = true;
895 else result = false;
896 break;
897 case OpGreaterThan:
898 if (*(float*)src1 > *(float*)src2) result = true;
899 else result = false;
900 break;
901 case OpGreaterThanEquals:
902 if (*(float*)src1 >= *(float*)src2) result = true;
903 else result = false;
904 break;
906 return result;
909 bool AllDataType::compareDateVal(void* src1, void *src2, ComparisionOp op)
911 bool result = false;
912 switch(op)
914 case OpEquals:
915 if (*(Date*)src1 == *(Date*)src2) result = true;
916 else result = false;
917 break;
918 case OpNotEquals:
919 if (*(Date*)src1 != *(Date*)src2) result = true;
920 else result = false;
921 break;
922 case OpLessThan:
923 if (*(Date*)src1 < *(Date*)src2) result = true;
924 else result = false;
925 break;
926 case OpLessThanEquals:
927 if (*(Date*)src1 <= *(Date*)src2) result = true;
928 else result = false;
929 break;
930 case OpGreaterThan:
931 if (*(Date*)src1 > *(Date*)src2) result = true;
932 else result = false;
933 break;
934 case OpGreaterThanEquals:
935 if (*(Date*)src1 >= *(Date*)src2) result = true;
936 else result = false;
937 break;
939 return result;
942 bool AllDataType::compareTimeVal(void* src1, void *src2, ComparisionOp op)
944 bool result = false;
945 switch(op)
947 case OpEquals:
948 if (*(Time*)src1 == *(Time*)src2) result = true;
949 else result = false;
950 break;
951 case OpNotEquals:
952 if (*(Time*)src1 != *(Time*)src2) result = true;
953 else result = false;
954 break;
955 case OpLessThan:
956 if (*(Time*)src1 < *(Time*)src2) result = true;
957 else result = false;
958 break;
959 case OpLessThanEquals:
960 if (*(Time*)src1 <= *(Time*)src2) result = true;
961 else result = false;
962 break;
963 case OpGreaterThan:
964 if (*(Time*)src1 > *(Time*)src2) result = true;
965 else result = false;
966 break;
967 case OpGreaterThanEquals:
968 if (*(Time*)src1 >= *(Time*)src2) result = true;
969 else result = false;
970 break;
972 return result;
975 bool AllDataType::compareTimeStampVal(void* src1, void *src2, ComparisionOp op)
977 bool result = false;
978 switch(op)
980 case OpEquals:
981 if (*(TimeStamp*)src1 == *(TimeStamp*)src2) result = true;
982 else result = false;
983 break;
984 case OpNotEquals:
985 if (*(TimeStamp*)src1 != *(TimeStamp*)src2) result = true;
986 else result = false;
987 break;
988 case OpLessThan:
989 if (*(TimeStamp*)src1 < *(TimeStamp*)src2) result = true;
990 else result = false;
991 break;
992 case OpLessThanEquals:
993 if (*(TimeStamp*)src1 <= *(TimeStamp*)src2) result = true;
994 else result = false;
995 break;
996 case OpGreaterThan:
997 if (*(TimeStamp*)src1 > *(TimeStamp*)src2) result = true;
998 else result = false;
999 break;
1000 case OpGreaterThanEquals:
1001 if (*(TimeStamp*)src1 >= *(TimeStamp*)src2) result = true;
1002 else result = false;
1003 break;
1005 return result;
1008 bool AllDataType::compareStringVal(void* src1, void *src2, ComparisionOp op)
1010 bool result = false;
1011 int ret = strcmp((char*)src1, (char*)src2);
1012 switch(op)
1014 case OpEquals:
1015 if (ret == 0 ) result= true; else result = false;
1016 break;
1017 case OpNotEquals:
1018 if (ret != 0 ) result= true; else result = false;
1019 break;
1020 case OpLessThan:
1021 if (ret < 0 ) result= true; else result = false;
1022 break;
1023 case OpLessThanEquals:
1024 printf("Illegal Operator:Not Supported for String\n");
1025 break;
1026 case OpGreaterThan:
1027 if (ret > 0 ) result= true; else result = false;
1028 break;
1029 case OpGreaterThanEquals:
1030 printf("Illegal Operator:Not Supported for String\n");
1031 break;
1033 return result;
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);
1041 switch(op)
1043 case OpEquals:
1044 if (ret == 0 ) result= true; else result = false;
1045 break;
1046 case OpNotEquals:
1047 if (ret != 0 ) result= true; else result = false;
1048 break;
1049 case OpLessThan:
1050 if (ret < 0 ) result= true; else result = false;
1051 break;
1052 case OpLessThanEquals:
1053 printf("Illegal Operator:Not Supported for Binary\n");
1054 break;
1055 case OpGreaterThan:
1056 if (ret > 0 ) result= true; else result = false;
1057 break;
1058 case OpGreaterThanEquals:
1059 printf("Illegal Operator:Not Supported for Binary\n");
1060 break;
1062 return result;
1067 ComparisionOp AllDataType::getComparisionOperator(char *str)
1069 ComparisionOp op;
1070 if (strcmp(str, "<=") == 0)
1071 op = OpLessThanEquals;
1072 else if (strcmp(str, ">=") == 0)
1073 op = OpGreaterThanEquals;
1074 else if (strcmp(str, "<") == 0)
1075 op = OpLessThan;
1076 else if (strcmp(str, ">") == 0)
1077 op = OpGreaterThan;
1078 else if (strcmp(str, "=") == 0)
1079 op = OpEquals;
1080 else if (strcmp(str, "!=") == 0 || strcmp(str, "<>") == 0 )
1081 op = OpNotEquals;
1082 else if (strcasecmp(str, "LIKE") == 0 )
1083 op = OpLike;
1084 else
1085 op = OpInvalidComparisionOp;
1086 return op;
1089 void* AllDataType::alloc(DataType type, int length)
1091 void *dest;
1092 switch(type)
1094 case typeInt:
1095 dest = malloc(sizeof(int));
1096 break;
1097 case typeLong:
1098 dest = malloc(sizeof(long));
1099 break;
1100 case typeLongLong:
1101 dest = malloc(sizeof(long long));
1102 break;
1103 case typeShort:
1104 dest = malloc(sizeof(short));
1105 break;
1106 case typeByteInt:
1107 dest = malloc(sizeof(char));
1108 break;
1109 case typeDouble:
1110 dest = malloc(sizeof(double));
1111 break;
1112 case typeFloat:
1113 dest = malloc(sizeof(float));
1114 break;
1115 case typeDecimal:
1116 //TODO::for porting
1117 //fldDef.length_ = sizeof(long double);
1118 break;
1119 case typeString:
1120 if (length == 0 ) return NULL;
1121 dest = malloc(length);
1122 break;
1123 case typeBinary:
1124 if (length == 0 || length > 256 ) return NULL;
1125 dest = malloc(length);
1126 memset(dest, 0, length);
1127 break;
1128 case typeDate:
1129 dest = malloc(sizeof(Date));
1130 break;
1131 case typeTime:
1132 dest = malloc(sizeof(Time));
1133 break;
1134 case typeTimeStamp:
1135 dest = malloc(sizeof(TimeStamp));
1136 break;
1138 return dest;
1140 DbRetVal AllDataType::strToValue(void* dest, char *src, DataType type, int length)
1142 switch(type)
1144 case typeInt: {
1145 int val;
1146 sscanf( src, "%d", &val);
1147 *(int*)dest = val;
1148 break; }
1149 case typeLong: {
1150 long val;
1151 sscanf( src, "%ld", &val);
1152 *(long*)dest = val;
1153 break; }
1154 case typeLongLong: {
1155 long long val;
1156 sscanf( src, "%lld", &val);
1157 *(long long*)dest = val;
1158 break; }
1159 case typeShort: {
1160 short val;
1161 sscanf( src, "%hd", &val);
1162 *(short*)dest = val;
1163 break; }
1164 case typeByteInt: {
1165 char val;
1166 sscanf( src, "%c", &val);
1167 *(char*)dest = val;
1168 break; }
1169 case typeDouble: {
1170 double val;
1171 sscanf( src, "%lg", &val);
1172 *(double*)dest = val;
1173 break; }
1174 case typeFloat: {
1175 float val;
1176 sscanf( src, "%f", &val);
1177 *(float*)dest = val;
1178 break; }
1179 case typeDecimal:
1180 //TODO::for porting
1181 case typeString: {
1182 strncpy((char*)dest, (char*)src, length);
1183 char *d =(char*)dest;
1184 d[length-1] = '\0';
1185 break;}
1186 case typeDate: {
1187 int d,m,y,res=0;
1188 res = sscanf( src, "%d-%d-%d", &y, &m, &d );
1189 if( res != 3 )
1190 res = sscanf( src, "%d/%d/%d", &y, &m, &d );
1191 if( res != 3 )
1193 fprintf(stderr,"Error reading date. yyyy{-/}mm{-/}dd is the valid format.");
1194 d=m=y=0;
1196 Date dateObj(y,m,d);
1197 *(Date*)dest = dateObj;
1198 break; }
1199 case typeTime: {
1200 int h,m,s,res=0;
1201 res = sscanf( src, "%d:%d:%d", &h, &m, &s );
1202 if( res != 3 )
1204 fprintf(stderr, "Error reading time, hh:mm:ss is the valid format.");
1205 h=m=s=0;
1207 Time timeObj(h,m,s);
1208 *(Time*)dest = timeObj;
1209 break; }
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 );
1213 if( res != 6 )
1214 res = sscanf( src, "%d-%d-%d, %d:%d:%d", &y, &m, &d, &h, &mn, &s );
1215 if( res != 6 )
1216 res = sscanf( src, "%d/%d/%d %d:%d:%d", &y, &m, &d, &h, &mn, &s );
1217 if( res != 6 )
1218 res = sscanf( src, "%d/%d/%d, %d:%d:%d", &y, &m, &d, &h, &mn, &s );
1219 if( res != 6 )
1221 fprintf(stderr, "Error reading timestamp, yyyy{-/}mm{-/}dd[,] hh:mm:ss is the valid format.");
1222 d=m=y=h=mn=s=0;
1224 TimeStamp timeStampObj(y,m,d,h,mn,s);
1225 *(TimeStamp*)dest = timeStampObj;
1226 break; }
1227 case typeBinary: {
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;
1232 int i = 0;
1233 while (i < length * 2) {
1234 c = *str++; i++;
1235 if (c == '\0') { *val = *val | c; break; }
1236 if (!isxdigit((int)c)) {
1237 printError(ErrBadArg, "Invalid hexadecimal value");
1238 return ErrBadArg;
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++; }
1246 break;
1248 default:
1249 break;
1251 return OK;
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;
1269 //TODO
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;
1277 default: return;
1281 void AllDataType::convertToInt( void* dest, void* src, DataType srcType )
1283 switch(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;
1296 case typeDate:
1297 case typeTime:
1298 case typeTimeStamp:
1299 case typeBinary:
1300 default: *(int *)dest = (int) 0;
1304 void AllDataType::convertToLong( void* dest, void* src, DataType srcType )
1306 switch(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;
1319 case typeDate:
1320 case typeTime:
1321 case typeTimeStamp:
1322 case typeBinary:
1323 default: *(long *)dest = (long) 0;
1328 void AllDataType::convertToLongLong( void* dest, void* src, DataType srcType )
1330 switch(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;
1343 case typeDate:
1344 case typeTime:
1345 case typeTimeStamp:
1346 case typeBinary:
1347 default: *(long long *)dest = (long long) 0;
1351 void AllDataType::convertToShort( void* dest, void* src, DataType srcType )
1353 switch(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;
1366 case typeDate:
1367 case typeTime:
1368 case typeTimeStamp:
1369 case typeBinary:
1370 default: *(short*)dest = (short) 0;
1374 void AllDataType::convertToByteInt( void* dest, void* src, DataType srcType )
1376 switch(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;
1389 case typeDate:
1390 case typeTime:
1391 case typeTimeStamp:
1392 case typeBinary:
1393 default: *(char*)dest = (char) 0;
1397 void AllDataType::convertToFloat( void* dest, void* src, DataType srcType )
1399 switch(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;
1412 case typeDate:
1413 case typeTime:
1414 case typeTimeStamp:
1415 case typeBinary:
1416 default: *(float *)dest = (float) 0;
1420 void AllDataType::convertToDouble( void* dest, void* src, DataType srcType )
1422 switch(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;
1435 case typeDate:
1436 case typeTime:
1437 case typeTimeStamp:
1438 case typeBinary:
1439 default: *(double *)dest = (double) 0;
1443 void AllDataType::convertToString( void* dest, void* src, DataType srcType, int length )
1445 switch(srcType)
1447 case typeInt:
1449 sprintf ((char *)dest, "%d", *(int *)src);
1450 break;
1452 case typeLong:
1454 sprintf ((char *)dest, "%ld", *(long *)src);
1455 break;
1457 case typeLongLong:
1459 sprintf ((char *)dest, "%lld", *(long long *)src);
1460 break;
1462 case typeShort:
1464 sprintf ((char *)dest, "%hd", *(short *)src);
1465 break;
1467 case typeByteInt:
1469 sprintf ((char *)dest, "%hd", *(char *)src);
1470 break;
1473 case typeFloat:
1475 sprintf ((char *)dest, "%f", *(float *)src);
1476 break;
1478 case typeDouble:
1480 sprintf ((char *) dest, "%lf", *(double *)src);
1481 break;
1484 case typeString:
1486 strcpy((char*)dest, (char*)src);
1487 break;
1489 case typeDate:
1491 Date* dt = (Date*)src;
1492 sprintf((char*) dest, "%d/%d/%d", dt->year(),
1493 dt->month(), dt->dayOfMonth());
1494 break;
1496 case typeTime:
1498 Time* tm = (Time*)src;
1499 sprintf((char*)dest,"%d:%d:%d.%d", tm->hours(), tm->minutes(), tm->seconds(), 0);
1500 break;
1502 case typeTimeStamp:
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 );
1508 break;
1510 case typeBinary:
1512 unsigned char *c = (unsigned char *) src;
1513 unsigned char *str = (unsigned char *) dest;
1514 unsigned char p = 0;
1515 int i = 0;
1516 while (i < length) {
1517 p = *c >> 4;
1518 if (p < 10) sprintf ((char *)str++, "%c", '0' + p);
1519 else sprintf((char *)str++, "%c", 'A' + p - 10);
1520 p = *c & 0xF;
1521 if (p < 10) sprintf ((char *)str++, "%c", '0' + p);
1522 else sprintf((char *)str++, "%c", 'A' + p - 10);
1523 i++; c++;
1525 break;
1528 default: ((char*)dest)[0] = '\0';
1532 void AllDataType::convertToDate( void* dest, void* src, DataType srcType )
1534 switch(srcType)
1536 case typeInt:
1537 case typeLong:
1538 case typeLongLong:
1539 case typeShort:
1540 case typeByteInt:
1541 case typeFloat:
1542 case typeDouble:
1543 case typeDate:
1544 case typeTime:
1545 case typeTimeStamp:
1546 case typeString:
1548 Date *dt = (Date*) dest;
1549 dt->parseFrom((char*)src);
1550 break;
1552 default: ((char*)dest)[0] = '\0';
1556 void AllDataType::convertToTime( void* dest, void* src, DataType srcType )
1558 switch(srcType)
1560 case typeInt:
1561 case typeLong:
1562 case typeLongLong:
1563 case typeShort:
1564 case typeByteInt:
1565 case typeFloat:
1566 case typeDouble:
1567 case typeDate:
1568 case typeTime:
1569 case typeTimeStamp:
1570 case typeString:
1572 Time *dt = (Time*) dest;
1573 dt->parseFrom((char*)src);
1574 break;
1576 default: ((char*)dest)[0] = '\0';
1580 void AllDataType::convertToTimeStamp( void* dest, void* src, DataType srcType )
1582 switch(srcType)
1584 case typeInt:
1585 case typeLong:
1586 case typeLongLong:
1587 case typeShort:
1588 case typeByteInt:
1589 case typeFloat:
1590 case typeDouble:
1591 case typeDate:
1592 case typeTime:
1593 case typeTimeStamp:
1594 case typeString:
1596 TimeStamp *dt = (TimeStamp*) dest;
1597 dt->parseFrom((char*)src);
1598 break;
1600 default: ((char*)dest)[0] = '\0';
1604 void AllDataType::convertToBinary(void *dest, void *src, DataType srcType, int length)
1606 switch(srcType)
1608 case typeString:
1610 unsigned char c = 0;
1611 const char *str = (const char *)src;
1612 unsigned char *val = (unsigned char *)dest;
1613 int i = 0;
1614 while (i < length * 2) {
1615 c = *str++; i++;
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++; }
1623 break;
1628 int AllDataType::printVal(void* src, DataType srcType, int length )
1630 int count = 0;
1631 switch(srcType)
1633 case typeInt:
1635 count = printf ("%d", *(int *)src);
1636 break;
1638 case typeLong:
1640 count = printf ("%ld", *(long *)src);
1641 break;
1643 case typeLongLong:
1645 count = printf ("%lld", *(long long *)src);
1646 break;
1648 case typeShort:
1650 count = printf("%hd", *(short *)src);
1651 break;
1653 case typeByteInt:
1655 count = printf("%hd", *(char *)src);
1656 break;
1659 case typeFloat:
1661 count = printf("%f", *(float *)src);
1662 break;
1664 case typeDouble:
1666 count = printf("%lf", *(double *)src);
1667 break;
1670 case typeString:
1672 count = printf("%s", (char*)src);
1673 break;
1675 case typeDate:
1677 Date* dt = (Date*)src;
1678 count = printf("%d/%d/%d", dt->year(),
1679 dt->month(), dt->dayOfMonth());
1680 break;
1682 case typeTime:
1684 Time* tm = (Time*)src;
1685 count = printf("%d:%d:%d.%d", tm->hours(), tm->minutes(), tm->seconds(), 0);
1686 break;
1688 case typeTimeStamp:
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 );
1694 break;
1696 case typeBinary:
1698 unsigned char *c = (unsigned char *) src;
1699 unsigned char p = 0;
1700 int i = 0;
1701 while (i < length) {
1702 p = *c >> 4;
1703 if (p < 10) printf ("%c", '0' + p);
1704 else printf("%c", 'A' + p - 10);
1705 p = *c & 0xF;
1706 if (p < 10) printf ("%c", '0' + p);
1707 else printf("%c", 'A' + p - 10);
1708 i++; c++;
1710 count = length * 2;
1711 break;
1713 default: { printf("DataType not supported\n"); break; }
1715 return count;