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