1 chuck 1.2 //%2003////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2000, 2001, 2002 BMC Software, Hewlett-Packard Development
4 // Company, L. P., IBM Corp., The Open Group, Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L. P.;
6 // IBM Corp.; EMC Corporation, The Open Group.
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to
10 // deal in the Software without restriction, including without limitation the
11 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 // sell copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
16 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
17 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
18 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
19 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 chuck 1.2 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 //
24 //==============================================================================
25 //
26 // Author: Humberto Rivero (hurivero@us.ibm.com)
27 //
28 // Modified By:
29 //
30 //%/////////////////////////////////////////////////////////////////////////////
31
32 #ifndef Pegasus_CQLParser_h
33 #define Pegasus_CQLParser_h
34
35 #include <Pegasus/Common/Config.h>
36 #include <Pegasus/Common/ArrayInternal.h>
37 #include <Pegasus/CQL/Linkage.h>
38 #include <Pegasus/CQL/CQLSelectStatement.h>
39
40 #ifdef PEGASUS_USE_EXPERIMENTAL_INTERFACES
41
42 PEGASUS_NAMESPACE_BEGIN
43 chuck 1.2
44 /** This class is the main interface to the CQL parser used for parsing CQL1
45 compliant SQL statements.
46
47 Here's an example which parses a SELECT statement:
48
49 <pre>
50 const char TEXT[] = "SELECT X,Y FROM MyClass WHERE X > 10 AND Y < 3";
51
52 // Note that this array must be null-terminated (sizeof(TEXT) includes
53 // the null-terminator in the count).
54
55 Array<Sint8> text(TEXT, sizeof(TEXT));
56
57 WQLSelectStatement selectStatement;
58
59 WQLParser parser;
60
61 try
62 {
63 parser.parse(text, selectStatement);
64 chuck 1.2 }
65 catch (ParseError&)
66 {
67 ...
68 }
69 catch (MissingNullTerminator&)
70 {
71 ...
72 }
73 </pre>
74
75 Note that the text must be NULL terminated or else the MissingNullTerminator
76 exception is thrown.
77
78 The text is read and the result is left in the selectStatement output
79 argument.
80
81 At this point you might wish to peek at the contents of the selectStatement.
82 This may be done by calling WQLSelectStatement::print() like this:
83
84 <pre>
85 chuck 1.2 WQLSelectStatement selectStatement;
86 ...
87 selectStatement.print();
88 </pre>
89
90 For the above query text, the following is printed:
91
92 <pre>
93 WQLSelectStatement
94 {
95 _className: "MyClass"
96
97 _propertyNames[0]: "X"
98 _propertyNames[1]: "Y"
99
100 _operations[0]: "WQL_GT"
101 _operations[1]: "WQL_LT"
102 _operations[2]: "WQL_AND"
103
104 _operands[0]: "PROPERTY_NAME: X"
105 _operands[1]: "INTEGER_VALUE: 10"
106 chuck 1.2 _operands[2]: "PROPERTY_NAME: Y"
107 _operands[3]: "INTEGER_VALUE: 3"
108 }
109 </pre>
110
111 The WQLSelectStatement::evaluateWhereClause() method may be called to
112 determine whether a particular instance (whose properties are made
113 available to he evaluateWhereClause() by a user implementation of the
114 WQLPropertySource class). This method returns true, if the where clause
115 matches this instance. Here is an example:
116
117 <pre>
118 WQLSelectStatement selectStatement;
119 ...
120 WQLPropertySource* propertySource = new MyPropertySource(...);
121
122 if (selectStatement.evaluateWhereClause(propertySource))
123 {
124 // It's a match!
125 }
126 </pre>
127 chuck 1.2
128 The evaluateWhereClause() method calls propertySource->getValue() to
129 obtain values for each of the properties referred to in where clause (X
130 and Y in the above query example).
131
132 The implementer of the WQLPropertySource interface must provide the
133 implementation of getValue() to produce values for the target data
134 types. The WQL library makes no assumptions about the nature of the
135 target data representation. This was done so that this libary could be
136 adapted to multiple data representations.
137
138 For use with Pegasus CIMInstance objects, a CIMInstancePropertySource
139 class could be developed whose getValue() method fetches values from
140 a CIMInstance. Here is an example of how it might be used.
141
142 <pre>
143 CIMInstancePropertySource* propertySource
144 = new CIMInstancePropertySource(...);
145
146 CIMInstance instance;
147
148 chuck 1.2 while (instance = GetNextInstance(...))
149 {
150 propertySource->setInstance(currentInstance);
151
152 if (selectStatement.evaluateWhereClause(propertySource))
153 {
154 // It's a match!
155 }
156 }
157 </pre>
158
159 Of course the numeration of instances is left to the user of WQL.
160 */
161 class PEGASUS_CQL_LINKAGE CQLParser
162 {
163 public:
164
165 /** Parse the SELECT statement given by the text parameter and initialize
166 the statement parameter accordingly.
167
168 Please note that this method is not thread safe. It must be guarded
169 chuck 1.2 with mutexes by the caller.
170
171 @param text null-terminated C-string which points to SQL statement.
172 @param statement object which holds the compiled version of the SELECT
173 statement upon return.
174 @exception ParseError if text is not a valid SELECT statement.
175 @exception MissingNullTerminator if text argument is not
176 terminated with a null.
177 */
178 static void parse(
179 const char* text,
180 CQLSelectStatement& statement);
181
182 /** Version of parse() taking an array of characters.
183 */
184 static void parse(
185 const Array<Sint8>& text,
186 CQLSelectStatement& statement);
187
188 /** Version of parse() taking a string.
189 */
190 chuck 1.2 static void parse(
191 const String& text,
192 CQLSelectStatement& statement);
193
194 private:
195
196 /** This method cleans up all the strings which were created by LEX and
197 passed to YACC. These strings cannot be cleaned up by YACC actions
198 since the actions that clean up certain strings are not always reached
199 when errors occur.
200 */
201 static void cleanup();
202
203 /** Private constructor to avoid user from creating instance of this class.
204 */
205 CQLParser() { }
206 };
207
208 PEGASUS_NAMESPACE_END
209 #endif
210 #endif /* Pegasus_WQLParser_h */
|