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