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