Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / connectivity / source / drivers / ado / ADriver.cxx
blob6568f7a1ee84de8b25fe8aae2076fdf8a9885f55
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
33 #include <objbase.h>
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)
48 ,m_xContext(_xORB)
49 ,mnNbCallCoInitializeExForReinit(0)
51 o3tl::safeCoInitializeEx(COINIT_APARTMENTTHREADED, mnNbCallCoInitializeExForReinit);
54 ODriver::~ODriver()
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);
67 if (xComp.is())
68 xComp->dispose();
70 m_xConnections.clear();
72 ODriver_BASE::disposing();
74 // static ServiceInfo
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) )
94 return nullptr;
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));
102 return pCon;
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" };
127 return
130 "IgnoreDriverPrivileges",
131 "Ignore the privileges from the database driver.",
132 false,
133 "false",
134 aBooleanValues
137 "EscapeDateTime",
138 "Escape date time format.",
139 false,
140 "true",
141 aBooleanValues
144 "TypeInfoSettings",
145 "Defines how the type info of the database metadata should be manipulated.",
146 false,
152 return Sequence< DriverPropertyInfo >();
155 sal_Int32 SAL_CALL ODriver::getMajorVersion( )
157 return 1;
160 sal_Int32 SAL_CALL ODriver::getMinorVersion( )
162 return 0;
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; });
179 if (foundConnection)
180 pConnection = pSearchConnection;
183 Reference< XTablesSupplier > xTab;
184 if(pConnection)
186 WpADOCatalog aCatalog;
187 aCatalog.Create();
188 if(aCatalog.IsValid())
190 aCatalog.putref_ActiveConnection(pConnection->getConnection());
191 rtl::Reference<OCatalog> pCatalog = new OCatalog(aCatalog,pConnection);
192 xTab = pCatalog;
193 pConnection->setCatalog(xTab);
194 pConnection->setCatalog(pCatalog.get());
197 return xTab;
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);
211 if(!pErrors)
212 return; // no error found
214 // read all noted errors and issue them
215 sal_Int32 nLen;
216 pErrors->get_Count(&nLen);
217 if (nLen)
219 SQLException aException;
220 aException.ErrorCode = 1000;
221 for (sal_Int32 i = nLen-1; i>=0; --i)
223 WpADOError aErr;
224 pErrors->get_Item(OLEVariant(i),&aErr);
225 OSL_ENSURE(aErr,"No error in collection found! BAD!");
226 if(aErr)
228 if(i==nLen-1)
229 aException = SQLException(aErr.GetDescription(),_xInterface,aErr.GetSQLState(),aErr.GetNumber(),Any());
230 else
232 SQLException aTemp(aErr.GetDescription(),
233 _xInterface,aErr.GetSQLState(),aErr.GetNumber(),Any(aException));
234 aTemp.NextException <<= aException;
235 aException = aTemp;
239 pErrors->Clear();
240 throw 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: */