LibreOffice Module connectivity (master) 1
ADriver.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
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>
31#include <rtl/ref.hxx>
32#include <strings.hrc>
33#include <objbase.h>
34
36
37using namespace connectivity;
38using namespace connectivity::ado;
39using namespace com::sun::star::uno;
40using namespace com::sun::star::lang;
41using namespace com::sun::star::beans;
42using namespace com::sun::star::sdbc;
43using namespace com::sun::star::sdbcx;
44
45
46ODriver::ODriver(const css::uno::Reference< css::uno::XComponentContext >& _xORB)
48 ,m_xContext(_xORB)
49 ,mnNbCallCoInitializeExForReinit(0)
50{
51 o3tl::safeCoInitializeEx(COINIT_APARTMENTTHREADED, mnNbCallCoInitializeExForReinit);
52}
53
55{
56 o3tl::safeCoUninitializeReinit(COINIT_MULTITHREADED, mnNbCallCoInitializeExForReinit);
57}
58
60{
61 ::osl::MutexGuard aGuard(m_aMutex);
62
63
64 for (auto& rxConnection : m_xConnections)
65 {
66 Reference< XComponent > xComp(rxConnection.get(), UNO_QUERY);
67 if (xComp.is())
68 xComp->dispose();
69 }
70 m_xConnections.clear();
71
72 ODriver_BASE::disposing();
73}
74// static ServiceInfo
75
77{
78 return "com.sun.star.comp.sdbc.ado.ODriver";
79}
80
82{
83 return { "com.sun.star.sdbc.Driver", "com.sun.star.sdbcx.Driver" };
84}
85
86sal_Bool SAL_CALL ODriver::supportsService( const OUString& _rServiceName )
87{
88 return cppu::supportsService(this, _rServiceName);
89}
90
91Reference< XConnection > SAL_CALL ODriver::connect( const OUString& url, const Sequence< PropertyValue >& info )
92{
93 if ( ! acceptsURL(url) )
94 return nullptr;
95
96 // we need to wrap the connection as the construct call might throw
98 pCon->construct(url,info);
99 OConnection* pPtr = pCon.get();
100 m_xConnections.push_back(WeakReferenceHelper(*pPtr));
101
102 return pCon;
103}
104
105sal_Bool SAL_CALL ODriver::acceptsURL( const OUString& url )
106{
107 return url.startsWith("sdbc:ado:");
108}
109
110void ODriver::impl_checkURL_throw(const OUString& _sUrl)
111{
112 if ( !acceptsURL(_sUrl) )
113 {
114 SharedResources aResources;
115 const OUString sMessage = aResources.getResourceString(STR_URI_SYNTAX_ERROR);
117 } // if ( !acceptsURL(_sUrl) )
118}
119
120Sequence< DriverPropertyInfo > SAL_CALL ODriver::getPropertyInfo( const OUString& url, const Sequence< PropertyValue >& /*info*/ )
121{
123 if ( acceptsURL(url) )
124 {
125 Sequence< OUString > aBooleanValues{ "false", "true" };
126
127 return
128 {
129 {
130 "IgnoreDriverPrivileges",
131 "Ignore the privileges from the database driver.",
132 false,
133 "false",
134 aBooleanValues
135 },
136 {
137 "EscapeDateTime",
138 "Escape date time format.",
139 false,
140 "true",
141 aBooleanValues
142 },
143 {
144 "TypeInfoSettings",
145 "Defines how the type info of the database metadata should be manipulated.",
146 false,
147 {},
148 {}
149 }
150 };
151 }
153}
154
155sal_Int32 SAL_CALL ODriver::getMajorVersion( )
156{
157 return 1;
158}
159
160sal_Int32 SAL_CALL ODriver::getMinorVersion( )
161{
162 return 0;
163}
164
165// XDataDefinitionSupplier
167{
168 ::osl::MutexGuard aGuard( m_aMutex );
169 if (ODriver_BASE::rBHelper.bDisposed)
170 throw DisposedException();
171
172 OConnection* pConnection = nullptr;
173 Reference< css::lang::XUnoTunnel> xTunnel(connection,UNO_QUERY);
174 if (auto pSearchConnection = comphelper::getFromUnoTunnel<OConnection>(xTunnel))
175 {
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;
181 }
182
184 if(pConnection)
185 {
186 WpADOCatalog aCatalog;
187 aCatalog.Create();
188 if(aCatalog.IsValid())
189 {
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());
195 }
196 }
197 return xTab;
198}
199
201{
203 return getDataDefinitionByConnection(connect(url,info));
204}
205
206
207void ADOS::ThrowException(ADOConnection* _pAdoCon,const Reference< XInterface >& _xInterface)
208{
209 sal::systools::COMReference<ADOErrors> pErrors;
210 _pAdoCon->get_Errors(&pErrors);
211 if(!pErrors)
212 return; // no error found
213
214 // read all noted errors and issue them
215 sal_Int32 nLen;
216 pErrors->get_Count(&nLen);
217 if (nLen)
218 {
219 SQLException aException;
220 aException.ErrorCode = 1000;
221 for (sal_Int32 i = nLen-1; i>=0; --i)
222 {
223 WpADOError aErr;
224 pErrors->get_Item(OLEVariant(i),&aErr);
225 OSL_ENSURE(aErr,"No error in collection found! BAD!");
226 if(aErr)
227 {
228 if(i==nLen-1)
229 aException = SQLException(aErr.GetDescription(),_xInterface,aErr.GetSQLState(),aErr.GetNumber(),Any());
230 else
231 {
232 SQLException aTemp(aErr.GetDescription(),
233 _xInterface,aErr.GetSQLState(),aErr.GetNumber(),Any(aException));
234 aTemp.NextException <<= aException;
235 aException = aTemp;
236 }
237 }
238 }
239 pErrors->Clear();
240 throw aException;
241 }
242}
243
244extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
246 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
247{
248 return cppu::acquire(new ODriver(context));
249}
250/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * connectivity_ado_ODriver_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
Definition: ADriver.cxx:245
Reference< XComponentContext > m_xContext
helper class for accessing resources shared by different libraries in the connectivity module
OUString getResourceString(TranslateId pResId) const
loads a string from the shared resource file
static void ThrowException(ADOConnection *_pAdoCon, const css::uno::Reference< css::uno::XInterface > &_xInterface)
Definition: ADriver.cxx:207
virtual void SAL_CALL setCatalog(const OUString &catalog) override
WpADOConnection & getConnection()
virtual sal_Int32 SAL_CALL getMajorVersion() override
Definition: ADriver.cxx:155
virtual css::uno::Reference< css::sdbcx::XTablesSupplier > SAL_CALL getDataDefinitionByConnection(const css::uno::Reference< css::sdbc::XConnection > &connection) override
Definition: ADriver.cxx:166
virtual css::uno::Reference< css::sdbc::XConnection > SAL_CALL connect(const OUString &url, const css::uno::Sequence< css::beans::PropertyValue > &info) override
virtual sal_Int32 SAL_CALL getMinorVersion() override
Definition: ADriver.cxx:160
virtual css::uno::Sequence< css::sdbc::DriverPropertyInfo > SAL_CALL getPropertyInfo(const OUString &url, const css::uno::Sequence< css::beans::PropertyValue > &info) override
virtual OUString SAL_CALL getImplementationName() override
Definition: ADriver.cxx:76
connectivity::OWeakRefArray m_xConnections
Definition: ADriver.hxx:40
virtual css::uno::Reference< css::sdbcx::XTablesSupplier > SAL_CALL getDataDefinitionByURL(const OUString &url, const css::uno::Sequence< css::beans::PropertyValue > &info) override
Definition: ADriver.cxx:200
virtual void SAL_CALL disposing() override
Definition: ADriver.cxx:59
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: ADriver.cxx:81
void impl_checkURL_throw(const OUString &_sUrl)
Definition: ADriver.cxx:110
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
Definition: ADriver.cxx:86
virtual sal_Bool SAL_CALL acceptsURL(const OUString &url) override
Definition: ADriver.cxx:105
void putref_ActiveConnection(IDispatch *pCon)
Definition: Awrapado.cxx:1237
OUString GetDescription() const
Definition: Awrapado.cxx:390
sal_Int32 GetNumber() const
Definition: Awrapado.cxx:406
OUString GetSQLState() const
Definition: Awrapado.cxx:414
std::mutex m_aMutex
::cppu::WeakComponentImplHelper< css::sdbc::XDriver, css::sdbcx::XDataDefinitionSupplier, css::lang::XServiceInfo > ODriver_BASE
Definition: ADriver.hxx:35
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
void throwGenericSQLException(const OUString &_rMsg, const css::uno::Reference< css::uno::XInterface > &_rxSource)
throw a generic SQLException, i.e.
int i
OUString sMessage
unsigned char sal_Bool