(file) Return to WQLSelectStatement.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / WQL

  1 mike  1.1.2.1 //%/////////////////////////////////////////////////////////////////////////////
  2               //
  3               // Copyright (c) 2000, 2001 BMC Software, Hewlett-Packard Company, IBM, 
  4               // The Open Group, Tivoli Systems
  5               //
  6               // Permission is hereby granted, free of charge, to any person obtaining a copy
  7               // of this software and associated documentation files (the "Software"), to 
  8               // deal in the Software without restriction, including without limitation the 
  9               // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 
 10               // sell copies of the Software, and to permit persons to whom the Software is
 11               // furnished to do so, subject to the following conditions:
 12               // 
 13               // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN 
 14               // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 15               // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 16               // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
 17               // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
 18               // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
 19               // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 20               // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 21               //
 22 mike  1.1.2.1 //==============================================================================
 23               //
 24               // Author: Mike Brasher (mbrasher@bmc.com)
 25               //
 26               // Modified By:
 27               //
 28               //%/////////////////////////////////////////////////////////////////////////////
 29               
 30               #include <iostream>
 31 mike  1.1.2.4 #include <Pegasus/Common/Stack.h>
 32 mike  1.1.2.1 #include "WQLSelectStatement.h"
 33               
 34               PEGASUS_USING_STD;
 35               
 36               PEGASUS_NAMESPACE_BEGIN
 37               
 38 mike  1.1.2.4 template<class T>
 39               inline static Boolean _Compare(const T& x, const T& y, WQLOperation op)
 40               {
 41                   switch (op)
 42                   {
 43               	case WQL_EQ: 
 44               	    return x == y;
 45               
 46               	case WQL_NE: 
 47               	    return x != y;
 48               
 49               	case WQL_LT: 
 50               	    return x < y;
 51               	case WQL_LE: 
 52               	    return x <= y;
 53               
 54               	case WQL_GT: 
 55               	    return x > y;
 56               
 57               	case WQL_GE: 
 58               	    return x >= y;
 59 mike  1.1.2.4 
 60               	default:
 61               	    PEGASUS_ASSERT(0);
 62                   }
 63               
 64                   return false;
 65               }
 66               
 67               static Boolean _Evaluate(
 68                   const WQLOperand& lhs, 
 69                   const WQLOperand& rhs, 
 70                   WQLOperation op)
 71               {
 72                   switch (lhs.getType())
 73                   {
 74               	case WQLOperand::NULL_VALUE:
 75               	{
 76               	    // ATTN: not sure what to do here:
 77               	    PEGASUS_ASSERT(0);
 78               	    break;
 79               	}
 80 mike  1.1.2.4 
 81               	case WQLOperand::INTEGER_VALUE:
 82               	{
 83               	    return _Compare(
 84               		lhs.valueOf(WQLOperand::INTEGER_VALUE_TAG), 
 85               		rhs.valueOf(WQLOperand::INTEGER_VALUE_TAG), 
 86               		op);
 87               	}
 88               
 89               	case WQLOperand::DOUBLE_VALUE:
 90               	{
 91               	    return _Compare(
 92               		lhs.valueOf(WQLOperand::DOUBLE_VALUE_TAG), 
 93               		rhs.valueOf(WQLOperand::DOUBLE_VALUE_TAG), 
 94               		op);
 95               	}
 96               
 97               	case WQLOperand::BOOLEAN_VALUE:
 98               	{
 99               	    return _Compare(
100               		lhs.valueOf(WQLOperand::BOOLEAN_VALUE_TAG), 
101 mike  1.1.2.4 		rhs.valueOf(WQLOperand::BOOLEAN_VALUE_TAG), 
102               		op);
103               	}
104               
105               	case WQLOperand::STRING_VALUE:
106               	{
107               	    return _Compare(
108               		lhs.valueOf(WQLOperand::STRING_VALUE_TAG), 
109               		rhs.valueOf(WQLOperand::STRING_VALUE_TAG), 
110               		op);
111               	}
112               
113               	default:
114               	    PEGASUS_ASSERT(0);
115                   }
116               
117                   return false;
118               }
119               
120 mike  1.1.2.1 WQLSelectStatement::WQLSelectStatement()
121               {
122                   //
123                   // Reserve space for a where clause with up to sixteen terms.
124                   //
125               
126                   _operations.reserve(16);
127                   _operands.reserve(16);
128               }
129               
130               WQLSelectStatement::~WQLSelectStatement()
131               {
132               
133               }
134               
135               void WQLSelectStatement::clear()
136               {
137                   _className.clear();
138                   _propertyNames.clear();
139                   _operations.clear();
140                   _operands.clear();
141 mike  1.1.2.1 }
142               
143               Boolean WQLSelectStatement::evaluateWhereClause(
144 mike  1.1.2.4     const WQLPropertySource* source) const
145 mike  1.1.2.1 {
146 mike  1.1.2.4     WQLSelectStatement* that = (WQLSelectStatement*)this;
147                   Stack<Boolean> stack;
148                   stack.reserve(16);
149               
150                   // 
151                   // Counter for operands:
152                   //
153               
154                   Uint32 j = 0;
155               
156                   //
157                   // Process each of the operations:
158                   //
159               
160                   for (Uint32 i = 0, n = _operations.size(); i < n; i++)
161                   {
162               	WQLOperation op = _operations[i];
163               
164               	switch (op)
165               	{
166               	    case WQL_OR:
167 mike  1.1.2.4 	    {
168               		PEGASUS_ASSERT(stack.size() >= 2);
169               
170               		Boolean op1 = stack.top();
171               		stack.pop();
172               
173               		Boolean op2 = stack.top();
174               
175               		stack.top() = op1 || op2;
176               		break;
177               	    }
178               
179               	    case WQL_AND:
180               	    {
181               		PEGASUS_ASSERT(stack.size() >= 2);
182               
183               		Boolean op1 = stack.top();
184               		stack.pop();
185               
186               		Boolean op2 = stack.top();
187               
188 mike  1.1.2.4 		stack.top() = op1 && op2;
189               		break;
190               	    }
191               
192               	    case WQL_NOT:
193               	    {
194               		PEGASUS_ASSERT(stack.size() >= 1);
195               
196               		Boolean op = stack.top();
197               		stack.top() = !op;
198               		break;
199               	    }
200               
201               	    case WQL_EQ:
202               	    case WQL_NE:
203               	    case WQL_LT:
204               	    case WQL_LE:
205               	    case WQL_GT:
206               	    case WQL_GE:
207               	    {
208               		PEGASUS_ASSERT(_operands.size() >= 2);
209 mike  1.1.2.4 
210               		//
211               		// Resolve the left-hand-side to a value (if not already
212               		// a value).
213               		//
214               
215               		WQLOperand& lhs = that->_operands[j++];
216               
217               		if (lhs.getType() == WQLOperand::PROPERTY_NAME)
218               		{
219               		    const String& propertyName = 
220               			lhs.valueOf(WQLOperand::PROPERTY_NAME_TAG);
221               
222               		    if (!source->getValue(propertyName, lhs))
223               			throw NoSuchProperty(propertyName);
224               		}
225               
226               		//
227               		// Resolve the right-hand-side to a value (if not already
228               		// a value).
229               		//
230 mike  1.1.2.4 
231               		WQLOperand& rhs = that->_operands[j++];
232               
233               		if (rhs.getType() == WQLOperand::PROPERTY_NAME)
234               		{
235               		    const String& propertyName = 
236               			rhs.valueOf(WQLOperand::PROPERTY_NAME_TAG);
237               
238               		    if (!source->getValue(propertyName, rhs))
239               			throw NoSuchProperty(propertyName);
240               		}
241               
242               		//
243               		// Check for a type mismatch:
244               		//
245               
246               		// PEGASUS_OUT(lhs.toString());
247               		// PEGASUS_OUT(rhs.toString());
248               
249               		if (rhs.getType() != lhs.getType())
250               		    throw TypeMismatch();
251 mike  1.1.2.4 
252               		//
253               		// Now that the types are known to be alike, apply the
254               		// operation:
255               		//
256               
257               		stack.push(_Evaluate(lhs, rhs, op));
258               		break;
259               	    }
260               
261               	    // 
262               	    // ATTN: implement these next!
263               	    //
264               
265               	    case WQL_IS_NULL:
266               	    case WQL_IS_TRUE:
267               	    case WQL_IS_FALSE:
268               	    case WQL_IS_NOT_NULL:
269               	    case WQL_IS_NOT_TRUE:
270               	    case WQL_IS_NOT_FALSE:
271               		PEGASUS_ASSERT(0);
272 mike  1.1.2.4 		break;
273               	}
274                   }
275               
276                   PEGASUS_ASSERT(stack.size() == 1);
277                   return stack.top();
278 mike  1.1.2.1 }
279               
280               void WQLSelectStatement::print() const
281               {
282                   //
283                   // Print the header:
284                   //
285                   
286                   cout << "WQLSelectStatement" << endl;
287                   cout << "{" << endl;
288               
289                   //
290                   // Print the class name:
291                   //
292               
293                   cout << "    _className: \"" << _className << '"' << endl;
294               
295                   // 
296                   // Print the property:
297                   //
298               
299 mike  1.1.2.1     for (Uint32 i = 0; i < _propertyNames.size(); i++)
300                   {
301 mike  1.1.2.2 	if (i == 0)
302               	    cout << endl;
303               
304 mike  1.1.2.1 	cout << "    _propertyNames[" << i << "]: ";
305               	cout << '"' << _propertyNames[i] << '"' << endl;
306                   }
307               
308                   //
309                   // Print the operations:
310                   //
311               
312                   for (Uint32 i = 0; i < _operations.size(); i++)
313                   {
314 mike  1.1.2.2 	if (i == 0)
315               	    cout << endl;
316               
317 mike  1.1.2.1 	cout << "    _operations[" << i << "]: ";
318               	cout << '"' << WQLOperationToString(_operations[i]) << '"' << endl;
319                   }
320               
321                   //
322                   // Print the operands:
323                   //
324               
325                   for (Uint32 i = 0; i < _operands.size(); i++)
326                   {
327 mike  1.1.2.2 	if (i == 0)
328               	    cout << endl;
329               
330               	cout << "    _operands[" << i << "]: ";
331 mike  1.1.2.1 	cout << '"' << _operands[i].toString() << '"' << endl;
332                   }
333               
334                   //
335                   // Print the trailer:
336                   //
337               
338                   cout << "}" << endl;
339               }
340               
341               PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2