src/server/PredicateImpl.cxx

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by www.databasecache.com                           *
00003  *   Contact: praba_tuty@databasecache.com                                 *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015   ***************************************************************************/
00016 #include<Table.h>
00017 #include<Index.h>
00018 #include<CatalogTables.h>
00019 #include<Lock.h>
00020 #include<Debug.h>
00021 #include<PredicateImpl.h>
00022 #include<Table.h>
00023 #include<TableImpl.h>
00024 void PredicateImpl::print()
00025 {
00026     printf("FieldName1 %s, FieldName2 %s", fldName1, fldName2);
00027     printf("CompOp %d, operand %x operandPtr%x", compOp, operand);
00028     printf("lhs %x, rhs %x", lhs, rhs);
00029     
00030 }
00031 
00032 void PredicateImpl::setTerm(const char* fName1, ComparisionOp op,
00033                         const char *fName2)
00034 {
00035     strcpy(fldName1, fName1);
00036     strcpy(fldName2, fName2);
00037     compOp = op;
00038     operand = NULL;
00039     operandPtr = NULL;
00040     lhs = rhs = NULL;
00041     logicalOp = OpInvalidLogicalOp;
00042 }
00043 
00044 //Operand should be of the same type of the field.This is must
00045 void PredicateImpl::setTerm(const char* fName1, ComparisionOp op, void *opnd)
00046 {
00047     strcpy(fldName1, fName1);
00048     compOp = op;
00049     operand = opnd;
00050     operandPtr = NULL;
00051     lhs = rhs = NULL;
00052     logicalOp = OpInvalidLogicalOp;
00053 }
00054 
00055 void PredicateImpl::setTerm(const char* fName1, ComparisionOp op, void **opnd)
00056 {
00057     strcpy(fldName1, fName1);
00058     compOp = op;
00059     operand = NULL;
00060     operandPtr = opnd;
00061     lhs = rhs = NULL;
00062     logicalOp = OpInvalidLogicalOp;
00063 }
00064 
00065 
00066 void PredicateImpl::setTerm(Predicate *p1, LogicalOp op, Predicate *p2 )
00067 {
00068     if (p2 == NULL && op != OpNot || op == OpNot && p2 != NULL) 
00069     { 
00070         //TODO::printError
00071         return; 
00072     }
00073     lhs = (PredicateImpl*)p1;
00074     rhs = (PredicateImpl*)p2;
00075     logicalOp = op;
00076     compOp = OpInvalidComparisionOp;
00077 }
00078 
00079 void PredicateImpl::setTable(Table *tbl)
00080 {
00081     if (NULL != lhs)
00082         lhs->setTable((TableImpl*)tbl);
00083     if (NULL != rhs)
00084         rhs->setTable((TableImpl*)tbl);
00085    table = (TableImpl*)tbl;
00086 }
00087 
00088 void PredicateImpl::setTuple(void *tpl)
00089 {
00090     if (NULL != lhs)
00091         lhs->setTuple(tpl);
00092     if (NULL != rhs)
00093          rhs->setTuple(tpl);
00094     tuple = tpl;
00095 }
00096 bool PredicateImpl::isSingleTerm()
00097 {
00098     if (NULL == lhs  && NULL == rhs) return true; else false;
00099 }
00100 
00101 
00102 bool PredicateImpl::isNotOrInvolved()
00103 {
00104     bool lhsResult = true, rhsResult = true;
00105     if (NULL != lhs)
00106     {
00107         lhsResult = lhs->isNotOrInvolved();
00108     }
00109     if (NULL != rhs)
00110     {
00111         rhsResult = rhs->isNotOrInvolved();
00112     }
00113     if (NULL != lhs)
00114     {
00115         //Means it involves only Logical operator
00116             switch(logicalOp)
00117             {
00118                 case OpAnd:
00119                      if (lhsResult || rhsResult) return true;  else return false;
00120                      break;
00121                 case OpOr:
00122                      return true;
00123                      break;
00124                 case OpNot:
00125                 default:
00126                      return true;
00127                      break;
00128         }
00129     }
00130     return false;
00131 }
00132 
00133 DbRetVal PredicateImpl::evaluate(bool &result)
00134 {
00135     bool rhsResult = false, lhsResult=false;
00136     printDebug(DM_Predicate, "Evaluate start logical:%d compOp:%d", logicalOp, compOp);
00137     DbRetVal retCode =OK;
00138     result = false;
00139     if (NULL != lhs)
00140     {
00141         retCode = lhs->evaluate(lhsResult);
00142         printDebug(DM_Predicate, "LHS result %d retcode: %d", lhsResult, retCode);
00143         if (retCode != OK) return ErrInvalidExpr;
00144     }
00145     if (NULL != rhs)
00146     {
00147         retCode = rhs->evaluate(rhsResult);
00148         printDebug(DM_Predicate, "RHS result %d retcode:%d", rhsResult, retCode);
00149         if (retCode != OK) return ErrInvalidExpr;
00150     }
00151     if (NULL != lhs)
00152     {
00153         //Means it involves only Logical operator
00154         printDebug(DM_Predicate,"Evalute operator %d lhsResult %d : rhsResult %d", logicalOp, lhsResult, rhsResult );
00155             switch(logicalOp)
00156             {
00157                 case OpAnd:
00158                     if (lhsResult && rhsResult) result = true;
00159                     break;
00160                 case OpOr:
00161                     if (lhsResult || rhsResult) result = true;
00162                     break;
00163                 case OpNot:
00164                     if (lhsResult)  result = false; else result = true;
00165                     break;
00166                 default:
00167                     return ErrInvalidExpr;
00168 
00169             }
00170             printDebug(DM_Predicate, "result is %d", result);
00171             return OK;
00172     }
00173     printDebug(DM_Predicate, "Evaluating comparision predicate op:%d", compOp);
00174     //Means it is relational expression
00175     //first operand is always field identifier
00176     //get the value in the tuple
00177     int offset1, offset2;
00178     offset1 = table->getFieldOffset(fldName1);
00179     //TODO::do not call getFieldXXX many times, instead get it using getFieldInfo
00180     char *val1, *val2 ;
00181     //Assumes that fldName2 data type is also same for expr f1 <f2
00182     DataType srcType = table->getFieldType(fldName1);
00183     val1 = ((char*) tuple) + offset1;
00184     if (operand == NULL && operandPtr == NULL)
00185     {
00186         if (fldName2) {
00187             offset2 = table->getFieldOffset(fldName2);
00188             val2 = ((char*)tuple) + offset2; 
00189         }
00190     } 
00191     else if(operand != NULL && operandPtr == NULL)
00192     { 
00193         val2 = (char*) operand;
00194     }
00195     else if(operand == NULL && operandPtr != NULL)
00196     { 
00197         val2 = *(char**)operandPtr;
00198     }
00199     int ret = 0;
00200     printDebug(DM_Predicate, " fldname :%s ", fldName1);
00201     result = AllDataType::compareVal(val1, val2, compOp, srcType,
00202                               table->getFieldLength(fldName1));
00203     return OK;
00204 }
00205 
00206 bool PredicateImpl::pointLookupInvolved(const char *fname)
00207 {
00208     bool rhsResult, lhsResult;
00209     if (NULL != lhs)
00210     {
00211         lhsResult = lhs->pointLookupInvolved(fname);
00212     }
00213     if (NULL != rhs)
00214     {
00215         rhsResult = rhs->pointLookupInvolved(fname);
00216     }
00217     if (NULL != lhs)
00218     {
00219         //Means it involves only Logical operator
00220             switch(logicalOp)
00221             {
00222                 case OpAnd:
00223                      //return lhsResult;
00224                      if (lhsResult || rhsResult) return true;  else return false;
00225                      break;
00226                 case OpOr:
00227                      return false;
00228                      break;
00229                 case OpNot:
00230                 default:
00231                      return false;
00232                      break;
00233         }
00234     }
00235     //Means it is relational expression
00236     //first operand is always field identifier
00237     if (OpEquals == compOp)
00238     {
00239         //for expressions f1 == f2 use full scan, so return false
00240         if(NULL == operand && NULL == operandPtr) return false;
00241         if(0 == strcmp(fldName1, fname)) 
00242         {
00243             return true;
00244         }
00245     }
00246     return false;
00247 }
00248 
00249 void* PredicateImpl::valPtrForIndexField(const char *fname)
00250 {
00251     void *lhsRet, *rhsRet;
00252     if (NULL != lhs)
00253     {
00254         lhsRet = lhs->valPtrForIndexField(fname);
00255     }
00256     if (NULL != rhs)
00257     {
00258         rhsRet = rhs->valPtrForIndexField(fname);
00259     }
00260     if (NULL != lhs)
00261     {
00262         //Means it involves only Logical operator
00263         if ( lhsRet !=  NULL) return lhsRet;
00264         if ( rhsRet !=  NULL) return rhsRet;
00265     }
00266     //Means it is relational expression
00267     //first operand is always field identifier
00268     if (OpEquals == compOp)
00269     {
00270         if(0 == strcmp(fldName1, fname)) 
00271         {
00272             if (operand) return operand; else return *(void**)operandPtr;
00273         }
00274     }
00275     return NULL;
00276 }

Generated on Mon Jun 9 22:37:15 2008 for csql by  doxygen 1.4.7