Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <com/sun/star/sdbc/XConnection.hpp>
21 : #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
22 : #include <com/sun/star/sdbc/XDataSource.hpp>
23 : #include <com/sun/star/sdb/SQLContext.hpp>
24 : #include <swdbtoolsclient.hxx>
25 : #include <osl/diagnose.h>
26 : #include <tools/solar.h>
27 :
28 : using namespace ::connectivity::simple;
29 : using namespace ::com::sun::star;
30 : using namespace ::com::sun::star::sdbc;
31 : using namespace ::com::sun::star::lang;
32 : using namespace ::com::sun::star::util;
33 : using namespace ::com::sun::star::uno;
34 : using namespace ::com::sun::star::beans;
35 : using namespace ::com::sun::star::sdb;
36 :
37 : //====================================================================
38 : //= SwDbtoolsClient
39 : //====================================================================
40 : namespace
41 : {
42 : // -----------------------------------------------------------------------------
43 : // this namespace contains access to all static members of the class SwDbtoolsClient
44 : // to make the initialize of the dll a little bit faster
45 : // -----------------------------------------------------------------------------
46 0 : ::osl::Mutex& getDbtoolsClientMutex()
47 : {
48 0 : static ::osl::Mutex aMutex;
49 0 : return aMutex;
50 : }
51 : // -----------------------------------------------------------------------------
52 0 : sal_Int32& getDbToolsClientClients()
53 : {
54 : static sal_Int32 nClients = 0;
55 0 : return nClients;
56 : }
57 : // -----------------------------------------------------------------------------
58 0 : oslModule& getDbToolsClientModule()
59 : {
60 : static oslModule hDbtoolsModule = NULL;
61 0 : return hDbtoolsModule;
62 : }
63 : // -----------------------------------------------------------------------------
64 0 : createDataAccessToolsFactoryFunction& getDbToolsClientFactoryFunction()
65 : {
66 : static createDataAccessToolsFactoryFunction pFactoryCreationFunc = NULL;
67 0 : return pFactoryCreationFunc;
68 : }
69 : // -----------------------------------------------------------------------------
70 : }
71 :
72 0 : SwDbtoolsClient::SwDbtoolsClient()
73 : {
74 0 : }
75 :
76 0 : SwDbtoolsClient::~SwDbtoolsClient()
77 : {
78 0 : if(m_xDataAccessFactory.is())
79 : {
80 : // clear the factory _before_ revoking the client
81 : // (the revocation may unload the DBT lib)
82 0 : m_xDataAccessFactory = NULL;
83 : // revoke the client
84 0 : revokeClient();
85 : }
86 0 : }
87 :
88 : #ifndef DISABLE_DYNLOADING
89 :
90 0 : extern "C" { static void SAL_CALL thisModule() {} }
91 :
92 : #else
93 :
94 : extern "C" void * createDataAccessToolsFactory();
95 :
96 : #endif
97 :
98 0 : void SwDbtoolsClient::registerClient()
99 : {
100 0 : ::osl::MutexGuard aGuard(getDbtoolsClientMutex());
101 0 : if (1 == ++getDbToolsClientClients())
102 : {
103 : OSL_ENSURE(NULL == getDbToolsClientModule(), "SwDbtoolsClient::registerClient: inconsistence: already have a module!");
104 : OSL_ENSURE(NULL == getDbToolsClientFactoryFunction(), "SwDbtoolsClient::registerClient: inconsistence: already have a factory function!");
105 :
106 : #ifndef DISABLE_DYNLOADING
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 : #else
129 : getDbToolsClientFactoryFunction() = createDataAccessToolsFactory;
130 : #endif
131 0 : }
132 0 : }
133 :
134 0 : void SwDbtoolsClient::revokeClient()
135 : {
136 0 : ::osl::MutexGuard aGuard(getDbtoolsClientMutex());
137 0 : if (0 == --getDbToolsClientClients())
138 : {
139 : #ifndef DISABLE_DYNLOADING
140 0 : getDbToolsClientFactoryFunction() = NULL;
141 0 : if (getDbToolsClientModule())
142 0 : osl_unloadModule(getDbToolsClientModule());
143 : #endif
144 0 : getDbToolsClientModule() = NULL;
145 0 : }
146 0 : }
147 :
148 0 : void SwDbtoolsClient::getFactory()
149 : {
150 0 : if(!m_xDataAccessFactory.is())
151 : {
152 0 : registerClient();
153 0 : if(getDbToolsClientFactoryFunction())
154 : { // loading the lib succeeded
155 0 : void* pUntypedFactory = (*getDbToolsClientFactoryFunction())();
156 0 : IDataAccessToolsFactory* pDBTFactory = static_cast<IDataAccessToolsFactory*>(pUntypedFactory);
157 : OSL_ENSURE(pDBTFactory, "SwDbtoolsClient::SwDbtoolsClient: no factory returned!");
158 0 : if (pDBTFactory)
159 : {
160 0 : m_xDataAccessFactory = pDBTFactory;
161 : // by definition, the factory was aquired once
162 0 : m_xDataAccessFactory->release();
163 : }
164 : }
165 : }
166 0 : }
167 :
168 : ::rtl::Reference< ::connectivity::simple::IDataAccessTools >
169 0 : SwDbtoolsClient::getDataAccessTools()
170 : {
171 0 : if(!m_xDataAccessTools.is())
172 : {
173 0 : getFactory();
174 0 : if(m_xDataAccessFactory.is())
175 0 : m_xDataAccessTools = m_xDataAccessFactory->getDataAccessTools();
176 : }
177 0 : return m_xDataAccessTools;
178 : }
179 :
180 : ::rtl::Reference< ::connectivity::simple::IDataAccessTypeConversion >
181 0 : SwDbtoolsClient::getAccessTypeConversion()
182 : {
183 0 : if(!m_xAccessTypeConversion.is())
184 : {
185 0 : getFactory();
186 0 : if(m_xDataAccessFactory.is())
187 0 : m_xAccessTypeConversion = m_xDataAccessFactory->getTypeConversionHelper();
188 : }
189 0 : return m_xAccessTypeConversion;
190 : }
191 :
192 0 : Reference< XDataSource > SwDbtoolsClient::getDataSource(
193 : const ::rtl::OUString& rRegisteredName,
194 : const Reference<XComponentContext>& rxContext
195 : )
196 : {
197 0 : Reference< XDataSource > xRet;
198 0 : ::rtl::Reference< ::connectivity::simple::IDataAccessTools > xAccess = getDataAccessTools();
199 0 : if(xAccess.is())
200 0 : xRet = xAccess->getDataSource(rRegisteredName, rxContext);
201 0 : return xRet;
202 : }
203 :
204 0 : sal_Int32 SwDbtoolsClient::getDefaultNumberFormat(
205 : const Reference< XPropertySet >& rxColumn,
206 : const Reference< XNumberFormatTypes >& rxTypes,
207 : const Locale& rLocale
208 : )
209 : {
210 0 : sal_Int32 nRet = -1;
211 0 : ::rtl::Reference< ::connectivity::simple::IDataAccessTools > xAccess = getDataAccessTools();
212 0 : if(xAccess.is())
213 0 : nRet = xAccess->getDefaultNumberFormat( rxColumn, rxTypes, rLocale);
214 0 : return nRet;
215 : }
216 :
217 0 : ::rtl::OUString SwDbtoolsClient::getFormattedValue(
218 : const uno::Reference< beans::XPropertySet>& _rxColumn,
219 : const uno::Reference< util::XNumberFormatter>& _rxFormatter,
220 : const lang::Locale& _rLocale,
221 : const util::Date& _rNullDate
222 : )
223 :
224 : {
225 : ::rtl::Reference< ::connectivity::simple::IDataAccessTypeConversion > xConversion =
226 0 : getAccessTypeConversion();
227 0 : rtl::OUString sRet;
228 0 : if(xConversion.is())
229 0 : sRet = xConversion->getFormattedValue(_rxColumn, _rxFormatter, _rLocale, _rNullDate);
230 0 : return sRet;
231 : }
232 :
233 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|