Update ooo320-m1
[ooovba.git] / sw / source / ui / dbui / swdbtoolsclient.cxx
blobbcaf8ae973cbdec9b96d5c253eeaccc7740d07af
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: swdbtoolsclient.cxx,v $
10 * $Revision: 1.9 $
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_sw.hxx"
33 #include <com/sun/star/sdbc/XConnection.hpp>
34 #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
35 #include <com/sun/star/sdbc/XDataSource.hpp>
36 #include <com/sun/star/sdb/SQLContext.hpp>
37 #include <swdbtoolsclient.hxx>
38 #include <osl/diagnose.h>
39 #include <tools/solar.h>
41 //........................................................................
43 using namespace ::connectivity::simple;
44 using namespace ::com::sun::star;
45 using namespace ::com::sun::star::sdbc;
46 using namespace ::com::sun::star::lang;
47 using namespace ::com::sun::star::util;
48 using namespace ::com::sun::star::uno;
49 using namespace ::com::sun::star::beans;
50 using namespace ::com::sun::star::sdb;
52 //====================================================================
53 //= SwDbtoolsClient
54 //====================================================================
55 namespace
57 // -----------------------------------------------------------------------------
58 // this namespace contains access to all static members of the class SwDbtoolsClient
59 // to make the initialize of the dll a little bit faster
60 // -----------------------------------------------------------------------------
61 ::osl::Mutex& getDbtoolsClientMutex()
63 static ::osl::Mutex aMutex;
64 return aMutex;
66 // -----------------------------------------------------------------------------
67 sal_Int32& getDbToolsClientClients()
69 static sal_Int32 nClients = 0;
70 return nClients;
72 // -----------------------------------------------------------------------------
73 oslModule& getDbToolsClientModule()
75 static oslModule hDbtoolsModule = NULL;
76 return hDbtoolsModule;
78 // -----------------------------------------------------------------------------
79 createDataAccessToolsFactoryFunction& getDbToolsClientFactoryFunction()
81 static createDataAccessToolsFactoryFunction pFactoryCreationFunc = NULL;
82 return pFactoryCreationFunc;
84 // -----------------------------------------------------------------------------
86 // -----------------------------------------------------------------------------
87 SwDbtoolsClient::SwDbtoolsClient()
91 //--------------------------------------------------------------------
92 SwDbtoolsClient::~SwDbtoolsClient()
94 if(m_xDataAccessFactory.is())
96 // clear the factory _before_ revoking the client
97 // (the revocation may unload the DBT lib)
98 m_xDataAccessFactory = NULL;
99 // revoke the client
100 revokeClient();
104 //--------------------------------------------------------------------
105 extern "C" { static void SAL_CALL thisModule() {} }
107 void SwDbtoolsClient::registerClient()
109 ::osl::MutexGuard aGuard(getDbtoolsClientMutex());
110 if (1 == ++getDbToolsClientClients())
112 OSL_ENSURE(NULL == getDbToolsClientModule(), "SwDbtoolsClient::registerClient: inconsistence: already have a module!");
113 OSL_ENSURE(NULL == getDbToolsClientFactoryFunction(), "SwDbtoolsClient::registerClient: inconsistence: already have a factory function!");
115 const ::rtl::OUString sModuleName = ::rtl::OUString::createFromAscii(
116 SVLIBRARY( "dbtools" )
119 // load the dbtools library
120 getDbToolsClientModule() = osl_loadModuleRelative(
121 &thisModule, sModuleName.pData, 0);
122 OSL_ENSURE(NULL != getDbToolsClientModule(), "SwDbtoolsClient::registerClient: could not load the dbtools library!");
123 if (NULL != getDbToolsClientModule())
125 // get the symbol for the method creating the factory
126 const ::rtl::OUString sFactoryCreationFunc = ::rtl::OUString::createFromAscii("createDataAccessToolsFactory");
127 // reinterpret_cast<createDataAccessToolsFactoryFunction> removed for gcc permissive
128 getDbToolsClientFactoryFunction() = reinterpret_cast< createDataAccessToolsFactoryFunction >(
129 osl_getFunctionSymbol(getDbToolsClientModule(), sFactoryCreationFunc.pData));
131 if (NULL == getDbToolsClientFactoryFunction())
132 { // did not find the symbol
133 OSL_ENSURE(sal_False, "SwDbtoolsClient::registerClient: could not find the symbol for creating the factory!");
134 osl_unloadModule(getDbToolsClientModule());
135 getDbToolsClientModule() = NULL;
141 //--------------------------------------------------------------------
142 void SwDbtoolsClient::revokeClient()
144 ::osl::MutexGuard aGuard(getDbtoolsClientMutex());
145 if (0 == --getDbToolsClientClients())
147 getDbToolsClientFactoryFunction() = NULL;
148 if (getDbToolsClientModule())
149 osl_unloadModule(getDbToolsClientModule());
150 getDbToolsClientModule() = NULL;
153 /* -----------------------------30.08.2001 14:58------------------------------
155 ---------------------------------------------------------------------------*/
156 void SwDbtoolsClient::getFactory()
158 if(!m_xDataAccessFactory.is())
160 registerClient();
161 if(getDbToolsClientFactoryFunction())
162 { // loading the lib succeeded
163 void* pUntypedFactory = (*getDbToolsClientFactoryFunction())();
164 IDataAccessToolsFactory* pDBTFactory = static_cast<IDataAccessToolsFactory*>(pUntypedFactory);
165 OSL_ENSURE(pDBTFactory, "SwDbtoolsClient::SwDbtoolsClient: no factory returned!");
166 if (pDBTFactory)
168 m_xDataAccessFactory = pDBTFactory;
169 // by definition, the factory was aquired once
170 m_xDataAccessFactory->release();
175 /* -----------------------------30.08.2001 11:32------------------------------
177 ---------------------------------------------------------------------------*/
178 ::rtl::Reference< ::connectivity::simple::IDataAccessTools >
179 SwDbtoolsClient::getDataAccessTools()
181 if(!m_xDataAccessTools.is())
183 getFactory();
184 if(m_xDataAccessFactory.is())
185 m_xDataAccessTools = m_xDataAccessFactory->getDataAccessTools();
187 return m_xDataAccessTools;
189 /* -----------------------------30.08.2001 12:40------------------------------
191 ---------------------------------------------------------------------------*/
192 ::rtl::Reference< ::connectivity::simple::IDataAccessTypeConversion >
193 SwDbtoolsClient::getAccessTypeConversion()
195 if(!m_xAccessTypeConversion.is())
197 getFactory();
198 if(m_xDataAccessFactory.is())
199 m_xAccessTypeConversion = m_xDataAccessFactory->getTypeConversionHelper();
201 return m_xAccessTypeConversion;
204 /* -----------------------------30.08.2001 11:37------------------------------
206 ---------------------------------------------------------------------------*/
207 Reference< XDataSource > SwDbtoolsClient::getDataSource(
208 const ::rtl::OUString& rRegisteredName,
209 const Reference< XMultiServiceFactory>& xFactory
212 Reference< XDataSource > xRet;
213 ::rtl::Reference< ::connectivity::simple::IDataAccessTools > xAccess = getDataAccessTools();
214 if(xAccess.is())
215 xRet = xAccess->getDataSource(rRegisteredName, xFactory);
216 return xRet;
218 /* -----------------------------30.08.2001 12:06------------------------------
220 ---------------------------------------------------------------------------*/
221 sal_Int32 SwDbtoolsClient::getDefaultNumberFormat(
222 const Reference< XPropertySet >& rxColumn,
223 const Reference< XNumberFormatTypes >& rxTypes,
224 const Locale& rLocale
227 sal_Int32 nRet = -1;
228 ::rtl::Reference< ::connectivity::simple::IDataAccessTools > xAccess = getDataAccessTools();
229 if(xAccess.is())
230 nRet = xAccess->getDefaultNumberFormat( rxColumn, rxTypes, rLocale);
231 return nRet;
233 /* -----------------------------30.08.2001 12:38------------------------------
235 ---------------------------------------------------------------------------*/
236 ::rtl::OUString SwDbtoolsClient::getValue(
237 const uno::Reference< beans::XPropertySet>& _rxColumn,
238 const uno::Reference< util::XNumberFormatter>& _rxFormatter,
239 const lang::Locale& _rLocale,
240 const util::Date& _rNullDate
244 ::rtl::Reference< ::connectivity::simple::IDataAccessTypeConversion > xConversion =
245 getAccessTypeConversion();
246 rtl::OUString sRet;
247 if(xConversion.is())
248 sRet = xConversion->getValue(_rxColumn, _rxFormatter, _rLocale, _rNullDate);
249 return sRet;