update emoji autocorrect entries from po-files
[LibreOffice.git] / connectivity / source / drivers / firebird / Driver.cxx
blob0a65ea9885e51ac749ef8c17b0d96e2e13daa6bb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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"
21 #include "Driver.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>
34 #include <unotools/localfilehelper.hxx>
36 using namespace com::sun::star;
37 using namespace com::sun::star::uno;
38 using namespace com::sun::star::lang;
39 using namespace com::sun::star::beans;
40 using namespace com::sun::star::sdbc;
41 using namespace com::sun::star::sdbcx;
43 using namespace ::osl;
45 using namespace connectivity::firebird;
47 namespace connectivity
49 namespace firebird
51 Reference< XInterface > SAL_CALL FirebirdDriver_CreateInstance(
52 const Reference< XMultiServiceFactory >& _rxFactory) throw( Exception )
54 SAL_INFO("connectivity.firebird", "FirebirdDriver_CreateInstance()" );
55 return *(new FirebirdDriver(comphelper::getComponentContext(_rxFactory)));
60 // Static const member variables
61 const OUString FirebirdDriver::our_sFirebirdTmpVar("FIREBIRD_TMP");
62 const OUString FirebirdDriver::our_sFirebirdLockVar("FIREBIRD_LOCK");
63 const OUString FirebirdDriver::our_sFirebirdMsgVar("FIREBIRD_MSG");
65 FirebirdDriver::FirebirdDriver(const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& _rxContext)
66 : ODriver_BASE(m_aMutex)
67 , m_aContext(_rxContext)
68 , m_firebirdTMPDirectory(NULL, true)
69 , m_firebirdLockDirectory(NULL, true)
71 // Note: TempFile caches the URL on first access; call this here so that
72 // ~FirebirdDriver is not the first access, because that is called
73 // when the ServiceManager is disposing, so GetURL() would fail!
74 m_firebirdTMPDirectory.GetURL();
75 m_firebirdLockDirectory.GetURL();
77 // ::utl::TempFile uses a unique temporary directory (subdirectory of
78 // /tmp or other user specific tmp directory) per instance in which
79 // we can create directories for firebird at will.
81 // Overrides firebird's default of /tmp or c:\temp
82 osl_setEnvironment(our_sFirebirdTmpVar.pData, m_firebirdTMPDirectory.GetFileName().pData);
84 // Overrides firebird's default of /tmp/firebird or c:\temp\firebird
85 osl_setEnvironment(our_sFirebirdLockVar.pData, m_firebirdLockDirectory.GetFileName().pData);
87 #ifndef SYSTEM_FIREBIRD
88 // Overrides firebird's hardcoded default of /usr/local/firebird on *nix,
89 // however on Windows it seems to use the current directory as a default.
90 OUString sMsgURL("$BRAND_BASE_DIR/$BRAND_SHARE_SUBDIR/firebird");
91 ::rtl::Bootstrap::expandMacros(sMsgURL);
92 OUString sMsgPath;
93 ::osl::FileBase::getSystemPathFromFileURL(sMsgURL, sMsgPath);
94 osl_setEnvironment(our_sFirebirdMsgVar.pData, sMsgPath.pData);
95 #endif
98 FirebirdDriver::~FirebirdDriver()
100 utl::removeTree(m_firebirdTMPDirectory.GetURL());
101 utl::removeTree(m_firebirdLockDirectory.GetURL());
104 void FirebirdDriver::disposing()
106 MutexGuard aGuard(m_aMutex);
108 for (OWeakRefArray::iterator i = m_xConnections.begin(); m_xConnections.end() != i; ++i)
110 Reference< XComponent > xComp(i->get(), UNO_QUERY);
111 if (xComp.is())
112 xComp->dispose();
114 m_xConnections.clear();
116 osl_clearEnvironment(our_sFirebirdTmpVar.pData);
117 osl_clearEnvironment(our_sFirebirdLockVar.pData);
119 #ifndef SYSTEM_FIREBIRD
120 osl_clearEnvironment(our_sFirebirdMsgVar.pData);
121 #endif
123 OSL_VERIFY(fb_shutdown(0, 1));
125 ODriver_BASE::disposing();
128 //----- static ServiceInfo ---------------------------------------------------
129 rtl::OUString FirebirdDriver::getImplementationName_Static() throw(RuntimeException)
131 return rtl::OUString("com.sun.star.comp.sdbc.firebird.Driver");
134 Sequence< OUString > FirebirdDriver::getSupportedServiceNames_Static() throw (RuntimeException)
136 Sequence< OUString > aSNS( 2 );
137 aSNS[0] = "com.sun.star.sdbc.Driver";
138 aSNS[1] = "com.sun.star.sdbcx.Driver";
139 return aSNS;
142 OUString SAL_CALL FirebirdDriver::getImplementationName() throw(RuntimeException, std::exception)
144 return getImplementationName_Static();
147 sal_Bool SAL_CALL FirebirdDriver::supportsService(const OUString& _rServiceName)
148 throw(RuntimeException, std::exception)
150 return cppu::supportsService(this, _rServiceName);
153 Sequence< OUString > SAL_CALL FirebirdDriver::getSupportedServiceNames()
154 throw(RuntimeException, std::exception)
156 return getSupportedServiceNames_Static();
159 // ---- XDriver -------------------------------------------------------------
160 Reference< XConnection > SAL_CALL FirebirdDriver::connect(
161 const OUString& url, const Sequence< PropertyValue >& info )
162 throw(SQLException, RuntimeException, std::exception)
164 Reference< XConnection > xConnection;
166 SAL_INFO("connectivity.firebird", "connect(), URL: " << url );
168 MutexGuard aGuard( m_aMutex );
169 if (ODriver_BASE::rBHelper.bDisposed)
170 throw DisposedException();
172 if ( ! acceptsURL(url) )
173 return NULL;
175 Connection* pCon = new Connection(this);
176 Reference< XConnection > xCon = pCon;
177 pCon->construct(url, info);
178 m_xConnections.push_back(WeakReferenceHelper(*pCon));
180 return xCon;
183 sal_Bool SAL_CALL FirebirdDriver::acceptsURL( const OUString& url )
184 throw(SQLException, RuntimeException, std::exception)
186 SvtMiscOptions aMiscOptions;
188 return aMiscOptions.IsExperimentalMode() &&
189 (url == "sdbc:embedded:firebird" || url.startsWith("sdbc:firebird:"));
192 Sequence< DriverPropertyInfo > SAL_CALL FirebirdDriver::getPropertyInfo(
193 const OUString& url, const Sequence< PropertyValue >& info )
194 throw(SQLException, RuntimeException, std::exception)
196 (void) info;
197 if ( ! acceptsURL(url) )
199 ::connectivity::SharedResources aResources;
200 const OUString sMessage = aResources.getResourceString(STR_URI_SYNTAX_ERROR);
201 ::dbtools::throwGenericSQLException(sMessage ,*this);
204 return Sequence< DriverPropertyInfo >();
207 sal_Int32 SAL_CALL FirebirdDriver::getMajorVersion( ) throw(RuntimeException, std::exception)
209 // The major and minor version are sdbc driver specific. Must begin with 1.0
210 // as per http://api.libreoffice.org/docs/common/ref/com/sun/star/sdbc/XDriver.html
211 return 1;
214 sal_Int32 SAL_CALL FirebirdDriver::getMinorVersion( ) throw(RuntimeException, std::exception)
216 return 0;
219 //----- XDataDefinitionSupplier
220 uno::Reference< XTablesSupplier > SAL_CALL FirebirdDriver::getDataDefinitionByConnection(
221 const uno::Reference< XConnection >& rConnection)
222 throw(SQLException, RuntimeException, std::exception)
224 Connection* pConnection = static_cast< Connection* >(rConnection.get());
225 return uno::Reference< XTablesSupplier >(pConnection->createCatalog(), UNO_QUERY);
228 uno::Reference< XTablesSupplier > SAL_CALL FirebirdDriver::getDataDefinitionByURL(
229 const OUString& rURL,
230 const uno::Sequence< PropertyValue >& rInfo)
231 throw(SQLException, RuntimeException, std::exception)
233 uno::Reference< XConnection > xConnection = connect(rURL, rInfo);
234 return getDataDefinitionByConnection(xConnection);
237 namespace connectivity
239 namespace firebird
242 void release(oslInterlockedCount& _refCount, ::cppu::OBroadcastHelper& rBHelper,
243 Reference< XInterface >& _xInterface, XComponent* _pObject)
245 if (osl_atomic_decrement( &_refCount ) == 0)
247 osl_atomic_increment( &_refCount );
249 if (!rBHelper.bDisposed && !rBHelper.bInDispose)
251 // remember the parent
252 Reference< XInterface > xParent;
254 MutexGuard aGuard( rBHelper.rMutex );
255 xParent = _xInterface;
256 _xInterface = NULL;
259 // First dispose
260 _pObject->dispose();
262 // only the alive ref holds the object
263 OSL_ASSERT( _refCount == 1 );
265 // release the parent in the ~
266 if (xParent.is())
268 MutexGuard aGuard( rBHelper.rMutex );
269 _xInterface = xParent;
273 else
274 osl_atomic_increment( &_refCount );
277 void checkDisposed(bool _bThrow) throw ( DisposedException )
279 if (_bThrow)
280 throw DisposedException();
285 } // namespace connectivity
288 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */