1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 ***************************************************************************/
18 #include<CatalogTables.h>
21 #include<PredicateImpl.h>
24 void PredicateImpl::print()
26 printf("FieldName1 %s, FieldName2 %s", fldName1
, fldName2
);
27 printf("CompOp %d, operand %x operandPtr%x", compOp
, operand
);
28 printf("lhs %x, rhs %x", lhs
, rhs
);
32 void PredicateImpl::setTerm(const char* fName1
, ComparisionOp op
,
35 strcpy(fldName1
, fName1
);
36 strcpy(fldName2
, fName2
);
43 //Operand should be of the same type of the field.This is must
44 void PredicateImpl::setTerm(const char* fName1
, ComparisionOp op
, void *opnd
)
46 strcpy(fldName1
, fName1
);
53 void PredicateImpl::setTerm(const char* fName1
, ComparisionOp op
, void **opnd
)
55 strcpy(fldName1
, fName1
);
63 void PredicateImpl::setTerm(Predicate
*p1
, LogicalOp op
, Predicate
*p2
)
65 if (p2
== NULL
&& op
!= OpNot
) return;
66 lhs
= (PredicateImpl
*)p1
;
67 rhs
= (PredicateImpl
*)p2
;
71 void PredicateImpl::setTable(Table
*tbl
)
74 lhs
->setTable((TableImpl
*)tbl
);
76 rhs
->setTable((TableImpl
*)tbl
);
77 table
= (TableImpl
*)tbl
;
80 void PredicateImpl::setTuple(void *tpl
)
89 DbRetVal
PredicateImpl::evaluate(bool &result
)
91 bool rhsResult
, lhsResult
;
95 retCode
= lhs
->evaluate(lhsResult
);
96 if (retCode
!= OK
) return ErrInvalidExpr
;
100 retCode
= rhs
->evaluate(rhsResult
);
101 if (retCode
!= OK
) return ErrInvalidExpr
;
105 //Means it involves only Logical operator
109 if (lhsResult
&& rhsResult
) result
= true;
112 if (lhsResult
|| rhsResult
) result
= true;
115 if (lhsResult
) result
= false; else result
= true;
118 return ErrInvalidExpr
;
123 //Means it is relational expression
124 //first operand is always field identifier
125 //get the value in the tuple
126 int offset1
, offset2
;
127 offset1
= table
->getFieldOffset(fldName1
);
128 //TODO::do not call getFieldXXX many times, instead get it using getFieldInfo
130 //Assumes that fldName2 data type is also same for expr f1 <f2
131 DataType srcType
= table
->getFieldType(fldName1
);
132 val1
= ((char*) tuple
) + offset1
;
133 if (operand
== NULL
&& operandPtr
== NULL
)
136 offset2
= table
->getFieldOffset(fldName2
);
137 val2
= ((char*)tuple
) + offset2
;
140 else if(operand
!= NULL
&& operandPtr
== NULL
)
142 val2
= (char*) operand
;
144 else if(operand
== NULL
&& operandPtr
!= NULL
)
146 val2
= *(char**)operandPtr
;
149 result
= AllDataType::compareVal(val1
, val2
, compOp
, srcType
,
150 table
->getFieldLength(fldName1
));
154 bool PredicateImpl::pointLookupInvolved(const char *fname
)
156 bool rhsResult
, lhsResult
;
159 lhsResult
= lhs
->pointLookupInvolved(fname
);
163 rhsResult
= rhs
->pointLookupInvolved(fname
);
167 //Means it involves only Logical operator
182 //Means it is relational expression
183 //first operand is always field identifier
184 if (OpEquals
== compOp
)
186 //for expressions f1 == f2 use full scan, so return false
187 if(NULL
== operand
&& NULL
== operandPtr
) return false;
188 if(0 == strcmp(fldName1
, fname
))
196 void* PredicateImpl::valPtrForIndexField(const char *fname
)
198 void *lhsRet
, *rhsRet
;
201 lhsRet
= lhs
->valPtrForIndexField(fname
);
205 rhsRet
= rhs
->valPtrForIndexField(fname
);
209 //Means it involves only Logical operator
210 if ( lhsRet
!= NULL
) return lhsRet
;
211 if ( rhsRet
!= NULL
) return rhsRet
;
213 //Means it is relational expression
214 //first operand is always field identifier
215 if (OpEquals
== compOp
)
217 if(0 == strcmp(fldName1
, fname
))
219 if (operand
) return operand
; else return *(void**)operandPtr
;