xref: /trunk/main/connectivity/source/drivers/ado/ADriver.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_ado.hxx"
26 #include "ado/ADriver.hxx"
27 #include "ado/AConnection.hxx"
28 #include "ado/Awrapadox.hxx"
29 #include "ado/ACatalog.hxx"
30 #include "ado/Awrapado.hxx"
31 #include "ado/adoimp.hxx"
32 #include <com/sun/star/lang/DisposedException.hpp>
33 #include "connectivity/dbexception.hxx"
34 #include "resource/ado_res.hrc"
35 #include <Objbase.h>
36 
37 
38 #include "resource/sharedresources.hxx"
39 
40 using namespace connectivity;
41 using namespace connectivity::ado;
42 using namespace com::sun::star::uno;
43 using namespace com::sun::star::lang;
44 using namespace com::sun::star::beans;
45 using namespace com::sun::star::sdbc;
46 using namespace com::sun::star::sdbcx;
47 using namespace com::sun::star::lang;
48 
49 // --------------------------------------------------------------------------------
50 // --------------------------------------------------------------------------------
ODriver(const::com::sun::star::uno::Reference<::com::sun::star::lang::XMultiServiceFactory> & _xORB)51 ODriver::ODriver(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _xORB)
52     : ODriver_BASE(m_aMutex)
53     ,m_xORB(_xORB)
54 {
55      if ( FAILED(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED)) )
56      {
57          CoUninitialize();
58          int h = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
59          (void)h;
60          ++h;
61      }
62 }
63 // -------------------------------------------------------------------------
~ODriver()64 ODriver::~ODriver()
65 {
66     CoUninitialize();
67     CoInitialize(NULL);
68 }
69 //------------------------------------------------------------------------------
disposing()70 void ODriver::disposing()
71 {
72     ::osl::MutexGuard aGuard(m_aMutex);
73 
74 
75     for (OWeakRefArray::iterator i = m_xConnections.begin(); m_xConnections.end() != i; ++i)
76     {
77         Reference< XComponent > xComp(i->get(), UNO_QUERY);
78         if (xComp.is())
79             xComp->dispose();
80     }
81     m_xConnections.clear();
82 
83     ODriver_BASE::disposing();
84 }
85 // static ServiceInfo
86 //------------------------------------------------------------------------------
getImplementationName_Static()87 rtl::OUString ODriver::getImplementationName_Static(  )
88 {
89     return rtl::OUString::createFromAscii("com.sun.star.comp.sdbc.ado.ODriver");
90 }
91 //------------------------------------------------------------------------------
getSupportedServiceNames_Static()92 Sequence< ::rtl::OUString > ODriver::getSupportedServiceNames_Static(  )
93 {
94     Sequence< ::rtl::OUString > aSNS( 2 );
95     aSNS[0] = ::rtl::OUString::createFromAscii("com.sun.star.sdbc.Driver");
96     aSNS[1] = ::rtl::OUString::createFromAscii("com.sun.star.sdbcx.Driver");
97     return aSNS;
98 }
99 //------------------------------------------------------------------
ODriver_CreateInstance(const::com::sun::star::uno::Reference<::com::sun::star::lang::XMultiServiceFactory> & _rxFactory)100 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >  SAL_CALL connectivity::ado::ODriver_CreateInstance(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxFactory)
101 {
102     return *(new ODriver(_rxFactory));
103 }
104 
105 // --------------------------------------------------------------------------------
getImplementationName()106 ::rtl::OUString SAL_CALL ODriver::getImplementationName(  )
107 {
108     return getImplementationName_Static();
109 }
110 
111 // --------------------------------------------------------------------------------
supportsService(const::rtl::OUString & _rServiceName)112 sal_Bool SAL_CALL ODriver::supportsService( const ::rtl::OUString& _rServiceName )
113 {
114     Sequence< ::rtl::OUString > aSupported(getSupportedServiceNames());
115     const ::rtl::OUString* pSupported = aSupported.getConstArray();
116     const ::rtl::OUString* pEnd = pSupported + aSupported.getLength();
117     for (;pSupported != pEnd && !pSupported->equals(_rServiceName); ++pSupported)
118         ;
119 
120     return pSupported != pEnd;
121 }
122 
123 // --------------------------------------------------------------------------------
getSupportedServiceNames()124 Sequence< ::rtl::OUString > SAL_CALL ODriver::getSupportedServiceNames(  )
125 {
126     return getSupportedServiceNames_Static();
127 }
128 
129 // --------------------------------------------------------------------------------
connect(const::rtl::OUString & url,const Sequence<PropertyValue> & info)130 Reference< XConnection > SAL_CALL ODriver::connect( const ::rtl::OUString& url, const Sequence< PropertyValue >& info )
131 {
132     if ( ! acceptsURL(url) )
133         return NULL;
134 
135     OConnection* pCon = new OConnection(this);
136     pCon->construct(url,info);
137     Reference< XConnection > xCon = pCon;
138     m_xConnections.push_back(WeakReferenceHelper(*pCon));
139 
140     return xCon;
141 }
142 // --------------------------------------------------------------------------------
acceptsURL(const::rtl::OUString & url)143 sal_Bool SAL_CALL ODriver::acceptsURL( const ::rtl::OUString& url )
144 {
145     return (!url.compareTo(::rtl::OUString::createFromAscii("sdbc:ado:"),9));
146 }
147 // -----------------------------------------------------------------------------
impl_checkURL_throw(const::rtl::OUString & _sUrl)148 void ODriver::impl_checkURL_throw(const ::rtl::OUString& _sUrl)
149 {
150     if ( !acceptsURL(_sUrl) )
151     {
152         SharedResources aResources;
153         const ::rtl::OUString sMessage = aResources.getResourceString(STR_URI_SYNTAX_ERROR);
154         ::dbtools::throwGenericSQLException(sMessage ,*this);
155     } // if ( !acceptsURL(_sUrl) )
156 }
157 // --------------------------------------------------------------------------------
getPropertyInfo(const::rtl::OUString & url,const Sequence<PropertyValue> &)158 Sequence< DriverPropertyInfo > SAL_CALL ODriver::getPropertyInfo( const ::rtl::OUString& url, const Sequence< PropertyValue >& /*info*/ )
159 {
160     impl_checkURL_throw(url);
161     if ( acceptsURL(url) )
162     {
163         ::std::vector< DriverPropertyInfo > aDriverInfo;
164 
165         Sequence< ::rtl::OUString > aBooleanValues(2);
166         aBooleanValues[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "false" ) );
167         aBooleanValues[1] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "true" ) );
168 
169         aDriverInfo.push_back(DriverPropertyInfo(
170                 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("IgnoreDriverPrivileges"))
171                 ,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Ignore the privileges from the database driver."))
172                 ,sal_False
173                 ,::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "false" ) )
174                 ,aBooleanValues)
175         );
176         aDriverInfo.push_back(DriverPropertyInfo(
177                 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("EscapeDateTime"))
178                 ,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Escape date time format."))
179                 ,sal_False
180                 ,::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "true" ) )
181                 ,aBooleanValues)
182         );
183         aDriverInfo.push_back(DriverPropertyInfo(
184                 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("TypeInfoSettings"))
185                 ,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Defines how the type info of the database metadata should be manipulated."))
186                 ,sal_False
187                 ,::rtl::OUString( )
188                 ,Sequence< ::rtl::OUString > ())
189         );
190         return Sequence< DriverPropertyInfo >(&aDriverInfo[0],aDriverInfo.size());
191     }
192     return Sequence< DriverPropertyInfo >();
193 }
194 // --------------------------------------------------------------------------------
getMajorVersion()195 sal_Int32 SAL_CALL ODriver::getMajorVersion(  )
196 {
197     return 1;
198 }
199 // --------------------------------------------------------------------------------
getMinorVersion()200 sal_Int32 SAL_CALL ODriver::getMinorVersion(  )
201 {
202     return 0;
203 }
204 // --------------------------------------------------------------------------------
205 // XDataDefinitionSupplier
getDataDefinitionByConnection(const Reference<::com::sun::star::sdbc::XConnection> & connection)206 Reference< XTablesSupplier > SAL_CALL ODriver::getDataDefinitionByConnection( const Reference< ::com::sun::star::sdbc::XConnection >& connection )
207 {
208     ::osl::MutexGuard aGuard( m_aMutex );
209     if (ODriver_BASE::rBHelper.bDisposed)
210         throw DisposedException();
211 
212     OConnection* pConnection = NULL;
213     Reference< ::com::sun::star::lang::XUnoTunnel> xTunnel(connection,UNO_QUERY);
214     if(xTunnel.is())
215     {
216         OConnection* pSearchConnection = reinterpret_cast< OConnection* >( xTunnel->getSomething(OConnection::getUnoTunnelImplementationId()) );
217 
218         for (OWeakRefArray::iterator i = m_xConnections.begin(); m_xConnections.end() != i; ++i)
219         {
220             if ((OConnection*) Reference< XConnection >::query(i->get().get()).get() == pSearchConnection)
221             {
222                 pConnection = pSearchConnection;
223                 break;
224             }
225         }
226 
227     }
228 
229     Reference< XTablesSupplier > xTab = NULL;
230     if(pConnection)
231     {
232         WpADOCatalog aCatalog;
233         aCatalog.Create();
234         if(aCatalog.IsValid())
235         {
236             aCatalog.putref_ActiveConnection(*pConnection->getConnection());
237             OCatalog* pCatalog = new OCatalog(aCatalog,pConnection);
238             xTab = pCatalog;
239             pConnection->setCatalog(xTab);
240             pConnection->setCatalog(pCatalog);
241         }
242     }
243     return xTab;
244 }
245 // --------------------------------------------------------------------------------
getDataDefinitionByURL(const::rtl::OUString & url,const Sequence<PropertyValue> & info)246 Reference< XTablesSupplier > SAL_CALL ODriver::getDataDefinitionByURL( const ::rtl::OUString& url, const Sequence< PropertyValue >& info )
247 {
248     impl_checkURL_throw(url);
249     return getDataDefinitionByConnection(connect(url,info));
250 }
251 
252 // -----------------------------------------------------------------------------
ThrowException(ADOConnection * _pAdoCon,const Reference<XInterface> & _xInterface)253 void ADOS::ThrowException(ADOConnection* _pAdoCon,const Reference< XInterface >& _xInterface)
254 {
255     ADOErrors *pErrors = NULL;
256     _pAdoCon->get_Errors(&pErrors);
257     if(!pErrors)
258         return; // no error found
259 
260     pErrors->AddRef( );
261 
262     // alle aufgelaufenen Fehler auslesen und ausgeben
263     sal_Int32 nLen;
264     pErrors->get_Count(&nLen);
265     if (nLen)
266     {
267         ::rtl::OUString sError;
268         ::rtl::OUString aSQLState;
269         SQLException aException;
270         aException.ErrorCode = 1000;
271         for (sal_Int32 i = nLen-1; i>=0; --i)
272         {
273             ADOError *pError = NULL;
274             pErrors->get_Item(OLEVariant(i),&pError);
275             WpADOError aErr(pError);
276             OSL_ENSURE(pError,"No error in collection found! BAD!");
277             if(pError)
278             {
279                 if(i==nLen-1)
280                     aException = SQLException(aErr.GetDescription(),_xInterface,aErr.GetSQLState(),aErr.GetNumber(),Any());
281                 else
282                 {
283                     SQLException aTemp = SQLException(aErr.GetDescription(),
284                         _xInterface,aErr.GetSQLState(),aErr.GetNumber(),makeAny(aException));
285                     aTemp.NextException <<= aException;
286                     aException = aTemp;
287                 }
288             }
289         }
290         pErrors->Clear();
291         pErrors->Release();
292         throw aException;
293     }
294     pErrors->Release();
295 }
296