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
;
36 #include <cppuhelper/supportsservice.hxx>
39 #include <cppconn/exception.h>
40 #ifdef SYSTEM_MYSQL_CPPCONN
41 #include <mysql_driver.h>
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 )
52 OSL_TRACE("MysqlCDriver::MysqlCDriver");
56 void MysqlCDriver::disposing()
58 OSL_TRACE("MysqlCDriver::disposing");
59 ::osl::MutexGuard
aGuard(m_aMutex
);
61 // when driver will be destroied so all our connections have to be destroied as well
62 for (OWeakRefArray::iterator i
= m_xConnections
.begin(); m_xConnections
.end() != i
; ++i
)
64 Reference
< XComponent
> xComp(i
->get(), UNO_QUERY
);
69 m_xConnections
.clear();
71 ODriver_BASE::disposing();
75 rtl::OUString
MysqlCDriver::getImplementationName_Static()
76 throw(RuntimeException
)
78 OSL_TRACE("MysqlCDriver::getImplementationName_Static");
79 return rtl::OUString( "com.sun.star.comp.sdbc.mysqlc.MysqlCDriver" );
82 Sequence
< rtl::OUString
> MysqlCDriver::getSupportedServiceNames_Static()
83 throw(RuntimeException
)
85 OSL_TRACE("MysqlCDriver::getSupportedServiceNames_Static");
86 // which service is supported
87 // for more information @see com.sun.star.sdbc.Driver
88 Sequence
< rtl::OUString
> aSNS(1);
89 aSNS
[0] = "com.sun.star.sdbc.Driver";
93 rtl::OUString SAL_CALL
MysqlCDriver::getImplementationName()
94 throw(RuntimeException
, std::exception
)
96 OSL_TRACE("MysqlCDriver::getImplementationName");
97 return getImplementationName_Static();
100 sal_Bool SAL_CALL
MysqlCDriver::supportsService(const rtl::OUString
& _rServiceName
)
101 throw(RuntimeException
, std::exception
)
103 return cppu::supportsService(this, _rServiceName
);
106 Sequence
< rtl::OUString
> SAL_CALL
MysqlCDriver::getSupportedServiceNames()
107 throw(RuntimeException
, std::exception
)
109 OSL_TRACE("MysqlCDriver::getSupportedServiceNames");
110 return getSupportedServiceNames_Static();
113 extern "C" { static void SAL_CALL
thisModule() {} }
115 void MysqlCDriver::impl_initCppConn_lck_throw()
117 #ifdef SYSTEM_MYSQL_CPPCONN
118 cppDriver
= get_driver_instance();
120 #ifdef BUNDLE_MARIADB
121 if ( !m_bAttemptedLoadCConn
)
123 const rtl::OUString
sModuleName(BUNDLE_MARIADB
);
124 m_hCConnModule
= osl_loadModuleRelative( &thisModule
, sModuleName
.pData
, 0 );
125 m_bAttemptedLoadCConn
= true;
128 // attempted to load - was it successful?
129 if ( !m_hCConnModule
)
131 OSL_FAIL( "MysqlCDriver::impl_initCppConn_lck_throw: could not load the " BUNDLE_MARIADB
" library!");
133 "Unable to load the " BUNDLE_MARIADB
" library.",
135 rtl::OUString( "08001" ), // "unable to connect"
141 if ( !m_bAttemptedLoadCppConn
)
143 const rtl::OUString
sModuleName(CPPCONN_LIB
);
144 m_hCppConnModule
= osl_loadModuleRelative( &thisModule
, sModuleName
.pData
, 0 );
145 m_bAttemptedLoadCppConn
= true;
148 // attempted to load - was it successful?
149 if ( !m_hCppConnModule
)
151 OSL_FAIL( "MysqlCDriver::impl_initCppConn_lck_throw: could not load the " CPPCONN_LIB
" library!");
153 "Unable to load the " CPPCONN_LIB
" library.",
155 rtl::OUString( "08001" ), // "unable to connect"
161 // find the factory symbol
162 const rtl::OUString sSymbolName
= "sql_mysql_get_driver_instance";
163 typedef void* (* FGetMySQLDriver
)();
165 const FGetMySQLDriver pFactoryFunction
= reinterpret_cast<FGetMySQLDriver
>( osl_getFunctionSymbol( m_hCppConnModule
, sSymbolName
.pData
) );
166 if ( !pFactoryFunction
)
168 OSL_FAIL( "MysqlCDriver::impl_initCppConn_lck_throw: could not find the factory symbol in " CPPCONN_LIB
"!");
170 CPPCONN_LIB
" is invalid: missing the driver factory function.",
172 rtl::OUString( "08001" ), // "unable to connect"
178 cppDriver
= static_cast< sql::Driver
* >( (*pFactoryFunction
)() );
183 "Unable to obtain the MySQL_Driver instance from Connector/C++.",
185 rtl::OUString( "08001" ), // "unable to connect"
192 Reference
< XConnection
> SAL_CALL
MysqlCDriver::connect(const rtl::OUString
& url
, const Sequence
< PropertyValue
>& info
)
193 throw(SQLException
, RuntimeException
, std::exception
)
195 ::osl::MutexGuard
aGuard( m_aMutex
);
197 OSL_TRACE("MysqlCDriver::connect");
198 if (!acceptsURL(url
)) {
204 impl_initCppConn_lck_throw();
206 throw RuntimeException("MySQLCDriver::connect: internal error.", *this );
209 Reference
< XConnection
> xConn
;
210 // create a new connection with the given properties and append it to our vector
212 OConnection
* pCon
= new OConnection(*this, cppDriver
);
215 pCon
->construct(url
,info
);
216 m_xConnections
.push_back(WeakReferenceHelper(*pCon
));
218 catch (const sql::SQLException
&e
)
220 mysqlc_sdbc_driver::translateAndThrow(e
, *this, getDefaultEncoding());
225 sal_Bool SAL_CALL
MysqlCDriver::acceptsURL(const rtl::OUString
& url
)
226 throw(SQLException
, RuntimeException
, std::exception
)
228 OSL_TRACE("MysqlCDriver::acceptsURL");
229 return url
.startsWith("sdbc:mysqlc:");
232 Sequence
< DriverPropertyInfo
> SAL_CALL
MysqlCDriver::getPropertyInfo(const rtl::OUString
& url
, const Sequence
< PropertyValue
>& /* info */)
233 throw(SQLException
, RuntimeException
, std::exception
)
235 OSL_TRACE("MysqlCDriver::getPropertyInfo");
236 if (acceptsURL(url
)) {
237 ::std::vector
< DriverPropertyInfo
> aDriverInfo
;
239 aDriverInfo
.push_back(DriverPropertyInfo(
240 rtl::OUString("Hostname")
241 ,rtl::OUString("Name of host")
243 ,rtl::OUString("localhost")
244 ,Sequence
< rtl::OUString
>())
246 aDriverInfo
.push_back(DriverPropertyInfo(
247 rtl::OUString("Port")
248 ,rtl::OUString("Port")
250 ,rtl::OUString("3306")
251 ,Sequence
< rtl::OUString
>())
253 return Sequence
< DriverPropertyInfo
>(&(aDriverInfo
[0]),aDriverInfo
.size());
256 return Sequence
< DriverPropertyInfo
>();
259 sal_Int32 SAL_CALL
MysqlCDriver::getMajorVersion()
260 throw(RuntimeException
, std::exception
)
262 OSL_TRACE("MysqlCDriver::getMajorVersion");
263 return MARIADBC_VERSION_MAJOR
;
266 sal_Int32 SAL_CALL
MysqlCDriver::getMinorVersion()
267 throw(RuntimeException
, std::exception
)
269 OSL_TRACE("MysqlCDriver::getMinorVersion");
270 return MARIADBC_VERSION_MINOR
;
273 namespace connectivity
278 Reference
< XInterface
> SAL_CALL
MysqlCDriver_CreateInstance(const Reference
< XMultiServiceFactory
>& _rxFactory
)
279 throw(::com::sun::star::uno::Exception
)
281 return(*(new MysqlCDriver(_rxFactory
)));
284 void release(oslInterlockedCount
& _refCount
,
285 ::cppu::OBroadcastHelper
& rBHelper
,
286 Reference
< XInterface
>& _xInterface
,
287 ::com::sun::star::lang::XComponent
* _pObject
)
289 if (osl_atomic_decrement(&_refCount
) == 0) {
290 osl_atomic_increment(&_refCount
);
292 if (!rBHelper
.bDisposed
&& !rBHelper
.bInDispose
) {
293 // remember the parent
294 Reference
< XInterface
> xParent
;
296 ::osl::MutexGuard
aGuard(rBHelper
.rMutex
);
297 xParent
= _xInterface
;
304 // only the alive ref holds the object
305 OSL_ASSERT(_refCount
== 1);
307 // release the parent in the destructor
309 ::osl::MutexGuard
aGuard(rBHelper
.rMutex
);
310 _xInterface
= xParent
;
314 osl_atomic_increment(&_refCount
);
318 void checkDisposed(bool _bThrow
)
319 throw (DisposedException
)
322 throw DisposedException();
334 * vim600: noet sw=4 ts=4 fdm=marker
335 * vim<600: noet sw=4 ts=4
338 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */