Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / forms / source / component / cachedrowset.cxx
blobb62075165b234ab74bd985bf99d7ab4e70b402ef
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/beans/XPropertySet.hpp>
26 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
27 #include <com/sun/star/sdbc/ResultSetType.hpp>
29 #include <tools/diagnose_ex.h>
32 namespace frm
36 using ::com::sun::star::uno::Reference;
37 using ::com::sun::star::uno::UNO_QUERY_THROW;
38 using ::com::sun::star::uno::UNO_SET_THROW;
39 using ::com::sun::star::uno::Exception;
40 using ::com::sun::star::sdbc::XConnection;
41 using ::com::sun::star::beans::XPropertySet;
42 using ::com::sun::star::uno::makeAny;
43 using ::com::sun::star::sdbc::SQLException;
44 using ::com::sun::star::sdb::XQueriesSupplier;
45 using ::com::sun::star::container::XNameAccess;
46 using ::com::sun::star::sdbc::XResultSet;
47 using ::com::sun::star::sdbc::XStatement;
49 namespace ResultSetType = ::com::sun::star::sdbc::ResultSetType;
51 struct CachedRowSet_Data
53 OUString sCommand;
54 bool bEscapeProcessing;
55 Reference< XConnection > xConnection;
57 bool bStatementDirty;
59 CachedRowSet_Data()
60 :sCommand()
61 ,bEscapeProcessing( false )
62 ,xConnection()
63 ,bStatementDirty( true )
68 CachedRowSet::CachedRowSet()
69 :m_pData( new CachedRowSet_Data )
74 CachedRowSet::~CachedRowSet()
76 dispose();
80 void CachedRowSet::setCommand( const OUString& _rCommand )
82 if ( m_pData->sCommand == _rCommand )
83 return;
85 m_pData->sCommand = _rCommand;
86 m_pData->bStatementDirty = true;
90 void CachedRowSet::setCommandFromQuery( const OUString& _rQueryName )
92 Reference< XQueriesSupplier > xSupplyQueries( m_pData->xConnection, UNO_QUERY_THROW );
93 Reference< XNameAccess > xQueries ( xSupplyQueries->getQueries(), UNO_QUERY_THROW );
94 Reference< XPropertySet > xQuery ( xQueries->getByName( _rQueryName ), UNO_QUERY_THROW );
96 bool bEscapeProcessing( false );
97 OSL_VERIFY( xQuery->getPropertyValue( PROPERTY_ESCAPE_PROCESSING ) >>= bEscapeProcessing );
98 setEscapeProcessing( bEscapeProcessing );
100 OUString sCommand;
101 OSL_VERIFY( xQuery->getPropertyValue( PROPERTY_COMMAND ) >>= sCommand );
102 setCommand( sCommand );
106 void CachedRowSet::setEscapeProcessing ( const bool _bEscapeProcessing )
108 if ( m_pData->bEscapeProcessing == _bEscapeProcessing )
109 return;
111 m_pData->bEscapeProcessing = _bEscapeProcessing;
112 m_pData->bStatementDirty = true;
116 void CachedRowSet::setConnection( const Reference< XConnection >& _rxConnection )
118 if ( m_pData->xConnection == _rxConnection )
119 return;
121 m_pData->xConnection = _rxConnection;
122 m_pData->bStatementDirty = true;
126 Reference< XResultSet > CachedRowSet::execute()
128 Reference< XResultSet > xResult;
131 OSL_PRECOND( m_pData->xConnection.is(), "CachedRowSet::execute: how am I expected to do this without a connection?" );
132 if ( !m_pData->xConnection.is() )
133 return xResult;
135 Reference< XStatement > xStatement( m_pData->xConnection->createStatement(), UNO_SET_THROW );
136 Reference< XPropertySet > xStatementProps( xStatement, UNO_QUERY_THROW );
137 xStatementProps->setPropertyValue( PROPERTY_ESCAPE_PROCESSING, makeAny( m_pData->bEscapeProcessing ) );
138 xStatementProps->setPropertyValue( PROPERTY_RESULTSET_TYPE, makeAny( ResultSetType::FORWARD_ONLY ) );
140 xResult.set( xStatement->executeQuery( m_pData->sCommand ), UNO_SET_THROW );
141 m_pData->bStatementDirty = false;
143 catch( const SQLException& )
145 throw;
147 catch( const Exception& )
149 DBG_UNHANDLED_EXCEPTION();
151 return xResult;
155 bool CachedRowSet::isDirty() const
157 return m_pData->bStatementDirty;
161 void CachedRowSet::dispose()
165 m_pData.reset( new CachedRowSet_Data );
167 catch( const Exception& )
169 DBG_UNHANDLED_EXCEPTION();
174 } // namespace frm
177 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */