xref: /aoo42x/main/svx/source/form/dbtoolsclient.cxx (revision 86e1cf34)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svx.hxx"
26 #include <com/sun/star/sdbc/XConnection.hpp>
27 #include <com/sun/star/sdbc/XDataSource.hpp>
28 #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
29 #include <com/sun/star/sdb/SQLContext.hpp>
30 #include "svx/dbtoolsclient.hxx"
31 #include <osl/diagnose.h>
32 #include <connectivity/formattedcolumnvalue.hxx>
33 
34 //........................................................................
35 namespace svxform
36 {
37 //........................................................................
38 
39 	using namespace ::connectivity::simple;
40 	using namespace ::com::sun::star::sdbc;
41 	using namespace ::com::sun::star::lang;
42 	using namespace ::com::sun::star::util;
43 	using namespace ::com::sun::star::uno;
44 	using namespace ::com::sun::star::beans;
45 	using namespace ::com::sun::star::sdb;
46 	using namespace ::com::sun::star::container;
47 
48 	//====================================================================
49 	//= ODbtoolsClient
50 	//====================================================================
51 	::osl::Mutex	ODbtoolsClient::s_aMutex;
52 	sal_Int32		ODbtoolsClient::s_nClients = 0;
53 	oslModule		ODbtoolsClient::s_hDbtoolsModule = NULL;
54 	createDataAccessToolsFactoryFunction
55 					ODbtoolsClient::s_pFactoryCreationFunc = NULL;
56 
57 	//--------------------------------------------------------------------
ODbtoolsClient()58 	ODbtoolsClient::ODbtoolsClient()
59 	{
60 		m_bCreateAlready = sal_False;
61 	}
62 
63 	//--------------------------------------------------------------------
ensureLoaded() const64 	bool ODbtoolsClient::ensureLoaded() const
65 	{
66 		if ( !m_bCreateAlready )
67         {
68 		    m_bCreateAlready = true;
69 
70             registerClient();
71 		    if ( s_pFactoryCreationFunc )
72 		    {	// loading the lib succeeded
73 			    void* pUntypedFactory = (*s_pFactoryCreationFunc)();
74 			    IDataAccessToolsFactory* pDBTFactory = static_cast< IDataAccessToolsFactory* >( pUntypedFactory );
75 			    OSL_ENSURE( pDBTFactory, "ODbtoolsClient::ODbtoolsClient: no factory returned!" );
76 			    if ( pDBTFactory )
77 			    {
78 				    m_xDataAccessFactory = pDBTFactory;
79 				    // by definition, the factory was acquired once
80 				    m_xDataAccessFactory->release();
81 			    }
82 		    }
83         }
84 		return m_xDataAccessFactory.is();
85 	}
86 
87 	//--------------------------------------------------------------------
~ODbtoolsClient()88 	ODbtoolsClient::~ODbtoolsClient()
89 	{
90 		// clear the factory _before_ revoking the client
91 		// (the revocation may unload the DBT lib)
92 		m_xDataAccessFactory = NULL;
93 		// revoke the client
94 		if ( m_bCreateAlready )
95 			revokeClient();
96 	}
97 
98 	//--------------------------------------------------------------------
thisModule()99     extern "C" { static void SAL_CALL thisModule() {} }
100 
registerClient()101 	void ODbtoolsClient::registerClient()
102 	{
103 		::osl::MutexGuard aGuard(s_aMutex);
104 		if (1 == ++s_nClients)
105 		{
106 			OSL_ENSURE(NULL == s_hDbtoolsModule, "ODbtoolsClient::registerClient: inconsistence: already have a module!");
107 			OSL_ENSURE(NULL == s_pFactoryCreationFunc, "ODbtoolsClient::registerClient: inconsistence: already have a factory function!");
108 
109 			const ::rtl::OUString sModuleName = ::rtl::OUString::createFromAscii(
110 				SVLIBRARY( "dbtools" )
111 			);
112 
113 			// load the dbtools library
114 			s_hDbtoolsModule = osl_loadModuleRelative(
115                 &thisModule, sModuleName.pData, 0);
116 			OSL_ENSURE(NULL != s_hDbtoolsModule, "ODbtoolsClient::registerClient: could not load the dbtools library!");
117 			if (NULL != s_hDbtoolsModule)
118 			{
119 				// get the symbol for the method creating the factory
120 				const ::rtl::OUString sFactoryCreationFunc = ::rtl::OUString::createFromAscii("createDataAccessToolsFactory");
121 				//  reinterpret_cast<createDataAccessToolsFactoryFunction>
122 				s_pFactoryCreationFunc = (createDataAccessToolsFactoryFunction)(
123 					osl_getFunctionSymbol(s_hDbtoolsModule, sFactoryCreationFunc.pData));
124 
125 				if (NULL == s_pFactoryCreationFunc)
126 				{	// did not find the symbol
127 					OSL_ENSURE(sal_False, "ODbtoolsClient::registerClient: could not find the symbol for creating the factory!");
128 					osl_unloadModule(s_hDbtoolsModule);
129 					s_hDbtoolsModule = NULL;
130 				}
131 			}
132 		}
133 	}
134 
135 	//--------------------------------------------------------------------
revokeClient()136 	void ODbtoolsClient::revokeClient()
137 	{
138 		::osl::MutexGuard aGuard(s_aMutex);
139 		if (0 == --s_nClients)
140 		{
141 			s_pFactoryCreationFunc = NULL;
142 			if (s_hDbtoolsModule)
143 				osl_unloadModule(s_hDbtoolsModule);
144 			s_hDbtoolsModule = NULL;
145 		}
146 
147 		OSL_ENSURE(s_nClients >= 0,"Illegall call of revokeClient()");
148 	}
149 
150 	//====================================================================
151 	//= OStaticDataAccessTools
152 	//====================================================================
153 	//--------------------------------------------------------------------
OStaticDataAccessTools()154 	OStaticDataAccessTools::OStaticDataAccessTools()
155 	{
156 	}
157 
158 	//--------------------------------------------------------------------
159 	//add by BerryJia for fixing Bug97420 Time:2002-9-12-11:00(PRC time)
ensureLoaded() const160 	bool OStaticDataAccessTools::ensureLoaded() const
161 	{
162         if ( !ODbtoolsClient::ensureLoaded() )
163             return false;
164 	 	m_xDataAccessTools = getFactory()->getDataAccessTools();
165         return m_xDataAccessTools.is();
166 	}
167 
168 	//--------------------------------------------------------------------
getNumberFormats(const Reference<XConnection> & _rxConn,sal_Bool _bAllowDefault) const169 	Reference< XNumberFormatsSupplier > OStaticDataAccessTools::getNumberFormats(const Reference< XConnection>& _rxConn, sal_Bool _bAllowDefault) const
170 	{
171 		Reference< XNumberFormatsSupplier > xReturn;
172 		if ( ensureLoaded() )
173 			xReturn = m_xDataAccessTools->getNumberFormats(_rxConn, _bAllowDefault);
174 		return xReturn;
175 	}
176 
177 	//--------------------------------------------------------------------
getDefaultNumberFormat(const Reference<XPropertySet> & _xColumn,const Reference<XNumberFormatTypes> & _xTypes,const Locale & _rLocale)178 	sal_Int32 OStaticDataAccessTools::getDefaultNumberFormat( const Reference< XPropertySet >& _xColumn, const Reference< XNumberFormatTypes >& _xTypes, const Locale& _rLocale )
179     {
180         sal_Int32 nReturn = 0;
181 		if ( ensureLoaded() )
182 			nReturn = m_xDataAccessTools->getDefaultNumberFormat( _xColumn, _xTypes, _rLocale );
183 		return nReturn;
184     }
185 
186 	//--------------------------------------------------------------------
getConnection_withFeedback(const::rtl::OUString & _rDataSourceName,const::rtl::OUString & _rUser,const::rtl::OUString & _rPwd,const Reference<XMultiServiceFactory> & _rxFactory) const187 	Reference< XConnection> OStaticDataAccessTools::getConnection_withFeedback(const ::rtl::OUString& _rDataSourceName,
188 		const ::rtl::OUString& _rUser, const ::rtl::OUString& _rPwd, const Reference< XMultiServiceFactory>& _rxFactory) const
189 			SAL_THROW ( (SQLException) )
190 	{
191 		Reference< XConnection > xReturn;
192 		if ( ensureLoaded() )
193 			xReturn = m_xDataAccessTools->getConnection_withFeedback(_rDataSourceName, _rUser, _rPwd, _rxFactory);
194 		return xReturn;
195 	}
196 
197 	//--------------------------------------------------------------------
connectRowset(const Reference<XRowSet> & _rxRowSet,const Reference<XMultiServiceFactory> & _rxFactory,sal_Bool _bSetAsActiveConnection) const198 	Reference< XConnection > OStaticDataAccessTools::connectRowset( const Reference< XRowSet >& _rxRowSet,
199         const Reference< XMultiServiceFactory >& _rxFactory, sal_Bool _bSetAsActiveConnection ) const
200         SAL_THROW ( ( SQLException, WrappedTargetException, RuntimeException ) )
201 	{
202 		Reference< XConnection > xReturn;
203 		if ( ensureLoaded() )
204 			xReturn = m_xDataAccessTools->connectRowset( _rxRowSet, _rxFactory, _bSetAsActiveConnection );
205 		return xReturn;
206 	}
207 
208 	//--------------------------------------------------------------------
getRowSetConnection(const Reference<XRowSet> & _rxRowSet) const209 	Reference< XConnection > OStaticDataAccessTools::getRowSetConnection(const Reference< XRowSet >& _rxRowSet) const SAL_THROW ( (RuntimeException) )
210 	{
211 		Reference< XConnection > xReturn;
212 		if ( ensureLoaded() )
213 			xReturn = m_xDataAccessTools->getRowSetConnection(_rxRowSet);
214 		return xReturn;
215 	}
216 
217 	//--------------------------------------------------------------------
TransferFormComponentProperties(const Reference<XPropertySet> & _rxOld,const Reference<XPropertySet> & _rxNew,const Locale & _rLocale) const218 	void OStaticDataAccessTools::TransferFormComponentProperties(const Reference< XPropertySet>& _rxOld,
219 		const Reference< XPropertySet>& _rxNew, const Locale& _rLocale) const
220 	{
221 		if ( ensureLoaded() )
222 			m_xDataAccessTools->TransferFormComponentProperties(_rxOld, _rxNew, _rLocale);
223 	}
224 
225 	//--------------------------------------------------------------------
quoteName(const::rtl::OUString & _rQuote,const::rtl::OUString & _rName) const226 	::rtl::OUString OStaticDataAccessTools::quoteName(const ::rtl::OUString& _rQuote, const ::rtl::OUString& _rName) const
227 	{
228 		::rtl::OUString sReturn;
229 		if ( ensureLoaded() )
230 			sReturn = m_xDataAccessTools->quoteName(_rQuote, _rName);
231 		return sReturn;
232 	}
233 
234 	// ------------------------------------------------
composeTableNameForSelect(const Reference<XConnection> & _rxConnection,const Reference<XPropertySet> & _xTable) const235 	::rtl::OUString OStaticDataAccessTools::composeTableNameForSelect( const Reference< XConnection >& _rxConnection, const Reference< XPropertySet>& _xTable ) const
236     {
237 		::rtl::OUString sReturn;
238 		if ( ensureLoaded() )
239 			sReturn = m_xDataAccessTools->composeTableNameForSelect( _rxConnection, _xTable );
240         return sReturn;
241     }
242 
243 	//--------------------------------------------------------------------
prependContextInfo(SQLException & _rException,const Reference<XInterface> & _rxContext,const::rtl::OUString & _rContextDescription,const::rtl::OUString & _rContextDetails) const244 	SQLContext OStaticDataAccessTools::prependContextInfo(SQLException& _rException, const Reference< XInterface >& _rxContext,
245 		const ::rtl::OUString& _rContextDescription, const ::rtl::OUString& _rContextDetails) const
246 	{
247 		SQLContext aReturn;
248 		if ( ensureLoaded() )
249 			aReturn = m_xDataAccessTools->prependContextInfo(_rException, _rxContext, _rContextDescription, _rContextDetails);
250 		return aReturn;
251 	}
252 
253 	//----------------------------------------------------------------
getDataSource(const::rtl::OUString & _rsRegisteredName,const Reference<XMultiServiceFactory> & _rxFactory) const254 	Reference< XDataSource > OStaticDataAccessTools::getDataSource( const ::rtl::OUString& _rsRegisteredName, const Reference< XMultiServiceFactory>& _rxFactory ) const
255 	{
256 		Reference< XDataSource > xReturn;
257 		if ( ensureLoaded() )
258 			xReturn = m_xDataAccessTools->getDataSource(_rsRegisteredName,_rxFactory);
259 		return xReturn;
260 	}
261 
262 	//----------------------------------------------------------------
canInsert(const Reference<XPropertySet> & _rxCursorSet) const263 	sal_Bool OStaticDataAccessTools::canInsert(const Reference< XPropertySet>& _rxCursorSet) const
264 	{
265 		sal_Bool bRet = sal_False;
266 		if ( ensureLoaded() )
267 			bRet = m_xDataAccessTools->canInsert( _rxCursorSet );
268 		return bRet;
269 	}
270 
271 	//----------------------------------------------------------------
canUpdate(const Reference<XPropertySet> & _rxCursorSet) const272 	sal_Bool OStaticDataAccessTools::canUpdate(const Reference< XPropertySet>& _rxCursorSet) const
273 	{
274 		sal_Bool bRet = sal_False;
275 		if ( ensureLoaded() )
276 			bRet = m_xDataAccessTools->canUpdate( _rxCursorSet );
277 		return bRet;
278 	}
279 
280 	//----------------------------------------------------------------
canDelete(const Reference<XPropertySet> & _rxCursorSet) const281 	sal_Bool OStaticDataAccessTools::canDelete(const Reference< XPropertySet>& _rxCursorSet) const
282 	{
283 		sal_Bool bRet = sal_False;
284 		if ( ensureLoaded() )
285 			bRet = m_xDataAccessTools->canDelete( _rxCursorSet );
286 		return bRet;
287 	}
288 
289 	//----------------------------------------------------------------
getFieldsByCommandDescriptor(const Reference<XConnection> & _rxConnection,const sal_Int32 _nCommandType,const::rtl::OUString & _rCommand,Reference<XComponent> & _rxKeepFieldsAlive,::dbtools::SQLExceptionInfo * _pErrorInfo)290 	Reference< XNameAccess > OStaticDataAccessTools::getFieldsByCommandDescriptor( const Reference< XConnection >& _rxConnection,
291 		const sal_Int32 _nCommandType, const ::rtl::OUString& _rCommand,
292 			Reference< XComponent >& _rxKeepFieldsAlive, ::dbtools::SQLExceptionInfo* _pErrorInfo ) SAL_THROW( ( ) )
293 	{
294 		Reference< XNameAccess > aFields;
295 		if ( ensureLoaded() )
296 			aFields = m_xDataAccessTools->getFieldsByCommandDescriptor( _rxConnection, _nCommandType,
297 				_rCommand, _rxKeepFieldsAlive, _pErrorInfo );
298 
299 		return aFields;
300 	}
301 
302 	//----------------------------------------------------------------
getFieldNamesByCommandDescriptor(const Reference<XConnection> & _rxConnection,const sal_Int32 _nCommandType,const::rtl::OUString & _rCommand,::dbtools::SQLExceptionInfo * _pErrorInfo)303 	Sequence< ::rtl::OUString > OStaticDataAccessTools::getFieldNamesByCommandDescriptor(
304 		const Reference< XConnection >& _rxConnection, const sal_Int32 _nCommandType,
305 		const ::rtl::OUString& _rCommand, ::dbtools::SQLExceptionInfo* _pErrorInfo ) SAL_THROW( ( ) )
306 	{
307 		Sequence< ::rtl::OUString > aNames;
308 		if ( ensureLoaded() )
309 			aNames = m_xDataAccessTools->getFieldNamesByCommandDescriptor( _rxConnection, _nCommandType,
310 				_rCommand, _pErrorInfo );
311 		return aNames;
312 	}
313 
314 	//----------------------------------------------------------------
isEmbeddedInDatabase(const Reference<XInterface> & _rxComponent,Reference<XConnection> & _rxActualConnection)315     bool OStaticDataAccessTools::isEmbeddedInDatabase( const Reference< XInterface >& _rxComponent, Reference< XConnection >& _rxActualConnection )
316     {
317         bool bReturn = false;
318 		if ( ensureLoaded() )
319 			bReturn = m_xDataAccessTools->isEmbeddedInDatabase( _rxComponent, _rxActualConnection );
320         return bReturn;
321     }
322 
323 	//----------------------------------------------------------------
isEmbeddedInDatabase(const Reference<XInterface> & _rxComponent)324     bool OStaticDataAccessTools::isEmbeddedInDatabase( const Reference< XInterface >& _rxComponent )
325     {
326         bool bReturn = false;
327 		if ( ensureLoaded() )
328         {
329             Reference< XConnection > xDummy;
330 			bReturn = m_xDataAccessTools->isEmbeddedInDatabase( _rxComponent, xDummy );
331         }
332         return bReturn;
333     }
334 
335     //====================================================================
336 	//= DBToolsObjectFactory
337 	//====================================================================
338 	//----------------------------------------------------------------
DBToolsObjectFactory()339     DBToolsObjectFactory::DBToolsObjectFactory()
340     {
341     }
342 
343 	//----------------------------------------------------------------
~DBToolsObjectFactory()344     DBToolsObjectFactory::~DBToolsObjectFactory()
345     {
346     }
347 
348 	//----------------------------------------------------------------
createFormattedColumnValue(const::comphelper::ComponentContext & _rContext,const Reference<XRowSet> & _rxRowSet,const Reference<XPropertySet> & _rxColumn)349     ::std::auto_ptr< ::dbtools::FormattedColumnValue > DBToolsObjectFactory::createFormattedColumnValue(
350         const ::comphelper::ComponentContext& _rContext, const Reference< XRowSet >& _rxRowSet, const Reference< XPropertySet >& _rxColumn )
351     {
352         ::std::auto_ptr< ::dbtools::FormattedColumnValue > pValue;
353 		if ( ensureLoaded() )
354             pValue = getFactory()->createFormattedColumnValue( _rContext, _rxRowSet, _rxColumn );
355         return pValue;
356     }
357 
358 //........................................................................
359 }	// namespace svxform
360 //........................................................................
361 
362 
363