performance fixes and fix for core dump in test tools/csql/test029.ksh
[csql.git] / src / storage / Expression.cxx
blob2815f9f372e7fbe1483faf2080a9d6bd7ba224fb
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 char *rhsResult = NULL , *lhsResult = NULL;
75 if (NULL != lhs)
77 lhsResult =(char *) lhs->evaluate(type,result);
78 if (NULL == lhsResult) return lhsResult;
80 if (NULL != rhs)
82 rhsResult = (char *)rhs->evaluate(type,result);
83 if (NULL == rhsResult) return rhsResult;
85 if(result){return tuple;}
86 int offset;
87 char *val=NULL;
88 if(NULL==lhs && NULL == rhs)
90 if(strcmp(fldName,"\0")!=0)
92 DataType srcType = table->getFieldType(fldName);
93 if(srcType > 12) return NULL;
94 else
96 offset=table->getFieldOffset(fldName);
97 val= ((char*) tuple) + offset;
98 if(table->isFldNull(fldName))
100 result=true;
101 return tuple;
106 if(constVal!= NULL && strcmp(fldName,"\0")!=0)
108 os::memcpy(calVal,val,table->getFieldLength(fldName));
109 solve(calVal, constVal, type, arOp);
111 else if(constVal!= NULL && 0==strcmp(fldName,"\0"))
113 AllDataType::copyVal(calVal, constVal, type, IDENTIFIER_LENGTH);
115 else if( NULL==constVal && strcmp(fldName,"\0")!=0)
117 os::memcpy(calVal,val,table->getFieldLength(fldName));
119 return calVal;
121 if(NULL!=lhsResult && NULL!=rhsResult)
123 solve(lhsResult, rhsResult, type, arOp);
124 return lhsResult;
129 void Expression::solve(void *opand1, void *opand2, DataType type, ArithOperator arOp)
131 switch(arOp)
133 case addition:
134 AllDataType::addVal(opand1, opand2, type );
135 break;
136 case subtraction:
137 AllDataType::subVal(opand1, opand2, type);
138 break;
139 case multiplication:
140 AllDataType::mulVal(opand1, opand2, type);
141 break;
142 case division:
143 AllDataType::divVal(opand1, opand2, type);
144 break;
145 case modulus:
146 AllDataType::mudVal(opand1, opand2, type);
147 break;
148 default:
149 break;
151 return;
154 bool Expression::isSingleTerm()
156 if (NULL==lhs && NULL==rhs ) return true;
157 else false;
159 void Expression::memFree()
161 if(lhs!=NULL)
162 lhs->memFree();
163 if(rhs!=NULL)
164 rhs->memFree();
165 free(calVal);
168 void Expression::convertStrToVal(DataType type)
170 if(lhs!=NULL)
171 lhs->convertStrToVal(type);
172 if(rhs!=NULL)
173 rhs->convertStrToVal(type);
174 if(constVal !=NULL)
176 void *parseString=constVal;
177 constVal=AllDataType::alloc(type);
178 AllDataType::strToValue(constVal,(char*)parseString, type);
179 free(parseString);
183 void Expression::freeVal()
185 if(lhs!=NULL)
186 lhs->freeVal();
187 if(rhs!=NULL)
188 rhs->freeVal();
189 if(constVal !=NULL)
190 free(constVal);