LCOV - code coverage report
Current view: top level - libreoffice/dbaccess/source/core/api - FilteredContainer.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 188 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             : #include "dbastrings.hrc"
      21             : #include "FilteredContainer.hxx"
      22             : #include "RefreshListener.hxx"
      23             : #include "sdbcoretools.hxx"
      24             : #include <com/sun/star/sdbc/XRow.hpp>
      25             : #include <connectivity/dbtools.hxx>
      26             : #include <tools/wldcrd.hxx>
      27             : #include <tools/diagnose_ex.h>
      28             : #include <rtl/logfile.hxx>
      29             : #include <boost/optional.hpp>
      30             : 
      31             : namespace dbaccess
      32             : {
      33             :     using namespace dbtools;
      34             :     using namespace ::com::sun::star::uno;
      35             :     using namespace ::com::sun::star::lang;
      36             :     using namespace ::com::sun::star::beans;
      37             :     using namespace ::com::sun::star::sdbc;
      38             :     using namespace ::com::sun::star::sdb;
      39             :     using namespace ::com::sun::star::sdbcx;
      40             :     using namespace ::com::sun::star::util;
      41             :     using namespace ::com::sun::star::container;
      42             :     using namespace ::osl;
      43             :     using namespace ::comphelper;
      44             :     using namespace ::cppu;
      45             :     using namespace ::connectivity::sdbcx;
      46             : 
      47             : /** creates a vector of WildCards and reduce the _rTableFilter of the length of WildsCards
      48             : */
      49           0 : sal_Int32 createWildCardVector(Sequence< ::rtl::OUString >& _rTableFilter, ::std::vector< WildCard >& _rOut)
      50             : {
      51             :     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "api", "Ocke.Janssen@sun.com", "OFilteredContainer::createWildCardVector" );
      52             :     // for wildcard search : remove all table filters which are a wildcard expression and build a WilCard
      53             :     // for them
      54           0 :     ::rtl::OUString* pTableFilters = _rTableFilter.getArray();
      55           0 :     ::rtl::OUString* pEnd          = pTableFilters + _rTableFilter.getLength();
      56           0 :     sal_Int32 nShiftPos = 0;
      57           0 :     for (sal_Int32 i=0; pEnd != pTableFilters; ++pTableFilters,++i)
      58             :     {
      59           0 :         if (pTableFilters->indexOf('%') != -1)
      60             :         {
      61           0 :             _rOut.push_back(WildCard(pTableFilters->replace('%', '*')));
      62             :         }
      63             :         else
      64             :         {
      65           0 :             if (nShiftPos != i)
      66             :             {
      67           0 :                 _rTableFilter.getArray()[nShiftPos] = _rTableFilter.getArray()[i];
      68             :             }
      69           0 :             ++nShiftPos;
      70             :         }
      71             :     }
      72             :     // now aTableFilter contains nShiftPos non-wc-strings and aWCSearch all wc-strings
      73           0 :     _rTableFilter.realloc(nShiftPos);
      74           0 :     return nShiftPos;
      75             : }
      76             : 
      77           0 :     bool lcl_isElementAllowed(  const ::rtl::OUString& _rName,
      78             :                                 const Sequence< ::rtl::OUString >& _rTableFilter,
      79             :                                 const ::std::vector< WildCard >& _rWCSearch )
      80             :     {
      81           0 :         sal_Int32 nTableFilterLen = _rTableFilter.getLength();
      82             : 
      83           0 :         const ::rtl::OUString* tableFilter = _rTableFilter.getConstArray();
      84           0 :         const ::rtl::OUString* tableFilterEnd = _rTableFilter.getConstArray() + nTableFilterLen;
      85           0 :         bool bFilterMatch = ::std::find( tableFilter, tableFilterEnd, _rName ) != tableFilterEnd;
      86             :         // the table is allowed to "pass" if we had no filters at all or any of the non-wildcard filters matches
      87           0 :         if (!bFilterMatch && !_rWCSearch.empty())
      88             :         {   // or if one of the wildcrad expression matches
      89           0 :             for (   ::std::vector< WildCard >::const_iterator aLoop = _rWCSearch.begin();
      90           0 :                     aLoop != _rWCSearch.end() && !bFilterMatch;
      91             :                     ++aLoop
      92             :                 )
      93           0 :                 bFilterMatch = aLoop->Matches( _rName );
      94             :         }
      95             : 
      96           0 :         return bFilterMatch;
      97             :     }
      98             : 
      99             :     typedef ::boost::optional< ::rtl::OUString >    OptionalString;
     100           0 :     struct TableInfo
     101             :     {
     102             :         OptionalString  sComposedName;
     103             :         OptionalString  sType;
     104             :         OptionalString  sCatalog;
     105             :         OptionalString  sSchema;
     106             :         OptionalString  sName;
     107             : 
     108           0 :         TableInfo( const ::rtl::OUString& _composedName )
     109           0 :             :sComposedName( _composedName )
     110             :         {
     111           0 :         }
     112             : 
     113           0 :         TableInfo( const ::rtl::OUString& _catalog, const ::rtl::OUString& _schema, const ::rtl::OUString& _name,
     114             :             const ::rtl::OUString& _type )
     115             :             :sComposedName()
     116             :             ,sType( _type )
     117             :             ,sCatalog( _catalog )
     118             :             ,sSchema( _schema )
     119           0 :             ,sName( _name )
     120             :         {
     121           0 :         }
     122             :     };
     123             :     typedef ::std::vector< TableInfo >    TableInfos;
     124             : 
     125           0 :     void lcl_ensureComposedName( TableInfo& _io_tableInfo, const Reference< XDatabaseMetaData >& _metaData )
     126             :     {
     127           0 :         if ( !_metaData.is() )
     128           0 :             throw RuntimeException();
     129             : 
     130           0 :         if ( !_io_tableInfo.sComposedName )
     131             :         {
     132             :             OSL_ENSURE( !!_io_tableInfo.sCatalog && !!_io_tableInfo.sSchema && !!_io_tableInfo.sName, "lcl_ensureComposedName: How should I composed the name from nothing!?" );
     133             : 
     134             :             _io_tableInfo.sComposedName = OptionalString(
     135           0 :                 composeTableName( _metaData, *_io_tableInfo.sCatalog, *_io_tableInfo.sSchema, *_io_tableInfo.sName,
     136             :                 sal_False, ::dbtools::eInDataManipulation )
     137           0 :             );
     138             :         }
     139           0 :     }
     140             : 
     141           0 :     void lcl_ensureType( TableInfo& _io_tableInfo, const Reference< XDatabaseMetaData >& _metaData, const Reference< XNameAccess >& _masterContainer )
     142             :     {
     143           0 :         if ( !!_io_tableInfo.sType )
     144           0 :             return;
     145             : 
     146           0 :         lcl_ensureComposedName( _io_tableInfo, _metaData );
     147             : 
     148           0 :         if ( !_masterContainer.is() )
     149           0 :             throw RuntimeException();
     150             : 
     151           0 :         ::rtl::OUString sTypeName;
     152             :         try
     153             :         {
     154           0 :             Reference< XPropertySet > xTable( _masterContainer->getByName( *_io_tableInfo.sComposedName ), UNO_QUERY_THROW );
     155           0 :             OSL_VERIFY( xTable->getPropertyValue( PROPERTY_TYPE ) >>= sTypeName );
     156             :         }
     157           0 :         catch( const Exception& )
     158             :         {
     159             :             DBG_UNHANDLED_EXCEPTION();
     160             :         }
     161           0 :         _io_tableInfo.sType = OptionalString( sTypeName );
     162             :     }
     163             : 
     164           0 :     connectivity::TStringVector lcl_filter( const TableInfos& _unfilteredTables,
     165             :         const Sequence< ::rtl::OUString >& _tableFilter, const Sequence< ::rtl::OUString >& _tableTypeFilter,
     166             :         const Reference< XDatabaseMetaData >& _metaData, const Reference< XNameAccess >& _masterContainer )
     167             :     {
     168           0 :         TableInfos aFilteredTables;
     169             : 
     170             :         // first, filter for the table names
     171           0 :         sal_Int32 nTableFilterCount = _tableFilter.getLength();
     172           0 :         sal_Bool dontFilterTableNames = ( ( nTableFilterCount == 1 ) && _tableFilter[0].equalsAsciiL( "%", 1 ) );
     173           0 :         if( dontFilterTableNames )
     174             :         {
     175           0 :             aFilteredTables = _unfilteredTables;
     176             :         }
     177             :         else
     178             :         {
     179             :             // for wildcard search : remove all table filters which are a wildcard expression and build a WildCard
     180             :             // for them
     181           0 :             ::std::vector< WildCard > aWildCardTableFilter;
     182           0 :             Sequence< ::rtl::OUString > aNonWildCardTableFilter = _tableFilter;
     183           0 :             nTableFilterCount = createWildCardVector( aNonWildCardTableFilter, aWildCardTableFilter );
     184             : 
     185           0 :             TableInfos aUnfilteredTables( _unfilteredTables );
     186           0 :             aUnfilteredTables.reserve( nTableFilterCount + ( aWildCardTableFilter.size() * 10 ) );
     187             : 
     188           0 :             for (   TableInfos::iterator table = aUnfilteredTables.begin();
     189           0 :                     table != aUnfilteredTables.end();
     190             :                     ++table
     191             :                 )
     192             :             {
     193           0 :                 lcl_ensureComposedName( *table, _metaData );
     194             : 
     195           0 :                 if ( lcl_isElementAllowed( *table->sComposedName, aNonWildCardTableFilter, aWildCardTableFilter ) )
     196           0 :                     aFilteredTables.push_back( *table );
     197           0 :             }
     198             :         }
     199             : 
     200             :         // second, filter for the table types
     201           0 :         sal_Int32 nTableTypeFilterCount = _tableTypeFilter.getLength();
     202           0 :         sal_Bool dontFilterTableTypes = ( ( nTableTypeFilterCount == 1 ) && _tableTypeFilter[0].equalsAsciiL( "%", 1 ) );
     203           0 :         dontFilterTableTypes = dontFilterTableTypes || ( nTableTypeFilterCount == 0 );
     204             :             // (for TableTypeFilter, unlike TableFilter, "empty" means "do not filter at all")
     205           0 :         if ( !dontFilterTableTypes )
     206             :         {
     207           0 :             TableInfos aUnfilteredTables;
     208           0 :             aUnfilteredTables.swap( aFilteredTables );
     209             : 
     210           0 :             const ::rtl::OUString* pTableTypeFilterBegin = _tableTypeFilter.getConstArray();
     211           0 :             const ::rtl::OUString* pTableTypeFilterEnd = pTableTypeFilterBegin + _tableTypeFilter.getLength();
     212             : 
     213           0 :             for (   TableInfos::iterator table = aUnfilteredTables.begin();
     214           0 :                     table != aUnfilteredTables.end();
     215             :                     ++table
     216             :                 )
     217             :             {
     218             :                 // ensure that we know the table type
     219           0 :                 lcl_ensureType( *table, _metaData, _masterContainer );
     220             : 
     221           0 :                 if ( ::std::find( pTableTypeFilterBegin, pTableTypeFilterEnd, *table->sType ) != pTableTypeFilterEnd )
     222           0 :                     aFilteredTables.push_back( *table );
     223           0 :             }
     224             :         }
     225             : 
     226           0 :         connectivity::TStringVector aReturn;
     227           0 :         for (   TableInfos::iterator table = aFilteredTables.begin();
     228           0 :                 table != aFilteredTables.end();
     229             :                 ++table
     230             :             )
     231             :         {
     232           0 :             lcl_ensureComposedName( *table, _metaData );
     233           0 :             aReturn.push_back( *table->sComposedName );
     234             :         }
     235           0 :         return aReturn;
     236             :     }
     237             : 
     238             :     //==========================================================================
     239             :     //= OViewContainer
     240             :     //==========================================================================
     241           0 :     OFilteredContainer::OFilteredContainer(::cppu::OWeakObject& _rParent,
     242             :                                  ::osl::Mutex& _rMutex,
     243             :                                  const Reference< XConnection >& _xCon,
     244             :                                  sal_Bool _bCase,
     245             :                                  IRefreshListener*  _pRefreshListener,
     246             :                                  ::dbtools::IWarningsContainer* _pWarningsContainer
     247             :                                  ,oslInterlockedCount& _nInAppend)
     248             :         :OCollection(_rParent,_bCase,_rMutex,::std::vector< ::rtl::OUString>())
     249             :         ,m_bConstructed(sal_False)
     250             :         ,m_pWarningsContainer(_pWarningsContainer)
     251             :         ,m_pRefreshListener(_pRefreshListener)
     252             :         ,m_nInAppend(_nInAppend)
     253           0 :         ,m_xConnection(_xCon)
     254             :     {
     255           0 :     }
     256             : 
     257           0 :     void OFilteredContainer::construct(const Reference< XNameAccess >& _rxMasterContainer,
     258             :                                     const Sequence< ::rtl::OUString >& _rTableFilter,
     259             :                                     const Sequence< ::rtl::OUString >& _rTableTypeFilter)
     260             :     {
     261             :         try
     262             :         {
     263           0 :             Reference<XConnection> xCon = m_xConnection;
     264           0 :             if ( xCon.is() )
     265           0 :                 m_xMetaData = xCon->getMetaData();
     266             :         }
     267           0 :         catch(SQLException&)
     268             :         {
     269             :             DBG_UNHANDLED_EXCEPTION();
     270             :         }
     271             : 
     272           0 :         m_xMasterContainer = _rxMasterContainer;
     273             : 
     274           0 :         if ( m_xMasterContainer.is() )
     275             :         {
     276           0 :             addMasterContainerListener();
     277             : 
     278           0 :             TableInfos aUnfilteredTables;
     279             : 
     280           0 :             Sequence< ::rtl::OUString > aNames = m_xMasterContainer->getElementNames();
     281           0 :             const ::rtl::OUString*  name = aNames.getConstArray();
     282           0 :             const ::rtl::OUString*  nameEnd = name + aNames.getLength();
     283           0 :             for ( ; name != nameEnd; ++name )
     284           0 :                 aUnfilteredTables.push_back( TableInfo( *name ) );
     285             : 
     286             :             reFill( lcl_filter( aUnfilteredTables,
     287           0 :                 _rTableFilter, _rTableTypeFilter, m_xMetaData, m_xMasterContainer ) );
     288             : 
     289           0 :             m_bConstructed = sal_True;
     290             :         }
     291             :         else
     292             :         {
     293           0 :             construct( _rTableFilter, _rTableTypeFilter );
     294             :         }
     295           0 :     }
     296             : 
     297           0 :     void OFilteredContainer::construct(const Sequence< ::rtl::OUString >& _rTableFilter, const Sequence< ::rtl::OUString >& _rTableTypeFilter)
     298             :     {
     299             :         // build sorted versions of the filter sequences, so the visibility decision is faster
     300           0 :         Sequence< ::rtl::OUString > aTableFilter(_rTableFilter);
     301             : 
     302             :         // for wildcard search : remove all table filters which are a wildcard expression and build a WildCard
     303             :         // for them
     304           0 :         ::std::vector< WildCard > aWCSearch;
     305           0 :         createWildCardVector(aTableFilter,aWCSearch);
     306             : 
     307             :         try
     308             :         {
     309           0 :             Reference< XConnection > xCon( m_xConnection, UNO_SET_THROW );
     310           0 :             m_xMetaData.set( xCon->getMetaData(), UNO_SET_THROW );
     311             : 
     312             :             // create a table table filter suitable for the XDatabaseMetaData::getTables call,
     313             :             // taking into account both the externally-provided table type filter, and any
     314             :             // table type restriction which is inherent to the container
     315           0 :             Sequence< ::rtl::OUString > aTableTypeFilter;
     316           0 :             ::rtl::OUString sInherentTableTypeRestriction( getTableTypeRestriction() );
     317           0 :             if ( !sInherentTableTypeRestriction.isEmpty() )
     318             :             {
     319           0 :                 if ( _rTableTypeFilter.getLength() != 0 )
     320             :                 {
     321           0 :                     const ::rtl::OUString* tableType    = _rTableTypeFilter.getConstArray();
     322           0 :                     const ::rtl::OUString* tableTypeEnd = tableType + _rTableTypeFilter.getLength();
     323           0 :                     for ( ; tableType != tableTypeEnd; ++tableType )
     324             :                     {
     325           0 :                         if ( *tableType == sInherentTableTypeRestriction )
     326           0 :                             break;
     327             :                     }
     328           0 :                     if ( tableType == tableTypeEnd )
     329             :                     {   // the only table type which can be part of this container is not allowed
     330             :                         // by the externally provided table type filter.
     331           0 :                         m_bConstructed = sal_True;
     332             :                         return;
     333             :                     }
     334             :                 }
     335           0 :                 aTableTypeFilter.realloc( 1 );
     336           0 :                 aTableTypeFilter[0] = sInherentTableTypeRestriction;
     337             :             }
     338             :             else
     339             :             {
     340             :                 // no container-inherent restriction for the table types
     341           0 :                 if ( _rTableTypeFilter.getLength() == 0 )
     342             :                 {   // no externally-provided table type filter => use the default filter
     343           0 :                     getAllTableTypeFilter( aTableTypeFilter );
     344             :                 }
     345             :                 else
     346             :                 {
     347           0 :                     aTableTypeFilter = _rTableTypeFilter;
     348             :                 }
     349             :             }
     350             : 
     351           0 :             static const ::rtl::OUString sAll("%");
     352           0 :             Reference< XResultSet > xTables = m_xMetaData->getTables( Any(), sAll, sAll, aTableTypeFilter );
     353           0 :             Reference< XRow > xCurrentRow( xTables, UNO_QUERY_THROW );
     354             : 
     355           0 :             TableInfos aUnfilteredTables;
     356             : 
     357           0 :             ::rtl::OUString sCatalog, sSchema, sName, sType;
     358           0 :             while ( xTables->next() )
     359             :             {
     360           0 :                 sCatalog    = xCurrentRow->getString(1);
     361           0 :                 sSchema     = xCurrentRow->getString(2);
     362           0 :                 sName       = xCurrentRow->getString(3);
     363           0 :                 sType       = xCurrentRow->getString(4);
     364             : 
     365           0 :                 aUnfilteredTables.push_back( TableInfo( sCatalog, sSchema, sName, sType ) );
     366             :             }
     367             : 
     368             :             reFill( lcl_filter( aUnfilteredTables,
     369           0 :                 _rTableFilter, aTableTypeFilter, m_xMetaData, NULL ) );
     370             : 
     371           0 :             disposeComponent( xTables );
     372             :         }
     373           0 :         catch (const Exception&)
     374             :         {
     375             :             DBG_UNHANDLED_EXCEPTION();
     376           0 :             disposing();
     377             :             return;
     378             :         }
     379             : 
     380           0 :         m_bConstructed = sal_True;
     381             :     }
     382             : 
     383           0 :     void OFilteredContainer::disposing()
     384             :     {
     385           0 :         OCollection::disposing();
     386             : 
     387           0 :         if ( m_xMasterContainer.is() )
     388           0 :             removeMasterContainerListener();
     389             : 
     390           0 :         m_xMasterContainer  = NULL;
     391           0 :         m_xMetaData         = NULL;
     392           0 :         m_pWarningsContainer = NULL;
     393           0 :         m_pRefreshListener  = NULL;
     394           0 :         m_bConstructed      = sal_False;
     395           0 :     }
     396             : 
     397           0 :     void OFilteredContainer::impl_refresh() throw(RuntimeException)
     398             :     {
     399             :         RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "api", "Ocke.Janssen@sun.com", "OFilteredContainer::impl_refresh" );
     400           0 :         if ( m_pRefreshListener )
     401             :         {
     402           0 :             m_bConstructed = sal_False;
     403           0 :             Reference<XRefreshable> xRefresh(m_xMasterContainer,UNO_QUERY);
     404           0 :             if ( xRefresh.is() )
     405           0 :                 xRefresh->refresh();
     406           0 :             m_pRefreshListener->refresh(this);
     407             :         }
     408           0 :     }
     409             : 
     410           0 :     ::rtl::OUString OFilteredContainer::getNameForObject(const ObjectType& _xObject)
     411             :     {
     412             :         OSL_ENSURE( _xObject.is(), "OFilteredContainer::getNameForObject: Object is NULL!" );
     413           0 :         return ::dbtools::composeTableName( m_xMetaData, _xObject, ::dbtools::eInDataManipulation, false, false, false );
     414             :     }
     415             : 
     416             :     // multiple to obtain all tables from XDatabaseMetaData::getTables, via passing a particular
     417             :     // table type filter:
     418             :     // adhere to the standard, which requests to pass a NULL table type filter, if
     419             :     // you want to retrieve all tables
     420             :     #define FILTER_MODE_STANDARD 0
     421             :     // only pass %, which is not allowed by the standard, but understood by some drivers
     422             :     #define FILTER_MODE_WILDCARD 1
     423             :     // only pass TABLE and VIEW
     424             :     #define FILTER_MODE_FIXED    2
     425             :     // do the thing which showed to be the safest way, understood by nearly all
     426             :     // drivers, even the ones which do not understand the standard
     427             :     #define FILTER_MODE_MIX_ALL  3
     428             : 
     429           0 :     void OFilteredContainer::getAllTableTypeFilter( Sequence< ::rtl::OUString >& /* [out] */ _rFilter ) const
     430             :     {
     431           0 :         sal_Int32 nFilterMode = FILTER_MODE_MIX_ALL;
     432             :             // for compatibility reasons, this is the default: we used this way before we
     433             :             // introduced the TableTypeFilterMode setting
     434             : 
     435             :         // obtain the data source we belong to, and the TableTypeFilterMode setting
     436           0 :         Any aFilterModeSetting;
     437           0 :         if ( getDataSourceSetting( getDataSource( (Reference< XInterface >)m_rParent ), "TableTypeFilterMode", aFilterModeSetting ) )
     438             :         {
     439           0 :             OSL_VERIFY( aFilterModeSetting >>= nFilterMode );
     440             :         }
     441             : 
     442           0 :         const ::rtl::OUString sAll( "%"  );
     443           0 :         const ::rtl::OUString sView( "VIEW"  );
     444           0 :         const ::rtl::OUString sTable( "TABLE"  );
     445             : 
     446           0 :         switch ( nFilterMode )
     447             :         {
     448             :         default:
     449             :             OSL_FAIL( "OTableContainer::getAllTableTypeFilter: unknown TableTypeFilterMode!" );
     450             :         case FILTER_MODE_MIX_ALL:
     451           0 :             _rFilter.realloc( 3 );
     452           0 :             _rFilter[0] = sView;
     453           0 :             _rFilter[1] = sTable;
     454           0 :             _rFilter[2] = sAll;
     455           0 :             break;
     456             :         case FILTER_MODE_FIXED:
     457           0 :             _rFilter.realloc( 2 );
     458           0 :             _rFilter[0] = sView;
     459           0 :             _rFilter[1] = sTable;
     460           0 :             break;
     461             :         case FILTER_MODE_WILDCARD:
     462           0 :             _rFilter.realloc( 1 );
     463           0 :             _rFilter[0] = sAll;
     464           0 :             break;
     465             :         case FILTER_MODE_STANDARD:
     466           0 :             _rFilter.realloc( 0 );
     467           0 :             break;
     468           0 :         }
     469           0 :     }
     470             : 
     471             : } // namespace
     472             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10