LCOV - code coverage report
Current view: top level - svx/source/form - dataaccessdescriptor.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 143 167 85.6 %
Date: 2015-06-13 12:38:46 Functions: 24 27 88.9 %
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 <svx/dataaccessdescriptor.hxx>
      21             : #include <comphelper/propertysetinfo.hxx>
      22             : #include <comphelper/genericpropertyset.hxx>
      23             : #include <osl/diagnose.h>
      24             : #include <com/sun/star/sdbc/XConnection.hpp>
      25             : #include <com/sun/star/ucb/XContent.hpp>
      26             : #include <com/sun/star/beans/PropertyAttribute.hpp>
      27             : #include <tools/urlobj.hxx>
      28             : 
      29             : 
      30             : namespace svx
      31             : {
      32             : 
      33             : 
      34             :     using namespace ::com::sun::star::uno;
      35             :     using namespace ::com::sun::star::sdbc;
      36             :     using namespace ::com::sun::star::beans;
      37             :     using namespace ::com::sun::star::ucb;
      38             :     using namespace ::comphelper;
      39             : 
      40         144 :     class ODADescriptorImpl
      41             :     {
      42             :     protected:
      43             :         bool                    m_bSetOutOfDate         : 1;
      44             :         bool                    m_bSequenceOutOfDate    : 1;
      45             : 
      46             :     public:
      47             :         typedef ::std::map< DataAccessDescriptorProperty, Any >     DescriptorValues;
      48             :         DescriptorValues            m_aValues;
      49             :         Sequence< PropertyValue >   m_aAsSequence;
      50             :         Reference< XPropertySet >   m_xAsSet;
      51             : 
      52             :         typedef ::std::map< OUString, PropertyMapEntry const * >    MapString2PropertyEntry;
      53             : 
      54             :     public:
      55             :         ODADescriptorImpl();
      56             :         ODADescriptorImpl(const ODADescriptorImpl& _rSource);
      57             : 
      58             :         void invalidateExternRepresentations();
      59             : 
      60             :         void updateSequence();
      61             : 
      62             :         /** builds the descriptor from a property value sequence
      63             :             @return <TRUE/>
      64             :                 if and only if the sequence contained valid properties only
      65             :         */
      66             :         bool buildFrom( const Sequence< PropertyValue >& _rValues );
      67             : 
      68             :         /** builds the descriptor from a property set
      69             :             @return <TRUE/>
      70             :                 if and only if the set contained valid properties only
      71             :         */
      72             :         bool buildFrom( const Reference< XPropertySet >& _rValues );
      73             : 
      74             :     protected:
      75             :         static PropertyValue                    buildPropertyValue( const DescriptorValues::const_iterator& _rPos );
      76             :         static const MapString2PropertyEntry&   getPropertyMap( );
      77             :         static PropertyMapEntry const *         getPropertyMapEntry( const DescriptorValues::const_iterator& _rPos );
      78             :     };
      79             : 
      80             : 
      81         144 :     ODADescriptorImpl::ODADescriptorImpl()
      82             :         :m_bSetOutOfDate(true)
      83         144 :         ,m_bSequenceOutOfDate(true)
      84             :     {
      85         144 :     }
      86             : 
      87             : 
      88           2 :     ODADescriptorImpl::ODADescriptorImpl(const ODADescriptorImpl& _rSource)
      89             :         :m_bSetOutOfDate( _rSource.m_bSetOutOfDate )
      90             :         ,m_bSequenceOutOfDate( _rSource.m_bSequenceOutOfDate )
      91           2 :         ,m_aValues( _rSource.m_aValues )
      92             :     {
      93           2 :         if (!m_bSetOutOfDate)
      94           0 :             m_xAsSet = _rSource.m_xAsSet;
      95           2 :         if (!m_bSequenceOutOfDate)
      96           2 :             m_aAsSequence = _rSource.m_aAsSequence;
      97           2 :     }
      98             : 
      99             : 
     100           4 :     bool ODADescriptorImpl::buildFrom( const Sequence< PropertyValue >& _rValues )
     101             :     {
     102           4 :         const MapString2PropertyEntry& rProperties = getPropertyMap();
     103             : 
     104           4 :         bool bValidPropsOnly = true;
     105             : 
     106             :         // loop through the sequence, and fill our m_aValues
     107           4 :         const PropertyValue* pValues = _rValues.getConstArray();
     108           4 :         const PropertyValue* pValuesEnd = pValues + _rValues.getLength();
     109          65 :         for (;pValues != pValuesEnd; ++pValues)
     110             :         {
     111          61 :             MapString2PropertyEntry::const_iterator aPropPos = rProperties.find( pValues->Name );
     112          61 :             if ( aPropPos != rProperties.end() )
     113             :             {
     114          15 :                 DataAccessDescriptorProperty eProperty = (DataAccessDescriptorProperty)aPropPos->second->mnHandle;
     115          15 :                 m_aValues[eProperty] = pValues->Value;
     116             :             }
     117             :             else
     118             :                 // unknown property
     119          46 :                 bValidPropsOnly = false;
     120             :         }
     121             : 
     122           4 :         if (bValidPropsOnly)
     123             :         {
     124           3 :             m_aAsSequence = _rValues;
     125           3 :             m_bSequenceOutOfDate = false;
     126             :         }
     127             :         else
     128           1 :             m_bSequenceOutOfDate = true;
     129             : 
     130           4 :         return bValidPropsOnly;
     131             :     }
     132             : 
     133             : 
     134           1 :     bool ODADescriptorImpl::buildFrom( const Reference< XPropertySet >& _rxValues )
     135             :     {
     136           1 :         Reference< XPropertySetInfo > xPropInfo;
     137           1 :         if (_rxValues.is())
     138           1 :             xPropInfo = _rxValues->getPropertySetInfo();
     139           1 :         if (!xPropInfo.is())
     140             :         {
     141             :             OSL_FAIL("ODADescriptorImpl::buildFrom: invalid property set!");
     142           0 :             return false;
     143             :         }
     144             : 
     145             :         // build a PropertyValue sequence with the current values
     146           2 :         Sequence< Property > aProperties = xPropInfo->getProperties();
     147           1 :         const Property* pProperty = aProperties.getConstArray();
     148           1 :         const Property* pPropertyEnd = pProperty + aProperties.getLength();
     149             : 
     150           2 :         Sequence< PropertyValue > aValues(aProperties.getLength());
     151           1 :         PropertyValue* pValues = aValues.getArray();
     152             : 
     153          53 :         for (;pProperty != pPropertyEnd; ++pProperty, ++pValues)
     154             :         {
     155          52 :             pValues->Name = pProperty->Name;
     156          52 :             pValues->Value = _rxValues->getPropertyValue(pProperty->Name);
     157             :         }
     158             : 
     159           1 :         bool bValidPropsOnly = buildFrom(aValues);
     160           1 :         if (bValidPropsOnly)
     161             :         {
     162           0 :             m_xAsSet = _rxValues;
     163           0 :             m_bSetOutOfDate = false;
     164             :         }
     165             :         else
     166           1 :             m_bSetOutOfDate = true;
     167             : 
     168           2 :         return bValidPropsOnly;
     169             :     }
     170             : 
     171             : 
     172         140 :     void ODADescriptorImpl::invalidateExternRepresentations()
     173             :     {
     174         140 :         m_bSetOutOfDate = true;
     175         140 :         m_bSequenceOutOfDate = true;
     176         140 :     }
     177             : 
     178             : 
     179          10 :     const ODADescriptorImpl::MapString2PropertyEntry& ODADescriptorImpl::getPropertyMap( )
     180             :     {
     181             :         // the properties we know
     182          10 :         static MapString2PropertyEntry s_aProperties;
     183          10 :         if ( s_aProperties.empty() )
     184             :         {
     185             :             static PropertyMapEntry const s_aDesriptorProperties[] =
     186             :             {
     187           1 :                 { OUString("ActiveConnection"),   daConnection,           cppu::UnoType<XConnection>::get(),   PropertyAttribute::TRANSIENT, 0 },
     188           1 :                 { OUString("BookmarkSelection"),  daBookmarkSelection,    cppu::UnoType<bool>::get(),                                           PropertyAttribute::TRANSIENT, 0 },
     189           1 :                 { OUString("Column"),             daColumnObject,         cppu::UnoType<XPropertySet>::get(),  PropertyAttribute::TRANSIENT, 0 },
     190           1 :                 { OUString("ColumnName"),         daColumnName,           ::cppu::UnoType<OUString>::get(),            PropertyAttribute::TRANSIENT, 0 },
     191           1 :                 { OUString("Command"),            daCommand,              ::cppu::UnoType<OUString>::get(),            PropertyAttribute::TRANSIENT, 0 },
     192           1 :                 { OUString("CommandType"),        daCommandType,          ::cppu::UnoType<sal_Int32>::get(),                  PropertyAttribute::TRANSIENT, 0 },
     193           1 :                 { OUString("Component"),          daComponent,            cppu::UnoType<XContent>::get(),      PropertyAttribute::TRANSIENT, 0 },
     194           1 :                 { OUString("ConnectionResource"), daConnectionResource,   ::cppu::UnoType<OUString>::get(),            PropertyAttribute::TRANSIENT, 0 },
     195           1 :                 { OUString("Cursor"),             daCursor,               cppu::UnoType<XResultSet>::get(),     PropertyAttribute::TRANSIENT, 0 },
     196           1 :                 { OUString("DataSourceName"),     daDataSource,           ::cppu::UnoType<OUString>::get(),            PropertyAttribute::TRANSIENT, 0 },
     197           1 :                 { OUString("DatabaseLocation"),   daDatabaseLocation,     ::cppu::UnoType<OUString>::get(),            PropertyAttribute::TRANSIENT, 0 },
     198           1 :                 { OUString("EscapeProcessing"),   daEscapeProcessing,     cppu::UnoType<bool>::get(),                                           PropertyAttribute::TRANSIENT, 0 },
     199           1 :                 { OUString("Filter"),             daFilter,               ::cppu::UnoType<OUString>::get(),            PropertyAttribute::TRANSIENT, 0 },
     200           1 :                 { OUString("Selection"),          daSelection,            cppu::UnoType<Sequence< Any >>::get(),            PropertyAttribute::TRANSIENT, 0 },
     201             :                 { OUString(), 0, css::uno::Type(), 0, 0 }
     202          16 :             };
     203             : 
     204           1 :             PropertyMapEntry const * pEntry = s_aDesriptorProperties;
     205          16 :             while ( !pEntry->maName.isEmpty() )
     206             :             {
     207          14 :                 s_aProperties[ pEntry->maName ] = pEntry;
     208          14 :                 ++pEntry;
     209             :             }
     210             :         }
     211             : 
     212          10 :         return s_aProperties;
     213             :     }
     214             : 
     215             : 
     216           6 :     PropertyMapEntry const * ODADescriptorImpl::getPropertyMapEntry( const DescriptorValues::const_iterator& _rPos )
     217             :     {
     218           6 :         const MapString2PropertyEntry& rProperties = getPropertyMap();
     219             : 
     220           6 :         sal_Int32 nNeededHandle = (sal_Int32)(_rPos->first);
     221             : 
     222         126 :         for ( MapString2PropertyEntry::const_iterator loop = rProperties.begin();
     223          84 :               loop != rProperties.end();
     224             :               ++loop
     225             :             )
     226             :         {
     227          42 :             if ( nNeededHandle == loop->second->mnHandle )
     228          12 :                 return loop->second;
     229             :         }
     230           0 :         throw RuntimeException();
     231             :     }
     232             : 
     233             : 
     234           6 :     PropertyValue ODADescriptorImpl::buildPropertyValue( const DescriptorValues::const_iterator& _rPos )
     235             :     {
     236             :         // the map entry
     237           6 :         PropertyMapEntry const * pProperty = getPropertyMapEntry( _rPos );
     238             : 
     239             :         // build the property value
     240           6 :         PropertyValue aReturn;
     241           6 :         aReturn.Name    = pProperty->maName;
     242           6 :         aReturn.Handle  = pProperty->mnHandle;
     243           6 :         aReturn.Value   = _rPos->second;
     244           6 :         aReturn.State   = PropertyState_DIRECT_VALUE;
     245             : 
     246             :         // outta here
     247           6 :         return aReturn;
     248             :     }
     249             : 
     250             : 
     251           2 :     void ODADescriptorImpl::updateSequence()
     252             :     {
     253           2 :         if (!m_bSequenceOutOfDate)
     254           2 :             return;
     255             : 
     256           2 :         m_aAsSequence.realloc(m_aValues.size());
     257           2 :         PropertyValue* pValue = m_aAsSequence.getArray();
     258             : 
     259             :         // loop through all our values
     260          24 :         for (   DescriptorValues::const_iterator aLoop = m_aValues.begin();
     261          16 :                 aLoop != m_aValues.end();
     262             :                 ++aLoop, ++pValue
     263             :             )
     264             :         {
     265           6 :             *pValue = buildPropertyValue(aLoop);
     266             :         }
     267             : 
     268             :         // don't need to rebuild next time
     269           2 :         m_bSequenceOutOfDate = false;
     270             :     }
     271             : 
     272         141 :     ODataAccessDescriptor::ODataAccessDescriptor()
     273         141 :         :m_pImpl(new ODADescriptorImpl)
     274             :     {
     275         141 :     }
     276             : 
     277             : 
     278           0 :     ODataAccessDescriptor::ODataAccessDescriptor( const ODataAccessDescriptor& _rSource )
     279           0 :         :m_pImpl(new ODADescriptorImpl(*_rSource.m_pImpl))
     280             :     {
     281           0 :     }
     282             : 
     283             : 
     284           2 :     const ODataAccessDescriptor& ODataAccessDescriptor::operator=(const ODataAccessDescriptor& _rSource)
     285             :     {
     286           2 :         delete m_pImpl;
     287           2 :         m_pImpl = new ODADescriptorImpl(*_rSource.m_pImpl);
     288           2 :         return *this;
     289             :     }
     290             : 
     291             : 
     292           1 :     ODataAccessDescriptor::ODataAccessDescriptor( const Reference< XPropertySet >& _rValues )
     293           1 :         :m_pImpl(new ODADescriptorImpl)
     294             :     {
     295           1 :         m_pImpl->buildFrom(_rValues);
     296           1 :     }
     297             : 
     298             : 
     299           0 :     ODataAccessDescriptor::ODataAccessDescriptor( const Any& _rValues )
     300           0 :         :m_pImpl(new ODADescriptorImpl)
     301             :     {
     302             :         // check if we know the format in the Any
     303           0 :         Sequence< PropertyValue > aValues;
     304           0 :         Reference< XPropertySet > xValues;
     305           0 :         if ( _rValues >>= aValues )
     306           0 :             m_pImpl->buildFrom( aValues );
     307           0 :         else if ( _rValues >>= xValues )
     308           0 :             m_pImpl->buildFrom( xValues );
     309           0 :     }
     310             : 
     311             : 
     312           2 :     ODataAccessDescriptor::ODataAccessDescriptor( const Sequence< PropertyValue >& _rValues )
     313           2 :         :m_pImpl(new ODADescriptorImpl)
     314             :     {
     315           2 :         m_pImpl->buildFrom(_rValues);
     316           2 :     }
     317             : 
     318             : 
     319         142 :     ODataAccessDescriptor::~ODataAccessDescriptor()
     320             :     {
     321         142 :         delete m_pImpl;
     322         142 :     }
     323             : 
     324             : 
     325           1 :     void ODataAccessDescriptor::clear()
     326             :     {
     327           1 :         m_pImpl->m_aValues.clear();
     328           1 :     }
     329             : 
     330             : 
     331           0 :     void ODataAccessDescriptor::erase(DataAccessDescriptorProperty _eWhich)
     332             :     {
     333             :         OSL_ENSURE(has(_eWhich), "ODataAccessDescriptor::erase: invalid call!");
     334           0 :         if (has(_eWhich))
     335           0 :             m_pImpl->m_aValues.erase(_eWhich);
     336           0 :     }
     337             : 
     338             : 
     339         258 :     bool ODataAccessDescriptor::has(DataAccessDescriptorProperty _eWhich) const
     340             :     {
     341         258 :         return m_pImpl->m_aValues.find(_eWhich) != m_pImpl->m_aValues.end();
     342             :     }
     343             : 
     344             : 
     345         129 :     const Any& ODataAccessDescriptor::operator [] ( DataAccessDescriptorProperty _eWhich ) const
     346             :     {
     347         129 :         if (!has(_eWhich))
     348             :         {
     349             :             OSL_FAIL("ODataAccessDescriptor::operator[]: invalid acessor!");
     350           0 :             static const Any aDummy;
     351           0 :             return aDummy;
     352             :         }
     353             : 
     354         129 :         return m_pImpl->m_aValues[_eWhich];
     355             :     }
     356             : 
     357             : 
     358         140 :     Any& ODataAccessDescriptor::operator[] ( DataAccessDescriptorProperty _eWhich )
     359             :     {
     360         140 :         m_pImpl->invalidateExternRepresentations();
     361         140 :         return m_pImpl->m_aValues[_eWhich];
     362             :     }
     363             : 
     364             : 
     365           1 :     void ODataAccessDescriptor::initializeFrom(const Sequence< PropertyValue >& _rValues, bool _bClear)
     366             :     {
     367           1 :         if (_bClear)
     368           1 :             clear();
     369           1 :         m_pImpl->buildFrom(_rValues);
     370           1 :     }
     371             : 
     372             : 
     373           2 :     Sequence< PropertyValue > ODataAccessDescriptor::createPropertyValueSequence()
     374             :     {
     375           2 :         m_pImpl->updateSequence();
     376           2 :         return m_pImpl->m_aAsSequence;
     377             :     }
     378             : 
     379             : 
     380          84 :     OUString ODataAccessDescriptor::getDataSource() const
     381             :     {
     382          84 :         OUString sDataSourceName;
     383          84 :         if ( has(daDataSource) )
     384          83 :             (*this)[daDataSource] >>= sDataSourceName;
     385           1 :         else if ( has(daDatabaseLocation) )
     386           0 :             (*this)[daDatabaseLocation] >>= sDataSourceName;
     387          84 :         return sDataSourceName;
     388             :     }
     389             : 
     390          15 :     void ODataAccessDescriptor::setDataSource(const OUString& _sDataSourceNameOrLocation)
     391             :     {
     392          15 :         if ( !_sDataSourceNameOrLocation.isEmpty() )
     393             :         {
     394           8 :             INetURLObject aURL(_sDataSourceNameOrLocation);
     395           8 :             (*this)[ (( aURL.GetProtocol() == INetProtocol::File ) ? daDatabaseLocation : daDataSource)] <<= _sDataSourceNameOrLocation;
     396             :         }
     397             :         else
     398           7 :             (*this)[ daDataSource ] <<= OUString();
     399          15 :     }
     400             : 
     401             : 
     402             : }
     403             : 
     404             : 
     405             : 
     406             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11