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