LCOV - code coverage report
Current view: top level - dbaccess/source/ui/relationdesign - RTableConnectionData.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 221 0.0 %
Date: 2014-04-14 Functions: 0 16 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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 "RTableConnectionData.hxx"
      21             : #include <tools/debug.hxx>
      22             : #include <com/sun/star/sdbc/KeyRule.hpp>
      23             : #include <com/sun/star/sdbcx/KeyType.hpp>
      24             : #include <com/sun/star/sdbcx/XKeysSupplier.hpp>
      25             : #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
      26             : #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
      27             : #include <com/sun/star/sdbcx/XAppend.hpp>
      28             : #include <com/sun/star/sdbcx/XDrop.hpp>
      29             : #include <com/sun/star/container/XIndexAccess.hpp>
      30             : #include "dbustrings.hrc"
      31             : #include "dbu_rel.hrc"
      32             : #include "UITools.hxx"
      33             : #include "moduledbu.hxx"
      34             : #include <connectivity/dbexception.hxx>
      35             : #include <connectivity/dbtools.hxx>
      36             : 
      37             : using namespace dbaui;
      38             : using namespace ::com::sun::star::sdbc;
      39             : using namespace ::com::sun::star::sdbcx;
      40             : using namespace ::com::sun::star::uno;
      41             : using namespace ::com::sun::star::beans;
      42             : using namespace ::com::sun::star::container;
      43             : using namespace ::com::sun::star::lang;
      44             : 
      45             : // class ORelationTableConnectionData
      46           0 : ORelationTableConnectionData::ORelationTableConnectionData()
      47             :     :OTableConnectionData()
      48             :     ,m_nUpdateRules(KeyRule::NO_ACTION)
      49             :     ,m_nDeleteRules(KeyRule::NO_ACTION)
      50           0 :     ,m_nCardinality(CARDINAL_UNDEFINED)
      51             : {
      52           0 : }
      53             : 
      54           0 : ORelationTableConnectionData::ORelationTableConnectionData( const TTableWindowData::value_type& _pReferencingTable,
      55             :                                                             const TTableWindowData::value_type& _pReferencedTable,
      56             :                                                             const OUString& rConnName )
      57             :     :OTableConnectionData( _pReferencingTable, _pReferencedTable )
      58             :     ,m_nUpdateRules(KeyRule::NO_ACTION)
      59             :     ,m_nDeleteRules(KeyRule::NO_ACTION)
      60           0 :     ,m_nCardinality(CARDINAL_UNDEFINED)
      61             : {
      62           0 :     m_aConnName = rConnName;
      63             : 
      64           0 :     if ( !m_aConnName.isEmpty() )
      65           0 :         SetCardinality();
      66           0 : }
      67             : 
      68           0 : ORelationTableConnectionData::ORelationTableConnectionData( const ORelationTableConnectionData& rConnData )
      69           0 :     :OTableConnectionData( rConnData )
      70             : {
      71           0 :     *this = rConnData;
      72           0 : }
      73             : 
      74           0 : ORelationTableConnectionData::~ORelationTableConnectionData()
      75             : {
      76           0 : }
      77             : 
      78           0 : sal_Bool ORelationTableConnectionData::DropRelation()
      79             : {
      80           0 :     ::osl::MutexGuard aGuard( m_aMutex );
      81             :     // delete relation
      82           0 :     Reference< XIndexAccess> xKeys = getReferencingTable()->getKeys();
      83           0 :     if( !m_aConnName.isEmpty() && xKeys.is() )
      84             :     {
      85           0 :         const sal_Int32 nCount = xKeys->getCount();
      86           0 :         for(sal_Int32 i = 0;i < nCount;++i)
      87             :         {
      88           0 :             Reference< XPropertySet> xKey(xKeys->getByIndex(i),UNO_QUERY);
      89             :             OSL_ENSURE(xKey.is(),"Key is not valid!");
      90           0 :             if(xKey.is())
      91             :             {
      92           0 :                 OUString sName;
      93           0 :                 xKey->getPropertyValue(PROPERTY_NAME) >>= sName;
      94           0 :                 if(sName == m_aConnName)
      95             :                 {
      96           0 :                     Reference< XDrop> xDrop(xKeys,UNO_QUERY);
      97             :                     OSL_ENSURE(xDrop.is(),"can't drop key because we haven't a drop interface!");
      98           0 :                     if(xDrop.is())
      99           0 :                         xDrop->dropByIndex(i);
     100           0 :                     break;
     101           0 :                 }
     102             :             }
     103           0 :         }
     104             :     }
     105           0 :     return sal_True;
     106             : }
     107             : 
     108           0 : void ORelationTableConnectionData::ChangeOrientation()
     109             : {
     110             :     // exchange Source- and DestFieldName of the lines
     111           0 :     OUString sTempString;
     112           0 :     OConnectionLineDataVec::iterator aIter = m_vConnLineData.begin();
     113           0 :     OConnectionLineDataVec::iterator aEnd = m_vConnLineData.end();
     114           0 :     for(;aIter != aEnd;++aIter)
     115             :     {
     116           0 :         sTempString = (*aIter)->GetSourceFieldName();
     117           0 :         (*aIter)->SetSourceFieldName( (*aIter)->GetDestFieldName() );
     118           0 :         (*aIter)->SetDestFieldName( sTempString );
     119             :     }
     120             : 
     121             :     // adapt member
     122           0 :     TTableWindowData::value_type pTemp = m_pReferencingTable;
     123           0 :     m_pReferencingTable = m_pReferencedTable;
     124           0 :     m_pReferencedTable = pTemp;
     125           0 : }
     126             : 
     127           0 : void ORelationTableConnectionData::SetCardinality()
     128             : {
     129           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     130           0 :     m_nCardinality = CARDINAL_UNDEFINED;
     131             : 
     132           0 :     if( IsSourcePrimKey() )
     133             :     {
     134           0 :         if( IsDestPrimKey() )
     135           0 :             m_nCardinality = CARDINAL_ONE_ONE;
     136             :         else
     137           0 :             m_nCardinality = CARDINAL_ONE_MANY;
     138             :     }
     139             : 
     140           0 :     if( IsDestPrimKey() )
     141             :     {
     142           0 :         if( !IsSourcePrimKey() )
     143           0 :             m_nCardinality = CARDINAL_MANY_ONE;
     144           0 :     }
     145             : 
     146           0 : }
     147             : 
     148           0 : sal_Bool ORelationTableConnectionData::checkPrimaryKey(const Reference< XPropertySet>& i_xTable,EConnectionSide _eEConnectionSide) const
     149             : {
     150             :     // check if Table has the primary key column dependig on _eEConnectionSide
     151           0 :     sal_uInt16  nPrimKeysCount      = 0,
     152           0 :             nValidLinesCount    = 0;
     153           0 :     const Reference< XNameAccess> xKeyColumns = dbtools::getPrimaryKeyColumns_throw(i_xTable);
     154           0 :     if ( xKeyColumns.is() )
     155             :     {
     156           0 :         Sequence< OUString> aKeyColumns = xKeyColumns->getElementNames();
     157           0 :         const OUString* pKeyIter = aKeyColumns.getConstArray();
     158           0 :         const OUString* pKeyEnd  = pKeyIter + aKeyColumns.getLength();
     159             : 
     160           0 :         for(;pKeyIter != pKeyEnd;++pKeyIter)
     161             :         {
     162           0 :             OConnectionLineDataVec::const_iterator aIter = m_vConnLineData.begin();
     163           0 :             OConnectionLineDataVec::const_iterator aEnd = m_vConnLineData.end();
     164           0 :             for(;aIter != aEnd;++aIter)
     165             :             {
     166           0 :                 ++nValidLinesCount;
     167           0 :                 if ( (*aIter)->GetFieldName(_eEConnectionSide) == *pKeyIter )
     168             :                 {
     169           0 :                     ++nPrimKeysCount;
     170           0 :                     break;
     171             :                 }
     172             :             }
     173             :         }
     174           0 :         if ( nPrimKeysCount != aKeyColumns.getLength() )
     175           0 :             return sal_False;
     176             :     }
     177           0 :     if ( !nPrimKeysCount || nPrimKeysCount != nValidLinesCount )
     178           0 :         return sal_False;
     179             : 
     180           0 :     return sal_True;
     181             : }
     182             : 
     183           0 : sal_Bool ORelationTableConnectionData::IsConnectionPossible()
     184             : {
     185           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     186             : 
     187             :     // if the SourceFields are a PrimKey, it's only the orientation which is wrong
     188           0 :     if ( IsSourcePrimKey() && !IsDestPrimKey() )
     189           0 :         ChangeOrientation();
     190             : 
     191           0 :     return sal_True;
     192             : }
     193             : 
     194           0 : OConnectionLineDataRef ORelationTableConnectionData::CreateLineDataObj()
     195             : {
     196           0 :     return new OConnectionLineData();
     197             : }
     198             : 
     199           0 : OConnectionLineDataRef ORelationTableConnectionData::CreateLineDataObj( const OConnectionLineData& rConnLineData )
     200             : {
     201           0 :     return new OConnectionLineData( rConnLineData );
     202             : }
     203             : 
     204           0 : void ORelationTableConnectionData::CopyFrom(const OTableConnectionData& rSource)
     205             : {
     206             :     // retract to the (non-virtual) operator= like in the base class
     207           0 :     *this = *static_cast<const ORelationTableConnectionData*>(&rSource);
     208           0 : }
     209             : 
     210           0 : ORelationTableConnectionData& ORelationTableConnectionData::operator=( const ORelationTableConnectionData& rConnData )
     211             : {
     212           0 :     if (&rConnData == this)
     213           0 :         return *this;
     214             : 
     215           0 :     OTableConnectionData::operator=( rConnData );
     216           0 :     m_nUpdateRules = rConnData.GetUpdateRules();
     217           0 :     m_nDeleteRules = rConnData.GetDeleteRules();
     218           0 :     m_nCardinality = rConnData.GetCardinality();
     219             : 
     220           0 :     return *this;
     221             : }
     222             : 
     223             : namespace dbaui
     224             : {
     225           0 : bool operator==(const ORelationTableConnectionData& lhs, const ORelationTableConnectionData& rhs)
     226             : {
     227           0 :     bool bEqual = (lhs.m_nUpdateRules == rhs.m_nUpdateRules)
     228           0 :         && (lhs.m_nDeleteRules == rhs.m_nDeleteRules)
     229           0 :         && (lhs.m_nCardinality == rhs.m_nCardinality)
     230           0 :         && (lhs.getReferencingTable() == rhs.getReferencingTable())
     231           0 :         && (lhs.getReferencedTable() == rhs.getReferencedTable())
     232           0 :         && (lhs.m_aConnName == rhs.m_aConnName)
     233           0 :         && (lhs.m_vConnLineData.size() == rhs.m_vConnLineData.size());
     234             : 
     235           0 :     if ( bEqual )
     236             :     {
     237           0 :         std::vector< OConnectionLineDataRef >::const_iterator aIter = lhs.m_vConnLineData.begin();
     238           0 :         std::vector< OConnectionLineDataRef >::const_iterator aEnd = lhs.m_vConnLineData.end();
     239           0 :         for (sal_Int32 i = 0; aIter != aEnd; ++aIter,++i)
     240             :         {
     241           0 :             if ( *(rhs.m_vConnLineData[i]) != **aIter )
     242           0 :                 break;
     243             :         }
     244           0 :         bEqual = aIter == aEnd;
     245             :     }
     246           0 :     return bEqual;
     247             : }
     248             : 
     249             : }
     250             : 
     251           0 : sal_Bool ORelationTableConnectionData::Update()
     252             : {
     253           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     254             :     // delete old relation
     255             :     {
     256           0 :         DropRelation();
     257           0 :         if( !IsConnectionPossible() )
     258           0 :             return sal_False;
     259             :     }
     260             : 
     261             :     // reassign the keys because the orientaion could be changed
     262           0 :     Reference<XPropertySet> xTableProp(getReferencingTable()->getTable());
     263           0 :     Reference< XIndexAccess> xKeys ( getReferencingTable()->getKeys());
     264             : 
     265           0 :     if ( !xKeys.is() )
     266           0 :         return sal_False;
     267             :     // create new relation
     268           0 :     Reference<XDataDescriptorFactory> xKeyFactory(xKeys,UNO_QUERY);
     269             :     OSL_ENSURE(xKeyFactory.is(),"No XDataDescriptorFactory Interface!");
     270           0 :     Reference<XAppend> xAppend(xKeyFactory,UNO_QUERY);
     271             :     OSL_ENSURE(xAppend.is(),"No XAppend Interface!");
     272             : 
     273           0 :     Reference<XPropertySet> xKey = xKeyFactory->createDataDescriptor();
     274             :     OSL_ENSURE(xKey.is(),"Key is null!");
     275           0 :     if ( xKey.is() && xTableProp.is() )
     276             :     {
     277             :         // build a foreign key name
     278           0 :         OUString sSourceName;
     279           0 :         xTableProp->getPropertyValue(PROPERTY_NAME) >>= sSourceName;
     280           0 :         OUString sKeyName = sSourceName;
     281           0 :         sKeyName += getReferencedTable()->GetTableName();
     282             : 
     283           0 :         xKey->setPropertyValue(PROPERTY_NAME,makeAny(sKeyName));
     284           0 :         xKey->setPropertyValue(PROPERTY_TYPE,makeAny(KeyType::FOREIGN));
     285           0 :         xKey->setPropertyValue(PROPERTY_REFERENCEDTABLE,makeAny(OUString(getReferencedTable()->GetTableName())));
     286           0 :         xKey->setPropertyValue(PROPERTY_UPDATERULE, makeAny(GetUpdateRules()));
     287           0 :         xKey->setPropertyValue(PROPERTY_DELETERULE, makeAny(GetDeleteRules()));
     288             :     }
     289             : 
     290           0 :     Reference<XColumnsSupplier> xColSup(xKey,UNO_QUERY);
     291           0 :     if ( xColSup.is() )
     292             :     {
     293           0 :         Reference<XNameAccess> xColumns = xColSup->getColumns();
     294           0 :         Reference<XDataDescriptorFactory> xColumnFactory(xColumns,UNO_QUERY);
     295           0 :         Reference<XAppend> xColumnAppend(xColumns,UNO_QUERY);
     296           0 :         if ( xColumnFactory.is() )
     297             :         {
     298           0 :             OConnectionLineDataVec::iterator aIter = m_vConnLineData.begin();
     299           0 :             OConnectionLineDataVec::iterator aEnd = m_vConnLineData.end();
     300           0 :             for(;aIter != aEnd;++aIter)
     301             :             {
     302           0 :                 if(!((*aIter)->GetSourceFieldName().isEmpty() || (*aIter)->GetDestFieldName().isEmpty()))
     303             :                 {
     304           0 :                     Reference<XPropertySet> xColumn;
     305           0 :                     xColumn = xColumnFactory->createDataDescriptor();
     306           0 :                     if ( xColumn.is() )
     307             :                     {
     308           0 :                         xColumn->setPropertyValue(PROPERTY_NAME,makeAny((*aIter)->GetSourceFieldName()));
     309           0 :                         xColumn->setPropertyValue(PROPERTY_RELATEDCOLUMN,makeAny((*aIter)->GetDestFieldName()));
     310           0 :                         xColumnAppend->appendByDescriptor(xColumn);
     311           0 :                     }
     312             :                 }
     313             :             }
     314             : 
     315           0 :             if ( xColumns->hasElements() )
     316           0 :                 xAppend->appendByDescriptor(xKey);
     317           0 :         }
     318             :         // to get the key we have to reget it because after append it is no longer valid
     319             :     }
     320             : 
     321             :     // get the name of foreign key; search for columns
     322           0 :     m_aConnName = OUString();
     323           0 : xKey.clear();
     324           0 :     bool bDropRelation = false;
     325           0 :     for(sal_Int32 i=0;i<xKeys->getCount();++i)
     326             :     {
     327           0 :         xKeys->getByIndex(i) >>= xKey;
     328             :         OSL_ENSURE(xKey.is(),"Key is not valid!");
     329           0 :         if ( xKey.is() )
     330             :         {
     331           0 :             sal_Int32 nType = 0;
     332           0 :             xKey->getPropertyValue(PROPERTY_TYPE) >>= nType;
     333           0 :             OUString sReferencedTable;
     334           0 :             xKey->getPropertyValue(PROPERTY_REFERENCEDTABLE) >>= sReferencedTable;
     335           0 :             if ( sReferencedTable == getReferencedTable()->GetTableName() )
     336             :             {
     337           0 :                 xColSup.set(xKey,UNO_QUERY_THROW);
     338             :                 try
     339             :                 {
     340           0 :                     Reference<XNameAccess> xColumns = xColSup->getColumns();
     341           0 :                     Sequence< OUString> aNames = xColumns->getElementNames();
     342           0 :                     const OUString* pIter = aNames.getConstArray();
     343           0 :                     const OUString* pEnd = pIter + aNames.getLength();
     344             : 
     345           0 :                     Reference<XPropertySet> xColumn;
     346           0 :                     OUString sName,sRelatedColumn;
     347           0 :                     for ( ; pIter != pEnd ; ++pIter )
     348             :                     {
     349           0 :                         xColumn.set(xColumns->getByName(*pIter),UNO_QUERY_THROW);
     350           0 :                         xColumn->getPropertyValue(PROPERTY_NAME)            >>= sName;
     351           0 :                         xColumn->getPropertyValue(PROPERTY_RELATEDCOLUMN)   >>= sRelatedColumn;
     352             : 
     353           0 :                         OConnectionLineDataVec::iterator aIter = m_vConnLineData.begin();
     354           0 :                         OConnectionLineDataVec::iterator aEnd = m_vConnLineData.end();
     355           0 :                         for(;aIter != aEnd;++aIter)
     356             :                         {
     357           0 :                             if(    (*aIter)->GetSourceFieldName() == sName
     358           0 :                                 && (*aIter)->GetDestFieldName() == sRelatedColumn )
     359             :                             {
     360           0 :                                 break;
     361             :                             }
     362             :                         }
     363           0 :                         if ( aIter == m_vConnLineData.end() )
     364           0 :                             break;
     365             :                     }
     366           0 :                     if ( pIter == pEnd )
     367             :                     {
     368           0 :                         xKey->getPropertyValue(PROPERTY_NAME) >>= sName;
     369           0 :                         m_aConnName = sName;
     370           0 :                         bDropRelation = aNames.getLength() == 0; // the key contains no column, so it isn't valid and we have to drop it
     371             :                         //here we already know our column structure so we don't have to recreate the table connection data
     372           0 :                         xColSup.clear();
     373           0 :                         break;
     374           0 :                     }
     375             :                 }
     376           0 :                 catch(Exception&)
     377             :                 {
     378             :                 }
     379           0 :             }
     380             :         }
     381           0 :     xKey.clear();
     382             :     }
     383           0 :     if ( bDropRelation )
     384             :     {
     385           0 :         DropRelation();
     386           0 :         OUString sError(ModuleRes(STR_QUERY_REL_COULD_NOT_CREATE));
     387           0 :         ::dbtools::throwGenericSQLException(sError,NULL);
     388             :     }
     389             : 
     390             :     // The fields the relation marks may not be the same as our LineDatas mark after the relation has been updated
     391           0 :     if ( xColSup.is() )
     392             :     {
     393           0 :         OConnectionLineDataVec().swap(m_vConnLineData);
     394           0 :         Reference<XNameAccess> xColumns = xColSup->getColumns();
     395           0 :         Sequence< OUString> aNames = xColumns->getElementNames();
     396           0 :         const OUString* pIter = aNames.getConstArray();
     397           0 :         const OUString* pEnd = pIter + aNames.getLength();
     398             : 
     399           0 :         m_vConnLineData.reserve( aNames.getLength() );
     400           0 :         Reference<XPropertySet> xColumn;
     401           0 :         OUString sName,sRelatedColumn;
     402             : 
     403           0 :         for(;pIter != pEnd;++pIter)
     404             :         {
     405           0 :             xColumns->getByName(*pIter) >>= xColumn;
     406           0 :             if ( xColumn.is() )
     407             :             {
     408           0 :                 OConnectionLineDataRef pNewData = CreateLineDataObj();
     409             : 
     410           0 :                 xColumn->getPropertyValue(PROPERTY_NAME)            >>= sName;
     411           0 :                 xColumn->getPropertyValue(PROPERTY_RELATEDCOLUMN)   >>= sRelatedColumn;
     412             : 
     413           0 :                 pNewData->SetSourceFieldName(sName);
     414           0 :                 pNewData->SetDestFieldName(sRelatedColumn);
     415           0 :                 m_vConnLineData.push_back(pNewData);
     416             :             }
     417           0 :         }
     418             :     }
     419             :     // NOTE : the caller is responsible for updating any other objects referencing the old LineDatas (for instance a ConnLine)
     420             : 
     421             :     // determine cardinality
     422           0 :     SetCardinality();
     423             : 
     424           0 :     return sal_True;
     425             : }
     426             : 
     427             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10