xref: /aoo42x/main/svx/source/form/dbtoolsclient.cxx (revision cdf0e10c)
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_svx.hxx"
30 #include <com/sun/star/sdbc/XConnection.hpp>
31 #include <com/sun/star/sdbc/XDataSource.hpp>
32 #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
33 #include <com/sun/star/sdb/SQLContext.hpp>
34 #include "svx/dbtoolsclient.hxx"
35 #include <osl/diagnose.h>
36 #include <connectivity/formattedcolumnvalue.hxx>
37 
38 //........................................................................
39 namespace svxform
40 {
41 //........................................................................
42 
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;
51 
52 	//====================================================================
53 	//= ODbtoolsClient
54 	//====================================================================
55 	::osl::Mutex	ODbtoolsClient::s_aMutex;
56 	sal_Int32		ODbtoolsClient::s_nClients = 0;
57 	oslModule		ODbtoolsClient::s_hDbtoolsModule = NULL;
58 	createDataAccessToolsFactoryFunction
59 					ODbtoolsClient::s_pFactoryCreationFunc = NULL;
60 
61 	//--------------------------------------------------------------------
62 	ODbtoolsClient::ODbtoolsClient()
63 	{
64 		m_bCreateAlready = sal_False;
65 	}
66 
67 	//--------------------------------------------------------------------
68 	bool ODbtoolsClient::ensureLoaded() const
69 	{
70 		if ( !m_bCreateAlready )
71         {
72 		    m_bCreateAlready = true;
73 
74             registerClient();
75 		    if ( s_pFactoryCreationFunc )
76 		    {	// loading the lib succeeded
77 			    void* pUntypedFactory = (*s_pFactoryCreationFunc)();
78 			    IDataAccessToolsFactory* pDBTFactory = static_cast< IDataAccessToolsFactory* >( pUntypedFactory );
79 			    OSL_ENSURE( pDBTFactory, "ODbtoolsClient::ODbtoolsClient: no factory returned!" );
80 			    if ( pDBTFactory )
81 			    {
82 				    m_xDataAccessFactory = pDBTFactory;
83 				    // by definition, the factory was aquired once
84 				    m_xDataAccessFactory->release();
85 			    }
86 		    }
87         }
88 		return m_xDataAccessFactory.is();
89 	}
90 
91 	//--------------------------------------------------------------------
92 	ODbtoolsClient::~ODbtoolsClient()
93 	{
94 		// clear the factory _before_ revoking the client
95 		// (the revocation may unload the DBT lib)
96 		m_xDataAccessFactory = NULL;
97 		// revoke the client
98 		if ( m_bCreateAlready )
99 			revokeClient();
100 	}
101 
102 	//--------------------------------------------------------------------
103     extern "C" { static void SAL_CALL thisModule() {} }
104 
105 	void ODbtoolsClient::registerClient()
106 	{
107 		::osl::MutexGuard aGuard(s_aMutex);
108 		if (1 == ++s_nClients)
109 		{
110 			OSL_ENSURE(NULL == s_hDbtoolsModule, "ODbtoolsClient::registerClient: inconsistence: already have a module!");
111 			OSL_ENSURE(NULL == s_pFactoryCreationFunc, "ODbtoolsClient::registerClient: inconsistence: already have a factory function!");
112 
113 			const ::rtl::OUString sModuleName = ::rtl::OUString::createFromAscii(
114 				SVLIBRARY( "dbtools" )
115 			);
116 
117 			// load the dbtools library
118 			s_hDbtoolsModule = osl_loadModuleRelative(
119                 &thisModule, sModuleName.pData, 0);
120 			OSL_ENSURE(NULL != s_hDbtoolsModule, "ODbtoolsClient::registerClient: could not load the dbtools library!");
121 			if (NULL != s_hDbtoolsModule)
122 			{
123 				// get the symbol for the method creating the factory
124 				const ::rtl::OUString sFactoryCreationFunc = ::rtl::OUString::createFromAscii("createDataAccessToolsFactory");
125 				//  reinterpret_cast<createDataAccessToolsFactoryFunction>
126 				s_pFactoryCreationFunc = (createDataAccessToolsFactoryFunction)(
127 					osl_getFunctionSymbol(s_hDbtoolsModule, sFactoryCreationFunc.pData));
128 
129 				if (NULL == s_pFactoryCreationFunc)
130 				{	// did not find the symbol
131 					OSL_ENSURE(sal_False, "ODbtoolsClient::registerClient: could not find the symbol for creating the factory!");
132 					osl_unloadModule(s_hDbtoolsModule);
133 					s_hDbtoolsModule = NULL;
134 				}
135 			}
136 		}
137 	}
138 
139 	//--------------------------------------------------------------------
140 	void ODbtoolsClient::revokeClient()
141 	{
142 		::osl::MutexGuard aGuard(s_aMutex);
143 		if (0 == --s_nClients)
144 		{
145 			s_pFactoryCreationFunc = NULL;
146 			if (s_hDbtoolsModule)
147 				osl_unloadModule(s_hDbtoolsModule);
148 			s_hDbtoolsModule = NULL;
149 		}
150 
151 		OSL_ENSURE(s_nClients >= 0,"Illegall call of revokeClient()");
152 	}
153 
154 	//====================================================================
155 	//= OStaticDataAccessTools
156 	//====================================================================
157 	//--------------------------------------------------------------------
158 	OStaticDataAccessTools::OStaticDataAccessTools()
159 	{
160 	}
161 
162 	//--------------------------------------------------------------------
163 	//add by BerryJia for fixing Bug97420 Time:2002-9-12-11:00(PRC time)
164 	bool OStaticDataAccessTools::ensureLoaded() const
165 	{
166         if ( !ODbtoolsClient::ensureLoaded() )
167             return false;
168 	 	m_xDataAccessTools = getFactory()->getDataAccessTools();
169         return m_xDataAccessTools.is();
170 	}
171 
172 	//--------------------------------------------------------------------
173 	Reference< XNumberFormatsSupplier > OStaticDataAccessTools::getNumberFormats(const Reference< XConnection>& _rxConn, sal_Bool _bAllowDefault) const
174 	{
175 		Reference< XNumberFormatsSupplier > xReturn;
176 		if ( ensureLoaded() )
177 			xReturn = m_xDataAccessTools->getNumberFormats(_rxConn, _bAllowDefault);
178 		return xReturn;
179 	}
180 
181 	//--------------------------------------------------------------------
182 	sal_Int32 OStaticDataAccessTools::getDefaultNumberFormat( const Reference< XPropertySet >& _xColumn, const Reference< XNumberFormatTypes >& _xTypes, const Locale& _rLocale )
183     {
184         sal_Int32 nReturn = 0;
185 		if ( ensureLoaded() )
186 			nReturn = m_xDataAccessTools->getDefaultNumberFormat( _xColumn, _xTypes, _rLocale );
187 		return nReturn;
188     }
189 
190 	//--------------------------------------------------------------------
191 	Reference< XConnection> OStaticDataAccessTools::getConnection_withFeedback(const ::rtl::OUString& _rDataSourceName,
192 		const ::rtl::OUString& _rUser, const ::rtl::OUString& _rPwd, const Reference< XMultiServiceFactory>& _rxFactory) const
193 			SAL_THROW ( (SQLException) )
194 	{
195 		Reference< XConnection > xReturn;
196 		if ( ensureLoaded() )
197 			xReturn = m_xDataAccessTools->getConnection_withFeedback(_rDataSourceName, _rUser, _rPwd, _rxFactory);
198 		return xReturn;
199 	}
200 
201 	//--------------------------------------------------------------------
202 	Reference< XConnection > OStaticDataAccessTools::connectRowset( const Reference< XRowSet >& _rxRowSet,
203         const Reference< XMultiServiceFactory >& _rxFactory, sal_Bool _bSetAsActiveConnection ) const
204         SAL_THROW ( ( SQLException, WrappedTargetException, RuntimeException ) )
205 	{
206 		Reference< XConnection > xReturn;
207 		if ( ensureLoaded() )
208 			xReturn = m_xDataAccessTools->connectRowset( _rxRowSet, _rxFactory, _bSetAsActiveConnection );
209 		return xReturn;
210 	}
211 
212 	//--------------------------------------------------------------------
213 	Reference< XConnection > OStaticDataAccessTools::getRowSetConnection(const Reference< XRowSet >& _rxRowSet) const SAL_THROW ( (RuntimeException) )
214 	{
215 		Reference< XConnection > xReturn;
216 		if ( ensureLoaded() )
217 			xReturn = m_xDataAccessTools->getRowSetConnection(_rxRowSet);
218 		return xReturn;
219 	}
220 
221 	//--------------------------------------------------------------------
222 	void OStaticDataAccessTools::TransferFormComponentProperties(const Reference< XPropertySet>& _rxOld,
223 		const Reference< XPropertySet>& _rxNew, const Locale& _rLocale) const
224 	{
225 		if ( ensureLoaded() )
226 			m_xDataAccessTools->TransferFormComponentProperties(_rxOld, _rxNew, _rLocale);
227 	}
228 
229 	//--------------------------------------------------------------------
230 	::rtl::OUString OStaticDataAccessTools::quoteName(const ::rtl::OUString& _rQuote, const ::rtl::OUString& _rName) const
231 	{
232 		::rtl::OUString sReturn;
233 		if ( ensureLoaded() )
234 			sReturn = m_xDataAccessTools->quoteName(_rQuote, _rName);
235 		return sReturn;
236 	}
237 
238 	// ------------------------------------------------
239 	::rtl::OUString OStaticDataAccessTools::composeTableNameForSelect( const Reference< XConnection >& _rxConnection, const Reference< XPropertySet>& _xTable ) const
240     {
241 		::rtl::OUString sReturn;
242 		if ( ensureLoaded() )
243 			sReturn = m_xDataAccessTools->composeTableNameForSelect( _rxConnection, _xTable );
244         return sReturn;
245     }
246 
247 	//--------------------------------------------------------------------
248 	SQLContext OStaticDataAccessTools::prependContextInfo(SQLException& _rException, const Reference< XInterface >& _rxContext,
249 		const ::rtl::OUString& _rContextDescription, const ::rtl::OUString& _rContextDetails) const
250 	{
251 		SQLContext aReturn;
252 		if ( ensureLoaded() )
253 			aReturn = m_xDataAccessTools->prependContextInfo(_rException, _rxContext, _rContextDescription, _rContextDetails);
254 		return aReturn;
255 	}
256 
257 	//----------------------------------------------------------------
258 	Reference< XDataSource > OStaticDataAccessTools::getDataSource( const ::rtl::OUString& _rsRegisteredName, const Reference< XMultiServiceFactory>& _rxFactory ) const
259 	{
260 		Reference< XDataSource > xReturn;
261 		if ( ensureLoaded() )
262 			xReturn = m_xDataAccessTools->getDataSource(_rsRegisteredName,_rxFactory);
263 		return xReturn;
264 	}
265 
266 	//----------------------------------------------------------------
267 	sal_Bool OStaticDataAccessTools::canInsert(const Reference< XPropertySet>& _rxCursorSet) const
268 	{
269 		sal_Bool bRet = sal_False;
270 		if ( ensureLoaded() )
271 			bRet = m_xDataAccessTools->canInsert( _rxCursorSet );
272 		return bRet;
273 	}
274 
275 	//----------------------------------------------------------------
276 	sal_Bool OStaticDataAccessTools::canUpdate(const Reference< XPropertySet>& _rxCursorSet) const
277 	{
278 		sal_Bool bRet = sal_False;
279 		if ( ensureLoaded() )
280 			bRet = m_xDataAccessTools->canUpdate( _rxCursorSet );
281 		return bRet;
282 	}
283 
284 	//----------------------------------------------------------------
285 	sal_Bool OStaticDataAccessTools::canDelete(const Reference< XPropertySet>& _rxCursorSet) const
286 	{
287 		sal_Bool bRet = sal_False;
288 		if ( ensureLoaded() )
289 			bRet = m_xDataAccessTools->canDelete( _rxCursorSet );
290 		return bRet;
291 	}
292 
293 	//----------------------------------------------------------------
294 	Reference< XNameAccess > OStaticDataAccessTools::getFieldsByCommandDescriptor( const Reference< XConnection >& _rxConnection,
295 		const sal_Int32 _nCommandType, const ::rtl::OUString& _rCommand,
296 			Reference< XComponent >& _rxKeepFieldsAlive, ::dbtools::SQLExceptionInfo* _pErrorInfo ) SAL_THROW( ( ) )
297 	{
298 		Reference< XNameAccess > aFields;
299 		if ( ensureLoaded() )
300 			aFields = m_xDataAccessTools->getFieldsByCommandDescriptor( _rxConnection, _nCommandType,
301 				_rCommand, _rxKeepFieldsAlive, _pErrorInfo );
302 
303 		return aFields;
304 	}
305 
306 	//----------------------------------------------------------------
307 	Sequence< ::rtl::OUString > OStaticDataAccessTools::getFieldNamesByCommandDescriptor(
308 		const Reference< XConnection >& _rxConnection, const sal_Int32 _nCommandType,
309 		const ::rtl::OUString& _rCommand, ::dbtools::SQLExceptionInfo* _pErrorInfo ) SAL_THROW( ( ) )
310 	{
311 		Sequence< ::rtl::OUString > aNames;
312 		if ( ensureLoaded() )
313 			aNames = m_xDataAccessTools->getFieldNamesByCommandDescriptor( _rxConnection, _nCommandType,
314 				_rCommand, _pErrorInfo );
315 		return aNames;
316 	}
317 
318 	//----------------------------------------------------------------
319     bool OStaticDataAccessTools::isEmbeddedInDatabase( const Reference< XInterface >& _rxComponent, Reference< XConnection >& _rxActualConnection )
320     {
321         bool bReturn = false;
322 		if ( ensureLoaded() )
323 			bReturn = m_xDataAccessTools->isEmbeddedInDatabase( _rxComponent, _rxActualConnection );
324         return bReturn;
325     }
326 
327 	//----------------------------------------------------------------
328     bool OStaticDataAccessTools::isEmbeddedInDatabase( const Reference< XInterface >& _rxComponent )
329     {
330         bool bReturn = false;
331 		if ( ensureLoaded() )
332         {
333             Reference< XConnection > xDummy;
334 			bReturn = m_xDataAccessTools->isEmbeddedInDatabase( _rxComponent, xDummy );
335         }
336         return bReturn;
337     }
338 
339     //====================================================================
340 	//= DBToolsObjectFactory
341 	//====================================================================
342 	//----------------------------------------------------------------
343     DBToolsObjectFactory::DBToolsObjectFactory()
344     {
345     }
346 
347 	//----------------------------------------------------------------
348     DBToolsObjectFactory::~DBToolsObjectFactory()
349     {
350     }
351 
352 	//----------------------------------------------------------------
353     ::std::auto_ptr< ::dbtools::FormattedColumnValue > DBToolsObjectFactory::createFormattedColumnValue(
354         const ::comphelper::ComponentContext& _rContext, const Reference< XRowSet >& _rxRowSet, const Reference< XPropertySet >& _rxColumn )
355     {
356         ::std::auto_ptr< ::dbtools::FormattedColumnValue > pValue;
357 		if ( ensureLoaded() )
358             pValue = getFactory()->createFormattedColumnValue( _rContext, _rxRowSet, _rxColumn );
359         return pValue;
360     }
361 
362 //........................................................................
363 }	// namespace svxform
364 //........................................................................
365 
366 
367