fix for trie index
[csql.git] / src / storage / Expression.cxx
blobf586f0c1677b5058f6d548053ba88ae2b17b0453
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * ***************************************************************************/
17 #include<Expression.h>
18 #include<Table.h>
19 #include<TableImpl.h>
21 void Expression::setTable(Table *tbl )
23 if(NULL!=lhs)
24 lhs->setTable(tbl);
25 if(NULL!=rhs)
26 rhs->setTable(tbl);
28 table=(TableImpl*) tbl;
31 void Expression::setTuple(void *tpl)
33 if(NULL!=lhs)
34 lhs->setTuple(tpl);
35 if(NULL!=rhs)
36 rhs->setTuple(tpl);
38 tuple = tpl;
40 void Expression::setFunctionType(FunctionType type)
42 if(NULL!=lhs)
43 lhs->setFunctionType(type);
44 if(NULL!=rhs)
45 rhs->setFunctionType(type);
46 fType = type;
48 void Expression::setExpr(Expression* exp1, FunctionType type,Expression* exp2)
50 lhs = exp1;
51 rhs = exp2;
52 setFunctionType(type);
53 arOp=unKnownOperator;
55 void Expression::setExpr(void *cVal,bool flag)
57 arOp=unKnownOperator;
58 constVal=cVal;
59 lhs = rhs = NULL;
62 void Expression::setExpr(char const *name,ArithOperator op,void *cVal)
64 strcpy(fldName, name);
65 arOp = op;
66 constVal = cVal;
67 lhs = rhs = NULL;
70 void Expression::setExpr(char const *name)
72 strcpy(fldName, name);
73 arOp=unKnownOperator;
74 constVal=NULL;
75 lhs = rhs = NULL;
78 void Expression::setExpr(Expression *exp1, ArithOperator op, Expression *exp2)
80 lhs = exp1;
81 rhs = exp2;
82 arOp = op;
85 void *Expression::evaluate(DataType type,bool &result)
87 calVal=AllDataType::alloc(type,IDENTIFIER_LENGTH);
88 AllDataType::memoryset(calVal,type);
89 char *rhsResult = NULL , *lhsResult = NULL;
90 if (NULL != lhs)
92 lhsResult =(char *) lhs->evaluate(type,result);
93 if (NULL == lhsResult) return lhsResult;
95 if (NULL != rhs)
97 rhsResult = (char *)rhs->evaluate(type,result);
98 if (NULL == rhsResult) return rhsResult;
100 if(result){return tuple;}
101 if (0==strcmp(fldName,"NULL")){
102 result=true;return tuple;
104 int offset;
105 char *val=NULL;
106 if(NULL==lhs && NULL == rhs)
108 if(strcmp(fldName,"\0")!=0)
110 DataType srcType = table->getFieldType(fldName);
111 if(srcType > 12) return NULL;
112 else
114 offset=table->getFieldOffset(fldName);
115 val= ((char*) tuple) + offset;
116 if(table->isFldNull(fldName))
118 result=true;
119 return tuple;
124 if(constVal!= NULL && strcmp(fldName,"\0")!=0)
126 AllDataType::convert(table->getFieldType(fldName), val,
127 type, calVal, table->getFieldLength(fldName));
128 solve(calVal, constVal, type, arOp);
130 else if(constVal!= NULL && 0==strcmp(fldName,"\0"))
132 AllDataType::copyVal(calVal, constVal, type, IDENTIFIER_LENGTH);
134 else if( NULL==constVal && strcmp(fldName,"\0")!=0)
136 AllDataType::convert(table->getFieldType(fldName), val,
137 type, calVal, table->getFieldLength(fldName));
139 return calVal;
141 if(NULL!=lhsResult && NULL!=rhsResult)
143 solve(lhsResult, rhsResult, type, arOp);
144 return lhsResult;
149 void *Expression::evaluateForFunction(DataType type)
151 void *rhsResult = NULL , *lhsResult = NULL;
152 if (NULL != lhs)
154 lhsResult =(char *) lhs->evaluateForFunction(type);
155 if (NULL == lhsResult) return lhsResult;
157 if (NULL != rhs)
159 rhsResult = (char *)rhs->evaluateForFunction(type);
160 if (NULL == rhsResult) return rhsResult;
162 int offset;
163 char *val=NULL;
164 if(NULL==lhs && NULL == rhs)
166 if(strcmp(fldName,"\0")!=0)
168 DataType srcType = table->getFieldType(fldName);
169 offset=table->getFieldOffset(fldName);
170 setDataType(srcType);
171 val= ((char*) tuple) + offset;
172 if(table->isFldNull(fldName))
174 return NULL;
175 }else {
176 calVal=AllDataType::alloc(srcType,IDENTIFIER_LENGTH);
177 AllDataType::copyVal(calVal,val, srcType,IDENTIFIER_LENGTH);
178 return calVal;
182 calVal=AllDataType::alloc(type,IDENTIFIER_LENGTH);
183 AllDataType::memoryset(calVal,type);
184 if(constVal!= NULL && strcmp(fldName,"\0")==0)
186 copyFunctionVal(calVal, constVal, fType, IDENTIFIER_LENGTH);
187 return calVal;
190 if(NULL!=lhsResult && NULL!=rhsResult)
192 if(calVal==NULL){
193 calVal=AllDataType::alloc(type,IDENTIFIER_LENGTH);
194 AllDataType::memoryset(calVal,type);
196 return evaluateAndGetValPtr(lhsResult,rhsResult);
198 if(NULL!=lhsResult && NULL==rhsResult)
200 if(calVal==NULL){
201 calVal=AllDataType::alloc(type,IDENTIFIER_LENGTH);
202 AllDataType::memoryset(calVal,type);
204 dType = lhs->getDataType();
205 if(dType == typeTimeStamp)
207 if(fType == EXTRACTYEARFROMDAY || fType == EXTRACTHOURFROMTIME
208 || fType == EXTRACTMINFROMTIME || fType ==EXTRACTSECFROMTIME
209 || fType == EXTRACTMONFROMDAY || fType ==EXTRACTDAYFROMDAY)
211 fType = (FunctionType)((int)(fType)+3);
214 return evaluateAndGetValPtr(lhsResult,rhsResult);
216 return NULL;
219 void *Expression::evaluateAndGetValPtr( void *lhsResult, void *rhsResult)
221 switch(fType)
223 case DATEDIFF:
225 *(int *)calVal = (*(Date*)lhsResult - *(Date*)rhsResult);
226 return calVal;
228 case DATEADDWITHYEAR:
230 ((Date*)lhsResult)->addYear(*(int*)rhsResult);
231 return lhsResult;
233 case DATEADDWITHMON:
235 ((Date*)lhsResult)->addMonth(*(int*)rhsResult);
236 return lhsResult;
238 case DATEADDWITHDAY:
240 ((Date*)lhsResult)->addDay(*(int*)rhsResult);
241 return lhsResult;
243 case DATESUBWITHYEAR:
245 ((Date*)lhsResult)->subYear(*(int*)rhsResult);
246 return lhsResult;
248 case DATESUBWITHMON:
250 ((Date*)lhsResult)->subMonth(*(int*)rhsResult);
251 return lhsResult;
253 case DATESUBWITHDAY:
255 ((Date*)lhsResult)->subDay(*(int*)rhsResult);
256 return lhsResult;
258 case TIMEDIFF:
260 *(int *)calVal = (*(Time*)lhsResult - *(Time*)rhsResult);
261 return calVal;
263 case TIMEADDWITHHOUR:
265 ((Time*)lhsResult)->addHour(*(int*)rhsResult);
266 return lhsResult;
268 case TIMEADDWITHMIN:
270 ((Time*)lhsResult)->addMin(*(int*)rhsResult);
271 return lhsResult;
273 case TIMEADDWITHSEC:
275 ((Time*)lhsResult)->addSec(*(int*)rhsResult);
276 return lhsResult;
278 case TIMESUBWITHHOUR:
280 ((Time*)lhsResult)->subHour(*(int*)rhsResult);
281 return lhsResult;
283 case TIMESUBWITHMIN:
285 ((Time*)lhsResult)->subMin(*(int*)rhsResult);
286 return lhsResult;
288 case TIMESUBWITHSEC:
290 ((Time*)lhsResult)->subSec(*(int*)rhsResult);
291 return lhsResult;
293 case TIMESTAMPADDWITHYEAR:
295 ((TimeStamp*)lhsResult)->addYear(*(int*)rhsResult);
296 return lhsResult;
298 case TIMESTAMPADDWITHMON:
300 ((TimeStamp*)lhsResult)->addMonth(*(int*)rhsResult);
301 return lhsResult;
303 case TIMESTAMPADDWITHDAY:
305 ((TimeStamp*)lhsResult)->addDay(*(int*)rhsResult);
306 return lhsResult;
308 case TIMESTAMPSUBWITHYEAR:
310 ((TimeStamp*)lhsResult)->subYear(*(int*)rhsResult);
311 return lhsResult;
313 case TIMESTAMPSUBWITHMON:
315 ((TimeStamp*)lhsResult)->subMonth(*(int*)rhsResult);
316 return lhsResult;
318 case TIMESTAMPSUBWITHDAY:
320 ((TimeStamp*)lhsResult)->subDay(*(int*)rhsResult);
321 return lhsResult;
323 case TIMESTAMPADDWITHHOUR:
325 ((TimeStamp*)lhsResult)->addHour(*(int*)rhsResult);
326 return lhsResult;
328 case TIMESTAMPADDWITHMIN:
330 ((TimeStamp*)lhsResult)->addMin(*(int*)rhsResult);
331 return lhsResult;
333 case TIMESTAMPADDWITHSEC:
335 ((TimeStamp*)lhsResult)->addSec(*(int*)rhsResult);
336 return lhsResult;
338 case TIMESTAMPSUBWITHHOUR:
340 ((TimeStamp*)lhsResult)->subHour(*(int*)rhsResult);
341 return lhsResult;
343 case TIMESTAMPSUBWITHMIN:
345 ((TimeStamp*)lhsResult)->subMin(*(int*)rhsResult);
346 return lhsResult;
348 case TIMESTAMPSUBWITHSEC:
350 ((TimeStamp*)lhsResult)->subSec(*(int*)rhsResult);
351 return lhsResult;
353 case TIMESTAMPDIFFYEAR:
355 *(int *)calVal = ((TimeStamp*)lhsResult)->yearDiff((*(TimeStamp*)rhsResult));
356 return calVal;
358 case TIMESTAMPDIFFMON:
360 *(int *)calVal = ((TimeStamp*)lhsResult)->monthDiff((*(TimeStamp*)rhsResult));
361 return calVal;
363 case TIMESTAMPDIFFDAY:
365 *(int *)calVal = ((TimeStamp*)lhsResult)->dayDiff((*(TimeStamp*)rhsResult));
366 return calVal;
368 case TIMESTAMPDIFFHOUR:
370 *(int *)calVal = ((TimeStamp*)lhsResult)->hourDiff((*(TimeStamp*)rhsResult));
371 return calVal;
373 case TIMESTAMPDIFFMIN:
375 *(int *)calVal = ((TimeStamp*)lhsResult)->minDiff((*(TimeStamp*)rhsResult));
376 return calVal;
378 case TIMESTAMPDIFFSEC:
380 *(int *)calVal = ((TimeStamp*)lhsResult)->secDiff((*(TimeStamp*)rhsResult));
381 return calVal;
383 case EXTRACTYEARFROMDAY:
385 *(int *)calVal = ((Date*)lhsResult)->year();
386 return calVal;
388 case EXTRACTMONFROMDAY:
390 *(int *)calVal = ((Date*)lhsResult)->month();
391 return calVal;
393 case EXTRACTDAYFROMDAY:
395 *(int *)calVal = ((Date*)lhsResult)->dayOfMonth();
396 return calVal;
398 case EXTRACTHOURFROMTIME:
400 *(int *)calVal = ((Time*)lhsResult)->hours();
401 return calVal;
403 case EXTRACTMINFROMTIME:
405 *(int *)calVal = ((Time*)lhsResult)->minutes();
406 return calVal;
408 case EXTRACTSECFROMTIME:
410 *(int *)calVal = ((Time*)lhsResult)->seconds();
411 return calVal;
413 case EXTRACTYEARFROMTIMESTAMP:
415 *(int *)calVal = ((TimeStamp*)lhsResult)->year();
416 return calVal;
418 case EXTRACTMONFROMTIMESTAMP:
420 *(int *)calVal = ((TimeStamp*)lhsResult)->month();
421 return calVal;
423 case EXTRACTDAYFROMTIMESTAMP:
425 *(int *)calVal = ((TimeStamp*)lhsResult)->dayOfMonth();
426 return calVal;
428 case EXTRACTHOURFROMTIMESTAMP:
430 *(int *)calVal = ((TimeStamp*)lhsResult)->hours();
431 return calVal;
433 case EXTRACTMINFROMTIMESTAMP:
435 *(int *)calVal = ((TimeStamp*)lhsResult)->minutes();
436 return calVal;
438 case EXTRACTSECFROMTIMESTAMP:
440 *(int *)calVal = ((TimeStamp*)lhsResult)->seconds();
441 return calVal;
443 case DATEFROMTIMESTAMP:
445 ((TimeStamp*)lhsResult)->getDate(*(Date *)calVal);
446 return calVal;
448 case TIMEFROMTIMESTAMP:
450 ((TimeStamp*)lhsResult)->getTime(*(Time *)calVal);
451 return calVal;
453 default: return NULL;
458 void Expression::copyFunctionVal(void *dest,void *src, FunctionType type, int length)
460 switch(type)
462 case DATEDIFF:
464 //Date date;
465 //date.parseFrom((char*)src);
466 AllDataType::copyVal(dest, src, typeDate,length);
467 return;
469 case DATEADDWITHYEAR:
471 AllDataType::copyVal(dest, src,typeInt,length);
472 return;
474 case DATEADDWITHMON:
476 AllDataType::copyVal(dest, src,typeInt,length);
477 return;
479 case DATEADDWITHDAY:
481 AllDataType::copyVal(dest, src,typeInt,length);
482 return;
484 case DATESUBWITHYEAR:
486 AllDataType::copyVal(dest, src,typeInt,length);
487 return;
489 case DATESUBWITHMON:
491 AllDataType::copyVal(dest, src,typeInt,length);
492 return;
494 case DATESUBWITHDAY:
496 AllDataType::copyVal(dest, src,typeInt,length);
497 return;
499 case TIMEDIFF:
501 AllDataType::copyVal(dest, src, typeTime,length);
502 return;
504 case TIMEADDWITHHOUR:
505 case TIMEADDWITHMIN:
506 case TIMEADDWITHSEC:
507 case TIMESUBWITHHOUR:
508 case TIMESUBWITHMIN:
509 case TIMESUBWITHSEC:
511 AllDataType::copyVal(dest, src,typeInt,length);
512 return;
514 case TIMESTAMPADDWITHYEAR:
515 case TIMESTAMPADDWITHMON:
516 case TIMESTAMPADDWITHDAY:
517 case TIMESTAMPSUBWITHYEAR:
518 case TIMESTAMPSUBWITHMON:
519 case TIMESTAMPSUBWITHDAY:
520 case TIMESTAMPADDWITHHOUR:
521 case TIMESTAMPADDWITHMIN:
522 case TIMESTAMPADDWITHSEC:
523 case TIMESTAMPSUBWITHHOUR:
524 case TIMESTAMPSUBWITHMIN:
525 case TIMESTAMPSUBWITHSEC:
527 AllDataType::copyVal(dest, src,typeInt,length);
528 return;
530 case TIMESTAMPDIFFYEAR:
531 case TIMESTAMPDIFFMON:
532 case TIMESTAMPDIFFDAY:
533 case TIMESTAMPDIFFHOUR:
534 case TIMESTAMPDIFFMIN:
535 case TIMESTAMPDIFFSEC:
537 AllDataType::copyVal(dest, src,typeTimeStamp,length);
538 return;
540 case EXTRACTYEARFROMDAY:
541 case EXTRACTMONFROMDAY:
542 case EXTRACTDAYFROMDAY:
544 AllDataType::copyVal(dest, src,typeDate,length);
545 return;
547 case EXTRACTHOURFROMTIME:
548 case EXTRACTMINFROMTIME:
549 case EXTRACTSECFROMTIME:
551 AllDataType::copyVal(dest, src,typeTime,length);
552 return;
554 case EXTRACTYEARFROMTIMESTAMP:
555 case EXTRACTMONFROMTIMESTAMP:
556 case EXTRACTDAYFROMTIMESTAMP:
557 case EXTRACTHOURFROMTIMESTAMP:
558 case EXTRACTMINFROMTIMESTAMP:
559 case EXTRACTSECFROMTIMESTAMP:
561 AllDataType::copyVal(dest, src,typeTimeStamp,length);
562 return;
565 case DATEFROMTIMESTAMP:
566 case TIMEFROMTIMESTAMP:
568 AllDataType::copyVal(dest, src,typeTimeStamp,length);
569 return;
572 default: return;
577 void Expression::solve(void *opand1, void *opand2, DataType type, ArithOperator arOp)
579 switch(arOp)
581 case addition:
582 AllDataType::addVal(opand1, opand2, type );
583 break;
584 case subtraction:
585 AllDataType::subVal(opand1, opand2, type);
586 break;
587 case multiplication:
588 AllDataType::mulVal(opand1, opand2, type);
589 break;
590 case division:
591 AllDataType::divVal(opand1, opand2, type);
592 break;
593 case modulus:
594 AllDataType::mudVal(opand1, opand2, type);
595 break;
596 default:
597 break;
599 return;
602 bool Expression::isSingleTerm()
604 if (NULL==lhs && NULL==rhs ) return true;
605 else false;
607 void Expression::memFree()
609 if(lhs!=NULL)
610 lhs->memFree();
611 if(rhs!=NULL)
612 rhs->memFree();
613 free(calVal);
616 void Expression::convertStrToVal(DataType type)
618 if(lhs!=NULL)
619 lhs->convertStrToVal(type);
620 if(rhs!=NULL)
621 rhs->convertStrToVal(type);
622 if(constVal !=NULL)
624 void *parseString=constVal;
625 constVal=AllDataType::alloc(type);
626 AllDataType::strToValue(constVal,(char*)parseString, type);
627 free(parseString);
631 void Expression::freeVal()
633 if(lhs!=NULL)
634 lhs->freeVal();
635 if(rhs!=NULL)
636 rhs->freeVal();
637 if(constVal !=NULL)
638 free(constVal);