LCOV - code coverage report
Current view: top level - connectivity/source/drivers/postgresql - pq_xcolumns.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 163 0.0 %
Date: 2012-08-25 Functions: 0 14 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                 :            : #include <rtl/strbuf.hxx>
      60                 :            : 
      61                 :            : #include <com/sun/star/sdbc/XRow.hpp>
      62                 :            : #include <com/sun/star/sdbc/XParameters.hpp>
      63                 :            : #include <com/sun/star/sdbc/DataType.hpp>
      64                 :            : #include <com/sun/star/sdbc/ColumnValue.hpp>
      65                 :            : 
      66                 :            : #include <cppuhelper/implbase1.hxx>
      67                 :            : 
      68                 :            : #include "pq_xcolumns.hxx"
      69                 :            : #include "pq_xcolumn.hxx"
      70                 :            : #include "pq_statics.hxx"
      71                 :            : #include "pq_tools.hxx"
      72                 :            : 
      73                 :            : using osl::MutexGuard;
      74                 :            : 
      75                 :            : using rtl::OUString;
      76                 :            : using rtl::OUStringBuffer;
      77                 :            : using rtl::OUStringToOString;
      78                 :            : 
      79                 :            : using com::sun::star::beans::XPropertySet;
      80                 :            : using com::sun::star::beans::XPropertyChangeListener;
      81                 :            : using com::sun::star::beans::PropertyChangeEvent;
      82                 :            : 
      83                 :            : using com::sun::star::uno::Any;
      84                 :            : using com::sun::star::uno::makeAny;
      85                 :            : using com::sun::star::uno::UNO_QUERY;
      86                 :            : using com::sun::star::uno::Type;
      87                 :            : using com::sun::star::uno::XInterface;
      88                 :            : using com::sun::star::uno::Reference;
      89                 :            : using com::sun::star::uno::Sequence;
      90                 :            : using com::sun::star::uno::RuntimeException;
      91                 :            : 
      92                 :            : using com::sun::star::container::NoSuchElementException;
      93                 :            : using com::sun::star::lang::WrappedTargetException;
      94                 :            : 
      95                 :            : using com::sun::star::sdbc::XRow;
      96                 :            : using com::sun::star::sdbc::XCloseable;
      97                 :            : using com::sun::star::sdbc::XStatement;
      98                 :            : using com::sun::star::sdbc::XResultSet;
      99                 :            : using com::sun::star::sdbc::XParameters;
     100                 :            : using com::sun::star::sdbc::XPreparedStatement;
     101                 :            : using com::sun::star::sdbc::XDatabaseMetaData;
     102                 :            : using com::sun::star::sdbc::SQLException;
     103                 :            : 
     104                 :            : namespace pq_sdbc_driver
     105                 :            : {
     106                 :            : #define ASCII_STR(x) OUString( RTL_CONSTASCII_USTRINGPARAM( x ) )
     107                 :            : 
     108                 :          0 : static Any isCurrency( const rtl::OUString & typeName )
     109                 :            : {
     110                 :          0 :     sal_Bool b = typeName.equalsIgnoreAsciiCaseAsciiL(RTL_CONSTASCII_STRINGPARAM("money"));
     111                 :          0 :     return Any( &b, getBooleanCppuType() );
     112                 :            : }
     113                 :            : 
     114                 :            : // static sal_Bool isAutoIncrement8( const rtl::OUString & typeName )
     115                 :            : // {
     116                 :            : //     return typeName.equalsIgnoreAsciiCaseAsciiL(RTL_CONSTASCII_STRINGPARAM("serial8")) ||
     117                 :            : //         typeName.equalsIgnoreAsciiCaseAsciiL(RTL_CONSTASCII_STRINGPARAM("bigserial"));
     118                 :            : // }
     119                 :            : 
     120                 :          0 : static Any isAutoIncrement( const rtl::OUString & defaultValue )
     121                 :            : {
     122                 :          0 :     sal_Bool ret = sal_False;
     123                 :            : 
     124                 :          0 :     ret = defaultValue.matchAsciiL( RTL_CONSTASCII_STRINGPARAM( "nextval(" ) );
     125                 :            : //     printf( "%s %d\n",
     126                 :            : //             OUStringToOString(defaultValue, RTL_TEXTENCODING_ASCII_US).getStr(),
     127                 :            : //             ret );
     128                 :            : //     {
     129                 :            : //     static const char  * const serials[] =
     130                 :            : //         {
     131                 :            : //             "serial", "serial4", "serial8", "bigserial", 0
     132                 :            : //         };
     133                 :            : // s    sal_Bool b = sal_False;
     134                 :            : //     for( int i = 0; !b && serials[i] ; i ++ )
     135                 :            : //     {
     136                 :            : //         b = b || typeName.equalsIgnoreAsciiCaseAscii( serials[i] );
     137                 :            : //     }
     138                 :          0 :     return Any ( &ret, getBooleanCppuType() );
     139                 :            : }
     140                 :            : 
     141                 :          0 : Columns::Columns(
     142                 :            :         const ::rtl::Reference< RefCountedMutex > & refMutex,
     143                 :            :         const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection >  & origin,
     144                 :            :         ConnectionSettings *pSettings,
     145                 :            :         const rtl::OUString &schemaName,
     146                 :            :         const rtl::OUString &tableName)
     147                 :            :     : Container( refMutex, origin, pSettings,  ASCII_STR( "COLUMN" ) ),
     148                 :            :       m_schemaName( schemaName ),
     149                 :          0 :       m_tableName( tableName )
     150                 :          0 : {}
     151                 :            : 
     152                 :          0 : Columns::~Columns()
     153                 :          0 : {}
     154                 :            : 
     155                 :          0 : rtl::OUString columnMetaData2SDBCX(
     156                 :            :     ReflectionBase *pBase, const com::sun::star::uno::Reference< com::sun::star::sdbc::XRow > &xRow )
     157                 :            : {
     158                 :          0 :     Statics & st = getStatics();
     159                 :            : 
     160                 :            :     //  1. TABLE_CAT string => table catalog (may be NULL)
     161                 :            :     //               => not supported
     162                 :            :     //  2. TABLE_SCHEM string => table schema (may be NULL)
     163                 :            :     //               => pg_namespace.nspname
     164                 :            :     //  3. TABLE_NAME string => table name
     165                 :            :     //               => pg_class.relname
     166                 :            :     //  4. COLUMN_NAME string => column name
     167                 :            :     //               => pg_attribure.attname
     168                 :            :     //  5. DATA_TYPE short => SQL type from java.sql.Types
     169                 :            :     //               => pg_type.typname => sdbc.DataType
     170                 :            :     //  6. TYPE_NAME string => Data source dependent type name, for a UDT the
     171                 :            :     //                         type name is fully qualified
     172                 :            :     //               => pg_type.typname
     173                 :            :     //  7. COLUMN_SIZE long => column size. For char or date types this is
     174                 :            :     //                         the maximum number of characters, for numeric
     175                 :            :     //                         or decimal types this is precision.
     176                 :            :     //               => pg_type.typlen ( TODO: What is about variable size ? )
     177                 :            :     //  8. BUFFER_LENGTH is not used.
     178                 :            :     //               => not used
     179                 :            :     //  9. DECIMAL_DIGITS long => the number of fractional digits
     180                 :            :     //               => don't know ! TODO !
     181                 :            :     //  10. NUM_PREC_RADIX long => Radix (typically either 10 or 2)
     182                 :            :     //               => TODO ??
     183                 :            :     //  11. NULLABLE long => is NULL allowed?
     184                 :            :     //                      NO_NULLS - might not allow NULL values
     185                 :            :     //                      NULABLE - definitely allows NULL values
     186                 :            :     //                      NULLABLE_UNKNOWN - nullability unknown
     187                 :            :     //               => pg_attribute.attnotnull
     188                 :            :     //  12. REMARKS string => comment describing column (may be NULL )
     189                 :            :     //               => Don't know, there does not seem to exist something like
     190                 :            :     //                  that in postgres
     191                 :            :     //  13. COLUMN_DEF string => default value (may be NULL)
     192                 :            :     //               => pg_type.typdefault
     193                 :            :     //  14. SQL_DATA_TYPE long => unused
     194                 :            :     //               => empty
     195                 :            :     //  15. SQL_DATETIME_SUB long => unused
     196                 :            :     //               => empty
     197                 :            :     //  16. CHAR_OCTET_LENGTH long => for char types the maximum number of
     198                 :            :     //                                bytes in the column
     199                 :            :     //               => pg_type.typlen
     200                 :            :     //  17. ORDINAL_POSITION int => index of column in table (starting at 1)
     201                 :            :     //                              pg_attribute.attnum
     202                 :            :     //  18. IS_NULLABLE string => "NO" means column definitely does not allow
     203                 :            :     //                            NULL values; "YES" means the column might
     204                 :            :     //                            allow NULL values. An empty string means
     205                 :            :     //                            nobody knows.
     206                 :            :     //               => pg_attribute.attnotnull
     207                 :            : 
     208                 :            :     static const int COLUMN_NAME = 4;
     209                 :            :     static const int DATA_TYPE = 5;
     210                 :            :     static const int TYPE_NAME = 6;
     211                 :            :     static const int COLUMN_SIZE = 7;
     212                 :            :     static const int DECIMAL_DIGITS = 9;
     213                 :            :     static const int IS_NULLABLE = 11;
     214                 :            :     static const int DESCRIPTION = 12;
     215                 :            :     static const int DEFAULT_VALUE = 13;
     216                 :            : 
     217                 :          0 :     OUString name = xRow->getString( COLUMN_NAME );
     218                 :          0 :     OUString typeName  = xRow->getString( TYPE_NAME );
     219                 :            : 
     220                 :            :     pBase->setPropertyValue_NoBroadcast_public(
     221                 :          0 :         st.NAME, makeAny( name ) );
     222                 :            : 
     223                 :            :     pBase->setPropertyValue_NoBroadcast_public(
     224                 :          0 :         st.TYPE, makeAny( xRow->getInt( DATA_TYPE ) ) );
     225                 :            : 
     226                 :            :     pBase->setPropertyValue_NoBroadcast_public(
     227                 :          0 :         st.TYPE_NAME, makeAny( typeName ) );
     228                 :            : 
     229                 :            :     pBase->setPropertyValue_NoBroadcast_public(
     230                 :          0 :         st.PRECISION, makeAny( xRow->getInt( COLUMN_SIZE ) ) );
     231                 :            : 
     232                 :            :     pBase->setPropertyValue_NoBroadcast_public(
     233                 :          0 :         st.SCALE, makeAny( xRow->getInt( DECIMAL_DIGITS ) ) );
     234                 :            : 
     235                 :            :     pBase->setPropertyValue_NoBroadcast_public(
     236                 :          0 :         st.IS_NULLABLE, makeAny( xRow->getInt( IS_NULLABLE ) ) );
     237                 :            : 
     238                 :            :     pBase->setPropertyValue_NoBroadcast_public(
     239                 :          0 :         st.DEFAULT_VALUE, makeAny( xRow->getString( DEFAULT_VALUE ) ) );
     240                 :            : 
     241                 :            : //     pBase->setPropertyValue_NoBroadcast_public(
     242                 :            : //         st.DESCRIPTION, makeAny( xRow->getString( DESCRIPTION ) ) );
     243                 :            : 
     244                 :            : //     if( pBase->getPropertySetInfo()->hasPropertyByName( st.HELP_TEXT ) )
     245                 :            : //         pBase->setPropertyValue_NoBroadcast_public(
     246                 :            : //             st.HELP_TEXT, makeAny( xRow->getString( DESCRIPTION ) ) );
     247                 :            : //     else // for key columns, etc. ...
     248                 :            :         pBase->setPropertyValue_NoBroadcast_public(
     249                 :          0 :             st.DESCRIPTION, makeAny( xRow->getString( DESCRIPTION ) ) );
     250                 :            : 
     251                 :            : 
     252                 :            :     // maybe a better criterium than the type name can be found in future
     253                 :            :     pBase->setPropertyValue_NoBroadcast_public(
     254                 :          0 :         st.IS_AUTO_INCREMENT, isAutoIncrement(xRow->getString( DEFAULT_VALUE )) );
     255                 :            : 
     256                 :            :     pBase->setPropertyValue_NoBroadcast_public(
     257                 :          0 :         st.IS_CURRENCY, isCurrency( typeName));
     258                 :          0 :     return name;
     259                 :            : }
     260                 :            : 
     261                 :            : 
     262                 :            : // class CommentChanger : public cppu::WeakImplHelper1< XPropertyChangeListener >
     263                 :            : // {
     264                 :            : //     ::rtl::Reference< RefCountedMutex > m_refMutex;
     265                 :            : //     ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection > m_connection;
     266                 :            : //     ConnectionSettings *m_pSettings;
     267                 :            : //     rtl::OUString m_schema;
     268                 :            : //     rtl::OUString m_table;
     269                 :            : //     rtl::OUString m_column;
     270                 :            : 
     271                 :            : // public:
     272                 :            : //     CommentChanger(
     273                 :            : //         const ::rtl::Reference< RefCountedMutex > & refMutex,
     274                 :            : //         const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection > & connection,
     275                 :            : //         ConnectionSettings *pSettings,
     276                 :            : //         const rtl::OUString & schema,
     277                 :            : //         const rtl::OUString & table,
     278                 :            : //         const rtl::OUString & column ) :
     279                 :            : //         m_refMutex( refMutex ),
     280                 :            : //         m_connection( connection ),
     281                 :            : //         m_pSettings( pSettings ),
     282                 :            : //         m_schema ( schema ),
     283                 :            : //         m_table ( table ),
     284                 :            : //         m_column ( column )
     285                 :            : //     {}
     286                 :            : 
     287                 :            : 
     288                 :            : //     // Methods
     289                 :            : //     virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException)
     290                 :            : //     {
     291                 :            : //         osl::MutexGuard guard( m_refMutex->mutex );
     292                 :            : //         m_connection.clear();
     293                 :            : //     }
     294                 :            : //         // Methods
     295                 :            : //     virtual void SAL_CALL propertyChange( const ::com::sun::star::beans::PropertyChangeEvent& evt ) throw (::com::sun::star::uno::RuntimeException)
     296                 :            : //     {
     297                 :            : //         osl::MutexGuard guard( m_refMutex->mutex );
     298                 :            : //         OUStringBuffer buf( 128 );
     299                 :            : //         OUString comment;
     300                 :            : //         evt.NewValue >>= comment;
     301                 :            : //         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "COMMENT ON COLUMN" ) );
     302                 :            : //         bufferQuoteQualifiedIdentifier( buf, m_schema, m_table , m_column );
     303                 :            : //         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "IS " ) );
     304                 :            : //         bufferQuoteConstant( buf, comment,m_pSettings->encoding);
     305                 :            : 
     306                 :            : //         printf( "changing comment of column %s to %s\n",
     307                 :            : //                 OUStringToOString( m_column, RTL_TEXTENCODING_ASCII_US ).getStr(),
     308                 :            : //                 OUStringToOString( comment, RTL_TEXTENCODING_ASCII_US ).getStr() );
     309                 :            : 
     310                 :            : //         m_connection->createStatement()->executeUpdate( buf.makeStringAndClear() );
     311                 :            : //     }
     312                 :            : // };
     313                 :            : 
     314                 :          0 : void Columns::refresh()
     315                 :            :     throw (::com::sun::star::uno::RuntimeException)
     316                 :            : {
     317                 :            :     try
     318                 :            :     {
     319                 :          0 :         if( isLog( m_pSettings, LogLevel::INFO ) )
     320                 :            :         {
     321                 :          0 :             rtl::OStringBuffer buf;
     322                 :          0 :             buf.append( "sdbcx.Columns get refreshed for table " );
     323                 :          0 :             buf.append( OUStringToOString( m_schemaName, m_pSettings->encoding ) );
     324                 :          0 :             buf.append( "." );
     325                 :          0 :             buf.append( OUStringToOString( m_tableName, m_pSettings->encoding ) );
     326                 :          0 :             log( m_pSettings, LogLevel::INFO, buf.makeStringAndClear().getStr() );
     327                 :            :         }
     328                 :          0 :         osl::MutexGuard guard( m_refMutex->mutex );
     329                 :            : 
     330                 :          0 :         Statics &st = getStatics();
     331                 :          0 :         Reference< XDatabaseMetaData > meta = m_origin->getMetaData();
     332                 :            : 
     333                 :            :         Reference< XResultSet > rs =
     334                 :          0 :             meta->getColumns( Any(), m_schemaName, m_tableName, st.cPERCENT );
     335                 :            : 
     336                 :          0 :         DisposeGuard disposeIt( rs );
     337                 :          0 :         Reference< XRow > xRow( rs , UNO_QUERY );
     338                 :            : 
     339                 :          0 :         String2IntMap map;
     340                 :            : 
     341                 :          0 :         m_values = Sequence< com::sun::star::uno::Any > ();
     342                 :          0 :         int columnIndex = 0;
     343                 :          0 :         while( rs->next() )
     344                 :            :         {
     345                 :            :             Column * pColumn =
     346                 :          0 :                 new Column( m_refMutex, m_origin, m_pSettings );
     347                 :          0 :             Reference< com::sun::star::beans::XPropertySet > prop = pColumn;
     348                 :            : 
     349                 :          0 :             OUString name = columnMetaData2SDBCX( pColumn, xRow );
     350                 :            : //             pColumn->addPropertyChangeListener(
     351                 :            : //                 st.HELP_TEXT,
     352                 :            : //                 new CommentChanger(
     353                 :            : //                     m_refMutex,
     354                 :            : //                     m_origin,
     355                 :            : //                     m_pSettings,
     356                 :            : //                     m_schemaName,
     357                 :            : //                     m_tableName,
     358                 :            : //                     name ) );
     359                 :            : 
     360                 :            :             {
     361                 :          0 :                 const int currentColumnIndex = columnIndex++;
     362                 :            :                 assert(currentColumnIndex  == m_values.getLength());
     363                 :          0 :                 m_values.realloc( columnIndex );
     364                 :          0 :                 m_values[currentColumnIndex] = makeAny( prop );
     365                 :          0 :                 map[ name ] = currentColumnIndex;
     366                 :            :             }
     367                 :          0 :         }
     368                 :          0 :         m_name2index.swap( map );
     369                 :            :     }
     370                 :          0 :     catch ( com::sun::star::sdbc::SQLException & e )
     371                 :            :     {
     372                 :          0 :         throw RuntimeException( e.Message , e.Context );
     373                 :            :     }
     374                 :          0 :     fire( RefreshedBroadcaster( *this ) );
     375                 :          0 : }
     376                 :            : 
     377                 :            : 
     378                 :          0 : void alterColumnByDescriptor(
     379                 :            :     const OUString & schemaName,
     380                 :            :     const OUString & tableName,
     381                 :            :     ConnectionSettings *settings,
     382                 :            :     const Reference< XStatement > &stmt,
     383                 :            :     const com::sun::star::uno::Reference< com::sun::star::beans::XPropertySet > & past,
     384                 :            :     const com::sun::star::uno::Reference< com::sun::star::beans::XPropertySet > & future)
     385                 :            : {
     386                 :          0 :     Statics & st  = getStatics();
     387                 :            : 
     388                 :            : //     if( past->getPropertyValue( st.TABLE_NAME ) != future->getPropertyValue( st.TABLE_NAME ) ||
     389                 :            : //         past->getPropertyValue( st.SCHEMA_NAME ) != future->getPropertyValue( st.SCHEMA_NAME ))
     390                 :            : //     {
     391                 :            : //         OUStringBuffer buf(128);
     392                 :            : //         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "Can't move column " ) );
     393                 :            : //         buf.append( extractStringProperty( past, st.COLUMN_NAME ) );
     394                 :            : //         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( " from table " ) );
     395                 :            : //         buf.append( extractStringProperty( past, st.TABLE_NAME ) );
     396                 :            : //         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( " to table " ) );
     397                 :            : //         buf.append( extractStringProperty( past, st.TABLE_NAME ) );
     398                 :            : //         throw SQLException( buf.makeStringAndClear(), Reference< XInterface > () );
     399                 :            : //     }
     400                 :            : 
     401                 :            : //     OUString tableName = extractStringProperty( past, st.TABLE_NAME );
     402                 :            : //     OUString schemaName = extractStringProperty( past, st.SCHEMA_NAME );
     403                 :          0 :     OUString pastColumnName = extractStringProperty( past, st.NAME );
     404                 :          0 :     OUString futureColumnName = extractStringProperty( future, st.NAME );
     405                 :          0 :     OUString pastTypeName = sqltype2string( past );
     406                 :          0 :     OUString futureTypeName = sqltype2string( future );
     407                 :            : 
     408                 :          0 :     TransactionGuard transaction( stmt );
     409                 :            : 
     410                 :          0 :     OUStringBuffer buf( 128 );
     411                 :          0 :     if( ! pastColumnName.getLength())
     412                 :            :     {
     413                 :            :         // create a new column
     414                 :          0 :         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER TABLE" ) );
     415                 :          0 :         bufferQuoteQualifiedIdentifier( buf, schemaName, tableName, settings );
     416                 :          0 :         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ADD COLUMN" ) );
     417                 :          0 :         bufferQuoteIdentifier( buf, futureColumnName, settings );
     418                 :          0 :         buf.append( futureTypeName );
     419                 :          0 :         transaction.executeUpdate( buf.makeStringAndClear() );
     420                 :            :     }
     421                 :            :     else
     422                 :            :     {
     423                 :          0 :         if( pastTypeName != futureTypeName )
     424                 :            :         {
     425                 :            :             throw RuntimeException(
     426                 :            :                 ASCII_STR( "Can't modify column types, drop the column and create a new one" ),
     427                 :          0 :                 Reference< XInterface > () );
     428                 :            :         }
     429                 :            : 
     430                 :          0 :         if( pastColumnName != futureColumnName )
     431                 :            :         {
     432                 :          0 :             buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER TABLE" ) );
     433                 :          0 :             bufferQuoteQualifiedIdentifier( buf, schemaName, tableName, settings );
     434                 :          0 :             buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "RENAME COLUMN" ) );
     435                 :          0 :             bufferQuoteIdentifier( buf, pastColumnName, settings );
     436                 :          0 :             buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "TO" ) );
     437                 :          0 :             bufferQuoteIdentifier( buf, futureColumnName, settings );
     438                 :          0 :             transaction.executeUpdate( buf.makeStringAndClear() );
     439                 :            :         }
     440                 :            :     }
     441                 :            : 
     442                 :          0 :     OUString futureDefaultValue = extractStringProperty( future, st.DEFAULT_VALUE );
     443                 :          0 :     OUString pastDefaultValue = extractStringProperty( past, st.DEFAULT_VALUE );
     444                 :          0 :     if( futureDefaultValue != pastDefaultValue )
     445                 :            :     {
     446                 :          0 :         buf = OUStringBuffer( 128 );
     447                 :          0 :         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER TABLE" ) );
     448                 :          0 :         bufferQuoteQualifiedIdentifier( buf, schemaName, tableName, settings );
     449                 :          0 :         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER COLUMN" ) );
     450                 :          0 :         bufferQuoteIdentifier( buf, futureColumnName, settings );
     451                 :          0 :         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "SET DEFAULT " ) );
     452                 :            :         // LEM TODO: check out
     453                 :            :         // default value is not quoted, caller needs to quote himself (otherwise
     454                 :            :         // how to pass e.g. nextval('something' ) ????
     455                 :          0 :         buf.append( futureDefaultValue );
     456                 :            : //        bufferQuoteConstant( buf, defaultValue, encoding );
     457                 :          0 :         transaction.executeUpdate( buf.makeStringAndClear() );
     458                 :            :     }
     459                 :            : 
     460                 :          0 :     sal_Int32 futureNullable = extractIntProperty( future, st.IS_NULLABLE );
     461                 :          0 :     sal_Int32 pastNullable = extractIntProperty( past, st.IS_NULLABLE );
     462                 :          0 :     if( futureNullable != pastNullable )
     463                 :            :     {
     464                 :          0 :         buf = OUStringBuffer( 128 );
     465                 :          0 :         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER TABLE" ) );
     466                 :          0 :         bufferQuoteQualifiedIdentifier( buf, schemaName, tableName, settings );
     467                 :          0 :         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "ALTER COLUMN" ) );
     468                 :          0 :         bufferQuoteIdentifier( buf, futureColumnName, settings );
     469                 :          0 :         if( futureNullable == com::sun::star::sdbc::ColumnValue::NO_NULLS )
     470                 :            :         {
     471                 :          0 :             buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "SET" ) );
     472                 :            :         }
     473                 :            :         else
     474                 :            :         {
     475                 :          0 :             buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "DROP" ) );
     476                 :            :         }
     477                 :          0 :         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( " NOT NULL" ) );
     478                 :          0 :         transaction.executeUpdate( buf.makeStringAndClear() );
     479                 :            :     }
     480                 :            : 
     481                 :            : //     OUString futureComment = extractStringProperty( future, st.HELP_TEXT );
     482                 :            : //     OUString pastComment = extractStringProperty( past, st.HELP_TEXT );
     483                 :            : //     printf( "past Comment %s, futureComment %s\n",
     484                 :            : //             OUStringToOString( pastComment, RTL_TEXTENCODING_ASCII_US ).getStr(),
     485                 :            : //             OUStringToOString( futureComment, RTL_TEXTENCODING_ASCII_US ).getStr() );
     486                 :          0 :     OUString futureComment = extractStringProperty( future, st.DESCRIPTION );
     487                 :          0 :     OUString pastComment = extractStringProperty( past, st.DESCRIPTION );
     488                 :            : 
     489                 :          0 :     if( futureComment != pastComment )
     490                 :            :     {
     491                 :          0 :         buf = OUStringBuffer( 128 );
     492                 :          0 :         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "COMMENT ON COLUMN" ) );
     493                 :          0 :         bufferQuoteQualifiedIdentifier( buf, schemaName, tableName , futureColumnName, settings );
     494                 :          0 :         buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "IS " ) );
     495                 :          0 :         bufferQuoteConstant( buf, futureComment, settings );
     496                 :          0 :         transaction.executeUpdate( buf.makeStringAndClear() );
     497                 :            :     }
     498                 :          0 :     transaction.commit();
     499                 :          0 : }
     500                 :            : 
     501                 :          0 : void Columns::appendByDescriptor(
     502                 :            :     const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& future )
     503                 :            :     throw (::com::sun::star::sdbc::SQLException,
     504                 :            :            ::com::sun::star::container::ElementExistException,
     505                 :            :            ::com::sun::star::uno::RuntimeException)
     506                 :            : {
     507                 :          0 :     osl::MutexGuard guard( m_refMutex->mutex );
     508                 :          0 :     Statics & st = getStatics();
     509                 :          0 :     Reference< XPropertySet > past = createDataDescriptor();
     510                 :          0 :     past->setPropertyValue( st.IS_NULLABLE, makeAny( com::sun::star::sdbc::ColumnValue::NULLABLE ) );
     511                 :            :     alterColumnByDescriptor(
     512                 :          0 :         m_schemaName, m_tableName, m_pSettings, m_origin->createStatement() , past, future  );
     513                 :            : 
     514                 :          0 :     refresh();
     515                 :          0 : }
     516                 :            : 
     517                 :            : // void Columns::dropByName( const ::rtl::OUString& elementName )
     518                 :            : //     throw (::com::sun::star::sdbc::SQLException,
     519                 :            : //            ::com::sun::star::container::NoSuchElementException,
     520                 :            : //            ::com::sun::star::uno::RuntimeException)
     521                 :            : // {
     522                 :            : //     String2IntMap::const_iterator ii = m_name2index.find( elementName );
     523                 :            : //     if( ii == m_name2index.end() )
     524                 :            : //     {
     525                 :            : //         OUStringBuffer buf( 128 );
     526                 :            : //         buf.appendAscii( "Column " );
     527                 :            : //         buf.append( elementName );
     528                 :            : //         buf.appendAscii( " is unknown in table " );
     529                 :            : //         buf.append( m_schemaName );
     530                 :            : //         buf.appendAscii( "." );
     531                 :            : //         buf.append( m_tableName );
     532                 :            : //         buf.appendAscii( ", so it can't be dropped" );
     533                 :            : //         throw com::sun::star::container::NoSuchElementException(
     534                 :            : //             buf.makeStringAndClear(), *this );
     535                 :            : //     }
     536                 :            : //     dropByIndex( ii->second );
     537                 :            : // }
     538                 :            : 
     539                 :          0 : void Columns::dropByIndex( sal_Int32 index )
     540                 :            :     throw (::com::sun::star::sdbc::SQLException,
     541                 :            :            ::com::sun::star::lang::IndexOutOfBoundsException,
     542                 :            :            ::com::sun::star::uno::RuntimeException)
     543                 :            : {
     544                 :          0 :     osl::MutexGuard guard( m_refMutex->mutex );
     545                 :          0 :     if( index < 0 ||  index >= m_values.getLength() )
     546                 :            :     {
     547                 :          0 :         OUStringBuffer buf( 128 );
     548                 :          0 :         buf.appendAscii( "COLUMNS: Index out of range (allowed 0 to " );
     549                 :          0 :         buf.append((sal_Int32)(m_values.getLength() -1) );
     550                 :          0 :         buf.appendAscii( ", got " );
     551                 :          0 :         buf.append( index );
     552                 :          0 :         buf.appendAscii( ")" );
     553                 :            :         throw com::sun::star::lang::IndexOutOfBoundsException(
     554                 :          0 :             buf.makeStringAndClear(), *this );
     555                 :            :     }
     556                 :            : 
     557                 :          0 :     Reference< XPropertySet > set;
     558                 :          0 :     m_values[index] >>= set;
     559                 :          0 :     Statics &st = getStatics();
     560                 :          0 :     OUString name;
     561                 :          0 :     set->getPropertyValue( st.NAME ) >>= name;
     562                 :            : 
     563                 :          0 :     OUStringBuffer update( 128 );
     564                 :          0 :     update.appendAscii( "ALTER TABLE ONLY");
     565                 :          0 :     bufferQuoteQualifiedIdentifier( update, m_schemaName, m_tableName, m_pSettings );
     566                 :          0 :     update.appendAscii( "DROP COLUMN" );
     567                 :          0 :     bufferQuoteIdentifier( update, name, m_pSettings );
     568                 :          0 :     Reference< XStatement > stmt = m_origin->createStatement( );
     569                 :          0 :     DisposeGuard disposeIt( stmt );
     570                 :          0 :     stmt->executeUpdate( update.makeStringAndClear() );
     571                 :            : 
     572                 :          0 :     Container::dropByIndex( index );
     573                 :          0 : }
     574                 :            : 
     575                 :            : 
     576                 :          0 : ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > Columns::createDataDescriptor()
     577                 :            :         throw (::com::sun::star::uno::RuntimeException)
     578                 :            : {
     579                 :          0 :     return new ColumnDescriptor( m_refMutex, m_origin, m_pSettings );
     580                 :            : }
     581                 :            : 
     582                 :          0 : Reference< com::sun::star::container::XNameAccess > Columns::create(
     583                 :            :     const ::rtl::Reference< RefCountedMutex > & refMutex,
     584                 :            :     const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection >  & origin,
     585                 :            :     ConnectionSettings *pSettings,
     586                 :            :     const rtl::OUString &schemaName,
     587                 :            :     const rtl::OUString &tableName,
     588                 :            :     Columns **ppColumns)
     589                 :            : {
     590                 :            :     *ppColumns = new Columns(
     591                 :          0 :         refMutex, origin, pSettings, schemaName, tableName );
     592                 :          0 :     Reference< com::sun::star::container::XNameAccess > ret = *ppColumns;
     593                 :          0 :     (*ppColumns)->refresh();
     594                 :            : 
     595                 :          0 :     return ret;
     596                 :            : }
     597                 :            : 
     598                 :            : 
     599                 :            : //_____________________________________________________________________________________
     600                 :          0 : ColumnDescriptors::ColumnDescriptors(
     601                 :            :         const ::rtl::Reference< RefCountedMutex > & refMutex,
     602                 :            :         const ::com::sun::star::uno::Reference< com::sun::star::sdbc::XConnection >  & origin,
     603                 :            :         ConnectionSettings *pSettings )
     604                 :          0 :     : Container( refMutex, origin, pSettings,  ASCII_STR( "COLUMN-DESCRIPTOR" ) )
     605                 :          0 : {}
     606                 :            : 
     607                 :            : 
     608                 :          0 : Reference< ::com::sun::star::beans::XPropertySet > ColumnDescriptors::createDataDescriptor()
     609                 :            :         throw (::com::sun::star::uno::RuntimeException)
     610                 :            : {
     611                 :          0 :     return new ColumnDescriptor( m_refMutex, m_origin, m_pSettings );
     612                 :            : }
     613                 :            : 
     614                 :            : }

Generated by: LCOV version 1.10