LCOV - code coverage report
Current view: top level - connectivity/source/drivers/postgresql - pq_xviews.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 99 0.0 %
Date: 2012-08-25 Functions: 0 10 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 0 -

           Branch data     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                 :            :  *    Version: MPL 1.1 / GPLv3+ / LGPLv2.1+
      32                 :            :  *
      33                 :            :  *    The contents of this file are subject to the Mozilla Public License Version
      34                 :            :  *    1.1 (the "License"); you may not use this file except in compliance with
      35                 :            :  *    the License or as specified alternatively below. You may obtain a copy of
      36                 :            :  *    the License at http://www.mozilla.org/MPL/
      37                 :            :  *
      38                 :            :  *    Software distributed under the License is distributed on an "AS IS" basis,
      39                 :            :  *    WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      40                 :            :  *    for the specific language governing rights and limitations under the
      41                 :            :  *    License.
      42                 :            :  *
      43                 :            :  *    Major Contributor(s):
      44                 :            :  *    [ Copyright (C) 2011 Lionel Elie Mamane <lionel@mamane.lu> ]
      45                 :            :  *
      46                 :            :  *    All Rights Reserved.
      47                 :            :  *
      48                 :            :  *    For minor contributions see the git repository.
      49                 :            :  *
      50                 :            :  *    Alternatively, the contents of this file may be used under the terms of
      51                 :            :  *    either the GNU General Public License Version 3 or later (the "GPLv3+"), or
      52                 :            :  *    the GNU Lesser General Public License Version 2.1 or later (the "LGPLv2.1+"),
      53                 :            :  *    in which case the provisions of the GPLv3+ or the LGPLv2.1+ are applicable
      54                 :            :  *    instead of those above.
      55                 :            :  *
      56                 :            :  ************************************************************************/
      57                 :            : 
      58                 :            : #include <rtl/ustrbuf.hxx>
      59                 :            : 
      60                 :            : #include <com/sun/star/sdbc/XRow.hpp>
      61                 :            : #include <com/sun/star/sdbc/XParameters.hpp>
      62                 :            : #include <com/sun/star/sdbcx/Privilege.hpp>
      63                 :            : 
      64                 :            : #include "pq_xviews.hxx"
      65                 :            : #include "pq_xview.hxx"
      66                 :            : #include "pq_xtables.hxx"
      67                 :            : #include "pq_statics.hxx"
      68                 :            : #include "pq_tools.hxx"
      69                 :            : 
      70                 :            : using osl::MutexGuard;
      71                 :            : 
      72                 :            : using rtl::OUString;
      73                 :            : using rtl::OUStringBuffer;
      74                 :            : using rtl::OUStringToOString;
      75                 :            : 
      76                 :            : using com::sun::star::beans::XPropertySet;
      77                 :            : 
      78                 :            : using com::sun::star::uno::Any;
      79                 :            : using com::sun::star::uno::makeAny;
      80                 :            : using com::sun::star::uno::UNO_QUERY;
      81                 :            : using com::sun::star::uno::Type;
      82                 :            : using com::sun::star::uno::XInterface;
      83                 :            : using com::sun::star::uno::Reference;
      84                 :            : using com::sun::star::uno::Sequence;
      85                 :            : using com::sun::star::uno::RuntimeException;
      86                 :            : 
      87                 :            : using com::sun::star::container::NoSuchElementException;
      88                 :            : using com::sun::star::lang::WrappedTargetException;
      89                 :            : 
      90                 :            : using com::sun::star::sdbc::XRow;
      91                 :            : using com::sun::star::sdbc::XCloseable;
      92                 :            : using com::sun::star::sdbc::XStatement;
      93                 :            : using com::sun::star::sdbc::XResultSet;
      94                 :            : using com::sun::star::sdbc::XParameters;
      95                 :            : using com::sun::star::sdbc::XPreparedStatement;
      96                 :            : using com::sun::star::sdbc::XDatabaseMetaData;
      97                 :            : 
      98                 :            : //  using com::sun::star::sdbcx::Privilege;
      99                 :            : 
     100                 :            : namespace pq_sdbc_driver
     101                 :            : {
     102                 :            : #define ASCII_STR(x) OUString( RTL_CONSTASCII_USTRINGPARAM( x ) )
     103                 :          0 : Views::Views(
     104                 :            :         const ::rtl::Reference< RefCountedMutex > & refMutex,
     105                 :            :         const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection >  & origin,
     106                 :            :         ConnectionSettings *pSettings )
     107                 :          0 :     : Container( refMutex, origin, pSettings,  getStatics().VIEW )
     108                 :          0 : {}
     109                 :            : 
     110                 :          0 : Views::~Views()
     111                 :          0 : {}
     112                 :            : 
     113                 :          0 : void Views::refresh()
     114                 :            :     throw (::com::sun::star::uno::RuntimeException)
     115                 :            : {
     116                 :            :     try
     117                 :            :     {
     118                 :          0 :         osl::MutexGuard guard( m_refMutex->mutex );
     119                 :          0 :         Statics & st = getStatics();
     120                 :            : 
     121                 :          0 :         Reference< XStatement > stmt = m_origin->createStatement();
     122                 :            : 
     123                 :          0 :         Reference< XResultSet > rs = stmt->executeQuery(
     124                 :            :             ASCII_STR( "SELECT "
     125                 :            :                               "DISTINCT ON( pg_namespace.nspname, relname) " // needed because of duplicates
     126                 :            :                               "pg_namespace.nspname,"     // 1
     127                 :            :                               "relname,"                  // 2
     128                 :            :                               "pg_get_viewdef(ev_class) " // 3
     129                 :            :                        "FROM pg_namespace, pg_class, pg_rewrite "
     130                 :            :                        "WHERE pg_namespace.oid = relnamespace "
     131                 :            :                              "AND pg_class.oid = ev_class "
     132                 :          0 :                        "AND relkind='v'" ) );
     133                 :            : 
     134                 :          0 :         Reference< XRow > xRow( rs , UNO_QUERY );
     135                 :            : 
     136                 :          0 :         m_values = Sequence< com::sun::star::uno::Any > ();
     137                 :          0 :         String2IntMap map;
     138                 :          0 :         sal_Int32 viewIndex = 0;
     139                 :            : 
     140                 :          0 :         while( rs->next() )
     141                 :            :         {
     142                 :          0 :             rtl::OUString table, schema, command;
     143                 :          0 :             schema = xRow->getString( 1 );
     144                 :          0 :             table = xRow->getString( 2 );
     145                 :          0 :             command = xRow->getString( 3 );
     146                 :            : 
     147                 :          0 :             View *pView = new View (m_refMutex, m_origin, m_pSettings );
     148                 :          0 :             Reference< com::sun::star::beans::XPropertySet > prop = pView;
     149                 :            : 
     150                 :          0 :             pView->setPropertyValue_NoBroadcast_public(st.NAME , makeAny(table) );
     151                 :          0 :             pView->setPropertyValue_NoBroadcast_public(st.SCHEMA_NAME, makeAny(schema) );
     152                 :          0 :             pView->setPropertyValue_NoBroadcast_public(st.COMMAND, makeAny(command) );
     153                 :            : 
     154                 :            :             {
     155                 :          0 :                 const int currentViewIndex = viewIndex++;
     156                 :            :                 assert(currentViewIndex  == m_values.getLength());
     157                 :          0 :                 m_values.realloc( viewIndex );
     158                 :          0 :                 m_values[currentViewIndex] = makeAny( prop );
     159                 :          0 :                 OUStringBuffer buf( table.getLength() + schema.getLength() + 1);
     160                 :          0 :                 buf.append( schema ).appendAscii( "." ).append( table );
     161                 :          0 :                 map[ buf.makeStringAndClear() ] = currentViewIndex;
     162                 :            :             }
     163                 :          0 :         }
     164                 :          0 :         m_name2index.swap( map );
     165                 :            :     }
     166                 :          0 :     catch ( com::sun::star::sdbc::SQLException & e )
     167                 :            :     {
     168                 :          0 :         throw RuntimeException( e.Message , e.Context );
     169                 :            :     }
     170                 :          0 :     fire( RefreshedBroadcaster( *this ) );
     171                 :          0 : }
     172                 :            : 
     173                 :            : 
     174                 :          0 : void Views::appendByDescriptor(
     175                 :            :     const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& descriptor )
     176                 :            :     throw (::com::sun::star::sdbc::SQLException,
     177                 :            :            ::com::sun::star::container::ElementExistException,
     178                 :            :            ::com::sun::star::uno::RuntimeException)
     179                 :            : {
     180                 :          0 :     osl::MutexGuard guard( m_refMutex->mutex );
     181                 :            : 
     182                 :          0 :     Statics &st = getStatics();
     183                 :          0 :     OUString name,schema,command;
     184                 :          0 :     descriptor->getPropertyValue( st.SCHEMA_NAME ) >>= schema;
     185                 :          0 :     descriptor->getPropertyValue( st.NAME ) >>= name;
     186                 :          0 :     descriptor->getPropertyValue( st.COMMAND ) >>= command;
     187                 :            : 
     188                 :          0 :     Reference< XStatement > stmt = m_origin->createStatement();
     189                 :            : 
     190                 :          0 :     OUStringBuffer buf( 128 );
     191                 :            : 
     192                 :          0 :     buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "CREATE VIEW " ) );
     193                 :          0 :     bufferQuoteQualifiedIdentifier( buf, schema, name, m_pSettings );
     194                 :          0 :     buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( " AS " ) );
     195                 :          0 :     buf.append( command );
     196                 :            : 
     197                 :          0 :     stmt->executeUpdate( buf.makeStringAndClear() );
     198                 :            : 
     199                 :          0 :     disposeNoThrow( stmt );
     200                 :          0 :     refresh();
     201                 :          0 :     if( m_pSettings->tables.is() )
     202                 :            :     {
     203                 :          0 :         m_pSettings->pTablesImpl->refresh();
     204                 :          0 :     }
     205                 :            :     // increase the vector
     206                 :            : //     sal_Int32 index = m_values.getLength();
     207                 :            : //     m_values.realloc( index + 1 );
     208                 :            : 
     209                 :            : //     View * pView =
     210                 :            : //         new View( m_refMutex, m_origin, m_pSettings, false /*modifiable*/ );
     211                 :            : //     Reference< com::sun::star::beans::XPropertySet > prop = pTable;
     212                 :            : //     copyProperties( pTable, descriptor );
     213                 :            : //     m_values[index] = makeAny( prop );
     214                 :            : //     OUStringBuffer buf( name.getLength() + 1 + schema.getLength() );
     215                 :            : //     buf.append( schema ).appendAscii( "." ).append( name );
     216                 :            : //     m_name2index[ buf.makeStringAndClear() ] = index;
     217                 :          0 : }
     218                 :            : 
     219                 :          0 : void Views::dropByName( const ::rtl::OUString& elementName )
     220                 :            :     throw (::com::sun::star::sdbc::SQLException,
     221                 :            :            ::com::sun::star::container::NoSuchElementException,
     222                 :            :            ::com::sun::star::uno::RuntimeException)
     223                 :            : {
     224                 :          0 :     String2IntMap::const_iterator ii = m_name2index.find( elementName );
     225                 :          0 :     if( ii == m_name2index.end() )
     226                 :            :     {
     227                 :          0 :         OUStringBuffer buf( 128 );
     228                 :          0 :         buf.appendAscii( "View " );
     229                 :          0 :         buf.append( elementName );
     230                 :          0 :         buf.appendAscii( " is unknown, so it can't be dropped" );
     231                 :            :         throw com::sun::star::container::NoSuchElementException(
     232                 :          0 :             buf.makeStringAndClear(), *this );
     233                 :            :     }
     234                 :          0 :     dropByIndex( ii->second );
     235                 :          0 : }
     236                 :            : 
     237                 :          0 : void Views::dropByIndex( sal_Int32 index )
     238                 :            :     throw (::com::sun::star::sdbc::SQLException,
     239                 :            :            ::com::sun::star::lang::IndexOutOfBoundsException,
     240                 :            :            ::com::sun::star::uno::RuntimeException)
     241                 :            : {
     242                 :            : //     throw SQLException(
     243                 :            : //         ASCII_STR( "view deletion not supported" ), *this, OUString(), 1, Any() );
     244                 :          0 :     osl::MutexGuard guard( m_refMutex->mutex );
     245                 :          0 :     if( index < 0 ||  index >= m_values.getLength() )
     246                 :            :     {
     247                 :          0 :         OUStringBuffer buf( 128 );
     248                 :          0 :         buf.appendAscii( "VIEWS: Index out of range (allowed 0 to " );
     249                 :          0 :         buf.append( (sal_Int32) (m_values.getLength() -1) );
     250                 :          0 :         buf.appendAscii( ", got " );
     251                 :          0 :         buf.append( index );
     252                 :          0 :         buf.appendAscii( ")" );
     253                 :            :         throw com::sun::star::lang::IndexOutOfBoundsException(
     254                 :          0 :             buf.makeStringAndClear(), *this );
     255                 :            :     }
     256                 :            : 
     257                 :          0 :     Reference< XPropertySet > set;
     258                 :          0 :     m_values[index] >>= set;
     259                 :          0 :     Statics &st = getStatics();
     260                 :          0 :     OUString name,schema;
     261                 :          0 :     set->getPropertyValue( st.SCHEMA_NAME ) >>= schema;
     262                 :          0 :     set->getPropertyValue( st.NAME ) >>= name;
     263                 :            : 
     264                 :          0 :     OUStringBuffer update( 128 );
     265                 :          0 :     update.appendAscii( "DROP VIEW \"" ).append( schema ).appendAscii( "\".\"" );
     266                 :          0 :     update.append( name ).appendAscii( "\"" );
     267                 :            : 
     268                 :          0 :     Reference< XStatement > stmt = m_origin->createStatement( );
     269                 :            : 
     270                 :          0 :     stmt->executeUpdate( update.makeStringAndClear() );
     271                 :          0 : }
     272                 :            : 
     273                 :            : 
     274                 :          0 : ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > Views::createDataDescriptor()
     275                 :            :         throw (::com::sun::star::uno::RuntimeException)
     276                 :            : {
     277                 :          0 :     return new ViewDescriptor( m_refMutex, m_origin, m_pSettings );
     278                 :            : }
     279                 :            : 
     280                 :          0 : Reference< com::sun::star::container::XNameAccess > Views::create(
     281                 :            :     const ::rtl::Reference< RefCountedMutex > & refMutex,
     282                 :            :     const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection >  & origin,
     283                 :            :     ConnectionSettings *pSettings,
     284                 :            :     Views **ppViews)
     285                 :            : {
     286                 :          0 :     *ppViews = new Views( refMutex, origin, pSettings );
     287                 :          0 :     Reference< com::sun::star::container::XNameAccess > ret = *ppViews;
     288                 :          0 :     (*ppViews)->refresh();
     289                 :            : 
     290                 :          0 :     return ret;
     291                 :            : }
     292                 :            : 
     293                 :          0 : void Views::disposing()
     294                 :            : {
     295                 :          0 :     Container::disposing();
     296                 :          0 : }
     297                 :            : 
     298                 :            : 
     299                 :            : 
     300                 :            : };

Generated by: LCOV version 1.10