code reorg and moving files to csql base directory
[csql.git] / src / base / DataType.cxx
blob638260050242f95f80149399b0a9ce7391c73cad
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>
19 #include<sqlext.h>
20 #include<sqltypes.h>
22 DataType AllDataType::getCsqlTypeFromFunctionType(FunctionType type)
24 switch(type)
26 case DATEDIFF: return typeDate;
27 case TIMEDIFF: return typeTime;
28 case DATEADDWITHYEAR:
29 case DATEADDWITHMON:
30 case DATEADDWITHDAY:
31 case DATESUBWITHYEAR:
32 case DATESUBWITHMON:
33 case DATESUBWITHDAY:
34 case TIMEADDWITHHOUR:
35 case TIMEADDWITHMIN:
36 case TIMEADDWITHSEC:
37 case TIMESUBWITHHOUR:
38 case TIMESUBWITHMIN:
39 case TIMESUBWITHSEC: return typeInt;
40 case TIMESTAMPADDWITHYEAR:
41 case TIMESTAMPADDWITHMON:
42 case TIMESTAMPADDWITHDAY:
43 case TIMESTAMPSUBWITHYEAR:
44 case TIMESTAMPSUBWITHMON:
45 case TIMESTAMPSUBWITHDAY:
46 case TIMESTAMPADDWITHHOUR:
47 case TIMESTAMPADDWITHMIN:
48 case TIMESTAMPADDWITHSEC:
49 case TIMESTAMPSUBWITHHOUR:
50 case TIMESTAMPSUBWITHMIN:
51 case TIMESTAMPSUBWITHSEC: return typeInt;
52 case TIMESTAMPDIFFYEAR:
53 case TIMESTAMPDIFFMON:
54 case TIMESTAMPDIFFDAY:
55 case TIMESTAMPDIFFHOUR:
56 case TIMESTAMPDIFFMIN:
57 case TIMESTAMPDIFFSEC: return typeTimeStamp;
58 case EXTRACTYEARFROMDAY:
59 case EXTRACTMONFROMDAY:
60 case EXTRACTDAYFROMDAY:return typeDate;
61 case EXTRACTHOURFROMTIME:
62 case EXTRACTMINFROMTIME:
63 case EXTRACTSECFROMTIME: return typeTime;
64 case EXTRACTYEARFROMTIMESTAMP:
65 case EXTRACTMONFROMTIMESTAMP:
66 case EXTRACTDAYFROMTIMESTAMP:
67 case EXTRACTHOURFROMTIMESTAMP:
68 case EXTRACTMINFROMTIMESTAMP:
69 case EXTRACTSECFROMTIMESTAMP: return typeTimeStamp;
70 case DATEFROMTIMESTAMP: return typeTimeStamp;
71 case TIMEFROMTIMESTAMP: return typeTimeStamp;
72 default: return typeInt;
75 DataType AllDataType::getCsqlTypeFromFunctionTypeForComparision(FunctionType type)
77 switch(type)
79 case DATEDIFF: return typeInt;
80 case DATEADDWITHYEAR: return typeDate;
81 case DATEADDWITHMON: return typeDate;
82 case DATEADDWITHDAY: return typeDate;
83 case DATESUBWITHYEAR: return typeDate;
84 case DATESUBWITHMON: return typeDate;
85 case DATESUBWITHDAY: return typeDate;
86 case TIMEDIFF: return typeInt;
87 case TIMEADDWITHHOUR: return typeTime;
88 case TIMEADDWITHMIN: return typeTime;
89 case TIMEADDWITHSEC: return typeTime;
90 case TIMESUBWITHHOUR: return typeTime;
91 case TIMESUBWITHMIN: return typeTime;
92 case TIMESUBWITHSEC: return typeTime;
93 case TIMESTAMPADDWITHYEAR: return typeTimeStamp;
94 case TIMESTAMPADDWITHMON: return typeTimeStamp;
95 case TIMESTAMPADDWITHDAY: return typeTimeStamp;
96 case TIMESTAMPSUBWITHYEAR: return typeTimeStamp;
97 case TIMESTAMPSUBWITHMON: return typeTimeStamp;
98 case TIMESTAMPSUBWITHDAY: return typeTimeStamp;
99 case TIMESTAMPADDWITHHOUR: return typeTimeStamp;
100 case TIMESTAMPADDWITHMIN: return typeTimeStamp;
101 case TIMESTAMPADDWITHSEC: return typeTimeStamp;
102 case TIMESTAMPSUBWITHHOUR: return typeTimeStamp;
103 case TIMESTAMPSUBWITHMIN: return typeTimeStamp;
104 case TIMESTAMPSUBWITHSEC: return typeTimeStamp;
105 case TIMESTAMPDIFFYEAR:
106 case TIMESTAMPDIFFMON:
107 case TIMESTAMPDIFFDAY:
108 case TIMESTAMPDIFFHOUR:
109 case TIMESTAMPDIFFMIN:
110 case TIMESTAMPDIFFSEC:
111 case EXTRACTYEARFROMDAY:
112 case EXTRACTMONFROMDAY:
113 case EXTRACTDAYFROMDAY:
114 case EXTRACTHOURFROMTIME:
115 case EXTRACTMINFROMTIME:
116 case EXTRACTSECFROMTIME:
117 case EXTRACTYEARFROMTIMESTAMP:
118 case EXTRACTMONFROMTIMESTAMP:
119 case EXTRACTDAYFROMTIMESTAMP:
120 case EXTRACTHOURFROMTIMESTAMP:
121 case EXTRACTMINFROMTIMESTAMP:
122 case EXTRACTSECFROMTIMESTAMP:return typeInt;
123 case DATEFROMTIMESTAMP: return typeDate;
124 case TIMEFROMTIMESTAMP: return typeTime;
125 default: return typeInt;
130 char* AllDataType::getSQLString(DataType type)
132 switch(type)
134 case typeInt: return "INT";
135 case typeLong: return "INT";
136 case typeLongLong: return "BIGINT";
137 case typeShort: return "SMALLINT";
138 case typeByteInt: return "TINYINT";
139 case typeDouble: return "REAL";
140 case typeFloat: return "FLOAT";
141 case typeDate: return "DATE";
142 case typeTime: return "TIME";
143 case typeTimeStamp: return "TIMESTAMP";
144 case typeString: return "CHAR";
145 case typeVarchar: return "VARCHAR";
146 case typeBinary: return "BINARY";
147 default: return "UNKNOWN";
152 short AllDataType::convertToSQLType(DataType type)
154 switch(type)
156 case typeInt:
157 return SQL_INTEGER;
158 case typeLong:
159 return SQL_INTEGER;
160 case typeLongLong:
161 //TODO
162 return SQL_BIGINT;
163 case typeShort:
164 return SQL_SMALLINT;
165 case typeByteInt:
166 //TODO
167 return SQL_INTEGER;
168 case typeDouble:
169 return SQL_DOUBLE;
170 case typeFloat:
171 return SQL_REAL;
172 case typeDecimal:
173 //TODO
174 return SQL_INTEGER;
175 case typeDate:
176 return SQL_TYPE_DATE;
177 case typeTime:
178 return SQL_TYPE_TIME;
179 case typeTimeStamp:
180 return SQL_TYPE_TIMESTAMP;
181 case typeString:
182 return SQL_CHAR;
183 case typeBinary:
184 return SQL_BINARY;
185 case typeVarchar:
186 return SQL_VARCHAR;
188 return SQL_INTEGER;
190 SQLSMALLINT AllDataType::convertToCSQLSQLType(DataType type)
192 switch(type)
194 case typeInt:
195 return SQL_INTEGER;
196 case typeLong:
197 return SQL_INTEGER;
198 case typeLongLong:
199 //TODO
200 return SQL_BIGINT;
201 case typeShort:
202 return SQL_SMALLINT;
203 case typeByteInt:
204 //TODO
205 return SQL_TINYINT;
206 case typeDouble:
207 return SQL_DOUBLE;
208 case typeFloat:
209 return SQL_REAL;
210 case typeDecimal:
211 //TODO
212 return SQL_INTEGER;
213 case typeDate:
214 return SQL_TYPE_DATE;
215 case typeTime:
216 return SQL_TYPE_TIME;
217 case typeTimeStamp:
218 return SQL_TYPE_TIMESTAMP;
219 case typeString:
220 return SQL_CHAR;
221 case typeBinary:
222 return SQL_BINARY;
223 case typeVarchar:
224 return SQL_VARCHAR;
226 return SQL_INTEGER;
229 SQLSMALLINT AllDataType::convertToSQL_C_Type(DataType type,TDBInfo tdbname)
231 switch(type)
233 case typeInt:
234 return SQL_C_SLONG;
235 case typeLong:
236 return SQL_C_SLONG;
237 case typeLongLong:
239 if(tdbname == postgres)
240 return SQL_C_CHAR;
241 else
242 return SQL_C_SBIGINT;
244 case typeShort:
245 return SQL_C_SSHORT;
246 case typeByteInt:
247 return SQL_C_STINYINT;
248 case typeDouble:
249 return SQL_C_DOUBLE;
250 case typeFloat:
251 return SQL_C_FLOAT;
252 case typeDecimal:
253 //TODO
254 return SQL_INTEGER;
255 case typeDate:
256 return SQL_C_TYPE_DATE;
257 case typeTime:
258 return SQL_C_TYPE_TIME;
259 case typeTimeStamp:
260 return SQL_C_TYPE_TIMESTAMP;
261 case typeVarchar:
262 case typeString:
263 return SQL_C_CHAR;
264 case typeBinary:
265 return SQL_C_BINARY;
267 return SQL_C_SLONG;
270 DataType AllDataType::convertFromSQLType(SQLSMALLINT type,int length,int scale,TDBInfo tdbname)
272 if(tdbname==postgres)
274 switch(type)
276 case SQL_INTEGER :
277 return typeInt;
278 case SQL_SMALLINT:
279 return typeShort;
280 case SQL_BIGINT:
281 return typeLongLong;
282 case SQL_FLOAT:
283 case SQL_DOUBLE:
284 return typeDouble;
285 case SQL_REAL:
286 return typeFloat;
287 case SQL_TYPE_DATE:
288 return typeDate;
289 case SQL_TYPE_TIME :
290 return typeTime;
291 case SQL_TYPE_TIMESTAMP :
292 return typeTimeStamp;
293 case SQL_CHAR:
294 return typeString;
295 case SQL_LONGVARCHAR:
296 return typeString;
297 case SQL_VARCHAR:
298 return typeVarchar;
299 case SQL_BINARY:
300 return typeBinary;
302 return typeInt;
304 switch(type)
306 case SQL_TINYINT:
307 return typeByteInt;
308 case SQL_INTEGER :
309 return typeInt;
310 case SQL_SMALLINT:
311 return typeShort;
312 case SQL_BIGINT:
313 return typeLongLong;
314 case SQL_DOUBLE:
315 return typeDouble;
316 case SQL_FLOAT:
317 case SQL_REAL:
318 return typeFloat;
319 case SQL_TYPE_DATE:
320 return typeDate;
321 case SQL_TYPE_TIME :
322 return typeTime;
323 case SQL_TYPE_TIMESTAMP :
324 return typeTimeStamp;
325 case SQL_CHAR:
326 return typeString;
327 case SQL_LONGVARCHAR:
328 return typeString;
329 case SQL_VARCHAR:
330 return typeVarchar;
331 case SQL_BINARY:
332 return typeBinary;
334 return typeInt;
336 void AllDataType::subVal(void* dest, void *src, DataType type)
338 switch(type)
340 case typeInt:
341 *(int*)dest = *(int*)dest - *(int*)src;
342 break;
343 case typeLong:
344 *(long*)dest = *(long*)dest - *(long*)src;
345 break;
346 case typeLongLong:
347 *(long long*)dest = *(long long*)dest - *(long long*)src;
348 break;
349 case typeShort:
350 *(short*)dest = *(short*)dest - *(short*)src;
351 break;
352 case typeByteInt:
353 *(char*)dest = *(char*)dest - *(char*)src;
354 break;
355 case typeDouble:
356 *(double*)dest = *(double*)dest - *(double*)src;
357 break;
358 case typeFloat:
359 *(float*)dest = *(float*)dest - *(float*)src;
360 break;
361 case typeDecimal:
362 case typeDate:
363 case typeTime:
364 case typeTimeStamp:
365 case typeBinary:
366 default:
367 break;
369 return;
371 void AllDataType::mulVal(void* dest, void *src, DataType type)
373 switch(type)
375 case typeInt:
376 *(int*)dest = *(int*)dest * (*(int*)src);
377 break;
378 case typeLong:
379 *(long*)dest = *(long*)dest * (*(long*)src);
380 break;
381 case typeLongLong:
382 *(long long*)dest = *(long long*)dest * (*(long long*)src);
383 break;
384 case typeShort:
385 *(short*)dest = *(short*)dest * (*(short*)src);
386 break;
387 case typeByteInt:
388 *(char*)dest = *(char*)dest * (*(char*)src);
389 break;
390 case typeDouble:
391 *(double*)dest = *(double*)dest * (*(double*)src);
392 break;
393 case typeFloat:
394 *(float*)dest = *(float*)dest * (*(float*)src);
395 break;
396 case typeDecimal:
397 case typeDate:
398 case typeTime:
399 case typeTimeStamp:
400 case typeBinary:
401 default:
402 break;
404 return;
406 void AllDataType::mudVal(void* dest, void *src, DataType type)
408 if (type == typeInt)
410 *(int*)dest = *(int*)dest % (*(int*)src);
411 return;
413 switch(type)
415 case typeInt:
416 *(int*)dest = *(int*)dest % (*(int*)src);
417 break;
418 case typeLong:
419 *(long*)dest = *(long*)dest % (*(long*)src);
420 break;
421 case typeLongLong:
422 *(long long*)dest = *(long long*)dest % (*(long long*)src);
423 break;
424 case typeShort:
425 *(short*)dest = *(short*)dest % (*(short*)src);
426 break;
427 case typeByteInt:
428 *(char*)dest = *(char*)dest % (*(char*)src);
429 break;
430 case typeDouble:
431 *(double*)dest = *(long long*)dest % (*(long long*)src);
432 break;
433 case typeFloat:
434 *(float*)dest = *(int*)dest % (*(int*)src);
435 break;
436 case typeDecimal:
437 case typeDate:
438 case typeTime:
439 case typeTimeStamp:
440 case typeBinary:
441 default:
442 break;
444 return;
446 void AllDataType::divVal(void* dest, void *src, DataType type)
448 if (type == typeInt)
450 *(int*)dest = *(int*)dest / (*(int*)src);
451 return;
453 switch(type)
455 case typeInt:
456 *(int*)dest = *(int*)dest / (*(int*)src);
457 break;
458 case typeLong:
459 *(long*)dest = *(long*)dest / (*(long*)src);
460 break;
461 case typeLongLong:
462 *(long long*)dest = *(long long*)dest / (*(long long*)src);
463 break;
464 case typeShort:
465 *(short*)dest = *(short*)dest / (*(short*)src);
466 break;
467 case typeByteInt:
468 *(char*)dest = *(char*)dest / (*(char*)src);
469 break;
470 case typeDouble:
471 *(double*)dest = *(double *)dest / (*(double*)src);
472 break;
473 case typeFloat:
474 *(float*)dest = *(float*)dest / (*(float*)src);
475 break;
476 case typeDecimal:
477 case typeDate:
478 case typeTime:
479 case typeTimeStamp:
480 case typeBinary:
481 default:
482 break;
484 return;
486 void AllDataType::divVal(double* dest, int src, DataType type)
488 switch(type)
490 case typeInt:
491 case typeLong:
492 case typeLongLong:
493 case typeShort:
494 case typeByteInt:
495 case typeDouble:
496 case typeFloat:
497 case typeDecimal:
498 *dest = *dest / src;
499 break;
500 //TODO::for porting
501 case typeDate:
502 case typeTime:
503 case typeTimeStamp:
504 case typeBinary:
505 default:
506 break;
508 return;
511 void AllDataType::increment(void* dest, void *src, DataType type)
513 switch(type)
515 case typeInt:
516 *(int*)dest = *(int*)src + 1;
517 break;
518 case typeLong:
519 *(long*)dest = *(long*)src + 1;
520 break;
521 case typeLongLong:
522 *(long long*)dest = *(long long*)src + 1;
523 break;
524 case typeByteInt:
525 *(char*)dest = *(char*)src + 1;
526 break;
527 case typeShort:
528 *(short*)dest = *(short*)src + 1;
529 break;
530 case typeDouble:
531 *(double*)dest = *(double*)src + 1;
532 break;
533 case typeFloat:
534 *(float*)dest = *(float*)src+ 1;
535 break;
536 default:
537 break;
541 bool AllDataType::isValueZero(void *src, DataType type)
543 switch(type)
545 case typeInt:
546 if (*(int*)src == 0) return true;
547 else return false;
548 case typeLong:
549 if (*(int*)src == 0) return true;
550 else return false;
551 case typeLongLong:
552 if (*(int*)src == 0) return true;
553 else return false;
554 case typeShort:
555 if (*(int*)src == 0) return true;
556 else return false;
557 case typeDouble:
558 if (*(int*)src == 0) return true;
559 else return false;
560 case typeFloat:
561 if (*(int*)src == 0) return true;
562 else return false;
563 default:
564 break;
566 return false;
569 void* AllDataType::alloc(DataType type, int length)
571 void *dest;
572 switch(type)
574 case typeInt:
575 dest = malloc(sizeof(int));
576 break;
577 case typeLong:
578 dest = malloc(sizeof(long));
579 break;
580 case typeLongLong:
581 dest = malloc(sizeof(long long));
582 break;
583 case typeShort:
584 dest = malloc(sizeof(short));
585 break;
586 case typeByteInt:
587 dest = malloc(sizeof(char));
588 break;
589 case typeDouble:
590 dest = malloc(sizeof(double));
591 break;
592 case typeFloat:
593 dest = malloc(sizeof(float));
594 break;
595 case typeDecimal:
596 //TODO::for porting
597 //fldDef.length_ = sizeof(long double);
598 break;
599 case typeVarchar:
600 case typeString:
601 if (length == 0 ) return NULL;
602 dest = malloc(length);
603 break;
604 case typeBinary:
605 if (length == 0 || length > 512) return NULL;
606 dest = malloc(length);
607 memset(dest, 0, length);
608 break;
609 case typeDate:
610 dest = malloc(sizeof(Date));
611 break;
612 case typeTime:
613 dest = malloc(sizeof(Time));
614 break;
615 case typeTimeStamp:
616 dest = malloc(sizeof(TimeStamp));
617 break;
619 return dest;
622 void AllDataType::memoryset(void *value,DataType type)
624 switch(type)
626 case typeInt:
627 value= ::memset(value, 0, sizeof(int));
628 break;
629 case typeLong:
630 value = ::memset(value, 0, sizeof(long));
631 break;
632 case typeLongLong:
633 value =::memset(value, 0, sizeof(long long));
634 break;
635 case typeShort:
636 value = ::memset(value, 0, sizeof(short));
637 break;
638 case typeByteInt:
639 value = ::memset(value, 0, sizeof(char));
640 break;
641 case typeDouble:
642 value = ::memset(value, 0, sizeof(double));
643 break;
644 case typeFloat:
645 value = ::memset(value, 0, sizeof(float));
646 break;
647 case typeDecimal:
648 break;
649 case typeString:
650 case typeVarchar:
651 *(char*)value = '\0';
652 //if (length == 0 ) return NULL;
653 //dest = malloc(length);
654 break;
655 case typeBinary:
656 /* if (length == 0 || length > 256 ) return NULL;
658 memset(dest, 0, length);*/
659 break;
660 case typeDate:
661 value = ::memset(value, 0, sizeof(Date));
662 break;
663 case typeTime:
664 value = ::memset(value, 0, sizeof(Time));
665 break;
666 case typeTimeStamp:
667 value = ::memset(value, 0, sizeof(TimeStamp));
668 break;
669 defalut:
670 break;
674 int AllDataType::printVal(void* src, DataType srcType, int length,int dbFlag )
676 //dbFlag is the database flag 0:mysql (default), 1:postgres
677 int count = 0;
678 switch(srcType)
680 case typeInt:
682 count = printf ("%d", *(int *)src);
683 break;
685 case typeLong:
687 count = printf ("%ld", *(long *)src);
688 break;
690 case typeLongLong:
692 if(1 == dbFlag){
693 long long temp = 0;
694 convertToLongLong((void*)&temp, src,typeString);
695 count = printf ("%lld", temp );
697 else count = printf ("%lld", *(long long *)src);
698 break;
700 case typeShort:
702 count = printf("%hd", *(short *)src);
703 break;
705 case typeByteInt:
707 count = printf("%hhd", *(char *)src);
708 break;
711 case typeFloat:
713 count = printf("%f", *(float *)src);
714 break;
716 case typeDouble:
718 if (*(double*)src > 9999999999999999.0F)
719 count = printf("%g", *(double *)src);
720 else
721 count = printf("%lf", *(double *)src);
722 break;
725 case typeString:
726 case typeVarchar:
728 count = printf("%s", (char*)src);
729 break;
731 case typeDate:
733 Date* dt = (Date*)src;
734 count = printf("%d/%d/%d", dt->year(),
735 dt->month(), dt->dayOfMonth());
736 break;
738 case typeTime:
740 Time* tm = (Time*)src;
741 count = printf("%d:%d:%d.%d", tm->hours(), tm->minutes(), tm->seconds(), 0);
742 break;
744 case typeTimeStamp:
746 TimeStamp* tm = (TimeStamp*)src;
747 count = printf("%d/%d/%d %d:%d:%d.%d", tm->year(),
748 tm->month(), tm->dayOfMonth(), tm->hours(),
749 tm->minutes(), tm->seconds(), 0 );
750 break;
752 case typeBinary:
754 unsigned char *c = (unsigned char *) src;
755 unsigned char p = 0;
756 int i = 0;
757 bool isDigitFound=false;
758 while (i < length) {
759 p = *c >> 4;
760 if(('0'+p)!='0'|| isDigitFound){
761 if (p < 10) printf ("%c", '0' + p);
762 else printf("%c", 'A' + p - 10);
763 isDigitFound=true;
765 p = *c & 0xF;
766 if(('0'+p)!='0' || isDigitFound){
767 if (p < 10) printf ("%c", '0' + p);
768 else printf("%c", 'A' + p - 10);
769 isDigitFound=true;
771 i++; c++;
773 count = length * 2;
774 break;
776 default: { printf("DataType not supported\n"); break; }
778 return count;