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