LibreOffice Module connectivity (master) 1
Driver.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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 "Connection.hxx"
21#include "Driver.hxx"
22#include "SubComponent.hxx"
23
25#include <strings.hrc>
27
30#include <osl/file.hxx>
31#include <osl/process.h>
32#include <rtl/bootstrap.hxx>
33#include <sal/log.hxx>
34
35using namespace com::sun::star;
36using namespace com::sun::star::uno;
37using namespace com::sun::star::lang;
38using namespace com::sun::star::beans;
39using namespace com::sun::star::sdbc;
40using namespace com::sun::star::sdbcx;
41
42using namespace ::osl;
43
44using namespace connectivity::firebird;
45
46// Static const variables
47namespace {
48constexpr OUStringLiteral our_sFirebirdTmpVar = u"FIREBIRD_TMP";
49constexpr OUStringLiteral our_sFirebirdLockVar = u"FIREBIRD_LOCK";
50constexpr OUStringLiteral our_sFirebirdMsgVar = u"FIREBIRD_MSG";
51#ifdef MACOSX
52constexpr OUStringLiteral our_sFirebirdLibVar = u"LIBREOFFICE_FIREBIRD_LIB";
53#endif
54};
55
56FirebirdDriver::FirebirdDriver(const css::uno::Reference< css::uno::XComponentContext >& _rxContext)
58 , m_aContext(_rxContext)
59 , m_firebirdTMPDirectory(nullptr, true)
60 , m_firebirdLockDirectory(nullptr, true)
61{
62 // ::utl::TempFile uses a unique temporary directory (subdirectory of
63 // /tmp or other user specific tmp directory) per instance in which
64 // we can create directories for firebird at will.
67
68 // Overrides firebird's default of /tmp or c:\temp
69 osl_setEnvironment(OUString(our_sFirebirdTmpVar).pData, m_firebirdTMPDirectory.GetFileName().pData);
70
71 // Overrides firebird's default of /tmp/firebird or c:\temp\firebird
72 osl_setEnvironment(OUString(our_sFirebirdLockVar).pData, m_firebirdLockDirectory.GetFileName().pData);
73
74#ifndef SYSTEM_FIREBIRD
75 // Overrides firebird's hardcoded default of /usr/local/firebird on *nix,
76 // however on Windows it seems to use the current directory as a default.
77 OUString sMsgURL("$BRAND_BASE_DIR/$BRAND_SHARE_SUBDIR/firebird");
78 ::rtl::Bootstrap::expandMacros(sMsgURL);
79 OUString sMsgPath;
80 ::osl::FileBase::getSystemPathFromFileURL(sMsgURL, sMsgPath);
81 osl_setEnvironment(OUString(our_sFirebirdMsgVar).pData, sMsgPath.pData);
82#ifdef MACOSX
83 // Set an env. variable to specify library location
84 // for dlopen used in fbclient.
85 OUString sLibURL("$LO_LIB_DIR");
86 ::rtl::Bootstrap::expandMacros(sLibURL);
87 OUString sLibPath;
88 ::osl::FileBase::getSystemPathFromFileURL(sLibURL, sLibPath);
89 osl_setEnvironment(OUString(our_sFirebirdLibVar).pData, sLibPath.pData);
90#endif /*MACOSX*/
91#endif
92}
93
95
97{
98 MutexGuard aGuard(m_aMutex);
99
100 for (auto const& elem : m_xConnections)
101 {
102 Reference< XComponent > xComp(elem.get(), UNO_QUERY);
103 if (xComp.is())
104 xComp->dispose();
105 }
106 m_xConnections.clear();
107
108 osl_clearEnvironment(OUString(our_sFirebirdTmpVar).pData);
109 osl_clearEnvironment(OUString(our_sFirebirdLockVar).pData);
110
111#ifndef SYSTEM_FIREBIRD
112 osl_clearEnvironment(OUString(our_sFirebirdMsgVar).pData);
113#ifdef MACOSX
114 osl_clearEnvironment(OUString(our_sFirebirdLibVar).pData);
115#endif /*MACOSX*/
116#endif
118 OSL_VERIFY(fb_shutdown(0, 1));
119
120 ODriver_BASE::disposing();
121}
122
124{
125 return "com.sun.star.comp.sdbc.firebird.Driver";
126}
127
128sal_Bool SAL_CALL FirebirdDriver::supportsService(const OUString& _rServiceName)
129{
130 return cppu::supportsService(this, _rServiceName);
131}
132
134{
135 return { "com.sun.star.sdbc.Driver", "com.sun.star.sdbcx.Driver" };
136}
137
138// ---- XDriver -------------------------------------------------------------
140 const OUString& url, const Sequence< PropertyValue >& info )
141{
142 SAL_INFO("connectivity.firebird", "connect(), URL: " << url );
143
144 MutexGuard aGuard( m_aMutex );
145 if (ODriver_BASE::rBHelper.bDisposed)
146 throw DisposedException();
147
148 if ( ! acceptsURL(url) )
149 return nullptr;
150
152 pCon->construct(url, info);
153
154 m_xConnections.push_back(WeakReferenceHelper(*pCon));
155
156 return pCon;
157}
158
159sal_Bool SAL_CALL FirebirdDriver::acceptsURL( const OUString& url )
160{
161 return (url == "sdbc:embedded:firebird" || url.startsWith("sdbc:firebird:"));
162}
163
165 const OUString& url, const Sequence< PropertyValue >& )
166{
167 if ( ! acceptsURL(url) )
168 {
170 const OUString sMessage = aResources.getResourceString(STR_URI_SYNTAX_ERROR);
172 }
173
175}
176
178{
179 // The major and minor version are sdbc driver specific. Must begin with 1.0
180 // as per https://api.libreoffice.org/docs/common/ref/com/sun/star/sdbc/XDriver.html
181 return 1;
182}
183
185{
186 return 0;
187}
188
189//----- XDataDefinitionSupplier
190uno::Reference< XTablesSupplier > SAL_CALL FirebirdDriver::getDataDefinitionByConnection(
191 const uno::Reference< XConnection >& rConnection)
192{
193 if (Connection* pConnection = comphelper::getFromUnoTunnel<Connection>(rConnection))
194 return pConnection->createCatalog();
195 return {};
196}
197
198uno::Reference< XTablesSupplier > SAL_CALL FirebirdDriver::getDataDefinitionByURL(
199 const OUString& rURL,
200 const uno::Sequence< PropertyValue >& rInfo)
201{
202 uno::Reference< XConnection > xConnection = connect(rURL, rInfo);
203 return getDataDefinitionByConnection(xConnection);
204}
205
207{
208 void checkDisposed(bool _bThrow)
209 {
210 if (_bThrow)
211 throw DisposedException();
212
213 }
214
215} // namespace
216
217extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
219 css::uno::XComponentContext* context , css::uno::Sequence<css::uno::Any> const&)
220{
221 try {
222 return cppu::acquire(new FirebirdDriver(context));
223 } catch (...) {
224 return nullptr;
225 }
226}
227
228/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * connectivity_FirebirdDriver_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
Definition: Driver.cxx:218
helper class for accessing resources shared by different libraries in the connectivity module
OUString getResourceString(TranslateId pResId) const
loads a string from the shared resource file
virtual css::uno::Sequence< css::sdbc::DriverPropertyInfo > SAL_CALL getPropertyInfo(const OUString &url, const css::uno::Sequence< css::beans::PropertyValue > &info) override
Definition: Driver.cxx:164
virtual OUString SAL_CALL getImplementationName() override
Definition: Driver.cxx:123
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: Driver.cxx:133
virtual void SAL_CALL disposing() override
Definition: Driver.cxx:96
virtual css::uno::Reference< css::sdbc::XConnection > SAL_CALL connect(const OUString &url, const css::uno::Sequence< css::beans::PropertyValue > &info) override
Definition: Driver.cxx:139
virtual css::uno::Reference< css::sdbcx::XTablesSupplier > SAL_CALL getDataDefinitionByURL(const OUString &rsURL, const css::uno::Sequence< css::beans::PropertyValue > &rInfo) override
Definition: Driver.cxx:198
virtual sal_Bool SAL_CALL acceptsURL(const OUString &url) override
Definition: Driver.cxx:159
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
Definition: Driver.cxx:128
virtual sal_Int32 SAL_CALL getMajorVersion() override
Definition: Driver.cxx:177
virtual css::uno::Reference< css::sdbcx::XTablesSupplier > SAL_CALL getDataDefinitionByConnection(const css::uno::Reference< css::sdbc::XConnection > &rxConnection) override
Definition: Driver.cxx:190
virtual sal_Int32 SAL_CALL getMinorVersion() override
Definition: Driver.cxx:184
void EnableKillingFile(bool bEnable=true)
OUString GetFileName() const
Reference< XComponentContext > m_aContext
float u
std::mutex m_aMutex
#define SAL_INFO(area, stream)
std::unique_ptr< sal_Int32[]> pData
void checkDisposed(bool _bThrow)
Definition: Driver.cxx:208
::cppu::WeakComponentImplHelper< css::sdbc::XDriver, css::sdbcx::XDataDefinitionSupplier, css::lang::XServiceInfo > ODriver_BASE
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
void throwGenericSQLException(const OUString &_rMsg, const css::uno::Reference< css::uno::XInterface > &_rxSource)
throw a generic SQLException, i.e.
OUString sMessage
unsigned char sal_Bool