commiting changes from enterprise version for V2.4
[csql.git] / src / storage / Expression.cxx
blobd8a49e95ddafb5d78e120823fdc75d3e881c7470
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::setExpr(void *cVal,bool flag)
43 arOp=unKnownOperator;
44 constVal=cVal;
45 lhs = rhs = NULL;
48 void Expression::setExpr(char const *name,ArithOperator op,void *cVal)
50 strcpy(fldName, name);
51 arOp = op;
52 constVal = cVal;
53 lhs = rhs = NULL;
56 void Expression::setExpr(char const *name)
58 strcpy(fldName, name);
59 arOp=unKnownOperator;
60 constVal=NULL;
61 lhs = rhs = NULL;
64 void Expression::setExpr(Expression *exp1, ArithOperator op, Expression *exp2)
66 lhs = exp1;
67 rhs = exp2;
68 arOp = op;
71 void *Expression::evaluate(DataType type,bool &result)
73 calVal=AllDataType::alloc(type,IDENTIFIER_LENGTH);
74 AllDataType::memoryset(calVal, type);
75 char *rhsResult = NULL , *lhsResult = NULL;
76 if (NULL != lhs)
78 lhsResult =(char *) lhs->evaluate(type,result);
79 if (NULL == lhsResult) return lhsResult;
81 if (NULL != rhs)
83 rhsResult = (char *)rhs->evaluate(type,result);
84 if (NULL == rhsResult) return rhsResult;
86 if(result){return tuple;}
87 if (0==strcmp(fldName,"NULL")){
88 result=true;return tuple;
90 int offset;
91 char *val=NULL;
92 if(NULL==lhs && NULL == rhs)
94 if(strcmp(fldName,"\0")!=0)
96 DataType srcType = table->getFieldType(fldName);
97 if(srcType > 12) return NULL;
98 else
100 offset=table->getFieldOffset(fldName);
101 val= ((char*) tuple) + offset;
102 if(table->isFldNull(fldName))
104 result=true;
105 return tuple;
110 if(constVal!= NULL && strcmp(fldName,"\0")!=0)
112 os::memcpy(calVal,val,table->getFieldLength(fldName));
113 solve(calVal, constVal, type, arOp);
115 else if(constVal!= NULL && 0==strcmp(fldName,"\0"))
117 AllDataType::copyVal(calVal, constVal, type, IDENTIFIER_LENGTH);
119 else if( NULL==constVal && strcmp(fldName,"\0")!=0)
121 os::memcpy(calVal,val,table->getFieldLength(fldName));
123 return calVal;
125 if(NULL!=lhsResult && NULL!=rhsResult)
127 solve(lhsResult, rhsResult, type, arOp);
128 return lhsResult;
133 void Expression::solve(void *opand1, void *opand2, DataType type, ArithOperator arOp)
135 switch(arOp)
137 case addition:
138 AllDataType::addVal(opand1, opand2, type );
139 break;
140 case subtraction:
141 AllDataType::subVal(opand1, opand2, type);
142 break;
143 case multiplication:
144 AllDataType::mulVal(opand1, opand2, type);
145 break;
146 case division:
147 AllDataType::divVal(opand1, opand2, type);
148 break;
149 case modulus:
150 AllDataType::mudVal(opand1, opand2, type);
151 break;
152 default:
153 break;
155 return;
158 bool Expression::isSingleTerm()
160 if (NULL==lhs && NULL==rhs ) return true;
161 else false;
163 void Expression::memFree()
165 if(lhs!=NULL)
166 lhs->memFree();
167 if(rhs!=NULL)
168 rhs->memFree();
169 free(calVal);
172 void Expression::convertStrToVal(DataType type)
174 if(lhs!=NULL)
175 lhs->convertStrToVal(type);
176 if(rhs!=NULL)
177 rhs->convertStrToVal(type);
178 if(constVal !=NULL)
180 void *parseString=constVal;
181 constVal=AllDataType::alloc(type);
182 AllDataType::strToValue(constVal,(char*)parseString, type);
183 free(parseString);
187 void Expression::freeVal()
189 if(lhs!=NULL)
190 lhs->freeVal();
191 if(rhs!=NULL)
192 rhs->freeVal();
193 if(constVal !=NULL)
194 free(constVal);