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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_forms.hxx"
26 
27 #include "cachedrowset.hxx"
28 #include "services.hxx"
29 #include "frm_strings.hxx"
30 
31 /** === begin UNO includes === **/
32 #include <com/sun/star/lang/XComponent.hpp>
33 #include <com/sun/star/beans/XPropertySet.hpp>
34 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
35 #include <com/sun/star/sdbc/ResultSetType.hpp>
36 /** === end UNO includes === **/
37 
38 #include <tools/diagnose_ex.h>
39 
40 //........................................................................
41 namespace frm
42 {
43 //........................................................................
44 
45 	/** === begin UNO using === **/
46 	using ::com::sun::star::uno::Reference;
47 	using ::com::sun::star::uno::UNO_QUERY;
48 	using ::com::sun::star::uno::UNO_QUERY_THROW;
49 	using ::com::sun::star::uno::UNO_SET_THROW;
50 	using ::com::sun::star::uno::Exception;
51 	using ::com::sun::star::uno::RuntimeException;
52     using ::com::sun::star::sdbc::XConnection;
53     using ::com::sun::star::lang::XComponent;
54     using ::com::sun::star::beans::XPropertySet;
55     using ::com::sun::star::uno::makeAny;
56     using ::com::sun::star::sdbc::SQLException;
57     using ::com::sun::star::uno::Any;
58     using ::com::sun::star::sdb::XQueriesSupplier;
59     using ::com::sun::star::container::XNameAccess;
60     using ::com::sun::star::sdbc::XResultSet;
61     using ::com::sun::star::sdbc::XStatement;
62 	/** === end UNO using === **/
63     namespace ResultSetType = ::com::sun::star::sdbc::ResultSetType;
64 
65 	//====================================================================
66 	//= CachedRowSet_Data
67 	//====================================================================
68     struct CachedRowSet_Data
69     {
70         ::comphelper::ComponentContext  aContext;
71         ::rtl::OUString                 sCommand;
72         sal_Bool                        bEscapeProcessing;
73         Reference< XConnection >        xConnection;
74 
75         bool                            bStatementDirty;
76 
CachedRowSet_Datafrm::CachedRowSet_Data77         CachedRowSet_Data( const ::comphelper::ComponentContext& _rContext )
78             :aContext( _rContext )
79             ,sCommand()
80             ,bEscapeProcessing( sal_False )
81             ,xConnection()
82             ,bStatementDirty( true )
83         {
84         }
85     };
86 
87 	//====================================================================
88 	//= CachedRowSet
89 	//====================================================================
90 	//--------------------------------------------------------------------
CachedRowSet(const::comphelper::ComponentContext & _rContext)91     CachedRowSet::CachedRowSet( const ::comphelper::ComponentContext& _rContext )
92         :m_pData( new CachedRowSet_Data( _rContext ) )
93     {
94     }
95 
96 	//--------------------------------------------------------------------
~CachedRowSet()97     CachedRowSet::~CachedRowSet()
98     {
99         dispose();
100     }
101 
102 	//--------------------------------------------------------------------
setCommand(const::rtl::OUString & _rCommand)103     void CachedRowSet::setCommand( const ::rtl::OUString& _rCommand )
104     {
105         if ( m_pData->sCommand == _rCommand )
106             return;
107 
108         m_pData->sCommand = _rCommand;
109         m_pData->bStatementDirty = true;
110     }
111 
112 	//--------------------------------------------------------------------
setCommandFromQuery(const::rtl::OUString & _rQueryName)113     void CachedRowSet::setCommandFromQuery( const ::rtl::OUString& _rQueryName )
114     {
115         Reference< XQueriesSupplier > xSupplyQueries( m_pData->xConnection, UNO_QUERY_THROW );
116         Reference< XNameAccess >      xQueries      ( xSupplyQueries->getQueries(), UNO_QUERY_THROW );
117         Reference< XPropertySet >     xQuery        ( xQueries->getByName( _rQueryName ), UNO_QUERY_THROW );
118 
119         sal_Bool bEscapeProcessing( sal_False );
120         OSL_VERIFY( xQuery->getPropertyValue( PROPERTY_ESCAPE_PROCESSING ) >>= bEscapeProcessing );
121         setEscapeProcessing( bEscapeProcessing );
122 
123         ::rtl::OUString sCommand;
124         OSL_VERIFY( xQuery->getPropertyValue( PROPERTY_COMMAND ) >>= sCommand );
125         setCommand( sCommand );
126     }
127 
128 	//--------------------------------------------------------------------
setEscapeProcessing(const sal_Bool _bEscapeProcessing)129     void CachedRowSet::setEscapeProcessing ( const sal_Bool _bEscapeProcessing )
130     {
131         if ( m_pData->bEscapeProcessing == _bEscapeProcessing )
132             return;
133 
134         m_pData->bEscapeProcessing = _bEscapeProcessing;
135         m_pData->bStatementDirty = true;
136     }
137 
138 	//--------------------------------------------------------------------
setConnection(const Reference<XConnection> & _rxConnection)139     void CachedRowSet::setConnection( const Reference< XConnection >& _rxConnection )
140     {
141         if ( m_pData->xConnection == _rxConnection )
142             return;
143 
144         m_pData->xConnection = _rxConnection;
145         m_pData->bStatementDirty = true;
146     }
147 
148 	//--------------------------------------------------------------------
execute()149     Reference< XResultSet > CachedRowSet::execute()
150     {
151         Reference< XResultSet > xResult;
152         try
153         {
154             OSL_PRECOND( m_pData->xConnection.is(), "CachedRowSet::execute: how am I expected to do this without a connection?" );
155             if ( !m_pData->xConnection.is() )
156                 return xResult;
157 
158             Reference< XStatement > xStatement( m_pData->xConnection->createStatement(), UNO_SET_THROW );
159             Reference< XPropertySet > xStatementProps( xStatement, UNO_QUERY_THROW );
160             xStatementProps->setPropertyValue( PROPERTY_ESCAPE_PROCESSING, makeAny( m_pData->bEscapeProcessing ) );
161             xStatementProps->setPropertyValue( PROPERTY_RESULTSET_TYPE, makeAny( ResultSetType::FORWARD_ONLY ) );
162 
163             xResult.set( xStatement->executeQuery( m_pData->sCommand ), UNO_SET_THROW );
164             m_pData->bStatementDirty = false;
165         }
166         catch( const SQLException& )
167         {
168             throw;
169         }
170         catch( const Exception& )
171         {
172         	DBG_UNHANDLED_EXCEPTION();
173         }
174         return xResult;
175     }
176 
177 	//--------------------------------------------------------------------
isDirty() const178     bool CachedRowSet::isDirty() const
179     {
180         return m_pData->bStatementDirty;
181     }
182 
183 	//--------------------------------------------------------------------
dispose()184     void CachedRowSet::dispose()
185     {
186         try
187         {
188             m_pData.reset( new CachedRowSet_Data( m_pData->aContext ) );
189         }
190         catch( const Exception& )
191         {
192         	DBG_UNHANDLED_EXCEPTION();
193         }
194     }
195 
196 //........................................................................
197 } // namespace frm
198 //........................................................................
199