Bump version to 4.1-6
[LibreOffice.git] / mysqlc / source / mysqlc_driver.cxx
blob7a6a32640122c85a66ea1450855a5775569d5047
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 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!");
157 throw SQLException(
158 OUString( "Unable to load the " CPPCONN_LIB " library." ),
159 *this,
160 OUString( "08001" ), // "unable to connect"
162 Any()
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 "!");
174 throw SQLException(
175 OUString( CPPCONN_LIB " is invalid: missing the driver factory function." ),
176 *this,
177 OUString( "08001" ), // "unable to connect"
179 Any()
183 cppDriver = static_cast< sql::Driver* >( (*pFactoryFunction)() );
184 #endif
185 if ( !cppDriver )
187 throw SQLException(
188 OUString( "Unable to obtain the MySQL_Driver instance from Connector/C++." ),
189 *this,
190 OUString( "08001" ), // "unable to connect"
192 Any()
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)) {
205 return NULL;
208 if ( !cppDriver )
210 impl_initCppConn_lck_throw();
211 OSL_POSTCOND( cppDriver, "MySQLCDriver::connect: internal error." );
212 if ( !cppDriver )
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
218 try {
219 OConnection* pCon = new OConnection(*this, cppDriver);
220 xConn = pCon;
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());
229 return xConn;
231 /* }}} */
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));
241 /* }}} */
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(
253 OUString("Hostname")
254 ,OUString("Name of host")
255 ,sal_True
256 ,OUString("localhost")
257 ,Sequence< OUString >())
259 aDriverInfo.push_back(DriverPropertyInfo(
260 OUString("Port")
261 ,OUString("Port")
262 ,sal_True
263 ,OUString("3306")
264 ,Sequence< OUString >())
266 return Sequence< DriverPropertyInfo >(&(aDriverInfo[0]),aDriverInfo.size());
269 return Sequence< DriverPropertyInfo >();
271 /* }}} */
274 /* {{{ MysqlCDriver::getMajorVersion() -I- */
275 sal_Int32 SAL_CALL MysqlCDriver::getMajorVersion()
276 throw(RuntimeException)
278 OSL_TRACE("MysqlCDriver::getMajorVersion");
279 return MARIADBC_VERSION_MAJOR;
281 /* }}} */
284 /* {{{ MysqlCDriver::getMinorVersion() -I- */
285 sal_Int32 SAL_CALL MysqlCDriver::getMinorVersion()
286 throw(RuntimeException)
288 OSL_TRACE("MysqlCDriver::getMinorVersion");
289 return MARIADBC_VERSION_MINOR;
291 /* }}} */
294 namespace connectivity
296 namespace mysqlc
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;
320 _xInterface = NULL;
323 // First dispose
324 _pObject->dispose();
326 // only the alive ref holds the object
327 OSL_ASSERT(_refCount == 1);
329 // release the parent in the destructor
330 if (xParent.is()) {
331 ::osl::MutexGuard aGuard(rBHelper.rMutex);
332 _xInterface = xParent;
335 } else {
336 osl_atomic_increment(&_refCount);
339 /* }}} */
343 /* {{{ connectivity::mysqlc::checkDisposed() -I- */
344 void checkDisposed(sal_Bool _bThrow)
345 throw (DisposedException)
347 if (_bThrow) {
348 throw DisposedException();
351 /* }}} */
353 } /* mysqlc */
354 } /* connectivity */
357 * Local variables:
358 * tab-width: 4
359 * c-basic-offset: 4
360 * End:
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: */