LCOV - code coverage report
Current view: top level - connectivity/source/drivers/postgresql - pq_resultset.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 112 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 "pq_resultset.hxx"
      59                 :            : #include "pq_resultsetmetadata.hxx"
      60                 :            : 
      61                 :            : #include <com/sun/star/sdbc/FetchDirection.hpp>
      62                 :            : #include <com/sun/star/sdbc/ResultSetConcurrency.hpp>
      63                 :            : #include <com/sun/star/sdbc/ResultSetType.hpp>
      64                 :            : #include <com/sun/star/sdbc/DataType.hpp>
      65                 :            : 
      66                 :            : using rtl::OUString;
      67                 :            : using rtl::OUStringToOString;
      68                 :            : 
      69                 :            : using osl::MutexGuard;
      70                 :            : 
      71                 :            : using com::sun::star::uno::RuntimeException;
      72                 :            : using com::sun::star::uno::Any;
      73                 :            : using com::sun::star::uno::makeAny;
      74                 :            : using com::sun::star::uno::Reference;
      75                 :            : using com::sun::star::uno::XInterface;
      76                 :            : 
      77                 :            : using com::sun::star::sdbc::SQLException;
      78                 :            : using com::sun::star::sdbc::XResultSetMetaData;
      79                 :            : 
      80                 :            : 
      81                 :            : namespace pq_sdbc_driver
      82                 :            : {
      83                 :            : #define ASCII_STR(x) OUString( RTL_CONSTASCII_USTRINGPARAM( x ) )
      84                 :            : 
      85                 :          0 : void ResultSet::checkClosed()
      86                 :            :     throw ( com::sun::star::sdbc::SQLException, com::sun::star::uno::RuntimeException )
      87                 :            : {
      88                 :          0 :     if( ! m_result )
      89                 :            :     {
      90                 :            :         throw SQLException( ASCII_STR( "pq_resultset: already closed" ),
      91                 :          0 :                             *this,  OUString(), 1, Any() );
      92                 :            :     }
      93                 :            : 
      94                 :          0 :     if( ! m_ppSettings || ! *m_ppSettings || ! (*m_ppSettings)->pConnection )
      95                 :            :     {
      96                 :            :         throw SQLException( ASCII_STR( "pq_resultset: statement has been closed already" ),
      97                 :          0 :                             *this, OUString(), 1, Any() );
      98                 :            :     }
      99                 :            : 
     100                 :          0 : }
     101                 :            : 
     102                 :          0 : ResultSet::ResultSet( const ::rtl::Reference< RefCountedMutex > & refMutex,
     103                 :            :                       const Reference< XInterface > & owner,
     104                 :            :                       ConnectionSettings **ppSettings,
     105                 :            :                       PGresult * result,
     106                 :            :                       const rtl::OUString &schema,
     107                 :            :                       const rtl::OUString &table)
     108                 :            :     : BaseResultSet(
     109                 :          0 :         refMutex, owner, PQntuples( result ),
     110                 :          0 :         PQnfields( result ),(*ppSettings)->tc ),
     111                 :            :       m_result( result ),
     112                 :            :       m_schema( schema ),
     113                 :            :       m_table( table ),
     114                 :          0 :       m_ppSettings( ppSettings )
     115                 :            : {
     116                 :            :     // LEM TODO: shouldn't these things be inherited from the statement or something like that?
     117                 :          0 :     sal_Bool b = sal_False;
     118                 :            :     // Positioned update/delete not supported, so no cursor name
     119                 :            :     // Fetch direction and size are cursor-specific things, so not used now.
     120                 :            :     // Fetch size not set
     121                 :            :     m_props[ BASERESULTSET_FETCH_DIRECTION ] = makeAny(
     122                 :          0 :         com::sun::star::sdbc::FetchDirection::UNKNOWN);
     123                 :            :     // No escape processing for now
     124                 :          0 :     m_props[ BASERESULTSET_ESCAPE_PROCESSING ] = Any( &b, getBooleanCppuType() );
     125                 :            :     // Bookmarks not implemented for now
     126                 :          0 :     m_props[ BASERESULTSET_IS_BOOKMARKABLE ] = Any( &b, getBooleanCppuType() );
     127                 :            :     m_props[ BASERESULTSET_RESULT_SET_CONCURRENCY ] = makeAny(
     128                 :          0 :         com::sun::star::sdbc::ResultSetConcurrency::READ_ONLY );
     129                 :            :     m_props[ BASERESULTSET_RESULT_SET_TYPE ] = makeAny(
     130                 :          0 :         com::sun::star::sdbc::ResultSetType::SCROLL_INSENSITIVE );
     131                 :          0 : }
     132                 :            : 
     133                 :            : 
     134                 :          0 : Any ResultSet::getValue( sal_Int32 columnIndex )
     135                 :            : {
     136                 :          0 :     Any ret;
     137                 :          0 :     if( PQgetisnull( m_result, m_row, columnIndex -1 ) )
     138                 :            :     {
     139                 :          0 :         m_wasNull = true;
     140                 :            :     }
     141                 :            :     else
     142                 :            :     {
     143                 :          0 :         m_wasNull = false;
     144                 :            :         ret <<= ::rtl::OUString(
     145                 :          0 :             PQgetvalue( m_result, m_row , columnIndex -1 ) ,
     146                 :          0 :             PQgetlength( m_result, m_row , columnIndex -1 ) ,
     147                 :          0 :             (*m_ppSettings)->encoding );
     148                 :            : 
     149                 :            :     }
     150                 :          0 :     return ret;
     151                 :            : }
     152                 :            : 
     153                 :          0 : ResultSet::~ResultSet()
     154                 :          0 : {}
     155                 :            : 
     156                 :          0 : void ResultSet::close(  ) throw (SQLException, RuntimeException)
     157                 :            : {
     158                 :          0 :     Reference< XInterface > owner;
     159                 :            :     {
     160                 :          0 :         MutexGuard guard( m_refMutex->mutex );
     161                 :          0 :         if( m_result )
     162                 :            :         {
     163                 :          0 :             PQclear(m_result );
     164                 :          0 :             m_result = 0;
     165                 :          0 :             m_row = -1;
     166                 :            :         }
     167                 :          0 :         owner = m_owner;
     168                 :          0 :         m_owner.clear();
     169                 :          0 :     }
     170                 :          0 : }
     171                 :            : 
     172                 :          0 : Reference< XResultSetMetaData > ResultSet::getMetaData(  ) throw (SQLException, RuntimeException)
     173                 :            : {
     174                 :          0 :     MutexGuard guard( m_refMutex->mutex );
     175                 :          0 :     checkClosed();
     176                 :            :     return new ResultSetMetaData(
     177                 :          0 :         m_refMutex, this, this, m_ppSettings, m_result, m_schema, m_table );
     178                 :            : }
     179                 :            : 
     180                 :          0 : sal_Int32 ResultSet::findColumn( const ::rtl::OUString& columnName )
     181                 :            :         throw (SQLException, RuntimeException)
     182                 :            : {
     183                 :          0 :     MutexGuard guard( m_refMutex->mutex );
     184                 :          0 :     checkClosed();
     185                 :            :     return PQfnumber(
     186                 :            :         m_result,
     187                 :          0 :         OUStringToOString( columnName, (*m_ppSettings)->encoding ).getStr())
     188                 :          0 :         +1;
     189                 :            : }
     190                 :            : 
     191                 :          0 : static bool isNumber( const char * data, sal_Int32 len )
     192                 :            : {
     193                 :          0 :     bool ret = false;
     194                 :          0 :     if( len )
     195                 :            :     {
     196                 :          0 :         ret = true;
     197                 :          0 :         for( int i = 0 ; i < len ; i ++ )
     198                 :            :         {
     199                 :          0 :             if( ( data[i] >= '0' && data[i] <= '9' ) ||
     200                 :          0 :                 data[i] == '-' || data[i] == '+' || data[i] == '.' || data[i] == ',' )
     201                 :            :             {
     202                 :          0 :                 if( data[i] == '-' && i != 0 && i != len-1 )
     203                 :            :                 {
     204                 :            :                     // no number, maybe a date
     205                 :          0 :                     ret = false;
     206                 :          0 :                     break;
     207                 :            :                 }
     208                 :            :             }
     209                 :            :             else
     210                 :            :             {
     211                 :          0 :                 ret = false;
     212                 :          0 :                 break;
     213                 :            :             }
     214                 :            :         }
     215                 :            :     }
     216                 :          0 :     return ret;
     217                 :            : }
     218                 :            : 
     219                 :          0 : static bool isInteger( const char * data, sal_Int32 len )
     220                 :            : {
     221                 :          0 :     bool ret = false;
     222                 :          0 :     if( len )
     223                 :            :     {
     224                 :          0 :         ret = true;
     225                 :          0 :         for( int i = 0 ; i < len ; i ++ )
     226                 :            :         {
     227                 :          0 :             if( ( data[i] >= '0' && data[i] <= '9' ) ||
     228                 :          0 :                 data[i] == '-' || data[i] == '+' )
     229                 :            :             {
     230                 :          0 :                 if( data[i] == '-' && i != 0 && i != len-1 )
     231                 :            :                 {
     232                 :            :                     // no number, maybe a date
     233                 :          0 :                     ret = false;
     234                 :          0 :                     break;
     235                 :            :                 }
     236                 :            :             }
     237                 :            :             else
     238                 :            :             {
     239                 :          0 :                 ret = false;
     240                 :          0 :                 break;
     241                 :            :             }
     242                 :            :         }
     243                 :            :     }
     244                 :          0 :     return ret;
     245                 :            : }
     246                 :            : 
     247                 :          0 : static bool isDate( const char * data, sal_Int32 len )
     248                 :            : {
     249                 :            :     return 10 == len &&
     250                 :          0 :         '-' == data[4] &&
     251                 :          0 :         '-' == data[7] &&
     252                 :          0 :         isInteger( &(data[0]),4 ) &&
     253                 :          0 :         isInteger( &(data[5]),2) &&
     254                 :          0 :         isInteger( &(data[8]),2 );
     255                 :            : }
     256                 :            : 
     257                 :          0 : static bool isTime( const char * data, sal_Int32 len )
     258                 :            : {
     259                 :            :     return 8 == len &&
     260                 :          0 :         ':' == data[2] &&
     261                 :          0 :         ':' == data[5] &&
     262                 :          0 :         isInteger( &(data[0]),2 ) &&
     263                 :          0 :         isInteger( &(data[3]),2) &&
     264                 :          0 :         isInteger( &(data[6]),2 );
     265                 :            : 
     266                 :            : }
     267                 :            : 
     268                 :          0 : static bool isTimestamp( const char * data, sal_Int32 len )
     269                 :            : {
     270                 :          0 :     return len == 19 && isDate( data, 10) && isTime( &(data[11]),8 );
     271                 :            : }
     272                 :            : 
     273                 :          0 : sal_Int32 ResultSet::guessDataType( sal_Int32 column )
     274                 :            : {
     275                 :            :     // we don't look into more than 100 rows ...
     276                 :          0 :     sal_Int32 ret = com::sun::star::sdbc::DataType::INTEGER;
     277                 :            : 
     278                 :          0 :     int maxRows = ( m_rowCount > 100 ? 100 : m_rowCount );
     279                 :          0 :     for( int i = 0 ; i < maxRows ; i ++ )
     280                 :            :     {
     281                 :          0 :         if( ! PQgetisnull( m_result, i , column-1  ) )
     282                 :            :         {
     283                 :          0 :             const char * p = PQgetvalue( m_result, i , column -1 );
     284                 :          0 :             int len = PQgetlength( m_result, i , column -1 );
     285                 :            : 
     286                 :          0 :             if( com::sun::star::sdbc::DataType::INTEGER == ret )
     287                 :            :             {
     288                 :          0 :                 if( ! isInteger( p,len ) )
     289                 :          0 :                     ret = com::sun::star::sdbc::DataType::NUMERIC;
     290                 :            :             }
     291                 :          0 :             if( com::sun::star::sdbc::DataType::NUMERIC == ret )
     292                 :            :             {
     293                 :          0 :                 if( ! isNumber( p,len ) )
     294                 :            :                 {
     295                 :          0 :                     ret = com::sun::star::sdbc::DataType::DATE;
     296                 :            :                 }
     297                 :            :             }
     298                 :          0 :             if( com::sun::star::sdbc::DataType::DATE == ret )
     299                 :            :             {
     300                 :          0 :                 if( ! isDate( p,len ) )
     301                 :            :                 {
     302                 :          0 :                     ret = com::sun::star::sdbc::DataType::TIME;
     303                 :            :                 }
     304                 :            :             }
     305                 :          0 :             if( com::sun::star::sdbc::DataType::TIME == ret )
     306                 :            :             {
     307                 :          0 :                 if( ! isTime( p,len ) )
     308                 :            :                 {
     309                 :          0 :                     ret = com::sun::star::sdbc::DataType::TIMESTAMP;
     310                 :            :                 }
     311                 :            :             }
     312                 :          0 :             if( com::sun::star::sdbc::DataType::TIMESTAMP == ret )
     313                 :            :             {
     314                 :          0 :                 if( ! isTimestamp( p,len ) )
     315                 :            :                 {
     316                 :          0 :                     ret = com::sun::star::sdbc::DataType::LONGVARCHAR;
     317                 :          0 :                     break;
     318                 :            :                 }
     319                 :            :             }
     320                 :            :         }
     321                 :            :     }
     322                 :          0 :     return ret;
     323                 :            : }
     324                 :            : 
     325                 :            : }

Generated by: LCOV version 1.10