1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #if defined _WIN32_WINNT
23 #define _WIN32_WINNT 0x0501
25 #include "ado/ADriver.hxx"
26 #include "ado/AConnection.hxx"
27 #include "ado/Awrapadox.hxx"
28 #include "ado/ACatalog.hxx"
29 #include "ado/Awrapado.hxx"
30 #include "ado/adoimp.hxx"
31 #include <com/sun/star/lang/DisposedException.hpp>
32 #include "connectivity/dbexception.hxx"
33 #include "resource/ado_res.hrc"
37 #include "resource/sharedresources.hxx"
39 using namespace connectivity
;
40 using namespace connectivity::ado
;
41 using namespace com::sun::star::uno
;
42 using namespace com::sun::star::lang
;
43 using namespace com::sun::star::beans
;
44 using namespace com::sun::star::sdbc
;
45 using namespace com::sun::star::sdbcx
;
47 // --------------------------------------------------------------------------------
48 // --------------------------------------------------------------------------------
49 ODriver::ODriver(const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& _xORB
)
50 : ODriver_BASE(m_aMutex
)
53 if ( FAILED(CoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
)) )
56 int h
= CoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
61 // -------------------------------------------------------------------------
67 //------------------------------------------------------------------------------
68 void ODriver::disposing()
70 ::osl::MutexGuard
aGuard(m_aMutex
);
73 for (OWeakRefArray::iterator i
= m_xConnections
.begin(); m_xConnections
.end() != i
; ++i
)
75 Reference
< XComponent
> xComp(i
->get(), UNO_QUERY
);
79 m_xConnections
.clear();
81 ODriver_BASE::disposing();
84 //------------------------------------------------------------------------------
85 OUString
ODriver::getImplementationName_Static( ) throw(RuntimeException
)
87 return OUString("com.sun.star.comp.sdbc.ado.ODriver");
89 //------------------------------------------------------------------------------
90 Sequence
< OUString
> ODriver::getSupportedServiceNames_Static( ) throw (RuntimeException
)
92 Sequence
< OUString
> aSNS( 2 );
93 aSNS
[0] = OUString("com.sun.star.sdbc.Driver");
94 aSNS
[1] = OUString("com.sun.star.sdbcx.Driver");
97 //------------------------------------------------------------------
98 ::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
)
100 return *(new ODriver(_rxFactory
));
103 // --------------------------------------------------------------------------------
104 OUString SAL_CALL
ODriver::getImplementationName( ) throw(RuntimeException
)
106 return getImplementationName_Static();
109 // --------------------------------------------------------------------------------
110 sal_Bool SAL_CALL
ODriver::supportsService( const OUString
& _rServiceName
) throw(RuntimeException
)
112 Sequence
< OUString
> aSupported(getSupportedServiceNames());
113 const OUString
* pSupported
= aSupported
.getConstArray();
114 const OUString
* pEnd
= pSupported
+ aSupported
.getLength();
115 for (;pSupported
!= pEnd
&& !pSupported
->equals(_rServiceName
); ++pSupported
)
118 return pSupported
!= pEnd
;
121 // --------------------------------------------------------------------------------
122 Sequence
< OUString
> SAL_CALL
ODriver::getSupportedServiceNames( ) throw(RuntimeException
)
124 return getSupportedServiceNames_Static();
127 // --------------------------------------------------------------------------------
128 Reference
< XConnection
> SAL_CALL
ODriver::connect( const OUString
& url
, const Sequence
< PropertyValue
>& info
) throw(SQLException
, RuntimeException
)
130 if ( ! acceptsURL(url
) )
133 OConnection
* pCon
= new OConnection(this);
134 pCon
->construct(url
,info
);
135 Reference
< XConnection
> xCon
= pCon
;
136 m_xConnections
.push_back(WeakReferenceHelper(*pCon
));
140 // --------------------------------------------------------------------------------
141 sal_Bool SAL_CALL
ODriver::acceptsURL( const OUString
& url
)
142 throw(SQLException
, RuntimeException
)
144 return url
.startsWith("sdbc:ado:");
146 // -----------------------------------------------------------------------------
147 void ODriver::impl_checkURL_throw(const OUString
& _sUrl
)
149 if ( !acceptsURL(_sUrl
) )
151 SharedResources aResources
;
152 const OUString sMessage
= aResources
.getResourceString(STR_URI_SYNTAX_ERROR
);
153 ::dbtools::throwGenericSQLException(sMessage
,*this);
154 } // if ( !acceptsURL(_sUrl) )
156 // --------------------------------------------------------------------------------
157 Sequence
< DriverPropertyInfo
> SAL_CALL
ODriver::getPropertyInfo( const OUString
& url
, const Sequence
< PropertyValue
>& /*info*/ ) throw(SQLException
, RuntimeException
)
159 impl_checkURL_throw(url
);
160 if ( acceptsURL(url
) )
162 ::std::vector
< DriverPropertyInfo
> aDriverInfo
;
164 Sequence
< OUString
> aBooleanValues(2);
165 aBooleanValues
[0] = OUString( "false" );
166 aBooleanValues
[1] = OUString( "true" );
168 aDriverInfo
.push_back(DriverPropertyInfo(
169 OUString("IgnoreDriverPrivileges")
170 ,OUString("Ignore the privileges from the database driver.")
175 aDriverInfo
.push_back(DriverPropertyInfo(
176 OUString("EscapeDateTime")
177 ,OUString("Escape date time format.")
182 aDriverInfo
.push_back(DriverPropertyInfo(
183 OUString("TypeInfoSettings")
184 ,OUString("Defines how the type info of the database metadata should be manipulated.")
187 ,Sequence
< OUString
> ())
189 return Sequence
< DriverPropertyInfo
>(&aDriverInfo
[0],aDriverInfo
.size());
191 return Sequence
< DriverPropertyInfo
>();
193 // --------------------------------------------------------------------------------
194 sal_Int32 SAL_CALL
ODriver::getMajorVersion( ) throw(RuntimeException
)
198 // --------------------------------------------------------------------------------
199 sal_Int32 SAL_CALL
ODriver::getMinorVersion( ) throw(RuntimeException
)
203 // --------------------------------------------------------------------------------
204 // XDataDefinitionSupplier
205 Reference
< XTablesSupplier
> SAL_CALL
ODriver::getDataDefinitionByConnection( const Reference
< ::com::sun::star::sdbc::XConnection
>& connection
) throw(::com::sun::star::sdbc::SQLException
, RuntimeException
)
207 ::osl::MutexGuard
aGuard( m_aMutex
);
208 if (ODriver_BASE::rBHelper
.bDisposed
)
209 throw DisposedException();
211 OConnection
* pConnection
= NULL
;
212 Reference
< ::com::sun::star::lang::XUnoTunnel
> xTunnel(connection
,UNO_QUERY
);
215 OConnection
* pSearchConnection
= reinterpret_cast< OConnection
* >( xTunnel
->getSomething(OConnection::getUnoTunnelImplementationId()) );
217 for (OWeakRefArray::iterator i
= m_xConnections
.begin(); m_xConnections
.end() != i
; ++i
)
219 if ((OConnection
*) Reference
< XConnection
>::query(i
->get().get()).get() == pSearchConnection
)
221 pConnection
= pSearchConnection
;
228 Reference
< XTablesSupplier
> xTab
= NULL
;
231 WpADOCatalog aCatalog
;
233 if(aCatalog
.IsValid())
235 aCatalog
.putref_ActiveConnection(*pConnection
->getConnection());
236 OCatalog
* pCatalog
= new OCatalog(aCatalog
,pConnection
);
238 pConnection
->setCatalog(xTab
);
239 pConnection
->setCatalog(pCatalog
);
244 // --------------------------------------------------------------------------------
245 Reference
< XTablesSupplier
> SAL_CALL
ODriver::getDataDefinitionByURL( const OUString
& url
, const Sequence
< PropertyValue
>& info
) throw(::com::sun::star::sdbc::SQLException
, RuntimeException
)
247 impl_checkURL_throw(url
);
248 return getDataDefinitionByConnection(connect(url
,info
));
251 // -----------------------------------------------------------------------------
252 void ADOS::ThrowException(ADOConnection
* _pAdoCon
,const Reference
< XInterface
>& _xInterface
) throw(SQLException
, RuntimeException
)
254 ADOErrors
*pErrors
= NULL
;
255 _pAdoCon
->get_Errors(&pErrors
);
257 return; // no error found
261 // read all noted errors and issue them
263 pErrors
->get_Count(&nLen
);
266 SQLException aException
;
267 aException
.ErrorCode
= 1000;
268 for (sal_Int32 i
= nLen
-1; i
>=0; --i
)
270 ADOError
*pError
= NULL
;
271 pErrors
->get_Item(OLEVariant(i
),&pError
);
272 WpADOError
aErr(pError
);
273 OSL_ENSURE(pError
,"No error in collection found! BAD!");
277 aException
= SQLException(aErr
.GetDescription(),_xInterface
,aErr
.GetSQLState(),aErr
.GetNumber(),Any());
280 SQLException aTemp
= SQLException(aErr
.GetDescription(),
281 _xInterface
,aErr
.GetSQLState(),aErr
.GetNumber(),makeAny(aException
));
282 aTemp
.NextException
<<= aException
;
295 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */