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 if ( !m_bAttemptedLoadCppConn
)
148 const OUString
sModuleName(CPPCONN_LIB
);
149 m_hCppConnModule
= osl_loadModuleRelative( &thisModule
, sModuleName
.pData
, 0 );
150 m_bAttemptedLoadCppConn
= true;
153 // attempted to load - was it successful?
154 if ( !m_hCppConnModule
)
156 OSL_FAIL( "MysqlCDriver::impl_initCppConn_lck_throw: could not load the " CPPCONN_LIB
" library!");
158 OUString( "Unable to load the " CPPCONN_LIB
" library." ),
160 OUString( "08001" ), // "unable to connect"
166 // find the factory symbol
167 const OUString sSymbolName
= OUString( "sql_mysql_get_driver_instance" );
168 typedef void* (* FGetMySQLDriver
)();
170 const FGetMySQLDriver pFactoryFunction
= (FGetMySQLDriver
)( osl_getFunctionSymbol( m_hCppConnModule
, sSymbolName
.pData
) );
171 if ( !pFactoryFunction
)
173 OSL_FAIL( "MysqlCDriver::impl_initCppConn_lck_throw: could not find the factory symbol in " CPPCONN_LIB
"!");
175 OUString( CPPCONN_LIB
" is invalid: missing the driver factory function." ),
177 OUString( "08001" ), // "unable to connect"
183 cppDriver
= static_cast< sql::Driver
* >( (*pFactoryFunction
)() );
188 OUString( "Unable to obtain the MySQL_Driver instance from Connector/C++." ),
190 OUString( "08001" ), // "unable to connect"
197 /* {{{ MysqlCDriver::connect() -I- */
198 Reference
< XConnection
> SAL_CALL
MysqlCDriver::connect(const OUString
& url
, const Sequence
< PropertyValue
>& info
)
199 throw(SQLException
, RuntimeException
)
201 ::osl::MutexGuard
aGuard( m_aMutex
);
203 OSL_TRACE("MysqlCDriver::connect");
204 if (!acceptsURL(url
)) {
210 impl_initCppConn_lck_throw();
211 OSL_POSTCOND( cppDriver
, "MySQLCDriver::connect: internal error." );
213 throw RuntimeException( OUString( "MySQLCDriver::connect: internal error." ), *this );
216 Reference
< XConnection
> xConn
;
217 // create a new connection with the given properties and append it to our vector
219 OConnection
* pCon
= new OConnection(*this, cppDriver
);
222 pCon
->construct(url
,info
);
223 m_xConnections
.push_back(WeakReferenceHelper(*pCon
));
225 catch (const sql::SQLException
&e
)
227 mysqlc_sdbc_driver::translateAndThrow(e
, *this, getDefaultEncoding());
234 /* {{{ MysqlCDriver::acceptsURL() -I- */
235 sal_Bool SAL_CALL
MysqlCDriver::acceptsURL(const OUString
& url
)
236 throw(SQLException
, RuntimeException
)
238 OSL_TRACE("MysqlCDriver::acceptsURL");
239 return (!url
.compareTo(OUString("sdbc:mysqlc:"), sizeof("sdbc:mysqlc:")-1));
244 /* {{{ MysqlCDriver::getPropertyInfo() -I- */
245 Sequence
< DriverPropertyInfo
> SAL_CALL
MysqlCDriver::getPropertyInfo(const OUString
& url
, const Sequence
< PropertyValue
>& /* info */)
246 throw(SQLException
, RuntimeException
)
248 OSL_TRACE("MysqlCDriver::getPropertyInfo");
249 if (acceptsURL(url
)) {
250 ::std::vector
< DriverPropertyInfo
> aDriverInfo
;
252 aDriverInfo
.push_back(DriverPropertyInfo(
254 ,OUString("Name of host")
256 ,OUString("localhost")
257 ,Sequence
< OUString
>())
259 aDriverInfo
.push_back(DriverPropertyInfo(
264 ,Sequence
< OUString
>())
266 return Sequence
< DriverPropertyInfo
>(&(aDriverInfo
[0]),aDriverInfo
.size());
269 return Sequence
< DriverPropertyInfo
>();
274 /* {{{ MysqlCDriver::getMajorVersion() -I- */
275 sal_Int32 SAL_CALL
MysqlCDriver::getMajorVersion()
276 throw(RuntimeException
)
278 OSL_TRACE("MysqlCDriver::getMajorVersion");
279 return MARIADBC_VERSION_MAJOR
;
284 /* {{{ MysqlCDriver::getMinorVersion() -I- */
285 sal_Int32 SAL_CALL
MysqlCDriver::getMinorVersion()
286 throw(RuntimeException
)
288 OSL_TRACE("MysqlCDriver::getMinorVersion");
289 return MARIADBC_VERSION_MINOR
;
294 namespace connectivity
299 Reference
< XInterface
> SAL_CALL
MysqlCDriver_CreateInstance(const Reference
< XMultiServiceFactory
>& _rxFactory
)
300 throw(::com::sun::star::uno::Exception
)
302 return(*(new MysqlCDriver(_rxFactory
)));
305 /* {{{ connectivity::mysqlc::release() -I- */
306 void release(oslInterlockedCount
& _refCount
,
307 ::cppu::OBroadcastHelper
& rBHelper
,
308 Reference
< XInterface
>& _xInterface
,
309 ::com::sun::star::lang::XComponent
* _pObject
)
311 if (osl_atomic_decrement(&_refCount
) == 0) {
312 osl_atomic_increment(&_refCount
);
314 if (!rBHelper
.bDisposed
&& !rBHelper
.bInDispose
) {
315 // remember the parent
316 Reference
< XInterface
> xParent
;
318 ::osl::MutexGuard
aGuard(rBHelper
.rMutex
);
319 xParent
= _xInterface
;
326 // only the alive ref holds the object
327 OSL_ASSERT(_refCount
== 1);
329 // release the parent in the destructor
331 ::osl::MutexGuard
aGuard(rBHelper
.rMutex
);
332 _xInterface
= xParent
;
336 osl_atomic_increment(&_refCount
);
343 /* {{{ connectivity::mysqlc::checkDisposed() -I- */
344 void checkDisposed(sal_Bool _bThrow
)
345 throw (DisposedException
)
348 throw DisposedException();
361 * vim600: noet sw=4 ts=4 fdm=marker
362 * vim<600: noet sw=4 ts=4
365 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */