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 #include <ado/ADriver.hxx>
21 #include <ado/AConnection.hxx>
22 #include <ado/Awrapadox.hxx>
23 #include <ado/ACatalog.hxx>
24 #include <ado/Awrapado.hxx>
25 #include <ado/adoimp.hxx>
26 #include <com/sun/star/lang/DisposedException.hpp>
27 #include <comphelper/servicehelper.hxx>
28 #include <connectivity/dbexception.hxx>
29 #include <cppuhelper/supportsservice.hxx>
30 #include <o3tl/safeCoInitUninit.hxx>
31 #include <rtl/ref.hxx>
32 #include <strings.hrc>
35 #include <resource/sharedresources.hxx>
37 using namespace connectivity
;
38 using namespace connectivity::ado
;
39 using namespace com::sun::star::uno
;
40 using namespace com::sun::star::lang
;
41 using namespace com::sun::star::beans
;
42 using namespace com::sun::star::sdbc
;
43 using namespace com::sun::star::sdbcx
;
46 ODriver::ODriver(const css::uno::Reference
< css::uno::XComponentContext
>& _xORB
)
47 : ODriver_BASE(m_aMutex
)
49 ,mnNbCallCoInitializeExForReinit(0)
51 o3tl::safeCoInitializeEx(COINIT_APARTMENTTHREADED
, mnNbCallCoInitializeExForReinit
);
56 o3tl::safeCoUninitializeReinit(COINIT_MULTITHREADED
, mnNbCallCoInitializeExForReinit
);
59 void ODriver::disposing()
61 ::osl::MutexGuard
aGuard(m_aMutex
);
64 for (auto& rxConnection
: m_xConnections
)
66 Reference
< XComponent
> xComp(rxConnection
.get(), UNO_QUERY
);
70 m_xConnections
.clear();
72 ODriver_BASE::disposing();
76 OUString
ODriver::getImplementationName( )
78 return "com.sun.star.comp.sdbc.ado.ODriver";
81 Sequence
< OUString
> ODriver::getSupportedServiceNames( )
83 return { "com.sun.star.sdbc.Driver", "com.sun.star.sdbcx.Driver" };
86 sal_Bool SAL_CALL
ODriver::supportsService( const OUString
& _rServiceName
)
88 return cppu::supportsService(this, _rServiceName
);
91 Reference
< XConnection
> SAL_CALL
ODriver::connect( const OUString
& url
, const Sequence
< PropertyValue
>& info
)
93 if ( ! acceptsURL(url
) )
96 // we need to wrap the connection as the construct call might throw
97 rtl::Reference
<OConnection
> pCon(new OConnection(this));
98 pCon
->construct(url
,info
);
99 OConnection
* pPtr
= pCon
.get();
100 m_xConnections
.push_back(WeakReferenceHelper(*pPtr
));
105 sal_Bool SAL_CALL
ODriver::acceptsURL( const OUString
& url
)
107 return url
.startsWith("sdbc:ado:");
110 void ODriver::impl_checkURL_throw(const OUString
& _sUrl
)
112 if ( !acceptsURL(_sUrl
) )
114 SharedResources aResources
;
115 const OUString sMessage
= aResources
.getResourceString(STR_URI_SYNTAX_ERROR
);
116 ::dbtools::throwGenericSQLException(sMessage
,*this);
117 } // if ( !acceptsURL(_sUrl) )
120 Sequence
< DriverPropertyInfo
> SAL_CALL
ODriver::getPropertyInfo( const OUString
& url
, const Sequence
< PropertyValue
>& /*info*/ )
122 impl_checkURL_throw(url
);
123 if ( acceptsURL(url
) )
125 Sequence
< OUString
> aBooleanValues
{ "false", "true" };
130 "IgnoreDriverPrivileges",
131 "Ignore the privileges from the database driver.",
138 "Escape date time format.",
145 "Defines how the type info of the database metadata should be manipulated.",
152 return Sequence
< DriverPropertyInfo
>();
155 sal_Int32 SAL_CALL
ODriver::getMajorVersion( )
160 sal_Int32 SAL_CALL
ODriver::getMinorVersion( )
165 // XDataDefinitionSupplier
166 Reference
< XTablesSupplier
> SAL_CALL
ODriver::getDataDefinitionByConnection( const Reference
< css::sdbc::XConnection
>& connection
)
168 ::osl::MutexGuard
aGuard( m_aMutex
);
169 if (ODriver_BASE::rBHelper
.bDisposed
)
170 throw DisposedException();
172 OConnection
* pConnection
= nullptr;
173 Reference
< css::lang::XUnoTunnel
> xTunnel(connection
,UNO_QUERY
);
174 if (auto pSearchConnection
= comphelper::getFromUnoTunnel
<OConnection
>(xTunnel
))
176 auto foundConnection
= std::any_of(m_xConnections
.begin(), m_xConnections
.end(),
177 [&pSearchConnection
](const css::uno::WeakReferenceHelper
& rxConnection
) {
178 return static_cast<OConnection
*>(Reference
< XConnection
>::query(rxConnection
.get()).get()) == pSearchConnection
; });
180 pConnection
= pSearchConnection
;
183 Reference
< XTablesSupplier
> xTab
;
186 WpADOCatalog aCatalog
;
188 if(aCatalog
.IsValid())
190 aCatalog
.putref_ActiveConnection(pConnection
->getConnection());
191 rtl::Reference
<OCatalog
> pCatalog
= new OCatalog(aCatalog
,pConnection
);
193 pConnection
->setCatalog(xTab
);
194 pConnection
->setCatalog(pCatalog
.get());
200 Reference
< XTablesSupplier
> SAL_CALL
ODriver::getDataDefinitionByURL( const OUString
& url
, const Sequence
< PropertyValue
>& info
)
202 impl_checkURL_throw(url
);
203 return getDataDefinitionByConnection(connect(url
,info
));
207 void ADOS::ThrowException(ADOConnection
* _pAdoCon
,const Reference
< XInterface
>& _xInterface
)
209 sal::systools::COMReference
<ADOErrors
> pErrors
;
210 _pAdoCon
->get_Errors(&pErrors
);
212 return; // no error found
214 // read all noted errors and issue them
216 pErrors
->get_Count(&nLen
);
219 SQLException aException
;
220 aException
.ErrorCode
= 1000;
221 for (sal_Int32 i
= nLen
-1; i
>=0; --i
)
224 pErrors
->get_Item(OLEVariant(i
),&aErr
);
225 OSL_ENSURE(aErr
,"No error in collection found! BAD!");
229 aException
= SQLException(aErr
.GetDescription(),_xInterface
,aErr
.GetSQLState(),aErr
.GetNumber(),Any());
232 SQLException
aTemp(aErr
.GetDescription(),
233 _xInterface
,aErr
.GetSQLState(),aErr
.GetNumber(),Any(aException
));
234 aTemp
.NextException
<<= aException
;
244 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
245 connectivity_ado_ODriver_get_implementation(
246 css::uno::XComponentContext
* context
, css::uno::Sequence
<css::uno::Any
> const&)
248 return cppu::acquire(new ODriver(context
));
250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */