xref: /AOO41X/main/sw/source/ui/dbui/swdbtoolsclient.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_sw.hxx"
30*cdf0e10cSrcweir #include <com/sun/star/sdbc/XConnection.hpp>
31*cdf0e10cSrcweir #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
32*cdf0e10cSrcweir #include <com/sun/star/sdbc/XDataSource.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/sdb/SQLContext.hpp>
34*cdf0e10cSrcweir #include <swdbtoolsclient.hxx>
35*cdf0e10cSrcweir #include <osl/diagnose.h>
36*cdf0e10cSrcweir #include <tools/solar.h>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir //........................................................................
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir using namespace ::connectivity::simple;
41*cdf0e10cSrcweir using namespace ::com::sun::star;
42*cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
43*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
44*cdf0e10cSrcweir using namespace ::com::sun::star::util;
45*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
46*cdf0e10cSrcweir using namespace ::com::sun::star::beans;
47*cdf0e10cSrcweir using namespace ::com::sun::star::sdb;
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir //====================================================================
50*cdf0e10cSrcweir //= SwDbtoolsClient
51*cdf0e10cSrcweir //====================================================================
52*cdf0e10cSrcweir namespace
53*cdf0e10cSrcweir {
54*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
55*cdf0e10cSrcweir 	// this namespace contains access to all static members of the class SwDbtoolsClient
56*cdf0e10cSrcweir 	// to make the initialize of the dll a little bit faster
57*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
58*cdf0e10cSrcweir 	::osl::Mutex& getDbtoolsClientMutex()
59*cdf0e10cSrcweir 	{
60*cdf0e10cSrcweir 		static  ::osl::Mutex aMutex;
61*cdf0e10cSrcweir 		return aMutex;
62*cdf0e10cSrcweir 	}
63*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
64*cdf0e10cSrcweir 	sal_Int32& getDbToolsClientClients()
65*cdf0e10cSrcweir 	{
66*cdf0e10cSrcweir 		static  sal_Int32 nClients = 0;
67*cdf0e10cSrcweir 		return nClients;
68*cdf0e10cSrcweir 	}
69*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
70*cdf0e10cSrcweir 	oslModule& getDbToolsClientModule()
71*cdf0e10cSrcweir 	{
72*cdf0e10cSrcweir 		static oslModule hDbtoolsModule = NULL;
73*cdf0e10cSrcweir 		return hDbtoolsModule;
74*cdf0e10cSrcweir 	}
75*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
76*cdf0e10cSrcweir 	createDataAccessToolsFactoryFunction& getDbToolsClientFactoryFunction()
77*cdf0e10cSrcweir 	{
78*cdf0e10cSrcweir 		static createDataAccessToolsFactoryFunction pFactoryCreationFunc = NULL;
79*cdf0e10cSrcweir 		return pFactoryCreationFunc;
80*cdf0e10cSrcweir 	}
81*cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
82*cdf0e10cSrcweir }
83*cdf0e10cSrcweir // -----------------------------------------------------------------------------
84*cdf0e10cSrcweir SwDbtoolsClient::SwDbtoolsClient()
85*cdf0e10cSrcweir {
86*cdf0e10cSrcweir }
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir //--------------------------------------------------------------------
89*cdf0e10cSrcweir SwDbtoolsClient::~SwDbtoolsClient()
90*cdf0e10cSrcweir {
91*cdf0e10cSrcweir     if(m_xDataAccessFactory.is())
92*cdf0e10cSrcweir     {
93*cdf0e10cSrcweir         // clear the factory _before_ revoking the client
94*cdf0e10cSrcweir         // (the revocation may unload the DBT lib)
95*cdf0e10cSrcweir         m_xDataAccessFactory = NULL;
96*cdf0e10cSrcweir         // revoke the client
97*cdf0e10cSrcweir         revokeClient();
98*cdf0e10cSrcweir     }
99*cdf0e10cSrcweir }
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir //--------------------------------------------------------------------
102*cdf0e10cSrcweir extern "C" { static void SAL_CALL thisModule() {} }
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir void SwDbtoolsClient::registerClient()
105*cdf0e10cSrcweir {
106*cdf0e10cSrcweir     ::osl::MutexGuard aGuard(getDbtoolsClientMutex());
107*cdf0e10cSrcweir     if (1 == ++getDbToolsClientClients())
108*cdf0e10cSrcweir     {
109*cdf0e10cSrcweir         OSL_ENSURE(NULL == getDbToolsClientModule(), "SwDbtoolsClient::registerClient: inconsistence: already have a module!");
110*cdf0e10cSrcweir         OSL_ENSURE(NULL == getDbToolsClientFactoryFunction(), "SwDbtoolsClient::registerClient: inconsistence: already have a factory function!");
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir         const ::rtl::OUString sModuleName = ::rtl::OUString::createFromAscii(
113*cdf0e10cSrcweir             SVLIBRARY( "dbtools" )
114*cdf0e10cSrcweir         );
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir         // load the dbtools library
117*cdf0e10cSrcweir         getDbToolsClientModule() = osl_loadModuleRelative(
118*cdf0e10cSrcweir             &thisModule, sModuleName.pData, 0);
119*cdf0e10cSrcweir         OSL_ENSURE(NULL != getDbToolsClientModule(), "SwDbtoolsClient::registerClient: could not load the dbtools library!");
120*cdf0e10cSrcweir         if (NULL != getDbToolsClientModule())
121*cdf0e10cSrcweir         {
122*cdf0e10cSrcweir             // get the symbol for the method creating the factory
123*cdf0e10cSrcweir             const ::rtl::OUString sFactoryCreationFunc = ::rtl::OUString::createFromAscii("createDataAccessToolsFactory");
124*cdf0e10cSrcweir             //  reinterpret_cast<createDataAccessToolsFactoryFunction> removed for gcc permissive
125*cdf0e10cSrcweir             getDbToolsClientFactoryFunction() = reinterpret_cast< createDataAccessToolsFactoryFunction >(
126*cdf0e10cSrcweir                 osl_getFunctionSymbol(getDbToolsClientModule(), sFactoryCreationFunc.pData));
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir             if (NULL == getDbToolsClientFactoryFunction())
129*cdf0e10cSrcweir             {   // did not find the symbol
130*cdf0e10cSrcweir                 OSL_ENSURE(sal_False, "SwDbtoolsClient::registerClient: could not find the symbol for creating the factory!");
131*cdf0e10cSrcweir                 osl_unloadModule(getDbToolsClientModule());
132*cdf0e10cSrcweir                 getDbToolsClientModule() = NULL;
133*cdf0e10cSrcweir             }
134*cdf0e10cSrcweir         }
135*cdf0e10cSrcweir     }
136*cdf0e10cSrcweir }
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir //--------------------------------------------------------------------
139*cdf0e10cSrcweir void SwDbtoolsClient::revokeClient()
140*cdf0e10cSrcweir {
141*cdf0e10cSrcweir     ::osl::MutexGuard aGuard(getDbtoolsClientMutex());
142*cdf0e10cSrcweir     if (0 == --getDbToolsClientClients())
143*cdf0e10cSrcweir     {
144*cdf0e10cSrcweir         getDbToolsClientFactoryFunction() = NULL;
145*cdf0e10cSrcweir         if (getDbToolsClientModule())
146*cdf0e10cSrcweir             osl_unloadModule(getDbToolsClientModule());
147*cdf0e10cSrcweir         getDbToolsClientModule() = NULL;
148*cdf0e10cSrcweir     }
149*cdf0e10cSrcweir }
150*cdf0e10cSrcweir /* -----------------------------30.08.2001 14:58------------------------------
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
153*cdf0e10cSrcweir void SwDbtoolsClient::getFactory()
154*cdf0e10cSrcweir {
155*cdf0e10cSrcweir     if(!m_xDataAccessFactory.is())
156*cdf0e10cSrcweir     {
157*cdf0e10cSrcweir         registerClient();
158*cdf0e10cSrcweir         if(getDbToolsClientFactoryFunction())
159*cdf0e10cSrcweir 		{	// loading the lib succeeded
160*cdf0e10cSrcweir             void* pUntypedFactory = (*getDbToolsClientFactoryFunction())();
161*cdf0e10cSrcweir 			IDataAccessToolsFactory* pDBTFactory = static_cast<IDataAccessToolsFactory*>(pUntypedFactory);
162*cdf0e10cSrcweir             OSL_ENSURE(pDBTFactory, "SwDbtoolsClient::SwDbtoolsClient: no factory returned!");
163*cdf0e10cSrcweir 			if (pDBTFactory)
164*cdf0e10cSrcweir 			{
165*cdf0e10cSrcweir 				m_xDataAccessFactory = pDBTFactory;
166*cdf0e10cSrcweir 				// by definition, the factory was aquired once
167*cdf0e10cSrcweir 				m_xDataAccessFactory->release();
168*cdf0e10cSrcweir 			}
169*cdf0e10cSrcweir 		}
170*cdf0e10cSrcweir     }
171*cdf0e10cSrcweir }
172*cdf0e10cSrcweir /* -----------------------------30.08.2001 11:32------------------------------
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
175*cdf0e10cSrcweir ::rtl::Reference< ::connectivity::simple::IDataAccessTools >
176*cdf0e10cSrcweir     SwDbtoolsClient::getDataAccessTools()
177*cdf0e10cSrcweir {
178*cdf0e10cSrcweir     if(!m_xDataAccessTools.is())
179*cdf0e10cSrcweir     {
180*cdf0e10cSrcweir         getFactory();
181*cdf0e10cSrcweir         if(m_xDataAccessFactory.is())
182*cdf0e10cSrcweir             m_xDataAccessTools = m_xDataAccessFactory->getDataAccessTools();
183*cdf0e10cSrcweir     }
184*cdf0e10cSrcweir     return m_xDataAccessTools;
185*cdf0e10cSrcweir }
186*cdf0e10cSrcweir /* -----------------------------30.08.2001 12:40------------------------------
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
189*cdf0e10cSrcweir ::rtl::Reference< ::connectivity::simple::IDataAccessTypeConversion >
190*cdf0e10cSrcweir     SwDbtoolsClient::getAccessTypeConversion()
191*cdf0e10cSrcweir {
192*cdf0e10cSrcweir     if(!m_xAccessTypeConversion.is())
193*cdf0e10cSrcweir     {
194*cdf0e10cSrcweir         getFactory();
195*cdf0e10cSrcweir         if(m_xDataAccessFactory.is())
196*cdf0e10cSrcweir             m_xAccessTypeConversion = m_xDataAccessFactory->getTypeConversionHelper();
197*cdf0e10cSrcweir     }
198*cdf0e10cSrcweir     return m_xAccessTypeConversion;
199*cdf0e10cSrcweir }
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir /* -----------------------------30.08.2001 11:37------------------------------
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
204*cdf0e10cSrcweir Reference< XDataSource > SwDbtoolsClient::getDataSource(
205*cdf0e10cSrcweir         const ::rtl::OUString& rRegisteredName,
206*cdf0e10cSrcweir         const Reference< XMultiServiceFactory>& xFactory
207*cdf0e10cSrcweir             )
208*cdf0e10cSrcweir {
209*cdf0e10cSrcweir     Reference< XDataSource > xRet;
210*cdf0e10cSrcweir     ::rtl::Reference< ::connectivity::simple::IDataAccessTools >    xAccess = getDataAccessTools();
211*cdf0e10cSrcweir     if(xAccess.is())
212*cdf0e10cSrcweir         xRet = xAccess->getDataSource(rRegisteredName, xFactory);
213*cdf0e10cSrcweir     return xRet;
214*cdf0e10cSrcweir }
215*cdf0e10cSrcweir /* -----------------------------30.08.2001 12:06------------------------------
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
218*cdf0e10cSrcweir sal_Int32 SwDbtoolsClient::getDefaultNumberFormat(
219*cdf0e10cSrcweir         const Reference< XPropertySet >& rxColumn,
220*cdf0e10cSrcweir         const Reference< XNumberFormatTypes >& rxTypes,
221*cdf0e10cSrcweir         const Locale& rLocale
222*cdf0e10cSrcweir             )
223*cdf0e10cSrcweir {
224*cdf0e10cSrcweir     sal_Int32 nRet = -1;
225*cdf0e10cSrcweir     ::rtl::Reference< ::connectivity::simple::IDataAccessTools >    xAccess = getDataAccessTools();
226*cdf0e10cSrcweir     if(xAccess.is())
227*cdf0e10cSrcweir         nRet = xAccess->getDefaultNumberFormat( rxColumn, rxTypes, rLocale);
228*cdf0e10cSrcweir     return nRet;
229*cdf0e10cSrcweir }
230*cdf0e10cSrcweir /* -----------------------------30.08.2001 12:38------------------------------
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir  ---------------------------------------------------------------------------*/
233*cdf0e10cSrcweir ::rtl::OUString SwDbtoolsClient::getFormattedValue(
234*cdf0e10cSrcweir         const uno::Reference< beans::XPropertySet>& _rxColumn,
235*cdf0e10cSrcweir         const uno::Reference< util::XNumberFormatter>& _rxFormatter,
236*cdf0e10cSrcweir         const lang::Locale& _rLocale,
237*cdf0e10cSrcweir         const util::Date& _rNullDate
238*cdf0e10cSrcweir             )
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir {
241*cdf0e10cSrcweir     ::rtl::Reference< ::connectivity::simple::IDataAccessTypeConversion > xConversion =
242*cdf0e10cSrcweir                     getAccessTypeConversion();
243*cdf0e10cSrcweir     rtl::OUString sRet;
244*cdf0e10cSrcweir     if(xConversion.is())
245*cdf0e10cSrcweir         sRet = xConversion->getFormattedValue(_rxColumn, _rxFormatter, _rLocale, _rNullDate);
246*cdf0e10cSrcweir     return sRet;
247*cdf0e10cSrcweir }
248*cdf0e10cSrcweir 
249