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