Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / svx / source / form / dbtoolsclient.cxx
blobda1ef3735d70175bef3fbdfedd3f37b6c7ced61d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include <com/sun/star/sdbc/XConnection.hpp>
30 #include <com/sun/star/sdbc/XDataSource.hpp>
31 #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
32 #include <com/sun/star/sdb/SQLContext.hpp>
33 #include "svx/dbtoolsclient.hxx"
34 #include <osl/diagnose.h>
35 #include <rtl/instance.hxx>
36 #include <connectivity/formattedcolumnvalue.hxx>
38 //........................................................................
39 namespace svxform
41 //........................................................................
43 using namespace ::connectivity::simple;
44 using namespace ::com::sun::star::sdbc;
45 using namespace ::com::sun::star::lang;
46 using namespace ::com::sun::star::util;
47 using namespace ::com::sun::star::uno;
48 using namespace ::com::sun::star::beans;
49 using namespace ::com::sun::star::sdb;
50 using namespace ::com::sun::star::container;
52 //====================================================================
53 //= ODbtoolsClient
54 //====================================================================
56 namespace
58 struct theODbtoolsClientMutex : public rtl::Static< osl::Mutex, theODbtoolsClientMutex> {};
61 sal_Int32 ODbtoolsClient::s_nClients = 0;
62 oslModule ODbtoolsClient::s_hDbtoolsModule = NULL;
63 createDataAccessToolsFactoryFunction
64 ODbtoolsClient::s_pFactoryCreationFunc = NULL;
66 //--------------------------------------------------------------------
67 ODbtoolsClient::ODbtoolsClient()
69 m_bCreateAlready = sal_False;
72 //--------------------------------------------------------------------
73 bool ODbtoolsClient::ensureLoaded() const
75 if ( !m_bCreateAlready )
77 m_bCreateAlready = true;
79 registerClient();
80 if ( s_pFactoryCreationFunc )
81 { // loading the lib succeeded
82 void* pUntypedFactory = (*s_pFactoryCreationFunc)();
83 IDataAccessToolsFactory* pDBTFactory = static_cast< IDataAccessToolsFactory* >( pUntypedFactory );
84 OSL_ENSURE( pDBTFactory, "ODbtoolsClient::ODbtoolsClient: no factory returned!" );
85 if ( pDBTFactory )
87 m_xDataAccessFactory = pDBTFactory;
88 // by definition, the factory was aquired once
89 m_xDataAccessFactory->release();
93 return m_xDataAccessFactory.is();
96 //--------------------------------------------------------------------
97 ODbtoolsClient::~ODbtoolsClient()
99 // clear the factory _before_ revoking the client
100 // (the revocation may unload the DBT lib)
101 m_xDataAccessFactory = NULL;
102 // revoke the client
103 if ( m_bCreateAlready )
104 revokeClient();
107 //--------------------------------------------------------------------
108 extern "C" { static void SAL_CALL thisModule() {} }
110 void ODbtoolsClient::registerClient()
112 ::osl::MutexGuard aGuard(theODbtoolsClientMutex::get());
113 if (1 == ++s_nClients)
115 OSL_ENSURE(NULL == s_hDbtoolsModule, "ODbtoolsClient::registerClient: inconsistence: already have a module!");
116 OSL_ENSURE(NULL == s_pFactoryCreationFunc, "ODbtoolsClient::registerClient: inconsistence: already have a factory function!");
118 const ::rtl::OUString sModuleName( SVLIBRARY( "dbtools" )
121 // load the dbtools library
122 s_hDbtoolsModule = osl_loadModuleRelative(
123 &thisModule, sModuleName.pData, 0);
124 OSL_ENSURE(NULL != s_hDbtoolsModule, "ODbtoolsClient::registerClient: could not load the dbtools library!");
125 if (NULL != s_hDbtoolsModule)
127 // get the symbol for the method creating the factory
128 const ::rtl::OUString sFactoryCreationFunc( "createDataAccessToolsFactory" );
129 // reinterpret_cast<createDataAccessToolsFactoryFunction>
130 s_pFactoryCreationFunc = (createDataAccessToolsFactoryFunction)(
131 osl_getFunctionSymbol(s_hDbtoolsModule, sFactoryCreationFunc.pData));
133 if (NULL == s_pFactoryCreationFunc)
134 { // did not find the symbol
135 OSL_FAIL("ODbtoolsClient::registerClient: could not find the symbol for creating the factory!");
136 osl_unloadModule(s_hDbtoolsModule);
137 s_hDbtoolsModule = NULL;
143 //--------------------------------------------------------------------
144 void ODbtoolsClient::revokeClient()
146 ::osl::MutexGuard aGuard(theODbtoolsClientMutex::get());
147 if (0 == --s_nClients)
149 s_pFactoryCreationFunc = NULL;
150 if (s_hDbtoolsModule)
151 osl_unloadModule(s_hDbtoolsModule);
152 s_hDbtoolsModule = NULL;
155 OSL_ENSURE(s_nClients >= 0,"Illegall call of revokeClient()");
158 //====================================================================
159 //= OStaticDataAccessTools
160 //====================================================================
161 //--------------------------------------------------------------------
162 OStaticDataAccessTools::OStaticDataAccessTools()
166 //--------------------------------------------------------------------
168 bool OStaticDataAccessTools::ensureLoaded() const
170 if ( !ODbtoolsClient::ensureLoaded() )
171 return false;
172 m_xDataAccessTools = getFactory()->getDataAccessTools();
173 return m_xDataAccessTools.is();
176 //--------------------------------------------------------------------
177 Reference< XNumberFormatsSupplier > OStaticDataAccessTools::getNumberFormats(const Reference< XConnection>& _rxConn, sal_Bool _bAllowDefault) const
179 Reference< XNumberFormatsSupplier > xReturn;
180 if ( ensureLoaded() )
181 xReturn = m_xDataAccessTools->getNumberFormats(_rxConn, _bAllowDefault);
182 return xReturn;
185 //--------------------------------------------------------------------
186 sal_Int32 OStaticDataAccessTools::getDefaultNumberFormat( const Reference< XPropertySet >& _xColumn, const Reference< XNumberFormatTypes >& _xTypes, const Locale& _rLocale )
188 sal_Int32 nReturn = 0;
189 if ( ensureLoaded() )
190 nReturn = m_xDataAccessTools->getDefaultNumberFormat( _xColumn, _xTypes, _rLocale );
191 return nReturn;
194 //--------------------------------------------------------------------
195 Reference< XConnection> OStaticDataAccessTools::getConnection_withFeedback(const ::rtl::OUString& _rDataSourceName,
196 const ::rtl::OUString& _rUser, const ::rtl::OUString& _rPwd, const Reference< XMultiServiceFactory>& _rxFactory) const
197 SAL_THROW ( (SQLException) )
199 Reference< XConnection > xReturn;
200 if ( ensureLoaded() )
201 xReturn = m_xDataAccessTools->getConnection_withFeedback(_rDataSourceName, _rUser, _rPwd, _rxFactory);
202 return xReturn;
205 //--------------------------------------------------------------------
206 Reference< XConnection > OStaticDataAccessTools::connectRowset( const Reference< XRowSet >& _rxRowSet,
207 const Reference< XMultiServiceFactory >& _rxFactory, sal_Bool _bSetAsActiveConnection ) const
208 SAL_THROW ( ( SQLException, WrappedTargetException, RuntimeException ) )
210 Reference< XConnection > xReturn;
211 if ( ensureLoaded() )
212 xReturn = m_xDataAccessTools->connectRowset( _rxRowSet, _rxFactory, _bSetAsActiveConnection );
213 return xReturn;
216 //--------------------------------------------------------------------
217 Reference< XConnection > OStaticDataAccessTools::getRowSetConnection(const Reference< XRowSet >& _rxRowSet) const SAL_THROW ( (RuntimeException) )
219 Reference< XConnection > xReturn;
220 if ( ensureLoaded() )
221 xReturn = m_xDataAccessTools->getRowSetConnection(_rxRowSet);
222 return xReturn;
225 //--------------------------------------------------------------------
226 void OStaticDataAccessTools::TransferFormComponentProperties(const Reference< XPropertySet>& _rxOld,
227 const Reference< XPropertySet>& _rxNew, const Locale& _rLocale) const
229 if ( ensureLoaded() )
230 m_xDataAccessTools->TransferFormComponentProperties(_rxOld, _rxNew, _rLocale);
233 //--------------------------------------------------------------------
234 ::rtl::OUString OStaticDataAccessTools::quoteName(const ::rtl::OUString& _rQuote, const ::rtl::OUString& _rName) const
236 ::rtl::OUString sReturn;
237 if ( ensureLoaded() )
238 sReturn = m_xDataAccessTools->quoteName(_rQuote, _rName);
239 return sReturn;
242 // ------------------------------------------------
243 ::rtl::OUString OStaticDataAccessTools::composeTableNameForSelect( const Reference< XConnection >& _rxConnection, const Reference< XPropertySet>& _xTable ) const
245 ::rtl::OUString sReturn;
246 if ( ensureLoaded() )
247 sReturn = m_xDataAccessTools->composeTableNameForSelect( _rxConnection, _xTable );
248 return sReturn;
251 //----------------------------------------------------------------
252 Reference< XDataSource > OStaticDataAccessTools::getDataSource( const ::rtl::OUString& _rsRegisteredName, const Reference< XMultiServiceFactory>& _rxFactory ) const
254 Reference< XDataSource > xReturn;
255 if ( ensureLoaded() )
256 xReturn = m_xDataAccessTools->getDataSource(_rsRegisteredName,_rxFactory);
257 return xReturn;
260 //----------------------------------------------------------------
261 sal_Bool OStaticDataAccessTools::canInsert(const Reference< XPropertySet>& _rxCursorSet) const
263 sal_Bool bRet = sal_False;
264 if ( ensureLoaded() )
265 bRet = m_xDataAccessTools->canInsert( _rxCursorSet );
266 return bRet;
269 //----------------------------------------------------------------
270 sal_Bool OStaticDataAccessTools::canUpdate(const Reference< XPropertySet>& _rxCursorSet) const
272 sal_Bool bRet = sal_False;
273 if ( ensureLoaded() )
274 bRet = m_xDataAccessTools->canUpdate( _rxCursorSet );
275 return bRet;
278 //----------------------------------------------------------------
279 Reference< XNameAccess > OStaticDataAccessTools::getFieldsByCommandDescriptor( const Reference< XConnection >& _rxConnection,
280 const sal_Int32 _nCommandType, const ::rtl::OUString& _rCommand,
281 Reference< XComponent >& _rxKeepFieldsAlive, ::dbtools::SQLExceptionInfo* _pErrorInfo ) SAL_THROW( ( ) )
283 Reference< XNameAccess > aFields;
284 if ( ensureLoaded() )
285 aFields = m_xDataAccessTools->getFieldsByCommandDescriptor( _rxConnection, _nCommandType,
286 _rCommand, _rxKeepFieldsAlive, _pErrorInfo );
288 return aFields;
291 //----------------------------------------------------------------
292 bool OStaticDataAccessTools::isEmbeddedInDatabase( const Reference< XInterface >& _rxComponent, Reference< XConnection >& _rxActualConnection )
294 bool bReturn = false;
295 if ( ensureLoaded() )
296 bReturn = m_xDataAccessTools->isEmbeddedInDatabase( _rxComponent, _rxActualConnection );
297 return bReturn;
300 //----------------------------------------------------------------
301 bool OStaticDataAccessTools::isEmbeddedInDatabase( const Reference< XInterface >& _rxComponent )
303 bool bReturn = false;
304 if ( ensureLoaded() )
306 Reference< XConnection > xDummy;
307 bReturn = m_xDataAccessTools->isEmbeddedInDatabase( _rxComponent, xDummy );
309 return bReturn;
312 //====================================================================
313 //= DBToolsObjectFactory
314 //====================================================================
315 //----------------------------------------------------------------
316 DBToolsObjectFactory::DBToolsObjectFactory()
320 //----------------------------------------------------------------
321 DBToolsObjectFactory::~DBToolsObjectFactory()
325 //----------------------------------------------------------------
326 SAL_WNODEPRECATED_DECLARATIONS_PUSH
327 ::std::auto_ptr< ::dbtools::FormattedColumnValue > DBToolsObjectFactory::createFormattedColumnValue(
328 const ::comphelper::ComponentContext& _rContext, const Reference< XRowSet >& _rxRowSet, const Reference< XPropertySet >& _rxColumn )
330 ::std::auto_ptr< ::dbtools::FormattedColumnValue > pValue;
331 if ( ensureLoaded() )
332 pValue = getFactory()->createFormattedColumnValue( _rContext, _rxRowSet, _rxColumn );
333 return pValue;
335 SAL_WNODEPRECATED_DECLARATIONS_POP
337 //........................................................................
338 } // namespace svxform
339 //........................................................................
342 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */