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