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