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>
25 void PredicateImpl::print()
27 printf("FieldName1 %s, FieldName2 %s", fldName1
, fldName2
);
28 printf("CompOp %d, operand %x operandPtr%x", compOp
, operand
);
29 printf("lhs %x, rhs %x", lhs
, rhs
);
33 void PredicateImpl::setTerm(const char* fName1
, ComparisionOp op
,
36 strcpy(fldName1
, fName1
);
37 strcpy(fldName2
, fName2
);
42 logicalOp
= OpInvalidLogicalOp
;
45 //Operand should be of the same type of the field.This is must
46 void PredicateImpl::setTerm(const char* fName1
, ComparisionOp op
, void *opnd
)
48 strcpy(fldName1
, fName1
);
50 char *c
= (char *) opnd
;
52 if (*c
== '_') *c
= '?';
53 else if(*c
== '%') *c
= '*';
61 logicalOp
= OpInvalidLogicalOp
;
64 void PredicateImpl::setTerm(const char* fName1
, ComparisionOp op
, void **opnd
)
66 strcpy(fldName1
, fName1
);
71 logicalOp
= OpInvalidLogicalOp
;
75 void PredicateImpl::setTerm(Predicate
*p1
, LogicalOp op
, Predicate
*p2
)
77 if (p2
== NULL
&& op
!= OpNot
|| op
== OpNot
&& p2
!= NULL
)
82 lhs
= (PredicateImpl
*)p1
;
83 rhs
= (PredicateImpl
*)p2
;
85 compOp
= OpInvalidComparisionOp
;
88 void PredicateImpl::setTable(Table
*tbl
)
91 lhs
->setTable((TableImpl
*)tbl
);
93 rhs
->setTable((TableImpl
*)tbl
);
94 table
= (TableImpl
*)tbl
;
97 void PredicateImpl::setTuple(void *tpl
)
105 bool PredicateImpl::isSingleTerm()
107 if (NULL
== lhs
&& NULL
== rhs
) return true; else false;
111 bool PredicateImpl::isNotOrInvolved()
113 bool lhsResult
= true, rhsResult
= true;
116 lhsResult
= lhs
->isNotOrInvolved();
120 rhsResult
= rhs
->isNotOrInvolved();
124 //Means it involves only Logical operator
128 if (lhsResult
|| rhsResult
) return true; else return false;
142 DbRetVal
PredicateImpl::evaluate(bool &result
)
144 bool rhsResult
= false, lhsResult
=false;
145 printDebug(DM_Predicate
, "Evaluate start logical:%d compOp:%d", logicalOp
, compOp
);
146 DbRetVal retCode
=OK
;
150 retCode
= lhs
->evaluate(lhsResult
);
151 printDebug(DM_Predicate
, "LHS result %d retcode: %d", lhsResult
, retCode
);
152 if (retCode
!= OK
) return ErrInvalidExpr
;
156 retCode
= rhs
->evaluate(rhsResult
);
157 printDebug(DM_Predicate
, "RHS result %d retcode:%d", rhsResult
, retCode
);
158 if (retCode
!= OK
) return ErrInvalidExpr
;
162 //Means it involves only Logical operator
163 printDebug(DM_Predicate
,"Evalute operator %d lhsResult %d : rhsResult %d", logicalOp
, lhsResult
, rhsResult
);
167 if (lhsResult
&& rhsResult
) result
= true;
170 if (lhsResult
|| rhsResult
) result
= true;
173 if (lhsResult
) result
= false; else result
= true;
176 return ErrInvalidExpr
;
179 printDebug(DM_Predicate
, "result is %d", result
);
182 printDebug(DM_Predicate
, "Evaluating comparision predicate op:%d", compOp
);
183 //Means it is relational expression
184 //first operand is always field identifier
185 //get the value in the tuple
186 int offset1
, offset2
;
187 offset1
= table
->getFieldOffset(fldName1
);
188 //TODO::do not call getFieldXXX many times, instead get it using getFieldInfo
190 //Assumes that fldName2 data type is also same for expr f1 <f2
191 DataType srcType
= table
->getFieldType(fldName1
);
192 val1
= ((char*) tuple
) + offset1
;
193 if (operand
== NULL
&& operandPtr
== NULL
)
196 offset2
= table
->getFieldOffset(fldName2
);
197 val2
= ((char*)tuple
) + offset2
;
200 else if(operand
!= NULL
&& operandPtr
== NULL
)
202 val2
= (char*) operand
;
204 else if(operand
== NULL
&& operandPtr
!= NULL
)
206 val2
= *(char**)operandPtr
;
209 printDebug(DM_Predicate
, " fldname :%s ", fldName1
);
210 if (compOp
== OpLike
) result
= ! fnmatch(val2
, val1
, 0);
211 else result
= AllDataType::compareVal(val1
, val2
, compOp
, srcType
,
212 table
->getFieldLength(fldName1
));
216 bool PredicateImpl::pointLookupInvolved(const char *fname
)
218 bool rhsResult
, lhsResult
;
221 lhsResult
= lhs
->pointLookupInvolved(fname
);
225 rhsResult
= rhs
->pointLookupInvolved(fname
);
229 //Means it involves only Logical operator
234 if (lhsResult
|| rhsResult
) return true; else return false;
245 //Means it is relational expression
246 //first operand is always field identifier
247 if (OpEquals
== compOp
)
249 //for expressions f1 == f2 use full scan, so return false
250 if(NULL
== operand
&& NULL
== operandPtr
) return false;
251 if(0 == strcmp(fldName1
, fname
))
259 void* PredicateImpl::valPtrForIndexField(const char *fname
)
261 void *lhsRet
, *rhsRet
;
264 lhsRet
= lhs
->valPtrForIndexField(fname
);
268 rhsRet
= rhs
->valPtrForIndexField(fname
);
272 //Means it involves only Logical operator
273 if ( lhsRet
!= NULL
) return lhsRet
;
274 if ( rhsRet
!= NULL
) return rhsRet
;
276 //Means it is relational expression
277 //first operand is always field identifier
278 if (OpEquals
== compOp
)
280 if(0 == strcmp(fldName1
, fname
))
282 if (operand
) return operand
; else return *(void**)operandPtr
;