LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/connectivity/source/drivers/hsqldb - HTable.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 174 0.0 %
Date: 2013-07-09 Functions: 0 19 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 <string.h>
      21             : #include "hsqldb/HTable.hxx"
      22             : #include "hsqldb/HTables.hxx"
      23             : #include <com/sun/star/sdbc/XRow.hpp>
      24             : #include <com/sun/star/sdbc/XResultSet.hpp>
      25             : #include <com/sun/star/sdbcx/KeyType.hpp>
      26             : #include <com/sun/star/sdbc/KeyRule.hpp>
      27             : #include <cppuhelper/typeprovider.hxx>
      28             : #include <com/sun/star/lang/DisposedException.hpp>
      29             : #include <com/sun/star/sdbc/ColumnValue.hpp>
      30             : #include <com/sun/star/sdbcx/Privilege.hpp>
      31             : #include <comphelper/property.hxx>
      32             : #include <comphelper/extract.hxx>
      33             : #include <comphelper/types.hxx>
      34             : #include "connectivity/dbtools.hxx"
      35             : #include "connectivity/sdbcx/VColumn.hxx"
      36             : #include "connectivity/TKeys.hxx"
      37             : #include "connectivity/TIndexes.hxx"
      38             : #include "connectivity/TColumnsHelper.hxx"
      39             : #include "hsqldb/HCatalog.hxx"
      40             : #include "hsqldb/HColumns.hxx"
      41             : #include "TConnection.hxx"
      42             : 
      43             : #include <tools/diagnose_ex.h>
      44             : 
      45             : 
      46             : using namespace ::comphelper;
      47             : using namespace connectivity::hsqldb;
      48             : using namespace connectivity::sdbcx;
      49             : using namespace connectivity;
      50             : using namespace ::com::sun::star::uno;
      51             : using namespace ::com::sun::star::beans;
      52             : using namespace ::com::sun::star::sdbcx;
      53             : using namespace ::com::sun::star::sdbc;
      54             : using namespace ::com::sun::star::container;
      55             : using namespace ::com::sun::star::lang;
      56             : 
      57           0 : OHSQLTable::OHSQLTable( sdbcx::OCollection* _pTables,
      58             :                            const Reference< XConnection >& _xConnection)
      59           0 :     :OTableHelper(_pTables,_xConnection,sal_True)
      60             : {
      61             :     // we create a new table here, so we should have all the rights or ;-)
      62             :     m_nPrivileges = Privilege::DROP         |
      63             :                     Privilege::REFERENCE    |
      64             :                     Privilege::ALTER        |
      65             :                     Privilege::CREATE       |
      66             :                     Privilege::READ         |
      67             :                     Privilege::DELETE       |
      68             :                     Privilege::UPDATE       |
      69             :                     Privilege::INSERT       |
      70           0 :                     Privilege::SELECT;
      71           0 :     construct();
      72           0 : }
      73             : // -------------------------------------------------------------------------
      74           0 : OHSQLTable::OHSQLTable( sdbcx::OCollection* _pTables,
      75             :                            const Reference< XConnection >& _xConnection,
      76             :                     const OUString& _Name,
      77             :                     const OUString& _Type,
      78             :                     const OUString& _Description ,
      79             :                     const OUString& _SchemaName,
      80             :                     const OUString& _CatalogName,
      81             :                     sal_Int32 _nPrivileges
      82             :                 ) : OTableHelper(   _pTables,
      83             :                                     _xConnection,
      84             :                                     sal_True,
      85             :                                     _Name,
      86             :                                     _Type,
      87             :                                     _Description,
      88             :                                     _SchemaName,
      89             :                                     _CatalogName)
      90           0 :  , m_nPrivileges(_nPrivileges)
      91             : {
      92           0 :     construct();
      93           0 : }
      94             : // -------------------------------------------------------------------------
      95           0 : void OHSQLTable::construct()
      96             : {
      97           0 :     OTableHelper::construct();
      98           0 :     if ( !isNew() )
      99           0 :         registerProperty(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRIVILEGES),  PROPERTY_ID_PRIVILEGES,PropertyAttribute::READONLY,&m_nPrivileges,  ::getCppuType(&m_nPrivileges));
     100           0 : }
     101             : // -----------------------------------------------------------------------------
     102           0 : ::cppu::IPropertyArrayHelper* OHSQLTable::createArrayHelper( sal_Int32 /*_nId*/ ) const
     103             : {
     104           0 :     return doCreateArrayHelper();
     105             : }
     106             : // -------------------------------------------------------------------------
     107           0 : ::cppu::IPropertyArrayHelper & OHSQLTable::getInfoHelper()
     108             : {
     109           0 :     return *static_cast<OHSQLTable_PROP*>(const_cast<OHSQLTable*>(this))->getArrayHelper(isNew() ? 1 : 0);
     110             : }
     111             : // -----------------------------------------------------------------------------
     112           0 : sdbcx::OCollection* OHSQLTable::createColumns(const TStringVector& _rNames)
     113             : {
     114           0 :     OHSQLColumns* pColumns = new OHSQLColumns(*this,sal_True,m_aMutex,_rNames);
     115           0 :     pColumns->setParent(this);
     116           0 :     return pColumns;
     117             : }
     118             : // -----------------------------------------------------------------------------
     119           0 : sdbcx::OCollection* OHSQLTable::createKeys(const TStringVector& _rNames)
     120             : {
     121           0 :     return new OKeysHelper(this,m_aMutex,_rNames);
     122             : }
     123             : // -----------------------------------------------------------------------------
     124           0 : sdbcx::OCollection* OHSQLTable::createIndexes(const TStringVector& _rNames)
     125             : {
     126           0 :     return new OIndexesHelper(this,m_aMutex,_rNames);
     127             : }
     128             : //--------------------------------------------------------------------------
     129           0 : Sequence< sal_Int8 > OHSQLTable::getUnoTunnelImplementationId()
     130             : {
     131             :     static ::cppu::OImplementationId * pId = 0;
     132           0 :     if (! pId)
     133             :     {
     134           0 :         ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
     135           0 :         if (! pId)
     136             :         {
     137           0 :             static ::cppu::OImplementationId aId;
     138           0 :             pId = &aId;
     139           0 :         }
     140             :     }
     141           0 :     return pId->getImplementationId();
     142             : }
     143             : 
     144             : // com::sun::star::lang::XUnoTunnel
     145             : //------------------------------------------------------------------
     146           0 : sal_Int64 OHSQLTable::getSomething( const Sequence< sal_Int8 > & rId ) throw (RuntimeException)
     147             : {
     148           0 :     return (rId.getLength() == 16 && 0 == memcmp(getUnoTunnelImplementationId().getConstArray(),  rId.getConstArray(), 16 ) )
     149             :                 ? reinterpret_cast< sal_Int64 >( this )
     150           0 :                 : OTable_TYPEDEF::getSomething(rId);
     151             : }
     152             : // -------------------------------------------------------------------------
     153             : // XAlterTable
     154           0 : void SAL_CALL OHSQLTable::alterColumnByName( const OUString& colName, const Reference< XPropertySet >& descriptor ) throw(SQLException, NoSuchElementException, RuntimeException)
     155             : {
     156           0 :     ::osl::MutexGuard aGuard(m_aMutex);
     157             :     checkDisposed(
     158             : #ifdef __GNUC__
     159             :         ::connectivity::sdbcx::OTableDescriptor_BASE::rBHelper.bDisposed
     160             : #else
     161             :         rBHelper.bDisposed
     162             : #endif
     163           0 :         );
     164             : 
     165           0 :     if ( m_pColumns && !m_pColumns->hasByName(colName) )
     166           0 :         throw NoSuchElementException(colName,*this);
     167             : 
     168             : 
     169           0 :     if ( !isNew() )
     170             :     {
     171             :         // first we have to check what should be altered
     172           0 :         Reference<XPropertySet> xProp;
     173           0 :         m_pColumns->getByName(colName) >>= xProp;
     174             :         // first check the types
     175           0 :         sal_Int32 nOldType = 0,nNewType = 0,nOldPrec = 0,nNewPrec = 0,nOldScale = 0,nNewScale = 0;
     176           0 :         OUString sOldTypeName, sNewTypeName;
     177             : 
     178           0 :         ::dbtools::OPropertyMap& rProp = OMetaConnection::getPropMap();
     179             : 
     180             :         // type/typename
     181           0 :         xProp->getPropertyValue(rProp.getNameByIndex(PROPERTY_ID_TYPE))         >>= nOldType;
     182           0 :         descriptor->getPropertyValue(rProp.getNameByIndex(PROPERTY_ID_TYPE))    >>= nNewType;
     183           0 :         xProp->getPropertyValue(rProp.getNameByIndex(PROPERTY_ID_TYPENAME))     >>= sOldTypeName;
     184           0 :         descriptor->getPropertyValue(rProp.getNameByIndex(PROPERTY_ID_TYPENAME))>>= sNewTypeName;
     185             : 
     186             :         // and precsions and scale
     187           0 :         xProp->getPropertyValue(rProp.getNameByIndex(PROPERTY_ID_PRECISION))    >>= nOldPrec;
     188           0 :         descriptor->getPropertyValue(rProp.getNameByIndex(PROPERTY_ID_PRECISION))>>= nNewPrec;
     189           0 :         xProp->getPropertyValue(rProp.getNameByIndex(PROPERTY_ID_SCALE))        >>= nOldScale;
     190           0 :         descriptor->getPropertyValue(rProp.getNameByIndex(PROPERTY_ID_SCALE))   >>= nNewScale;
     191             : 
     192             :         // second: check the "is nullable" value
     193           0 :         sal_Int32 nOldNullable = 0,nNewNullable = 0;
     194           0 :         xProp->getPropertyValue(rProp.getNameByIndex(PROPERTY_ID_ISNULLABLE))       >>= nOldNullable;
     195           0 :         descriptor->getPropertyValue(rProp.getNameByIndex(PROPERTY_ID_ISNULLABLE))  >>= nNewNullable;
     196             : 
     197             :         // check also the auto_increment
     198           0 :         sal_Bool bOldAutoIncrement = sal_False,bAutoIncrement = sal_False;
     199           0 :         xProp->getPropertyValue(rProp.getNameByIndex(PROPERTY_ID_ISAUTOINCREMENT))      >>= bOldAutoIncrement;
     200           0 :         descriptor->getPropertyValue(rProp.getNameByIndex(PROPERTY_ID_ISAUTOINCREMENT)) >>= bAutoIncrement;
     201             : 
     202             :         // now we should look if the name of the column changed
     203           0 :         OUString sNewColumnName;
     204           0 :         descriptor->getPropertyValue(rProp.getNameByIndex(PROPERTY_ID_NAME)) >>= sNewColumnName;
     205           0 :         if ( !sNewColumnName.equals(colName) )
     206             :         {
     207           0 :             const OUString sQuote = getMetaData()->getIdentifierQuoteString(  );
     208             : 
     209           0 :             OUString sSql = getAlterTableColumnPart();
     210           0 :             sSql += OUString(" ALTER COLUMN ");
     211           0 :             sSql += ::dbtools::quoteName(sQuote,colName);
     212             : 
     213           0 :             sSql += OUString(" RENAME TO ");
     214           0 :             sSql += ::dbtools::quoteName(sQuote,sNewColumnName);
     215             : 
     216           0 :             executeStatement(sSql);
     217             :         }
     218             : 
     219           0 :         if  (   nOldType != nNewType
     220           0 :             ||  sOldTypeName != sNewTypeName
     221           0 :             ||  nOldPrec != nNewPrec
     222           0 :             ||  nOldScale != nNewScale
     223           0 :             ||  nNewNullable != nOldNullable
     224           0 :             ||  bOldAutoIncrement != bAutoIncrement )
     225             :         {
     226             :             // special handling because they change the type names to distinguish
     227             :             // if a column should be an auto_incmrement one
     228           0 :             if ( bOldAutoIncrement != bAutoIncrement )
     229             :             {
     230             :                 /// TODO: insert special handling for auto increment "IDENTITY" and primary key
     231             :             }
     232           0 :             alterColumnType(nNewType,sNewColumnName,descriptor);
     233             :         }
     234             : 
     235             :         // third: check the default values
     236           0 :         OUString sNewDefault,sOldDefault;
     237           0 :         xProp->getPropertyValue(rProp.getNameByIndex(PROPERTY_ID_DEFAULTVALUE))     >>= sOldDefault;
     238           0 :         descriptor->getPropertyValue(rProp.getNameByIndex(PROPERTY_ID_DEFAULTVALUE)) >>= sNewDefault;
     239             : 
     240           0 :         if(!sOldDefault.isEmpty())
     241             :         {
     242           0 :             dropDefaultValue(colName);
     243           0 :             if(!sNewDefault.isEmpty() && sOldDefault != sNewDefault)
     244           0 :                 alterDefaultValue(sNewDefault,sNewColumnName);
     245             :         }
     246           0 :         else if(sOldDefault.isEmpty() && !sNewDefault.isEmpty())
     247           0 :             alterDefaultValue(sNewDefault,sNewColumnName);
     248             : 
     249           0 :         m_pColumns->refresh();
     250             :     }
     251             :     else
     252             :     {
     253           0 :         if(m_pColumns)
     254             :         {
     255           0 :             m_pColumns->dropByName(colName);
     256           0 :             m_pColumns->appendByDescriptor(descriptor);
     257             :         }
     258           0 :     }
     259             : 
     260           0 : }
     261             : // -----------------------------------------------------------------------------
     262           0 : void OHSQLTable::alterColumnType(sal_Int32 nNewType,const OUString& _rColName, const Reference<XPropertySet>& _xDescriptor)
     263             : {
     264           0 :     OUString sSql = getAlterTableColumnPart();
     265             : 
     266           0 :     sSql += OUString(" ALTER COLUMN ");
     267             : #if OSL_DEBUG_LEVEL > 0
     268             :     try
     269             :     {
     270             :         OUString sDescriptorName;
     271             :         OSL_ENSURE( _xDescriptor.is()
     272             :                 &&  ( _xDescriptor->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex( PROPERTY_ID_NAME ) ) >>= sDescriptorName )
     273             :                 &&  ( sDescriptorName == _rColName ),
     274             :                 "OHSQLTable::alterColumnType: unexpected column name!" );
     275             :     }
     276             :     catch( const Exception& )
     277             :     {
     278             :         DBG_UNHANDLED_EXCEPTION();
     279             :     }
     280             : #else
     281             :     (void)_rColName;
     282             : #endif
     283             : 
     284           0 :     OHSQLColumn* pColumn = new OHSQLColumn(sal_True);
     285           0 :     Reference<XPropertySet> xProp = pColumn;
     286           0 :     ::comphelper::copyProperties(_xDescriptor,xProp);
     287           0 :     xProp->setPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE),makeAny(nNewType));
     288             : 
     289           0 :     sSql += ::dbtools::createStandardColumnPart(xProp,getConnection());
     290           0 :     executeStatement(sSql);
     291           0 : }
     292             : // -----------------------------------------------------------------------------
     293           0 : void OHSQLTable::alterDefaultValue(const OUString& _sNewDefault,const OUString& _rColName)
     294             : {
     295           0 :     OUString sSql = getAlterTableColumnPart();
     296           0 :     sSql += OUString(" ALTER COLUMN ");
     297             : 
     298           0 :     const OUString sQuote = getMetaData()->getIdentifierQuoteString(  );
     299           0 :     sSql += ::dbtools::quoteName(sQuote,_rColName);
     300           0 :     sSql += OUString(" SET DEFAULT '") + _sNewDefault;
     301           0 :     sSql += OUString("'");
     302             : 
     303           0 :     executeStatement(sSql);
     304           0 : }
     305             : // -----------------------------------------------------------------------------
     306           0 : void OHSQLTable::dropDefaultValue(const OUString& _rColName)
     307             : {
     308           0 :     OUString sSql = getAlterTableColumnPart();
     309           0 :     sSql += OUString(" ALTER COLUMN ");
     310             : 
     311           0 :     const OUString sQuote = getMetaData()->getIdentifierQuoteString(  );
     312           0 :     sSql += ::dbtools::quoteName(sQuote,_rColName);
     313           0 :     sSql += OUString(" DROP DEFAULT");
     314             : 
     315           0 :     executeStatement(sSql);
     316           0 : }
     317             : // -----------------------------------------------------------------------------
     318           0 : OUString OHSQLTable::getAlterTableColumnPart()
     319             : {
     320           0 :     OUString sSql(  "ALTER TABLE " );
     321             : 
     322           0 :     OUString sComposedName( ::dbtools::composeTableName( getMetaData(), m_CatalogName, m_SchemaName, m_Name, sal_True, ::dbtools::eInTableDefinitions ) );
     323           0 :     sSql += sComposedName;
     324             : 
     325           0 :     return sSql;
     326             : }
     327             : // -----------------------------------------------------------------------------
     328           0 : void OHSQLTable::executeStatement(const OUString& _rStatement )
     329             : {
     330           0 :     OUString sSQL = _rStatement;
     331           0 :     if(sSQL.lastIndexOf(',') == (sSQL.getLength()-1))
     332           0 :         sSQL = sSQL.replaceAt(sSQL.getLength()-1,1,OUString(")"));
     333             : 
     334           0 :     Reference< XStatement > xStmt = getConnection()->createStatement(  );
     335           0 :     if ( xStmt.is() )
     336             :     {
     337           0 :         try { xStmt->execute(sSQL); }
     338           0 :         catch( const Exception& )
     339             :         {
     340           0 :             ::comphelper::disposeComponent(xStmt);
     341           0 :             throw;
     342             :         }
     343           0 :         ::comphelper::disposeComponent(xStmt);
     344           0 :     }
     345           0 : }
     346             : // -----------------------------------------------------------------------------
     347           0 : Sequence< Type > SAL_CALL OHSQLTable::getTypes(  ) throw(RuntimeException)
     348             : {
     349           0 :     if ( ! m_Type.compareToAscii("VIEW") )
     350             :     {
     351           0 :         Sequence< Type > aTypes = OTableHelper::getTypes();
     352           0 :         ::std::vector<Type> aOwnTypes;
     353           0 :         aOwnTypes.reserve(aTypes.getLength());
     354           0 :         const Type* pIter = aTypes.getConstArray();
     355           0 :         const Type* pEnd = pIter + aTypes.getLength();
     356           0 :         for(;pIter != pEnd;++pIter)
     357             :         {
     358           0 :             if( *pIter != ::getCppuType((const Reference<XRename>*)0) )
     359             :             {
     360           0 :                 aOwnTypes.push_back(*pIter);
     361             :             }
     362             :         }
     363           0 :         Type *pTypes = aOwnTypes.empty() ? 0 : &aOwnTypes[0];
     364           0 :         return Sequence< Type >(pTypes, aOwnTypes.size());
     365             :     }
     366           0 :     return OTableHelper::getTypes();
     367             : }
     368             : // -------------------------------------------------------------------------
     369             : // XRename
     370           0 : void SAL_CALL OHSQLTable::rename( const OUString& newName ) throw(SQLException, ElementExistException, RuntimeException)
     371             : {
     372           0 :     ::osl::MutexGuard aGuard(m_aMutex);
     373             :     checkDisposed(
     374             : #ifdef __GNUC__
     375             :         ::connectivity::sdbcx::OTableDescriptor_BASE::rBHelper.bDisposed
     376             : #else
     377             :         rBHelper.bDisposed
     378             : #endif
     379           0 :         );
     380             : 
     381           0 :     if(!isNew())
     382             :     {
     383           0 :         OUString sSql = OUString("ALTER ");
     384           0 :         if ( m_Type == OUString("VIEW") )
     385           0 :             sSql += OUString(" VIEW ");
     386             :         else
     387           0 :             sSql += OUString(" TABLE ");
     388             : 
     389           0 :         OUString sCatalog,sSchema,sTable;
     390           0 :         ::dbtools::qualifiedNameComponents(getMetaData(),newName,sCatalog,sSchema,sTable,::dbtools::eInDataManipulation);
     391             : 
     392             :         OUString sComposedName(
     393           0 :             ::dbtools::composeTableName( getMetaData(), m_CatalogName, m_SchemaName, m_Name, sal_True, ::dbtools::eInDataManipulation ) );
     394             :         sSql += sComposedName
     395           0 :             + OUString(" RENAME TO ");
     396           0 :         sSql += ::dbtools::composeTableName( getMetaData(), sCatalog, sSchema, sTable, sal_True, ::dbtools::eInDataManipulation );
     397             : 
     398           0 :         executeStatement(sSql);
     399             : 
     400           0 :         ::connectivity::OTable_TYPEDEF::rename(newName);
     401             :     }
     402             :     else
     403           0 :         ::dbtools::qualifiedNameComponents(getMetaData(),newName,m_CatalogName,m_SchemaName,m_Name,::dbtools::eInTableDefinitions);
     404           0 : }
     405             : 
     406             : // -------------------------------------------------------------------------
     407           0 : Any SAL_CALL OHSQLTable::queryInterface( const Type & rType ) throw(RuntimeException)
     408             : {
     409           0 :     if( !m_Type.compareToAscii("VIEW") && rType == ::getCppuType((const Reference<XRename>*)0) )
     410           0 :         return Any();
     411             : 
     412           0 :     return OTableHelper::queryInterface(rType);
     413             : }
     414             : // -------------------------------------------------------------------------
     415             : 
     416             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10