xref: /trunk/main/connectivity/source/drivers/mysql/YDriver.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_connectivity.hxx"
30*cdf0e10cSrcweir #include "mysql/YDriver.hxx"
31*cdf0e10cSrcweir #include "mysql/YCatalog.hxx"
32*cdf0e10cSrcweir #include <osl/diagnose.h>
33*cdf0e10cSrcweir #include <comphelper/namedvaluecollection.hxx>
34*cdf0e10cSrcweir #include "connectivity/dbexception.hxx"
35*cdf0e10cSrcweir #include <connectivity/dbcharset.hxx>
36*cdf0e10cSrcweir #include <com/sun/star/sdbc/XDriverAccess.hpp>
37*cdf0e10cSrcweir #include "TConnection.hxx"
38*cdf0e10cSrcweir #include "resource/common_res.hrc"
39*cdf0e10cSrcweir #include "resource/sharedresources.hxx"
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir //........................................................................
42*cdf0e10cSrcweir namespace connectivity
43*cdf0e10cSrcweir {
44*cdf0e10cSrcweir //........................................................................
45*cdf0e10cSrcweir     using namespace mysql;
46*cdf0e10cSrcweir     using namespace ::com::sun::star::uno;
47*cdf0e10cSrcweir     using namespace ::com::sun::star::sdbc;
48*cdf0e10cSrcweir     using namespace ::com::sun::star::sdbcx;
49*cdf0e10cSrcweir     using namespace ::com::sun::star::beans;
50*cdf0e10cSrcweir     using namespace ::com::sun::star::lang;
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir     namespace mysql
53*cdf0e10cSrcweir     {
54*cdf0e10cSrcweir         Reference< XInterface >  SAL_CALL ODriverDelegator_CreateInstance(const Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxFac) throw( Exception )
55*cdf0e10cSrcweir         {
56*cdf0e10cSrcweir             return *(new ODriverDelegator(_rxFac));
57*cdf0e10cSrcweir         }
58*cdf0e10cSrcweir     }
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir     //====================================================================
62*cdf0e10cSrcweir     //= ODriverDelegator
63*cdf0e10cSrcweir     //====================================================================
64*cdf0e10cSrcweir     //--------------------------------------------------------------------
65*cdf0e10cSrcweir     ODriverDelegator::ODriverDelegator(const Reference< XMultiServiceFactory >& _rxFactory)
66*cdf0e10cSrcweir         : ODriverDelegator_BASE(m_aMutex)
67*cdf0e10cSrcweir         ,m_xFactory(_rxFactory)
68*cdf0e10cSrcweir         ,m_eDriverType(D_ODBC)
69*cdf0e10cSrcweir     {
70*cdf0e10cSrcweir     }
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir     //--------------------------------------------------------------------
73*cdf0e10cSrcweir     ODriverDelegator::~ODriverDelegator()
74*cdf0e10cSrcweir     {
75*cdf0e10cSrcweir         try
76*cdf0e10cSrcweir         {
77*cdf0e10cSrcweir             ::comphelper::disposeComponent(m_xODBCDriver);
78*cdf0e10cSrcweir             ::comphelper::disposeComponent(m_xNativeDriver);
79*cdf0e10cSrcweir             TJDBCDrivers::iterator aIter = m_aJdbcDrivers.begin();
80*cdf0e10cSrcweir             TJDBCDrivers::iterator aEnd = m_aJdbcDrivers.end();
81*cdf0e10cSrcweir             for ( ;aIter != aEnd;++aIter )
82*cdf0e10cSrcweir                 ::comphelper::disposeComponent(aIter->second);
83*cdf0e10cSrcweir         }
84*cdf0e10cSrcweir         catch(const Exception&)
85*cdf0e10cSrcweir         {
86*cdf0e10cSrcweir         }
87*cdf0e10cSrcweir     }
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir     // --------------------------------------------------------------------------------
90*cdf0e10cSrcweir     void ODriverDelegator::disposing()
91*cdf0e10cSrcweir     {
92*cdf0e10cSrcweir         ::osl::MutexGuard aGuard(m_aMutex);
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir         for (TWeakPairVector::iterator i = m_aConnections.begin(); m_aConnections.end() != i; ++i)
96*cdf0e10cSrcweir         {
97*cdf0e10cSrcweir             Reference<XInterface > xTemp = i->first.get();
98*cdf0e10cSrcweir             ::comphelper::disposeComponent(xTemp);
99*cdf0e10cSrcweir         }
100*cdf0e10cSrcweir         m_aConnections.clear();
101*cdf0e10cSrcweir         TWeakPairVector().swap(m_aConnections);
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir         ODriverDelegator_BASE::disposing();
104*cdf0e10cSrcweir     }
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir     namespace
107*cdf0e10cSrcweir     {
108*cdf0e10cSrcweir         sal_Bool isOdbcUrl(const ::rtl::OUString& _sUrl)
109*cdf0e10cSrcweir         {
110*cdf0e10cSrcweir             return _sUrl.copy(0,16).equalsAscii("sdbc:mysql:odbc:");
111*cdf0e10cSrcweir         }
112*cdf0e10cSrcweir         //--------------------------------------------------------------------
113*cdf0e10cSrcweir         sal_Bool isNativeUrl(const ::rtl::OUString& _sUrl)
114*cdf0e10cSrcweir         {
115*cdf0e10cSrcweir             return (!_sUrl.compareTo(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("sdbc:mysql:mysqlc:")), sizeof("sdbc:mysql:mysqlc:")-1));
116*cdf0e10cSrcweir         }
117*cdf0e10cSrcweir         //--------------------------------------------------------------------
118*cdf0e10cSrcweir         T_DRIVERTYPE lcl_getDriverType(const ::rtl::OUString& _sUrl)
119*cdf0e10cSrcweir         {
120*cdf0e10cSrcweir             T_DRIVERTYPE eRet = D_JDBC;
121*cdf0e10cSrcweir             if ( isOdbcUrl(_sUrl ) )
122*cdf0e10cSrcweir                 eRet = D_ODBC;
123*cdf0e10cSrcweir             else if ( isNativeUrl(_sUrl ) )
124*cdf0e10cSrcweir                 eRet = D_NATIVE;
125*cdf0e10cSrcweir             return eRet;
126*cdf0e10cSrcweir         }
127*cdf0e10cSrcweir         //--------------------------------------------------------------------
128*cdf0e10cSrcweir         ::rtl::OUString transformUrl(const ::rtl::OUString& _sUrl)
129*cdf0e10cSrcweir         {
130*cdf0e10cSrcweir             ::rtl::OUString sNewUrl = _sUrl.copy(11);
131*cdf0e10cSrcweir             if ( isOdbcUrl( _sUrl ) )
132*cdf0e10cSrcweir                 sNewUrl = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("sdbc:")) + sNewUrl;
133*cdf0e10cSrcweir             else if ( isNativeUrl( _sUrl ) )
134*cdf0e10cSrcweir                 sNewUrl = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("sdbc:")) + sNewUrl;
135*cdf0e10cSrcweir             else
136*cdf0e10cSrcweir             {
137*cdf0e10cSrcweir                 sNewUrl = sNewUrl.copy(5);
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir                 ::rtl::OUString sTempUrl = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("jdbc:mysql://"));
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir                 sTempUrl += sNewUrl;
142*cdf0e10cSrcweir                 sNewUrl = sTempUrl;
143*cdf0e10cSrcweir             }
144*cdf0e10cSrcweir             return sNewUrl;
145*cdf0e10cSrcweir         }
146*cdf0e10cSrcweir         //--------------------------------------------------------------------
147*cdf0e10cSrcweir         Reference< XDriver > lcl_loadDriver(const Reference< XMultiServiceFactory >& _rxFactory,const ::rtl::OUString& _sUrl)
148*cdf0e10cSrcweir         {
149*cdf0e10cSrcweir             Reference<XDriverAccess> xDriverAccess(_rxFactory->createInstance(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.sdbc.DriverManager")) ),UNO_QUERY);
150*cdf0e10cSrcweir             OSL_ENSURE(xDriverAccess.is(),"Could not load driver manager!");
151*cdf0e10cSrcweir             Reference< XDriver > xDriver;
152*cdf0e10cSrcweir             if ( xDriverAccess.is() )
153*cdf0e10cSrcweir                 xDriver = xDriverAccess->getDriverByURL(_sUrl);
154*cdf0e10cSrcweir             return xDriver;
155*cdf0e10cSrcweir         }
156*cdf0e10cSrcweir         //--------------------------------------------------------------------
157*cdf0e10cSrcweir         Sequence< PropertyValue > lcl_convertProperties(T_DRIVERTYPE _eType,const Sequence< PropertyValue >& info,const ::rtl::OUString& _sUrl)
158*cdf0e10cSrcweir         {
159*cdf0e10cSrcweir             ::std::vector<PropertyValue> aProps;
160*cdf0e10cSrcweir             const PropertyValue* pSupported = info.getConstArray();
161*cdf0e10cSrcweir             const PropertyValue* pEnd = pSupported + info.getLength();
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir             aProps.reserve(info.getLength() + 5);
164*cdf0e10cSrcweir             for (;pSupported != pEnd; ++pSupported)
165*cdf0e10cSrcweir             {
166*cdf0e10cSrcweir                 aProps.push_back( *pSupported );
167*cdf0e10cSrcweir             }
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir             if ( _eType == D_ODBC )
170*cdf0e10cSrcweir             {
171*cdf0e10cSrcweir                 aProps.push_back( PropertyValue(
172*cdf0e10cSrcweir                                     ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Silent"))
173*cdf0e10cSrcweir                                     ,0
174*cdf0e10cSrcweir                                     ,makeAny(sal_True)
175*cdf0e10cSrcweir                                     ,PropertyState_DIRECT_VALUE) );
176*cdf0e10cSrcweir                 aProps.push_back( PropertyValue(
177*cdf0e10cSrcweir                                     ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("PreventGetVersionColumns"))
178*cdf0e10cSrcweir                                     ,0
179*cdf0e10cSrcweir                                     ,makeAny(sal_True)
180*cdf0e10cSrcweir                                     ,PropertyState_DIRECT_VALUE) );
181*cdf0e10cSrcweir             }
182*cdf0e10cSrcweir             else if ( _eType == D_JDBC )
183*cdf0e10cSrcweir             {
184*cdf0e10cSrcweir                 aProps.push_back( PropertyValue(
185*cdf0e10cSrcweir                                     ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("JavaDriverClass"))
186*cdf0e10cSrcweir                                     ,0
187*cdf0e10cSrcweir                                     ,makeAny(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.mysql.jdbc.Driver")))
188*cdf0e10cSrcweir                                     ,PropertyState_DIRECT_VALUE) );
189*cdf0e10cSrcweir             }
190*cdf0e10cSrcweir             else
191*cdf0e10cSrcweir             {
192*cdf0e10cSrcweir                 aProps.push_back( PropertyValue(
193*cdf0e10cSrcweir                                     ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("PublicConnectionURL"))
194*cdf0e10cSrcweir                                     ,0
195*cdf0e10cSrcweir                                     ,makeAny(_sUrl)
196*cdf0e10cSrcweir                                     ,PropertyState_DIRECT_VALUE) );
197*cdf0e10cSrcweir             }
198*cdf0e10cSrcweir             aProps.push_back( PropertyValue(
199*cdf0e10cSrcweir                                 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("IsAutoRetrievingEnabled"))
200*cdf0e10cSrcweir                                 ,0
201*cdf0e10cSrcweir                                 ,makeAny(sal_True)
202*cdf0e10cSrcweir                                 ,PropertyState_DIRECT_VALUE) );
203*cdf0e10cSrcweir             aProps.push_back( PropertyValue(
204*cdf0e10cSrcweir                                 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("AutoRetrievingStatement"))
205*cdf0e10cSrcweir                                 ,0
206*cdf0e10cSrcweir                                 ,makeAny(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("SELECT LAST_INSERT_ID()")))
207*cdf0e10cSrcweir                                 ,PropertyState_DIRECT_VALUE) );
208*cdf0e10cSrcweir             aProps.push_back( PropertyValue(
209*cdf0e10cSrcweir                                 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ParameterNameSubstitution"))
210*cdf0e10cSrcweir                                 ,0
211*cdf0e10cSrcweir                                 ,makeAny(sal_True)
212*cdf0e10cSrcweir                                 ,PropertyState_DIRECT_VALUE) );
213*cdf0e10cSrcweir             PropertyValue* pProps = aProps.empty() ? 0 : &aProps[0];
214*cdf0e10cSrcweir             return Sequence< PropertyValue >(pProps, aProps.size());
215*cdf0e10cSrcweir         }
216*cdf0e10cSrcweir     }
217*cdf0e10cSrcweir     //--------------------------------------------------------------------
218*cdf0e10cSrcweir     Reference< XDriver > ODriverDelegator::loadDriver( const ::rtl::OUString& url, const Sequence< PropertyValue >& info )
219*cdf0e10cSrcweir     {
220*cdf0e10cSrcweir         Reference< XDriver > xDriver;
221*cdf0e10cSrcweir         const ::rtl::OUString sCuttedUrl = transformUrl(url);
222*cdf0e10cSrcweir         const T_DRIVERTYPE eType = lcl_getDriverType( url );
223*cdf0e10cSrcweir         if ( eType == D_ODBC )
224*cdf0e10cSrcweir         {
225*cdf0e10cSrcweir             if ( !m_xODBCDriver.is() )
226*cdf0e10cSrcweir                 m_xODBCDriver = lcl_loadDriver(m_xFactory,sCuttedUrl);
227*cdf0e10cSrcweir             xDriver = m_xODBCDriver;
228*cdf0e10cSrcweir         } // if ( bIsODBC )
229*cdf0e10cSrcweir         else if ( eType == D_NATIVE )
230*cdf0e10cSrcweir         {
231*cdf0e10cSrcweir             if ( !m_xNativeDriver.is() )
232*cdf0e10cSrcweir                 m_xNativeDriver = lcl_loadDriver(m_xFactory,sCuttedUrl);
233*cdf0e10cSrcweir             xDriver = m_xNativeDriver;
234*cdf0e10cSrcweir         }
235*cdf0e10cSrcweir         else
236*cdf0e10cSrcweir         {
237*cdf0e10cSrcweir             ::comphelper::NamedValueCollection aSettings( info );
238*cdf0e10cSrcweir             ::rtl::OUString sDriverClass(RTL_CONSTASCII_USTRINGPARAM("com.mysql.jdbc.Driver"));
239*cdf0e10cSrcweir             sDriverClass = aSettings.getOrDefault( "JavaDriverClass", sDriverClass );
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir             TJDBCDrivers::iterator aFind = m_aJdbcDrivers.find(sDriverClass);
242*cdf0e10cSrcweir             if ( aFind == m_aJdbcDrivers.end() )
243*cdf0e10cSrcweir                 aFind = m_aJdbcDrivers.insert(TJDBCDrivers::value_type(sDriverClass,lcl_loadDriver(m_xFactory,sCuttedUrl))).first;
244*cdf0e10cSrcweir             xDriver = aFind->second;
245*cdf0e10cSrcweir         }
246*cdf0e10cSrcweir 
247*cdf0e10cSrcweir         return xDriver;
248*cdf0e10cSrcweir     }
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir     //--------------------------------------------------------------------
251*cdf0e10cSrcweir     Reference< XConnection > SAL_CALL ODriverDelegator::connect( const ::rtl::OUString& url, const Sequence< PropertyValue >& info ) throw (SQLException, RuntimeException)
252*cdf0e10cSrcweir     {
253*cdf0e10cSrcweir         Reference< XConnection > xConnection;
254*cdf0e10cSrcweir         if ( acceptsURL(url) )
255*cdf0e10cSrcweir         {
256*cdf0e10cSrcweir             Reference< XDriver > xDriver;
257*cdf0e10cSrcweir             xDriver = loadDriver(url,info);
258*cdf0e10cSrcweir             if ( xDriver.is() )
259*cdf0e10cSrcweir             {
260*cdf0e10cSrcweir                 ::rtl::OUString sCuttedUrl = transformUrl(url);
261*cdf0e10cSrcweir                 const T_DRIVERTYPE eType = lcl_getDriverType( url );
262*cdf0e10cSrcweir                 Sequence< PropertyValue > aConvertedProperties = lcl_convertProperties(eType,info,url);
263*cdf0e10cSrcweir                 if ( eType == D_JDBC )
264*cdf0e10cSrcweir                 {
265*cdf0e10cSrcweir                     ::comphelper::NamedValueCollection aSettings( info );
266*cdf0e10cSrcweir                     ::rtl::OUString sIanaName = aSettings.getOrDefault( "CharSet", ::rtl::OUString() );
267*cdf0e10cSrcweir                     if ( sIanaName.getLength() )
268*cdf0e10cSrcweir                     {
269*cdf0e10cSrcweir                         ::dbtools::OCharsetMap aLookupIanaName;
270*cdf0e10cSrcweir                         ::dbtools::OCharsetMap::const_iterator aLookup = aLookupIanaName.find(sIanaName, ::dbtools::OCharsetMap::IANA());
271*cdf0e10cSrcweir                         if (aLookup != aLookupIanaName.end() )
272*cdf0e10cSrcweir                         {
273*cdf0e10cSrcweir                             ::rtl::OUString sAdd;
274*cdf0e10cSrcweir                             if ( RTL_TEXTENCODING_UTF8 == (*aLookup).getEncoding() )
275*cdf0e10cSrcweir                             {
276*cdf0e10cSrcweir                                 static const ::rtl::OUString s_sCharSetOp(RTL_CONSTASCII_USTRINGPARAM("useUnicode=true&"));
277*cdf0e10cSrcweir                                 if ( !sCuttedUrl.matchIgnoreAsciiCase(s_sCharSetOp) )
278*cdf0e10cSrcweir                                 {
279*cdf0e10cSrcweir                                     sAdd = s_sCharSetOp;
280*cdf0e10cSrcweir                                 } // if ( !sCuttedUrl.matchIgnoreAsciiCase(s_sCharSetOp) )
281*cdf0e10cSrcweir                             } // if ( RTL_TEXTENCODING_UTF8 == (*aLookup).getEncoding() )
282*cdf0e10cSrcweir                             if ( sCuttedUrl.indexOf('?') == -1 )
283*cdf0e10cSrcweir                                 sCuttedUrl += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("?"));
284*cdf0e10cSrcweir                             else
285*cdf0e10cSrcweir                                 sCuttedUrl += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("&"));
286*cdf0e10cSrcweir                             sCuttedUrl += sAdd;
287*cdf0e10cSrcweir                             sCuttedUrl += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("characterEncoding="));
288*cdf0e10cSrcweir                             sCuttedUrl += sIanaName;
289*cdf0e10cSrcweir                         }
290*cdf0e10cSrcweir                     }
291*cdf0e10cSrcweir                 } // if ( !bIsODBC )
292*cdf0e10cSrcweir 
293*cdf0e10cSrcweir                 xConnection = xDriver->connect( sCuttedUrl, aConvertedProperties );
294*cdf0e10cSrcweir                 if ( xConnection.is() )
295*cdf0e10cSrcweir                 {
296*cdf0e10cSrcweir                     OMetaConnection* pMetaConnection = NULL;
297*cdf0e10cSrcweir                     // now we have to set the URL to get the correct answer for metadata()->getURL()
298*cdf0e10cSrcweir                     Reference< XUnoTunnel> xTunnel(xConnection,UNO_QUERY);
299*cdf0e10cSrcweir                     if ( xTunnel.is() )
300*cdf0e10cSrcweir                     {
301*cdf0e10cSrcweir                         pMetaConnection = reinterpret_cast<OMetaConnection*>(xTunnel->getSomething( OMetaConnection::getUnoTunnelImplementationId() ));
302*cdf0e10cSrcweir                         if ( pMetaConnection )
303*cdf0e10cSrcweir                             pMetaConnection->setURL(url);
304*cdf0e10cSrcweir                     }
305*cdf0e10cSrcweir                     m_aConnections.push_back(TWeakPair(WeakReferenceHelper(xConnection),TWeakConnectionPair(WeakReferenceHelper(),pMetaConnection)));
306*cdf0e10cSrcweir                 }
307*cdf0e10cSrcweir             }
308*cdf0e10cSrcweir         }
309*cdf0e10cSrcweir         return xConnection;
310*cdf0e10cSrcweir     }
311*cdf0e10cSrcweir 
312*cdf0e10cSrcweir     //--------------------------------------------------------------------
313*cdf0e10cSrcweir     sal_Bool SAL_CALL ODriverDelegator::acceptsURL( const ::rtl::OUString& url ) throw (SQLException, RuntimeException)
314*cdf0e10cSrcweir     {
315*cdf0e10cSrcweir         Sequence< PropertyValue > info;
316*cdf0e10cSrcweir 
317*cdf0e10cSrcweir         sal_Bool bOK =  url.matchAsciiL( RTL_CONSTASCII_STRINGPARAM( "sdbc:mysql:odbc:" ) )
318*cdf0e10cSrcweir                     ||  url.matchAsciiL( RTL_CONSTASCII_STRINGPARAM( "sdbc:mysql:jdbc:" ) )
319*cdf0e10cSrcweir                     ||  (   url.matchAsciiL( RTL_CONSTASCII_STRINGPARAM( "sdbc:mysql:mysqlc:" ) )
320*cdf0e10cSrcweir                         &&  loadDriver( url, info ).is()
321*cdf0e10cSrcweir                         );
322*cdf0e10cSrcweir         return bOK;
323*cdf0e10cSrcweir     }
324*cdf0e10cSrcweir 
325*cdf0e10cSrcweir     //--------------------------------------------------------------------
326*cdf0e10cSrcweir     Sequence< DriverPropertyInfo > SAL_CALL ODriverDelegator::getPropertyInfo( const ::rtl::OUString& url, const Sequence< PropertyValue >& /*info*/ ) throw (SQLException, RuntimeException)
327*cdf0e10cSrcweir     {
328*cdf0e10cSrcweir         ::std::vector< DriverPropertyInfo > aDriverInfo;
329*cdf0e10cSrcweir         if ( !acceptsURL(url) )
330*cdf0e10cSrcweir             return Sequence< DriverPropertyInfo >();
331*cdf0e10cSrcweir 
332*cdf0e10cSrcweir         Sequence< ::rtl::OUString > aBoolean(2);
333*cdf0e10cSrcweir         aBoolean[0] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("0"));
334*cdf0e10cSrcweir         aBoolean[1] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("1"));
335*cdf0e10cSrcweir 
336*cdf0e10cSrcweir 
337*cdf0e10cSrcweir         aDriverInfo.push_back(DriverPropertyInfo(
338*cdf0e10cSrcweir                 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("CharSet"))
339*cdf0e10cSrcweir                 ,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("CharSet of the database."))
340*cdf0e10cSrcweir                 ,sal_False
341*cdf0e10cSrcweir                 ,::rtl::OUString()
342*cdf0e10cSrcweir                 ,Sequence< ::rtl::OUString >())
343*cdf0e10cSrcweir                 );
344*cdf0e10cSrcweir         aDriverInfo.push_back(DriverPropertyInfo(
345*cdf0e10cSrcweir                 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("SuppressVersionColumns"))
346*cdf0e10cSrcweir                 ,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Display version columns (when available)."))
347*cdf0e10cSrcweir                 ,sal_False
348*cdf0e10cSrcweir                 ,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("0"))
349*cdf0e10cSrcweir                 ,aBoolean)
350*cdf0e10cSrcweir                 );
351*cdf0e10cSrcweir         const T_DRIVERTYPE eType = lcl_getDriverType( url );
352*cdf0e10cSrcweir         if ( eType == D_JDBC )
353*cdf0e10cSrcweir         {
354*cdf0e10cSrcweir             aDriverInfo.push_back(DriverPropertyInfo(
355*cdf0e10cSrcweir                     ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("JavaDriverClass"))
356*cdf0e10cSrcweir                     ,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("The JDBC driver class name."))
357*cdf0e10cSrcweir                     ,sal_True
358*cdf0e10cSrcweir                     ,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.mysql.jdbc.Driver"))
359*cdf0e10cSrcweir                     ,Sequence< ::rtl::OUString >())
360*cdf0e10cSrcweir                     );
361*cdf0e10cSrcweir         }
362*cdf0e10cSrcweir 
363*cdf0e10cSrcweir         return Sequence< DriverPropertyInfo >(&aDriverInfo[0],aDriverInfo.size());
364*cdf0e10cSrcweir     }
365*cdf0e10cSrcweir 
366*cdf0e10cSrcweir     //--------------------------------------------------------------------
367*cdf0e10cSrcweir     sal_Int32 SAL_CALL ODriverDelegator::getMajorVersion(  ) throw (RuntimeException)
368*cdf0e10cSrcweir     {
369*cdf0e10cSrcweir         return 1;
370*cdf0e10cSrcweir     }
371*cdf0e10cSrcweir 
372*cdf0e10cSrcweir     //--------------------------------------------------------------------
373*cdf0e10cSrcweir     sal_Int32 SAL_CALL ODriverDelegator::getMinorVersion(  ) throw (RuntimeException)
374*cdf0e10cSrcweir     {
375*cdf0e10cSrcweir         return 0;
376*cdf0e10cSrcweir     }
377*cdf0e10cSrcweir 
378*cdf0e10cSrcweir     //--------------------------------------------------------------------
379*cdf0e10cSrcweir     Reference< XTablesSupplier > SAL_CALL ODriverDelegator::getDataDefinitionByConnection( const Reference< XConnection >& connection ) throw (SQLException, RuntimeException)
380*cdf0e10cSrcweir     {
381*cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_aMutex );
382*cdf0e10cSrcweir         checkDisposed(ODriverDelegator_BASE::rBHelper.bDisposed);
383*cdf0e10cSrcweir 
384*cdf0e10cSrcweir         Reference< XTablesSupplier > xTab;
385*cdf0e10cSrcweir         Reference< XUnoTunnel> xTunnel(connection,UNO_QUERY);
386*cdf0e10cSrcweir         if ( xTunnel.is() )
387*cdf0e10cSrcweir         {
388*cdf0e10cSrcweir             OMetaConnection* pConnection = reinterpret_cast<OMetaConnection*>(xTunnel->getSomething( OMetaConnection::getUnoTunnelImplementationId() ));
389*cdf0e10cSrcweir             if ( pConnection )
390*cdf0e10cSrcweir             {
391*cdf0e10cSrcweir                 TWeakPairVector::iterator aEnd = m_aConnections.end();
392*cdf0e10cSrcweir                 for (TWeakPairVector::iterator i = m_aConnections.begin(); aEnd != i; ++i)
393*cdf0e10cSrcweir                 {
394*cdf0e10cSrcweir                     if ( i->second.second == pConnection )
395*cdf0e10cSrcweir                     {
396*cdf0e10cSrcweir                         xTab = Reference< XTablesSupplier >(i->second.first.get().get(),UNO_QUERY);
397*cdf0e10cSrcweir                         if ( !xTab.is() )
398*cdf0e10cSrcweir                         {
399*cdf0e10cSrcweir                             xTab = new OMySQLCatalog(connection);
400*cdf0e10cSrcweir                             i->second.first = WeakReferenceHelper(xTab);
401*cdf0e10cSrcweir                         }
402*cdf0e10cSrcweir                         break;
403*cdf0e10cSrcweir                     }
404*cdf0e10cSrcweir                 }
405*cdf0e10cSrcweir             }
406*cdf0e10cSrcweir         } // if ( xTunnel.is() )
407*cdf0e10cSrcweir         if ( !xTab.is() )
408*cdf0e10cSrcweir         {
409*cdf0e10cSrcweir             TWeakPairVector::iterator aEnd = m_aConnections.end();
410*cdf0e10cSrcweir             for (TWeakPairVector::iterator i = m_aConnections.begin(); aEnd != i; ++i)
411*cdf0e10cSrcweir             {
412*cdf0e10cSrcweir                 Reference< XConnection > xTemp(i->first.get(),UNO_QUERY);
413*cdf0e10cSrcweir                 if ( xTemp == connection )
414*cdf0e10cSrcweir                 {
415*cdf0e10cSrcweir                     xTab = Reference< XTablesSupplier >(i->second.first.get().get(),UNO_QUERY);
416*cdf0e10cSrcweir                     if ( !xTab.is() )
417*cdf0e10cSrcweir                     {
418*cdf0e10cSrcweir                         xTab = new OMySQLCatalog(connection);
419*cdf0e10cSrcweir                         i->second.first = WeakReferenceHelper(xTab);
420*cdf0e10cSrcweir                     }
421*cdf0e10cSrcweir                     break;
422*cdf0e10cSrcweir                 }
423*cdf0e10cSrcweir             }
424*cdf0e10cSrcweir         }
425*cdf0e10cSrcweir         return xTab;
426*cdf0e10cSrcweir     }
427*cdf0e10cSrcweir 
428*cdf0e10cSrcweir     //--------------------------------------------------------------------
429*cdf0e10cSrcweir     Reference< XTablesSupplier > SAL_CALL ODriverDelegator::getDataDefinitionByURL( const ::rtl::OUString& url, const Sequence< PropertyValue >& info ) throw (SQLException, RuntimeException)
430*cdf0e10cSrcweir     {
431*cdf0e10cSrcweir         if ( ! acceptsURL(url) )
432*cdf0e10cSrcweir         {
433*cdf0e10cSrcweir             ::connectivity::SharedResources aResources;
434*cdf0e10cSrcweir             const ::rtl::OUString sMessage = aResources.getResourceString(STR_URI_SYNTAX_ERROR);
435*cdf0e10cSrcweir             ::dbtools::throwGenericSQLException(sMessage ,*this);
436*cdf0e10cSrcweir         } // if ( ! acceptsURL(url) )
437*cdf0e10cSrcweir 
438*cdf0e10cSrcweir         return getDataDefinitionByConnection(connect(url,info));
439*cdf0e10cSrcweir     }
440*cdf0e10cSrcweir 
441*cdf0e10cSrcweir     // XServiceInfo
442*cdf0e10cSrcweir     // --------------------------------------------------------------------------------
443*cdf0e10cSrcweir     //------------------------------------------------------------------------------
444*cdf0e10cSrcweir     rtl::OUString ODriverDelegator::getImplementationName_Static(  ) throw(RuntimeException)
445*cdf0e10cSrcweir     {
446*cdf0e10cSrcweir         return rtl::OUString::createFromAscii("org.openoffice.comp.drivers.MySQL.Driver");
447*cdf0e10cSrcweir     }
448*cdf0e10cSrcweir     //------------------------------------------------------------------------------
449*cdf0e10cSrcweir     Sequence< ::rtl::OUString > ODriverDelegator::getSupportedServiceNames_Static(  ) throw (RuntimeException)
450*cdf0e10cSrcweir     {
451*cdf0e10cSrcweir         Sequence< ::rtl::OUString > aSNS( 2 );
452*cdf0e10cSrcweir         aSNS[0] = ::rtl::OUString::createFromAscii("com.sun.star.sdbc.Driver");
453*cdf0e10cSrcweir         aSNS[1] = ::rtl::OUString::createFromAscii("com.sun.star.sdbcx.Driver");
454*cdf0e10cSrcweir         return aSNS;
455*cdf0e10cSrcweir     }
456*cdf0e10cSrcweir     //------------------------------------------------------------------
457*cdf0e10cSrcweir     ::rtl::OUString SAL_CALL ODriverDelegator::getImplementationName(  ) throw(RuntimeException)
458*cdf0e10cSrcweir     {
459*cdf0e10cSrcweir         return getImplementationName_Static();
460*cdf0e10cSrcweir     }
461*cdf0e10cSrcweir 
462*cdf0e10cSrcweir     //------------------------------------------------------------------
463*cdf0e10cSrcweir     sal_Bool SAL_CALL ODriverDelegator::supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException)
464*cdf0e10cSrcweir     {
465*cdf0e10cSrcweir         Sequence< ::rtl::OUString > aSupported(getSupportedServiceNames());
466*cdf0e10cSrcweir         const ::rtl::OUString* pSupported = aSupported.getConstArray();
467*cdf0e10cSrcweir         const ::rtl::OUString* pEnd = pSupported + aSupported.getLength();
468*cdf0e10cSrcweir         for (;pSupported != pEnd && !pSupported->equals(_rServiceName); ++pSupported)
469*cdf0e10cSrcweir             ;
470*cdf0e10cSrcweir 
471*cdf0e10cSrcweir         return pSupported != pEnd;
472*cdf0e10cSrcweir     }
473*cdf0e10cSrcweir     //------------------------------------------------------------------
474*cdf0e10cSrcweir     Sequence< ::rtl::OUString > SAL_CALL ODriverDelegator::getSupportedServiceNames(  ) throw(RuntimeException)
475*cdf0e10cSrcweir     {
476*cdf0e10cSrcweir         return getSupportedServiceNames_Static();
477*cdf0e10cSrcweir     }
478*cdf0e10cSrcweir     //------------------------------------------------------------------
479*cdf0e10cSrcweir //........................................................................
480*cdf0e10cSrcweir }   // namespace connectivity
481*cdf0e10cSrcweir //........................................................................
482