xref: /trunk/main/sw/source/ui/dbui/swdbtoolsclient.cxx (revision efeef26f81c84063fb0a91bde3856d4a51172d90)
1*efeef26fSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*efeef26fSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*efeef26fSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*efeef26fSAndrew Rist  * distributed with this work for additional information
6*efeef26fSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*efeef26fSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*efeef26fSAndrew Rist  * "License"); you may not use this file except in compliance
9*efeef26fSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*efeef26fSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*efeef26fSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*efeef26fSAndrew Rist  * software distributed under the License is distributed on an
15*efeef26fSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*efeef26fSAndrew Rist  * KIND, either express or implied.  See the License for the
17*efeef26fSAndrew Rist  * specific language governing permissions and limitations
18*efeef26fSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*efeef26fSAndrew Rist  *************************************************************/
21*efeef26fSAndrew Rist 
22*efeef26fSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sw.hxx"
26cdf0e10cSrcweir #include <com/sun/star/sdbc/XConnection.hpp>
27cdf0e10cSrcweir #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
28cdf0e10cSrcweir #include <com/sun/star/sdbc/XDataSource.hpp>
29cdf0e10cSrcweir #include <com/sun/star/sdb/SQLContext.hpp>
30cdf0e10cSrcweir #include <swdbtoolsclient.hxx>
31cdf0e10cSrcweir #include <osl/diagnose.h>
32cdf0e10cSrcweir #include <tools/solar.h>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir //........................................................................
35cdf0e10cSrcweir 
36cdf0e10cSrcweir using namespace ::connectivity::simple;
37cdf0e10cSrcweir using namespace ::com::sun::star;
38cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
39cdf0e10cSrcweir using namespace ::com::sun::star::lang;
40cdf0e10cSrcweir using namespace ::com::sun::star::util;
41cdf0e10cSrcweir using namespace ::com::sun::star::uno;
42cdf0e10cSrcweir using namespace ::com::sun::star::beans;
43cdf0e10cSrcweir using namespace ::com::sun::star::sdb;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir //====================================================================
46cdf0e10cSrcweir //= SwDbtoolsClient
47cdf0e10cSrcweir //====================================================================
48cdf0e10cSrcweir namespace
49cdf0e10cSrcweir {
50cdf0e10cSrcweir     // -----------------------------------------------------------------------------
51cdf0e10cSrcweir     // this namespace contains access to all static members of the class SwDbtoolsClient
52cdf0e10cSrcweir     // to make the initialize of the dll a little bit faster
53cdf0e10cSrcweir     // -----------------------------------------------------------------------------
54cdf0e10cSrcweir     ::osl::Mutex& getDbtoolsClientMutex()
55cdf0e10cSrcweir     {
56cdf0e10cSrcweir         static  ::osl::Mutex aMutex;
57cdf0e10cSrcweir         return aMutex;
58cdf0e10cSrcweir     }
59cdf0e10cSrcweir     // -----------------------------------------------------------------------------
60cdf0e10cSrcweir     sal_Int32& getDbToolsClientClients()
61cdf0e10cSrcweir     {
62cdf0e10cSrcweir         static  sal_Int32 nClients = 0;
63cdf0e10cSrcweir         return nClients;
64cdf0e10cSrcweir     }
65cdf0e10cSrcweir     // -----------------------------------------------------------------------------
66cdf0e10cSrcweir     oslModule& getDbToolsClientModule()
67cdf0e10cSrcweir     {
68cdf0e10cSrcweir         static oslModule hDbtoolsModule = NULL;
69cdf0e10cSrcweir         return hDbtoolsModule;
70cdf0e10cSrcweir     }
71cdf0e10cSrcweir     // -----------------------------------------------------------------------------
72cdf0e10cSrcweir     createDataAccessToolsFactoryFunction& getDbToolsClientFactoryFunction()
73cdf0e10cSrcweir     {
74cdf0e10cSrcweir         static createDataAccessToolsFactoryFunction pFactoryCreationFunc = NULL;
75cdf0e10cSrcweir         return pFactoryCreationFunc;
76cdf0e10cSrcweir     }
77cdf0e10cSrcweir     // -----------------------------------------------------------------------------
78cdf0e10cSrcweir }
79cdf0e10cSrcweir // -----------------------------------------------------------------------------
80cdf0e10cSrcweir SwDbtoolsClient::SwDbtoolsClient()
81cdf0e10cSrcweir {
82cdf0e10cSrcweir }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir //--------------------------------------------------------------------
85cdf0e10cSrcweir SwDbtoolsClient::~SwDbtoolsClient()
86cdf0e10cSrcweir {
87cdf0e10cSrcweir     if(m_xDataAccessFactory.is())
88cdf0e10cSrcweir     {
89cdf0e10cSrcweir         // clear the factory _before_ revoking the client
90cdf0e10cSrcweir         // (the revocation may unload the DBT lib)
91cdf0e10cSrcweir         m_xDataAccessFactory = NULL;
92cdf0e10cSrcweir         // revoke the client
93cdf0e10cSrcweir         revokeClient();
94cdf0e10cSrcweir     }
95cdf0e10cSrcweir }
96cdf0e10cSrcweir 
97cdf0e10cSrcweir //--------------------------------------------------------------------
98cdf0e10cSrcweir extern "C" { static void SAL_CALL thisModule() {} }
99cdf0e10cSrcweir 
100cdf0e10cSrcweir void SwDbtoolsClient::registerClient()
101cdf0e10cSrcweir {
102cdf0e10cSrcweir     ::osl::MutexGuard aGuard(getDbtoolsClientMutex());
103cdf0e10cSrcweir     if (1 == ++getDbToolsClientClients())
104cdf0e10cSrcweir     {
105cdf0e10cSrcweir         OSL_ENSURE(NULL == getDbToolsClientModule(), "SwDbtoolsClient::registerClient: inconsistence: already have a module!");
106cdf0e10cSrcweir         OSL_ENSURE(NULL == getDbToolsClientFactoryFunction(), "SwDbtoolsClient::registerClient: inconsistence: already have a factory function!");
107cdf0e10cSrcweir 
108cdf0e10cSrcweir         const ::rtl::OUString sModuleName = ::rtl::OUString::createFromAscii(
109cdf0e10cSrcweir             SVLIBRARY( "dbtools" )
110cdf0e10cSrcweir         );
111cdf0e10cSrcweir 
112cdf0e10cSrcweir         // load the dbtools library
113cdf0e10cSrcweir         getDbToolsClientModule() = osl_loadModuleRelative(
114cdf0e10cSrcweir             &thisModule, sModuleName.pData, 0);
115cdf0e10cSrcweir         OSL_ENSURE(NULL != getDbToolsClientModule(), "SwDbtoolsClient::registerClient: could not load the dbtools library!");
116cdf0e10cSrcweir         if (NULL != getDbToolsClientModule())
117cdf0e10cSrcweir         {
118cdf0e10cSrcweir             // get the symbol for the method creating the factory
119cdf0e10cSrcweir             const ::rtl::OUString sFactoryCreationFunc = ::rtl::OUString::createFromAscii("createDataAccessToolsFactory");
120cdf0e10cSrcweir             //  reinterpret_cast<createDataAccessToolsFactoryFunction> removed for gcc permissive
121cdf0e10cSrcweir             getDbToolsClientFactoryFunction() = reinterpret_cast< createDataAccessToolsFactoryFunction >(
122cdf0e10cSrcweir                 osl_getFunctionSymbol(getDbToolsClientModule(), sFactoryCreationFunc.pData));
123cdf0e10cSrcweir 
124cdf0e10cSrcweir             if (NULL == getDbToolsClientFactoryFunction())
125cdf0e10cSrcweir             {   // did not find the symbol
126cdf0e10cSrcweir                 OSL_ENSURE(sal_False, "SwDbtoolsClient::registerClient: could not find the symbol for creating the factory!");
127cdf0e10cSrcweir                 osl_unloadModule(getDbToolsClientModule());
128cdf0e10cSrcweir                 getDbToolsClientModule() = NULL;
129cdf0e10cSrcweir             }
130cdf0e10cSrcweir         }
131cdf0e10cSrcweir     }
132cdf0e10cSrcweir }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir //--------------------------------------------------------------------
135cdf0e10cSrcweir void SwDbtoolsClient::revokeClient()
136cdf0e10cSrcweir {
137cdf0e10cSrcweir     ::osl::MutexGuard aGuard(getDbtoolsClientMutex());
138cdf0e10cSrcweir     if (0 == --getDbToolsClientClients())
139cdf0e10cSrcweir     {
140cdf0e10cSrcweir         getDbToolsClientFactoryFunction() = NULL;
141cdf0e10cSrcweir         if (getDbToolsClientModule())
142cdf0e10cSrcweir             osl_unloadModule(getDbToolsClientModule());
143cdf0e10cSrcweir         getDbToolsClientModule() = NULL;
144cdf0e10cSrcweir     }
145cdf0e10cSrcweir }
146cdf0e10cSrcweir /* -----------------------------30.08.2001 14:58------------------------------
147cdf0e10cSrcweir 
148cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
149cdf0e10cSrcweir void SwDbtoolsClient::getFactory()
150cdf0e10cSrcweir {
151cdf0e10cSrcweir     if(!m_xDataAccessFactory.is())
152cdf0e10cSrcweir     {
153cdf0e10cSrcweir         registerClient();
154cdf0e10cSrcweir         if(getDbToolsClientFactoryFunction())
155cdf0e10cSrcweir         {   // loading the lib succeeded
156cdf0e10cSrcweir             void* pUntypedFactory = (*getDbToolsClientFactoryFunction())();
157cdf0e10cSrcweir             IDataAccessToolsFactory* pDBTFactory = static_cast<IDataAccessToolsFactory*>(pUntypedFactory);
158cdf0e10cSrcweir             OSL_ENSURE(pDBTFactory, "SwDbtoolsClient::SwDbtoolsClient: no factory returned!");
159cdf0e10cSrcweir             if (pDBTFactory)
160cdf0e10cSrcweir             {
161cdf0e10cSrcweir                 m_xDataAccessFactory = pDBTFactory;
162cdf0e10cSrcweir                 // by definition, the factory was aquired once
163cdf0e10cSrcweir                 m_xDataAccessFactory->release();
164cdf0e10cSrcweir             }
165cdf0e10cSrcweir         }
166cdf0e10cSrcweir     }
167cdf0e10cSrcweir }
168cdf0e10cSrcweir /* -----------------------------30.08.2001 11:32------------------------------
169cdf0e10cSrcweir 
170cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
171cdf0e10cSrcweir ::rtl::Reference< ::connectivity::simple::IDataAccessTools >
172cdf0e10cSrcweir     SwDbtoolsClient::getDataAccessTools()
173cdf0e10cSrcweir {
174cdf0e10cSrcweir     if(!m_xDataAccessTools.is())
175cdf0e10cSrcweir     {
176cdf0e10cSrcweir         getFactory();
177cdf0e10cSrcweir         if(m_xDataAccessFactory.is())
178cdf0e10cSrcweir             m_xDataAccessTools = m_xDataAccessFactory->getDataAccessTools();
179cdf0e10cSrcweir     }
180cdf0e10cSrcweir     return m_xDataAccessTools;
181cdf0e10cSrcweir }
182cdf0e10cSrcweir /* -----------------------------30.08.2001 12:40------------------------------
183cdf0e10cSrcweir 
184cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
185cdf0e10cSrcweir ::rtl::Reference< ::connectivity::simple::IDataAccessTypeConversion >
186cdf0e10cSrcweir     SwDbtoolsClient::getAccessTypeConversion()
187cdf0e10cSrcweir {
188cdf0e10cSrcweir     if(!m_xAccessTypeConversion.is())
189cdf0e10cSrcweir     {
190cdf0e10cSrcweir         getFactory();
191cdf0e10cSrcweir         if(m_xDataAccessFactory.is())
192cdf0e10cSrcweir             m_xAccessTypeConversion = m_xDataAccessFactory->getTypeConversionHelper();
193cdf0e10cSrcweir     }
194cdf0e10cSrcweir     return m_xAccessTypeConversion;
195cdf0e10cSrcweir }
196cdf0e10cSrcweir 
197cdf0e10cSrcweir /* -----------------------------30.08.2001 11:37------------------------------
198cdf0e10cSrcweir 
199cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
200cdf0e10cSrcweir Reference< XDataSource > SwDbtoolsClient::getDataSource(
201cdf0e10cSrcweir         const ::rtl::OUString& rRegisteredName,
202cdf0e10cSrcweir         const Reference< XMultiServiceFactory>& xFactory
203cdf0e10cSrcweir             )
204cdf0e10cSrcweir {
205cdf0e10cSrcweir     Reference< XDataSource > xRet;
206cdf0e10cSrcweir     ::rtl::Reference< ::connectivity::simple::IDataAccessTools >    xAccess = getDataAccessTools();
207cdf0e10cSrcweir     if(xAccess.is())
208cdf0e10cSrcweir         xRet = xAccess->getDataSource(rRegisteredName, xFactory);
209cdf0e10cSrcweir     return xRet;
210cdf0e10cSrcweir }
211cdf0e10cSrcweir /* -----------------------------30.08.2001 12:06------------------------------
212cdf0e10cSrcweir 
213cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
214cdf0e10cSrcweir sal_Int32 SwDbtoolsClient::getDefaultNumberFormat(
215cdf0e10cSrcweir         const Reference< XPropertySet >& rxColumn,
216cdf0e10cSrcweir         const Reference< XNumberFormatTypes >& rxTypes,
217cdf0e10cSrcweir         const Locale& rLocale
218cdf0e10cSrcweir             )
219cdf0e10cSrcweir {
220cdf0e10cSrcweir     sal_Int32 nRet = -1;
221cdf0e10cSrcweir     ::rtl::Reference< ::connectivity::simple::IDataAccessTools >    xAccess = getDataAccessTools();
222cdf0e10cSrcweir     if(xAccess.is())
223cdf0e10cSrcweir         nRet = xAccess->getDefaultNumberFormat( rxColumn, rxTypes, rLocale);
224cdf0e10cSrcweir     return nRet;
225cdf0e10cSrcweir }
226cdf0e10cSrcweir /* -----------------------------30.08.2001 12:38------------------------------
227cdf0e10cSrcweir 
228cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
229cdf0e10cSrcweir ::rtl::OUString SwDbtoolsClient::getFormattedValue(
230cdf0e10cSrcweir         const uno::Reference< beans::XPropertySet>& _rxColumn,
231cdf0e10cSrcweir         const uno::Reference< util::XNumberFormatter>& _rxFormatter,
232cdf0e10cSrcweir         const lang::Locale& _rLocale,
233cdf0e10cSrcweir         const util::Date& _rNullDate
234cdf0e10cSrcweir             )
235cdf0e10cSrcweir 
236cdf0e10cSrcweir {
237cdf0e10cSrcweir     ::rtl::Reference< ::connectivity::simple::IDataAccessTypeConversion > xConversion =
238cdf0e10cSrcweir                     getAccessTypeConversion();
239cdf0e10cSrcweir     rtl::OUString sRet;
240cdf0e10cSrcweir     if(xConversion.is())
241cdf0e10cSrcweir         sRet = xConversion->getFormattedValue(_rxColumn, _rxFormatter, _rLocale, _rNullDate);
242cdf0e10cSrcweir     return sRet;
243cdf0e10cSrcweir }
244cdf0e10cSrcweir 
245