Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / connectivity / source / drivers / ado / AConnection.cxx
blob686a9fed58aa0e36e4cdb436aaf229fbbffb3d41
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 <sal/config.h>
22 #include <cstddef>
23 #include <string_view>
25 #include <ado/AConnection.hxx>
26 #include <ado/ADatabaseMetaData.hxx>
27 #include <ado/ADriver.hxx>
28 #include <ado/AStatement.hxx>
29 #include <ado/ACallableStatement.hxx>
30 #include <ado/APreparedStatement.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>
36 #include <comphelper/servicehelper.hxx>
37 #include <connectivity/dbexception.hxx>
38 #include <o3tl/string_view.hxx>
39 #include <osl/file.hxx>
40 #include <systools/win32/oleauto.hxx>
41 #include <strings.hrc>
43 using namespace dbtools;
44 using namespace connectivity::ado;
45 using namespace com::sun::star::uno;
46 using namespace com::sun::star::lang;
47 using namespace com::sun::star::beans;
48 using namespace com::sun::star::sdbc;
49 using namespace com::sun::star::sdbcx;
52 IMPLEMENT_SERVICE_INFO(OConnection,"com.sun.star.sdbcx.AConnection","com.sun.star.sdbc.Connection");
54 OConnection::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)
62 osl_atomic_increment( &m_refCount );
64 sal::systools::COMReference<IClassFactory2> pIUnknown;
65 if (!FAILED(pIUnknown.CoGetClassObject(ADOS::CLSID_ADOCONNECTION_21, CLSCTX_INPROC_SERVER)))
67 HRESULT hr = pIUnknown->CreateInstanceLic(nullptr,
68 nullptr,
69 ADOS::IID_ADOCONNECTION_21,
70 ADOS::GetKeyStr(),
71 reinterpret_cast<void**>(&m_aAdoConnection));
73 if( !FAILED( hr ) )
75 OSL_ENSURE(m_aAdoConnection, "OConnection::OConnection: invalid ADO object!");
79 osl_atomic_decrement( &m_refCount );
82 OConnection::~OConnection()
86 void OConnection::construct(std::u16string_view url,const Sequence< PropertyValue >& info)
88 osl_atomic_increment( &m_refCount );
90 setConnectionInfo(info);
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);
98 sal_Int32 nTimeout = 20;
99 const PropertyValue *pIter = info.getConstArray();
100 const PropertyValue *pEnd = pIter + info.getLength();
101 for(;pIter != pEnd;++pIter)
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;
112 if(m_aAdoConnection)
114 if(m_aAdoConnection.Open(aDSN,aUID,aPWD,adConnectUnspecified))
115 m_aAdoConnection.PutCommandTimeout(nTimeout);
116 else
117 ADOS::ThrowException(m_aAdoConnection,*this);
118 if(m_aAdoConnection.get_State() != adStateOpen)
119 throwGenericSQLException( STR_NO_CONNECTION,*this );
121 WpADOProperties aProps = m_aAdoConnection.get_Properties();
122 if(aProps.IsValid())
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();
130 buildTypeInfo();
131 //bErg = TRUE;
133 else
134 ::dbtools::throwFunctionSequenceException(*this);
137 catch(const Exception& )
139 osl_atomic_decrement( &m_refCount );
140 throw;
142 osl_atomic_decrement( &m_refCount );
145 Reference< XStatement > SAL_CALL OConnection::createStatement( )
147 ::osl::MutexGuard aGuard( m_aMutex );
148 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
150 Reference< XStatement > xStmt = new OStatement(this);
151 m_aStatements.push_back(WeakReferenceHelper(xStmt));
152 return xStmt;
155 Reference< XPreparedStatement > SAL_CALL OConnection::prepareStatement( const OUString& sql )
157 ::osl::MutexGuard aGuard( m_aMutex );
158 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
161 Reference< XPreparedStatement > xPStmt = new OPreparedStatement(this, sql);
162 m_aStatements.push_back(WeakReferenceHelper(xPStmt));
163 return xPStmt;
166 Reference< XPreparedStatement > SAL_CALL OConnection::prepareCall( const OUString& sql )
168 ::osl::MutexGuard aGuard( m_aMutex );
169 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
172 Reference< XPreparedStatement > xPStmt = new OCallableStatement(this, sql);
173 m_aStatements.push_back(WeakReferenceHelper(xPStmt));
174 return xPStmt;
177 OUString SAL_CALL OConnection::nativeSQL( const OUString& _sql )
179 ::osl::MutexGuard aGuard( m_aMutex );
180 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
183 OUString sql = _sql;
184 WpADOProperties aProps = m_aAdoConnection.get_Properties();
185 if(aProps.IsValid())
187 OTools::putValue(aProps, std::u16string_view(u"Jet OLEDB:ODBC Parsing"), true);
188 WpADOCommand aCommand;
189 aCommand.Create();
190 aCommand.put_ActiveConnection(static_cast<IDispatch*>(m_aAdoConnection));
191 aCommand.put_CommandText(sql);
192 sql = aCommand.get_CommandText();
195 return sql;
198 void SAL_CALL OConnection::setAutoCommit( sal_Bool autoCommit )
200 ::osl::MutexGuard aGuard( m_aMutex );
201 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
204 m_bAutocommit = autoCommit;
205 if(!autoCommit)
206 m_aAdoConnection.BeginTrans();
207 else
208 m_aAdoConnection.RollbackTrans();
211 sal_Bool SAL_CALL OConnection::getAutoCommit( )
213 ::osl::MutexGuard aGuard( m_aMutex );
214 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
217 return m_bAutocommit;
220 void SAL_CALL OConnection::commit( )
222 ::osl::MutexGuard aGuard( m_aMutex );
223 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
226 m_aAdoConnection.CommitTrans();
229 void SAL_CALL OConnection::rollback( )
231 ::osl::MutexGuard aGuard( m_aMutex );
232 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
235 m_aAdoConnection.RollbackTrans();
238 sal_Bool SAL_CALL OConnection::isClosed( )
240 ::osl::MutexGuard aGuard( m_aMutex );
242 return OConnection_BASE::rBHelper.bDisposed && !m_aAdoConnection.get_State();
245 Reference< XDatabaseMetaData > SAL_CALL OConnection::getMetaData( )
247 ::osl::MutexGuard aGuard( m_aMutex );
248 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
251 Reference< XDatabaseMetaData > xMetaData = m_xMetaData;
252 if(!xMetaData.is())
254 xMetaData = new ODatabaseMetaData(this);
255 m_xMetaData = xMetaData;
258 return xMetaData;
261 void SAL_CALL OConnection::setReadOnly( sal_Bool readOnly )
263 ::osl::MutexGuard aGuard( m_aMutex );
264 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
267 m_aAdoConnection.put_Mode(readOnly ? adModeRead : adModeReadWrite);
268 ADOS::ThrowException(m_aAdoConnection,*this);
271 sal_Bool SAL_CALL OConnection::isReadOnly( )
273 ::osl::MutexGuard aGuard( m_aMutex );
274 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
277 return m_aAdoConnection.get_Mode() == adModeRead;
280 void SAL_CALL OConnection::setCatalog( const OUString& catalog )
282 ::osl::MutexGuard aGuard( m_aMutex );
283 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
285 m_aAdoConnection.PutDefaultDatabase(catalog);
286 ADOS::ThrowException(m_aAdoConnection,*this);
289 OUString SAL_CALL OConnection::getCatalog( )
291 ::osl::MutexGuard aGuard( m_aMutex );
292 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
294 return m_aAdoConnection.GetDefaultDatabase();
297 void SAL_CALL OConnection::setTransactionIsolation( sal_Int32 level )
299 ::osl::MutexGuard aGuard( m_aMutex );
300 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
303 IsolationLevelEnum eIso;
304 switch(level)
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;
325 m_aAdoConnection.put_IsolationLevel(eIso);
326 ADOS::ThrowException(m_aAdoConnection,*this);
329 sal_Int32 SAL_CALL OConnection::getTransactionIsolation( )
331 ::osl::MutexGuard aGuard( m_aMutex );
332 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
335 sal_Int32 nRet = 0;
336 switch(m_aAdoConnection.get_IsolationLevel())
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");
356 ADOS::ThrowException(m_aAdoConnection,*this);
357 return nRet;
360 Reference< css::container::XNameAccess > SAL_CALL OConnection::getTypeMap( )
362 ::osl::MutexGuard aGuard( m_aMutex );
363 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
366 return nullptr;
369 void SAL_CALL OConnection::setTypeMap( const Reference< css::container::XNameAccess >& /*typeMap*/ )
371 ::dbtools::throwFeatureNotImplementedSQLException( "XConnection::setTypeMap", *this );
374 // XCloseable
375 void SAL_CALL OConnection::close( )
378 ::osl::MutexGuard aGuard( m_aMutex );
379 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
382 dispose();
385 // XWarningsSupplier
386 Any SAL_CALL OConnection::getWarnings( )
388 return Any();
391 void SAL_CALL OConnection::clearWarnings( )
395 void OConnection::buildTypeInfo()
397 ::osl::MutexGuard aGuard( m_aMutex );
399 ADORecordset *pRecordset = m_aAdoConnection.getTypeInfo();
400 if ( pRecordset )
402 pRecordset->AddRef();
403 VARIANT_BOOL bIsAtBOF;
404 pRecordset->get_BOF(&bIsAtBOF);
406 bool bOk = true;
407 if ( bIsAtBOF == VARIANT_TRUE )
408 bOk = SUCCEEDED(pRecordset->MoveNext());
410 if ( bOk )
412 // HACK for access
413 static const char s_sVarChar[] = "VarChar";
416 sal_Int32 nPos = 1;
417 OExtendedTypeInfo* aInfo = new OExtendedTypeInfo;
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)
438 aInfo->aSimpleType.nMaximumScale = 4;
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.
445 m_aTypeInfo.emplace(aInfo->eType,aInfo);
447 while ( SUCCEEDED(pRecordset->MoveNext()) );
449 pRecordset->Release();
453 void OConnection::disposing()
455 ::osl::MutexGuard aGuard(m_aMutex);
457 OConnection_BASE::disposing();
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;
464 m_aAdoConnection.Close();
466 for (auto& rEntry : m_aTypeInfo)
467 delete rEntry.second;
469 m_aTypeInfo.clear();
471 m_aAdoConnection.clear();
474 sal_Int64 SAL_CALL OConnection::getSomething( const css::uno::Sequence< sal_Int8 >& rId )
476 return comphelper::getSomethingImpl(rId, this,
477 comphelper::FallbackToGetSomethingOf<OConnection_BASE>{});
480 Sequence< sal_Int8 > OConnection::getUnoTunnelId()
482 static const comphelper::UnoIdInit implId;
483 return implId.getSeq();
486 const OExtendedTypeInfo* OConnection::getTypeInfoFromType(const OTypeInfoMap& _rTypeInfo,
487 DataTypeEnum _nType,
488 const OUString& _sTypeName,
489 sal_Int32 _nPrecision,
490 sal_Int32 _nScale,
491 bool& _brForceToType)
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
500 for(;aIter != aPair.second;++aIter)
502 // search the best matching type
503 OExtendedTypeInfo* pInfo = aIter->second;
504 if ( ( !_sTypeName.getLength()
505 || (pInfo->aSimpleType.aTypeName.equalsIgnoreAsciiCase(_sTypeName))
507 && (pInfo->aSimpleType.nPrecision >= _nPrecision)
508 && (pInfo->aSimpleType.nMaximumScale >= _nScale)
511 break;
514 if (aIter == aPair.second)
516 for(aIter = aPair.first; aIter != aPair.second; ++aIter)
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)
524 // we can not assert here because we could be in d&d
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());
532 break;
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)
542 // we can not assert here because we could be in d&d
543 pTypeInfo = aPair.first->second;
544 _brForceToType = true;
546 else
547 pTypeInfo = aIter->second;
549 else if ( _sTypeName.getLength() )
551 ::comphelper::UStringMixEqual aCase(false);
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);
558 if(aFind != _rTypeInfo.end())
559 pTypeInfo = aFind->second;
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;
568 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */