1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _CONNECTIVITY_EVOAB_STATEMENT_HXX_
25 #define _CONNECTIVITY_EVOAB_STATEMENT_HXX_
26 
27 #include <com/sun/star/sdbc/XStatement.hpp>
28 #include <com/sun/star/sdbc/XWarningsSupplier.hpp>
29 #include <com/sun/star/sdbc/XMultipleResults.hpp>
30 #include <com/sun/star/sdbc/XCloseable.hpp>
31 #include <com/sun/star/sdbc/SQLWarning.hpp>
32 #include <comphelper/proparrhlp.hxx>
33 #include <cppuhelper/compbase2.hxx>
34 #include <comphelper/uno3.hxx>
35 #include "connectivity/CommonTools.hxx"
36 #include <com/sun/star/lang/XServiceInfo.hpp>
37 #include <comphelper/broadcasthelper.hxx>
38 #include "connectivity/sqliterator.hxx"
39 #include "connectivity/sqlparse.hxx"
40 #include <connectivity/FValue.hxx>
41 #include "OSubComponent.hxx"
42 #include <com/sun/star/util/XCancellable.hpp>
43 #include <cppuhelper/compbase5.hxx>
44 #include <comphelper/propertycontainer.hxx>
45 #include <com/sun/star/lang/XServiceInfo.hpp>
46 
47 #include "EApi.h"
48 
49 #include <list>
50 
51 namespace connectivity
52 {
53     namespace evoab
54     {
55         class OEvoabResultSet;
56         class OEvoabConnection;
57         typedef ::cppu::WeakComponentImplHelper2    <   ::com::sun::star::sdbc::XWarningsSupplier
58                                                     ,   ::com::sun::star::sdbc::XCloseable
59                                                     >   OCommonStatement_IBase;
60 
61         struct FieldSort
62         {
63             sal_Int32       nField;
64             bool            bAscending;
65 
FieldSortconnectivity::evoab::FieldSort66             FieldSort() : nField(0), bAscending( true ) { }
FieldSortconnectivity::evoab::FieldSort67             FieldSort( const sal_Int32 _nField, const bool _bAscending ) : nField( _nField ), bAscending( _bAscending ) { }
68         };
69         typedef ::std::vector< FieldSort >  SortDescriptor;
70 
71         enum QueryFilterType
72         {
73             eFilterAlwaysFalse,
74             eFilterNone,
75             eFilterOther
76         };
77 
78         struct QueryData
79         {
80         private:
81             EBookQuery*     pQuery;
82 
83         public:
84             ::rtl::OUString                             sTable;
85             QueryFilterType                             eFilterType;
86             ::vos::ORef< ::connectivity::OSQLColumns >  xSelectColumns;
87             SortDescriptor                              aSortOrder;
88 
QueryDataconnectivity::evoab::QueryData89             QueryData()
90                 :pQuery( NULL )
91                 ,sTable()
92                 ,eFilterType( eFilterOther )
93                 ,xSelectColumns()
94                 ,aSortOrder()
95             {
96             }
97 
QueryDataconnectivity::evoab::QueryData98             QueryData( const QueryData& _rhs )
99                 :pQuery( NULL )
100                 ,sTable()
101                 ,eFilterType( eFilterType )
102                 ,xSelectColumns()
103                 ,aSortOrder()
104             {
105                 *this = _rhs;
106             }
107 
operator =connectivity::evoab::QueryData108             QueryData& operator=( const QueryData& _rhs )
109             {
110                 if ( this == &_rhs )
111                     return *this;
112 
113                 setQuery( _rhs.pQuery );
114                 sTable = _rhs.sTable;
115                 eFilterType = _rhs.eFilterType;
116                 xSelectColumns = _rhs.xSelectColumns;
117                 aSortOrder = _rhs.aSortOrder;
118 
119                 return *this;
120             }
121 
~QueryDataconnectivity::evoab::QueryData122             ~QueryData()
123             {
124                 setQuery( NULL );
125             }
126 
getQueryconnectivity::evoab::QueryData127             EBookQuery* getQuery() const { return pQuery; }
128 
setQueryconnectivity::evoab::QueryData129             void setQuery( EBookQuery* _pQuery )
130             {
131                 if ( pQuery )
132 	                e_book_query_unref( pQuery );
133                 pQuery = _pQuery;
134                 if ( pQuery )
135 	                e_book_query_ref( pQuery );
136             }
137         };
138 
139         //**************************************************************
140         //************ Class: OCommonStatement
141         // is a base class for the normal statement and for the prepared statement
142         //**************************************************************
143         class OCommonStatement;
144         typedef OSubComponent< OCommonStatement, OCommonStatement_IBase >   OStatement_CBase;
145 
146         class OCommonStatement  :public comphelper::OBaseMutex
147                                 ,public OCommonStatement_IBase
148                                 ,public ::comphelper::OPropertyContainer
149                                 ,public ::comphelper::OPropertyArrayUsageHelper< OCommonStatement >
150                                 ,public OStatement_CBase
151         {
152             friend class OSubComponent< OCommonStatement, OCommonStatement_IBase >;
153 
154         private:
155             ::com::sun::star::uno::WeakReference< ::com::sun::star::sdbc::XResultSet>    m_xResultSet;   // The last ResultSet created
156             OEvoabResultSet                      *m_pResultSet;
157             OEvoabConnection                     *m_pConnection;
158             connectivity::OSQLParser			  m_aParser;
159             connectivity::OSQLParseTreeIterator   m_aSQLIterator;
160             connectivity::OSQLParseNode          *m_pParseTree;
161 
162             // <properties>
163             ::rtl::OUString								m_aCursorName;
164             sal_Int32									m_nMaxFieldSize;
165             sal_Int32									m_nMaxRows;
166             sal_Int32									m_nQueryTimeOut;
167             sal_Int32									m_nFetchSize;
168             sal_Int32									m_nResultSetType;
169             sal_Int32									m_nFetchDirection;
170             sal_Int32									m_nResultSetConcurrency;
171             sal_Bool									m_bEscapeProcessing;
172             // </properties>
173 
174             ::cppu::OBroadcastHelper& rBHelper;
175 
176         protected:
177 
178             void disposeResultSet();
179 
180             // OPropertyArrayUsageHelper
181             virtual ::cppu::IPropertyArrayHelper* createArrayHelper() const;
182             // OPropertySetHelper
183             virtual ::cppu::IPropertyArrayHelper & SAL_CALL getInfoHelper();
184 
185             virtual ~OCommonStatement();
186 
187         protected:
188             void         reset () throw( ::com::sun::star::sdbc::SQLException);
189             void         clearMyResultSet () throw( ::com::sun::star::sdbc::SQLException);
190             void         parseSql( const ::rtl::OUString& sql, QueryData& _out_rQueryData );
191             EBookQuery  *whereAnalysis( const OSQLParseNode*  parseTree );
192             void         orderByAnalysis( const OSQLParseNode* _pOrderByClause, SortDescriptor& _out_rSort );
193             rtl::OUString getTableName();
194             EBookQuery  *createTrue();
195             EBookQuery  *createTest( const ::rtl::OUString &aColumnName,
196                                      EBookQueryTest eTest,
197                                      const ::rtl::OUString &aMatch );
198 
199         public:
200 
201             // other methods
getOwnConnection() const202             OEvoabConnection* getOwnConnection() const { return m_pConnection;}
203 
204             using OCommonStatement_IBase::operator ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >;
205 
206         protected:
207             OCommonStatement( OEvoabConnection* _pConnection );
208 
209             // OComponentHelper
210             virtual void SAL_CALL disposing(void);
211             // XInterface
212             virtual void SAL_CALL release() throw();
213             virtual void SAL_CALL acquire() throw();
214             // XInterface
215             virtual ::com::sun::star::uno::Any SAL_CALL queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException);
216             //XTypeProvider
217             virtual ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type > SAL_CALL getTypes(  ) throw(::com::sun::star::uno::RuntimeException);
218 
219             // XPropertySet
220             virtual ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo(  ) throw(::com::sun::star::uno::RuntimeException);
221 
222             // XWarningsSupplier
223             virtual ::com::sun::star::uno::Any SAL_CALL getWarnings(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
224             virtual void SAL_CALL clearWarnings(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
225 
226             // XCloseable
227             virtual void SAL_CALL close(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
228 
229         protected:
230             /** will return the EBookQuery representing the stamement's WHERE condition, or throw
231 
232                 Also, all statement dependent members (such as the parser/iterator) will be inited afterwards.
233             */
234             QueryData
235                 impl_getEBookQuery_throw( const ::rtl::OUString& _rSql );
236 
237             ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet >
238                 impl_executeQuery_throw( const ::rtl::OUString& _rSql );
239 
240             ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet >
241                 impl_executeQuery_throw( const QueryData& _rData );
242 
243             ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >
impl_getConnection()244                 impl_getConnection() { return ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >( (::com::sun::star::sdbc::XConnection*)m_pConnection ); }
245 
246             ::rtl::OUString
247                 impl_getColumnRefColumnName_throw( const ::connectivity::OSQLParseNode& _rColumnRef );
248         };
249 
250         typedef ::cppu::ImplHelper2 <   ::com::sun::star::lang::XServiceInfo
251                                     ,   ::com::sun::star::sdbc::XStatement
252                                     >   OStatement_IBase;
253         class OStatement    :public OCommonStatement
254                             ,public OStatement_IBase
255         {
256         protected:
~OStatement()257             virtual ~OStatement(){}
258 
259         public:
OStatement(OEvoabConnection * _pConnection)260             OStatement( OEvoabConnection* _pConnection)
261                 :OCommonStatement( _pConnection)
262             {
263             }
264 
265             // XInterface
266             virtual ::com::sun::star::uno::Any SAL_CALL queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException);
267             virtual void SAL_CALL acquire() throw();
268             virtual void SAL_CALL release() throw();
269 
270             // XTypeProvider
271             DECLARE_XTYPEPROVIDER()
272 
273             // XServiceInfo
274             DECLARE_SERVICE_INFO();
275 
276             // XStatement
277             virtual ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet > SAL_CALL executeQuery( const ::rtl::OUString& sql ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) ;
278             virtual sal_Int32 SAL_CALL executeUpdate( const ::rtl::OUString& sql ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) ;
279             virtual sal_Bool SAL_CALL execute( const ::rtl::OUString& sql ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) ;
280             virtual ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection > SAL_CALL getConnection(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) ;
281         };
282     }
283 }
284 #endif // CONNECTIVITY_SSTATEMENT_HXX
285