xref: /AOO41X/main/connectivity/source/drivers/ado/ADriver.cxx (revision 9b5730f6ddef7eb82608ca4d31dc0d7678e652cf)
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_connectivity.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(  ) throw(RuntimeException)
88 {
89     return rtl::OUString::createFromAscii("com.sun.star.comp.sdbc.ado.ODriver");
90 }
91 //------------------------------------------------------------------------------
getSupportedServiceNames_Static()92 Sequence< ::rtl::OUString > ODriver::getSupportedServiceNames_Static(  ) throw (RuntimeException)
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) throw( ::com::sun::star::uno::Exception )
101 {
102     return *(new ODriver(_rxFactory));
103 }
104 
105 // --------------------------------------------------------------------------------
getImplementationName()106 ::rtl::OUString SAL_CALL ODriver::getImplementationName(  ) throw(RuntimeException)
107 {
108     return getImplementationName_Static();
109 }
110 
111 // --------------------------------------------------------------------------------
supportsService(const::rtl::OUString & _rServiceName)112 sal_Bool SAL_CALL ODriver::supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException)
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(  ) throw(RuntimeException)
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 ) throw(SQLException, RuntimeException)
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         throw(SQLException, RuntimeException)
145 {
146     return (!url.compareTo(::rtl::OUString::createFromAscii("sdbc:ado:"),9));
147 }
148 // -----------------------------------------------------------------------------
impl_checkURL_throw(const::rtl::OUString & _sUrl)149 void ODriver::impl_checkURL_throw(const ::rtl::OUString& _sUrl)
150 {
151     if ( !acceptsURL(_sUrl) )
152     {
153         SharedResources aResources;
154         const ::rtl::OUString sMessage = aResources.getResourceString(STR_URI_SYNTAX_ERROR);
155         ::dbtools::throwGenericSQLException(sMessage ,*this);
156     } // if ( !acceptsURL(_sUrl) )
157 }
158 // --------------------------------------------------------------------------------
getPropertyInfo(const::rtl::OUString & url,const Sequence<PropertyValue> &)159 Sequence< DriverPropertyInfo > SAL_CALL ODriver::getPropertyInfo( const ::rtl::OUString& url, const Sequence< PropertyValue >& /*info*/ ) throw(SQLException, RuntimeException)
160 {
161     impl_checkURL_throw(url);
162     if ( acceptsURL(url) )
163     {
164         ::std::vector< DriverPropertyInfo > aDriverInfo;
165 
166         Sequence< ::rtl::OUString > aBooleanValues(2);
167         aBooleanValues[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "false" ) );
168         aBooleanValues[1] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "true" ) );
169 
170         aDriverInfo.push_back(DriverPropertyInfo(
171                 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("IgnoreDriverPrivileges"))
172                 ,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Ignore the privileges from the database driver."))
173                 ,sal_False
174                 ,::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "false" ) )
175                 ,aBooleanValues)
176         );
177         aDriverInfo.push_back(DriverPropertyInfo(
178                 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("EscapeDateTime"))
179                 ,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Escape date time format."))
180                 ,sal_False
181                 ,::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "true" ) )
182                 ,aBooleanValues)
183         );
184         aDriverInfo.push_back(DriverPropertyInfo(
185                 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("TypeInfoSettings"))
186                 ,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Defines how the type info of the database metadata should be manipulated."))
187                 ,sal_False
188                 ,::rtl::OUString( )
189                 ,Sequence< ::rtl::OUString > ())
190         );
191         return Sequence< DriverPropertyInfo >(&aDriverInfo[0],aDriverInfo.size());
192     }
193     return Sequence< DriverPropertyInfo >();
194 }
195 // --------------------------------------------------------------------------------
getMajorVersion()196 sal_Int32 SAL_CALL ODriver::getMajorVersion(  ) throw(RuntimeException)
197 {
198     return 1;
199 }
200 // --------------------------------------------------------------------------------
getMinorVersion()201 sal_Int32 SAL_CALL ODriver::getMinorVersion(  ) throw(RuntimeException)
202 {
203     return 0;
204 }
205 // --------------------------------------------------------------------------------
206 // XDataDefinitionSupplier
getDataDefinitionByConnection(const Reference<::com::sun::star::sdbc::XConnection> & connection)207 Reference< XTablesSupplier > SAL_CALL ODriver::getDataDefinitionByConnection( const Reference< ::com::sun::star::sdbc::XConnection >& connection ) throw(::com::sun::star::sdbc::SQLException, RuntimeException)
208 {
209     ::osl::MutexGuard aGuard( m_aMutex );
210     if (ODriver_BASE::rBHelper.bDisposed)
211         throw DisposedException();
212 
213     OConnection* pConnection = NULL;
214     Reference< ::com::sun::star::lang::XUnoTunnel> xTunnel(connection,UNO_QUERY);
215     if(xTunnel.is())
216     {
217         OConnection* pSearchConnection = reinterpret_cast< OConnection* >( xTunnel->getSomething(OConnection::getUnoTunnelImplementationId()) );
218 
219         for (OWeakRefArray::iterator i = m_xConnections.begin(); m_xConnections.end() != i; ++i)
220         {
221             if ((OConnection*) Reference< XConnection >::query(i->get().get()).get() == pSearchConnection)
222             {
223                 pConnection = pSearchConnection;
224                 break;
225             }
226         }
227 
228     }
229 
230     Reference< XTablesSupplier > xTab = NULL;
231     if(pConnection)
232     {
233         WpADOCatalog aCatalog;
234         aCatalog.Create();
235         if(aCatalog.IsValid())
236         {
237             aCatalog.putref_ActiveConnection(*pConnection->getConnection());
238             OCatalog* pCatalog = new OCatalog(aCatalog,pConnection);
239             xTab = pCatalog;
240             pConnection->setCatalog(xTab);
241             pConnection->setCatalog(pCatalog);
242         }
243     }
244     return xTab;
245 }
246 // --------------------------------------------------------------------------------
getDataDefinitionByURL(const::rtl::OUString & url,const Sequence<PropertyValue> & info)247 Reference< XTablesSupplier > SAL_CALL ODriver::getDataDefinitionByURL( const ::rtl::OUString& url, const Sequence< PropertyValue >& info ) throw(::com::sun::star::sdbc::SQLException, RuntimeException)
248 {
249     impl_checkURL_throw(url);
250     return getDataDefinitionByConnection(connect(url,info));
251 }
252 
253 // -----------------------------------------------------------------------------
ThrowException(ADOConnection * _pAdoCon,const Reference<XInterface> & _xInterface)254 void ADOS::ThrowException(ADOConnection* _pAdoCon,const Reference< XInterface >& _xInterface) throw(SQLException, RuntimeException)
255 {
256     ADOErrors *pErrors = NULL;
257     _pAdoCon->get_Errors(&pErrors);
258     if(!pErrors)
259         return; // no error found
260 
261     pErrors->AddRef( );
262 
263     // alle aufgelaufenen Fehler auslesen und ausgeben
264     sal_Int32 nLen;
265     pErrors->get_Count(&nLen);
266     if (nLen)
267     {
268         ::rtl::OUString sError;
269         ::rtl::OUString aSQLState;
270         SQLException aException;
271         aException.ErrorCode = 1000;
272         for (sal_Int32 i = nLen-1; i>=0; --i)
273         {
274             ADOError *pError = NULL;
275             pErrors->get_Item(OLEVariant(i),&pError);
276             WpADOError aErr(pError);
277             OSL_ENSURE(pError,"No error in collection found! BAD!");
278             if(pError)
279             {
280                 if(i==nLen-1)
281                     aException = SQLException(aErr.GetDescription(),_xInterface,aErr.GetSQLState(),aErr.GetNumber(),Any());
282                 else
283                 {
284                     SQLException aTemp = SQLException(aErr.GetDescription(),
285                         _xInterface,aErr.GetSQLState(),aErr.GetNumber(),makeAny(aException));
286                     aTemp.NextException <<= aException;
287                     aException = aTemp;
288                 }
289             }
290         }
291         pErrors->Clear();
292         pErrors->Release();
293         throw aException;
294     }
295     pErrors->Release();
296 }
297 
298 
299