LCOV - code coverage report
Current view: top level - libreoffice/dbaccess/source/ui/misc - indexcollection.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 166 0.0 %
Date: 2012-12-27 Functions: 0 17 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             : 
      21             : #include "indexcollection.hxx"
      22             : #include <tools/diagnose_ex.h>
      23             : #include <com/sun/star/sdbcx/XAppend.hpp>
      24             : #include <com/sun/star/beans/XPropertySet.hpp>
      25             : #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
      26             : #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
      27             : #include <comphelper/extract.hxx>
      28             : #include <com/sun/star/sdbcx/XDrop.hpp>
      29             : #include <com/sun/star/container/XNameContainer.hpp>
      30             : 
      31             : //......................................................................
      32             : namespace dbaui
      33             : {
      34             : //......................................................................
      35             : 
      36             :     using namespace ::com::sun::star::uno;
      37             :     using namespace ::com::sun::star::container;
      38             :     using namespace ::com::sun::star::beans;
      39             :     using namespace ::com::sun::star::sdbcx;
      40             :     using namespace ::com::sun::star::sdbc;
      41             : 
      42             :     //==================================================================
      43             :     //= OIndexCollection
      44             :     //==================================================================
      45             :     //------------------------------------------------------------------
      46           0 :     OIndexCollection::OIndexCollection()
      47             :     {
      48           0 :     }
      49             : 
      50             :     //------------------------------------------------------------------
      51           0 :     OIndexCollection::OIndexCollection(const OIndexCollection& _rSource)
      52             :     {
      53           0 :         *this = _rSource;
      54           0 :     }
      55             : 
      56             :     //------------------------------------------------------------------
      57           0 :     const OIndexCollection& OIndexCollection::operator=(const OIndexCollection& _rSource)
      58             :     {
      59           0 :         detach();
      60           0 :         m_xIndexes = _rSource.m_xIndexes;
      61           0 :         m_aIndexes = _rSource.m_aIndexes;
      62           0 :         return *this;
      63             :     }
      64             : 
      65             :     //------------------------------------------------------------------
      66           0 :     void OIndexCollection::attach(const Reference< XNameAccess >& _rxIndexes)
      67             :     {
      68           0 :         implConstructFrom(_rxIndexes);
      69           0 :     }
      70             : 
      71             :     //------------------------------------------------------------------
      72           0 :     void OIndexCollection::detach()
      73             :     {
      74           0 :         m_xIndexes.clear();
      75           0 :         m_aIndexes.clear();
      76           0 :     }
      77             : 
      78             :     //------------------------------------------------------------------
      79           0 :     Indexes::const_iterator OIndexCollection::find(const String& _rName) const
      80             :     {
      81           0 :         ::rtl::OUString sNameCompare(_rName);
      82             : 
      83             :         // loop'n'compare
      84           0 :         Indexes::const_iterator aSearch = m_aIndexes.begin();
      85           0 :         Indexes::const_iterator aEnd = m_aIndexes.end();
      86           0 :         for (; aSearch != aEnd; ++aSearch)
      87           0 :             if (aSearch->sName == sNameCompare)
      88           0 :                 break;
      89             : 
      90           0 :         return aSearch;
      91             :     }
      92             : 
      93             :     //------------------------------------------------------------------
      94           0 :     Indexes::iterator OIndexCollection::find(const String& _rName)
      95             :     {
      96           0 :         ::rtl::OUString sNameCompare(_rName);
      97             : 
      98             :         // loop'n'compare
      99           0 :         Indexes::iterator aSearch = m_aIndexes.begin();
     100           0 :         Indexes::iterator aEnd = m_aIndexes.end();
     101           0 :         for (; aSearch != aEnd; ++aSearch)
     102           0 :             if (aSearch->sName == sNameCompare)
     103           0 :                 break;
     104             : 
     105           0 :         return aSearch;
     106             :     }
     107             : 
     108             :     //------------------------------------------------------------------
     109           0 :     Indexes::const_iterator OIndexCollection::findOriginal(const String& _rName) const
     110             :     {
     111           0 :         ::rtl::OUString sNameCompare(_rName);
     112             : 
     113             :         // loop'n'compare
     114           0 :         Indexes::const_iterator aSearch = m_aIndexes.begin();
     115           0 :         Indexes::const_iterator aEnd = m_aIndexes.end();
     116           0 :         for (; aSearch != aEnd; ++aSearch)
     117           0 :             if (aSearch->getOriginalName() == sNameCompare)
     118           0 :                 break;
     119             : 
     120           0 :         return aSearch;
     121             :     }
     122             : 
     123             :     //------------------------------------------------------------------
     124           0 :     Indexes::iterator OIndexCollection::findOriginal(const String& _rName)
     125             :     {
     126           0 :         ::rtl::OUString sNameCompare(_rName);
     127             : 
     128             :         // loop'n'compare
     129           0 :         Indexes::iterator aSearch = m_aIndexes.begin();
     130           0 :         Indexes::iterator aEnd = m_aIndexes.end();
     131           0 :         for (; aSearch != aEnd; ++aSearch)
     132           0 :             if (aSearch->getOriginalName() == sNameCompare)
     133           0 :                 break;
     134             : 
     135           0 :         return aSearch;
     136             :     }
     137             : 
     138             :     //------------------------------------------------------------------
     139           0 :     void OIndexCollection::commitNewIndex(const Indexes::iterator& _rPos) SAL_THROW((SQLException))
     140             :     {
     141             :         OSL_ENSURE(_rPos->isNew(), "OIndexCollection::commitNewIndex: index must be new!");
     142             : 
     143             :         try
     144             :         {
     145           0 :             Reference< XDataDescriptorFactory > xIndexFactory(m_xIndexes, UNO_QUERY);
     146           0 :             Reference< XAppend > xAppendIndex(xIndexFactory, UNO_QUERY);
     147           0 :             if (!xAppendIndex.is())
     148             :             {
     149             :                 OSL_FAIL("OIndexCollection::commitNewIndex: missing an interface of the index container!");
     150             :                 return;
     151             :             }
     152             : 
     153           0 :             Reference< XPropertySet > xIndexDescriptor = xIndexFactory->createDataDescriptor();
     154           0 :             Reference< XColumnsSupplier > xColsSupp(xIndexDescriptor, UNO_QUERY);
     155           0 :             Reference< XNameAccess > xCols;
     156           0 :             if (xColsSupp.is())
     157           0 :                 xCols = xColsSupp->getColumns();
     158             : 
     159           0 :             Reference< XDataDescriptorFactory > xColumnFactory(xCols, UNO_QUERY);
     160           0 :             Reference< XAppend > xAppendCols(xColumnFactory, UNO_QUERY);
     161           0 :             if (!xAppendCols.is())
     162             :             {
     163             :                 OSL_FAIL("OIndexCollection::commitNewIndex: invalid index descriptor returned!");
     164             :                 return;
     165             :             }
     166             : 
     167             :             // set the properties
     168           0 :             static const ::rtl::OUString s_sUniquePropertyName = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("IsUnique"));
     169           0 :             static const ::rtl::OUString s_sSortPropertyName = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("IsAscending"));
     170           0 :             static const ::rtl::OUString s_sNamePropertyName = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Name"));
     171             :             // the index' own props
     172           0 :             xIndexDescriptor->setPropertyValue(s_sUniquePropertyName, ::cppu::bool2any(_rPos->bUnique));
     173           0 :             xIndexDescriptor->setPropertyValue(s_sNamePropertyName, makeAny(_rPos->sName));
     174             : 
     175             :             // the fields
     176           0 :             for (   ConstIndexFieldsIterator aFieldLoop = _rPos->aFields.begin();
     177           0 :                     aFieldLoop != _rPos->aFields.end();
     178             :                     ++aFieldLoop
     179             :                 )
     180             :             {
     181             :                 OSL_ENSURE(!xCols->hasByName(aFieldLoop->sFieldName), "OIndexCollection::commitNewIndex: double column name (need to prevent this outside)!");
     182             : 
     183           0 :                 Reference< XPropertySet > xColDescriptor = xColumnFactory->createDataDescriptor();
     184             :                 OSL_ENSURE(xColDescriptor.is(), "OIndexCollection::commitNewIndex: invalid column descriptor!");
     185           0 :                 if (xColDescriptor.is())
     186             :                 {
     187           0 :                     xColDescriptor->setPropertyValue(s_sSortPropertyName, ::cppu::bool2any(aFieldLoop->bSortAscending));
     188           0 :                     xColDescriptor->setPropertyValue(s_sNamePropertyName, makeAny(::rtl::OUString(aFieldLoop->sFieldName)));
     189           0 :                     xAppendCols->appendByDescriptor(xColDescriptor);
     190             :                 }
     191           0 :             }
     192             : 
     193           0 :             xAppendIndex->appendByDescriptor(xIndexDescriptor);
     194             : 
     195           0 :             _rPos->flagAsCommitted(GrantIndexAccess());
     196           0 :             _rPos->clearModified();
     197             :         }
     198           0 :         catch(SQLException&)
     199             :         {   // allowed to pass
     200           0 :             throw;
     201             :         }
     202           0 :         catch( const Exception& )
     203             :         {
     204             :             DBG_UNHANDLED_EXCEPTION();
     205             :         }
     206             :     }
     207             : 
     208             :     //------------------------------------------------------------------
     209           0 :     sal_Bool OIndexCollection::dropNoRemove(const Indexes::iterator& _rPos) SAL_THROW((SQLException))
     210             :     {
     211             :         try
     212             :         {
     213             :             OSL_ENSURE(m_xIndexes->hasByName(_rPos->getOriginalName()), "OIndexCollection::drop: invalid name!");
     214             : 
     215           0 :             Reference< XDrop > xDropIndex(m_xIndexes, UNO_QUERY);
     216           0 :             if (!xDropIndex.is())
     217             :             {
     218             :                 OSL_FAIL("OIndexCollection::drop: no XDrop interface!");
     219           0 :                 return sal_False;
     220             :             }
     221             : 
     222           0 :             xDropIndex->dropByName(_rPos->getOriginalName());
     223             :         }
     224           0 :         catch(SQLException&)
     225             :         {   // allowed to pass
     226           0 :             throw;
     227             :         }
     228           0 :         catch( const Exception& )
     229             :         {
     230             :             DBG_UNHANDLED_EXCEPTION();
     231           0 :             return sal_False;
     232             :         }
     233             : 
     234             :         // adjust the OIndex structure
     235           0 :         Indexes::iterator aDropped = findOriginal(_rPos->getOriginalName());
     236             :         OSL_ENSURE(aDropped != m_aIndexes.end(), "OIndexCollection::drop: invalid original name, but successfull commit?!");
     237           0 :         aDropped->flagAsNew(GrantIndexAccess());
     238             : 
     239           0 :         return sal_True;
     240             :     }
     241             : 
     242             :     //------------------------------------------------------------------
     243           0 :     sal_Bool OIndexCollection::drop(const Indexes::iterator& _rPos) SAL_THROW((SQLException))
     244             :     {
     245             :         OSL_ENSURE((_rPos >= m_aIndexes.begin()) && (_rPos < m_aIndexes.end()),
     246             :             "OIndexCollection::drop: invalid position (fasten your seatbelt .... this will crash)!");
     247             : 
     248           0 :         if (!_rPos->isNew())
     249           0 :             if (!dropNoRemove(_rPos))
     250           0 :                 return sal_False;
     251             : 
     252             :         // adjust the index array
     253           0 :         m_aIndexes.erase(_rPos);
     254           0 :         return sal_True;
     255             :     }
     256             : 
     257             :     //------------------------------------------------------------------
     258           0 :     void OIndexCollection::implFillIndexInfo(OIndex& _rIndex) SAL_THROW((Exception))
     259             :     {
     260             :         // get the UNO descriptor for the index
     261           0 :         Reference< XPropertySet > xIndex;
     262           0 :         m_xIndexes->getByName(_rIndex.getOriginalName()) >>= xIndex;
     263           0 :         if (!xIndex.is())
     264             :         {
     265             :             OSL_FAIL("OIndexCollection::implFillIndexInfo: got an invalid index object!");
     266             :         }
     267             :         else
     268           0 :             implFillIndexInfo(_rIndex, xIndex);
     269           0 :     }
     270             : 
     271             :     //------------------------------------------------------------------
     272           0 :     void OIndexCollection::implFillIndexInfo(OIndex& _rIndex, Reference< XPropertySet > _rxDescriptor) SAL_THROW((Exception))
     273             :     {
     274           0 :         static const ::rtl::OUString s_sPrimaryIndexPropertyName = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("IsPrimaryKeyIndex"));
     275           0 :         static const ::rtl::OUString s_sUniquePropertyName = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("IsUnique"));
     276           0 :         static const ::rtl::OUString s_sSortPropertyName = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("IsAscending"));
     277           0 :         static const ::rtl::OUString s_sCatalogPropertyName = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Catalog"));
     278             : 
     279           0 :         _rIndex.bPrimaryKey = ::cppu::any2bool(_rxDescriptor->getPropertyValue(s_sPrimaryIndexPropertyName));
     280           0 :         _rIndex.bUnique = ::cppu::any2bool(_rxDescriptor->getPropertyValue(s_sUniquePropertyName));
     281           0 :         _rxDescriptor->getPropertyValue(s_sCatalogPropertyName) >>= _rIndex.sDescription;
     282             : 
     283             :         // the columns
     284           0 :         Reference< XColumnsSupplier > xSuppCols(_rxDescriptor, UNO_QUERY);
     285           0 :         Reference< XNameAccess > xCols;
     286           0 :         if (xSuppCols.is())
     287           0 :             xCols = xSuppCols->getColumns();
     288             :         OSL_ENSURE(xCols.is(), "OIndexCollection::implFillIndexInfo: the index does not have columns!");
     289           0 :         if (xCols.is())
     290             :         {
     291           0 :             Sequence< ::rtl::OUString > aFieldNames = xCols->getElementNames();
     292           0 :             _rIndex.aFields.resize(aFieldNames.getLength());
     293             : 
     294           0 :             const ::rtl::OUString* pFieldNames = aFieldNames.getConstArray();
     295           0 :             const ::rtl::OUString* pFieldNamesEnd = pFieldNames + aFieldNames.getLength();
     296           0 :             IndexFields::iterator aCopyTo = _rIndex.aFields.begin();
     297             : 
     298           0 :             Reference< XPropertySet > xIndexColumn;
     299           0 :             for (;pFieldNames < pFieldNamesEnd; ++pFieldNames, ++aCopyTo)
     300             :             {
     301             :                 // extract the column
     302           0 :                 xIndexColumn.clear();
     303           0 :                 xCols->getByName(*pFieldNames) >>= xIndexColumn;
     304           0 :                 if (!xIndexColumn.is())
     305             :                 {
     306             :                     OSL_FAIL("OIndexCollection::implFillIndexInfo: invalid index column!");
     307           0 :                     --aCopyTo;
     308           0 :                     continue;
     309             :                 }
     310             : 
     311             :                 // get the relevant properties
     312           0 :                 aCopyTo->sFieldName = *pFieldNames;
     313           0 :                 aCopyTo->bSortAscending = ::cppu::any2bool(xIndexColumn->getPropertyValue(s_sSortPropertyName));
     314             :             }
     315             : 
     316           0 :             _rIndex.aFields.resize(aCopyTo - _rIndex.aFields.begin());
     317             :                 // (just in case some fields were invalid ...)
     318           0 :         }
     319           0 :     }
     320             : 
     321             :     //------------------------------------------------------------------
     322           0 :     void OIndexCollection::resetIndex(const Indexes::iterator& _rPos) SAL_THROW((SQLException))
     323             :     {
     324             :         OSL_ENSURE(_rPos >= m_aIndexes.begin() && _rPos < m_aIndexes.end(),
     325             :             "OIndexCollection::resetIndex: invalid position!");
     326             : 
     327             :         try
     328             :         {
     329           0 :             _rPos->sName = _rPos->getOriginalName();
     330           0 :             implFillIndexInfo(*_rPos);
     331             : 
     332           0 :             _rPos->clearModified();
     333           0 :             _rPos->flagAsCommitted(GrantIndexAccess());
     334             :         }
     335           0 :         catch(SQLException&)
     336             :         {   // allowed to pass
     337           0 :             throw;
     338             :         }
     339           0 :         catch( const Exception& )
     340             :         {
     341             :             DBG_UNHANDLED_EXCEPTION();
     342             :         }
     343           0 :     }
     344             : 
     345             :     //------------------------------------------------------------------
     346           0 :     Indexes::iterator OIndexCollection::insert(const String& _rName)
     347             :     {
     348             :         OSL_ENSURE(end() == find(_rName), "OIndexCollection::insert: invalid new name!");
     349           0 :         String tmpName;
     350           0 :         OIndex aNewIndex(tmpName);  // the empty string indicates the index is a new one
     351           0 :         aNewIndex.sName = _rName;
     352           0 :         m_aIndexes.push_back(aNewIndex);
     353           0 :         return m_aIndexes.end() - 1;    // the last element is the new one ...
     354             :     }
     355             : 
     356             :     //------------------------------------------------------------------
     357           0 :     void OIndexCollection::implConstructFrom(const Reference< XNameAccess >& _rxIndexes)
     358             :     {
     359           0 :         detach();
     360             : 
     361           0 :         m_xIndexes = _rxIndexes;
     362           0 :         if (m_xIndexes.is())
     363             :         {
     364             :             // loop through all the indexes
     365           0 :             Sequence< ::rtl::OUString > aNames = m_xIndexes->getElementNames();
     366           0 :             const ::rtl::OUString* pNames = aNames.getConstArray();
     367           0 :             const ::rtl::OUString* pEnd = pNames + aNames.getLength();
     368           0 :             for (; pNames < pEnd; ++pNames)
     369             :             {
     370             :                 // extract the index object
     371           0 :                 Reference< XPropertySet > xIndex;
     372           0 :                 m_xIndexes->getByName(*pNames) >>= xIndex;
     373           0 :                 if (!xIndex.is())
     374             :                 {
     375             :                     OSL_FAIL("OIndexCollection::implConstructFrom: got an invalid index object ... ignoring!");
     376           0 :                     continue;
     377             :                 }
     378             : 
     379             :                 // fill the OIndex structure
     380           0 :                 OIndex aCurrentIndex(*pNames);
     381           0 :                 implFillIndexInfo(aCurrentIndex);
     382           0 :                 m_aIndexes.push_back(aCurrentIndex);
     383           0 :             }
     384             :         }
     385           0 :     }
     386             : 
     387             : //......................................................................
     388             : }   // namespace dbaui
     389             : //......................................................................
     390             : 
     391             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10