1  /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef _CONNECTIVITY_EVOAB_STATEMENT_HXX_
29 #define _CONNECTIVITY_EVOAB_STATEMENT_HXX_
30 
31 #include <com/sun/star/sdbc/XStatement.hpp>
32 #include <com/sun/star/sdbc/XWarningsSupplier.hpp>
33 #include <com/sun/star/sdbc/XMultipleResults.hpp>
34 #include <com/sun/star/sdbc/XCloseable.hpp>
35 #include <com/sun/star/sdbc/SQLWarning.hpp>
36 #include <comphelper/proparrhlp.hxx>
37 #include <cppuhelper/compbase2.hxx>
38 #include <comphelper/uno3.hxx>
39 #include "connectivity/CommonTools.hxx"
40 #include <com/sun/star/lang/XServiceInfo.hpp>
41 #include <comphelper/broadcasthelper.hxx>
42 #include "connectivity/sqliterator.hxx"
43 #include "connectivity/sqlparse.hxx"
44 #include <connectivity/FValue.hxx>
45 #include "OSubComponent.hxx"
46 #include <com/sun/star/util/XCancellable.hpp>
47 #include <cppuhelper/compbase5.hxx>
48 #include <comphelper/propertycontainer.hxx>
49 #include <com/sun/star/lang/XServiceInfo.hpp>
50 
51 #include "EApi.h"
52 
53 #include <list>
54 
55 namespace connectivity
56 {
57     namespace evoab
58     {
59         class OEvoabResultSet;
60         class OEvoabConnection;
61         typedef ::cppu::WeakComponentImplHelper2    <   ::com::sun::star::sdbc::XWarningsSupplier
62                                                     ,   ::com::sun::star::sdbc::XCloseable
63                                                     >   OCommonStatement_IBase;
64 
65         struct FieldSort
66         {
67             sal_Int32       nField;
68             bool            bAscending;
69 
70             FieldSort() : nField(0), bAscending( true ) { }
71             FieldSort( const sal_Int32 _nField, const bool _bAscending ) : nField( _nField ), bAscending( _bAscending ) { }
72         };
73         typedef ::std::vector< FieldSort >  SortDescriptor;
74 
75         enum QueryFilterType
76         {
77             eFilterAlwaysFalse,
78             eFilterNone,
79             eFilterOther
80         };
81 
82         struct QueryData
83         {
84         private:
85             EBookQuery*     pQuery;
86 
87         public:
88             ::rtl::OUString                             sTable;
89             QueryFilterType                             eFilterType;
90             ::vos::ORef< ::connectivity::OSQLColumns >  xSelectColumns;
91             SortDescriptor                              aSortOrder;
92 
93             QueryData()
94                 :pQuery( NULL )
95                 ,sTable()
96                 ,eFilterType( eFilterOther )
97                 ,xSelectColumns()
98                 ,aSortOrder()
99             {
100             }
101 
102             QueryData( const QueryData& _rhs )
103                 :pQuery( NULL )
104                 ,sTable()
105                 ,eFilterType( eFilterType )
106                 ,xSelectColumns()
107                 ,aSortOrder()
108             {
109                 *this = _rhs;
110             }
111 
112             QueryData& operator=( const QueryData& _rhs )
113             {
114                 if ( this == &_rhs )
115                     return *this;
116 
117                 setQuery( _rhs.pQuery );
118                 sTable = _rhs.sTable;
119                 eFilterType = _rhs.eFilterType;
120                 xSelectColumns = _rhs.xSelectColumns;
121                 aSortOrder = _rhs.aSortOrder;
122 
123                 return *this;
124             }
125 
126             ~QueryData()
127             {
128                 setQuery( NULL );
129             }
130 
131             EBookQuery* getQuery() const { return pQuery; }
132 
133             void setQuery( EBookQuery* _pQuery )
134             {
135                 if ( pQuery )
136 	                e_book_query_unref( pQuery );
137                 pQuery = _pQuery;
138                 if ( pQuery )
139 	                e_book_query_ref( pQuery );
140             }
141         };
142 
143         //**************************************************************
144         //************ Class: OCommonStatement
145         // is a base class for the normal statement and for the prepared statement
146         //**************************************************************
147         class OCommonStatement;
148         typedef OSubComponent< OCommonStatement, OCommonStatement_IBase >   OStatement_CBase;
149 
150         class OCommonStatement  :public comphelper::OBaseMutex
151                                 ,public OCommonStatement_IBase
152                                 ,public ::comphelper::OPropertyContainer
153                                 ,public ::comphelper::OPropertyArrayUsageHelper< OCommonStatement >
154                                 ,public OStatement_CBase
155         {
156             friend class OSubComponent< OCommonStatement, OCommonStatement_IBase >;
157 
158         private:
159             ::com::sun::star::uno::WeakReference< ::com::sun::star::sdbc::XResultSet>    m_xResultSet;   // The last ResultSet created
160             OEvoabResultSet                      *m_pResultSet;
161             OEvoabConnection                     *m_pConnection;
162             connectivity::OSQLParser			  m_aParser;
163             connectivity::OSQLParseTreeIterator   m_aSQLIterator;
164             connectivity::OSQLParseNode          *m_pParseTree;
165 
166             // <properties>
167             ::rtl::OUString								m_aCursorName;
168             sal_Int32									m_nMaxFieldSize;
169             sal_Int32									m_nMaxRows;
170             sal_Int32									m_nQueryTimeOut;
171             sal_Int32									m_nFetchSize;
172             sal_Int32									m_nResultSetType;
173             sal_Int32									m_nFetchDirection;
174             sal_Int32									m_nResultSetConcurrency;
175             sal_Bool									m_bEscapeProcessing;
176             // </properties>
177 
178             ::cppu::OBroadcastHelper& rBHelper;
179 
180         protected:
181 
182             void disposeResultSet();
183 
184             // OPropertyArrayUsageHelper
185             virtual ::cppu::IPropertyArrayHelper* createArrayHelper() const;
186             // OPropertySetHelper
187             virtual ::cppu::IPropertyArrayHelper & SAL_CALL getInfoHelper();
188 
189             virtual ~OCommonStatement();
190 
191         protected:
192             void         reset () throw( ::com::sun::star::sdbc::SQLException);
193             void         clearMyResultSet () throw( ::com::sun::star::sdbc::SQLException);
194             void         parseSql( const ::rtl::OUString& sql, QueryData& _out_rQueryData );
195             EBookQuery  *whereAnalysis( const OSQLParseNode*  parseTree );
196             void         orderByAnalysis( const OSQLParseNode* _pOrderByClause, SortDescriptor& _out_rSort );
197             rtl::OUString getTableName();
198             EBookQuery  *createTrue();
199             EBookQuery  *createTest( const ::rtl::OUString &aColumnName,
200                                      EBookQueryTest eTest,
201                                      const ::rtl::OUString &aMatch );
202 
203         public:
204 
205             // other methods
206             OEvoabConnection* getOwnConnection() const { return m_pConnection;}
207 
208             using OCommonStatement_IBase::operator ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >;
209 
210         protected:
211             OCommonStatement( OEvoabConnection* _pConnection );
212 
213             // OComponentHelper
214             virtual void SAL_CALL disposing(void);
215             // XInterface
216             virtual void SAL_CALL release() throw();
217             virtual void SAL_CALL acquire() throw();
218             // XInterface
219             virtual ::com::sun::star::uno::Any SAL_CALL queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException);
220             //XTypeProvider
221             virtual ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type > SAL_CALL getTypes(  ) throw(::com::sun::star::uno::RuntimeException);
222 
223             // XPropertySet
224             virtual ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo(  ) throw(::com::sun::star::uno::RuntimeException);
225 
226             // XWarningsSupplier
227             virtual ::com::sun::star::uno::Any SAL_CALL getWarnings(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
228             virtual void SAL_CALL clearWarnings(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
229 
230             // XCloseable
231             virtual void SAL_CALL close(  ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
232 
233         protected:
234             /** will return the EBookQuery representing the stamement's WHERE condition, or throw
235 
236                 Also, all statement dependent members (such as the parser/iterator) will be inited afterwards.
237             */
238             QueryData
239                 impl_getEBookQuery_throw( const ::rtl::OUString& _rSql );
240 
241             ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet >
242                 impl_executeQuery_throw( const ::rtl::OUString& _rSql );
243 
244             ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet >
245                 impl_executeQuery_throw( const QueryData& _rData );
246 
247             ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >
248                 impl_getConnection() { return ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >( (::com::sun::star::sdbc::XConnection*)m_pConnection ); }
249 
250             ::rtl::OUString
251                 impl_getColumnRefColumnName_throw( const ::connectivity::OSQLParseNode& _rColumnRef );
252         };
253 
254         typedef ::cppu::ImplHelper2 <   ::com::sun::star::lang::XServiceInfo
255                                     ,   ::com::sun::star::sdbc::XStatement
256                                     >   OStatement_IBase;
257         class OStatement    :public OCommonStatement
258                             ,public OStatement_IBase
259         {
260         protected:
261             virtual ~OStatement(){}
262 
263         public:
264             OStatement( OEvoabConnection* _pConnection)
265                 :OCommonStatement( _pConnection)
266             {
267             }
268 
269             // XInterface
270             virtual ::com::sun::star::uno::Any SAL_CALL queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException);
271             virtual void SAL_CALL acquire() throw();
272             virtual void SAL_CALL release() throw();
273 
274             // XTypeProvider
275             DECLARE_XTYPEPROVIDER()
276 
277             // XServiceInfo
278             DECLARE_SERVICE_INFO();
279 
280             // XStatement
281             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) ;
282             virtual sal_Int32 SAL_CALL executeUpdate( const ::rtl::OUString& sql ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) ;
283             virtual sal_Bool SAL_CALL execute( const ::rtl::OUString& sql ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException) ;
284             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) ;
285         };
286     }
287 }
288 #endif // CONNECTIVITY_SSTATEMENT_HXX
289