dynamic_context.h
Go to the documentation of this file.
1 /*
2  * Copyright 2006-2008 The FLWOR Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef ZORBA_DYNAMIC_CONTEXT_API_H
17 #define ZORBA_DYNAMIC_CONTEXT_API_H
18 
19 #include <time.h>
20 #include <istream>
21 #include <memory>
22 
23 #include <zorba/config.h>
24 #include <zorba/api_shared_types.h>
26 #include <zorba/xmldatamanager.h>
28 
29 
30 namespace zorba {
31 
32 
33 /** \brief Instances of the class DynamicContext contain the information that is available at the
34  * time the query is executed.
35  *
36  * The class contains the information that is defined in the %XQuery
37  * specification (see http://www.w3.org/TR/xquery/#eval_context).
38  *
39  * A dynamic context always belongs to a particular query and, hence, can be retrieved by
40  * calling getDynamicContext on a compiled query (see XQuery::getDynamicContext()).
41  *
42  */
43 class ZORBA_DLL_PUBLIC DynamicContext
44 {
45  public:
46 
47  /**
48  * \brief Defines the external variable identified by aQName and assigns it
49  * the value of aItem.
50  *
51  * aQName may be in one of two forms: A lexical QName (eg. "ns:foo"), or a
52  * James Clark-style universal name (eg. "{nsuri}:foo"). If it is a universal
53  * name, then this method will find the named external variable in the main
54  * query or in any modules imported directly or indirectly by the query. If it
55  * is a lexical QName, then it is only possible to resolve the prefix in the
56  * the context of the main query, hence only external variables in the main
57  * query or those in directly-imported modules may be bound.
58  *
59  * @param aQName the QName that identifies the external variable.
60  * @param aItem the Item that is used as value for the variable.
61  * @return true if the variable has been set, false otherwise.
62  * @throw ZorbaException if an error occured (e.g. the given Item is not valid).
63  */
64  virtual bool
65  setVariable(
66  const String& aQName,
67  const Item& aItem) = 0;
68 
69  /**
70  * \brief Defines the external variable identified by aQName and assigns it
71  * the sequence that is returned by evaluating aIterator.
72  *
73  * aQName may be in one of two forms: A lexical QName (eg. "ns:foo"), or a
74  * James Clark-style universal name (eg. "{nsuri}:foo"). If it is a universal
75  * name, then this method will find the named external variable in the main
76  * query or in any modules imported directly or indirectly by the query. If it
77  * is a lexical QName, then it is only possible to resolve the prefix in the
78  * the context of the main query, hence only external variables in the main
79  * query or those in directly-imported modules may be bound.
80  *
81  * @param aQName the QName that identifies the external variable.
82  * @param aIterator the Iterator producing the sequence that is assigned
83  * to the variable.
84  * @return true if the variable has been set successfully, false otherwise.
85  * @throw ZorbaException if an error occured (e.g. the given Iterator is not valid).
86  */
87  virtual bool
88  setVariable(
89  const String& aQName,
90  const Iterator_t& aIterator) = 0;
91 
92  /**
93  * \brief Defines the external variable identified by an expanded QName and
94  * assigns it the sequence that is returned by evaluating aIterator.
95  *
96  * The named external variable may be located in the main query or in any
97  * modules imported directly or indirectly by the query.
98  *
99  * @param aNamespace the namespace URI of the variable's expanded QName
100  * @param aLocalname the local name of the variable's expanded QName
101  * @param aIterator the Iterator producing the sequence that is assigned
102  * to the variable.
103  * @return true if the variable has been set successfully, false otherwise.
104  * @throw ZorbaException if an error occured (e.g. the given Iterator is not valid).
105  */
106  virtual bool
107  setVariable(
108  const String& aNamespace,
109  const String& aLocalname,
110  const Iterator_t& aIterator) = 0;
111 
112  /** \brief Returns the current value of an external
113  * variable. Exactly one of the two return values (aItem or
114  * aIterator) will be non-null; that is, have isNull() == false.
115  *
116  * The named external variable may be located in the main query or in any
117  * modules imported directly or indirectly by the query.
118  *
119  * @param aNamespace the namespace URI of the variable's expanded QName
120  * @param aLocalname the local name of the variable's expanded QName
121  * @param aItem an Item representing the current (single-item) value of
122  * the external variable.
123  * @param aIterator an Iterator representing the current (possibly
124  * multi-item) value of the external variable.
125  * @return true if the variable has been retrieved successfully, false otherwise.
126  * @throw ZorbaException if an error occured.
127  */
128  virtual bool
129  getVariable( const String& aNamespace,
130  const String& aLocalname,
131  Item& aItem,
132  Iterator_t& aIterator) const = 0;
133 
134  /** \brief Defines the context item.
135  *
136  * @param aItem the Item that is used as value for the context item.
137  * @return true if the context item was set, false otherwise.
138  * @throw ZorbaException if an error occured (e.g. the given Item is not valid).
139  */
140  virtual bool
141  setContextItem ( const Item& aItem ) = 0;
142 
143  /** \brief Returns the current value of the context item.
144  *
145  * @param aItem an Item representing the current value of the context item.
146  * @return true if the variable has been retrieved successfully, false otherwise.
147  * @throw ZorbaException if an error occured.
148  */
149  virtual bool
150  getContextItem( Item& aItem ) const = 0;
151 
152  /** \brief Defines the value of the current date time that can be accessed by the
153  * fn:current-dateTime() function at the time the query is executed.
154  *
155  * If the current date time has not been set explicitly the value of the date
156  * and time is used at the time the query is created or cloned, respectively.
157  *
158  * @param aDateTimeItem the dateTime Item.
159  * @return true if the variable has been set successfully, false otherwise.
160  * @throw ZorbaException if an error occured (e.g. the given Item is invalid
161  * or not a Item of type dateTime)
162  */
163  virtual bool
164  setCurrentDateTime( const Item& aDateTimeItem ) = 0;
165 
166  /** \brief Retrieve the dateTime Item used at the time the query is executed
167  * (see setCurrentDateTime()).
168  *
169  * @return Item the dateTime Item used at the time the query is executed.
170  */
171  virtual Item
172  getCurrentDateTime( ) const = 0;
173 
174  /** \brief Defines the variable of the implicit timezone to be used when a date, time,
175  * or dateTime value that does not have a timezone is used in a comparison or
176  * arithmetic operation.
177  *
178  * @param aTimezone the implicit timezone as int that should be used.
179  * @return true if the implicit timezone has been set successfully, false otherwise.
180  * @throw ZorbaException if an error occured.
181  */
182  virtual bool
183  setImplicitTimezone( int aTimezone ) = 0;
184 
185  /** \brief Retrieve the implicit timezone used in comparisons or arithmetic operations
186  * of date, time, or dateTime values.
187  *
188  * @return int the implicit timezone. Note that 0 is returned if an error occured
189  * and an DiagnosticHandler is used.
190  * @throw ZorbaException if an error occured.
191  */
192  virtual int
193  getImplicitTimezone() const = 0;
194 
195  /** \brief Defines the value of the default collection that is used when calling the
196  * fn:collection function without a parameter.
197  *
198  * @param aCollectionUri the URI of the collection used by the fn:collection function.
199  * @return true if the default collection has been set successfully, false otherwise.
200  * @throw ZorbaException if an error occured.
201  */
202  virtual bool
203  setDefaultCollection( const Item& aCollectionUri ) = 0;
204 
205  /** \brief Return the value of the default collection that is used when calling the
206  * fn:collection function without a parameter.
207  *
208  * @return Item the default collection that is set in this dynamic context.
209  * @throw ZorbaException if an error occured.
210  */
211  virtual Item
212  getDefaultCollection() const = 0;
213 
214  /** \brief Add a name-value pair to this context.
215  * The value can be accessed in the evaluate method
216  * of external functions (see ContextualExternalFunction).
217  *
218  * @param aName the name of the parameter to add. If an entry with
219  * the same name already exists, the existing entry is replaced.
220  * @param aValue the value that can be accessed in the evaluate method.
221  * @return returns true if an entry with the same name did not already exist,
222  * false otherwise.
223  */
224  virtual bool
225  addExternalFunctionParam( const String& aName, void* aValue ) = 0;
226 
227  /** \brief Get the value of a pair that was registered using
228  * the addExternalFunctionParam method. This can
229  * be used in the evaluate method
230  * of external functions (see ContextualExternalFunction).
231  *
232  * @param aName the name of the parameter to retrieve
233  * @param aValue the value matching the given name if true is returned.
234  * @return true if an entry with the given name was found,
235  * false otherwise.
236  */
237  virtual bool
238  getExternalFunctionParam ( const String& aName, void*& aValue ) const = 0;
239 
240  /** \brief Add a name-value pair to this context.
241  * The value can be accessed in the evaluate method
242  * of external functions (see ContextualExternalFunction).
243  *
244  * @param aName the name of the parameter to add. If an entry with
245  * the same name already exists, the existing entry is replaced.
246  * @param aParam the parameter to add
247  * @return true if an entry with the same name did not exist already,
248  * false otherwise.
249  */
250  virtual bool
251  addExternalFunctionParameter ( const String& aName, ExternalFunctionParameter* aParam ) = 0;
252 
253  /** \brief Get the value of a pair that was registered using
254  * the addExternalFunctionParam method. This can
255  * be used in the evaluate method
256  * of external functions (see ContextualExternalFunction).
257  *
258  * @param aName the name of the parameter to retrieve
259  * @return the ExternalFunctionParameter* that was added using
260  * addExternalFunctionParameter, or 0 if no entry with the given
261  * name was found.
262  */
264  getExternalFunctionParameter ( const String& aName ) const = 0;
265 
266  /** \brief Returns true if the variable is bound to a value
267  *
268  * @param aNamespace the namespace of the qname of the variable to check
269  * @param aLocalname the localname of the qname of the variable to check
270  */
271  virtual bool
272  isBoundExternalVariable(const String& aNamespace, const String& aLocalname) const = 0;
273 
274  /** \brief Returns true if a context item has been bound to the Dynamic Context
275  */
276  virtual bool
277  isBoundContextItem() const = 0;
278 
279 protected:
280  /** \brief Destructor
281  */
282  virtual ~DynamicContext( ) {}
283 };
284 
285 } /* namespace zorba */
286 
287 #endif
288 /* vim:set et sw=2 ts=2: */
blog comments powered by Disqus