xref: /AOO41X/main/mysqlc/source/mysqlc_driver.cxx (revision 8809db7a87f97847b57a57f4cd2b0104b2b83182)
1 /*************************************************************************
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * Copyright 2008 by Sun Microsystems, Inc.
5 *
6 * OpenOffice.org - a multi-platform office productivity suite
7 *
8 * $RCSfile: mysqlc_driver.cxx,v $
9 *
10 * $Revision: 1.1.2.5 $
11 *
12 * This file is part of OpenOffice.org.
13 *
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
17 *
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
23 *
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org.  If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
28 ************************************************************************/
29 #include "mysqlc_driver.hxx"
30 #include "mysqlc_connection.hxx"
31 #include "mysqlc_general.hxx"
32 
33 using namespace com::sun::star::uno;
34 using namespace com::sun::star::lang;
35 using namespace com::sun::star::beans;
36 using namespace com::sun::star::sdbc;
37 using namespace connectivity::mysqlc;
38 using ::rtl::OUString;
39 #include <stdio.h>
40 
41 #include <preextstl.h>
42 #include <cppconn/exception.h>
43 #include <mysql_driver.h>
44 #include <postextstl.h>
45 
46 
47 /* {{{ MysqlCDriver::MysqlCDriver() -I- */
48 MysqlCDriver::MysqlCDriver(const Reference< XMultiServiceFactory >& _rxFactory)
49     : ODriver_BASE(m_aMutex)
50     ,m_xFactory(_rxFactory)
51 {
52     OSL_TRACE("MysqlCDriver::MysqlCDriver");
53     cppDriver = NULL;
54 }
55 /* }}} */
56 
57 
58 /* {{{ MysqlCDriver::disposing() -I- */
59 void MysqlCDriver::disposing()
60 {
61     OSL_TRACE("MysqlCDriver::disposing");
62     ::osl::MutexGuard aGuard(m_aMutex);
63 
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)
66     {
67         Reference< XComponent > xComp(i->get(), UNO_QUERY);
68         if (xComp.is()) {
69             xComp->dispose();
70         }
71     }
72     m_xConnections.clear();
73 
74     ODriver_BASE::disposing();
75 }
76 /* }}} */
77 
78 
79 // static ServiceInfo
80 /* {{{ MysqlCDriver::getImplementationName_Static() -I- */
81 OUString MysqlCDriver::getImplementationName_Static()
82     throw(RuntimeException)
83 {
84     OSL_TRACE("MysqlCDriver::getImplementationName_Static");
85     return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.sdbc.mysqlc.MysqlCDriver" ) );
86 }
87 /* }}} */
88 
89 
90 /* {{{ MysqlCDriver::getSupportedServiceNames_Static() -I- */
91 Sequence< OUString > MysqlCDriver::getSupportedServiceNames_Static()
92     throw(RuntimeException)
93 {
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::createFromAscii("com.sun.star.sdbc.Driver");
99     return aSNS;
100 }
101 /* }}} */
102 
103 
104 /* {{{ MysqlCDriver::getImplementationName() -I- */
105 OUString SAL_CALL MysqlCDriver::getImplementationName()
106     throw(RuntimeException)
107 {
108     OSL_TRACE("MysqlCDriver::getImplementationName");
109     return getImplementationName_Static();
110 }
111 /* }}} */
112 
113 
114 /* {{{ MysqlCDriver::supportsService() -I- */
115 sal_Bool SAL_CALL MysqlCDriver::supportsService(const OUString& _rServiceName)
116     throw(RuntimeException)
117 {
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){}
123 
124     return (pSupported != pEnd);
125 }
126 /* }}} */
127 
128 
129 /* {{{ MysqlCDriver::getSupportedServiceNames() -I- */
130 Sequence< OUString > SAL_CALL MysqlCDriver::getSupportedServiceNames()
131     throw(RuntimeException)
132 {
133     OSL_TRACE("MysqlCDriver::getSupportedServiceNames");
134     return getSupportedServiceNames_Static();
135 }
136 /* }}} */
137 
138 
139 extern "C" { static void SAL_CALL thisModule() {} }
140 
141 void MysqlCDriver::impl_initCppConn_lck_throw()
142 {
143     cppDriver = get_driver_instance();
144     if ( !cppDriver )
145     {
146         throw SQLException(
147             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Unable to obtain the MySQL_Driver instance from Connector/C++." ) ),
148             *this,
149             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "08001" ) ),  // "unable to connect"
150             0,
151             Any()
152         );
153     }
154 }
155 
156 /* {{{ MysqlCDriver::connect() -I- */
157 Reference< XConnection > SAL_CALL MysqlCDriver::connect(const OUString& url, const Sequence< PropertyValue >& info)
158     throw(SQLException, RuntimeException)
159 {
160     ::osl::MutexGuard aGuard( m_aMutex );
161 
162     OSL_TRACE("MysqlCDriver::connect");
163     if (!acceptsURL(url)) {
164         return NULL;
165     }
166 
167     if ( !cppDriver )
168     {
169         impl_initCppConn_lck_throw();
170         OSL_POSTCOND( cppDriver, "MySQLCDriver::connect: internal error." );
171         if ( !cppDriver )
172             throw RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MySQLCDriver::connect: internal error." ) ), *this );
173     }
174 
175     Reference< XConnection > xConn;
176     // create a new connection with the given properties and append it to our vector
177     try {
178         OConnection* pCon = new OConnection(*this, cppDriver);
179         xConn = pCon;
180 
181         pCon->construct(url,info);
182         m_xConnections.push_back(WeakReferenceHelper(*pCon));
183     }
184     catch (sql::SQLException &e)
185     {
186         mysqlc_sdbc_driver::translateAndThrow(e, *this, getDefaultEncoding());
187     }
188     return xConn;
189 }
190 /* }}} */
191 
192 
193 /* {{{ MysqlCDriver::acceptsURL() -I- */
194 sal_Bool SAL_CALL MysqlCDriver::acceptsURL(const OUString& url)
195         throw(SQLException, RuntimeException)
196 {
197     OSL_TRACE("MysqlCDriver::acceptsURL");
198     return (!url.compareTo(OUString::createFromAscii("sdbc:mysqlc:"), sizeof("sdbc:mysqlc:")-1));
199 }
200 /* }}} */
201 
202 
203 /* {{{ MysqlCDriver::getPropertyInfo() -I- */
204 Sequence< DriverPropertyInfo > SAL_CALL MysqlCDriver::getPropertyInfo(const OUString& url, const Sequence< PropertyValue >& /* info */)
205     throw(SQLException, RuntimeException)
206 {
207     OSL_TRACE("MysqlCDriver::getPropertyInfo");
208     if (acceptsURL(url)) {
209         ::std::vector< DriverPropertyInfo > aDriverInfo;
210 
211         aDriverInfo.push_back(DriverPropertyInfo(
212                 OUString(RTL_CONSTASCII_USTRINGPARAM("Hostname"))
213                 ,OUString(RTL_CONSTASCII_USTRINGPARAM("Name of host"))
214                 ,sal_True
215                 ,OUString::createFromAscii("localhost")
216                 ,Sequence< OUString >())
217             );
218         aDriverInfo.push_back(DriverPropertyInfo(
219                 OUString(RTL_CONSTASCII_USTRINGPARAM("Port"))
220                 ,OUString(RTL_CONSTASCII_USTRINGPARAM("Port"))
221                 ,sal_True
222                 ,OUString::createFromAscii("3306")
223                 ,Sequence< OUString >())
224             );
225         return Sequence< DriverPropertyInfo >(&(aDriverInfo[0]),aDriverInfo.size());
226     }
227 
228     return Sequence< DriverPropertyInfo >();
229 }
230 /* }}} */
231 
232 
233 /* {{{ MysqlCDriver::getMajorVersion() -I- */
234 sal_Int32 SAL_CALL MysqlCDriver::getMajorVersion()
235     throw(RuntimeException)
236 {
237     OSL_TRACE("MysqlCDriver::getMajorVersion");
238     return MYSQLC_VERSION_MAJOR;
239 }
240 /* }}} */
241 
242 
243 /* {{{ MysqlCDriver::getMinorVersion() -I- */
244 sal_Int32 SAL_CALL MysqlCDriver::getMinorVersion()
245     throw(RuntimeException)
246 {
247     OSL_TRACE("MysqlCDriver::getMinorVersion");
248     return MYSQLC_VERSION_MINOR;
249 }
250 /* }}} */
251 
252 
253 namespace connectivity
254 {
255 namespace mysqlc
256 {
257 
258 Reference< XInterface >  SAL_CALL MysqlCDriver_CreateInstance(const Reference< XMultiServiceFactory >& _rxFactory)
259     throw(::com::sun::star::uno::Exception)
260 {
261     return(*(new MysqlCDriver(_rxFactory)));
262 }
263 
264 /* {{{ connectivity::mysqlc::release() -I- */
265 void release(oslInterlockedCount& _refCount,
266              ::cppu::OBroadcastHelper& rBHelper,
267              Reference< XInterface >& _xInterface,
268              ::com::sun::star::lang::XComponent* _pObject)
269 {
270     if (osl_decrementInterlockedCount(&_refCount) == 0) {
271         osl_incrementInterlockedCount(&_refCount);
272 
273         if (!rBHelper.bDisposed && !rBHelper.bInDispose) {
274             // remember the parent
275             Reference< XInterface > xParent;
276             {
277                 ::osl::MutexGuard aGuard(rBHelper.rMutex);
278                 xParent = _xInterface;
279                 _xInterface = NULL;
280             }
281 
282             // First dispose
283             _pObject->dispose();
284 
285             // only the alive ref holds the object
286             OSL_ASSERT(_refCount == 1);
287 
288             // release the parent in the destructor
289             if (xParent.is()) {
290                 ::osl::MutexGuard aGuard(rBHelper.rMutex);
291                 _xInterface = xParent;
292             }
293         }
294     } else {
295         osl_incrementInterlockedCount(&_refCount);
296     }
297 }
298 /* }}} */
299 
300 
301 
302 /* {{{ connectivity::mysqlc::checkDisposed() -I- */
303 void checkDisposed(sal_Bool _bThrow)
304     throw (DisposedException)
305 {
306     if (_bThrow) {
307         throw DisposedException();
308     }
309 }
310 /* }}} */
311 
312 } /* mysqlc */
313 } /* connectivity */
314 
315 /*
316  * Local variables:
317  * tab-width: 4
318  * c-basic-offset: 4
319  * End:
320  * vim600: noet sw=4 ts=4 fdm=marker
321  * vim<600: noet sw=4 ts=4
322  */
323