PEP 55 Update license on source files to current license text and date
[tpot/pegasus/.git] / src / Pegasus / WQL / WQLParser.h
1 //%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 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 //
24 //==============================================================================
25 //
26 // Author: Mike Brasher (mbrasher@bmc.com)
27 //
28 // Modified By:
29 //
30 //%/////////////////////////////////////////////////////////////////////////////
31
32 #ifndef Pegasus_WQLParser_h
33 #define Pegasus_WQLParser_h
34
35 #include <Pegasus/Common/Config.h>
36 #include <Pegasus/Common/ArrayInternal.h>
37 #include <Pegasus/WQL/Linkage.h>
38 #include <Pegasus/WQL/WQLSelectStatement.h>
39
40 PEGASUS_NAMESPACE_BEGIN
41
42 /** This class is the main interface to the WQL parser used for parsing WQL1
43     compliant SQL statements.
44
45     Here's an example which parses a SELECT statement:
46
47     <pre>
48         const char TEXT[] = "SELECT X,Y FROM MyClass WHERE X > 10 AND Y < 3";
49
50         // Note that this array must be null-terminated (sizeof(TEXT) includes
51         // the null-terminator in the count).
52
53         Array<Sint8> text(TEXT, sizeof(TEXT));
54
55         WQLSelectStatement selectStatement;
56
57         WQLParser parser;
58
59         try
60         {
61             parser.parse(text, selectStatement);
62         }
63         catch (ParseError&)
64         {
65             ...
66         }
67         catch (MissingNullTerminator&)
68         {
69             ...
70         }
71     </pre>
72
73     Note that the text must be NULL terminated or else the MissingNullTerminator
74     exception is thrown.
75
76     The text is read and the result is left in the selectStatement output
77     argument.
78
79     At this point you might wish to peek at the contents of the selectStatement.
80     This may be done by calling WQLSelectStatement::print() like this:
81
82     <pre>
83         WQLSelectStatement selectStatement;
84         ...
85         selectStatement.print();
86     </pre>
87
88     For the above query text, the following is printed:
89
90     <pre>
91         WQLSelectStatement
92         {
93             _className: "MyClass"
94
95             _propertyNames[0]: "X"
96             _propertyNames[1]: "Y"
97
98             _operations[0]: "WQL_GT"
99             _operations[1]: "WQL_LT"
100             _operations[2]: "WQL_AND"
101
102             _operands[0]: "PROPERTY_NAME: X"
103             _operands[1]: "INTEGER_VALUE: 10"
104             _operands[2]: "PROPERTY_NAME: Y"
105             _operands[3]: "INTEGER_VALUE: 3"
106         }
107     </pre>
108
109     The WQLSelectStatement::evaluateWhereClause() method may be called to
110     determine whether a particular instance (whose properties are made 
111     available to he evaluateWhereClause() by a user implementation of the
112     WQLPropertySource class). This method returns true, if the where clause
113     matches this instance. Here is an example:
114
115     <pre>
116         WQLSelectStatement selectStatement;
117         ...
118         WQLPropertySource* propertySource = new MyPropertySource(...);
119
120         if (selectStatement.evaluateWhereClause(propertySource))
121         {
122             // It's a match!
123         }
124     </pre>
125
126     The evaluateWhereClause() method calls propertySource->getValue() to
127     obtain values for each of the properties referred to in where clause (X 
128     and Y in the above query example).
129
130     The implementer of the WQLPropertySource interface must provide the
131     implementation of getValue() to produce values for the target data
132     types. The WQL library makes no assumptions about the nature of the 
133     target data representation. This was done so that this libary could be
134     adapted to multiple data representations.
135
136     For use with Pegasus CIMInstance objects, a CIMInstancePropertySource
137     class could be developed whose getValue() method fetches values from 
138     a CIMInstance. Here is an example of how it might be used.
139
140     <pre>
141         CIMInstancePropertySource* propertySource 
142             = new CIMInstancePropertySource(...);
143
144         CIMInstance instance;
145
146         while (instance = GetNextInstance(...))
147         {
148             propertySource->setInstance(currentInstance);
149
150             if (selectStatement.evaluateWhereClause(propertySource))
151             {
152                 // It's a match!
153             }
154         }
155     </pre>
156
157     Of course the numeration of instances is left to the user of WQL.
158 */
159 class PEGASUS_WQL_LINKAGE WQLParser
160 {
161 public:
162
163     /** Parse the SELECT statement given by the text parameter and initialize
164         the statement parameter accordingly.
165
166         Please note that this method is not thread safe. It must be guarded 
167         with mutexes by the caller.
168
169         @param text null-terminated C-string which points to SQL statement.
170         @param statement object which holds the compiled version of the SELECT
171             statement upon return.
172         @exception ParseError if text is not a valid SELECT statement.
173         @exception MissingNullTerminator if text argument is not 
174             terminated with a null. 
175     */
176     static void parse(
177         const char* text,
178         WQLSelectStatement& statement);
179
180     /** Version of parse() taking an array of characters.
181     */
182     static void parse(
183         const Array<Sint8>& text,
184         WQLSelectStatement& statement);
185
186     /** Version of parse() taking a string.
187     */
188     static void parse(
189         const String& text,
190         WQLSelectStatement& statement);
191
192 private:
193
194     /** This method cleans up all the strings which were created by LEX and
195         passed to YACC. These strings cannot be cleaned up by YACC actions
196         since the actions that clean up certain strings are not always reached
197         when errors occur.
198     */
199     static void cleanup();
200
201     /** Private constructor to avoid user from creating instance of this class.
202     */
203     WQLParser() { }
204 };
205
206 PEGASUS_NAMESPACE_END
207
208 #endif /* Pegasus_WQLParser_h */