1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "Connection.hxx"
22 #include "SubComponent.hxx"
24 #include <connectivity/dbexception.hxx>
25 #include <strings.hrc>
26 #include <resource/sharedresources.hxx>
28 #include <comphelper/servicehelper.hxx>
29 #include <cppuhelper/supportsservice.hxx>
30 #include <osl/file.hxx>
31 #include <osl/process.h>
32 #include <rtl/bootstrap.hxx>
33 #include <sal/log.hxx>
35 using namespace com::sun::star
;
36 using namespace com::sun::star::uno
;
37 using namespace com::sun::star::lang
;
38 using namespace com::sun::star::beans
;
39 using namespace com::sun::star::sdbc
;
40 using namespace com::sun::star::sdbcx
;
42 using namespace ::osl
;
44 using namespace connectivity::firebird
;
46 // Static const variables
48 constexpr OUString our_sFirebirdTmpVar
= u
"FIREBIRD_TMP"_ustr
;
49 constexpr OUString our_sFirebirdLockVar
= u
"FIREBIRD_LOCK"_ustr
;
50 constexpr OUString our_sFirebirdMsgVar
= u
"FIREBIRD_MSG"_ustr
;
52 constexpr OUString our_sFirebirdLibVar
= u
"LIBREOFFICE_FIREBIRD_LIB"_ustr
;
56 FirebirdDriver::FirebirdDriver(const css::uno::Reference
< css::uno::XComponentContext
>& _rxContext
)
57 : ODriver_BASE(m_aMutex
)
58 , m_aContext(_rxContext
)
59 , m_firebirdTMPDirectory(nullptr, true)
60 , m_firebirdLockDirectory(nullptr, true)
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.
65 m_firebirdTMPDirectory
.EnableKillingFile(true);
66 m_firebirdLockDirectory
.EnableKillingFile(true);
68 // Overrides firebird's default of /tmp or c:\temp
69 osl_setEnvironment(our_sFirebirdTmpVar
.pData
, m_firebirdTMPDirectory
.GetFileName().pData
);
71 // Overrides firebird's default of /tmp/firebird or c:\temp\firebird
72 osl_setEnvironment(our_sFirebirdLockVar
.pData
, m_firebirdLockDirectory
.GetFileName().pData
);
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(u
"$BRAND_BASE_DIR/$BRAND_SHARE_SUBDIR/firebird"_ustr
);
78 ::rtl::Bootstrap::expandMacros(sMsgURL
);
80 ::osl::FileBase::getSystemPathFromFileURL(sMsgURL
, sMsgPath
);
81 osl_setEnvironment(our_sFirebirdMsgVar
.pData
, sMsgPath
.pData
);
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
);
88 ::osl::FileBase::getSystemPathFromFileURL(sLibURL
, sLibPath
);
89 osl_setEnvironment(our_sFirebirdLibVar
.pData
, sLibPath
.pData
);
91 #endif /*!SYSTEM_FIREBIRD*/
94 FirebirdDriver::~FirebirdDriver() = default;
96 void FirebirdDriver::disposing()
98 MutexGuard
aGuard(m_aMutex
);
100 for (auto const& elem
: m_xConnections
)
102 Reference
< XComponent
> xComp(elem
.get(), UNO_QUERY
);
106 m_xConnections
.clear();
108 osl_clearEnvironment(our_sFirebirdTmpVar
.pData
);
109 osl_clearEnvironment(our_sFirebirdLockVar
.pData
);
111 #ifndef SYSTEM_FIREBIRD
112 osl_clearEnvironment(our_sFirebirdMsgVar
.pData
);
114 osl_clearEnvironment(our_sFirebirdLibVar
.pData
);
116 #endif /*!SYSTEM_FIREBIRD*/
118 OSL_VERIFY(fb_shutdown(0, 1));
120 ODriver_BASE::disposing();
123 OUString SAL_CALL
FirebirdDriver::getImplementationName()
125 return u
"com.sun.star.comp.sdbc.firebird.Driver"_ustr
;
128 sal_Bool SAL_CALL
FirebirdDriver::supportsService(const OUString
& _rServiceName
)
130 return cppu::supportsService(this, _rServiceName
);
133 Sequence
< OUString
> SAL_CALL
FirebirdDriver::getSupportedServiceNames()
135 return { u
"com.sun.star.sdbc.Driver"_ustr
, u
"com.sun.star.sdbcx.Driver"_ustr
};
138 // ---- XDriver -------------------------------------------------------------
139 Reference
< XConnection
> SAL_CALL
FirebirdDriver::connect(
140 const OUString
& url
, const Sequence
< PropertyValue
>& info
)
142 SAL_INFO("connectivity.firebird", "connect(), URL: " << url
);
144 MutexGuard
aGuard( m_aMutex
);
145 if (ODriver_BASE::rBHelper
.bDisposed
)
146 throw DisposedException();
148 if ( ! acceptsURL(url
) )
151 rtl::Reference
<Connection
> pCon
= new Connection();
152 pCon
->construct(url
, info
);
154 m_xConnections
.emplace_back(*pCon
);
159 sal_Bool SAL_CALL
FirebirdDriver::acceptsURL( const OUString
& url
)
161 return (url
== "sdbc:embedded:firebird" || url
.startsWith("sdbc:firebird:"));
164 Sequence
< DriverPropertyInfo
> SAL_CALL
FirebirdDriver::getPropertyInfo(
165 const OUString
& url
, const Sequence
< PropertyValue
>& )
167 if ( ! acceptsURL(url
) )
169 ::connectivity::SharedResources aResources
;
170 const OUString sMessage
= aResources
.getResourceString(STR_URI_SYNTAX_ERROR
);
171 ::dbtools::throwGenericSQLException(sMessage
,*this);
174 return Sequence
< DriverPropertyInfo
>();
177 sal_Int32 SAL_CALL
FirebirdDriver::getMajorVersion( )
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
184 sal_Int32 SAL_CALL
FirebirdDriver::getMinorVersion( )
189 //----- XDataDefinitionSupplier
190 uno::Reference
< XTablesSupplier
> SAL_CALL
FirebirdDriver::getDataDefinitionByConnection(
191 const uno::Reference
< XConnection
>& rConnection
)
193 if (Connection
* pConnection
= comphelper::getFromUnoTunnel
<Connection
>(rConnection
))
194 return pConnection
->createCatalog();
198 uno::Reference
< XTablesSupplier
> SAL_CALL
FirebirdDriver::getDataDefinitionByURL(
199 const OUString
& rURL
,
200 const uno::Sequence
< PropertyValue
>& rInfo
)
202 uno::Reference
< XConnection
> xConnection
= connect(rURL
, rInfo
);
203 return getDataDefinitionByConnection(xConnection
);
206 namespace connectivity::firebird
208 void checkDisposed(bool _bThrow
)
211 throw DisposedException();
217 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
218 connectivity_FirebirdDriver_get_implementation(
219 css::uno::XComponentContext
* context
, css::uno::Sequence
<css::uno::Any
> const&)
222 return cppu::acquire(new FirebirdDriver(context
));
228 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */