1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
23 #include <connectivity/dbexception.hxx>
24 #include <resource/common_res.hrc>
25 #include <resource/hsqldb_res.hrc>
26 #include <resource/sharedresources.hxx>
28 #include <comphelper/processfactory.hxx>
29 #include <cppuhelper/supportsservice.hxx>
30 #include <osl/file.hxx>
31 #include <osl/process.h>
32 #include <rtl/bootstrap.hxx>
33 #include <svtools/miscopt.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 namespace connectivity
50 Reference
< XInterface
> SAL_CALL
FirebirdDriver_CreateInstance(
51 const Reference
< XMultiServiceFactory
>& _rxFactory
) throw( Exception
)
53 SAL_INFO("connectivity.firebird", "FirebirdDriver_CreateInstance()" );
54 return *(new FirebirdDriver(comphelper::getComponentContext(_rxFactory
)));
59 // Static const member variables
60 const OUString
FirebirdDriver::our_sFirebirdTmpVar("FIREBIRD_TMP");
61 const OUString
FirebirdDriver::our_sFirebirdLockVar("FIREBIRD_LOCK");
62 const OUString
FirebirdDriver::our_sFirebirdMsgVar("FIREBIRD_MSG");
64 FirebirdDriver::FirebirdDriver(const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& _rxContext
)
65 : ODriver_BASE(m_aMutex
)
66 , m_aContext(_rxContext
)
67 , m_firebirdTMPDirectory(NULL
, true)
68 , m_firebirdLockDirectory(NULL
, true)
70 m_firebirdTMPDirectory
.EnableKillingFile();
71 m_firebirdLockDirectory
.EnableKillingFile();
73 // ::utl::TempFile uses a unique temporary directory (subdirectory of
74 // /tmp or other user specific tmp directory) per instance in which
75 // we can create directories for firebird at will.
77 // Overrides firebird's default of /tmp or c:\temp
78 osl_setEnvironment(our_sFirebirdTmpVar
.pData
, m_firebirdTMPDirectory
.GetFileName().pData
);
80 // Overrides firebird's default of /tmp/firebird or c:\temp\firebird
81 osl_setEnvironment(our_sFirebirdLockVar
.pData
, m_firebirdLockDirectory
.GetFileName().pData
);
83 #ifndef SYSTEM_FIREBIRD
84 // Overrides firebird's hardcoded default of /usr/local/firebird on *nix,
85 // however on Windows it seems to use the current directory as a default.
86 OUString
sMsgURL("$BRAND_BASE_DIR/$BRAND_SHARE_SUBDIR/firebird");
87 ::rtl::Bootstrap::expandMacros(sMsgURL
);
89 ::osl::FileBase::getSystemPathFromFileURL(sMsgURL
, sMsgPath
);
90 osl_setEnvironment(our_sFirebirdMsgVar
.pData
, sMsgPath
.pData
);
94 void FirebirdDriver::disposing()
96 MutexGuard
aGuard(m_aMutex
);
98 for (OWeakRefArray::iterator i
= m_xConnections
.begin(); m_xConnections
.end() != i
; ++i
)
100 Reference
< XComponent
> xComp(i
->get(), UNO_QUERY
);
104 m_xConnections
.clear();
106 osl_clearEnvironment(our_sFirebirdTmpVar
.pData
);
107 osl_clearEnvironment(our_sFirebirdLockVar
.pData
);
109 #ifndef SYSTEM_FIREBIRD
110 osl_clearEnvironment(our_sFirebirdMsgVar
.pData
);
113 ODriver_BASE::disposing();
116 //----- static ServiceInfo ---------------------------------------------------
117 rtl::OUString
FirebirdDriver::getImplementationName_Static() throw(RuntimeException
)
119 return rtl::OUString("com.sun.star.comp.sdbc.firebird.Driver");
122 Sequence
< OUString
> FirebirdDriver::getSupportedServiceNames_Static() throw (RuntimeException
)
124 Sequence
< OUString
> aSNS( 2 );
125 aSNS
[0] = "com.sun.star.sdbc.Driver";
126 aSNS
[0] = "com.sun.star.sdbcx.Driver";
130 OUString SAL_CALL
FirebirdDriver::getImplementationName() throw(RuntimeException
, std::exception
)
132 return getImplementationName_Static();
135 sal_Bool SAL_CALL
FirebirdDriver::supportsService(const OUString
& _rServiceName
)
136 throw(RuntimeException
, std::exception
)
138 return cppu::supportsService(this, _rServiceName
);
141 Sequence
< OUString
> SAL_CALL
FirebirdDriver::getSupportedServiceNames()
142 throw(RuntimeException
, std::exception
)
144 return getSupportedServiceNames_Static();
147 // ---- XDriver -------------------------------------------------------------
148 Reference
< XConnection
> SAL_CALL
FirebirdDriver::connect(
149 const OUString
& url
, const Sequence
< PropertyValue
>& info
)
150 throw(SQLException
, RuntimeException
, std::exception
)
152 Reference
< XConnection
> xConnection
;
154 SAL_INFO("connectivity.firebird", "connect(), URL: " << url
);
156 MutexGuard
aGuard( m_aMutex
);
157 if (ODriver_BASE::rBHelper
.bDisposed
)
158 throw DisposedException();
160 if ( ! acceptsURL(url
) )
163 Connection
* pCon
= new Connection(this);
164 Reference
< XConnection
> xCon
= pCon
;
165 pCon
->construct(url
, info
);
166 m_xConnections
.push_back(WeakReferenceHelper(*pCon
));
171 sal_Bool SAL_CALL
FirebirdDriver::acceptsURL( const OUString
& url
)
172 throw(SQLException
, RuntimeException
, std::exception
)
174 SvtMiscOptions aMiscOptions
;
176 return aMiscOptions
.IsExperimentalMode() &&
177 (url
.equals("sdbc:embedded:firebird") || url
.startsWith("sdbc:firebird:"));
180 Sequence
< DriverPropertyInfo
> SAL_CALL
FirebirdDriver::getPropertyInfo(
181 const OUString
& url
, const Sequence
< PropertyValue
>& info
)
182 throw(SQLException
, RuntimeException
, std::exception
)
185 if ( ! acceptsURL(url
) )
187 ::connectivity::SharedResources aResources
;
188 const OUString sMessage
= aResources
.getResourceString(STR_URI_SYNTAX_ERROR
);
189 ::dbtools::throwGenericSQLException(sMessage
,*this);
192 return Sequence
< DriverPropertyInfo
>();
195 sal_Int32 SAL_CALL
FirebirdDriver::getMajorVersion( ) throw(RuntimeException
, std::exception
)
197 // The major and minor version are sdbc driver specific. Must begin with 1.0
198 // as per http://api.libreoffice.org/docs/common/ref/com/sun/star/sdbc/XDriver.html
202 sal_Int32 SAL_CALL
FirebirdDriver::getMinorVersion( ) throw(RuntimeException
, std::exception
)
207 //----- XDataDefinitionSupplier
208 uno::Reference
< XTablesSupplier
> SAL_CALL
FirebirdDriver::getDataDefinitionByConnection(
209 const uno::Reference
< XConnection
>& rConnection
)
210 throw(SQLException
, RuntimeException
, std::exception
)
212 Connection
* pConnection
= static_cast< Connection
* >(rConnection
.get());
213 return uno::Reference
< XTablesSupplier
>(pConnection
->createCatalog(), UNO_QUERY
);
216 uno::Reference
< XTablesSupplier
> SAL_CALL
FirebirdDriver::getDataDefinitionByURL(
217 const OUString
& rURL
,
218 const uno::Sequence
< PropertyValue
>& rInfo
)
219 throw(SQLException
, RuntimeException
, std::exception
)
221 uno::Reference
< XConnection
> xConnection
= connect(rURL
, rInfo
);
222 return getDataDefinitionByConnection(xConnection
);
225 namespace connectivity
230 void release(oslInterlockedCount
& _refCount
, ::cppu::OBroadcastHelper
& rBHelper
,
231 Reference
< XInterface
>& _xInterface
, XComponent
* _pObject
)
233 if (osl_atomic_decrement( &_refCount
) == 0)
235 osl_atomic_increment( &_refCount
);
237 if (!rBHelper
.bDisposed
&& !rBHelper
.bInDispose
)
239 // remember the parent
240 Reference
< XInterface
> xParent
;
242 MutexGuard
aGuard( rBHelper
.rMutex
);
243 xParent
= _xInterface
;
250 // only the alive ref holds the object
251 OSL_ASSERT( _refCount
== 1 );
253 // release the parent in the ~
256 MutexGuard
aGuard( rBHelper
.rMutex
);
257 _xInterface
= xParent
;
262 osl_atomic_increment( &_refCount
);
265 void checkDisposed(bool _bThrow
) throw ( DisposedException
)
268 throw DisposedException();
273 } // namespace connectivity
276 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */