1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include <com/sun/star/sdbc/XConnection.hpp>
21 #include <com/sun/star/sdbc/XDataSource.hpp>
22 #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
23 #include <com/sun/star/sdb/SQLContext.hpp>
24 #include "svx/dbtoolsclient.hxx"
25 #include <osl/diagnose.h>
26 #include <rtl/instance.hxx>
27 #include <connectivity/formattedcolumnvalue.hxx>
29 //........................................................................
32 //........................................................................
34 using namespace ::connectivity::simple
;
35 using namespace ::com::sun::star::sdbc
;
36 using namespace ::com::sun::star::lang
;
37 using namespace ::com::sun::star::util
;
38 using namespace ::com::sun::star::uno
;
39 using namespace ::com::sun::star::beans
;
40 using namespace ::com::sun::star::sdb
;
41 using namespace ::com::sun::star::container
;
43 //====================================================================
45 //====================================================================
49 struct theODbtoolsClientMutex
: public rtl::Static
< osl::Mutex
, theODbtoolsClientMutex
> {};
52 sal_Int32
ODbtoolsClient::s_nClients
= 0;
53 oslModule
ODbtoolsClient::s_hDbtoolsModule
= NULL
;
54 createDataAccessToolsFactoryFunction
55 ODbtoolsClient::s_pFactoryCreationFunc
= NULL
;
57 //--------------------------------------------------------------------
58 ODbtoolsClient::ODbtoolsClient()
60 m_bCreateAlready
= sal_False
;
63 //--------------------------------------------------------------------
64 bool ODbtoolsClient::ensureLoaded() const
66 if ( !m_bCreateAlready
)
68 m_bCreateAlready
= true;
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!" );
78 m_xDataAccessFactory
= pDBTFactory
;
79 // by definition, the factory was aquired once
80 m_xDataAccessFactory
->release();
84 return m_xDataAccessFactory
.is();
87 //--------------------------------------------------------------------
88 ODbtoolsClient::~ODbtoolsClient()
90 // clear the factory _before_ revoking the client
91 // (the revocation may unload the DBT lib)
92 m_xDataAccessFactory
= NULL
;
94 if ( m_bCreateAlready
)
98 //--------------------------------------------------------------------
100 #ifndef DISABLE_DYNLOADING
102 extern "C" { static void SAL_CALL
thisModule() {} }
106 extern "C" void * createDataAccessToolsFactory();
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 #ifndef DISABLE_DYNLOADING
119 const OUString
sModuleName( SVLIBRARY( "dbtools" )
122 // load the dbtools library
123 s_hDbtoolsModule
= osl_loadModuleRelative(
124 &thisModule
, sModuleName
.pData
, 0);
125 OSL_ENSURE(NULL
!= s_hDbtoolsModule
, "ODbtoolsClient::registerClient: could not load the dbtools library!");
126 if (NULL
!= s_hDbtoolsModule
)
128 // get the symbol for the method creating the factory
129 const OUString
sFactoryCreationFunc( "createDataAccessToolsFactory" );
130 // reinterpret_cast<createDataAccessToolsFactoryFunction>
131 s_pFactoryCreationFunc
= (createDataAccessToolsFactoryFunction
)(
132 osl_getFunctionSymbol(s_hDbtoolsModule
, sFactoryCreationFunc
.pData
));
134 if (NULL
== s_pFactoryCreationFunc
)
135 { // did not find the symbol
136 OSL_FAIL("ODbtoolsClient::registerClient: could not find the symbol for creating the factory!");
137 osl_unloadModule(s_hDbtoolsModule
);
138 s_hDbtoolsModule
= NULL
;
142 s_pFactoryCreationFunc
= createDataAccessToolsFactory
;
147 //--------------------------------------------------------------------
148 void ODbtoolsClient::revokeClient()
150 ::osl::MutexGuard
aGuard(theODbtoolsClientMutex::get());
151 if (0 == --s_nClients
)
153 #ifndef DISABLE_DYNLOADING
154 s_pFactoryCreationFunc
= NULL
;
155 if (s_hDbtoolsModule
)
156 osl_unloadModule(s_hDbtoolsModule
);
158 s_hDbtoolsModule
= NULL
;
161 OSL_ENSURE(s_nClients
>= 0,"Illegall call of revokeClient()");
164 //====================================================================
165 //= OStaticDataAccessTools
166 //====================================================================
167 //--------------------------------------------------------------------
168 OStaticDataAccessTools::OStaticDataAccessTools()
172 //--------------------------------------------------------------------
174 bool OStaticDataAccessTools::ensureLoaded() const
176 if ( !ODbtoolsClient::ensureLoaded() )
178 m_xDataAccessTools
= getFactory()->getDataAccessTools();
179 return m_xDataAccessTools
.is();
182 //--------------------------------------------------------------------
183 Reference
< XNumberFormatsSupplier
> OStaticDataAccessTools::getNumberFormats(const Reference
< XConnection
>& _rxConn
, sal_Bool _bAllowDefault
) const
185 Reference
< XNumberFormatsSupplier
> xReturn
;
186 if ( ensureLoaded() )
187 xReturn
= m_xDataAccessTools
->getNumberFormats(_rxConn
, _bAllowDefault
);
191 //--------------------------------------------------------------------
192 sal_Int32
OStaticDataAccessTools::getDefaultNumberFormat( const Reference
< XPropertySet
>& _xColumn
, const Reference
< XNumberFormatTypes
>& _xTypes
, const Locale
& _rLocale
)
194 sal_Int32 nReturn
= 0;
195 if ( ensureLoaded() )
196 nReturn
= m_xDataAccessTools
->getDefaultNumberFormat( _xColumn
, _xTypes
, _rLocale
);
200 //--------------------------------------------------------------------
201 Reference
< XConnection
> OStaticDataAccessTools::getConnection_withFeedback(const OUString
& _rDataSourceName
,
202 const OUString
& _rUser
, const OUString
& _rPwd
, const Reference
<XComponentContext
>& _rxContext
) const
203 SAL_THROW ( (SQLException
) )
205 Reference
< XConnection
> xReturn
;
206 if ( ensureLoaded() )
207 xReturn
= m_xDataAccessTools
->getConnection_withFeedback(_rDataSourceName
, _rUser
, _rPwd
, _rxContext
);
211 //--------------------------------------------------------------------
212 Reference
< XConnection
> OStaticDataAccessTools::connectRowset( const Reference
< XRowSet
>& _rxRowSet
,
213 const Reference
< XComponentContext
>& _rxContext
, sal_Bool _bSetAsActiveConnection
) const
214 SAL_THROW ( ( SQLException
, WrappedTargetException
, RuntimeException
) )
216 Reference
< XConnection
> xReturn
;
217 if ( ensureLoaded() )
218 xReturn
= m_xDataAccessTools
->connectRowset( _rxRowSet
, _rxContext
, _bSetAsActiveConnection
);
222 //--------------------------------------------------------------------
223 Reference
< XConnection
> OStaticDataAccessTools::getRowSetConnection(const Reference
< XRowSet
>& _rxRowSet
) const SAL_THROW ( (RuntimeException
) )
225 Reference
< XConnection
> xReturn
;
226 if ( ensureLoaded() )
227 xReturn
= m_xDataAccessTools
->getRowSetConnection(_rxRowSet
);
231 //--------------------------------------------------------------------
232 void OStaticDataAccessTools::TransferFormComponentProperties(const Reference
< XPropertySet
>& _rxOld
,
233 const Reference
< XPropertySet
>& _rxNew
, const Locale
& _rLocale
) const
235 if ( ensureLoaded() )
236 m_xDataAccessTools
->TransferFormComponentProperties(_rxOld
, _rxNew
, _rLocale
);
239 //--------------------------------------------------------------------
240 OUString
OStaticDataAccessTools::quoteName(const OUString
& _rQuote
, const OUString
& _rName
) const
243 if ( ensureLoaded() )
244 sReturn
= m_xDataAccessTools
->quoteName(_rQuote
, _rName
);
248 // ------------------------------------------------
249 OUString
OStaticDataAccessTools::composeTableNameForSelect( const Reference
< XConnection
>& _rxConnection
, const Reference
< XPropertySet
>& _xTable
) const
252 if ( ensureLoaded() )
253 sReturn
= m_xDataAccessTools
->composeTableNameForSelect( _rxConnection
, _xTable
);
257 //----------------------------------------------------------------
258 Reference
< XDataSource
> OStaticDataAccessTools::getDataSource( const OUString
& _rsRegisteredName
, const Reference
< XComponentContext
>& _rxContext
) const
260 Reference
< XDataSource
> xReturn
;
261 if ( ensureLoaded() )
262 xReturn
= m_xDataAccessTools
->getDataSource(_rsRegisteredName
,_rxContext
);
266 //----------------------------------------------------------------
267 sal_Bool
OStaticDataAccessTools::canInsert(const Reference
< XPropertySet
>& _rxCursorSet
) const
269 sal_Bool bRet
= sal_False
;
270 if ( ensureLoaded() )
271 bRet
= m_xDataAccessTools
->canInsert( _rxCursorSet
);
275 //----------------------------------------------------------------
276 sal_Bool
OStaticDataAccessTools::canUpdate(const Reference
< XPropertySet
>& _rxCursorSet
) const
278 sal_Bool bRet
= sal_False
;
279 if ( ensureLoaded() )
280 bRet
= m_xDataAccessTools
->canUpdate( _rxCursorSet
);
284 //----------------------------------------------------------------
285 Reference
< XNameAccess
> OStaticDataAccessTools::getFieldsByCommandDescriptor( const Reference
< XConnection
>& _rxConnection
,
286 const sal_Int32 _nCommandType
, const OUString
& _rCommand
,
287 Reference
< XComponent
>& _rxKeepFieldsAlive
, ::dbtools::SQLExceptionInfo
* _pErrorInfo
) SAL_THROW( ( ) )
289 Reference
< XNameAccess
> aFields
;
290 if ( ensureLoaded() )
291 aFields
= m_xDataAccessTools
->getFieldsByCommandDescriptor( _rxConnection
, _nCommandType
,
292 _rCommand
, _rxKeepFieldsAlive
, _pErrorInfo
);
297 //----------------------------------------------------------------
298 bool OStaticDataAccessTools::isEmbeddedInDatabase( const Reference
< XInterface
>& _rxComponent
, Reference
< XConnection
>& _rxActualConnection
)
300 bool bReturn
= false;
301 if ( ensureLoaded() )
302 bReturn
= m_xDataAccessTools
->isEmbeddedInDatabase( _rxComponent
, _rxActualConnection
);
306 //----------------------------------------------------------------
307 bool OStaticDataAccessTools::isEmbeddedInDatabase( const Reference
< XInterface
>& _rxComponent
)
309 bool bReturn
= false;
310 if ( ensureLoaded() )
312 Reference
< XConnection
> xDummy
;
313 bReturn
= m_xDataAccessTools
->isEmbeddedInDatabase( _rxComponent
, xDummy
);
318 //====================================================================
319 //= DBToolsObjectFactory
320 //====================================================================
321 //----------------------------------------------------------------
322 DBToolsObjectFactory::DBToolsObjectFactory()
326 //----------------------------------------------------------------
327 DBToolsObjectFactory::~DBToolsObjectFactory()
331 //----------------------------------------------------------------
332 SAL_WNODEPRECATED_DECLARATIONS_PUSH
333 ::std::auto_ptr
< ::dbtools::FormattedColumnValue
> DBToolsObjectFactory::createFormattedColumnValue(
334 const ::comphelper::ComponentContext
& _rContext
, const Reference
< XRowSet
>& _rxRowSet
, const Reference
< XPropertySet
>& _rxColumn
)
336 ::std::auto_ptr
< ::dbtools::FormattedColumnValue
> pValue
;
337 if ( ensureLoaded() )
338 pValue
= getFactory()->createFormattedColumnValue( _rContext
, _rxRowSet
, _rxColumn
);
341 SAL_WNODEPRECATED_DECLARATIONS_POP
343 //........................................................................
344 } // namespace svxform
345 //........................................................................
348 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */