LCOV - code coverage report
Current view: top level - connectivity/source/drivers/hsqldb - HTable.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 173 0.0 %
Date: 2014-04-14 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,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             :                                     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, std::exception)
     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, std::exception)
     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 :                 " ALTER COLUMN " +
     211           0 :                 ::dbtools::quoteName(sQuote,colName) +
     212           0 :                 " RENAME TO " +
     213           0 :                 ::dbtools::quoteName(sQuote,sNewColumnName);
     214             : 
     215           0 :             executeStatement(sSql);
     216             :         }
     217             : 
     218           0 :         if  (   nOldType != nNewType
     219           0 :             ||  sOldTypeName != sNewTypeName
     220           0 :             ||  nOldPrec != nNewPrec
     221           0 :             ||  nOldScale != nNewScale
     222           0 :             ||  nNewNullable != nOldNullable
     223           0 :             ||  bOldAutoIncrement != bAutoIncrement )
     224             :         {
     225             :             // special handling because they change the type names to distinguish
     226             :             // if a column should be an auto_incmrement one
     227           0 :             if ( bOldAutoIncrement != bAutoIncrement )
     228             :             {
     229             :                 /// TODO: insert special handling for auto increment "IDENTITY" and primary key
     230             :             }
     231           0 :             alterColumnType(nNewType,sNewColumnName,descriptor);
     232             :         }
     233             : 
     234             :         // third: check the default values
     235           0 :         OUString sNewDefault,sOldDefault;
     236           0 :         xProp->getPropertyValue(rProp.getNameByIndex(PROPERTY_ID_DEFAULTVALUE))     >>= sOldDefault;
     237           0 :         descriptor->getPropertyValue(rProp.getNameByIndex(PROPERTY_ID_DEFAULTVALUE)) >>= sNewDefault;
     238             : 
     239           0 :         if(!sOldDefault.isEmpty())
     240             :         {
     241           0 :             dropDefaultValue(colName);
     242           0 :             if(!sNewDefault.isEmpty() && sOldDefault != sNewDefault)
     243           0 :                 alterDefaultValue(sNewDefault,sNewColumnName);
     244             :         }
     245           0 :         else if(sOldDefault.isEmpty() && !sNewDefault.isEmpty())
     246           0 :             alterDefaultValue(sNewDefault,sNewColumnName);
     247             : 
     248           0 :         m_pColumns->refresh();
     249             :     }
     250             :     else
     251             :     {
     252           0 :         if(m_pColumns)
     253             :         {
     254           0 :             m_pColumns->dropByName(colName);
     255           0 :             m_pColumns->appendByDescriptor(descriptor);
     256             :         }
     257           0 :     }
     258             : 
     259           0 : }
     260             : 
     261           0 : void OHSQLTable::alterColumnType(sal_Int32 nNewType,const OUString& _rColName, const Reference<XPropertySet>& _xDescriptor)
     262             : {
     263           0 :     OUString sSql = getAlterTableColumnPart();
     264             : 
     265           0 :     sSql += " ALTER COLUMN ";
     266             : #if OSL_DEBUG_LEVEL > 0
     267             :     try
     268             :     {
     269             :         OUString sDescriptorName;
     270             :         OSL_ENSURE( _xDescriptor.is()
     271             :                 &&  ( _xDescriptor->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex( PROPERTY_ID_NAME ) ) >>= sDescriptorName )
     272             :                 &&  ( sDescriptorName == _rColName ),
     273             :                 "OHSQLTable::alterColumnType: unexpected column name!" );
     274             :     }
     275             :     catch( const Exception& )
     276             :     {
     277             :         DBG_UNHANDLED_EXCEPTION();
     278             :     }
     279             : #else
     280             :     (void)_rColName;
     281             : #endif
     282             : 
     283           0 :     OHSQLColumn* pColumn = new OHSQLColumn(sal_True);
     284           0 :     Reference<XPropertySet> xProp = pColumn;
     285           0 :     ::comphelper::copyProperties(_xDescriptor,xProp);
     286           0 :     xProp->setPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE),makeAny(nNewType));
     287             : 
     288           0 :     sSql += ::dbtools::createStandardColumnPart(xProp,getConnection());
     289           0 :     executeStatement(sSql);
     290           0 : }
     291             : 
     292           0 : void OHSQLTable::alterDefaultValue(const OUString& _sNewDefault,const OUString& _rColName)
     293             : {
     294           0 :     const OUString sQuote = getMetaData()->getIdentifierQuoteString(  );
     295           0 :     OUString sSql = getAlterTableColumnPart() +
     296           0 :         " ALTER COLUMN " +
     297           0 :         ::dbtools::quoteName(sQuote,_rColName) +
     298           0 :         " SET DEFAULT '" + _sNewDefault + "'";
     299             : 
     300           0 :     executeStatement(sSql);
     301           0 : }
     302             : 
     303           0 : void OHSQLTable::dropDefaultValue(const OUString& _rColName)
     304             : {
     305           0 :     const OUString sQuote = getMetaData()->getIdentifierQuoteString(  );
     306           0 :     OUString sSql = getAlterTableColumnPart() +
     307           0 :         " ALTER COLUMN " +
     308           0 :         ::dbtools::quoteName(sQuote,_rColName) +
     309           0 :         " DROP DEFAULT";
     310             : 
     311           0 :     executeStatement(sSql);
     312           0 : }
     313             : 
     314           0 : OUString OHSQLTable::getAlterTableColumnPart()
     315             : {
     316           0 :     OUString sSql(  "ALTER TABLE " );
     317             : 
     318           0 :     OUString sComposedName( ::dbtools::composeTableName( getMetaData(), m_CatalogName, m_SchemaName, m_Name, true, ::dbtools::eInTableDefinitions ) );
     319           0 :     sSql += sComposedName;
     320             : 
     321           0 :     return sSql;
     322             : }
     323             : 
     324           0 : void OHSQLTable::executeStatement(const OUString& _rStatement )
     325             : {
     326           0 :     OUString sSQL = _rStatement;
     327           0 :     if(sSQL.endsWith(","))
     328           0 :         sSQL = sSQL.replaceAt(sSQL.getLength()-1, 1, ")");
     329             : 
     330           0 :     Reference< XStatement > xStmt = getConnection()->createStatement(  );
     331           0 :     if ( xStmt.is() )
     332             :     {
     333           0 :         try { xStmt->execute(sSQL); }
     334           0 :         catch( const Exception& )
     335             :         {
     336           0 :             ::comphelper::disposeComponent(xStmt);
     337           0 :             throw;
     338             :         }
     339           0 :         ::comphelper::disposeComponent(xStmt);
     340           0 :     }
     341           0 : }
     342             : 
     343           0 : Sequence< Type > SAL_CALL OHSQLTable::getTypes(  ) throw(RuntimeException, std::exception)
     344             : {
     345           0 :     if ( m_Type.equalsAscii("VIEW") )
     346             :     {
     347           0 :         Sequence< Type > aTypes = OTableHelper::getTypes();
     348           0 :         ::std::vector<Type> aOwnTypes;
     349           0 :         aOwnTypes.reserve(aTypes.getLength());
     350           0 :         const Type* pIter = aTypes.getConstArray();
     351           0 :         const Type* pEnd = pIter + aTypes.getLength();
     352           0 :         for(;pIter != pEnd;++pIter)
     353             :         {
     354           0 :             if( *pIter != ::getCppuType((const Reference<XRename>*)0) )
     355             :             {
     356           0 :                 aOwnTypes.push_back(*pIter);
     357             :             }
     358             :         }
     359           0 :         Type *pTypes = aOwnTypes.empty() ? 0 : &aOwnTypes[0];
     360           0 :         return Sequence< Type >(pTypes, aOwnTypes.size());
     361             :     }
     362           0 :     return OTableHelper::getTypes();
     363             : }
     364             : 
     365             : // XRename
     366           0 : void SAL_CALL OHSQLTable::rename( const OUString& newName ) throw(SQLException, ElementExistException, RuntimeException, std::exception)
     367             : {
     368           0 :     ::osl::MutexGuard aGuard(m_aMutex);
     369             :     checkDisposed(
     370             : #ifdef __GNUC__
     371             :         ::connectivity::sdbcx::OTableDescriptor_BASE::rBHelper.bDisposed
     372             : #else
     373             :         rBHelper.bDisposed
     374             : #endif
     375           0 :         );
     376             : 
     377           0 :     if(!isNew())
     378             :     {
     379           0 :         OUString sSql = "ALTER ";
     380           0 :         if ( m_Type == "VIEW" )
     381           0 :             sSql += " VIEW ";
     382             :         else
     383           0 :             sSql += " TABLE ";
     384             : 
     385           0 :         OUString sCatalog,sSchema,sTable;
     386           0 :         ::dbtools::qualifiedNameComponents(getMetaData(),newName,sCatalog,sSchema,sTable,::dbtools::eInDataManipulation);
     387             : 
     388             :         sSql +=
     389           0 :             ::dbtools::composeTableName( getMetaData(), m_CatalogName, m_SchemaName, m_Name, true, ::dbtools::eInDataManipulation )
     390           0 :             + " RENAME TO "
     391           0 :             + ::dbtools::composeTableName( getMetaData(), sCatalog, sSchema, sTable, true, ::dbtools::eInDataManipulation );
     392             : 
     393           0 :         executeStatement(sSql);
     394             : 
     395           0 :         ::connectivity::OTable_TYPEDEF::rename(newName);
     396             :     }
     397             :     else
     398           0 :         ::dbtools::qualifiedNameComponents(getMetaData(),newName,m_CatalogName,m_SchemaName,m_Name,::dbtools::eInTableDefinitions);
     399           0 : }
     400             : 
     401             : 
     402           0 : Any SAL_CALL OHSQLTable::queryInterface( const Type & rType ) throw(RuntimeException, std::exception)
     403             : {
     404           0 :     if( m_Type.equalsAscii("VIEW") && rType == ::getCppuType((const Reference<XRename>*)0) )
     405           0 :         return Any();
     406             : 
     407           0 :     return OTableHelper::queryInterface(rType);
     408             : }
     409             : 
     410             : 
     411             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10