1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: dbtoolsclient.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
33 #include <com/sun/star/sdbc/XConnection.hpp>
34 #include <com/sun/star/sdbc/XDataSource.hpp>
35 #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
36 #include <com/sun/star/sdb/SQLContext.hpp>
37 #include "svx/dbtoolsclient.hxx"
38 #include <osl/diagnose.h>
39 #include <connectivity/formattedcolumnvalue.hxx>
41 //........................................................................
44 //........................................................................
46 using namespace ::connectivity::simple
;
47 using namespace ::com::sun::star::sdbc
;
48 using namespace ::com::sun::star::lang
;
49 using namespace ::com::sun::star::util
;
50 using namespace ::com::sun::star::uno
;
51 using namespace ::com::sun::star::beans
;
52 using namespace ::com::sun::star::sdb
;
53 using namespace ::com::sun::star::container
;
55 //====================================================================
57 //====================================================================
58 ::osl::Mutex
ODbtoolsClient::s_aMutex
;
59 sal_Int32
ODbtoolsClient::s_nClients
= 0;
60 oslModule
ODbtoolsClient::s_hDbtoolsModule
= NULL
;
61 createDataAccessToolsFactoryFunction
62 ODbtoolsClient::s_pFactoryCreationFunc
= NULL
;
64 //--------------------------------------------------------------------
65 ODbtoolsClient::ODbtoolsClient()
67 m_bCreateAlready
= FALSE
;
70 //--------------------------------------------------------------------
71 bool ODbtoolsClient::ensureLoaded() const
73 if ( !m_bCreateAlready
)
75 m_bCreateAlready
= true;
78 if ( s_pFactoryCreationFunc
)
79 { // loading the lib succeeded
80 void* pUntypedFactory
= (*s_pFactoryCreationFunc
)();
81 IDataAccessToolsFactory
* pDBTFactory
= static_cast< IDataAccessToolsFactory
* >( pUntypedFactory
);
82 OSL_ENSURE( pDBTFactory
, "ODbtoolsClient::ODbtoolsClient: no factory returned!" );
85 m_xDataAccessFactory
= pDBTFactory
;
86 // by definition, the factory was aquired once
87 m_xDataAccessFactory
->release();
91 return m_xDataAccessFactory
.is();
94 //--------------------------------------------------------------------
95 ODbtoolsClient::~ODbtoolsClient()
97 // clear the factory _before_ revoking the client
98 // (the revocation may unload the DBT lib)
99 m_xDataAccessFactory
= NULL
;
101 if ( m_bCreateAlready
)
105 //--------------------------------------------------------------------
106 extern "C" { static void SAL_CALL
thisModule() {} }
108 void ODbtoolsClient::registerClient()
110 ::osl::MutexGuard
aGuard(s_aMutex
);
111 if (1 == ++s_nClients
)
113 OSL_ENSURE(NULL
== s_hDbtoolsModule
, "ODbtoolsClient::registerClient: inconsistence: already have a module!");
114 OSL_ENSURE(NULL
== s_pFactoryCreationFunc
, "ODbtoolsClient::registerClient: inconsistence: already have a factory function!");
116 const ::rtl::OUString sModuleName
= ::rtl::OUString::createFromAscii(
117 SVLIBRARY( "dbtools" )
120 // load the dbtools library
121 s_hDbtoolsModule
= osl_loadModuleRelative(
122 &thisModule
, sModuleName
.pData
, 0);
123 OSL_ENSURE(NULL
!= s_hDbtoolsModule
, "ODbtoolsClient::registerClient: could not load the dbtools library!");
124 if (NULL
!= s_hDbtoolsModule
)
126 // get the symbol for the method creating the factory
127 const ::rtl::OUString sFactoryCreationFunc
= ::rtl::OUString::createFromAscii("createDataAccessToolsFactory");
128 // reinterpret_cast<createDataAccessToolsFactoryFunction>
129 s_pFactoryCreationFunc
= (createDataAccessToolsFactoryFunction
)(
130 osl_getFunctionSymbol(s_hDbtoolsModule
, sFactoryCreationFunc
.pData
));
132 if (NULL
== s_pFactoryCreationFunc
)
133 { // did not find the symbol
134 OSL_ENSURE(sal_False
, "ODbtoolsClient::registerClient: could not find the symbol for creating the factory!");
135 osl_unloadModule(s_hDbtoolsModule
);
136 s_hDbtoolsModule
= NULL
;
142 //--------------------------------------------------------------------
143 void ODbtoolsClient::revokeClient()
145 ::osl::MutexGuard
aGuard(s_aMutex
);
146 if (0 == --s_nClients
)
148 s_pFactoryCreationFunc
= NULL
;
149 if (s_hDbtoolsModule
)
150 osl_unloadModule(s_hDbtoolsModule
);
151 s_hDbtoolsModule
= NULL
;
154 OSL_ENSURE(s_nClients
>= 0,"Illegall call of revokeClient()");
157 //====================================================================
158 //= OStaticDataAccessTools
159 //====================================================================
160 //--------------------------------------------------------------------
161 OStaticDataAccessTools::OStaticDataAccessTools()
165 //--------------------------------------------------------------------
166 //add by BerryJia for fixing Bug97420 Time:2002-9-12-11:00(PRC time)
167 bool OStaticDataAccessTools::ensureLoaded() const
169 if ( !ODbtoolsClient::ensureLoaded() )
171 m_xDataAccessTools
= getFactory()->getDataAccessTools();
172 return m_xDataAccessTools
.is();
175 //--------------------------------------------------------------------
176 Reference
< XNumberFormatsSupplier
> OStaticDataAccessTools::getNumberFormats(const Reference
< XConnection
>& _rxConn
, sal_Bool _bAllowDefault
) const
178 Reference
< XNumberFormatsSupplier
> xReturn
;
179 if ( ensureLoaded() )
180 xReturn
= m_xDataAccessTools
->getNumberFormats(_rxConn
, _bAllowDefault
);
184 //--------------------------------------------------------------------
185 sal_Int32
OStaticDataAccessTools::getDefaultNumberFormat( const Reference
< XPropertySet
>& _xColumn
, const Reference
< XNumberFormatTypes
>& _xTypes
, const Locale
& _rLocale
)
187 sal_Int32 nReturn
= 0;
188 if ( ensureLoaded() )
189 nReturn
= m_xDataAccessTools
->getDefaultNumberFormat( _xColumn
, _xTypes
, _rLocale
);
193 //--------------------------------------------------------------------
194 Reference
< XConnection
> OStaticDataAccessTools::getConnection_withFeedback(const ::rtl::OUString
& _rDataSourceName
,
195 const ::rtl::OUString
& _rUser
, const ::rtl::OUString
& _rPwd
, const Reference
< XMultiServiceFactory
>& _rxFactory
) const
196 SAL_THROW ( (SQLException
) )
198 Reference
< XConnection
> xReturn
;
199 if ( ensureLoaded() )
200 xReturn
= m_xDataAccessTools
->getConnection_withFeedback(_rDataSourceName
, _rUser
, _rPwd
, _rxFactory
);
204 //--------------------------------------------------------------------
205 Reference
< XConnection
> OStaticDataAccessTools::connectRowset( const Reference
< XRowSet
>& _rxRowSet
,
206 const Reference
< XMultiServiceFactory
>& _rxFactory
, sal_Bool _bSetAsActiveConnection
) const
207 SAL_THROW ( ( SQLException
, WrappedTargetException
, RuntimeException
) )
209 Reference
< XConnection
> xReturn
;
210 if ( ensureLoaded() )
211 xReturn
= m_xDataAccessTools
->connectRowset( _rxRowSet
, _rxFactory
, _bSetAsActiveConnection
);
215 //--------------------------------------------------------------------
216 Reference
< XConnection
> OStaticDataAccessTools::getRowSetConnection(const Reference
< XRowSet
>& _rxRowSet
) const SAL_THROW ( (RuntimeException
) )
218 Reference
< XConnection
> xReturn
;
219 if ( ensureLoaded() )
220 xReturn
= m_xDataAccessTools
->getRowSetConnection(_rxRowSet
);
224 //--------------------------------------------------------------------
225 void OStaticDataAccessTools::TransferFormComponentProperties(const Reference
< XPropertySet
>& _rxOld
,
226 const Reference
< XPropertySet
>& _rxNew
, const Locale
& _rLocale
) const
228 if ( ensureLoaded() )
229 m_xDataAccessTools
->TransferFormComponentProperties(_rxOld
, _rxNew
, _rLocale
);
232 //--------------------------------------------------------------------
233 ::rtl::OUString
OStaticDataAccessTools::quoteName(const ::rtl::OUString
& _rQuote
, const ::rtl::OUString
& _rName
) const
235 ::rtl::OUString sReturn
;
236 if ( ensureLoaded() )
237 sReturn
= m_xDataAccessTools
->quoteName(_rQuote
, _rName
);
241 // ------------------------------------------------
242 ::rtl::OUString
OStaticDataAccessTools::composeTableNameForSelect( const Reference
< XConnection
>& _rxConnection
, const Reference
< XPropertySet
>& _xTable
) const
244 ::rtl::OUString sReturn
;
245 if ( ensureLoaded() )
246 sReturn
= m_xDataAccessTools
->composeTableNameForSelect( _rxConnection
, _xTable
);
250 //--------------------------------------------------------------------
251 SQLContext
OStaticDataAccessTools::prependContextInfo(SQLException
& _rException
, const Reference
< XInterface
>& _rxContext
,
252 const ::rtl::OUString
& _rContextDescription
, const ::rtl::OUString
& _rContextDetails
) const
255 if ( ensureLoaded() )
256 aReturn
= m_xDataAccessTools
->prependContextInfo(_rException
, _rxContext
, _rContextDescription
, _rContextDetails
);
260 //----------------------------------------------------------------
261 Reference
< XDataSource
> OStaticDataAccessTools::getDataSource( const ::rtl::OUString
& _rsRegisteredName
, const Reference
< XMultiServiceFactory
>& _rxFactory
) const
263 Reference
< XDataSource
> xReturn
;
264 if ( ensureLoaded() )
265 xReturn
= m_xDataAccessTools
->getDataSource(_rsRegisteredName
,_rxFactory
);
269 //----------------------------------------------------------------
270 sal_Bool
OStaticDataAccessTools::canInsert(const Reference
< XPropertySet
>& _rxCursorSet
) const
272 sal_Bool bRet
= sal_False
;
273 if ( ensureLoaded() )
274 bRet
= m_xDataAccessTools
->canInsert( _rxCursorSet
);
278 //----------------------------------------------------------------
279 sal_Bool
OStaticDataAccessTools::canUpdate(const Reference
< XPropertySet
>& _rxCursorSet
) const
281 sal_Bool bRet
= sal_False
;
282 if ( ensureLoaded() )
283 bRet
= m_xDataAccessTools
->canUpdate( _rxCursorSet
);
287 //----------------------------------------------------------------
288 sal_Bool
OStaticDataAccessTools::canDelete(const Reference
< XPropertySet
>& _rxCursorSet
) const
290 sal_Bool bRet
= sal_False
;
291 if ( ensureLoaded() )
292 bRet
= m_xDataAccessTools
->canDelete( _rxCursorSet
);
296 //----------------------------------------------------------------
297 Reference
< XNameAccess
> OStaticDataAccessTools::getFieldsByCommandDescriptor( const Reference
< XConnection
>& _rxConnection
,
298 const sal_Int32 _nCommandType
, const ::rtl::OUString
& _rCommand
,
299 Reference
< XComponent
>& _rxKeepFieldsAlive
, ::dbtools::SQLExceptionInfo
* _pErrorInfo
) SAL_THROW( ( ) )
301 Reference
< XNameAccess
> aFields
;
302 if ( ensureLoaded() )
303 aFields
= m_xDataAccessTools
->getFieldsByCommandDescriptor( _rxConnection
, _nCommandType
,
304 _rCommand
, _rxKeepFieldsAlive
, _pErrorInfo
);
309 //----------------------------------------------------------------
310 Sequence
< ::rtl::OUString
> OStaticDataAccessTools::getFieldNamesByCommandDescriptor(
311 const Reference
< XConnection
>& _rxConnection
, const sal_Int32 _nCommandType
,
312 const ::rtl::OUString
& _rCommand
, ::dbtools::SQLExceptionInfo
* _pErrorInfo
) SAL_THROW( ( ) )
314 Sequence
< ::rtl::OUString
> aNames
;
315 if ( ensureLoaded() )
316 aNames
= m_xDataAccessTools
->getFieldNamesByCommandDescriptor( _rxConnection
, _nCommandType
,
317 _rCommand
, _pErrorInfo
);
321 //----------------------------------------------------------------
322 bool OStaticDataAccessTools::isEmbeddedInDatabase( const Reference
< XInterface
>& _rxComponent
, Reference
< XConnection
>& _rxActualConnection
)
324 bool bReturn
= false;
325 if ( ensureLoaded() )
326 bReturn
= m_xDataAccessTools
->isEmbeddedInDatabase( _rxComponent
, _rxActualConnection
);
330 //----------------------------------------------------------------
331 bool OStaticDataAccessTools::isEmbeddedInDatabase( const Reference
< XInterface
>& _rxComponent
)
333 bool bReturn
= false;
334 if ( ensureLoaded() )
336 Reference
< XConnection
> xDummy
;
337 bReturn
= m_xDataAccessTools
->isEmbeddedInDatabase( _rxComponent
, xDummy
);
342 //====================================================================
343 //= DBToolsObjectFactory
344 //====================================================================
345 //----------------------------------------------------------------
346 DBToolsObjectFactory::DBToolsObjectFactory()
350 //----------------------------------------------------------------
351 DBToolsObjectFactory::~DBToolsObjectFactory()
355 //----------------------------------------------------------------
356 ::std::auto_ptr
< ::dbtools::FormattedColumnValue
> DBToolsObjectFactory::createFormattedColumnValue(
357 const ::comphelper::ComponentContext
& _rContext
, const Reference
< XRowSet
>& _rxRowSet
, const Reference
< XPropertySet
>& _rxColumn
)
359 ::std::auto_ptr
< ::dbtools::FormattedColumnValue
> pValue
;
360 if ( ensureLoaded() )
361 pValue
= getFactory()->createFormattedColumnValue( _rContext
, _rxRowSet
, _rxColumn
);
365 //........................................................................
366 } // namespace svxform
367 //........................................................................