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
;
37 #include <cppconn/exception.h>
38 #ifdef SYSTEM_MYSQL_CPPCONN
39 #include <mysql_driver.h>
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 )
52 OSL_TRACE("MysqlCDriver::MysqlCDriver");
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
);
72 m_xConnections
.clear();
74 ODriver_BASE::disposing();
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" );
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");
104 /* {{{ MysqlCDriver::getImplementationName() -I- */
105 OUString SAL_CALL
MysqlCDriver::getImplementationName()
106 throw(RuntimeException
)
108 OSL_TRACE("MysqlCDriver::getImplementationName");
109 return getImplementationName_Static();
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
);
129 /* {{{ MysqlCDriver::getSupportedServiceNames() -I- */
130 Sequence
< OUString
> SAL_CALL
MysqlCDriver::getSupportedServiceNames()
131 throw(RuntimeException
)
133 OSL_TRACE("MysqlCDriver::getSupportedServiceNames");
134 return getSupportedServiceNames_Static();
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();
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!");
159 OUString( "Unable to load the " BUNDLE_MARIADB
" library." ),
161 OUString( "08001" ), // "unable to connect"
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!");
179 OUString( "Unable to load the " CPPCONN_LIB
" library." ),
181 OUString( "08001" ), // "unable to connect"
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
"!");
196 OUString( CPPCONN_LIB
" is invalid: missing the driver factory function." ),
198 OUString( "08001" ), // "unable to connect"
204 cppDriver
= static_cast< sql::Driver
* >( (*pFactoryFunction
)() );
209 OUString( "Unable to obtain the MySQL_Driver instance from Connector/C++." ),
211 OUString( "08001" ), // "unable to connect"
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
)) {
231 impl_initCppConn_lck_throw();
232 OSL_POSTCOND( cppDriver
, "MySQLCDriver::connect: internal error." );
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
240 OConnection
* pCon
= new OConnection(*this, cppDriver
);
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());
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));
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(
275 ,OUString("Name of host")
277 ,OUString("localhost")
278 ,Sequence
< OUString
>())
280 aDriverInfo
.push_back(DriverPropertyInfo(
285 ,Sequence
< OUString
>())
287 return Sequence
< DriverPropertyInfo
>(&(aDriverInfo
[0]),aDriverInfo
.size());
290 return Sequence
< DriverPropertyInfo
>();
295 /* {{{ MysqlCDriver::getMajorVersion() -I- */
296 sal_Int32 SAL_CALL
MysqlCDriver::getMajorVersion()
297 throw(RuntimeException
)
299 OSL_TRACE("MysqlCDriver::getMajorVersion");
300 return MARIADBC_VERSION_MAJOR
;
305 /* {{{ MysqlCDriver::getMinorVersion() -I- */
306 sal_Int32 SAL_CALL
MysqlCDriver::getMinorVersion()
307 throw(RuntimeException
)
309 OSL_TRACE("MysqlCDriver::getMinorVersion");
310 return MARIADBC_VERSION_MINOR
;
315 namespace connectivity
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
;
347 // only the alive ref holds the object
348 OSL_ASSERT(_refCount
== 1);
350 // release the parent in the destructor
352 ::osl::MutexGuard
aGuard(rBHelper
.rMutex
);
353 _xInterface
= xParent
;
357 osl_atomic_increment(&_refCount
);
364 /* {{{ connectivity::mysqlc::checkDisposed() -I- */
365 void checkDisposed(sal_Bool _bThrow
)
366 throw (DisposedException
)
369 throw DisposedException();
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: */