LCOV - code coverage report
Current view: top level - dbaccess/source/core/api - tablecontainer.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 52 213 24.4 %
Date: 2014-11-03 Functions: 9 21 42.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 "tablecontainer.hxx"
      21             : #include "dbastrings.hrc"
      22             : #include "table.hxx"
      23             : #include <comphelper/property.hxx>
      24             : #include <comphelper/processfactory.hxx>
      25             : #include <tools/debug.hxx>
      26             : #include <comphelper/enumhelper.hxx>
      27             : #include "core_resource.hxx"
      28             : #include "core_resource.hrc"
      29             : #include <com/sun/star/sdb/CommandType.hpp>
      30             : #include <com/sun/star/beans/XPropertySet.hpp>
      31             : #include <com/sun/star/beans/PropertyState.hpp>
      32             : #include <com/sun/star/beans/XPropertyState.hpp>
      33             : #include <com/sun/star/sdb/TableDefinition.hpp>
      34             : #include <com/sun/star/sdbc/XConnection.hpp>
      35             : #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
      36             : #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
      37             : #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
      38             : #include <com/sun/star/sdbc/KeyRule.hpp>
      39             : #include <com/sun/star/sdbcx/KeyType.hpp>
      40             : #include <com/sun/star/sdbc/ColumnValue.hpp>
      41             : #include <com/sun/star/sdbc/XRow.hpp>
      42             : #include <comphelper/types.hxx>
      43             : #include <connectivity/dbtools.hxx>
      44             : #include <comphelper/extract.hxx>
      45             : #include <connectivity/dbexception.hxx>
      46             : #include "TableDeco.hxx"
      47             : #include "sdbcoretools.hxx"
      48             : #include "ContainerMediator.hxx"
      49             : #include "definitioncolumn.hxx"
      50             : #include "objectnameapproval.hxx"
      51             : #include <tools/diagnose_ex.h>
      52             : 
      53             : using namespace dbaccess;
      54             : using namespace dbtools;
      55             : using namespace ::com::sun::star::uno;
      56             : using namespace ::com::sun::star::lang;
      57             : using namespace ::com::sun::star::beans;
      58             : using namespace ::com::sun::star::sdbc;
      59             : using namespace ::com::sun::star::sdb;
      60             : using namespace ::com::sun::star::sdbcx;
      61             : using namespace ::com::sun::star::container;
      62             : using namespace ::com::sun::star::util;
      63             : using namespace ::osl;
      64             : using namespace ::comphelper;
      65             : using namespace ::cppu;
      66             : using namespace ::connectivity::sdbcx;
      67             : 
      68             : namespace
      69             : {
      70           0 :     bool lcl_isPropertySetDefaulted(const Sequence< OUString>& _aNames,const Reference<XPropertySet>& _xProp)
      71             :     {
      72           0 :         Reference<XPropertyState> xState(_xProp,UNO_QUERY);
      73           0 :         if ( xState.is() )
      74             :         {
      75           0 :             const OUString* pIter = _aNames.getConstArray();
      76           0 :             const OUString* pEnd   = pIter + _aNames.getLength();
      77           0 :             for(;pIter != pEnd;++pIter)
      78             :             {
      79             :                 try
      80             :                 {
      81           0 :                     PropertyState aState = xState->getPropertyState(*pIter);
      82           0 :                     if ( aState != PropertyState_DEFAULT_VALUE )
      83           0 :                         break;
      84             :                 }
      85           0 :                 catch(const Exception&)
      86             :                 {
      87             :                     SAL_WARN("dbaccess", "lcl_isPropertySetDefaulted: Exception caught!" );
      88             :                 }
      89             :             }
      90           0 :             return ( pIter == pEnd );
      91             :         }
      92           0 :         return false;
      93             :     }
      94             : }
      95             : 
      96             : // OTableContainer
      97             : 
      98          70 : OTableContainer::OTableContainer(::cppu::OWeakObject& _rParent,
      99             :                                  ::osl::Mutex& _rMutex,
     100             :                                  const Reference< XConnection >& _xCon,
     101             :                                  bool _bCase,
     102             :                                  const Reference< XNameContainer >& _xTableDefinitions,
     103             :                                  IRefreshListener*  _pRefreshListener,
     104             :                                  ::dbtools::IWarningsContainer* _pWarningsContainer
     105             :                                  ,oslInterlockedCount& _nInAppend)
     106             :     :OFilteredContainer(_rParent,_rMutex,_xCon,_bCase,_pRefreshListener,_pWarningsContainer,_nInAppend)
     107             :     ,m_xTableDefinitions(_xTableDefinitions)
     108             :     ,m_pTableMediator( NULL )
     109          70 :     ,m_bInDrop(false)
     110             : {
     111          70 : }
     112             : 
     113         136 : OTableContainer::~OTableContainer()
     114             : {
     115         136 : }
     116             : 
     117          62 : void OTableContainer::removeMasterContainerListener()
     118             : {
     119             :     try
     120             :     {
     121          62 :         Reference<XContainer> xCont( m_xMasterContainer, UNO_QUERY_THROW );
     122          62 :         xCont->removeContainerListener( this );
     123             :     }
     124           0 :     catch( const Exception& )
     125             :     {
     126             :         DBG_UNHANDLED_EXCEPTION();
     127             :     }
     128          62 : }
     129             : 
     130           0 : OUString OTableContainer::getTableTypeRestriction() const
     131             : {
     132             :     // no restriction at all (other than the ones provided externally)
     133           0 :     return OUString();
     134             : }
     135             : 
     136             : // XServiceInfo
     137           0 : IMPLEMENT_SERVICE_INFO2(OTableContainer, "com.sun.star.sdb.dbaccess.OTableContainer", SERVICE_SDBCX_CONTAINER, SERVICE_SDBCX_TABLES)
     138             : 
     139             : namespace
     140             : {
     141          38 : void lcl_createDefintionObject(const OUString& _rName
     142             :                            ,const Reference< XNameContainer >& _xTableDefinitions
     143             :                            ,Reference<XPropertySet>& _xTableDefinition
     144             :                            ,Reference<XNameAccess>& _xColumnDefinitions)
     145             : {
     146          38 :     if ( _xTableDefinitions.is() )
     147             :     {
     148          38 :         if ( _xTableDefinitions->hasByName(_rName) )
     149          18 :             _xTableDefinition.set(_xTableDefinitions->getByName(_rName),UNO_QUERY);
     150             :         else
     151             :         {
     152             :             // set as folder
     153          20 :             _xTableDefinition.set( TableDefinition::createWithName( ::comphelper::getProcessComponentContext(), _rName ), UNO_QUERY);
     154          20 :             _xTableDefinitions->insertByName(_rName,makeAny(_xTableDefinition));
     155             :         }
     156          38 :         Reference<XColumnsSupplier> xColumnsSupplier(_xTableDefinition,UNO_QUERY);
     157          38 :         if ( xColumnsSupplier.is() )
     158          38 :             _xColumnDefinitions = xColumnsSupplier->getColumns();
     159             :     }
     160          38 : }
     161             : 
     162             : }
     163             : 
     164          38 : connectivity::sdbcx::ObjectType OTableContainer::createObject(const OUString& _rName)
     165             : {
     166          38 :     Reference<XColumnsSupplier > xSup;
     167          38 :     if(m_xMasterContainer.is() && m_xMasterContainer->hasByName(_rName))
     168          38 :         xSup.set(m_xMasterContainer->getByName(_rName),UNO_QUERY);
     169             : 
     170          38 :     connectivity::sdbcx::ObjectType xRet;
     171          38 :     if ( m_xMetaData.is() )
     172             :     {
     173          38 :         Reference<XPropertySet> xTableDefinition;
     174          76 :         Reference<XNameAccess> xColumnDefinitions;
     175          38 :         lcl_createDefintionObject(_rName,m_xTableDefinitions,xTableDefinition,xColumnDefinitions);
     176             : 
     177          38 :         if ( xSup.is() )
     178             :         {
     179          38 :             ODBTableDecorator* pTable = new ODBTableDecorator( m_xConnection, xSup, ::dbtools::getNumberFormats( m_xConnection ) ,xColumnDefinitions);
     180          38 :             xRet = pTable;
     181          38 :             pTable->construct();
     182             :         }
     183             :         else
     184             :         {
     185           0 :             OUString sCatalog,sSchema,sTable;
     186             :             ::dbtools::qualifiedNameComponents(m_xMetaData,
     187             :                                                 _rName,
     188             :                                                 sCatalog,
     189             :                                                 sSchema,
     190             :                                                 sTable,
     191           0 :                                                 ::dbtools::eInDataManipulation);
     192           0 :             Any aCatalog;
     193           0 :             if(!sCatalog.isEmpty())
     194           0 :                 aCatalog <<= sCatalog;
     195           0 :             OUString sType,sDescription;
     196           0 :             Sequence< OUString> aTypeFilter;
     197           0 :             getAllTableTypeFilter( aTypeFilter );
     198             : 
     199           0 :             Reference< XResultSet > xRes =  m_xMetaData.is() ? m_xMetaData->getTables(aCatalog,sSchema,sTable,aTypeFilter) : Reference< XResultSet >();
     200           0 :             if(xRes.is() && xRes->next())
     201             :             {
     202           0 :                 Reference< XRow > xRow(xRes,UNO_QUERY);
     203           0 :                 if(xRow.is())
     204             :                 {
     205           0 :                     sType           = xRow->getString(4);
     206           0 :                     sDescription    = xRow->getString(5);
     207           0 :                 }
     208             :             }
     209           0 :             ::comphelper::disposeComponent(xRes);
     210             :             ODBTable* pTable = new ODBTable(this
     211             :                                 ,m_xConnection
     212             :                                 ,sCatalog
     213             :                                 ,sSchema
     214             :                                 ,sTable
     215             :                                 ,sType
     216             :                                 ,sDescription
     217           0 :                                 ,xColumnDefinitions);
     218           0 :             xRet = pTable;
     219           0 :             pTable->construct();
     220             :         }
     221          76 :         Reference<XPropertySet> xDest(xRet,UNO_QUERY);
     222          38 :         if ( xTableDefinition.is() )
     223          38 :             ::comphelper::copyProperties(xTableDefinition,xDest);
     224             : 
     225          38 :         if ( !m_pTableMediator.is() )
     226         152 :             m_pTableMediator = new OContainerMediator(
     227         114 :                     this, m_xTableDefinitions.get(), m_xConnection );
     228          38 :         if ( m_pTableMediator.is() )
     229          76 :             m_pTableMediator->notifyElementCreated(_rName,xDest);
     230             :     }
     231             : 
     232          38 :     return xRet;
     233             : }
     234             : 
     235           0 : Reference< XPropertySet > OTableContainer::createDescriptor()
     236             : {
     237           0 :     Reference< XPropertySet > xRet;
     238             : 
     239             :     // first we have to look if the master tables support this
     240             :     // and if so then create a table object as well with the master tables
     241           0 :     Reference<XColumnsSupplier > xMasterColumnsSup;
     242           0 :     Reference<XDataDescriptorFactory> xDataFactory(m_xMasterContainer,UNO_QUERY);
     243           0 :     if ( xDataFactory.is() && m_xMetaData.is() )
     244             :     {
     245           0 :         xMasterColumnsSup = Reference< XColumnsSupplier >( xDataFactory->createDataDescriptor(), UNO_QUERY );
     246           0 :         ODBTableDecorator* pTable = new ODBTableDecorator( m_xConnection, xMasterColumnsSup, ::dbtools::getNumberFormats( m_xConnection ) ,NULL);
     247           0 :         xRet = pTable;
     248           0 :         pTable->construct();
     249             :     }
     250             :     else
     251             :     {
     252           0 :         ODBTable* pTable = new ODBTable(this, m_xConnection);
     253           0 :         xRet = pTable;
     254           0 :         pTable->construct();
     255             :     }
     256           0 :     return xRet;
     257             : }
     258             : 
     259             : // XAppend
     260           0 : ObjectType OTableContainer::appendObject( const OUString& _rForName, const Reference< XPropertySet >& descriptor )
     261             : {
     262             :     // append the new table with a create stmt
     263           0 :     OUString aName = getString(descriptor->getPropertyValue(PROPERTY_NAME));
     264           0 :     if(m_xMasterContainer.is() && m_xMasterContainer->hasByName(aName))
     265             :     {
     266           0 :         OUString sMessage(DBACORE_RESSTRING(RID_STR_TABLE_IS_FILTERED));
     267           0 :         throw SQLException(sMessage.replaceAll("$name$", aName),static_cast<XTypeProvider*>(static_cast<OFilteredContainer*>(this)),SQLSTATE_GENERAL,1000,Any());
     268             :     }
     269             : 
     270           0 :     Reference< XConnection > xConnection( m_xConnection.get(), UNO_QUERY );
     271           0 :     PContainerApprove pApprove( new ObjectNameApproval( xConnection, ObjectNameApproval::TypeTable ) );
     272           0 :     pApprove->approveElement( aName, descriptor );
     273             : 
     274             :     try
     275             :     {
     276           0 :         EnsureReset aReset(m_nInAppend);
     277           0 :         Reference<XAppend> xAppend(m_xMasterContainer,UNO_QUERY);
     278           0 :         if(xAppend.is())
     279             :         {
     280           0 :             xAppend->appendByDescriptor(descriptor);
     281             :         }
     282             :         else
     283             :         {
     284           0 :             OUString aSql = ::dbtools::createSqlCreateTableStatement(descriptor,m_xConnection);
     285             : 
     286           0 :             Reference<XConnection> xCon = m_xConnection;
     287             :             OSL_ENSURE(xCon.is(),"Connection is null!");
     288           0 :             if ( xCon.is() )
     289             :             {
     290           0 :                 Reference< XStatement > xStmt = xCon->createStatement(  );
     291           0 :                 if ( xStmt.is() )
     292           0 :                     xStmt->execute(aSql);
     293           0 :                 ::comphelper::disposeComponent(xStmt);
     294           0 :             }
     295           0 :         }
     296             :     }
     297           0 :     catch(const Exception&)
     298             :     {
     299           0 :         throw;
     300             :     }
     301             : 
     302           0 :     Reference<XPropertySet> xTableDefinition;
     303           0 :     Reference<XNameAccess> xColumnDefinitions;
     304           0 :     lcl_createDefintionObject(getNameForObject(descriptor),m_xTableDefinitions,xTableDefinition,xColumnDefinitions);
     305           0 :     Reference<XColumnsSupplier> xSup(descriptor,UNO_QUERY);
     306           0 :     Reference<XDataDescriptorFactory> xFac(xColumnDefinitions,UNO_QUERY);
     307           0 :     Reference<XAppend> xAppend(xColumnDefinitions,UNO_QUERY);
     308           0 :     bool bModified = false;
     309           0 :     if ( xSup.is() && xColumnDefinitions.is() && xFac.is() && xAppend.is() )
     310             :     {
     311           0 :         Reference<XNameAccess> xNames = xSup->getColumns();
     312           0 :         if ( xNames.is() )
     313             :         {
     314           0 :             Reference<XPropertySet> xProp = xFac->createDataDescriptor();
     315           0 :             Sequence< OUString> aSeq = xNames->getElementNames();
     316           0 :             const OUString* pIter = aSeq.getConstArray();
     317           0 :             const OUString* pEnd   = pIter + aSeq.getLength();
     318           0 :             for(;pIter != pEnd;++pIter)
     319             :             {
     320           0 :                 if ( !xColumnDefinitions->hasByName(*pIter) )
     321             :                 {
     322           0 :                     Reference<XPropertySet> xColumn(xNames->getByName(*pIter),UNO_QUERY);
     323           0 :                     if ( !OColumnSettings::hasDefaultSettings( xColumn ) )
     324             :                     {
     325           0 :                         ::comphelper::copyProperties( xColumn, xProp );
     326           0 :                         xAppend->appendByDescriptor( xProp );
     327           0 :                         bModified = true;
     328           0 :                     }
     329             :                 }
     330           0 :             }
     331           0 :         }
     332             :     }
     333             :     const static OUString s_pTableProps[] = {    OUString(PROPERTY_FILTER), OUString(PROPERTY_ORDER)
     334             :                                                     , OUString(PROPERTY_APPLYFILTER), OUString(PROPERTY_FONT)
     335             :                                                     , OUString(PROPERTY_ROW_HEIGHT), OUString(PROPERTY_TEXTCOLOR)
     336             :                                                     , OUString(PROPERTY_TEXTLINECOLOR), OUString(PROPERTY_TEXTEMPHASIS)
     337           0 :                                                     , OUString(PROPERTY_TEXTRELIEF) };
     338           0 :     Sequence< OUString> aNames(s_pTableProps,sizeof(s_pTableProps)/sizeof(s_pTableProps[0]));
     339           0 :     if ( bModified || !lcl_isPropertySetDefaulted(aNames,xTableDefinition) )
     340           0 :         ::dbaccess::notifyDataSourceModified(m_xTableDefinitions,true);
     341             : 
     342           0 :     return createObject( _rForName );
     343             : }
     344             : 
     345             : // XDrop
     346           0 : void OTableContainer::dropObject(sal_Int32 _nPos, const OUString& _sElementName)
     347             : {
     348           0 :     m_bInDrop = true;
     349             :     try
     350             :     {
     351           0 :         Reference< XDrop > xDrop(m_xMasterContainer,UNO_QUERY);
     352           0 :         if(xDrop.is())
     353           0 :             xDrop->dropByName(_sElementName);
     354             :         else
     355             :         {
     356           0 :             OUString sCatalog,sSchema,sTable,sComposedName;
     357             : 
     358           0 :             bool bIsView = false;
     359           0 :             Reference<XPropertySet> xTable(getObject(_nPos),UNO_QUERY);
     360           0 :             if ( xTable.is() && m_xMetaData.is() )
     361             :             {
     362           0 :                 if( m_xMetaData.is() && m_xMetaData->supportsCatalogsInTableDefinitions() )
     363           0 :                     xTable->getPropertyValue(PROPERTY_CATALOGNAME)  >>= sCatalog;
     364           0 :                 if( m_xMetaData.is() && m_xMetaData->supportsSchemasInTableDefinitions() )
     365           0 :                     xTable->getPropertyValue(PROPERTY_SCHEMANAME)   >>= sSchema;
     366           0 :                 xTable->getPropertyValue(PROPERTY_NAME)         >>= sTable;
     367             : 
     368           0 :                 sComposedName = ::dbtools::composeTableName( m_xMetaData, sCatalog, sSchema, sTable, true, ::dbtools::eInTableDefinitions );
     369             : 
     370           0 :                 OUString sType;
     371           0 :                 xTable->getPropertyValue(PROPERTY_TYPE)         >>= sType;
     372           0 :                 bIsView = sType.equalsIgnoreAsciiCase("VIEW");
     373             :             }
     374             : 
     375           0 :             if(sComposedName.isEmpty())
     376           0 :                 ::dbtools::throwFunctionSequenceException(static_cast<XTypeProvider*>(static_cast<OFilteredContainer*>(this)));
     377             : 
     378           0 :             OUString aSql("DROP ");
     379             : 
     380           0 :             if ( bIsView ) // here we have a view
     381           0 :                 aSql += "VIEW ";
     382             :             else
     383           0 :                 aSql += "TABLE ";
     384           0 :             aSql += sComposedName;
     385           0 :             Reference<XConnection> xCon = m_xConnection;
     386             :             OSL_ENSURE(xCon.is(),"Connection is null!");
     387           0 :             if ( xCon.is() )
     388             :             {
     389           0 :                 Reference< XStatement > xStmt = xCon->createStatement(  );
     390           0 :                 if(xStmt.is())
     391           0 :                     xStmt->execute(aSql);
     392           0 :                 ::comphelper::disposeComponent(xStmt);
     393           0 :             }
     394             :         }
     395             : 
     396           0 :         if ( m_xTableDefinitions.is() && m_xTableDefinitions->hasByName(_sElementName) )
     397             :         {
     398           0 :             m_xTableDefinitions->removeByName(_sElementName);
     399           0 :         }
     400             :     }
     401           0 :     catch(const Exception&)
     402             :     {
     403           0 :         m_bInDrop = false;
     404           0 :         throw;
     405             :     }
     406           0 :     m_bInDrop = false;
     407           0 : }
     408             : 
     409           0 : void SAL_CALL OTableContainer::elementInserted( const ContainerEvent& Event ) throw (RuntimeException, std::exception)
     410             : {
     411           0 :     ::osl::MutexGuard aGuard(m_rMutex);
     412           0 :     OUString sName;
     413           0 :     Event.Accessor >>= sName;
     414           0 :     if ( !m_nInAppend && !hasByName(sName) )
     415             :     {
     416           0 :         if(!m_xMasterContainer.is() || m_xMasterContainer->hasByName(sName))
     417             :         {
     418           0 :             ObjectType xName = createObject(sName);
     419           0 :             insertElement(sName,xName);
     420             :             // and notify our listeners
     421           0 :             ContainerEvent aEvent(static_cast<XContainer*>(this), makeAny(sName), makeAny(xName), Any());
     422           0 :             m_aContainerListeners.notifyEach( &XContainerListener::elementInserted, aEvent );
     423             :         }
     424           0 :     }
     425           0 : }
     426             : 
     427           0 : void SAL_CALL OTableContainer::elementRemoved( const ContainerEvent& /*Event*/ ) throw (RuntimeException, std::exception)
     428             : {
     429           0 : }
     430             : 
     431           0 : void SAL_CALL OTableContainer::elementReplaced( const ContainerEvent& Event ) throw (RuntimeException, std::exception)
     432             : {
     433             :     // create a new config entry
     434             :     {
     435           0 :         OUString sOldComposedName,sNewComposedName;
     436           0 :         Event.ReplacedElement   >>= sOldComposedName;
     437           0 :         Event.Accessor          >>= sNewComposedName;
     438             : 
     439           0 :         renameObject(sOldComposedName,sNewComposedName);
     440             :     }
     441           0 : }
     442             : 
     443          68 : void SAL_CALL OTableContainer::disposing()
     444             : {
     445          68 :     OFilteredContainer::disposing();
     446             :     // say goodbye to our listeners
     447          68 :     m_xTableDefinitions = NULL;
     448          68 :     m_pTableMediator = NULL;
     449          68 : }
     450             : 
     451           2 : void SAL_CALL OTableContainer::disposing( const ::com::sun::star::lang::EventObject& /*Source*/ ) throw (::com::sun::star::uno::RuntimeException, std::exception)
     452             : {
     453           2 : }
     454             : 
     455          64 : void OTableContainer::addMasterContainerListener()
     456             : {
     457             :     try
     458             :     {
     459          64 :         Reference< XContainer > xCont( m_xMasterContainer, UNO_QUERY_THROW );
     460          64 :         xCont->addContainerListener( this );
     461             :     }
     462           0 :     catch( const Exception& )
     463             :     {
     464             :         DBG_UNHANDLED_EXCEPTION();
     465             :     }
     466          64 : }
     467             : 
     468             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10