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 : //= SwDbtoolsClient
38 :
39 : namespace
40 : {
41 :
42 : // this namespace contains access to all static members of the class SwDbtoolsClient
43 : // to make the initialize of the dll a little bit faster
44 :
45 0 : ::osl::Mutex& getDbtoolsClientMutex()
46 : {
47 0 : static ::osl::Mutex aMutex;
48 0 : return aMutex;
49 : }
50 :
51 0 : sal_Int32& getDbToolsClientClients()
52 : {
53 : static sal_Int32 nClients = 0;
54 0 : return nClients;
55 : }
56 :
57 0 : oslModule& getDbToolsClientModule()
58 : {
59 : static oslModule hDbtoolsModule = NULL;
60 0 : return hDbtoolsModule;
61 : }
62 :
63 0 : createDataAccessToolsFactoryFunction& getDbToolsClientFactoryFunction()
64 : {
65 : static createDataAccessToolsFactoryFunction pFactoryCreationFunc = NULL;
66 0 : return pFactoryCreationFunc;
67 : }
68 :
69 : }
70 :
71 0 : SwDbtoolsClient::SwDbtoolsClient()
72 : {
73 0 : }
74 :
75 0 : SwDbtoolsClient::~SwDbtoolsClient()
76 : {
77 0 : if(m_xDataAccessFactory.is())
78 : {
79 : // clear the factory _before_ revoking the client
80 : // (the revocation may unload the DBT lib)
81 0 : m_xDataAccessFactory = NULL;
82 : // revoke the client
83 0 : revokeClient();
84 : }
85 0 : }
86 :
87 : #ifndef DISABLE_DYNLOADING
88 :
89 0 : extern "C" { static void SAL_CALL thisModule() {} }
90 :
91 : #else
92 :
93 : extern "C" void * createDataAccessToolsFactory();
94 :
95 : #endif
96 :
97 0 : void SwDbtoolsClient::registerClient()
98 : {
99 0 : ::osl::MutexGuard aGuard(getDbtoolsClientMutex());
100 0 : if (1 == ++getDbToolsClientClients())
101 : {
102 : OSL_ENSURE(NULL == getDbToolsClientModule(), "SwDbtoolsClient::registerClient: inconsistence: already have a module!");
103 : OSL_ENSURE(NULL == getDbToolsClientFactoryFunction(), "SwDbtoolsClient::registerClient: inconsistence: already have a factory function!");
104 :
105 : #ifndef DISABLE_DYNLOADING
106 0 : const OUString sModuleName(SVLIBRARY("dbtools"));
107 :
108 : // load the dbtools library
109 0 : getDbToolsClientModule() = osl_loadModuleRelative(
110 0 : &thisModule, sModuleName.pData, 0);
111 : OSL_ENSURE(NULL != getDbToolsClientModule(), "SwDbtoolsClient::registerClient: could not load the dbtools library!");
112 0 : if (NULL != getDbToolsClientModule())
113 : {
114 : // get the symbol for the method creating the factory
115 0 : const OUString sFactoryCreationFunc("createDataAccessToolsFactory");
116 : // reinterpret_cast<createDataAccessToolsFactoryFunction> removed for gcc permissive
117 0 : getDbToolsClientFactoryFunction() = reinterpret_cast< createDataAccessToolsFactoryFunction >(
118 0 : osl_getFunctionSymbol(getDbToolsClientModule(), sFactoryCreationFunc.pData));
119 :
120 0 : if (NULL == getDbToolsClientFactoryFunction())
121 : { // did not find the symbol
122 : OSL_FAIL("SwDbtoolsClient::registerClient: could not find the symbol for creating the factory!");
123 0 : osl_unloadModule(getDbToolsClientModule());
124 0 : getDbToolsClientModule() = NULL;
125 0 : }
126 0 : }
127 : #else
128 : getDbToolsClientFactoryFunction() = createDataAccessToolsFactory;
129 : #endif
130 0 : }
131 0 : }
132 :
133 0 : void SwDbtoolsClient::revokeClient()
134 : {
135 0 : ::osl::MutexGuard aGuard(getDbtoolsClientMutex());
136 0 : if (0 == --getDbToolsClientClients())
137 : {
138 : #ifndef DISABLE_DYNLOADING
139 0 : getDbToolsClientFactoryFunction() = NULL;
140 0 : if (getDbToolsClientModule())
141 0 : osl_unloadModule(getDbToolsClientModule());
142 : #endif
143 0 : getDbToolsClientModule() = NULL;
144 0 : }
145 0 : }
146 :
147 0 : void SwDbtoolsClient::getFactory()
148 : {
149 0 : if(!m_xDataAccessFactory.is())
150 : {
151 0 : registerClient();
152 0 : if(getDbToolsClientFactoryFunction())
153 : { // loading the lib succeeded
154 0 : void* pUntypedFactory = (*getDbToolsClientFactoryFunction())();
155 0 : IDataAccessToolsFactory* pDBTFactory = static_cast<IDataAccessToolsFactory*>(pUntypedFactory);
156 : OSL_ENSURE(pDBTFactory, "SwDbtoolsClient::SwDbtoolsClient: no factory returned!");
157 0 : if (pDBTFactory)
158 : {
159 0 : m_xDataAccessFactory = pDBTFactory;
160 : // by definition, the factory was aquired once
161 0 : m_xDataAccessFactory->release();
162 : }
163 : }
164 : }
165 0 : }
166 :
167 : ::rtl::Reference< ::connectivity::simple::IDataAccessTools >
168 0 : SwDbtoolsClient::getDataAccessTools()
169 : {
170 0 : if(!m_xDataAccessTools.is())
171 : {
172 0 : getFactory();
173 0 : if(m_xDataAccessFactory.is())
174 0 : m_xDataAccessTools = m_xDataAccessFactory->getDataAccessTools();
175 : }
176 0 : return m_xDataAccessTools;
177 : }
178 :
179 : ::rtl::Reference< ::connectivity::simple::IDataAccessTypeConversion >
180 0 : SwDbtoolsClient::getAccessTypeConversion()
181 : {
182 0 : if(!m_xAccessTypeConversion.is())
183 : {
184 0 : getFactory();
185 0 : if(m_xDataAccessFactory.is())
186 0 : m_xAccessTypeConversion = m_xDataAccessFactory->getTypeConversionHelper();
187 : }
188 0 : return m_xAccessTypeConversion;
189 : }
190 :
191 0 : Reference< XDataSource > SwDbtoolsClient::getDataSource(
192 : const OUString& rRegisteredName,
193 : const Reference<XComponentContext>& rxContext
194 : )
195 : {
196 0 : Reference< XDataSource > xRet;
197 0 : ::rtl::Reference< ::connectivity::simple::IDataAccessTools > xAccess = getDataAccessTools();
198 0 : if(xAccess.is())
199 0 : xRet = xAccess->getDataSource(rRegisteredName, rxContext);
200 0 : return xRet;
201 : }
202 :
203 0 : sal_Int32 SwDbtoolsClient::getDefaultNumberFormat(
204 : const Reference< XPropertySet >& rxColumn,
205 : const Reference< XNumberFormatTypes >& rxTypes,
206 : const lang::Locale& rLocale
207 : )
208 : {
209 0 : sal_Int32 nRet = -1;
210 0 : ::rtl::Reference< ::connectivity::simple::IDataAccessTools > xAccess = getDataAccessTools();
211 0 : if(xAccess.is())
212 0 : nRet = xAccess->getDefaultNumberFormat( rxColumn, rxTypes, rLocale);
213 0 : return nRet;
214 : }
215 :
216 0 : OUString SwDbtoolsClient::getFormattedValue(
217 : const uno::Reference< beans::XPropertySet>& _rxColumn,
218 : const uno::Reference< util::XNumberFormatter>& _rxFormatter,
219 : const lang::Locale& _rLocale,
220 : const util::Date& _rNullDate
221 : )
222 :
223 : {
224 : ::rtl::Reference< ::connectivity::simple::IDataAccessTypeConversion > xConversion =
225 0 : getAccessTypeConversion();
226 0 : OUString sRet;
227 0 : if(xConversion.is())
228 0 : sRet = xConversion->getFormattedValue(_rxColumn, _rxFormatter, _rLocale, _rNullDate);
229 0 : return sRet;
230 : }
231 :
232 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|