1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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"
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
)
62 if ( FAILED(CoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
)) )
65 int h
= CoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
70 // -------------------------------------------------------------------------
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
);
88 m_xConnections
.clear();
90 ODriver_BASE::disposing();
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");
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
) )
142 OConnection
* pCon
= new OConnection(this);
143 pCon
->construct(url
,info
);
144 Reference
< XConnection
> xCon
= pCon
;
145 m_xConnections
.push_back(WeakReferenceHelper(*pCon
));
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
)
176 // --------------------------------------------------------------------------------
177 sal_Int32 SAL_CALL
ODriver::getMinorVersion( ) throw(RuntimeException
)
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
);
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
;
206 Reference
< XTablesSupplier
> xTab
= NULL
;
209 WpADOCatalog aCatalog
;
211 if(aCatalog
.IsValid())
213 aCatalog
.putref_ActiveConnection(*pConnection
->getConnection());
214 OCatalog
* pCatalog
= new OCatalog(aCatalog
,pConnection
);
216 pConnection
->setCatalog(xTab
);
217 pConnection
->setCatalog(pCatalog
);
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
);
235 return; // no error found
239 // alle aufgelaufenen Fehler auslesen und ausgeben
241 pErrors
->get_Count(&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!");
257 aException
= SQLException(aErr
.GetDescription(),_xInterface
,aErr
.GetSQLState(),aErr
.GetNumber(),Any());
260 SQLException aTemp
= SQLException(aErr
.GetDescription(),
261 _xInterface
,aErr
.GetSQLState(),aErr
.GetNumber(),makeAny(aException
));
262 aTemp
.NextException
<<= aException
;