fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / mysqlc / source / mysqlc_driver.cxx
blobcbd956f3562cb0f4ea47d8c6d1f067315eec37ac
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
25 ************************************************************************/
26 #include "mysqlc_driver.hxx"
27 #include "mysqlc_connection.hxx"
28 #include "mysqlc_general.hxx"
30 using namespace com::sun::star::uno;
31 using namespace com::sun::star::lang;
32 using namespace com::sun::star::beans;
33 using namespace com::sun::star::sdbc;
34 using namespace connectivity::mysqlc;
35 #include <stdio.h>
37 #include <cppconn/exception.h>
38 #ifdef SYSTEM_MYSQL_CPPCONN
39 #include <mysql_driver.h>
40 #endif
43 /* {{{ MysqlCDriver::MysqlCDriver() -I- */
44 MysqlCDriver::MysqlCDriver(const Reference< XMultiServiceFactory >& _rxFactory)
45 : ODriver_BASE(m_aMutex)
46 ,m_xFactory(_rxFactory)
47 #ifndef SYSTEM_MYSQL_CPPCONN
48 ,m_hCppConnModule( NULL )
49 ,m_bAttemptedLoadCppConn( false )
50 #endif
52 OSL_TRACE("MysqlCDriver::MysqlCDriver");
53 cppDriver = NULL;
55 /* }}} */
58 /* {{{ MysqlCDriver::disposing() -I- */
59 void MysqlCDriver::disposing()
61 OSL_TRACE("MysqlCDriver::disposing");
62 ::osl::MutexGuard aGuard(m_aMutex);
64 // when driver will be destroied so all our connections have to be destroied as well
65 for (OWeakRefArray::iterator i = m_xConnections.begin(); m_xConnections.end() != i; ++i)
67 Reference< XComponent > xComp(i->get(), UNO_QUERY);
68 if (xComp.is()) {
69 xComp->dispose();
72 m_xConnections.clear();
74 ODriver_BASE::disposing();
76 /* }}} */
79 // static ServiceInfo
80 /* {{{ MysqlCDriver::getImplementationName_Static() -I- */
81 OUString MysqlCDriver::getImplementationName_Static()
82 throw(RuntimeException)
84 OSL_TRACE("MysqlCDriver::getImplementationName_Static");
85 return OUString( "com.sun.star.comp.sdbc.mysqlc.MysqlCDriver" );
87 /* }}} */
90 /* {{{ MysqlCDriver::getSupportedServiceNames_Static() -I- */
91 Sequence< OUString > MysqlCDriver::getSupportedServiceNames_Static()
92 throw(RuntimeException)
94 OSL_TRACE("MysqlCDriver::getSupportedServiceNames_Static");
95 // which service is supported
96 // for more information @see com.sun.star.sdbc.Driver
97 Sequence< OUString > aSNS(1);
98 aSNS[0] = OUString("com.sun.star.sdbc.Driver");
99 return aSNS;
101 /* }}} */
104 /* {{{ MysqlCDriver::getImplementationName() -I- */
105 OUString SAL_CALL MysqlCDriver::getImplementationName()
106 throw(RuntimeException)
108 OSL_TRACE("MysqlCDriver::getImplementationName");
109 return getImplementationName_Static();
111 /* }}} */
114 /* {{{ MysqlCDriver::supportsService() -I- */
115 sal_Bool SAL_CALL MysqlCDriver::supportsService(const OUString& _rServiceName)
116 throw(RuntimeException)
118 OSL_TRACE("MysqlCDriver::supportsService");
119 Sequence< OUString > aSupported(getSupportedServiceNames());
120 const OUString* pSupported = aSupported.getConstArray();
121 const OUString* pEnd = pSupported + aSupported.getLength();
122 for (;pSupported != pEnd && !pSupported->equals(_rServiceName); ++pSupported){}
124 return (pSupported != pEnd);
126 /* }}} */
129 /* {{{ MysqlCDriver::getSupportedServiceNames() -I- */
130 Sequence< OUString > SAL_CALL MysqlCDriver::getSupportedServiceNames()
131 throw(RuntimeException)
133 OSL_TRACE("MysqlCDriver::getSupportedServiceNames");
134 return getSupportedServiceNames_Static();
136 /* }}} */
139 extern "C" { static void SAL_CALL thisModule() {} }
141 void MysqlCDriver::impl_initCppConn_lck_throw()
143 #ifdef SYSTEM_MYSQL_CPPCONN
144 cppDriver = get_driver_instance();
145 #else
146 #ifdef BUNDLE_MARIADB
147 if ( !m_bAttemptedLoadCConn )
149 const OUString sModuleName(BUNDLE_MARIADB);
150 m_hCConnModule = osl_loadModuleRelative( &thisModule, sModuleName.pData, 0 );
151 m_bAttemptedLoadCConn = true;
154 // attempted to load - was it successful?
155 if ( !m_hCConnModule )
157 OSL_FAIL( "MysqlCDriver::impl_initCppConn_lck_throw: could not load the " BUNDLE_MARIADB " library!");
158 throw SQLException(
159 OUString( "Unable to load the " BUNDLE_MARIADB " library." ),
160 *this,
161 OUString( "08001" ), // "unable to connect"
163 Any()
166 #endif
167 if ( !m_bAttemptedLoadCppConn )
169 const OUString sModuleName(CPPCONN_LIB);
170 m_hCppConnModule = osl_loadModuleRelative( &thisModule, sModuleName.pData, 0 );
171 m_bAttemptedLoadCppConn = true;
174 // attempted to load - was it successful?
175 if ( !m_hCppConnModule )
177 OSL_FAIL( "MysqlCDriver::impl_initCppConn_lck_throw: could not load the " CPPCONN_LIB " library!");
178 throw SQLException(
179 OUString( "Unable to load the " CPPCONN_LIB " library." ),
180 *this,
181 OUString( "08001" ), // "unable to connect"
183 Any()
187 // find the factory symbol
188 const OUString sSymbolName = OUString( "sql_mysql_get_driver_instance" );
189 typedef void* (* FGetMySQLDriver)();
191 const FGetMySQLDriver pFactoryFunction = (FGetMySQLDriver)( osl_getFunctionSymbol( m_hCppConnModule, sSymbolName.pData ) );
192 if ( !pFactoryFunction )
194 OSL_FAIL( "MysqlCDriver::impl_initCppConn_lck_throw: could not find the factory symbol in " CPPCONN_LIB "!");
195 throw SQLException(
196 OUString( CPPCONN_LIB " is invalid: missing the driver factory function." ),
197 *this,
198 OUString( "08001" ), // "unable to connect"
200 Any()
204 cppDriver = static_cast< sql::Driver* >( (*pFactoryFunction)() );
205 #endif
206 if ( !cppDriver )
208 throw SQLException(
209 OUString( "Unable to obtain the MySQL_Driver instance from Connector/C++." ),
210 *this,
211 OUString( "08001" ), // "unable to connect"
213 Any()
218 /* {{{ MysqlCDriver::connect() -I- */
219 Reference< XConnection > SAL_CALL MysqlCDriver::connect(const OUString& url, const Sequence< PropertyValue >& info)
220 throw(SQLException, RuntimeException)
222 ::osl::MutexGuard aGuard( m_aMutex );
224 OSL_TRACE("MysqlCDriver::connect");
225 if (!acceptsURL(url)) {
226 return NULL;
229 if ( !cppDriver )
231 impl_initCppConn_lck_throw();
232 OSL_POSTCOND( cppDriver, "MySQLCDriver::connect: internal error." );
233 if ( !cppDriver )
234 throw RuntimeException( OUString( "MySQLCDriver::connect: internal error." ), *this );
237 Reference< XConnection > xConn;
238 // create a new connection with the given properties and append it to our vector
239 try {
240 OConnection* pCon = new OConnection(*this, cppDriver);
241 xConn = pCon;
243 pCon->construct(url,info);
244 m_xConnections.push_back(WeakReferenceHelper(*pCon));
246 catch (const sql::SQLException &e)
248 mysqlc_sdbc_driver::translateAndThrow(e, *this, getDefaultEncoding());
250 return xConn;
252 /* }}} */
255 /* {{{ MysqlCDriver::acceptsURL() -I- */
256 sal_Bool SAL_CALL MysqlCDriver::acceptsURL(const OUString& url)
257 throw(SQLException, RuntimeException)
259 OSL_TRACE("MysqlCDriver::acceptsURL");
260 return (!url.compareTo(OUString("sdbc:mysqlc:"), sizeof("sdbc:mysqlc:")-1));
262 /* }}} */
265 /* {{{ MysqlCDriver::getPropertyInfo() -I- */
266 Sequence< DriverPropertyInfo > SAL_CALL MysqlCDriver::getPropertyInfo(const OUString& url, const Sequence< PropertyValue >& /* info */)
267 throw(SQLException, RuntimeException)
269 OSL_TRACE("MysqlCDriver::getPropertyInfo");
270 if (acceptsURL(url)) {
271 ::std::vector< DriverPropertyInfo > aDriverInfo;
273 aDriverInfo.push_back(DriverPropertyInfo(
274 OUString("Hostname")
275 ,OUString("Name of host")
276 ,sal_True
277 ,OUString("localhost")
278 ,Sequence< OUString >())
280 aDriverInfo.push_back(DriverPropertyInfo(
281 OUString("Port")
282 ,OUString("Port")
283 ,sal_True
284 ,OUString("3306")
285 ,Sequence< OUString >())
287 return Sequence< DriverPropertyInfo >(&(aDriverInfo[0]),aDriverInfo.size());
290 return Sequence< DriverPropertyInfo >();
292 /* }}} */
295 /* {{{ MysqlCDriver::getMajorVersion() -I- */
296 sal_Int32 SAL_CALL MysqlCDriver::getMajorVersion()
297 throw(RuntimeException)
299 OSL_TRACE("MysqlCDriver::getMajorVersion");
300 return MARIADBC_VERSION_MAJOR;
302 /* }}} */
305 /* {{{ MysqlCDriver::getMinorVersion() -I- */
306 sal_Int32 SAL_CALL MysqlCDriver::getMinorVersion()
307 throw(RuntimeException)
309 OSL_TRACE("MysqlCDriver::getMinorVersion");
310 return MARIADBC_VERSION_MINOR;
312 /* }}} */
315 namespace connectivity
317 namespace mysqlc
320 Reference< XInterface > SAL_CALL MysqlCDriver_CreateInstance(const Reference< XMultiServiceFactory >& _rxFactory)
321 throw(::com::sun::star::uno::Exception)
323 return(*(new MysqlCDriver(_rxFactory)));
326 /* {{{ connectivity::mysqlc::release() -I- */
327 void release(oslInterlockedCount& _refCount,
328 ::cppu::OBroadcastHelper& rBHelper,
329 Reference< XInterface >& _xInterface,
330 ::com::sun::star::lang::XComponent* _pObject)
332 if (osl_atomic_decrement(&_refCount) == 0) {
333 osl_atomic_increment(&_refCount);
335 if (!rBHelper.bDisposed && !rBHelper.bInDispose) {
336 // remember the parent
337 Reference< XInterface > xParent;
339 ::osl::MutexGuard aGuard(rBHelper.rMutex);
340 xParent = _xInterface;
341 _xInterface = NULL;
344 // First dispose
345 _pObject->dispose();
347 // only the alive ref holds the object
348 OSL_ASSERT(_refCount == 1);
350 // release the parent in the destructor
351 if (xParent.is()) {
352 ::osl::MutexGuard aGuard(rBHelper.rMutex);
353 _xInterface = xParent;
356 } else {
357 osl_atomic_increment(&_refCount);
360 /* }}} */
364 /* {{{ connectivity::mysqlc::checkDisposed() -I- */
365 void checkDisposed(sal_Bool _bThrow)
366 throw (DisposedException)
368 if (_bThrow) {
369 throw DisposedException();
372 /* }}} */
374 } /* mysqlc */
375 } /* connectivity */
378 * Local variables:
379 * tab-width: 4
380 * c-basic-offset: 4
381 * End:
382 * vim600: noet sw=4 ts=4 fdm=marker
383 * vim<600: noet sw=4 ts=4
386 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */