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/util/XNumberFormatsSupplier.hpp>
22 #include <com/sun/star/sdbc/XDataSource.hpp>
23 #include <com/sun/star/sdb/SQLContext.hpp>
24 #include <swdbtoolsclient.hxx>
25 #include <osl/diagnose.h>
26 #include <tools/solar.h>
28 using namespace ::connectivity::simple
;
29 using namespace ::com::sun::star
;
30 using namespace ::com::sun::star::sdbc
;
31 using namespace ::com::sun::star::lang
;
32 using namespace ::com::sun::star::util
;
33 using namespace ::com::sun::star::uno
;
34 using namespace ::com::sun::star::beans
;
35 using namespace ::com::sun::star::sdb
;
37 //====================================================================
39 //====================================================================
42 // -----------------------------------------------------------------------------
43 // this namespace contains access to all static members of the class SwDbtoolsClient
44 // to make the initialize of the dll a little bit faster
45 // -----------------------------------------------------------------------------
46 ::osl::Mutex
& getDbtoolsClientMutex()
48 static ::osl::Mutex aMutex
;
51 // -----------------------------------------------------------------------------
52 sal_Int32
& getDbToolsClientClients()
54 static sal_Int32 nClients
= 0;
57 // -----------------------------------------------------------------------------
58 oslModule
& getDbToolsClientModule()
60 static oslModule hDbtoolsModule
= NULL
;
61 return hDbtoolsModule
;
63 // -----------------------------------------------------------------------------
64 createDataAccessToolsFactoryFunction
& getDbToolsClientFactoryFunction()
66 static createDataAccessToolsFactoryFunction pFactoryCreationFunc
= NULL
;
67 return pFactoryCreationFunc
;
69 // -----------------------------------------------------------------------------
72 SwDbtoolsClient::SwDbtoolsClient()
76 SwDbtoolsClient::~SwDbtoolsClient()
78 if(m_xDataAccessFactory
.is())
80 // clear the factory _before_ revoking the client
81 // (the revocation may unload the DBT lib)
82 m_xDataAccessFactory
= NULL
;
88 #ifndef DISABLE_DYNLOADING
90 extern "C" { static void SAL_CALL
thisModule() {} }
94 extern "C" void * createDataAccessToolsFactory();
98 void SwDbtoolsClient::registerClient()
100 ::osl::MutexGuard
aGuard(getDbtoolsClientMutex());
101 if (1 == ++getDbToolsClientClients())
103 OSL_ENSURE(NULL
== getDbToolsClientModule(), "SwDbtoolsClient::registerClient: inconsistence: already have a module!");
104 OSL_ENSURE(NULL
== getDbToolsClientFactoryFunction(), "SwDbtoolsClient::registerClient: inconsistence: already have a factory function!");
106 #ifndef DISABLE_DYNLOADING
107 const OUString
sModuleName(SVLIBRARY("dbtools"));
109 // load the dbtools library
110 getDbToolsClientModule() = osl_loadModuleRelative(
111 &thisModule
, sModuleName
.pData
, 0);
112 OSL_ENSURE(NULL
!= getDbToolsClientModule(), "SwDbtoolsClient::registerClient: could not load the dbtools library!");
113 if (NULL
!= getDbToolsClientModule())
115 // get the symbol for the method creating the factory
116 const OUString
sFactoryCreationFunc("createDataAccessToolsFactory");
117 // reinterpret_cast<createDataAccessToolsFactoryFunction> removed for gcc permissive
118 getDbToolsClientFactoryFunction() = reinterpret_cast< createDataAccessToolsFactoryFunction
>(
119 osl_getFunctionSymbol(getDbToolsClientModule(), sFactoryCreationFunc
.pData
));
121 if (NULL
== getDbToolsClientFactoryFunction())
122 { // did not find the symbol
123 OSL_FAIL("SwDbtoolsClient::registerClient: could not find the symbol for creating the factory!");
124 osl_unloadModule(getDbToolsClientModule());
125 getDbToolsClientModule() = NULL
;
129 getDbToolsClientFactoryFunction() = createDataAccessToolsFactory
;
134 void SwDbtoolsClient::revokeClient()
136 ::osl::MutexGuard
aGuard(getDbtoolsClientMutex());
137 if (0 == --getDbToolsClientClients())
139 #ifndef DISABLE_DYNLOADING
140 getDbToolsClientFactoryFunction() = NULL
;
141 if (getDbToolsClientModule())
142 osl_unloadModule(getDbToolsClientModule());
144 getDbToolsClientModule() = NULL
;
148 void SwDbtoolsClient::getFactory()
150 if(!m_xDataAccessFactory
.is())
153 if(getDbToolsClientFactoryFunction())
154 { // loading the lib succeeded
155 void* pUntypedFactory
= (*getDbToolsClientFactoryFunction())();
156 IDataAccessToolsFactory
* pDBTFactory
= static_cast<IDataAccessToolsFactory
*>(pUntypedFactory
);
157 OSL_ENSURE(pDBTFactory
, "SwDbtoolsClient::SwDbtoolsClient: no factory returned!");
160 m_xDataAccessFactory
= pDBTFactory
;
161 // by definition, the factory was aquired once
162 m_xDataAccessFactory
->release();
168 ::rtl::Reference
< ::connectivity::simple::IDataAccessTools
>
169 SwDbtoolsClient::getDataAccessTools()
171 if(!m_xDataAccessTools
.is())
174 if(m_xDataAccessFactory
.is())
175 m_xDataAccessTools
= m_xDataAccessFactory
->getDataAccessTools();
177 return m_xDataAccessTools
;
180 ::rtl::Reference
< ::connectivity::simple::IDataAccessTypeConversion
>
181 SwDbtoolsClient::getAccessTypeConversion()
183 if(!m_xAccessTypeConversion
.is())
186 if(m_xDataAccessFactory
.is())
187 m_xAccessTypeConversion
= m_xDataAccessFactory
->getTypeConversionHelper();
189 return m_xAccessTypeConversion
;
192 Reference
< XDataSource
> SwDbtoolsClient::getDataSource(
193 const OUString
& rRegisteredName
,
194 const Reference
<XComponentContext
>& rxContext
197 Reference
< XDataSource
> xRet
;
198 ::rtl::Reference
< ::connectivity::simple::IDataAccessTools
> xAccess
= getDataAccessTools();
200 xRet
= xAccess
->getDataSource(rRegisteredName
, rxContext
);
204 sal_Int32
SwDbtoolsClient::getDefaultNumberFormat(
205 const Reference
< XPropertySet
>& rxColumn
,
206 const Reference
< XNumberFormatTypes
>& rxTypes
,
207 const lang::Locale
& rLocale
211 ::rtl::Reference
< ::connectivity::simple::IDataAccessTools
> xAccess
= getDataAccessTools();
213 nRet
= xAccess
->getDefaultNumberFormat( rxColumn
, rxTypes
, rLocale
);
217 OUString
SwDbtoolsClient::getFormattedValue(
218 const uno::Reference
< beans::XPropertySet
>& _rxColumn
,
219 const uno::Reference
< util::XNumberFormatter
>& _rxFormatter
,
220 const lang::Locale
& _rLocale
,
221 const util::Date
& _rNullDate
225 ::rtl::Reference
< ::connectivity::simple::IDataAccessTypeConversion
> xConversion
=
226 getAccessTypeConversion();
229 sRet
= xConversion
->getFormattedValue(_rxColumn
, _rxFormatter
, _rLocale
, _rNullDate
);
233 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */