code reorg
[csql.git] / src / relational / relop / Expression.cxx
blobe909fff2e4567c42697bde8a5d811022fe2b5415
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;
41 void Expression::setFunctionType(FunctionType type)
43 if(NULL!=lhs)
44 lhs->setFunctionType(type);
45 if(NULL!=rhs)
46 rhs->setFunctionType(type);
47 fType = type;
50 void Expression::setExpr(Expression* exp1, FunctionType type,Expression* exp2)
52 lhs = exp1;
53 rhs = exp2;
54 setFunctionType(type);
55 arOp=unKnownOperator;
58 void Expression::setExpr(void *cVal,bool flag)
60 arOp=unKnownOperator;
61 constVal=cVal;
62 lhs = rhs = NULL;
65 void Expression::setExpr(char const *name,ArithOperator op,void *cVal)
67 strcpy(fldName, name);
68 arOp = op;
69 constVal = cVal;
70 lhs = rhs = NULL;
73 void Expression::setExpr(char const *name)
75 strcpy(fldName, name);
76 arOp=unKnownOperator;
77 constVal=NULL;
78 lhs = rhs = NULL;
81 void Expression::setExpr(Expression *exp1, ArithOperator op, Expression *exp2)
83 lhs = exp1;
84 rhs = exp2;
85 arOp = op;
88 void *Expression::evaluate(DataType type,bool &result)
90 calVal=AllDataType::alloc(type,IDENTIFIER_LENGTH);
91 AllDataType::memoryset(calVal,type);
92 char *rhsResult = NULL , *lhsResult = NULL;
93 if (NULL != lhs)
95 lhsResult =(char *) lhs->evaluate(type,result);
96 if (NULL == lhsResult) return lhsResult;
98 if (NULL != rhs)
100 rhsResult = (char *)rhs->evaluate(type,result);
101 if (NULL == rhsResult) return rhsResult;
103 if(result){return tuple;}
104 if (0==strcmp(fldName,"NULL")){
105 result=true;return tuple;
107 int offset;
108 char *val=NULL;
109 if(NULL==lhs && NULL == rhs)
111 if(strcmp(fldName,"\0")!=0)
113 DataType srcType = table->getFieldType(fldName);
114 if(srcType > 12) return NULL;
115 else
117 offset=table->getFieldOffset(fldName);
118 val= ((char*) tuple) + offset;
119 if(table->isFldNull(fldName))
121 result=true;
122 return tuple;
127 if(constVal!= NULL && strcmp(fldName,"\0")!=0)
129 AllDataType::convert(table->getFieldType(fldName), val,
130 type, calVal, table->getFieldLength(fldName));
131 solve(calVal, constVal, type, arOp);
133 else if(constVal!= NULL && 0==strcmp(fldName,"\0"))
135 AllDataType::copyVal(calVal, constVal, type, IDENTIFIER_LENGTH);
137 else if( NULL==constVal && strcmp(fldName,"\0")!=0)
139 AllDataType::convert(table->getFieldType(fldName), val,
140 type, calVal, table->getFieldLength(fldName));
142 return calVal;
144 if(NULL!=lhsResult && NULL!=rhsResult)
146 solve(lhsResult, rhsResult, type, arOp);
147 return lhsResult;
149 return NULL;
153 void *Expression::evaluateForFunction(DataType type)
155 void *rhsResult = NULL , *lhsResult = NULL;
156 if (NULL != lhs)
158 lhsResult =(char *) lhs->evaluateForFunction(type);
159 if (NULL == lhsResult) return lhsResult;
161 if (NULL != rhs)
163 rhsResult = (char *)rhs->evaluateForFunction(type);
164 if (NULL == rhsResult) return rhsResult;
166 int offset;
167 char *val=NULL;
168 if(NULL==lhs && NULL == rhs)
170 if(strcmp(fldName,"\0")!=0)
172 DataType srcType = table->getFieldType(fldName);
173 offset=table->getFieldOffset(fldName);
174 setDataType(srcType);
175 val= ((char*) tuple) + offset;
176 if(table->isFldNull(fldName))
178 return NULL;
179 }else {
180 calVal=AllDataType::alloc(srcType,IDENTIFIER_LENGTH);
181 AllDataType::copyVal(calVal,val, srcType,IDENTIFIER_LENGTH);
182 return calVal;
186 calVal=AllDataType::alloc(type,IDENTIFIER_LENGTH);
187 AllDataType::memoryset(calVal,type);
188 if(constVal!= NULL && strcmp(fldName,"\0")==0)
190 copyFunctionVal(calVal, constVal, fType, IDENTIFIER_LENGTH);
191 return calVal;
194 if(NULL!=lhsResult && NULL!=rhsResult)
196 if(calVal==NULL){
197 calVal=AllDataType::alloc(type,IDENTIFIER_LENGTH);
198 AllDataType::memoryset(calVal,type);
200 return evaluateAndGetValPtr(lhsResult,rhsResult);
202 if(NULL!=lhsResult && NULL==rhsResult)
204 if(calVal==NULL){
205 calVal=AllDataType::alloc(type,IDENTIFIER_LENGTH);
206 AllDataType::memoryset(calVal,type);
208 dType = lhs->getDataType();
209 if(dType == typeTimeStamp)
211 if(fType == EXTRACTYEARFROMDAY || fType == EXTRACTHOURFROMTIME
212 || fType == EXTRACTMINFROMTIME || fType ==EXTRACTSECFROMTIME
213 || fType == EXTRACTMONFROMDAY || fType ==EXTRACTDAYFROMDAY)
215 fType = (FunctionType)((int)(fType)+3);
218 return evaluateAndGetValPtr(lhsResult,rhsResult);
220 return NULL;
223 void *Expression::evaluateAndGetValPtr( void *lhsResult, void *rhsResult)
225 switch(fType)
227 case DATEDIFF:
229 *(int *)calVal = (*(Date*)lhsResult - *(Date*)rhsResult);
230 return calVal;
232 case DATEADDWITHYEAR:
234 ((Date*)lhsResult)->addYear(*(int*)rhsResult);
235 return lhsResult;
237 case DATEADDWITHMON:
239 ((Date*)lhsResult)->addMonth(*(int*)rhsResult);
240 return lhsResult;
242 case DATEADDWITHDAY:
244 ((Date*)lhsResult)->addDay(*(int*)rhsResult);
245 return lhsResult;
247 case DATESUBWITHYEAR:
249 ((Date*)lhsResult)->subYear(*(int*)rhsResult);
250 return lhsResult;
252 case DATESUBWITHMON:
254 ((Date*)lhsResult)->subMonth(*(int*)rhsResult);
255 return lhsResult;
257 case DATESUBWITHDAY:
259 ((Date*)lhsResult)->subDay(*(int*)rhsResult);
260 return lhsResult;
262 case TIMEDIFF:
264 *(int *)calVal = (*(Time*)lhsResult - *(Time*)rhsResult);
265 return calVal;
267 case TIMEADDWITHHOUR:
269 ((Time*)lhsResult)->addHour(*(int*)rhsResult);
270 return lhsResult;
272 case TIMEADDWITHMIN:
274 ((Time*)lhsResult)->addMin(*(int*)rhsResult);
275 return lhsResult;
277 case TIMEADDWITHSEC:
279 ((Time*)lhsResult)->addSec(*(int*)rhsResult);
280 return lhsResult;
282 case TIMESUBWITHHOUR:
284 ((Time*)lhsResult)->subHour(*(int*)rhsResult);
285 return lhsResult;
287 case TIMESUBWITHMIN:
289 ((Time*)lhsResult)->subMin(*(int*)rhsResult);
290 return lhsResult;
292 case TIMESUBWITHSEC:
294 ((Time*)lhsResult)->subSec(*(int*)rhsResult);
295 return lhsResult;
297 case TIMESTAMPADDWITHYEAR:
299 ((TimeStamp*)lhsResult)->addYear(*(int*)rhsResult);
300 return lhsResult;
302 case TIMESTAMPADDWITHMON:
304 ((TimeStamp*)lhsResult)->addMonth(*(int*)rhsResult);
305 return lhsResult;
307 case TIMESTAMPADDWITHDAY:
309 ((TimeStamp*)lhsResult)->addDay(*(int*)rhsResult);
310 return lhsResult;
312 case TIMESTAMPSUBWITHYEAR:
314 ((TimeStamp*)lhsResult)->subYear(*(int*)rhsResult);
315 return lhsResult;
317 case TIMESTAMPSUBWITHMON:
319 ((TimeStamp*)lhsResult)->subMonth(*(int*)rhsResult);
320 return lhsResult;
322 case TIMESTAMPSUBWITHDAY:
324 ((TimeStamp*)lhsResult)->subDay(*(int*)rhsResult);
325 return lhsResult;
327 case TIMESTAMPADDWITHHOUR:
329 ((TimeStamp*)lhsResult)->addHour(*(int*)rhsResult);
330 return lhsResult;
332 case TIMESTAMPADDWITHMIN:
334 ((TimeStamp*)lhsResult)->addMin(*(int*)rhsResult);
335 return lhsResult;
337 case TIMESTAMPADDWITHSEC:
339 ((TimeStamp*)lhsResult)->addSec(*(int*)rhsResult);
340 return lhsResult;
342 case TIMESTAMPSUBWITHHOUR:
344 ((TimeStamp*)lhsResult)->subHour(*(int*)rhsResult);
345 return lhsResult;
347 case TIMESTAMPSUBWITHMIN:
349 ((TimeStamp*)lhsResult)->subMin(*(int*)rhsResult);
350 return lhsResult;
352 case TIMESTAMPSUBWITHSEC:
354 ((TimeStamp*)lhsResult)->subSec(*(int*)rhsResult);
355 return lhsResult;
357 case TIMESTAMPDIFFYEAR:
359 *(int *)calVal = ((TimeStamp*)lhsResult)->yearDiff((*(TimeStamp*)rhsResult));
360 return calVal;
362 case TIMESTAMPDIFFMON:
364 *(int *)calVal = ((TimeStamp*)lhsResult)->monthDiff((*(TimeStamp*)rhsResult));
365 return calVal;
367 case TIMESTAMPDIFFDAY:
369 *(int *)calVal = ((TimeStamp*)lhsResult)->dayDiff((*(TimeStamp*)rhsResult));
370 return calVal;
372 case TIMESTAMPDIFFHOUR:
374 *(int *)calVal = ((TimeStamp*)lhsResult)->hourDiff((*(TimeStamp*)rhsResult));
375 return calVal;
377 case TIMESTAMPDIFFMIN:
379 *(int *)calVal = ((TimeStamp*)lhsResult)->minDiff((*(TimeStamp*)rhsResult));
380 return calVal;
382 case TIMESTAMPDIFFSEC:
384 *(int *)calVal = ((TimeStamp*)lhsResult)->secDiff((*(TimeStamp*)rhsResult));
385 return calVal;
387 case EXTRACTYEARFROMDAY:
389 *(int *)calVal = ((Date*)lhsResult)->year();
390 return calVal;
392 case EXTRACTMONFROMDAY:
394 *(int *)calVal = ((Date*)lhsResult)->month();
395 return calVal;
397 case EXTRACTDAYFROMDAY:
399 *(int *)calVal = ((Date*)lhsResult)->dayOfMonth();
400 return calVal;
402 case EXTRACTHOURFROMTIME:
404 *(int *)calVal = ((Time*)lhsResult)->hours();
405 return calVal;
407 case EXTRACTMINFROMTIME:
409 *(int *)calVal = ((Time*)lhsResult)->minutes();
410 return calVal;
412 case EXTRACTSECFROMTIME:
414 *(int *)calVal = ((Time*)lhsResult)->seconds();
415 return calVal;
417 case EXTRACTYEARFROMTIMESTAMP:
419 *(int *)calVal = ((TimeStamp*)lhsResult)->year();
420 return calVal;
422 case EXTRACTMONFROMTIMESTAMP:
424 *(int *)calVal = ((TimeStamp*)lhsResult)->month();
425 return calVal;
427 case EXTRACTDAYFROMTIMESTAMP:
429 *(int *)calVal = ((TimeStamp*)lhsResult)->dayOfMonth();
430 return calVal;
432 case EXTRACTHOURFROMTIMESTAMP:
434 *(int *)calVal = ((TimeStamp*)lhsResult)->hours();
435 return calVal;
437 case EXTRACTMINFROMTIMESTAMP:
439 *(int *)calVal = ((TimeStamp*)lhsResult)->minutes();
440 return calVal;
442 case EXTRACTSECFROMTIMESTAMP:
444 *(int *)calVal = ((TimeStamp*)lhsResult)->seconds();
445 return calVal;
447 case DATEFROMTIMESTAMP:
449 ((TimeStamp*)lhsResult)->getDate(*(Date *)calVal);
450 return calVal;
452 case TIMEFROMTIMESTAMP:
454 ((TimeStamp*)lhsResult)->getTime(*(Time *)calVal);
455 return calVal;
457 default: return NULL;
462 void Expression::copyFunctionVal(void *dest,void *src, FunctionType type, int length)
464 switch(type)
466 case DATEDIFF:
468 //Date date;
469 //date.parseFrom((char*)src);
470 AllDataType::copyVal(dest, src, typeDate,length);
471 return;
473 case DATEADDWITHYEAR:
475 AllDataType::copyVal(dest, src,typeInt,length);
476 return;
478 case DATEADDWITHMON:
480 AllDataType::copyVal(dest, src,typeInt,length);
481 return;
483 case DATEADDWITHDAY:
485 AllDataType::copyVal(dest, src,typeInt,length);
486 return;
488 case DATESUBWITHYEAR:
490 AllDataType::copyVal(dest, src,typeInt,length);
491 return;
493 case DATESUBWITHMON:
495 AllDataType::copyVal(dest, src,typeInt,length);
496 return;
498 case DATESUBWITHDAY:
500 AllDataType::copyVal(dest, src,typeInt,length);
501 return;
503 case TIMEDIFF:
505 AllDataType::copyVal(dest, src, typeTime,length);
506 return;
508 case TIMEADDWITHHOUR:
509 case TIMEADDWITHMIN:
510 case TIMEADDWITHSEC:
511 case TIMESUBWITHHOUR:
512 case TIMESUBWITHMIN:
513 case TIMESUBWITHSEC:
515 AllDataType::copyVal(dest, src,typeInt,length);
516 return;
518 case TIMESTAMPADDWITHYEAR:
519 case TIMESTAMPADDWITHMON:
520 case TIMESTAMPADDWITHDAY:
521 case TIMESTAMPSUBWITHYEAR:
522 case TIMESTAMPSUBWITHMON:
523 case TIMESTAMPSUBWITHDAY:
524 case TIMESTAMPADDWITHHOUR:
525 case TIMESTAMPADDWITHMIN:
526 case TIMESTAMPADDWITHSEC:
527 case TIMESTAMPSUBWITHHOUR:
528 case TIMESTAMPSUBWITHMIN:
529 case TIMESTAMPSUBWITHSEC:
531 AllDataType::copyVal(dest, src,typeInt,length);
532 return;
534 case TIMESTAMPDIFFYEAR:
535 case TIMESTAMPDIFFMON:
536 case TIMESTAMPDIFFDAY:
537 case TIMESTAMPDIFFHOUR:
538 case TIMESTAMPDIFFMIN:
539 case TIMESTAMPDIFFSEC:
541 AllDataType::copyVal(dest, src,typeTimeStamp,length);
542 return;
544 case EXTRACTYEARFROMDAY:
545 case EXTRACTMONFROMDAY:
546 case EXTRACTDAYFROMDAY:
548 AllDataType::copyVal(dest, src,typeDate,length);
549 return;
551 case EXTRACTHOURFROMTIME:
552 case EXTRACTMINFROMTIME:
553 case EXTRACTSECFROMTIME:
555 AllDataType::copyVal(dest, src,typeTime,length);
556 return;
558 case EXTRACTYEARFROMTIMESTAMP:
559 case EXTRACTMONFROMTIMESTAMP:
560 case EXTRACTDAYFROMTIMESTAMP:
561 case EXTRACTHOURFROMTIMESTAMP:
562 case EXTRACTMINFROMTIMESTAMP:
563 case EXTRACTSECFROMTIMESTAMP:
565 AllDataType::copyVal(dest, src,typeTimeStamp,length);
566 return;
569 case DATEFROMTIMESTAMP:
570 case TIMEFROMTIMESTAMP:
572 AllDataType::copyVal(dest, src,typeTimeStamp,length);
573 return;
576 default: return;
581 void Expression::solve(void *opand1, void *opand2, DataType type, ArithOperator arOp)
583 switch(arOp)
585 case addition:
586 AllDataType::addVal(opand1, opand2, type );
587 break;
588 case subtraction:
589 AllDataType::subVal(opand1, opand2, type);
590 break;
591 case multiplication:
592 AllDataType::mulVal(opand1, opand2, type);
593 break;
594 case division:
595 AllDataType::divVal(opand1, opand2, type);
596 break;
597 case modulus:
598 AllDataType::mudVal(opand1, opand2, type);
599 break;
600 default:
601 break;
603 return;
606 bool Expression::isSingleTerm()
608 if (NULL==lhs && NULL==rhs ) return true;
609 else false;
612 void Expression::memFree()
614 if(lhs!=NULL)
615 lhs->memFree();
616 if(rhs!=NULL)
617 rhs->memFree();
618 free(calVal);
621 void Expression::convertStrToVal(DataType type)
623 if(lhs!=NULL)
624 lhs->convertStrToVal(type);
625 if(rhs!=NULL)
626 rhs->convertStrToVal(type);
627 if(constVal !=NULL)
629 void *parseString=constVal;
630 constVal=AllDataType::alloc(type);
631 AllDataType::strToValue(constVal,(char*)parseString, type);
632 free(parseString);
637 void Expression::freeVal()
639 if(lhs!=NULL)
640 lhs->freeVal();
641 if(rhs!=NULL)
642 rhs->freeVal();
643 if(constVal !=NULL)
644 free(constVal);