Update ooo320-m1
[ooovba.git] / connectivity / source / drivers / ado / ADriver.cxx
blob6ff8dedcf74ca5974e6cce8f6358e3367c09ae49
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ADriver.cxx,v $
10 * $Revision: 1.19.56.1 $
12 * This file is part of OpenOffice.org.
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.
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).
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.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
33 #include "ado/ADriver.hxx"
34 #include "ado/AConnection.hxx"
35 #include "ado/Awrapadox.hxx"
36 #include "ado/ACatalog.hxx"
37 #include "ado/Awrapado.hxx"
38 #include "ado/adoimp.hxx"
39 #include <com/sun/star/lang/DisposedException.hpp>
40 #include "connectivity/dbexception.hxx"
41 #include "resource/ado_res.hrc"
42 #include <Objbase.h>
45 #include "resource/sharedresources.hxx"
47 using namespace connectivity;
48 using namespace connectivity::ado;
49 using namespace com::sun::star::uno;
50 using namespace com::sun::star::lang;
51 using namespace com::sun::star::beans;
52 using namespace com::sun::star::sdbc;
53 using namespace com::sun::star::sdbcx;
54 using namespace com::sun::star::lang;
56 // --------------------------------------------------------------------------------
57 // --------------------------------------------------------------------------------
58 ODriver::ODriver(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _xORB)
59 : ODriver_BASE(m_aMutex)
60 ,m_xORB(_xORB)
62 if ( FAILED(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED)) )
64 CoUninitialize();
65 int h = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
66 (void)h;
67 ++h;
70 // -------------------------------------------------------------------------
71 ODriver::~ODriver()
73 CoUninitialize();
74 CoInitialize(NULL);
76 //------------------------------------------------------------------------------
77 void ODriver::disposing()
79 ::osl::MutexGuard aGuard(m_aMutex);
82 for (OWeakRefArray::iterator i = m_xConnections.begin(); m_xConnections.end() != i; ++i)
84 Reference< XComponent > xComp(i->get(), UNO_QUERY);
85 if (xComp.is())
86 xComp->dispose();
88 m_xConnections.clear();
90 ODriver_BASE::disposing();
92 // static ServiceInfo
93 //------------------------------------------------------------------------------
94 rtl::OUString ODriver::getImplementationName_Static( ) throw(RuntimeException)
96 return rtl::OUString::createFromAscii("com.sun.star.comp.sdbc.ado.ODriver");
98 //------------------------------------------------------------------------------
99 Sequence< ::rtl::OUString > ODriver::getSupportedServiceNames_Static( ) throw (RuntimeException)
101 Sequence< ::rtl::OUString > aSNS( 2 );
102 aSNS[0] = ::rtl::OUString::createFromAscii("com.sun.star.sdbc.Driver");
103 aSNS[1] = ::rtl::OUString::createFromAscii("com.sun.star.sdbcx.Driver");
104 return aSNS;
106 //------------------------------------------------------------------
107 ::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 )
109 return *(new ODriver(_rxFactory));
112 // --------------------------------------------------------------------------------
113 ::rtl::OUString SAL_CALL ODriver::getImplementationName( ) throw(RuntimeException)
115 return getImplementationName_Static();
118 // --------------------------------------------------------------------------------
119 sal_Bool SAL_CALL ODriver::supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException)
121 Sequence< ::rtl::OUString > aSupported(getSupportedServiceNames());
122 const ::rtl::OUString* pSupported = aSupported.getConstArray();
123 const ::rtl::OUString* pEnd = pSupported + aSupported.getLength();
124 for (;pSupported != pEnd && !pSupported->equals(_rServiceName); ++pSupported)
127 return pSupported != pEnd;
130 // --------------------------------------------------------------------------------
131 Sequence< ::rtl::OUString > SAL_CALL ODriver::getSupportedServiceNames( ) throw(RuntimeException)
133 return getSupportedServiceNames_Static();
136 // --------------------------------------------------------------------------------
137 Reference< XConnection > SAL_CALL ODriver::connect( const ::rtl::OUString& url, const Sequence< PropertyValue >& info ) throw(SQLException, RuntimeException)
139 if ( ! acceptsURL(url) )
140 return NULL;
142 OConnection* pCon = new OConnection(this);
143 pCon->construct(url,info);
144 Reference< XConnection > xCon = pCon;
145 m_xConnections.push_back(WeakReferenceHelper(*pCon));
147 return xCon;
149 // --------------------------------------------------------------------------------
150 sal_Bool SAL_CALL ODriver::acceptsURL( const ::rtl::OUString& url )
151 throw(SQLException, RuntimeException)
153 return (!url.compareTo(::rtl::OUString::createFromAscii("sdbc:ado:"),9));
155 // -----------------------------------------------------------------------------
156 void ODriver::impl_checkURL_throw(const ::rtl::OUString& _sUrl)
158 if ( !acceptsURL(_sUrl) )
160 SharedResources aResources;
161 const ::rtl::OUString sMessage = aResources.getResourceString(STR_URI_SYNTAX_ERROR);
162 ::dbtools::throwGenericSQLException(sMessage ,*this);
163 } // if ( !acceptsURL(_sUrl) )
165 // --------------------------------------------------------------------------------
166 Sequence< DriverPropertyInfo > SAL_CALL ODriver::getPropertyInfo( const ::rtl::OUString& url, const Sequence< PropertyValue >& /*info*/ ) throw(SQLException, RuntimeException)
168 impl_checkURL_throw(url);
169 return Sequence< DriverPropertyInfo >();
171 // --------------------------------------------------------------------------------
172 sal_Int32 SAL_CALL ODriver::getMajorVersion( ) throw(RuntimeException)
174 return 1;
176 // --------------------------------------------------------------------------------
177 sal_Int32 SAL_CALL ODriver::getMinorVersion( ) throw(RuntimeException)
179 return 0;
181 // --------------------------------------------------------------------------------
182 // XDataDefinitionSupplier
183 Reference< XTablesSupplier > SAL_CALL ODriver::getDataDefinitionByConnection( const Reference< ::com::sun::star::sdbc::XConnection >& connection ) throw(::com::sun::star::sdbc::SQLException, RuntimeException)
185 ::osl::MutexGuard aGuard( m_aMutex );
186 if (ODriver_BASE::rBHelper.bDisposed)
187 throw DisposedException();
189 OConnection* pConnection = NULL;
190 Reference< ::com::sun::star::lang::XUnoTunnel> xTunnel(connection,UNO_QUERY);
191 if(xTunnel.is())
193 OConnection* pSearchConnection = reinterpret_cast< OConnection* >( xTunnel->getSomething(OConnection::getUnoTunnelImplementationId()) );
195 for (OWeakRefArray::iterator i = m_xConnections.begin(); m_xConnections.end() != i; ++i)
197 if ((OConnection*) Reference< XConnection >::query(i->get().get()).get() == pSearchConnection)
199 pConnection = pSearchConnection;
200 break;
206 Reference< XTablesSupplier > xTab = NULL;
207 if(pConnection)
209 WpADOCatalog aCatalog;
210 aCatalog.Create();
211 if(aCatalog.IsValid())
213 aCatalog.putref_ActiveConnection(*pConnection->getConnection());
214 OCatalog* pCatalog = new OCatalog(aCatalog,pConnection);
215 xTab = pCatalog;
216 pConnection->setCatalog(xTab);
217 pConnection->setCatalog(pCatalog);
220 return xTab;
222 // --------------------------------------------------------------------------------
223 Reference< XTablesSupplier > SAL_CALL ODriver::getDataDefinitionByURL( const ::rtl::OUString& url, const Sequence< PropertyValue >& info ) throw(::com::sun::star::sdbc::SQLException, RuntimeException)
225 impl_checkURL_throw(url);
226 return getDataDefinitionByConnection(connect(url,info));
229 // -----------------------------------------------------------------------------
230 void ADOS::ThrowException(ADOConnection* _pAdoCon,const Reference< XInterface >& _xInterface) throw(SQLException, RuntimeException)
232 ADOErrors *pErrors = NULL;
233 _pAdoCon->get_Errors(&pErrors);
234 if(!pErrors)
235 return; // no error found
237 pErrors->AddRef( );
239 // alle aufgelaufenen Fehler auslesen und ausgeben
240 sal_Int32 nLen;
241 pErrors->get_Count(&nLen);
242 if (nLen)
244 ::rtl::OUString sError;
245 ::rtl::OUString aSQLState;
246 SQLException aException;
247 aException.ErrorCode = 1000;
248 for (sal_Int32 i = nLen-1; i>=0; --i)
250 ADOError *pError = NULL;
251 pErrors->get_Item(OLEVariant(i),&pError);
252 WpADOError aErr(pError);
253 OSL_ENSURE(pError,"No error in collection found! BAD!");
254 if(pError)
256 if(i==nLen-1)
257 aException = SQLException(aErr.GetDescription(),_xInterface,aErr.GetSQLState(),aErr.GetNumber(),Any());
258 else
260 SQLException aTemp = SQLException(aErr.GetDescription(),
261 _xInterface,aErr.GetSQLState(),aErr.GetNumber(),makeAny(aException));
262 aTemp.NextException <<= aException;
263 aException = aTemp;
267 pErrors->Clear();
268 pErrors->Release();
269 throw aException;
271 pErrors->Release();