> The XQuery Processor
00001 /* 00002 * Copyright 2006-2008 The FLWOR Foundation. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #ifndef ZORBA_XQUERY_API_H 00017 #define ZORBA_XQUERY_API_H 00018 00019 #include <ostream> 00020 00021 #include <zorba/config.h> 00022 #include <zorba/sax2.h> 00023 #include <zorba/api_shared_types.h> 00024 #include <zorba/options.h> 00025 00026 00027 namespace zorba { 00028 00029 /** \brief This class is the representation of an %XQuery in the %Zorba engine. 00030 * 00031 * To compile and execute an XQuery, an instance of this class must be created. 00032 * This is done by using either the createQuery or compileQuery methods of the 00033 * Zorba class. These methods return an instance of XQuery_t, which is a 00034 * reference counted smart pointer to a dynamically allocated XQuery object. After receiving 00035 * an XQuery_t, an application can make multiple copies of it. 00036 * Hence, each XQuery object can have multiple owners, potentially in different 00037 * threads. The XQuery object is deleted when all XQuery_t objects that point 00038 * to it are destroyed. 00039 * 00040 * Although an XQuery instance may be accessible from multiple threads, the 00041 * instance is not thread safe, i.e.\ its methods should not be invoked in 00042 * parallel by multiple threads. The only exception to this rule is the clone() 00043 * method. Cloning is the way to execute the same query from multiple threads. 00044 * Cloning can be done only after the query has been compiled. The clone() 00045 * method creates and returns a new XQuery object that shares the same 00046 * execution plan as the original XQuery object. After clone() returns, the 00047 * calling thread can invoke the various execution methods on the XQuery clone. 00048 * 00049 * The file \link simple.cpp \endlink contains some basic examples the demonstrate 00050 * the use of this class. 00051 * 00052 * Note: This class is reference counted. When writing multi-threaded clients, 00053 * it is the responibility of the client code to synchronize assignments to the 00054 * SmartPtr holding this object. 00055 */ 00056 class ZORBA_EXTERN_DECL XQuery : public SmartObject 00057 { 00058 public: 00059 /** \brief Destructor that destroys this XQuery object. 00060 * 00061 * The destructor is called automatically when there are no more XQuery_t 00062 * smart pointers pointing to this XQuery instance. 00063 */ 00064 virtual ~XQuery() {} 00065 00066 /** \brief Register an ErrorHandler to which errors during compilation or 00067 * execution/serialization are reported. 00068 * 00069 * If no ErrorHandler has been set using this function or when creating/compiling 00070 * the query using the Zorba object (i.e.\ createQuery or compileQuery), 00071 * then subclasses of the ZorbaException class are thrown to report errors. 00072 * 00073 * @param aErrorHandler ErrorHandler to which errors are reported. The 00074 * caller retains ownership over the ErrorHandler passed as 00075 * parameter. 00076 * @throw SystemException if the query has been closed. 00077 * @see close() 00078 */ 00079 virtual void 00080 registerErrorHandler(ErrorHandler* aErrorHandler) = 0; 00081 00082 /** \brief Reset the error handling mechanism to throwing exceptions, 00083 * i.e.\ behave as if no ErrorHandler had been set. 00084 * 00085 * @throw SystemException if the query has been closed already. 00086 * @see registerErrorHandler(ErrorHandler*) 00087 */ 00088 virtual void 00089 resetErrorHandler() = 0; 00090 00091 /** \brief Check if this query is an updating query. 00092 * 00093 * @return true if the query is an updating query, false otherwise. 00094 * @throw SystemException if the query is not compiled or has been closed. 00095 * @see close() 00096 * @see compile(...) 00097 */ 00098 virtual bool 00099 isUpdateQuery() const = 0; 00100 00101 /** \brief Serialize the result of the query as text to the given output stream. 00102 * 00103 * @param aOutStream the output stream on which the result is written. 00104 * @param aSerOptions an optinal set of serialization options. 00105 * @throw ZorbaException if an error occurs (e.g. the query is closed or has not been compiled) 00106 */ 00107 virtual void 00108 serialize(std::ostream& aOutStream, 00109 const Zorba_SerializerOptions_t& aSerOptions = Zorba_SerializerOptions_default()) = 0; 00110 00111 /** \brief Apply/execute the query if it is an updating query. 00112 * 00113 * @throw ZorbaException if an error occured, e.g. if the query has been closed, if the query 00114 * has not been compiled, or an error occures during execution. 00115 */ 00116 virtual void 00117 applyUpdates() = 0; 00118 00119 /** \brief Get an iterator for the result of the query. 00120 * 00121 * @return ResultIterator iterator over the result sequence. 00122 * @throw ZorbaException if an error occurs (e.g. the query is closed or has not been compiled). 00123 */ 00124 virtual ResultIterator_t 00125 iterator() = 0; 00126 00127 /** \brief Get the dynamic context of this query. 00128 * 00129 * This function returns the dynamic context that belongs to this query and 00130 * is used during query execution. 00131 * The context can be used, for example, to set values of external variables, 00132 * the default collation, or the current datetime. 00133 * It is only available if the query has been compiled, otherwise 00134 * an error is reported. Moreover, the context must not be modified during the 00135 * execution of a query (i.e. if a ResultIterator is opened). 00136 * The lifetime of the context returned by this function is restricted 00137 * by the lifetime of the according query object. 00138 * 00139 * @throw SystemException if the query has not been compiled or is closed. 00140 * @return DynamicContext of this query. 00141 */ 00142 virtual DynamicContext* 00143 getDynamicContext() const = 0; 00144 00145 /** \brief Get the static context of this query. 00146 * 00147 * This function returns the static context that belongs to this query. 00148 * The static context is only available if the query has been compiled, otherwise 00149 * an error is reported. 00150 * The context has all the components and values that have been set by the either 00151 * the static context that was passed when creating the query and and those that 00152 * were set in the prolog of the query. 00153 * Note that after compilation of the query the static context is a read only structure. 00154 * Moreover, the lifetime of the context returned by this function is restricted 00155 * by the lifetime of the according query object. 00156 * 00157 * @throw SystemException if the query has not been compiled or is closed. 00158 * @return StaticContext of this query. 00159 */ 00160 virtual const StaticContext* 00161 getStaticContext() const = 0; 00162 00163 /** \brief Compile the give query String. 00164 * 00165 * @param aQuery the query String to compile. 00166 * @throw ZorbaException if the query has been closed, is already compiled, or 00167 * an error occurs while compiling the query. 00168 */ 00169 virtual void 00170 compile(const String& aQuery) = 0; 00171 00172 /** \brief Compile the give query String with the given compiler hints. 00173 * 00174 * @param aQuery the query String to compile. 00175 * @param aHints hints passed to the query compiler. 00176 * @throw ZorbaException if the query has been closed, is already compiled, or 00177 * an error occurs while compiling the query. 00178 */ 00179 virtual void 00180 compile(const String& aQuery, const Zorba_CompilerHints_t& aHints) = 0; 00181 00182 /** \brief Compile the query given as an input stream with the given compiler hints. 00183 * 00184 * @param aQuery the query input stream. 00185 * @param aHints hints passed to the query compiler. 00186 * @throw ZorbaException if the query has been closed, is already compiled, or 00187 * an error occurs while compiling the query. 00188 */ 00189 virtual void 00190 compile(std::istream& aQuery, const Zorba_CompilerHints_t& aHints) = 0; 00191 00192 /** \brief Compile the give query String with the given static context and the 00193 * given compiler hints. 00194 * 00195 * @param aQuery the query String to compile. 00196 * @param aStaticContext the static context. 00197 * @param aHints hints passed to the query compiler. 00198 * @throw ZorbaException if the query has been closed, is already compiled, or 00199 * an error occurs while compiling the query. 00200 */ 00201 virtual void 00202 compile(const String& aQuery, const StaticContext_t& aStaticContext, 00203 const Zorba_CompilerHints_t& aHints) = 0; 00204 00205 /** \brief Compile the query given as an input stream with the given static context and the 00206 * given compiler hints. 00207 * 00208 * @param aQuery the query input stream. 00209 * @param aStaticContext the static context. 00210 * @param aHints hints passed to the query compiler. 00211 * @throw ZorbaException if the query has been closed, is already compiled, or 00212 * an error occurs while compiling the query. 00213 */ 00214 virtual void 00215 compile(std::istream& aQuery, const StaticContext_t& aStaticContext, 00216 const Zorba_CompilerHints_t& aHints) = 0; 00217 00218 /** \brief Set the filename of a query. 00219 * 00220 * This (after URI-encoding) becomes the encapsulating entity's 00221 * retrieval URI (in RFC 3986 terms). 00222 */ 00223 virtual void 00224 setFileName( const String& ) = 0; 00225 00226 /** \brief Register a SAX2_ContentHandler for retrieving the serialized 00227 * query result as SAX events when executeSAX() is called. 00228 * 00229 * @param aContentHandler the content handler on which SAX callbacks are called. 00230 */ 00231 virtual void 00232 registerSAXHandler( SAX2_ContentHandler* aContentHandler ) = 0; 00233 00234 /** \brief Register a SAX2_ContentHandler for retrieving the serialized 00235 * query result as SAX. 00236 * 00237 * @param aContentHandler the content handler on which SAX callbacks are called. 00238 */ 00239 virtual void 00240 executeSAX( SAX2_ContentHandler* aContentHandler) = 0; 00241 00242 /** \brief Serialize the query result as SAX events and call the callbacks 00243 * of the SAX2_ContentHandler that has been set using registerSAXHandler. 00244 * 00245 * @throw ZorbaException if an error occurs (e.g. no SAX2_ContentHandler has been registered). 00246 */ 00247 virtual void 00248 executeSAX() = 0; 00249 00250 /** \brief Close the query and release all aquired ressources. 00251 * 00252 * While a query is compiled and/or active, it holds on to a couple of ressources. 00253 * Before Zorba can be safely shutdown, all ressources must be released. 00254 * For queries this can be done by calling close. However, if close is not 00255 * called explicitly, it will be automatically called by the XQuery objects 00256 * destructor, i.e. if the last smart pointer holding this XQuery object is destroyed. 00257 * 00258 * Note that calling close explicitly is usually not necessary. 00259 * 00260 * @throw SystemException if the XQuery object has already been closed. 00261 */ 00262 virtual void 00263 close() = 0; 00264 00265 /** \brief Clone this query object for executing it in another thread. 00266 * 00267 * A query object is not thread safe, i.e. it can't be executed in several 00268 * threads. In order to execute it in another thread it needs to be cloned 00269 * with this function. However, please note that if an ErrorHandler has 00270 * been provided by the user (see registerErrorHandler()), this ErrorHandler 00271 * will also be used in the cloned query, and as a result, the user should 00272 * provide a thread-safe ErrorHandler. Alternatively, a new ErrorHandler 00273 * can be registered in the cloned query by using registerErrorHandler 00274 * again. Or, the cloned query can be reset to use the default ErrorHandler 00275 * (which just throws exceptions) by calling resetErrorHandler. 00276 * 00277 * This function also clones the StaticContext and DynamicContext of the 00278 * XQuery object. In the DynamicContext of the cloned query different 00279 * variable values can be used, e.g. set different external variable 00280 * values. For an example of cloning a query and setting different values 00281 * in the dynamic context see example_10 in file \link simple.cpp \endlink. 00282 * 00283 * @return The cloned XQuery object. 00284 * @throw SystemException if the query has not been compiled or is closed. 00285 */ 00286 virtual XQuery_t 00287 clone() const = 0; 00288 00289 }; 00290 00291 // xml serialization of the query (equiv to calling serialize(os) 00292 ZORBA_EXTERN_DECL 00293 std::ostream& operator<< (std::ostream& os, const XQuery_t& aQuery); 00294 ZORBA_EXTERN_DECL 00295 std::ostream& operator<< (std::ostream& os, XQuery* aQuery); 00296 00297 00298 } /* namespace zorba */ 00299 00300 #endif