code reorg and moving files to csql base directory
[csql.git] / src / base / TypeConvert.cxx
blob49a50c97af132eb2a36036dd95caf30a96dd2006
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 DbRetVal AllDataType::strToValue(void* dest, char *src, DataType type, int length)
22 Date date;
23 Time time;
24 switch(type)
26 case typeInt: {
27 int val;
28 sscanf( src, "%d", &val);
29 *(int*)dest = val;
30 break; }
31 case typeLong: {
32 long val;
33 sscanf( src, "%ld", &val);
34 *(long*)dest = val;
35 break; }
36 case typeLongLong: {
37 long long val;
38 sscanf( src, "%lld", &val);
39 *(long long*)dest = val;
40 break; }
41 case typeShort: {
42 short val;
43 sscanf( src, "%hd", &val);
44 *(short*)dest = val;
45 break; }
46 case typeByteInt: {
47 char val;
48 sscanf( src, "%hhd", &val);
49 *(char*)dest = *(char *)&val;
50 break; }
51 case typeDouble: {
52 double val;
53 sscanf( src, "%lg", &val);
54 *(double*)dest = val;
55 break; }
56 case typeFloat: {
57 float val;
58 sscanf( src, "%f", &val);
59 *(float*)dest = val;
60 break; }
61 case typeDecimal:
62 //TODO::for porting
63 case typeString:
64 case typeVarchar: {
65 strncpy((char*)dest, (char*)src, length);
66 char *d =(char*)dest;
67 d[length-1] = '\0';
68 break;}
69 case typeDate: {
70 int d,m,y,res=0;
71 if (strlen(src) == 11) {
72 if ( src[6] == '-' || src[6] == '/' ) {
73 Date::changeToCsqlFormat(src);
76 res = sscanf( src, "%d-%d-%d", &y, &m, &d );
77 if( res != 3 )
78 res = sscanf( src, "%d/%d/%d", &y, &m, &d );
79 if (strcmp(src,"now")==0 || strcmp(src,"NOW")==0){
80 Date *dt = (Date*) dest;
81 dt->parseFrom((char*)src);
82 break;
84 if( res != 3 )
86 fprintf(stderr,"Error reading date. yyyy{-/}mm{-/}dd is the valid format.");
87 d=m=y=0;
89 /* Looking up at ranges of year-month-day */
90 if(!date.isValidDate(y, m, d)){
91 fprintf(stderr,"Error reading date. The value \"%d-%d-%d\" is inappropriate.",y,m,d);
92 d=m=y=0;
93 return ErrBadArg;
95 Date dateObj(y,m,d);
96 *(Date*)dest = dateObj;
97 break; }
98 case typeTime: {
99 int h,m,s,res=0;
100 res = sscanf( src, "%d:%d:%d", &h, &m, &s );
101 if(strcmp(src,"now")==0 || strcmp(src,"NOW")==0){
102 Time *dt = (Time*) dest;
103 dt->parseFrom((char*)src);
104 break;
106 if( res != 3 )
108 int d,mn,y;
109 res = sscanf( src, "%d-%d-%d %d:%d:%d", &y, &mn, &d, &h, &m, &s );
110 if(res!=6){
111 fprintf(stderr, "Error reading time, hh:mm:ss is the valid format.");
112 h=m=s=0;
113 return ErrBadArg;
116 /* validate time */
117 if(!time.isValidTime(h, m, s)){
118 fprintf(stderr,"Error reading Time. The value \"%d-%d-%d\" is inappropriate.",h,m,s);
119 h=m=s=0;
120 return ErrBadArg;
122 Time timeObj(h,m,s);
123 *(Time*)dest = timeObj;
124 break; }
125 case typeTimeStamp: {
126 int d,m,y, h,mn,s, res=0;
127 bool isNow = ( strcasecmp(src,"now")==0 );
128 if(!isNow && isalpha(int(src[5]))) {
129 TimeStamp::changeToCsqlFormat(src);
131 res = sscanf( src, "%d-%d-%d %d:%d:%d", &y, &m, &d, &h, &mn, &s );
132 if( res != 6 )
133 res = sscanf( src, "%d-%d-%d, %d:%d:%d", &y, &m, &d, &h, &mn, &s );
134 if( res != 6 )
135 res = sscanf( src, "%d/%d/%d %d:%d:%d", &y, &m, &d, &h, &mn, &s );
136 if( res != 6 )
137 res = sscanf( src, "%d/%d/%d, %d:%d:%d", &y, &m, &d, &h, &mn, &s );
139 if(isNow){
140 TimeStamp *dt = (TimeStamp*) dest;
141 dt->parseFrom((char*)src);
142 break;
144 if ( res != 6 )
146 fprintf(stderr, "Error reading timestamp, yyyy{-/}mm{-/}dd[,] hh:mm:ss is the valid format.");
147 d=m=y=h=mn=s=0;
148 return ErrBadArg;
150 /* Looking up at 'YY-MM-DD' */
151 if(!date.isValidDate(y, m, d)){
152 fprintf(stderr,"Error reading date. The value \"%d-%d-%d\" is inappropriate.",y,m,d);
153 d=m=y=h=mn=s=0;
154 return ErrBadArg;
156 /* Looking up at 'Hour-Min-Sec' */
157 if(!time.isValidTime(h, m, s)){
158 fprintf(stderr,"Error reading Time. The value \"%d-%d-%d\" is inappropriate.",h,m,s);
159 d=m=y=h=mn=s=0;
160 return ErrBadArg;
162 TimeStamp timeStampObj(y,m,d,h,mn,s);
163 *(TimeStamp*)dest = timeStampObj;
164 break; }
165 case typeBinary: {
166 memset ((void *) dest, 0, length);
167 unsigned char c = 0;
168 const char *str = (const char *)src;
169 int i=0;
170 i = strlen(src)-1;
171 if( i > (length*2)-1 ){ i = (length*2)-1 ; }
172 bool evenlegth=false;
173 if(i%2){ evenlegth=true;}
174 unsigned char *val = (unsigned char *)dest+(length-1);
175 while (i >= 0) {
176 c = str[i];
177 if (!isxdigit((int)c)) {
178 printError(ErrBadArg, "Invalid hexadecimal value");
179 return ErrBadArg;
181 if (c <= '9') c -= '0';
182 else if (c >= 'a') c = c - 'a' + 10;
183 else c = c - 'A' + 10;
184 if(evenlegth)
186 if ((i+1) % 2) { *val = *val | (c<<4); val--;}
187 else { *val = c; }
188 }else{
189 if (i % 2) { *val = *val | (c<<4); val--;}
190 else { *val = c; }
192 i--;
194 break;
196 default:
197 break;
199 return OK;
203 void AllDataType::convert(DataType srcType, void *src,
204 DataType destType, void *dest, int length)
206 switch ((DataType) destType )
208 case typeInt: convertToInt(dest, src, srcType); break;
209 case typeLong: convertToLong(dest, src, srcType); break;
210 case typeLongLong: convertToLongLong(dest, src, srcType); break;
211 case typeShort: convertToShort(dest, src, srcType); break;
212 case typeByteInt: convertToByteInt(dest, src, srcType); break;
214 case typeFloat: convertToFloat(dest, src, srcType); break;
215 case typeDouble: convertToDouble(dest, src, srcType); break;
217 //TODO
218 case typeDecimal: convertToDouble(dest, src, srcType); break;
219 case typeVarchar:
220 case typeString: convertToString(dest, src, srcType); break;
221 case typeBinary: convertToBinary(dest, src, srcType,length); break;
222 case typeDate: convertToDate(dest, src, srcType); break;
223 case typeTime: convertToTime(dest, src, srcType); break;
224 case typeTimeStamp: convertToTimeStamp(dest, src, srcType); break;
225 default: return;
229 void AllDataType::convertToInt( void* dest, void* src, DataType srcType )
231 switch(srcType)
233 case typeInt: *(int *)dest = *(int *)src; break;
234 case typeLong: *(int *)dest =(int) *(long *)src; break;
235 case typeLongLong: *(int *)dest =(int) *(long long *)src; break;
236 case typeShort: *(int *)dest =(int) *(short *)src; break;
237 case typeByteInt: *(int *)dest =(int) *(char *)src; break;
239 case typeFloat: *(int *)dest = (int) *(float *)src; break;
240 case typeDouble: *(int *)dest =(int) *(double *)src; break;
241 case typeVarchar:
242 case typeString: sscanf((const char*)src, "%d", (int*) dest); break;
244 case typeDate:
245 case typeTime:
246 case typeTimeStamp:
247 case typeBinary:
248 default: *(int *)dest = (int) 0;
252 void AllDataType::convertToLong( void* dest, void* src, DataType srcType )
254 switch(srcType)
256 case typeInt: *(long *)dest =(long) *(int *)src; break;
257 case typeLong: *(long *)dest = *(long *)src; break;
258 case typeLongLong: *(long *)dest =(long) *(long long *)src; break;
259 case typeShort: *(long *)dest =(long) *(short *)src; break;
260 case typeByteInt: *(long *)dest =(long) *(char *)src; break;
262 case typeFloat: *(long *)dest = (long) *(float *)src; break;
263 case typeDouble: *(long *)dest =(long) *(double *)src; break;
264 case typeVarchar:
265 case typeString: sscanf((const char*)src, "%ld", (long*) dest); break;
267 case typeDate:
268 case typeTime:
269 case typeTimeStamp:
270 case typeBinary:
271 default: *(long *)dest = (long) 0;
276 void AllDataType::convertToLongLong( void* dest, void* src, DataType srcType )
278 switch(srcType)
280 case typeInt: *(long long *)dest =(long long) *(int *)src; break;
281 case typeLong: *(long long *)dest = (long long) *(long *)src; break;
282 case typeLongLong: *(long long *)dest = *(long long *)src; break;
283 case typeShort: *(long long *)dest =(long long) *(short *)src; break;
284 case typeByteInt: *(long long *)dest =(long long) *(char *)src; break;
286 case typeFloat: *(long long *)dest = (long long) *(float *)src; break;
287 case typeDouble: *(long long *)dest =(long long) *(double *)src; break;
288 case typeVarchar:
289 case typeString: sscanf((const char*)src, "%lld", (long long*) dest); break;
291 case typeDate:
292 case typeTime:
293 case typeTimeStamp:
294 case typeBinary:
295 default: *(long long *)dest = (long long) 0;
299 void AllDataType::convertToShort( void* dest, void* src, DataType srcType )
301 switch(srcType)
303 case typeInt: *(short*)dest =(short) *(int*)src; break;
304 case typeLong: *(short*)dest = (short) *(long*)src; break;
305 case typeLongLong: *(short*)dest = (short) *(long long*)src; break;
306 case typeShort: *(short*)dest = *(short*)src; break;
307 case typeByteInt: *(short*)dest =(short) *(char *)src; break;
309 case typeFloat: *(short*)dest = (short) *(float *)src; break;
310 case typeDouble: *(short*)dest =(short) *(double *)src; break;
311 case typeVarchar:
312 case typeString: sscanf((const char*)src, "%hd", (short*) dest); break;
314 case typeDate:
315 case typeTime:
316 case typeTimeStamp:
317 case typeBinary:
318 default: *(short*)dest = (short) 0;
322 void AllDataType::convertToByteInt( void* dest, void* src, DataType srcType )
324 switch(srcType)
326 case typeInt: *(char*)dest = (char) *(int*)src; break;
327 case typeLong: *(char*)dest = (char) *(long*)src; break;
328 case typeLongLong: *(char*)dest = (char) *(long long*)src; break;
329 case typeShort: *(char*)dest = (char) *(short*)src; break;
330 case typeByteInt: *(char*)dest = *(char *)src; break;
332 case typeFloat: *(char*)dest = (char) *(float *)src; break;
333 case typeDouble: *(char*)dest =(char) *(double *)src; break;
334 case typeVarchar:
335 case typeString: sscanf((const char*)src, "%hhd", (char*) dest); break;
337 case typeDate:
338 case typeTime:
339 case typeTimeStamp:
340 case typeBinary:
341 default: *(char*)dest = (char) 0;
345 void AllDataType::convertToFloat( void* dest, void* src, DataType srcType )
347 switch(srcType)
349 case typeInt: *(float *)dest =(float) *(int *)src; break;
350 case typeLong: *(float *)dest =(float) *(long *)src; break;
351 case typeLongLong: *(float *)dest =(float) *(long long *)src; break;
352 case typeShort: *(float *)dest =(float) *(short *)src; break;
353 case typeByteInt: *(float *)dest =(float) *(char *)src; break;
355 case typeFloat: *(float *)dest = *(float *)src; break;
356 case typeDouble: *(float *)dest =(float) *(double *)src; break;
357 case typeVarchar:
358 case typeString: sscanf((const char*)src, "%f", (float*) dest); break;
360 case typeDate:
361 case typeTime:
362 case typeTimeStamp:
363 case typeBinary:
364 default: *(float *)dest = (float) 0;
368 void AllDataType::convertToDouble( void* dest, void* src, DataType srcType )
370 switch(srcType)
372 case typeInt: *(double *)dest =(double) *(int *)src; break;
373 case typeLong: *(double *)dest =(double) *(long *)src; break;
374 case typeLongLong: *(double *)dest =(double) *(long long *)src; break;
375 case typeShort: *(double *)dest =(double) *(short *)src; break;
376 case typeByteInt: *(double *)dest =(double) *(char *)src; break;
378 case typeFloat: *(double *)dest =(double) *(float *)src; break;
379 case typeDouble: *(double *)dest = *(double *)src; break;
380 case typeVarchar:
381 case typeString: sscanf((const char*)src, "%lf", (double*) dest); break;
383 case typeDate:
384 case typeTime:
385 case typeTimeStamp:
386 case typeBinary:
387 default: *(double *)dest = (double) 0;
391 void AllDataType::convertToString( void* dest, void* src, DataType srcType, int length,TDBInfo tdbname )
393 switch(srcType)
395 case typeInt:
397 Util::itoa(*(int*)src, (char*)dest);
398 break;
400 case typeLong:
402 sprintf ((char *)dest, "%ld", *(long *)src);
403 break;
405 case typeLongLong:
407 sprintf ((char *)dest, "%lld", *(long long *)src);
408 break;
410 case typeShort:
412 sprintf ((char *)dest, "%hd", *(short *)src);
413 break;
415 case typeByteInt:
417 sprintf ((char *)dest, "%hhd", *(char *)src);
418 break;
421 case typeFloat:
423 sprintf ((char *)dest, "%f", *(float *)src);
424 break;
426 case typeDouble:
428 sprintf ((char *) dest, "%lf", *(double *)src);
429 break;
431 case typeVarchar:
432 case typeString:
434 strcpy((char*)dest, (char*)src);
435 break;
437 case typeDate:
439 Date* dt = (Date*)src;
440 sprintf((char*) dest, "%d/%d/%d", dt->year(),dt->month(), dt->dayOfMonth());
441 break;
443 case typeTime:
445 Time* tm = (Time*)src;
446 sprintf((char*)dest,"%d:%d:%d.%d", tm->hours(), tm->minutes(), tm->seconds(), 0);
447 break;
449 case typeTimeStamp:
451 TimeStamp* tm = (TimeStamp*)src;
452 sprintf((char*)dest, "%d/%d/%d %d:%d:%d.%d", tm->year(),tm->month(), tm->dayOfMonth(), tm->hours(),tm->minutes(), tm->seconds(), 0 );
453 break;
455 case typeBinary:
457 unsigned char *c = (unsigned char *) src;
458 unsigned char *str = (unsigned char *) dest;
459 unsigned char p = 0;
460 int i = 0;
461 while (i < length) {
462 p = *c >> 4;
463 if (p < 10) sprintf ((char *)str++, "%c", '0' + p);
464 else sprintf((char *)str++, "%c", 'A' + p - 10);
465 p = *c & 0xF;
466 if (p < 10) sprintf ((char *)str++, "%c", '0' + p);
467 else sprintf((char *)str++, "%c", 'A' + p - 10);
468 i++; c++;
470 break;
473 default: ((char*)dest)[0] = '\0';
477 void AllDataType::convertToDate( void* dest, void* src, DataType srcType )
479 switch(srcType)
481 case typeInt:
482 case typeLong:
483 case typeLongLong:
484 case typeShort:
485 case typeByteInt:
486 case typeFloat:
487 case typeDouble:
488 case typeDate:
489 case typeTime:
490 case typeTimeStamp:
491 case typeVarchar:
492 case typeString:
494 Date *dt = (Date*) dest;
495 dt->parseFrom((char*)src);
496 break;
498 default: ((char*)dest)[0] = '\0';
502 void AllDataType::convertToTime( void* dest, void* src, DataType srcType )
504 switch(srcType)
506 case typeInt:
507 case typeLong:
508 case typeLongLong:
509 case typeShort:
510 case typeByteInt:
511 case typeFloat:
512 case typeDouble:
513 case typeDate:
514 case typeTime:
515 case typeTimeStamp:
516 case typeVarchar:
517 case typeString:
519 Time *dt = (Time*) dest;
520 dt->parseFrom((char*)src);
521 break;
523 default: ((char*)dest)[0] = '\0';
527 void AllDataType::convertToTimeStamp( void* dest, void* src, DataType srcType )
529 switch(srcType)
531 case typeInt:
532 case typeLong:
533 case typeLongLong:
534 case typeShort:
535 case typeByteInt:
536 case typeFloat:
537 case typeDouble:
538 case typeDate:
539 case typeTime:
540 case typeTimeStamp:
541 case typeVarchar:
542 case typeString:
544 TimeStamp *dt = (TimeStamp*) dest;
545 dt->parseFrom((char*)src);
546 break;
548 default: ((char*)dest)[0] = '\0';
552 void AllDataType::convertToBinary(void *dest, void *src, DataType srcType, int length)
554 switch(srcType)
556 case typeString:
558 memset ((void *) dest, 0, length);
559 unsigned char c = 0;
560 const char *str = (const char *)src;
561 int i=0;
562 i = strlen((char*)src)-1;
563 if( i > (length*2)-1 ){ i = (length*2)-1 ; }
564 bool evenlegth=false;
565 if(i%2){ evenlegth=true;}
566 unsigned char *val = (unsigned char *)dest+(length-1);
567 while (i >= 0) {
568 c = str[i];
569 if (c <= '9') c -= '0';
570 else if (c >= 'a') c = c - 'a' + 10;
571 else c = c - 'A' + 10;
572 if(evenlegth)
574 if ((i+1) % 2) { *val = *val | (c<<4); val--;}
575 else { *val = c; }
576 }else{
577 if (i % 2) { *val = *val | (c<<4); val--;}
578 else { *val = c; }
580 i--;
582 break;