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