LibreOffice Module connectivity (master) 1
AConnection.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 <sal/config.h>
21
22#include <cstddef>
23#include <string_view>
24
25#include <ado/AConnection.hxx>
27#include <ado/ADriver.hxx>
28#include <ado/AStatement.hxx>
31#include <ado/ACatalog.hxx>
32#include <com/sun/star/sdbc/ColumnValue.hpp>
33#include <com/sun/star/sdbc/TransactionIsolation.hpp>
34#include <com/sun/star/sdbc/XRow.hpp>
35#include <com/sun/star/lang/DisposedException.hpp>
38#include <o3tl/string_view.hxx>
39#include <osl/file.hxx>
40#include <systools/win32/oleauto.hxx>
41#include <strings.hrc>
42
43using namespace dbtools;
44using namespace connectivity::ado;
45using namespace com::sun::star::uno;
46using namespace com::sun::star::lang;
47using namespace com::sun::star::beans;
48using namespace com::sun::star::sdbc;
49using namespace com::sun::star::sdbcx;
50
51
52IMPLEMENT_SERVICE_INFO(OConnection,"com.sun.star.sdbcx.AConnection","com.sun.star.sdbc.Connection");
53
54OConnection::OConnection(ODriver* _pDriver)
55 : m_xCatalog(nullptr),
56 m_pDriver(_pDriver),
57 m_pCatalog(nullptr),
58 m_nEngineType(0),
59 m_bClosed(false),
60 m_bAutocommit(true)
61{
62 osl_atomic_increment( &m_refCount );
63
64 sal::systools::COMReference<IClassFactory2> pIUnknown;
65 if (!FAILED(pIUnknown.CoGetClassObject(ADOS::CLSID_ADOCONNECTION_21, CLSCTX_INPROC_SERVER)))
66 {
67 HRESULT hr = pIUnknown->CreateInstanceLic(nullptr,
68 nullptr,
71 reinterpret_cast<void**>(&m_aAdoConnection));
72
73 if( !FAILED( hr ) )
74 {
75 OSL_ENSURE(m_aAdoConnection, "OConnection::OConnection: invalid ADO object!");
76 }
77 }
78
79 osl_atomic_decrement( &m_refCount );
80}
81
83{
84}
85
86void OConnection::construct(std::u16string_view url,const Sequence< PropertyValue >& info)
87{
88 osl_atomic_increment( &m_refCount );
89
91
92 std::size_t nLen = url.find(':');
93 nLen = url.find(':',nLen == std::u16string_view::npos ? 0 : nLen+1);
94 std::u16string_view aDSN(url.substr(nLen == std::u16string_view::npos ? 0 : nLen+1));
95 OUString aUID,aPWD;
96 o3tl::starts_with(aDSN, u"access:", &aDSN);
97
98 sal_Int32 nTimeout = 20;
99 const PropertyValue *pIter = info.getConstArray();
100 const PropertyValue *pEnd = pIter + info.getLength();
101 for(;pIter != pEnd;++pIter)
102 {
103 if(pIter->Name == "Timeout")
104 pIter->Value >>= nTimeout;
105 else if(pIter->Name == "user")
106 pIter->Value >>= aUID;
107 else if(pIter->Name == "password")
108 pIter->Value >>= aPWD;
109 }
110 try
111 {
113 {
114 if(m_aAdoConnection.Open(aDSN,aUID,aPWD,adConnectUnspecified))
116 else
118 if(m_aAdoConnection.get_State() != adStateOpen)
119 throwGenericSQLException( STR_NO_CONNECTION,*this );
120
122 if(aProps.IsValid())
123 {
124 OTools::putValue(aProps, std::u16string_view(u"Jet OLEDB:ODBC Parsing"), true);
125 OLEVariant aVar(
126 OTools::getValue(aProps, std::u16string_view(u"Jet OLEDB:Engine Type")));
127 if(!aVar.isNull() && !aVar.isEmpty())
128 m_nEngineType = aVar.getInt32();
129 }
131 //bErg = TRUE;
132 }
133 else
135
136 }
137 catch(const Exception& )
138 {
139 osl_atomic_decrement( &m_refCount );
140 throw;
141 }
142 osl_atomic_decrement( &m_refCount );
143}
144
146{
147 ::osl::MutexGuard aGuard( m_aMutex );
148 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
149
150 Reference< XStatement > xStmt = new OStatement(this);
151 m_aStatements.push_back(WeakReferenceHelper(xStmt));
152 return xStmt;
153}
154
156{
157 ::osl::MutexGuard aGuard( m_aMutex );
158 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
159
160
162 m_aStatements.push_back(WeakReferenceHelper(xPStmt));
163 return xPStmt;
164}
165
167{
168 ::osl::MutexGuard aGuard( m_aMutex );
169 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
170
171
173 m_aStatements.push_back(WeakReferenceHelper(xPStmt));
174 return xPStmt;
175}
176
177OUString SAL_CALL OConnection::nativeSQL( const OUString& _sql )
178{
179 ::osl::MutexGuard aGuard( m_aMutex );
180 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
181
182
183 OUString sql = _sql;
185 if(aProps.IsValid())
186 {
187 OTools::putValue(aProps, std::u16string_view(u"Jet OLEDB:ODBC Parsing"), true);
189 aCommand.Create();
190 aCommand.put_ActiveConnection(static_cast<IDispatch*>(m_aAdoConnection));
191 aCommand.put_CommandText(sql);
192 sql = aCommand.get_CommandText();
193 }
194
195 return sql;
196}
197
198void SAL_CALL OConnection::setAutoCommit( sal_Bool autoCommit )
199{
200 ::osl::MutexGuard aGuard( m_aMutex );
201 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
202
203
204 m_bAutocommit = autoCommit;
205 if(!autoCommit)
207 else
209}
210
212{
213 ::osl::MutexGuard aGuard( m_aMutex );
214 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
215
216
217 return m_bAutocommit;
218}
219
220void SAL_CALL OConnection::commit( )
221{
222 ::osl::MutexGuard aGuard( m_aMutex );
223 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
224
225
227}
228
229void SAL_CALL OConnection::rollback( )
230{
231 ::osl::MutexGuard aGuard( m_aMutex );
232 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
233
234
236}
237
239{
240 ::osl::MutexGuard aGuard( m_aMutex );
241
242 return OConnection_BASE::rBHelper.bDisposed && !m_aAdoConnection.get_State();
243}
244
246{
247 ::osl::MutexGuard aGuard( m_aMutex );
248 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
249
250
252 if(!xMetaData.is())
253 {
254 xMetaData = new ODatabaseMetaData(this);
255 m_xMetaData = xMetaData;
256 }
257
258 return xMetaData;
259}
260
261void SAL_CALL OConnection::setReadOnly( sal_Bool readOnly )
262{
263 ::osl::MutexGuard aGuard( m_aMutex );
264 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
265
266
267 m_aAdoConnection.put_Mode(readOnly ? adModeRead : adModeReadWrite);
269}
270
272{
273 ::osl::MutexGuard aGuard( m_aMutex );
274 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
275
276
277 return m_aAdoConnection.get_Mode() == adModeRead;
278}
279
280void SAL_CALL OConnection::setCatalog( const OUString& catalog )
281{
282 ::osl::MutexGuard aGuard( m_aMutex );
283 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
284
287}
288
289OUString SAL_CALL OConnection::getCatalog( )
290{
291 ::osl::MutexGuard aGuard( m_aMutex );
292 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
293
295}
296
297void SAL_CALL OConnection::setTransactionIsolation( sal_Int32 level )
298{
299 ::osl::MutexGuard aGuard( m_aMutex );
300 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
301
302
303 IsolationLevelEnum eIso;
304 switch(level)
305 {
306 case TransactionIsolation::NONE:
307 eIso = adXactUnspecified;
308 break;
309 case TransactionIsolation::READ_UNCOMMITTED:
310 eIso = adXactReadUncommitted;
311 break;
312 case TransactionIsolation::READ_COMMITTED:
313 eIso = adXactReadCommitted;
314 break;
315 case TransactionIsolation::REPEATABLE_READ:
316 eIso = adXactRepeatableRead;
317 break;
318 case TransactionIsolation::SERIALIZABLE:
319 eIso = adXactSerializable;
320 break;
321 default:
322 OSL_FAIL("OConnection::setTransactionIsolation invalid level");
323 return;
324 }
327}
328
330{
331 ::osl::MutexGuard aGuard( m_aMutex );
332 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
333
334
335 sal_Int32 nRet = 0;
337 {
338 case adXactUnspecified:
339 nRet = TransactionIsolation::NONE;
340 break;
341 case adXactReadUncommitted:
342 nRet = TransactionIsolation::READ_UNCOMMITTED;
343 break;
344 case adXactReadCommitted:
345 nRet = TransactionIsolation::READ_COMMITTED;
346 break;
347 case adXactRepeatableRead:
348 nRet = TransactionIsolation::REPEATABLE_READ;
349 break;
350 case adXactSerializable:
351 nRet = TransactionIsolation::SERIALIZABLE;
352 break;
353 default:
354 OSL_FAIL("OConnection::setTransactionIsolation invalid level");
355 }
357 return nRet;
358}
359
361{
362 ::osl::MutexGuard aGuard( m_aMutex );
363 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
364
365
366 return nullptr;
367}
368
370{
371 ::dbtools::throwFeatureNotImplementedSQLException( "XConnection::setTypeMap", *this );
372}
373
374// XCloseable
375void SAL_CALL OConnection::close( )
376{
377 {
378 ::osl::MutexGuard aGuard( m_aMutex );
379 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
380
381 }
382 dispose();
383}
384
385// XWarningsSupplier
387{
388 return Any();
389}
390
392{
393}
394
396{
397 ::osl::MutexGuard aGuard( m_aMutex );
398
399 ADORecordset *pRecordset = m_aAdoConnection.getTypeInfo();
400 if ( pRecordset )
401 {
402 pRecordset->AddRef();
403 VARIANT_BOOL bIsAtBOF;
404 pRecordset->get_BOF(&bIsAtBOF);
405
406 bool bOk = true;
407 if ( bIsAtBOF == VARIANT_TRUE )
408 bOk = SUCCEEDED(pRecordset->MoveNext());
409
410 if ( bOk )
411 {
412 // HACK for access
413 static const char s_sVarChar[] = "VarChar";
414 do
415 {
416 sal_Int32 nPos = 1;
418 aInfo->aSimpleType.aTypeName = ADOS::getField(pRecordset,nPos++).get_Value().getString();
419 aInfo->eType = static_cast<DataTypeEnum>(ADOS::getField(pRecordset,nPos++).get_Value().getInt32());
420 if ( aInfo->eType == adWChar && aInfo->aSimpleType.aTypeName == s_sVarChar )
421 aInfo->eType = adVarWChar;
422 aInfo->aSimpleType.nType = static_cast<sal_Int16>(ADOS::MapADOType2Jdbc(aInfo->eType));
423 aInfo->aSimpleType.nPrecision = ADOS::getField(pRecordset,nPos++).get_Value().getInt32();
424 nPos++; // aLiteralPrefix
425 nPos++; // aLiteralSuffix
426 nPos++; // aCreateParams
427 nPos++; // bNullable
428 nPos++; // bCaseSensitive
429 nPos++; // nSearchType
430 nPos++; // bUnsigned
431 nPos++; // bCurrency
432 nPos++; // bAutoIncrement
433 aInfo->aSimpleType.aLocalTypeName = ADOS::getField(pRecordset,nPos++).get_Value().getString();
434 nPos++; // nMinimumScale
435 aInfo->aSimpleType.nMaximumScale = ADOS::getField(pRecordset,nPos++).get_Value().getInt16();
436 if ( adCurrency == aInfo->eType && !aInfo->aSimpleType.nMaximumScale)
437 {
438 aInfo->aSimpleType.nMaximumScale = 4;
439 }
440 nPos++; // nNumPrecRadix
441 // Now that we have the type info, save it
442 // in the Hashtable if we don't already have an
443 // entry for this SQL type.
444
445 m_aTypeInfo.emplace(aInfo->eType,aInfo);
446 }
447 while ( SUCCEEDED(pRecordset->MoveNext()) );
448 }
449 pRecordset->Release();
450 }
451}
452
454{
455 ::osl::MutexGuard aGuard(m_aMutex);
456
458
459 m_bClosed = true;
460 m_xMetaData = css::uno::WeakReference< css::sdbc::XDatabaseMetaData>();
461 m_xCatalog = css::uno::WeakReference< css::sdbcx::XTablesSupplier>();
462 m_pDriver = nullptr;
463
465
466 for (auto& rEntry : m_aTypeInfo)
467 delete rEntry.second;
468
469 m_aTypeInfo.clear();
470
472}
473
474sal_Int64 SAL_CALL OConnection::getSomething( const css::uno::Sequence< sal_Int8 >& rId )
475{
476 return comphelper::getSomethingImpl(rId, this,
478}
479
481{
482 static const comphelper::UnoIdInit implId;
483 return implId.getSeq();
484}
485
487 DataTypeEnum _nType,
488 const OUString& _sTypeName,
489 sal_Int32 _nPrecision,
490 sal_Int32 _nScale,
491 bool& _brForceToType)
492{
493 const OExtendedTypeInfo* pTypeInfo = nullptr;
494 _brForceToType = false;
495 // search for type
496 std::pair<OTypeInfoMap::const_iterator, OTypeInfoMap::const_iterator> aPair = _rTypeInfo.equal_range(_nType);
497 OTypeInfoMap::const_iterator aIter = aPair.first;
498 if(aIter != _rTypeInfo.end()) // compare with end is correct here
499 {
500 for(;aIter != aPair.second;++aIter)
501 {
502 // search the best matching type
503 OExtendedTypeInfo* pInfo = aIter->second;
504 if ( ( !_sTypeName.getLength()
505 || (pInfo->aSimpleType.aTypeName.equalsIgnoreAsciiCase(_sTypeName))
506 )
507 && (pInfo->aSimpleType.nPrecision >= _nPrecision)
508 && (pInfo->aSimpleType.nMaximumScale >= _nScale)
509
510 )
511 break;
512 }
513
514 if (aIter == aPair.second)
515 {
516 for(aIter = aPair.first; aIter != aPair.second; ++aIter)
517 {
518 // search the best matching type (now comparing the local names)
519 if ( (aIter->second->aSimpleType.aLocalTypeName.equalsIgnoreAsciiCase(_sTypeName))
520 && (aIter->second->aSimpleType.nPrecision >= _nPrecision)
521 && (aIter->second->aSimpleType.nMaximumScale >= _nScale)
522 )
523 {
524// we can not assert here because we could be in d&d
525/*
526 OSL_FAIL(( OString("getTypeInfoFromType: assuming column type ")
527 += OString(aIter->second->aTypeName.getStr(), aIter->second->aTypeName.getLength(), osl_getThreadTextEncoding())
528 += OString("\" (expected type name ")
529 += OString(_sTypeName.getStr(), _sTypeName.getLength(), osl_getThreadTextEncoding())
530 += OString(" matches the type's local name).")).getStr());
531*/
532 break;
533 }
534 }
535 }
536
537 if (aIter == aPair.second)
538 { // no match for the names, no match for the local names
539 // -> drop the precision and the scale restriction, accept any type with the property
540 // type id (nType)
541
542 // we can not assert here because we could be in d&d
543 pTypeInfo = aPair.first->second;
544 _brForceToType = true;
545 }
546 else
547 pTypeInfo = aIter->second;
548 }
549 else if ( _sTypeName.getLength() )
550 {
552 // search for typeinfo where the typename is equal _sTypeName
553 OTypeInfoMap::const_iterator aFind = std::find_if(_rTypeInfo.begin(), _rTypeInfo.end(),
554 [&aCase, &_sTypeName] (const OTypeInfoMap::value_type& typeInfo) {
555 return aCase(typeInfo.second->getDBName(), _sTypeName);
556 });
557
558 if(aFind != _rTypeInfo.end())
559 pTypeInfo = aFind->second;
560 }
561
562// we can not assert here because we could be in d&d
563// OSL_ENSURE(pTypeInfo, "getTypeInfoFromType: no type info found for this type!");
564 return pTypeInfo;
565}
566
567
568/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
IMPLEMENT_SERVICE_INFO(OConnection,"com.sun.star.sdbcx.AConnection","com.sun.star.sdbc.Connection")
const css::uno::Sequence< sal_Int8 > & getSeq() const
connectivity::OWeakRefArray m_aStatements
Definition: TConnection.hxx:47
void setConnectionInfo(const css::uno::Sequence< css::beans::PropertyValue > &_aInfo)
Definition: TConnection.hxx:67
css::uno::WeakReference< css::sdbc::XDatabaseMetaData > m_xMetaData
Definition: TConnection.hxx:53
void throwGenericSQLException(TranslateId pErrorResourceId, const css::uno::Reference< css::uno::XInterface > &_xContext)
Definition: TConnection.cxx:74
virtual void SAL_CALL disposing() override
Definition: TConnection.cxx:39
static void ThrowException(ADOConnection *_pAdoCon, const css::uno::Reference< css::uno::XInterface > &_xInterface)
Definition: ADriver.cxx:207
static const CLSID CLSID_ADOCONNECTION_21
Definition: adoimp.hxx:42
static const IID IID_ADOCONNECTION_21
Definition: adoimp.hxx:43
static sal_Int32 MapADOType2Jdbc(DataTypeEnum eType)
Definition: adoimp.cxx:78
static WpADOField getField(ADORecordset *_pRecordSet, sal_Int32 _nColumnIndex)
Definition: adoimp.cxx:309
static sal::systools::BStr & GetKeyStr()
Definition: adoimp.cxx:71
virtual sal_Int32 SAL_CALL getTransactionIsolation() override
virtual void SAL_CALL setCatalog(const OUString &catalog) override
virtual void SAL_CALL setAutoCommit(sal_Bool autoCommit) override
virtual void SAL_CALL setTransactionIsolation(sal_Int32 level) override
virtual void SAL_CALL disposing() override
virtual css::uno::Reference< css::sdbc::XDatabaseMetaData > SAL_CALL getMetaData() override
void construct(std::u16string_view url, const css::uno::Sequence< css::beans::PropertyValue > &info)
virtual void SAL_CALL rollback() override
virtual OUString SAL_CALL nativeSQL(const OUString &sql) override
virtual void SAL_CALL close() override
virtual sal_Bool SAL_CALL isClosed() override
virtual css::uno::Reference< css::container::XNameAccess > SAL_CALL getTypeMap() override
virtual css::uno::Reference< css::sdbc::XPreparedStatement > SAL_CALL prepareCall(const OUString &sql) override
WpADOConnection m_aAdoConnection
Definition: AConnection.hxx:60
static css::uno::Sequence< sal_Int8 > getUnoTunnelId()
css::uno::WeakReference< css::sdbcx::XTablesSupplier > m_xCatalog
Definition: AConnection.hxx:57
virtual void SAL_CALL clearWarnings() override
virtual css::uno::Reference< css::sdbc::XPreparedStatement > SAL_CALL prepareStatement(const OUString &sql) override
virtual sal_Bool SAL_CALL getAutoCommit() override
virtual void SAL_CALL setReadOnly(sal_Bool readOnly) override
static const OExtendedTypeInfo * getTypeInfoFromType(const OTypeInfoMap &_rTypeInfo, DataTypeEnum _nType, const OUString &_sTypeName, sal_Int32 _nPrecision, sal_Int32 _nScale, bool &_brForceToType)
virtual void SAL_CALL setTypeMap(const css::uno::Reference< css::container::XNameAccess > &typeMap) override
virtual sal_Bool SAL_CALL isReadOnly() override
virtual sal_Int64 SAL_CALL getSomething(const css::uno::Sequence< sal_Int8 > &aIdentifier) override
virtual css::uno::Reference< css::sdbc::XStatement > SAL_CALL createStatement() override
virtual OUString SAL_CALL getCatalog() override
virtual css::uno::Any SAL_CALL getWarnings() override
virtual void SAL_CALL commit() override
static OLEVariant getValue(const WpADOProperties &_rProps, const OLEVariant &_aPosition)
getValue returns a specific property value
Definition: Awrapado.cxx:2003
static void putValue(const WpADOProperties &_rProps, const OLEVariant &_aPosition, const OLEVariant &_aValVar)
putValue set the property value at the ado column
Definition: Awrapado.cxx:1992
bool put_Mode(const ConnectModeEnum &eNum)
Definition: Awrapado.cxx:204
OUString GetDefaultDatabase() const
Definition: Awrapado.cxx:138
WpADOProperties get_Properties() const
Definition: Awrapado.cxx:37
bool Open(std::u16string_view ConnectionString, std::u16string_view UserID, std::u16string_view Password, long Options)
Definition: Awrapado.cxx:122
ADORecordset * getTypeInfo(DataTypeEnum _eType=adEmpty)
Definition: Awrapado.cxx:1950
void PutCommandTimeout(sal_Int32 nRet)
Definition: Awrapado.cxx:69
bool put_IsolationLevel(const IsolationLevelEnum &eNum)
Definition: Awrapado.cxx:162
IsolationLevelEnum get_IsolationLevel() const
Definition: Awrapado.cxx:154
ConnectModeEnum get_Mode() const
Definition: Awrapado.cxx:196
bool PutDefaultDatabase(std::u16string_view _bstr)
Definition: Awrapado.cxx:145
void get_Value(OLEVariant &aValVar) const
Definition: Awrapado.cxx:486
ULONG m_refCount
float u
sal_uInt16 nPos
sal_Int64 getSomethingImpl(const css::uno::Sequence< sal_Int8 > &rId, T *pThis, FallbackToGetSomethingOf< Base >={})
std::multimap< DataTypeEnum, OExtendedTypeInfo * > OTypeInfoMap
Definition: AConnection.hxx:43
void checkDisposed(bool _bThrow)
Definition: dbtools.cxx:1951
void throwFunctionSequenceException(const Reference< XInterface > &Context, const Any &Next)
void throwFeatureNotImplementedSQLException(const OUString &_rFeatureName, const Reference< XInterface > &_rxContext, const Any &_rNextException)
constexpr bool starts_with(std::basic_string_view< charT, traits > sv, std::basic_string_view< charT, traits > x) noexcept
void dispose()
return hr
::connectivity::OTypeInfo aSimpleType
Definition: AConnection.hxx:35
OUString aCommand
unsigned char sal_Bool