00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
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
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
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
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
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
00175
00176
00177 int offset1, offset2;
00178 offset1 = table->getFieldOffset(fldName1);
00179
00180 char *val1, *val2 ;
00181
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
00220 switch(logicalOp)
00221 {
00222 case OpAnd:
00223
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
00236
00237 if (OpEquals == compOp)
00238 {
00239
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
00263 if ( lhsRet != NULL) return lhsRet;
00264 if ( rhsRet != NULL) return rhsRet;
00265 }
00266
00267
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 }