Minor fixes
[csql.git] / src / server / PredicateImpl.cxx
blob05f0ea05e6f4173305e51861457ffcfa1ce8d3a9
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
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. *
9 * *
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. *
14 * *
15 ***************************************************************************/
16 #include<Table.h>
17 #include<Index.h>
18 #include<CatalogTables.h>
19 #include<Lock.h>
20 #include<Debug.h>
21 #include<PredicateImpl.h>
22 #include<Table.h>
23 #include<TableImpl.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,
33 const char *fName2)
35 strcpy(fldName1, fName1);
36 strcpy(fldName2, fName2);
37 compOp = op;
38 operand = NULL;
39 operandPtr = NULL;
40 lhs = rhs = NULL;
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);
47 compOp = op;
48 operand = opnd;
49 operandPtr = NULL;
50 lhs = rhs = NULL;
53 void PredicateImpl::setTerm(const char* fName1, ComparisionOp op, void **opnd)
55 strcpy(fldName1, fName1);
56 compOp = op;
57 operand = NULL;
58 operandPtr = opnd;
59 lhs = rhs = NULL;
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;
68 logicalOp = op;
71 void PredicateImpl::setTable(Table *tbl)
73 if (NULL != lhs)
74 lhs->setTable((TableImpl*)tbl);
75 if (NULL != rhs)
76 rhs->setTable((TableImpl*)tbl);
77 table = (TableImpl*)tbl;
80 void PredicateImpl::setTuple(void *tpl)
82 if (NULL != lhs)
83 lhs->setTuple(tpl);
84 if (NULL != rhs)
85 rhs->setTuple(tpl);
86 tuple = tpl;
89 DbRetVal PredicateImpl::evaluate(bool &result)
91 bool rhsResult, lhsResult;
92 DbRetVal retCode =OK;
93 if (NULL != lhs)
95 retCode = lhs->evaluate(lhsResult);
96 if (retCode != OK) return ErrInvalidExpr;
98 if (NULL != rhs)
100 retCode = rhs->evaluate(rhsResult);
101 if (retCode != OK) return ErrInvalidExpr;
103 if (NULL != lhs)
105 //Means it involves only Logical operator
106 switch(logicalOp)
108 case OpAnd:
109 if (lhsResult && rhsResult) result = true;
110 break;
111 case OpOr:
112 if (lhsResult || rhsResult) result = true;
113 break;
114 case OpNot:
115 if (lhsResult) result = false; else result = true;
116 break;
117 default:
118 return ErrInvalidExpr;
121 return OK;
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
129 char *val1, *val2 ;
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)
135 if (fldName2) {
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;
148 int ret = 0;
149 result = AllDataType::compareVal(val1, val2, compOp, srcType,
150 table->getFieldLength(fldName1));
151 return OK;
154 bool PredicateImpl::pointLookupInvolved(const char *fname)
156 bool rhsResult, lhsResult;
157 if (NULL != lhs)
159 lhsResult = lhs->pointLookupInvolved(fname);
161 if (NULL != rhs)
163 rhsResult = rhs->pointLookupInvolved(fname);
165 if (NULL != lhs)
167 //Means it involves only Logical operator
168 switch(logicalOp)
170 case OpAnd:
171 return lhsResult;
172 break;
173 case OpOr:
174 return false;
175 break;
176 case OpNot:
177 default:
178 return false;
179 break;
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))
190 return true;
193 return false;
196 void* PredicateImpl::valPtrForIndexField(const char *fname)
198 void *lhsRet, *rhsRet;
199 if (NULL != lhs)
201 lhsRet = lhs->valPtrForIndexField(fname);
203 if (NULL != rhs)
205 rhsRet = rhs->valPtrForIndexField(fname);
207 if (NULL != lhs)
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;
222 return NULL;