xref: /AOO41X/main/mysqlc/source/mysqlc_driver.cxx (revision 079eb5772d0a9e49bbf5c2cd738fc5b5d43e5181)
1*079eb577SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*079eb577SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*079eb577SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*079eb577SAndrew Rist  * distributed with this work for additional information
6*079eb577SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*079eb577SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*079eb577SAndrew Rist  * "License"); you may not use this file except in compliance
9*079eb577SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*079eb577SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*079eb577SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*079eb577SAndrew Rist  * software distributed under the License is distributed on an
15*079eb577SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*079eb577SAndrew Rist  * KIND, either express or implied.  See the License for the
17*079eb577SAndrew Rist  * specific language governing permissions and limitations
18*079eb577SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*079eb577SAndrew Rist  *************************************************************/
21cdf0e10cSrcweir #include "mysqlc_driver.hxx"
22cdf0e10cSrcweir #include "mysqlc_connection.hxx"
23cdf0e10cSrcweir #include "mysqlc_general.hxx"
24cdf0e10cSrcweir 
25cdf0e10cSrcweir using namespace com::sun::star::uno;
26cdf0e10cSrcweir using namespace com::sun::star::lang;
27cdf0e10cSrcweir using namespace com::sun::star::beans;
28cdf0e10cSrcweir using namespace com::sun::star::sdbc;
29cdf0e10cSrcweir using namespace connectivity::mysqlc;
30cdf0e10cSrcweir using ::rtl::OUString;
31cdf0e10cSrcweir #include <stdio.h>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <preextstl.h>
34cdf0e10cSrcweir #include <cppconn/exception.h>
35cdf0e10cSrcweir #include <mysql_driver.h>
36cdf0e10cSrcweir #include <postextstl.h>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir 
39cdf0e10cSrcweir /* {{{ MysqlCDriver::MysqlCDriver() -I- */
MysqlCDriver(const Reference<XMultiServiceFactory> & _rxFactory)40cdf0e10cSrcweir MysqlCDriver::MysqlCDriver(const Reference< XMultiServiceFactory >& _rxFactory)
41cdf0e10cSrcweir 	: ODriver_BASE(m_aMutex)
42cdf0e10cSrcweir     ,m_xFactory(_rxFactory)
43cdf0e10cSrcweir {
44cdf0e10cSrcweir 	OSL_TRACE("MysqlCDriver::MysqlCDriver");
45cdf0e10cSrcweir 	cppDriver = NULL;
46cdf0e10cSrcweir }
47cdf0e10cSrcweir /* }}} */
48cdf0e10cSrcweir 
49cdf0e10cSrcweir 
50cdf0e10cSrcweir /* {{{ MysqlCDriver::disposing() -I- */
disposing()51cdf0e10cSrcweir void MysqlCDriver::disposing()
52cdf0e10cSrcweir {
53cdf0e10cSrcweir 	OSL_TRACE("MysqlCDriver::disposing");
54cdf0e10cSrcweir 	::osl::MutexGuard aGuard(m_aMutex);
55cdf0e10cSrcweir 
56cdf0e10cSrcweir 	// when driver will be destroied so all our connections have to be destroied as well
57cdf0e10cSrcweir 	for (OWeakRefArray::iterator i = m_xConnections.begin(); m_xConnections.end() != i; ++i)
58cdf0e10cSrcweir 	{
59cdf0e10cSrcweir 		Reference< XComponent > xComp(i->get(), UNO_QUERY);
60cdf0e10cSrcweir 		if (xComp.is()) {
61cdf0e10cSrcweir 			xComp->dispose();
62cdf0e10cSrcweir 		}
63cdf0e10cSrcweir 	}
64cdf0e10cSrcweir 	m_xConnections.clear();
65cdf0e10cSrcweir 
66cdf0e10cSrcweir 	ODriver_BASE::disposing();
67cdf0e10cSrcweir }
68cdf0e10cSrcweir /* }}} */
69cdf0e10cSrcweir 
70cdf0e10cSrcweir 
71cdf0e10cSrcweir // static ServiceInfo
72cdf0e10cSrcweir /* {{{ MysqlCDriver::getImplementationName_Static() -I- */
getImplementationName_Static()73cdf0e10cSrcweir OUString MysqlCDriver::getImplementationName_Static()
74cdf0e10cSrcweir 	throw(RuntimeException)
75cdf0e10cSrcweir {
76cdf0e10cSrcweir 	OSL_TRACE("MysqlCDriver::getImplementationName_Static");
77cdf0e10cSrcweir     return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.sdbc.mysqlc.MysqlCDriver" ) );
78cdf0e10cSrcweir }
79cdf0e10cSrcweir /* }}} */
80cdf0e10cSrcweir 
81cdf0e10cSrcweir 
82cdf0e10cSrcweir /* {{{ MysqlCDriver::getSupportedServiceNames_Static() -I- */
getSupportedServiceNames_Static()83cdf0e10cSrcweir Sequence< OUString > MysqlCDriver::getSupportedServiceNames_Static()
84cdf0e10cSrcweir 	throw(RuntimeException)
85cdf0e10cSrcweir {
86cdf0e10cSrcweir 	OSL_TRACE("MysqlCDriver::getSupportedServiceNames_Static");
87cdf0e10cSrcweir 	// which service is supported
88cdf0e10cSrcweir 	// for more information @see com.sun.star.sdbc.Driver
89cdf0e10cSrcweir 	Sequence< OUString > aSNS(1);
90cdf0e10cSrcweir 	aSNS[0] = OUString::createFromAscii("com.sun.star.sdbc.Driver");
91cdf0e10cSrcweir 	return aSNS;
92cdf0e10cSrcweir }
93cdf0e10cSrcweir /* }}} */
94cdf0e10cSrcweir 
95cdf0e10cSrcweir 
96cdf0e10cSrcweir /* {{{ MysqlCDriver::getImplementationName() -I- */
getImplementationName()97cdf0e10cSrcweir OUString SAL_CALL MysqlCDriver::getImplementationName()
98cdf0e10cSrcweir 	throw(RuntimeException)
99cdf0e10cSrcweir {
100cdf0e10cSrcweir 	OSL_TRACE("MysqlCDriver::getImplementationName");
101cdf0e10cSrcweir 	return getImplementationName_Static();
102cdf0e10cSrcweir }
103cdf0e10cSrcweir /* }}} */
104cdf0e10cSrcweir 
105cdf0e10cSrcweir 
106cdf0e10cSrcweir /* {{{ MysqlCDriver::supportsService() -I- */
supportsService(const OUString & _rServiceName)107cdf0e10cSrcweir sal_Bool SAL_CALL MysqlCDriver::supportsService(const OUString& _rServiceName)
108cdf0e10cSrcweir 	throw(RuntimeException)
109cdf0e10cSrcweir {
110cdf0e10cSrcweir 	OSL_TRACE("MysqlCDriver::supportsService");
111cdf0e10cSrcweir 	Sequence< OUString > aSupported(getSupportedServiceNames());
112cdf0e10cSrcweir 	const OUString* pSupported = aSupported.getConstArray();
113cdf0e10cSrcweir 	const OUString* pEnd = pSupported + aSupported.getLength();
114cdf0e10cSrcweir 	for (;pSupported != pEnd && !pSupported->equals(_rServiceName); ++pSupported){}
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 	return (pSupported != pEnd);
117cdf0e10cSrcweir }
118cdf0e10cSrcweir /* }}} */
119cdf0e10cSrcweir 
120cdf0e10cSrcweir 
121cdf0e10cSrcweir /* {{{ MysqlCDriver::getSupportedServiceNames() -I- */
getSupportedServiceNames()122cdf0e10cSrcweir Sequence< OUString > SAL_CALL MysqlCDriver::getSupportedServiceNames()
123cdf0e10cSrcweir 	throw(RuntimeException)
124cdf0e10cSrcweir {
125cdf0e10cSrcweir 	OSL_TRACE("MysqlCDriver::getSupportedServiceNames");
126cdf0e10cSrcweir 	return getSupportedServiceNames_Static();
127cdf0e10cSrcweir }
128cdf0e10cSrcweir /* }}} */
129cdf0e10cSrcweir 
130cdf0e10cSrcweir 
thisModule()131cdf0e10cSrcweir extern "C" { static void SAL_CALL thisModule() {} }
132cdf0e10cSrcweir 
impl_initCppConn_lck_throw()133cdf0e10cSrcweir void MysqlCDriver::impl_initCppConn_lck_throw()
134cdf0e10cSrcweir {
135cdf0e10cSrcweir     cppDriver = get_driver_instance();
136cdf0e10cSrcweir     if ( !cppDriver )
137cdf0e10cSrcweir     {
138cdf0e10cSrcweir         throw SQLException(
139cdf0e10cSrcweir             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Unable to obtain the MySQL_Driver instance from Connector/C++." ) ),
140cdf0e10cSrcweir             *this,
141cdf0e10cSrcweir             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "08001" ) ),  // "unable to connect"
142cdf0e10cSrcweir             0,
143cdf0e10cSrcweir             Any()
144cdf0e10cSrcweir         );
145cdf0e10cSrcweir     }
146cdf0e10cSrcweir }
147cdf0e10cSrcweir 
148cdf0e10cSrcweir /* {{{ MysqlCDriver::connect() -I- */
connect(const OUString & url,const Sequence<PropertyValue> & info)149cdf0e10cSrcweir Reference< XConnection > SAL_CALL MysqlCDriver::connect(const OUString& url, const Sequence< PropertyValue >& info)
150cdf0e10cSrcweir 	throw(SQLException, RuntimeException)
151cdf0e10cSrcweir {
152cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
153cdf0e10cSrcweir 
154cdf0e10cSrcweir 	OSL_TRACE("MysqlCDriver::connect");
155cdf0e10cSrcweir 	if (!acceptsURL(url)) {
156cdf0e10cSrcweir 		return NULL;
157cdf0e10cSrcweir 	}
158cdf0e10cSrcweir 
159cdf0e10cSrcweir     if ( !cppDriver )
160cdf0e10cSrcweir     {
161cdf0e10cSrcweir         impl_initCppConn_lck_throw();
162cdf0e10cSrcweir         OSL_POSTCOND( cppDriver, "MySQLCDriver::connect: internal error." );
163cdf0e10cSrcweir         if ( !cppDriver )
164cdf0e10cSrcweir             throw RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MySQLCDriver::connect: internal error." ) ), *this );
165cdf0e10cSrcweir     }
166cdf0e10cSrcweir 
167cdf0e10cSrcweir     Reference< XConnection > xConn;
168cdf0e10cSrcweir 	// create a new connection with the given properties and append it to our vector
169cdf0e10cSrcweir 	try {
170cdf0e10cSrcweir 		OConnection* pCon = new OConnection(*this, cppDriver);
171cdf0e10cSrcweir 		xConn = pCon;
172cdf0e10cSrcweir 
173cdf0e10cSrcweir         pCon->construct(url,info);
174cdf0e10cSrcweir         m_xConnections.push_back(WeakReferenceHelper(*pCon));
175cdf0e10cSrcweir 	}
176cdf0e10cSrcweir     catch (sql::SQLException &e)
177cdf0e10cSrcweir     {
178cdf0e10cSrcweir 		mysqlc_sdbc_driver::translateAndThrow(e, *this, getDefaultEncoding());
179cdf0e10cSrcweir 	}
180cdf0e10cSrcweir     return xConn;
181cdf0e10cSrcweir }
182cdf0e10cSrcweir /* }}} */
183cdf0e10cSrcweir 
184cdf0e10cSrcweir 
185cdf0e10cSrcweir /* {{{ MysqlCDriver::acceptsURL() -I- */
acceptsURL(const OUString & url)186cdf0e10cSrcweir sal_Bool SAL_CALL MysqlCDriver::acceptsURL(const OUString& url)
187cdf0e10cSrcweir 		throw(SQLException, RuntimeException)
188cdf0e10cSrcweir {
189cdf0e10cSrcweir 	OSL_TRACE("MysqlCDriver::acceptsURL");
190cdf0e10cSrcweir 	return (!url.compareTo(OUString::createFromAscii("sdbc:mysqlc:"), sizeof("sdbc:mysqlc:")-1));
191cdf0e10cSrcweir }
192cdf0e10cSrcweir /* }}} */
193cdf0e10cSrcweir 
194cdf0e10cSrcweir 
195cdf0e10cSrcweir /* {{{ MysqlCDriver::getPropertyInfo() -I- */
getPropertyInfo(const OUString & url,const Sequence<PropertyValue> &)196cdf0e10cSrcweir Sequence< DriverPropertyInfo > SAL_CALL MysqlCDriver::getPropertyInfo(const OUString& url, const Sequence< PropertyValue >& /* info */)
197cdf0e10cSrcweir 	throw(SQLException, RuntimeException)
198cdf0e10cSrcweir {
199cdf0e10cSrcweir 	OSL_TRACE("MysqlCDriver::getPropertyInfo");
200cdf0e10cSrcweir 	if (acceptsURL(url)) {
201cdf0e10cSrcweir 		::std::vector< DriverPropertyInfo > aDriverInfo;
202cdf0e10cSrcweir 
203cdf0e10cSrcweir 		aDriverInfo.push_back(DriverPropertyInfo(
204cdf0e10cSrcweir 				OUString(RTL_CONSTASCII_USTRINGPARAM("Hostname"))
205cdf0e10cSrcweir 				,OUString(RTL_CONSTASCII_USTRINGPARAM("Name of host"))
206cdf0e10cSrcweir 				,sal_True
207cdf0e10cSrcweir 				,OUString::createFromAscii("localhost")
208cdf0e10cSrcweir 				,Sequence< OUString >())
209cdf0e10cSrcweir 			);
210cdf0e10cSrcweir 		aDriverInfo.push_back(DriverPropertyInfo(
211cdf0e10cSrcweir 				OUString(RTL_CONSTASCII_USTRINGPARAM("Port"))
212cdf0e10cSrcweir 				,OUString(RTL_CONSTASCII_USTRINGPARAM("Port"))
213cdf0e10cSrcweir 				,sal_True
214cdf0e10cSrcweir 				,OUString::createFromAscii("3306")
215cdf0e10cSrcweir 				,Sequence< OUString >())
216cdf0e10cSrcweir 			);
217cdf0e10cSrcweir 		return Sequence< DriverPropertyInfo >(&(aDriverInfo[0]),aDriverInfo.size());
218cdf0e10cSrcweir 	}
219cdf0e10cSrcweir 
220cdf0e10cSrcweir 	return Sequence< DriverPropertyInfo >();
221cdf0e10cSrcweir }
222cdf0e10cSrcweir /* }}} */
223cdf0e10cSrcweir 
224cdf0e10cSrcweir 
225cdf0e10cSrcweir /* {{{ MysqlCDriver::getMajorVersion() -I- */
getMajorVersion()226cdf0e10cSrcweir sal_Int32 SAL_CALL MysqlCDriver::getMajorVersion()
227cdf0e10cSrcweir 	throw(RuntimeException)
228cdf0e10cSrcweir {
229cdf0e10cSrcweir 	OSL_TRACE("MysqlCDriver::getMajorVersion");
230cdf0e10cSrcweir 	return MYSQLC_VERSION_MAJOR;
231cdf0e10cSrcweir }
232cdf0e10cSrcweir /* }}} */
233cdf0e10cSrcweir 
234cdf0e10cSrcweir 
235cdf0e10cSrcweir /* {{{ MysqlCDriver::getMinorVersion() -I- */
getMinorVersion()236cdf0e10cSrcweir sal_Int32 SAL_CALL MysqlCDriver::getMinorVersion()
237cdf0e10cSrcweir 	throw(RuntimeException)
238cdf0e10cSrcweir {
239cdf0e10cSrcweir 	OSL_TRACE("MysqlCDriver::getMinorVersion");
240cdf0e10cSrcweir 	return MYSQLC_VERSION_MINOR;
241cdf0e10cSrcweir }
242cdf0e10cSrcweir /* }}} */
243cdf0e10cSrcweir 
244cdf0e10cSrcweir 
245cdf0e10cSrcweir namespace connectivity
246cdf0e10cSrcweir {
247cdf0e10cSrcweir namespace mysqlc
248cdf0e10cSrcweir {
249cdf0e10cSrcweir 
MysqlCDriver_CreateInstance(const Reference<XMultiServiceFactory> & _rxFactory)250cdf0e10cSrcweir Reference< XInterface >  SAL_CALL MysqlCDriver_CreateInstance(const Reference< XMultiServiceFactory >& _rxFactory)
251cdf0e10cSrcweir 	throw(::com::sun::star::uno::Exception)
252cdf0e10cSrcweir {
253cdf0e10cSrcweir 	return(*(new MysqlCDriver(_rxFactory)));
254cdf0e10cSrcweir }
255cdf0e10cSrcweir 
256cdf0e10cSrcweir /* {{{ connectivity::mysqlc::release() -I- */
release(oslInterlockedCount & _refCount,::cppu::OBroadcastHelper & rBHelper,Reference<XInterface> & _xInterface,::com::sun::star::lang::XComponent * _pObject)257cdf0e10cSrcweir void release(oslInterlockedCount& _refCount,
258cdf0e10cSrcweir 			 ::cppu::OBroadcastHelper& rBHelper,
259cdf0e10cSrcweir 			 Reference< XInterface >& _xInterface,
260cdf0e10cSrcweir 			 ::com::sun::star::lang::XComponent* _pObject)
261cdf0e10cSrcweir {
262cdf0e10cSrcweir 	if (osl_decrementInterlockedCount(&_refCount) == 0) {
263cdf0e10cSrcweir 		osl_incrementInterlockedCount(&_refCount);
264cdf0e10cSrcweir 
265cdf0e10cSrcweir 		if (!rBHelper.bDisposed && !rBHelper.bInDispose) {
266cdf0e10cSrcweir 			// remember the parent
267cdf0e10cSrcweir 			Reference< XInterface > xParent;
268cdf0e10cSrcweir 			{
269cdf0e10cSrcweir 				::osl::MutexGuard aGuard(rBHelper.rMutex);
270cdf0e10cSrcweir 				xParent = _xInterface;
271cdf0e10cSrcweir 				_xInterface = NULL;
272cdf0e10cSrcweir 			}
273cdf0e10cSrcweir 
274cdf0e10cSrcweir 			// First dispose
275cdf0e10cSrcweir 			_pObject->dispose();
276cdf0e10cSrcweir 
277cdf0e10cSrcweir 			// only the alive ref holds the object
278cdf0e10cSrcweir 			OSL_ASSERT(_refCount == 1);
279cdf0e10cSrcweir 
280cdf0e10cSrcweir 			// release the parent in the destructor
281cdf0e10cSrcweir 			if (xParent.is()) {
282cdf0e10cSrcweir 				::osl::MutexGuard aGuard(rBHelper.rMutex);
283cdf0e10cSrcweir 				_xInterface = xParent;
284cdf0e10cSrcweir 			}
285cdf0e10cSrcweir 		}
286cdf0e10cSrcweir 	} else {
287cdf0e10cSrcweir 		osl_incrementInterlockedCount(&_refCount);
288cdf0e10cSrcweir 	}
289cdf0e10cSrcweir }
290cdf0e10cSrcweir /* }}} */
291cdf0e10cSrcweir 
292cdf0e10cSrcweir 
293cdf0e10cSrcweir 
294cdf0e10cSrcweir /* {{{ connectivity::mysqlc::checkDisposed() -I- */
checkDisposed(sal_Bool _bThrow)295cdf0e10cSrcweir void checkDisposed(sal_Bool _bThrow)
296cdf0e10cSrcweir 	throw (DisposedException)
297cdf0e10cSrcweir {
298cdf0e10cSrcweir 	if (_bThrow) {
299cdf0e10cSrcweir 		throw DisposedException();
300cdf0e10cSrcweir 	}
301cdf0e10cSrcweir }
302cdf0e10cSrcweir /* }}} */
303cdf0e10cSrcweir 
304cdf0e10cSrcweir } /* mysqlc */
305cdf0e10cSrcweir } /* connectivity */
306cdf0e10cSrcweir 
307cdf0e10cSrcweir /*
308cdf0e10cSrcweir  * Local variables:
309cdf0e10cSrcweir  * tab-width: 4
310cdf0e10cSrcweir  * c-basic-offset: 4
311cdf0e10cSrcweir  * End:
312cdf0e10cSrcweir  * vim600: noet sw=4 ts=4 fdm=marker
313cdf0e10cSrcweir  * vim<600: noet sw=4 ts=4
314cdf0e10cSrcweir  */
315