1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
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. *
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. *
16 * ***************************************************************************/
17 #include<Expression.h>
21 void Expression::setTable(Table
*tbl
)
28 table
=(TableImpl
*) tbl
;
31 void Expression::setTuple(void *tpl
)
41 void Expression::setExpr(void *cVal
,bool flag
)
48 void Expression::setExpr(char const *name
,ArithOperator op
,void *cVal
)
50 strcpy(fldName
, name
);
56 void Expression::setExpr(char const *name
)
58 strcpy(fldName
, name
);
64 void Expression::setExpr(Expression
*exp1
, ArithOperator op
, Expression
*exp2
)
71 void *Expression::evaluate(DataType type
)
73 calVal
=AllDataType::alloc(type
,IDENTIFIER_LENGTH
);
74 char *rhsResult
= NULL
, *lhsResult
= NULL
;
77 lhsResult
=(char *) lhs
->evaluate(type
);
78 if (NULL
== lhsResult
) return lhsResult
;
82 rhsResult
= (char *)rhs
->evaluate(type
);
83 if (NULL
== rhsResult
) return rhsResult
;
87 if(NULL
==lhs
&& NULL
== rhs
)
89 if(strcmp(fldName
,"\0")!=0)
91 DataType srcType
= table
->getFieldType(fldName
);
92 if(srcType
> 12) return NULL
;
95 offset
=table
->getFieldOffset(fldName
);
96 val
= ((char*) tuple
) + offset
;
99 if(constVal
!= NULL
&& strcmp(fldName
,"\0")!=0)
101 os::memcpy(calVal
,val
,table
->getFieldLength(fldName
));
102 solve(calVal
, constVal
, type
, arOp
);
104 else if(constVal
!= NULL
&& 0==strcmp(fldName
,"\0"))
106 AllDataType::copyVal(calVal
, constVal
, type
, IDENTIFIER_LENGTH
);
108 else if( NULL
==constVal
&& strcmp(fldName
,"\0")!=0)
110 os::memcpy(calVal
,val
,table
->getFieldLength(fldName
));
114 if(NULL
!=lhsResult
&& NULL
!=rhsResult
)
116 solve(lhsResult
, rhsResult
, type
, arOp
);
122 void Expression::solve(void *opand1
, void *opand2
, DataType type
, ArithOperator arOp
)
127 AllDataType::addVal(opand1
, opand2
, type
);
130 AllDataType::subVal(opand1
, opand2
, type
);
133 AllDataType::mulVal(opand1
, opand2
, type
);
136 AllDataType::divVal(opand1
, opand2
, type
);
139 AllDataType::mudVal(opand1
, opand2
, type
);
147 bool Expression::isSingleTerm()
149 if (NULL
==lhs
&& NULL
==rhs
) return true;
152 void Expression::memFree()
161 void Expression::convertStrToVal(DataType type
)
164 lhs
->convertStrToVal(type
);
166 rhs
->convertStrToVal(type
);
169 void *parseString
=constVal
;
170 constVal
=AllDataType::alloc(type
);
171 AllDataType::strToValue(constVal
,(char*)parseString
, type
);
176 void Expression::freeVal()