bump product version to 5.0.4.1
[LibreOffice.git] / forms / source / component / cachedrowset.cxx
blobf8f891129939bc598704b68d47d0d956ef6e0b2f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "cachedrowset.hxx"
22 #include "services.hxx"
23 #include "frm_strings.hxx"
25 #include <com/sun/star/lang/XComponent.hpp>
26 #include <com/sun/star/beans/XPropertySet.hpp>
27 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
28 #include <com/sun/star/sdbc/ResultSetType.hpp>
30 #include <tools/diagnose_ex.h>
33 namespace frm
37 using ::com::sun::star::uno::Reference;
38 using ::com::sun::star::uno::UNO_QUERY;
39 using ::com::sun::star::uno::UNO_QUERY_THROW;
40 using ::com::sun::star::uno::UNO_SET_THROW;
41 using ::com::sun::star::uno::Exception;
42 using ::com::sun::star::uno::RuntimeException;
43 using ::com::sun::star::uno::XComponentContext;
44 using ::com::sun::star::sdbc::XConnection;
45 using ::com::sun::star::lang::XComponent;
46 using ::com::sun::star::beans::XPropertySet;
47 using ::com::sun::star::uno::makeAny;
48 using ::com::sun::star::sdbc::SQLException;
49 using ::com::sun::star::uno::Any;
50 using ::com::sun::star::sdb::XQueriesSupplier;
51 using ::com::sun::star::container::XNameAccess;
52 using ::com::sun::star::sdbc::XResultSet;
53 using ::com::sun::star::sdbc::XStatement;
55 namespace ResultSetType = ::com::sun::star::sdbc::ResultSetType;
57 struct CachedRowSet_Data
59 OUString sCommand;
60 bool bEscapeProcessing;
61 Reference< XConnection > xConnection;
63 bool bStatementDirty;
65 CachedRowSet_Data()
66 :sCommand()
67 ,bEscapeProcessing( false )
68 ,xConnection()
69 ,bStatementDirty( true )
74 CachedRowSet::CachedRowSet()
75 :m_pData( new CachedRowSet_Data )
80 CachedRowSet::~CachedRowSet()
82 dispose();
86 void CachedRowSet::setCommand( const OUString& _rCommand )
88 if ( m_pData->sCommand == _rCommand )
89 return;
91 m_pData->sCommand = _rCommand;
92 m_pData->bStatementDirty = true;
96 void CachedRowSet::setCommandFromQuery( const OUString& _rQueryName )
98 Reference< XQueriesSupplier > xSupplyQueries( m_pData->xConnection, UNO_QUERY_THROW );
99 Reference< XNameAccess > xQueries ( xSupplyQueries->getQueries(), UNO_QUERY_THROW );
100 Reference< XPropertySet > xQuery ( xQueries->getByName( _rQueryName ), UNO_QUERY_THROW );
102 bool bEscapeProcessing( false );
103 OSL_VERIFY( xQuery->getPropertyValue( PROPERTY_ESCAPE_PROCESSING ) >>= bEscapeProcessing );
104 setEscapeProcessing( bEscapeProcessing );
106 OUString sCommand;
107 OSL_VERIFY( xQuery->getPropertyValue( PROPERTY_COMMAND ) >>= sCommand );
108 setCommand( sCommand );
112 void CachedRowSet::setEscapeProcessing ( const bool _bEscapeProcessing )
114 if ( m_pData->bEscapeProcessing == _bEscapeProcessing )
115 return;
117 m_pData->bEscapeProcessing = _bEscapeProcessing;
118 m_pData->bStatementDirty = true;
122 void CachedRowSet::setConnection( const Reference< XConnection >& _rxConnection )
124 if ( m_pData->xConnection == _rxConnection )
125 return;
127 m_pData->xConnection = _rxConnection;
128 m_pData->bStatementDirty = true;
132 Reference< XResultSet > CachedRowSet::execute()
134 Reference< XResultSet > xResult;
137 OSL_PRECOND( m_pData->xConnection.is(), "CachedRowSet::execute: how am I expected to do this without a connection?" );
138 if ( !m_pData->xConnection.is() )
139 return xResult;
141 Reference< XStatement > xStatement( m_pData->xConnection->createStatement(), UNO_SET_THROW );
142 Reference< XPropertySet > xStatementProps( xStatement, UNO_QUERY_THROW );
143 xStatementProps->setPropertyValue( PROPERTY_ESCAPE_PROCESSING, makeAny( m_pData->bEscapeProcessing ) );
144 xStatementProps->setPropertyValue( PROPERTY_RESULTSET_TYPE, makeAny( ResultSetType::FORWARD_ONLY ) );
146 xResult.set( xStatement->executeQuery( m_pData->sCommand ), UNO_SET_THROW );
147 m_pData->bStatementDirty = false;
149 catch( const SQLException& )
151 throw;
153 catch( const Exception& )
155 DBG_UNHANDLED_EXCEPTION();
157 return xResult;
161 bool CachedRowSet::isDirty() const
163 return m_pData->bStatementDirty;
167 void CachedRowSet::dispose()
171 m_pData.reset( new CachedRowSet_Data );
173 catch( const Exception& )
175 DBG_UNHANDLED_EXCEPTION();
180 } // namespace frm
183 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */