LCOV - code coverage report
Current view: top level - connectivity/source/drivers/postgresql - pq_xviews.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 92 0.0 %
Date: 2014-04-14 Functions: 0 10 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             :  *
       4             :  *  Effective License of whole file:
       5             :  *
       6             :  *    This library is free software; you can redistribute it and/or
       7             :  *    modify it under the terms of the GNU Lesser General Public
       8             :  *    License version 2.1, as published by the Free Software Foundation.
       9             :  *
      10             :  *    This library is distributed in the hope that it will be useful,
      11             :  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
      12             :  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13             :  *    Lesser General Public License for more details.
      14             :  *
      15             :  *    You should have received a copy of the GNU Lesser General Public
      16             :  *    License along with this library; if not, write to the Free Software
      17             :  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
      18             :  *    MA  02111-1307  USA
      19             :  *
      20             :  *  Parts "Copyright by Sun Microsystems, Inc" prior to August 2011:
      21             :  *
      22             :  *    The Contents of this file are made available subject to the terms of
      23             :  *    the GNU Lesser General Public License Version 2.1
      24             :  *
      25             :  *    Copyright: 2000 by Sun Microsystems, Inc.
      26             :  *
      27             :  *    Contributor(s): Joerg Budischewski
      28             :  *
      29             :  *  All parts contributed on or after August 2011:
      30             :  *
      31             :  *    This Source Code Form is subject to the terms of the Mozilla Public
      32             :  *    License, v. 2.0. If a copy of the MPL was not distributed with this
      33             :  *    file, You can obtain one at http://mozilla.org/MPL/2.0/.
      34             :  *
      35             :  ************************************************************************/
      36             : 
      37             : #include <rtl/ustrbuf.hxx>
      38             : 
      39             : #include <com/sun/star/sdbc/XRow.hpp>
      40             : #include <com/sun/star/sdbc/XParameters.hpp>
      41             : #include <com/sun/star/sdbcx/Privilege.hpp>
      42             : 
      43             : #include "pq_xviews.hxx"
      44             : #include "pq_xview.hxx"
      45             : #include "pq_xtables.hxx"
      46             : #include "pq_statics.hxx"
      47             : #include "pq_tools.hxx"
      48             : 
      49             : using osl::MutexGuard;
      50             : 
      51             : 
      52             : using com::sun::star::beans::XPropertySet;
      53             : 
      54             : using com::sun::star::uno::Any;
      55             : using com::sun::star::uno::makeAny;
      56             : using com::sun::star::uno::UNO_QUERY;
      57             : using com::sun::star::uno::Type;
      58             : using com::sun::star::uno::XInterface;
      59             : using com::sun::star::uno::Reference;
      60             : using com::sun::star::uno::Sequence;
      61             : using com::sun::star::uno::RuntimeException;
      62             : 
      63             : using com::sun::star::container::NoSuchElementException;
      64             : using com::sun::star::lang::WrappedTargetException;
      65             : 
      66             : using com::sun::star::sdbc::XRow;
      67             : using com::sun::star::sdbc::XCloseable;
      68             : using com::sun::star::sdbc::XStatement;
      69             : using com::sun::star::sdbc::XResultSet;
      70             : using com::sun::star::sdbc::XParameters;
      71             : using com::sun::star::sdbc::XPreparedStatement;
      72             : using com::sun::star::sdbc::XDatabaseMetaData;
      73             : 
      74             : //  using com::sun::star::sdbcx::Privilege;
      75             : 
      76             : namespace pq_sdbc_driver
      77             : {
      78           0 : Views::Views(
      79             :         const ::rtl::Reference< RefCountedMutex > & refMutex,
      80             :         const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection >  & origin,
      81             :         ConnectionSettings *pSettings )
      82           0 :     : Container( refMutex, origin, pSettings,  getStatics().VIEW )
      83           0 : {}
      84             : 
      85           0 : Views::~Views()
      86           0 : {}
      87             : 
      88           0 : void Views::refresh()
      89             :     throw (::com::sun::star::uno::RuntimeException, std::exception)
      90             : {
      91             :     try
      92             :     {
      93           0 :         osl::MutexGuard guard( m_refMutex->mutex );
      94           0 :         Statics & st = getStatics();
      95             : 
      96           0 :         Reference< XStatement > stmt = m_origin->createStatement();
      97             : 
      98           0 :         Reference< XResultSet > rs = stmt->executeQuery("SELECT "
      99             :                                                         "DISTINCT ON( pg_namespace.nspname, relname) " // needed because of duplicates
     100             :                                                         "pg_namespace.nspname,"    // 1
     101             :                                                         "relname,"                 // 2
     102             :                                                         "pg_get_viewdef(ev_class) " // 3
     103             :                                                         "FROM pg_namespace, pg_class, pg_rewrite "
     104             :                                                        "WHERE pg_namespace.oid = relnamespace "
     105             :                                                          "AND pg_class.oid = ev_class "
     106           0 :                                                          "AND relkind=\'v\'" );
     107             : 
     108           0 :         Reference< XRow > xRow( rs , UNO_QUERY );
     109             : 
     110           0 :         m_values = Sequence< com::sun::star::uno::Any > ();
     111           0 :         String2IntMap map;
     112           0 :         sal_Int32 viewIndex = 0;
     113             : 
     114           0 :         while( rs->next() )
     115             :         {
     116           0 :             OUString table, schema, command;
     117           0 :             schema = xRow->getString( 1 );
     118           0 :             table = xRow->getString( 2 );
     119           0 :             command = xRow->getString( 3 );
     120             : 
     121           0 :             View *pView = new View (m_refMutex, m_origin, m_pSettings );
     122           0 :             Reference< com::sun::star::beans::XPropertySet > prop = pView;
     123             : 
     124           0 :             pView->setPropertyValue_NoBroadcast_public(st.NAME , makeAny(table) );
     125           0 :             pView->setPropertyValue_NoBroadcast_public(st.SCHEMA_NAME, makeAny(schema) );
     126           0 :             pView->setPropertyValue_NoBroadcast_public(st.COMMAND, makeAny(command) );
     127             : 
     128             :             {
     129           0 :                 const int currentViewIndex = viewIndex++;
     130             :                 assert(currentViewIndex  == m_values.getLength());
     131           0 :                 m_values.realloc( viewIndex );
     132           0 :                 m_values[currentViewIndex] = makeAny( prop );
     133           0 :                 OUStringBuffer buf( table.getLength() + schema.getLength() + 1);
     134           0 :                 buf.append( schema + "." + table );
     135           0 :                 map[ buf.makeStringAndClear() ] = currentViewIndex;
     136             :             }
     137           0 :         }
     138           0 :         m_name2index.swap( map );
     139             :     }
     140           0 :     catch ( com::sun::star::sdbc::SQLException & e )
     141             :     {
     142           0 :         throw RuntimeException( e.Message , e.Context );
     143             :     }
     144           0 :     fire( RefreshedBroadcaster( *this ) );
     145           0 : }
     146             : 
     147             : 
     148           0 : void Views::appendByDescriptor(
     149             :     const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& descriptor )
     150             :     throw (::com::sun::star::sdbc::SQLException,
     151             :            ::com::sun::star::container::ElementExistException,
     152             :            ::com::sun::star::uno::RuntimeException, std::exception)
     153             : {
     154           0 :     osl::MutexGuard guard( m_refMutex->mutex );
     155             : 
     156           0 :     Statics &st = getStatics();
     157           0 :     OUString name,schema,command;
     158           0 :     descriptor->getPropertyValue( st.SCHEMA_NAME ) >>= schema;
     159           0 :     descriptor->getPropertyValue( st.NAME ) >>= name;
     160           0 :     descriptor->getPropertyValue( st.COMMAND ) >>= command;
     161             : 
     162           0 :     Reference< XStatement > stmt = m_origin->createStatement();
     163             : 
     164           0 :     OUStringBuffer buf( 128 );
     165             : 
     166           0 :     buf.append( "CREATE VIEW ");
     167           0 :     bufferQuoteQualifiedIdentifier( buf, schema, name, m_pSettings );
     168           0 :     buf.append(" AS " + command );
     169             : 
     170           0 :     stmt->executeUpdate( buf.makeStringAndClear() );
     171             : 
     172           0 :     disposeNoThrow( stmt );
     173           0 :     refresh();
     174           0 :     if( m_pSettings->tables.is() )
     175             :     {
     176           0 :         m_pSettings->pTablesImpl->refresh();
     177           0 :     }
     178           0 : }
     179             : 
     180           0 : void Views::dropByName( const OUString& elementName )
     181             :     throw (::com::sun::star::sdbc::SQLException,
     182             :            ::com::sun::star::container::NoSuchElementException,
     183             :            ::com::sun::star::uno::RuntimeException, std::exception)
     184             : {
     185           0 :     String2IntMap::const_iterator ii = m_name2index.find( elementName );
     186           0 :     if( ii == m_name2index.end() )
     187             :     {
     188           0 :         OUStringBuffer buf( 128 );
     189           0 :         buf.append( "View " + elementName + " is unknown, so it can't be dropped" );
     190             :         throw com::sun::star::container::NoSuchElementException(
     191           0 :             buf.makeStringAndClear(), *this );
     192             :     }
     193           0 :     dropByIndex( ii->second );
     194           0 : }
     195             : 
     196           0 : void Views::dropByIndex( sal_Int32 index )
     197             :     throw (::com::sun::star::sdbc::SQLException,
     198             :            ::com::sun::star::lang::IndexOutOfBoundsException,
     199             :            ::com::sun::star::uno::RuntimeException, std::exception)
     200             : {
     201           0 :     osl::MutexGuard guard( m_refMutex->mutex );
     202           0 :     if( index < 0 ||  index >= m_values.getLength() )
     203             :     {
     204           0 :         OUStringBuffer buf( 128 );
     205           0 :         buf.append( "VIEWS: Index out of range (allowed 0 to " + OUString::number(m_values.getLength() -1) +
     206           0 :                     ", got " + OUString::number( index ) + ")");
     207             :         throw com::sun::star::lang::IndexOutOfBoundsException(
     208           0 :             buf.makeStringAndClear(), *this );
     209             :     }
     210             : 
     211           0 :     Reference< XPropertySet > set;
     212           0 :     m_values[index] >>= set;
     213           0 :     Statics &st = getStatics();
     214           0 :     OUString name,schema;
     215           0 :     set->getPropertyValue( st.SCHEMA_NAME ) >>= schema;
     216           0 :     set->getPropertyValue( st.NAME ) >>= name;
     217             : 
     218           0 :     OUStringBuffer update( 128 );
     219           0 :     update.append( "DROP VIEW \"" + schema + "\".\"" + name + "\"" );
     220             : 
     221           0 :     Reference< XStatement > stmt = m_origin->createStatement( );
     222             : 
     223           0 :     stmt->executeUpdate( update.makeStringAndClear() );
     224           0 : }
     225             : 
     226             : 
     227           0 : ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > Views::createDataDescriptor()
     228             :         throw (::com::sun::star::uno::RuntimeException, std::exception)
     229             : {
     230           0 :     return new ViewDescriptor( m_refMutex, m_origin, m_pSettings );
     231             : }
     232             : 
     233           0 : Reference< com::sun::star::container::XNameAccess > Views::create(
     234             :     const ::rtl::Reference< RefCountedMutex > & refMutex,
     235             :     const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection >  & origin,
     236             :     ConnectionSettings *pSettings,
     237             :     Views **ppViews)
     238             : {
     239           0 :     *ppViews = new Views( refMutex, origin, pSettings );
     240           0 :     Reference< com::sun::star::container::XNameAccess > ret = *ppViews;
     241           0 :     (*ppViews)->refresh();
     242             : 
     243           0 :     return ret;
     244             : }
     245             : 
     246           0 : void Views::disposing()
     247             : {
     248           0 :     Container::disposing();
     249           0 : }
     250             : 
     251             : 
     252             : 
     253             : };
     254             : 
     255             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10