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