LCOV - code coverage report
Current view: top level - connectivity/source/drivers/dbase - DTable.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 510 1447 35.2 %
Date: 2014-11-03 Functions: 27 50 54.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 "dbase/DTable.hxx"
      21             : #include <com/sun/star/sdbc/ColumnValue.hpp>
      22             : #include <com/sun/star/sdbc/DataType.hpp>
      23             : #include <com/sun/star/ucb/XContentAccess.hpp>
      24             : #include <com/sun/star/sdbc/XRow.hpp>
      25             : #include <svl/converter.hxx>
      26             : #include "dbase/DConnection.hxx"
      27             : #include "dbase/DColumns.hxx"
      28             : #include <osl/thread.h>
      29             : #include <tools/config.hxx>
      30             : #include "dbase/DIndex.hxx"
      31             : #include "dbase/DIndexes.hxx"
      32             : #include <comphelper/processfactory.hxx>
      33             : #include <comphelper/sequence.hxx>
      34             : #include <svl/zforlist.hxx>
      35             : #include <unotools/syslocale.hxx>
      36             : #include <rtl/math.hxx>
      37             : #include <stdio.h>
      38             : #include <ucbhelper/content.hxx>
      39             : #include <connectivity/dbexception.hxx>
      40             : #include <com/sun/star/lang/DisposedException.hpp>
      41             : #include <comphelper/property.hxx>
      42             : #include <comphelper/string.hxx>
      43             : #include <unotools/tempfile.hxx>
      44             : #include <unotools/ucbhelper.hxx>
      45             : #include <comphelper/types.hxx>
      46             : #include <cppuhelper/exc_hlp.hxx>
      47             : #include <connectivity/PColumn.hxx>
      48             : #include <connectivity/dbtools.hxx>
      49             : #include <connectivity/FValue.hxx>
      50             : #include <connectivity/dbconversion.hxx>
      51             : #include "resource/dbase_res.hrc"
      52             : #include <rtl/strbuf.hxx>
      53             : 
      54             : #include <algorithm>
      55             : 
      56             : using namespace ::comphelper;
      57             : using namespace connectivity;
      58             : using namespace connectivity::sdbcx;
      59             : using namespace connectivity::dbase;
      60             : using namespace connectivity::file;
      61             : using namespace ::ucbhelper;
      62             : using namespace ::utl;
      63             : using namespace ::cppu;
      64             : using namespace ::dbtools;
      65             : using namespace ::com::sun::star::uno;
      66             : using namespace ::com::sun::star::ucb;
      67             : using namespace ::com::sun::star::beans;
      68             : using namespace ::com::sun::star::sdbcx;
      69             : using namespace ::com::sun::star::sdbc;
      70             : using namespace ::com::sun::star::container;
      71             : using namespace ::com::sun::star::lang;
      72             : using namespace ::com::sun::star::i18n;
      73             : 
      74             : // stored as the Field Descriptor terminator
      75             : #define FIELD_DESCRIPTOR_TERMINATOR 0x0D
      76             : #define DBF_EOL                     0x1A
      77             : 
      78             : namespace
      79             : {
      80          32 : sal_Size lcl_getFileSize(SvStream& _rStream)
      81             : {
      82          32 :     sal_Size nFileSize = 0;
      83          32 :     _rStream.Seek(STREAM_SEEK_TO_END);
      84          32 :     _rStream.SeekRel(-1);
      85             :     char cEOL;
      86          32 :     _rStream.ReadChar( cEOL );
      87          32 :     nFileSize = _rStream.Tell();
      88          32 :     if ( cEOL == DBF_EOL )
      89          16 :         nFileSize -= 1;
      90          32 :     return nFileSize;
      91             : }
      92             : /**
      93             :     calculates the Julian date
      94             : */
      95           0 : void lcl_CalcJulDate(sal_Int32& _nJulianDate,sal_Int32& _nJulianTime,const com::sun::star::util::DateTime _aDateTime)
      96             : {
      97           0 :     com::sun::star::util::DateTime aDateTime = _aDateTime;
      98             :     // weird: months fix
      99           0 :     if (aDateTime.Month > 12)
     100             :     {
     101           0 :         aDateTime.Month--;
     102           0 :         sal_uInt16 delta = _aDateTime.Month / 12;
     103           0 :         aDateTime.Year += delta;
     104           0 :         aDateTime.Month -= delta * 12;
     105           0 :         aDateTime.Month++;
     106             :     }
     107             : 
     108           0 :     _nJulianTime = ((aDateTime.Hours*3600000)+(aDateTime.Minutes*60000)+(aDateTime.Seconds*1000)+(aDateTime.NanoSeconds/1000000));
     109             :     /* conversion factors */
     110             :     sal_uInt16 iy0;
     111             :     sal_uInt16 im0;
     112           0 :     if ( aDateTime.Month <= 2 )
     113             :     {
     114           0 :         iy0 = aDateTime.Year - 1;
     115           0 :         im0 = aDateTime.Month + 12;
     116             :     }
     117             :     else
     118             :     {
     119           0 :         iy0 = aDateTime.Year;
     120           0 :         im0 = aDateTime.Month;
     121             :     }
     122           0 :     sal_Int32 ia = iy0 / 100;
     123           0 :     sal_Int32 ib = 2 - ia + (ia >> 2);
     124             :     /* calculate julian date    */
     125           0 :     if ( aDateTime.Year <= 0 )
     126             :     {
     127           0 :         _nJulianDate = (sal_Int32) ((365.25 * iy0) - 0.75)
     128           0 :             + (sal_Int32) (30.6001 * (im0 + 1) )
     129           0 :             + aDateTime.Day + 1720994;
     130             :     } // if ( _aDateTime.Year <= 0 )
     131             :     else
     132             :     {
     133           0 :         _nJulianDate = static_cast<sal_Int32>( ((365.25 * iy0)
     134           0 :             + (sal_Int32) (30.6001 * (im0 + 1))
     135           0 :             + aDateTime.Day + 1720994));
     136             :     }
     137           0 :     double JD = _nJulianDate + 0.5;
     138           0 :     _nJulianDate = (sal_Int32)( JD + 0.5);
     139           0 :     const double gyr = aDateTime.Year + (0.01 * aDateTime.Month) + (0.0001 * aDateTime.Day);
     140           0 :     if ( gyr >= 1582.1015 ) /* on or after 15 October 1582  */
     141           0 :         _nJulianDate += ib;
     142           0 : }
     143             : 
     144             : /**
     145             :     calculates date time from the Julian Date
     146             : */
     147           0 : void lcl_CalDate(sal_Int32 _nJulianDate,sal_Int32 _nJulianTime,com::sun::star::util::DateTime& _rDateTime)
     148             : {
     149           0 :     if ( _nJulianDate )
     150             :     {
     151             :         sal_Int32 ialp;
     152           0 :         sal_Int32 ka = _nJulianDate;
     153           0 :         if ( _nJulianDate >= 2299161 )
     154             :         {
     155           0 :             ialp = (sal_Int32)( ((double) _nJulianDate - 1867216.25 ) / ( 36524.25 ));
     156           0 :             ka = _nJulianDate + 1 + ialp - ( ialp >> 2 );
     157             :         }
     158           0 :         sal_Int32 kb = ka + 1524;
     159           0 :         sal_Int32 kc =  (sal_Int32) ( ((double) kb - 122.1 ) / 365.25 );
     160           0 :         sal_Int32 kd = (sal_Int32) ((double) kc * 365.25);
     161           0 :         sal_Int32 ke = (sal_Int32) ((double) ( kb - kd ) / 30.6001 );
     162           0 :         _rDateTime.Day = static_cast<sal_uInt16>(kb - kd - ((sal_Int32) ( (double) ke * 30.6001 )));
     163           0 :         if ( ke > 13 )
     164           0 :             _rDateTime.Month = static_cast<sal_uInt16>(ke - 13);
     165             :         else
     166           0 :             _rDateTime.Month = static_cast<sal_uInt16>(ke - 1);
     167           0 :         if ( (_rDateTime.Month == 2) && (_rDateTime.Day > 28) )
     168           0 :             _rDateTime.Day = 29;
     169           0 :         if ( (_rDateTime.Month == 2) && (_rDateTime.Day == 29) && (ke == 3) )
     170           0 :             _rDateTime.Year = static_cast<sal_uInt16>(kc - 4716);
     171           0 :         else if ( _rDateTime.Month > 2 )
     172           0 :             _rDateTime.Year = static_cast<sal_uInt16>(kc - 4716);
     173             :         else
     174           0 :             _rDateTime.Year = static_cast<sal_uInt16>(kc - 4715);
     175             :     }
     176             : 
     177           0 :     if ( _nJulianTime )
     178             :     {
     179           0 :         double d_s = _nJulianTime / 1000.0;
     180           0 :         double d_m = d_s / 60.0;
     181           0 :         double d_h  = d_m / 60.0;
     182           0 :         _rDateTime.Hours = (sal_uInt16) (d_h);
     183           0 :         _rDateTime.Minutes = (sal_uInt16) d_m;
     184           0 :         _rDateTime.Seconds = static_cast<sal_uInt16>(( d_m - (double) _rDateTime.Minutes ) * 60.0);
     185             :     }
     186           0 : }
     187             : 
     188             : }
     189             : 
     190             : 
     191          30 : void ODbaseTable::readHeader()
     192             : {
     193             :     OSL_ENSURE(m_pFileStream,"No Stream available!");
     194          30 :     if(!m_pFileStream)
     195          30 :         return;
     196          30 :     m_pFileStream->RefreshBuffer(); // Make sure, that the header information actually is read again
     197          30 :     m_pFileStream->Seek(STREAM_SEEK_TO_BEGIN);
     198             : 
     199          30 :     sal_uInt8 nType=0;
     200          30 :     (*m_pFileStream).ReadUChar( nType );
     201          30 :     if(ERRCODE_NONE != m_pFileStream->GetErrorCode())
     202           0 :         throwInvalidDbaseFormat();
     203             : 
     204          30 :     m_pFileStream->Read((char*)(&m_aHeader.db_aedat), 3*sizeof(sal_uInt8));
     205          30 :     if(ERRCODE_NONE != m_pFileStream->GetErrorCode())
     206           0 :         throwInvalidDbaseFormat();
     207          30 :     (*m_pFileStream).ReadUInt32( m_aHeader.db_anz );
     208          30 :     if(ERRCODE_NONE != m_pFileStream->GetErrorCode())
     209           0 :         throwInvalidDbaseFormat();
     210          30 :     (*m_pFileStream).ReadUInt16( m_aHeader.db_kopf );
     211          30 :     if(ERRCODE_NONE != m_pFileStream->GetErrorCode())
     212           0 :         throwInvalidDbaseFormat();
     213          30 :     (*m_pFileStream).ReadUInt16( m_aHeader.db_slng );
     214          30 :     if(ERRCODE_NONE != m_pFileStream->GetErrorCode())
     215           0 :         throwInvalidDbaseFormat();
     216          30 :     if (m_aHeader.db_slng == 0)
     217           0 :         throwInvalidDbaseFormat();
     218          30 :     m_pFileStream->Read((char*)(&m_aHeader.db_frei), 20*sizeof(sal_uInt8));
     219          30 :     if(ERRCODE_NONE != m_pFileStream->GetErrorCode())
     220           0 :         throwInvalidDbaseFormat();
     221             : 
     222          30 :     if ( ( ( m_aHeader.db_kopf - 1 ) / 32 - 1 ) <= 0 ) // number of fields
     223             :     {
     224             :         // no dbase file
     225           0 :         throwInvalidDbaseFormat();
     226             :     }
     227             :     else
     228             :     {
     229             :         // Consistency check of the header:
     230          30 :         m_aHeader.db_typ = (DBFType)nType;
     231          30 :         switch (m_aHeader.db_typ)
     232             :         {
     233             :             case dBaseIII:
     234             :             case dBaseIV:
     235             :             case dBaseV:
     236             :             case VisualFoxPro:
     237             :             case VisualFoxProAuto:
     238             :             case dBaseFS:
     239             :             case dBaseFSMemo:
     240             :             case dBaseIVMemoSQL:
     241             :             case dBaseIIIMemo:
     242             :             case FoxProMemo:
     243          30 :                 m_pFileStream->SetNumberFormatInt(NUMBERFORMAT_INT_LITTLEENDIAN);
     244          60 :                 if ( m_aHeader.db_frei[17] != 0x00
     245          30 :                     && !m_aHeader.db_frei[18] && !m_aHeader.db_frei[19] && getConnection()->isTextEncodingDefaulted() )
     246             :                 {
     247           0 :                     switch(m_aHeader.db_frei[17])
     248             :                     {
     249           0 :                         case 0x01: m_eEncoding = RTL_TEXTENCODING_IBM_437; break;       // DOS USA  code page 437
     250           0 :                         case 0x02: m_eEncoding = RTL_TEXTENCODING_IBM_850; break;       // DOS Multilingual code page 850
     251           0 :                         case 0x03: m_eEncoding = RTL_TEXTENCODING_MS_1252; break;       // Windows ANSI code page 1252
     252           0 :                         case 0x04: m_eEncoding = RTL_TEXTENCODING_APPLE_ROMAN; break;   // Standard Macintosh
     253           0 :                         case 0x64: m_eEncoding = RTL_TEXTENCODING_IBM_852; break;       // EE MS-DOS    code page 852
     254           0 :                         case 0x65: m_eEncoding = RTL_TEXTENCODING_IBM_865; break;       // Nordic MS-DOS    code page 865
     255           0 :                         case 0x66: m_eEncoding = RTL_TEXTENCODING_IBM_866; break;       // Russian MS-DOS   code page 866
     256           0 :                         case 0x67: m_eEncoding = RTL_TEXTENCODING_IBM_861; break;       // Icelandic MS-DOS
     257             :                         //case 0x68: m_eEncoding = ; break;     // Kamenicky (Czech) MS-DOS
     258             :                         //case 0x69: m_eEncoding = ; break;     // Mazovia (Polish) MS-DOS
     259           0 :                         case 0x6A: m_eEncoding = RTL_TEXTENCODING_IBM_737; break;       // Greek MS-DOS (437G)
     260           0 :                         case 0x6B: m_eEncoding = RTL_TEXTENCODING_IBM_857; break;       // Turkish MS-DOS
     261           0 :                         case 0x6C: m_eEncoding = RTL_TEXTENCODING_IBM_863; break;       // MS-DOS, Canada
     262           0 :                         case 0x78: m_eEncoding = RTL_TEXTENCODING_MS_950; break;        // Windows, Traditional Chinese
     263           0 :                         case 0x79: m_eEncoding = RTL_TEXTENCODING_MS_949; break;        // Windows, Korean (Hangul)
     264           0 :                         case 0x7A: m_eEncoding = RTL_TEXTENCODING_MS_936; break;        // Windows, Simplified Chinese
     265           0 :                         case 0x7B: m_eEncoding = RTL_TEXTENCODING_MS_932; break;        // Windows, Japanese (Shift-jis)
     266           0 :                         case 0x7C: m_eEncoding = RTL_TEXTENCODING_MS_874; break;        // Windows, Thai
     267           0 :                         case 0x7D: m_eEncoding = RTL_TEXTENCODING_MS_1255; break;       // Windows, Hebrew
     268           0 :                         case 0x7E: m_eEncoding = RTL_TEXTENCODING_MS_1256; break;       // Windows, Arabic
     269           0 :                         case 0x96: m_eEncoding = RTL_TEXTENCODING_APPLE_CYRILLIC; break;    // Russian Macintosh
     270           0 :                         case 0x97: m_eEncoding = RTL_TEXTENCODING_APPLE_CENTEURO; break;    // Eastern European Macintosh
     271           0 :                         case 0x98: m_eEncoding = RTL_TEXTENCODING_APPLE_GREEK; break;   // Greek Macintosh
     272           0 :                         case 0xC8: m_eEncoding = RTL_TEXTENCODING_MS_1250; break;       // Windows EE   code page 1250
     273           0 :                         case 0xC9: m_eEncoding = RTL_TEXTENCODING_MS_1251; break;       // Russian Windows
     274           0 :                         case 0xCA: m_eEncoding = RTL_TEXTENCODING_MS_1254; break;       // Turkish Windows
     275           0 :                         case 0xCB: m_eEncoding = RTL_TEXTENCODING_MS_1253; break;       // Greek Windows
     276           0 :                         case 0xCC: m_eEncoding = RTL_TEXTENCODING_MS_1257; break;       // Windows, Baltic
     277             :                         default:
     278             :                             // Default Encoding
     279           0 :                             m_eEncoding = RTL_TEXTENCODING_IBM_850;
     280           0 :                             break;
     281             :                     }
     282             :                 }
     283          30 :                 break;
     284             :             case dBaseIVMemo:
     285           0 :                 m_pFileStream->SetNumberFormatInt(NUMBERFORMAT_INT_LITTLEENDIAN);
     286           0 :                 break;
     287             :             default:
     288             :             {
     289           0 :                 throwInvalidDbaseFormat();
     290             :             }
     291             :         }
     292             :     }
     293             : }
     294             : 
     295          30 : void ODbaseTable::fillColumns()
     296             : {
     297          30 :     m_pFileStream->Seek(STREAM_SEEK_TO_BEGIN);
     298          30 :     m_pFileStream->Seek(32L);
     299             : 
     300          30 :     if(!m_aColumns.is())
     301           0 :         m_aColumns = new OSQLColumns();
     302             :     else
     303          30 :         m_aColumns->get().clear();
     304             : 
     305          30 :     m_aTypes.clear();
     306          30 :     m_aPrecisions.clear();
     307          30 :     m_aScales.clear();
     308             : 
     309             :     // Number of fields:
     310          30 :     const sal_Int32 nFieldCount = (m_aHeader.db_kopf - 1) / 32 - 1;
     311             :     OSL_ENSURE(nFieldCount,"No columns in table!");
     312             : 
     313          30 :     m_aColumns->get().reserve(nFieldCount);
     314          30 :     m_aTypes.reserve(nFieldCount);
     315          30 :     m_aPrecisions.reserve(nFieldCount);
     316          30 :     m_aScales.reserve(nFieldCount);
     317             : 
     318          30 :     OUString aTypeName;
     319          30 :     const bool bCase = getConnection()->getMetaData()->supportsMixedCaseQuotedIdentifiers();
     320          30 :     const bool bFoxPro = m_aHeader.db_typ == VisualFoxPro || m_aHeader.db_typ == VisualFoxProAuto || m_aHeader.db_typ == FoxProMemo;
     321             : 
     322          30 :     sal_Int32 i = 0;
     323        1280 :     for (; i < nFieldCount; i++)
     324             :     {
     325             :         DBFColumn aDBFColumn;
     326         610 :         sal_Size nRead = m_pFileStream->Read(&aDBFColumn, sizeof(aDBFColumn));
     327         610 :         if (nRead != sizeof(aDBFColumn))
     328             :         {
     329             :             SAL_WARN("connectivity.drivers", "ODbaseTable::fillColumns: short read!");
     330           0 :             break;
     331             :         }
     332         610 :         if ( FIELD_DESCRIPTOR_TERMINATOR == aDBFColumn.db_fnm[0] ) // 0x0D stored as the Field Descriptor terminator.
     333           0 :             break;
     334             : 
     335         610 :         aDBFColumn.db_fnm[sizeof(aDBFColumn.db_fnm)-1] = 0; //ensure null termination for broken input
     336         610 :         const OUString aColumnName((const char *)aDBFColumn.db_fnm, strlen((const char *)aDBFColumn.db_fnm), m_eEncoding);
     337             : 
     338         610 :         bool bIsRowVersion = bFoxPro && ( aDBFColumn.db_frei2[0] & 0x01 ) == 0x01;
     339             : 
     340         610 :         m_aRealFieldLengths.push_back(aDBFColumn.db_flng);
     341         610 :         sal_Int32 nPrecision = aDBFColumn.db_flng;
     342             :         sal_Int32 eType;
     343         610 :         bool bIsCurrency = false;
     344             : 
     345             :         char cType[2];
     346         610 :         cType[0] = aDBFColumn.db_typ;
     347         610 :         cType[1] = 0;
     348         610 :         aTypeName = OUString::createFromAscii(cType);
     349             :         SAL_INFO( "connectivity.drivers","column type: " << aDBFColumn.db_typ);
     350             : 
     351         610 :         switch (aDBFColumn.db_typ)
     352             :         {
     353             :         case 'C':
     354         212 :             eType = DataType::VARCHAR;
     355         212 :             aTypeName = "VARCHAR";
     356         212 :             break;
     357             :         case 'F':
     358             :         case 'N':
     359          80 :             aTypeName = "DECIMAL";
     360          80 :             if ( aDBFColumn.db_typ == 'N' )
     361          80 :                 aTypeName = "NUMERIC";
     362          80 :             eType = DataType::DECIMAL;
     363             : 
     364             :             // for numeric fields two characters more are written, than the precision of the column description predescribes,
     365             :             // to keep room for the possible sign and the comma. This has to be considered...
     366          80 :             nPrecision = SvDbaseConverter::ConvertPrecisionToOdbc(nPrecision,aDBFColumn.db_dez);
     367             :             // This is not true for older versions ....
     368          80 :             break;
     369             :         case 'L':
     370          16 :             eType = DataType::BIT;
     371          16 :             aTypeName = "BOOLEAN";
     372          16 :             break;
     373             :         case 'Y':
     374           0 :             bIsCurrency = true;
     375           0 :             eType = DataType::DOUBLE;
     376           0 :             aTypeName = "DOUBLE";
     377           0 :             break;
     378             :         case 'D':
     379          32 :             eType = DataType::DATE;
     380          32 :             aTypeName = "DATE";
     381          32 :             break;
     382             :         case 'T':
     383           0 :             eType = DataType::TIMESTAMP;
     384           0 :             aTypeName = "TIMESTAMP";
     385           0 :             break;
     386             :         case 'I':
     387           0 :             eType = DataType::INTEGER;
     388           0 :             aTypeName = "INTEGER";
     389           0 :             break;
     390             :         case 'M':
     391         270 :             if ( bFoxPro && ( aDBFColumn.db_frei2[0] & 0x04 ) == 0x04 )
     392             :             {
     393           0 :                 eType = DataType::LONGVARBINARY;
     394           0 :                 aTypeName = "LONGVARBINARY";
     395             :             }
     396             :             else
     397             :             {
     398         270 :                 aTypeName = "LONGVARCHAR";
     399         270 :                 eType = DataType::LONGVARCHAR;
     400             :             }
     401         270 :             nPrecision = 2147483647;
     402         270 :             break;
     403             :         case 'P':
     404           0 :             aTypeName = "LONGVARBINARY";
     405           0 :             eType = DataType::LONGVARBINARY;
     406           0 :             nPrecision = 2147483647;
     407           0 :             break;
     408             :         case '0':
     409             :         case 'B':
     410           0 :             if ( m_aHeader.db_typ == VisualFoxPro || m_aHeader.db_typ == VisualFoxProAuto )
     411             :             {
     412           0 :                 aTypeName = "DOUBLE";
     413           0 :                 eType = DataType::DOUBLE;
     414             :             }
     415             :             else
     416             :             {
     417           0 :                 aTypeName = "LONGVARBINARY";
     418           0 :                 eType = DataType::LONGVARBINARY;
     419           0 :                 nPrecision = 2147483647;
     420             :             }
     421           0 :             break;
     422             :         default:
     423           0 :             eType = DataType::OTHER;
     424             :         }
     425             : 
     426         610 :         m_aTypes.push_back(eType);
     427         610 :         m_aPrecisions.push_back(nPrecision);
     428         610 :         m_aScales.push_back(aDBFColumn.db_dez);
     429             : 
     430             :         Reference< XPropertySet> xCol = new sdbcx::OColumn(aColumnName,
     431             :                                                     aTypeName,
     432             :                                                     OUString(),
     433             :                                                     OUString(),
     434             :                                                     ColumnValue::NULLABLE,
     435             :                                                     nPrecision,
     436             :                                                     aDBFColumn.db_dez,
     437             :                                                     eType,
     438             :                                                     false,
     439             :                                                     bIsRowVersion,
     440             :                                                     bIsCurrency,
     441             :                                                     bCase,
     442         610 :                                                     m_CatalogName, getSchema(), getName());
     443         610 :         m_aColumns->get().push_back(xCol);
     444         610 :     } // for (; i < nFieldCount; i++)
     445          30 :     OSL_ENSURE(i,"No columns in table!");
     446          30 : }
     447             : 
     448           0 : ODbaseTable::ODbaseTable(sdbcx::OCollection* _pTables, ODbaseConnection* _pConnection)
     449             :     : ODbaseTable_BASE(_pTables,_pConnection)
     450             :     , m_pMemoStream(NULL)
     451           0 :     , m_bWriteableMemo(false)
     452             : {
     453             :     // initialize the header
     454           0 :     memset(&m_aHeader, 0, sizeof(m_aHeader));
     455           0 :     m_aHeader.db_typ = dBaseIII;
     456           0 :     m_eEncoding = getConnection()->getTextEncoding();
     457           0 : }
     458             : 
     459          30 : ODbaseTable::ODbaseTable(sdbcx::OCollection* _pTables, ODbaseConnection* _pConnection,
     460             :                          const OUString& _Name,
     461             :                          const OUString& _Type,
     462             :                          const OUString& _Description ,
     463             :                          const OUString& _SchemaName,
     464             :                          const OUString& _CatalogName )
     465             :     : ODbaseTable_BASE(_pTables,_pConnection,_Name,
     466             :                        _Type,
     467             :                        _Description,
     468             :                        _SchemaName,
     469             :                        _CatalogName)
     470             :     , m_pMemoStream(NULL)
     471          30 :     , m_bWriteableMemo(false)
     472             : {
     473          30 :     memset(&m_aHeader, 0, sizeof(m_aHeader));
     474          30 :     m_eEncoding = getConnection()->getTextEncoding();
     475          30 : }
     476             : 
     477             : 
     478          30 : void ODbaseTable::construct()
     479             : {
     480             :     // initialize the header
     481          30 :     m_aHeader.db_typ    = dBaseIII;
     482          30 :     m_aHeader.db_anz    = 0;
     483          30 :     m_aHeader.db_kopf   = 0;
     484          30 :     m_aHeader.db_slng   = 0;
     485          30 :     m_aMemoHeader.db_size = 0;
     486             : 
     487          30 :     OUString sFileName(getEntry(m_pConnection, m_Name));
     488             : 
     489          60 :     INetURLObject aURL;
     490          30 :     aURL.SetURL(sFileName);
     491             : 
     492             :     OSL_ENSURE( m_pConnection->matchesExtension( aURL.getExtension() ),
     493             :         "ODbaseTable::ODbaseTable: invalid extension!");
     494             :         // getEntry is expected to ensure the correct file name
     495             : 
     496          30 :     m_pFileStream = createStream_simpleError( sFileName, STREAM_READWRITE | STREAM_NOCREATE | STREAM_SHARE_DENYWRITE);
     497          30 :     m_bWriteable = ( m_pFileStream != NULL );
     498             : 
     499          30 :     if ( !m_pFileStream )
     500             :     {
     501           0 :         m_bWriteable = false;
     502           0 :         m_pFileStream = createStream_simpleError( sFileName, STREAM_READ | STREAM_NOCREATE | STREAM_SHARE_DENYNONE);
     503             :     }
     504             : 
     505          30 :     if(m_pFileStream)
     506             :     {
     507          30 :         readHeader();
     508          30 :         if (HasMemoFields())
     509             :         {
     510             :         // Create Memo-Filename (.DBT):
     511             :         // nyi: Ugly for Unix and Mac!
     512             : 
     513          30 :             if ( m_aHeader.db_typ == FoxProMemo || VisualFoxPro == m_aHeader.db_typ || VisualFoxProAuto == m_aHeader.db_typ ) // foxpro uses another extension
     514           0 :                 aURL.SetExtension("fpt");
     515             :             else
     516          30 :                 aURL.SetExtension("dbt");
     517             : 
     518             :             // If the memo file isn't found, the data will be displayed anyhow.
     519             :             // However, updates can't be done
     520             :             // but the operation is executed
     521          30 :             m_pMemoStream = createStream_simpleError( aURL.GetMainURL(INetURLObject::NO_DECODE), STREAM_READWRITE | STREAM_NOCREATE | STREAM_SHARE_DENYWRITE);
     522          30 :             if ( !m_pMemoStream )
     523             :             {
     524          16 :                 m_bWriteableMemo = false;
     525          16 :                 m_pMemoStream = createStream_simpleError( aURL.GetMainURL(INetURLObject::NO_DECODE), STREAM_READ | STREAM_NOCREATE | STREAM_SHARE_DENYNONE);
     526             :             }
     527          30 :             if (m_pMemoStream)
     528          14 :                 ReadMemoHeader();
     529             :         }
     530          30 :         fillColumns();
     531             : 
     532          30 :         sal_Size nFileSize = lcl_getFileSize(*m_pFileStream);
     533          30 :         m_pFileStream->Seek(STREAM_SEEK_TO_BEGIN);
     534             :         // seems to be empty or someone wrote bullshit into the dbase file
     535             :         // try and recover if m_aHeader.db_slng is sane
     536          30 :         if (m_aHeader.db_anz == 0 && m_aHeader.db_slng)
     537             :         {
     538           0 :             sal_Size nRecords = (nFileSize-m_aHeader.db_kopf)/m_aHeader.db_slng;
     539           0 :             if (nRecords > 0)
     540           0 :                 m_aHeader.db_anz = nRecords;
     541             :         }
     542             : 
     543             :         // Buffersize dependent on the file size
     544             :         m_pFileStream->SetBufferSize(nFileSize > 1000000 ? 32768 :
     545             :                                   nFileSize > 100000 ? 16384 :
     546          30 :                                   nFileSize > 10000 ? 4096 : 1024);
     547             : 
     548          30 :         if (m_pMemoStream)
     549             :         {
     550             :             // set the buffer extactly to the length of a record
     551          14 :             m_pMemoStream->Seek(STREAM_SEEK_TO_END);
     552          14 :             nFileSize = m_pMemoStream->Tell();
     553          14 :             m_pMemoStream->Seek(STREAM_SEEK_TO_BEGIN);
     554             : 
     555             :             // Buffersize dependent on the file size
     556             :             m_pMemoStream->SetBufferSize(nFileSize > 1000000 ? 32768 :
     557             :                                           nFileSize > 100000 ? 16384 :
     558             :                                           nFileSize > 10000 ? 4096 :
     559          14 :                                           m_aMemoHeader.db_size);
     560             :         }
     561             : 
     562          30 :         AllocBuffer();
     563          30 :     }
     564          30 : }
     565             : 
     566          14 : bool ODbaseTable::ReadMemoHeader()
     567             : {
     568          14 :     m_pMemoStream->SetNumberFormatInt(NUMBERFORMAT_INT_LITTLEENDIAN);
     569          14 :     m_pMemoStream->RefreshBuffer();         // make sure that the header information is actually read again
     570          14 :     m_pMemoStream->Seek(0L);
     571             : 
     572          14 :     (*m_pMemoStream).ReadUInt32( m_aMemoHeader.db_next );
     573          14 :     switch (m_aHeader.db_typ)
     574             :     {
     575             :         case dBaseIIIMemo:  // dBase III: fixed block size
     576             :         case dBaseIVMemo:
     577             :             // sometimes dBase3 is attached to dBase4 memo
     578          14 :             m_pMemoStream->Seek(20L);
     579          14 :             (*m_pMemoStream).ReadUInt16( m_aMemoHeader.db_size );
     580          14 :             if (m_aMemoHeader.db_size > 1 && m_aMemoHeader.db_size != 512)  // 1 is also for dBase 3
     581           0 :                 m_aMemoHeader.db_typ  = MemodBaseIV;
     582          14 :             else if (m_aMemoHeader.db_size > 1 && m_aMemoHeader.db_size == 512)
     583             :             {
     584             :                 // There are files using size specification, though they are dBase-files
     585             :                 char sHeader[4];
     586           0 :                 m_pMemoStream->Seek(m_aMemoHeader.db_size);
     587           0 :                 m_pMemoStream->Read(sHeader,4);
     588             : 
     589           0 :                 if ((m_pMemoStream->GetErrorCode() != ERRCODE_NONE) || ((sal_uInt8)sHeader[0]) != 0xFF || ((sal_uInt8)sHeader[1]) != 0xFF || ((sal_uInt8)sHeader[2]) != 0x08)
     590           0 :                     m_aMemoHeader.db_typ  = MemodBaseIII;
     591             :                 else
     592           0 :                     m_aMemoHeader.db_typ  = MemodBaseIV;
     593             :             }
     594             :             else
     595             :             {
     596          14 :                 m_aMemoHeader.db_typ  = MemodBaseIII;
     597          14 :                 m_aMemoHeader.db_size = 512;
     598             :             }
     599          14 :             break;
     600             :         case VisualFoxPro:
     601             :         case VisualFoxProAuto:
     602             :         case FoxProMemo:
     603           0 :             m_aMemoHeader.db_typ    = MemoFoxPro;
     604           0 :             m_pMemoStream->Seek(6L);
     605           0 :             m_pMemoStream->SetNumberFormatInt(NUMBERFORMAT_INT_BIGENDIAN);
     606           0 :             (*m_pMemoStream).ReadUInt16( m_aMemoHeader.db_size );
     607           0 :             break;
     608             :         default:
     609             :             SAL_WARN( "connectivity.drivers", "ODbaseTable::ReadMemoHeader: unsupported memo type!" );
     610           0 :             break;
     611             :     }
     612          14 :     return true;
     613             : }
     614             : 
     615          60 : OUString ODbaseTable::getEntry(OConnection* _pConnection,const OUString& _sName )
     616             : {
     617          60 :     OUString sURL;
     618             :     try
     619             :     {
     620          60 :         Reference< XResultSet > xDir = _pConnection->getDir()->getStaticResultSet();
     621         120 :         Reference< XRow> xRow(xDir,UNO_QUERY);
     622         120 :         OUString sName;
     623         120 :         OUString sExt;
     624         120 :         INetURLObject aURL;
     625          60 :         static const OUString s_sSeparator("/");
     626          60 :         xDir->beforeFirst();
     627         260 :         while(xDir->next())
     628             :         {
     629         200 :             sName = xRow->getString(1);
     630         200 :             aURL.SetSmartProtocol(INET_PROT_FILE);
     631         200 :             OUString sUrl = _pConnection->getURL() +  s_sSeparator + sName;
     632         200 :             aURL.SetSmartURL( sUrl );
     633             : 
     634             :             // cut the extension
     635         200 :             sExt = aURL.getExtension();
     636             : 
     637             :             // name and extension have to coincide
     638         200 :             if ( _pConnection->matchesExtension( sExt ) )
     639             :             {
     640         100 :                 sName = sName.replaceAt(sName.getLength() - (sExt.getLength() + 1), sExt.getLength() + 1, OUString());
     641         100 :                 if ( sName == _sName )
     642             :                 {
     643          60 :                     Reference< XContentAccess > xContentAccess( xDir, UNO_QUERY );
     644          60 :                     sURL = xContentAccess->queryContentIdentifierString();
     645          60 :                     break;
     646             :                 }
     647             :             }
     648         140 :         }
     649         120 :         xDir->beforeFirst(); // move back to before first record
     650             :     }
     651           0 :     catch(const Exception&)
     652             :     {
     653             :         OSL_ASSERT(false);
     654             :     }
     655          60 :     return sURL;
     656             : }
     657             : 
     658          30 : void ODbaseTable::refreshColumns()
     659             : {
     660          30 :     ::osl::MutexGuard aGuard( m_aMutex );
     661             : 
     662          60 :     TStringVector aVector;
     663          30 :     aVector.reserve(m_aColumns->get().size());
     664             : 
     665         640 :     for(OSQLColumns::Vector::const_iterator aIter = m_aColumns->get().begin();aIter != m_aColumns->get().end();++aIter)
     666         610 :         aVector.push_back(Reference< XNamed>(*aIter,UNO_QUERY)->getName());
     667             : 
     668          30 :     if(m_pColumns)
     669           0 :         m_pColumns->reFill(aVector);
     670             :     else
     671          60 :         m_pColumns  = new ODbaseColumns(this,m_aMutex,aVector);
     672          30 : }
     673             : 
     674          30 : void ODbaseTable::refreshIndexes()
     675             : {
     676          30 :     TStringVector aVector;
     677          30 :     if(m_pFileStream && (!m_pIndexes || m_pIndexes->getCount() == 0))
     678             :     {
     679          30 :         INetURLObject aURL;
     680          30 :         aURL.SetURL(getEntry(m_pConnection,m_Name));
     681             : 
     682          30 :         aURL.setExtension("inf");
     683          60 :         Config aInfFile(aURL.getFSysPath(INetURLObject::FSYS_DETECT));
     684          30 :         aInfFile.SetGroup(dBASE_III_GROUP);
     685          30 :         sal_uInt16 nKeyCnt = aInfFile.GetKeyCount();
     686          60 :         OString aKeyName;
     687             : 
     688          30 :         for (sal_uInt16 nKey = 0; nKey < nKeyCnt; nKey++)
     689             :         {
     690             :             // Refences the key an index-file?
     691           0 :             aKeyName = aInfFile.GetKeyName( nKey );
     692             :             //...if yes, add the index list of the table
     693           0 :             if (aKeyName.copy(0,3).equals("NDX"))
     694             :             {
     695           0 :                 OString aIndexName = aInfFile.ReadKey(aKeyName);
     696           0 :                 aURL.setName(OStringToOUString(aIndexName, m_eEncoding));
     697             :                 try
     698             :                 {
     699           0 :                     Content aCnt(aURL.GetMainURL(INetURLObject::NO_DECODE),Reference<XCommandEnvironment>(), comphelper::getProcessComponentContext());
     700           0 :                     if (aCnt.isDocument())
     701             :                     {
     702           0 :                         aVector.push_back(aURL.getBase());
     703           0 :                     }
     704             :                 }
     705           0 :                 catch(const Exception&) // an exception is thrown when no file exists
     706             :                 {
     707           0 :                 }
     708             :             }
     709          30 :         }
     710             :     }
     711          30 :     if(m_pIndexes)
     712           0 :         m_pIndexes->reFill(aVector);
     713             :     else
     714          30 :         m_pIndexes  = new ODbaseIndexes(this,m_aMutex,aVector);
     715          30 : }
     716             : 
     717             : 
     718          30 : void SAL_CALL ODbaseTable::disposing(void)
     719             : {
     720          30 :     OFileTable::disposing();
     721          30 :     ::osl::MutexGuard aGuard(m_aMutex);
     722          30 :     m_aColumns = NULL;
     723          30 : }
     724             : 
     725           0 : Sequence< Type > SAL_CALL ODbaseTable::getTypes(  ) throw(RuntimeException, std::exception)
     726             : {
     727           0 :     Sequence< Type > aTypes = OTable_TYPEDEF::getTypes();
     728           0 :     ::std::vector<Type> aOwnTypes;
     729           0 :     aOwnTypes.reserve(aTypes.getLength());
     730             : 
     731           0 :     const Type* pBegin = aTypes.getConstArray();
     732           0 :     const Type* pEnd = pBegin + aTypes.getLength();
     733           0 :     for(;pBegin != pEnd;++pBegin)
     734             :     {
     735           0 :         if(!(*pBegin == cppu::UnoType<XKeysSupplier>::get()||
     736           0 :             *pBegin == cppu::UnoType<XDataDescriptorFactory>::get()))
     737             :         {
     738           0 :             aOwnTypes.push_back(*pBegin);
     739             :         }
     740             :     }
     741           0 :     aOwnTypes.push_back(cppu::UnoType<com::sun::star::lang::XUnoTunnel>::get());
     742           0 :     Type *pTypes = aOwnTypes.empty() ? 0 : &aOwnTypes[0];
     743           0 :     return Sequence< Type >(pTypes, aOwnTypes.size());
     744             : }
     745             : 
     746             : 
     747        5496 : Any SAL_CALL ODbaseTable::queryInterface( const Type & rType ) throw(RuntimeException, std::exception)
     748             : {
     749       10992 :     if( rType == cppu::UnoType<XKeysSupplier>::get()||
     750        5496 :         rType == cppu::UnoType<XDataDescriptorFactory>::get())
     751           0 :         return Any();
     752             : 
     753        5496 :     Any aRet = OTable_TYPEDEF::queryInterface(rType);
     754        5496 :     return aRet.hasValue() ? aRet : ::cppu::queryInterface(rType,static_cast< ::com::sun::star::lang::XUnoTunnel*> (this));
     755             : }
     756             : 
     757             : 
     758         122 : Sequence< sal_Int8 > ODbaseTable::getUnoTunnelImplementationId()
     759             : {
     760             :     static ::cppu::OImplementationId * pId = 0;
     761         122 :     if (! pId)
     762             :     {
     763           8 :         ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
     764           8 :         if (! pId)
     765             :         {
     766           8 :             static ::cppu::OImplementationId aId;
     767           8 :             pId = &aId;
     768           8 :         }
     769             :     }
     770         122 :     return pId->getImplementationId();
     771             : }
     772             : 
     773             : // com::sun::star::lang::XUnoTunnel
     774             : 
     775         122 : sal_Int64 ODbaseTable::getSomething( const Sequence< sal_Int8 > & rId ) throw (RuntimeException, std::exception)
     776             : {
     777         488 :     return (rId.getLength() == 16 && 0 == memcmp(getUnoTunnelImplementationId().getConstArray(),  rId.getConstArray(), 16 ) )
     778             :                 ? reinterpret_cast< sal_Int64 >( this )
     779         366 :                 : ODbaseTable_BASE::getSomething(rId);
     780             : }
     781             : 
     782        4478 : bool ODbaseTable::fetchRow(OValueRefRow& _rRow, const OSQLColumns & _rCols, bool _bUseTableDefs, bool bRetrieveData)
     783             : {
     784        4478 :     if (!m_pBuffer)
     785           0 :         return false;
     786             : 
     787             :     // Read the data
     788        4478 :     bool bIsCurRecordDeleted = (char)m_pBuffer[0] == '*';
     789             : 
     790             :     // only read the bookmark
     791             : 
     792             :     // Mark record as deleted
     793        4478 :     _rRow->setDeleted(bIsCurRecordDeleted);
     794        4478 :     *(_rRow->get())[0] = m_nFilePos;
     795             : 
     796        4478 :     if (!bRetrieveData)
     797         228 :         return true;
     798             : 
     799        4250 :     sal_Size nByteOffset = 1;
     800             :     // Fields:
     801        4250 :     OSQLColumns::Vector::const_iterator aIter = _rCols.get().begin();
     802        4250 :     OSQLColumns::Vector::const_iterator aEnd  = _rCols.get().end();
     803        4250 :     const sal_Size nCount = _rRow->get().size();
     804      130800 :     for (sal_Size i = 1; aIter != aEnd && nByteOffset <= m_nBufferSize && i < nCount;++aIter, i++)
     805             :     {
     806             :         // Lengths depending on data type:
     807      126550 :         sal_Int32 nLen = 0;
     808      126550 :         sal_Int32 nType = 0;
     809      126550 :         if(_bUseTableDefs)
     810             :         {
     811      126550 :             nLen    = m_aPrecisions[i-1];
     812      126550 :             nType   = m_aTypes[i-1];
     813             :         }
     814             :         else
     815             :         {
     816           0 :             (*aIter)->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRECISION)) >>= nLen;
     817           0 :             (*aIter)->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE))      >>= nType;
     818             :         }
     819             : 
     820      126550 :         switch(nType)
     821             :         {
     822             :             case DataType::INTEGER:
     823             :             case DataType::DOUBLE:
     824             :             case DataType::TIMESTAMP:
     825             :             case DataType::DATE:
     826             :             case DataType::BIT:
     827             :             case DataType::LONGVARCHAR:
     828             :             case DataType::LONGVARBINARY:
     829       69130 :                 nLen = m_aRealFieldLengths[i-1];
     830       69130 :                 break;
     831             :             case DataType::DECIMAL:
     832        1300 :                 if(_bUseTableDefs)
     833        1300 :                     nLen = SvDbaseConverter::ConvertPrecisionToDbase(nLen,m_aScales[i-1]);
     834             :                 else
     835           0 :                     nLen = SvDbaseConverter::ConvertPrecisionToDbase(nLen,getINT32((*aIter)->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCALE))));
     836        1300 :                 break;  // the sign and the comma
     837             : 
     838             :             case DataType::BINARY:
     839             :             case DataType::OTHER:
     840           0 :                 nByteOffset += nLen;
     841       54762 :                 continue;
     842             :         }
     843             : 
     844             :         // Is the variable bound?
     845      126550 :         if ( !(_rRow->get())[i]->isBound() )
     846             :         {
     847             :             // No - next field.
     848       37480 :             nByteOffset += nLen;
     849             :             OSL_ENSURE( nByteOffset <= m_nBufferSize ,"ByteOffset > m_nBufferSize!");
     850       37480 :             continue;
     851             :         } // if ( !(_rRow->get())[i]->isBound() )
     852       89070 :         if ( ( nByteOffset + nLen) > m_nBufferSize )
     853           0 :             break; // length doesn't match buffer size.
     854             : 
     855       89070 :         char *pData = (char *) (m_pBuffer + nByteOffset);
     856             : 
     857       89070 :         if (nType == DataType::CHAR || nType == DataType::VARCHAR)
     858             :         {
     859       39800 :             sal_Int32 nLastPos = -1;
     860    10095960 :             for (sal_Int32 k = 0; k < nLen; ++k)
     861             :             {
     862    10056160 :                 if (pData[k] != ' ')
     863             :                     // Record last non-empty position.
     864       92840 :                     nLastPos = k;
     865             :             }
     866       39800 :             if (nLastPos < 0)
     867             :             {
     868             :                 // Empty string.  Skip it.
     869       25010 :                 (_rRow->get())[i]->setNull();
     870             :             }
     871             :             else
     872             :             {
     873             :                 // Commit the string.  Use intern() to ref-count it.
     874       14790 :                 *(_rRow->get())[i] = OUString::intern(pData, static_cast<sal_Int32>(nLastPos+1), m_eEncoding);
     875       39800 :             }
     876             :         } // if (nType == DataType::CHAR || nType == DataType::VARCHAR)
     877       49270 :         else if ( DataType::TIMESTAMP == nType )
     878             :         {
     879           0 :             sal_Int32 nDate = 0,nTime = 0;
     880           0 :             memcpy(&nDate, pData, 4);
     881           0 :             memcpy(&nTime, pData+ 4, 4);
     882           0 :             if ( !nDate && !nTime )
     883             :             {
     884           0 :                 (_rRow->get())[i]->setNull();
     885             :             }
     886             :             else
     887             :             {
     888           0 :                 ::com::sun::star::util::DateTime aDateTime;
     889           0 :                 lcl_CalDate(nDate,nTime,aDateTime);
     890           0 :                 *(_rRow->get())[i] = aDateTime;
     891             :             }
     892             :         }
     893       49270 :         else if ( DataType::INTEGER == nType )
     894             :         {
     895           0 :             sal_Int32 nValue = 0;
     896           0 :             if (static_cast<size_t>(nLen) > sizeof(nValue))
     897           0 :                 return false;
     898           0 :             memcpy(&nValue, pData, nLen);
     899           0 :             *(_rRow->get())[i] = nValue;
     900             :         }
     901       49270 :         else if ( DataType::DOUBLE == nType )
     902             :         {
     903           0 :             double d = 0.0;
     904           0 :             if (getBOOL((*aIter)->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISCURRENCY)))) // Currency is treated separately
     905             :             {
     906           0 :                 sal_Int64 nValue = 0;
     907           0 :                 if (static_cast<size_t>(nLen) > sizeof(nValue))
     908           0 :                     return false;
     909           0 :                 memcpy(&nValue, pData, nLen);
     910             : 
     911           0 :                 if ( m_aScales[i-1] )
     912           0 :                     d = (double)(nValue / pow(10.0,(int)m_aScales[i-1]));
     913             :                 else
     914           0 :                     d = (double)(nValue);
     915             :             }
     916             :             else
     917             :             {
     918           0 :                 if (static_cast<size_t>(nLen) > sizeof(d))
     919           0 :                     return false;
     920           0 :                 memcpy(&d, pData, nLen);
     921             :             }
     922             : 
     923           0 :             *(_rRow->get())[i] = d;
     924             :         }
     925             :         else
     926             :         {
     927       49270 :             sal_Int32 nPos1 = -1, nPos2 = -1;
     928             :             // If the string contains Nul-characters, then convert them to blanks!
     929      548778 :             for (sal_Int32 k = 0; k < nLen; k++)
     930             :             {
     931      499508 :                 if (pData[k] == '\0')
     932       29828 :                     pData[k] = ' ';
     933             : 
     934      499508 :                 if (pData[k] != ' ')
     935             :                 {
     936      319016 :                     if (nPos1 < 0)
     937             :                         // first non-empty char position.
     938       31988 :                         nPos1 = k;
     939             : 
     940             :                     // last non-empty char position.
     941      319016 :                     nPos2 = k;
     942             :                 }
     943             :             }
     944             : 
     945       49270 :             if (nPos1 < 0)
     946             :             {
     947             :                 // Empty string.  Skip it.
     948       17282 :                 nByteOffset += nLen;
     949       17282 :                 (_rRow->get())[i]->setNull();   // no values -> done
     950       17282 :                 continue;
     951             :             }
     952             : 
     953       31988 :             OUString aStr = OUString::intern(pData+nPos1, nPos2-nPos1+1, m_eEncoding);
     954             : 
     955       31988 :             switch (nType)
     956             :             {
     957             :                 case DataType::DATE:
     958             :                 {
     959         108 :                     if (aStr.getLength() != nLen)
     960             :                     {
     961           0 :                         (_rRow->get())[i]->setNull();
     962           0 :                         break;
     963             :                     }
     964         108 :                     const sal_uInt16  nYear   = (sal_uInt16)aStr.copy( 0, 4 ).toInt32();
     965         108 :                     const sal_uInt16  nMonth  = (sal_uInt16)aStr.copy( 4, 2 ).toInt32();
     966         108 :                     const sal_uInt16  nDay    = (sal_uInt16)aStr.copy( 6, 2 ).toInt32();
     967             : 
     968         108 :                     const ::com::sun::star::util::Date aDate(nDay,nMonth,nYear);
     969         108 :                     *(_rRow->get())[i] = aDate;
     970             :                 }
     971         108 :                 break;
     972             :                 case DataType::DECIMAL:
     973         216 :                     *(_rRow->get())[i] = ORowSetValue(aStr);
     974         216 :                 break;
     975             :                 case DataType::BIT:
     976             :                 {
     977             :                     bool b;
     978           0 :                     switch (* ((const char *)pData))
     979             :                     {
     980             :                         case 'T':
     981             :                         case 'Y':
     982           0 :                         case 'J':   b = true; break;
     983           0 :                         default:    b = false; break;
     984             :                     }
     985           0 :                     *(_rRow->get())[i] = b;
     986             :                 }
     987           0 :                 break;
     988             :                 case DataType::LONGVARBINARY:
     989             :                 case DataType::BINARY:
     990             :                 case DataType::LONGVARCHAR:
     991             :                 {
     992       31664 :                     const long nBlockNo = aStr.toInt32();   // read blocknumber
     993       31664 :                     if (nBlockNo > 0 && m_pMemoStream) // Read data from memo-file, only if
     994             :                     {
     995       63328 :                         if ( !ReadMemo(nBlockNo, (_rRow->get())[i]->get()) )
     996           0 :                             break;
     997             :                     }
     998             :                     else
     999           0 :                         (_rRow->get())[i]->setNull();
    1000       31664 :                 }   break;
    1001             :                 default:
    1002             :                     SAL_WARN( "connectivity.drivers","Falscher Type");
    1003             :             }
    1004       31988 :             (_rRow->get())[i]->setTypeKind(nType);
    1005             :         }
    1006             : 
    1007       71788 :         nByteOffset += nLen;
    1008             :         OSL_ENSURE( nByteOffset <= m_nBufferSize ,"ByteOffset > m_nBufferSize!");
    1009             :     }
    1010        4250 :     return true;
    1011             : }
    1012             : 
    1013             : 
    1014          30 : void ODbaseTable::FileClose()
    1015             : {
    1016          30 :     ::osl::MutexGuard aGuard(m_aMutex);
    1017             :     // if not everything has been written yet
    1018          30 :     if (m_pMemoStream && m_pMemoStream->IsWritable())
    1019          14 :         m_pMemoStream->Flush();
    1020             : 
    1021          30 :     delete m_pMemoStream;
    1022          30 :     m_pMemoStream = NULL;
    1023             : 
    1024          30 :     ODbaseTable_BASE::FileClose();
    1025          30 : }
    1026             : 
    1027           0 : bool ODbaseTable::CreateImpl()
    1028             : {
    1029             :     OSL_ENSURE(!m_pFileStream, "SequenceError");
    1030             : 
    1031           0 :     if ( m_pConnection->isCheckEnabled() && ::dbtools::convertName2SQLName(m_Name,OUString()) != m_Name )
    1032             :     {
    1033           0 :         const OUString sError( getConnection()->getResources().getResourceStringWithSubstitution(
    1034             :                 STR_SQL_NAME_ERROR,
    1035             :                 "$name$", m_Name
    1036           0 :              ) );
    1037           0 :         ::dbtools::throwGenericSQLException( sError, *this );
    1038             :     }
    1039             : 
    1040           0 :     INetURLObject aURL;
    1041           0 :     aURL.SetSmartProtocol(INET_PROT_FILE);
    1042           0 :     OUString aName = getEntry(m_pConnection, m_Name);
    1043           0 :     if(aName.isEmpty())
    1044             :     {
    1045           0 :         OUString aIdent = m_pConnection->getContent()->getIdentifier()->getContentIdentifier();
    1046           0 :         if ( aIdent.lastIndexOf('/') != (aIdent.getLength()-1) )
    1047           0 :             aIdent += "/";
    1048           0 :         aIdent += m_Name;
    1049           0 :         aName = aIdent.getStr();
    1050             :     }
    1051           0 :     aURL.SetURL(aName);
    1052             : 
    1053           0 :     if ( !m_pConnection->matchesExtension( aURL.getExtension() ) )
    1054           0 :         aURL.setExtension(m_pConnection->getExtension());
    1055             : 
    1056             :     try
    1057             :     {
    1058           0 :         Content aContent(aURL.GetMainURL(INetURLObject::NO_DECODE),Reference<XCommandEnvironment>(), comphelper::getProcessComponentContext());
    1059           0 :         if (aContent.isDocument())
    1060             :         {
    1061             :             // Only if the file exists with length > 0 raise an error
    1062           0 :             SvStream* pFileStream = createStream_simpleError( aURL.GetMainURL(INetURLObject::NO_DECODE),STREAM_READ);
    1063             : 
    1064           0 :             if (pFileStream && pFileStream->Seek(STREAM_SEEK_TO_END))
    1065             :             {
    1066           0 :                 return false;
    1067             :             }
    1068           0 :             delete pFileStream;
    1069           0 :         }
    1070             :     }
    1071           0 :     catch(const Exception&) // an exception is thrown when no file exists
    1072             :     {
    1073             :     }
    1074             : 
    1075           0 :     bool bMemoFile = false;
    1076             : 
    1077           0 :     bool bOk = CreateFile(aURL, bMemoFile);
    1078             : 
    1079           0 :     FileClose();
    1080             : 
    1081           0 :     if (!bOk)
    1082             :     {
    1083             :         try
    1084             :         {
    1085           0 :             Content aContent(aURL.GetMainURL(INetURLObject::NO_DECODE),Reference<XCommandEnvironment>(), comphelper::getProcessComponentContext());
    1086           0 :             aContent.executeCommand( "delete", css::uno::Any( true ) );
    1087             :         }
    1088           0 :         catch(const Exception&) // an exception is thrown when no file exists
    1089             :         {
    1090             :         }
    1091           0 :         return false;
    1092             :     }
    1093             : 
    1094           0 :     if (bMemoFile)
    1095             :     {
    1096           0 :         OUString aExt = aURL.getExtension();
    1097           0 :         aURL.setExtension("dbt");                      // extension for memo file
    1098             : 
    1099           0 :         bool bMemoAlreadyExists = false;
    1100             :         try
    1101             :         {
    1102           0 :             Content aMemo1Content(aURL.GetMainURL(INetURLObject::NO_DECODE),Reference<XCommandEnvironment>(), comphelper::getProcessComponentContext());
    1103           0 :             bMemoAlreadyExists = aMemo1Content.isDocument();
    1104             :         }
    1105           0 :         catch(const Exception&) // an exception is thrown when no file exists
    1106             :         {
    1107             :         }
    1108           0 :         if (bMemoAlreadyExists)
    1109             :         {
    1110           0 :             aURL.setExtension(aExt);      // kill dbf file
    1111             :             try
    1112             :             {
    1113           0 :                 Content aMemoContent(aURL.GetMainURL(INetURLObject::NO_DECODE),Reference<XCommandEnvironment>(), comphelper::getProcessComponentContext());
    1114           0 :                 aMemoContent.executeCommand( "delete", css::uno::Any( true ) );
    1115             :             }
    1116           0 :             catch(const Exception&)
    1117             :             {
    1118             : 
    1119           0 :                 const OUString sError( getConnection()->getResources().getResourceStringWithSubstitution(
    1120             :                         STR_COULD_NOT_DELETE_FILE,
    1121             :                         "$name$", aName
    1122           0 :                      ) );
    1123           0 :                 ::dbtools::throwGenericSQLException( sError, *this );
    1124             :             }
    1125             :         }
    1126           0 :         if (!CreateMemoFile(aURL))
    1127             :         {
    1128           0 :             aURL.setExtension(aExt);      // kill dbf file
    1129             :             try
    1130             :             {
    1131           0 :                 Content aMemoContent(aURL.GetMainURL(INetURLObject::NO_DECODE),Reference<XCommandEnvironment>(), comphelper::getProcessComponentContext());
    1132           0 :                 aMemoContent.executeCommand( "delete", css::uno::Any( true ) );
    1133             :             }
    1134           0 :             catch(const ContentCreationException&)
    1135             :             {
    1136           0 :                 const OUString sError( getConnection()->getResources().getResourceStringWithSubstitution(
    1137             :                         STR_COULD_NOT_DELETE_FILE,
    1138             :                         "$name$", aName
    1139           0 :                      ) );
    1140           0 :                 ::dbtools::throwGenericSQLException( sError, *this );
    1141             :             }
    1142           0 :             return false;
    1143             :         }
    1144           0 :         m_aHeader.db_typ = dBaseIIIMemo;
    1145             :     }
    1146             :     else
    1147           0 :         m_aHeader.db_typ = dBaseIII;
    1148             : 
    1149           0 :     return true;
    1150             : }
    1151             : 
    1152           0 : void ODbaseTable::throwInvalidColumnType(const sal_uInt16 _nErrorId,const OUString& _sColumnName)
    1153             : {
    1154             :     try
    1155             :     {
    1156             :         // we have to drop the file because it is corrupted now
    1157           0 :         DropImpl();
    1158             :     }
    1159           0 :     catch(const Exception&)
    1160             :     {
    1161             :     }
    1162             : 
    1163           0 :     const OUString sError( getConnection()->getResources().getResourceStringWithSubstitution(
    1164             :             _nErrorId,
    1165             :             "$columnname$", _sColumnName
    1166           0 :          ) );
    1167           0 :     ::dbtools::throwGenericSQLException( sError, *this );
    1168           0 : }
    1169             : 
    1170             : // creates in principle dBase IV file format
    1171           0 : bool ODbaseTable::CreateFile(const INetURLObject& aFile, bool& bCreateMemo)
    1172             : {
    1173           0 :     bCreateMemo = false;
    1174           0 :     Date aDate( Date::SYSTEM );                     // current date
    1175             : 
    1176           0 :     m_pFileStream = createStream_simpleError( aFile.GetMainURL(INetURLObject::NO_DECODE),STREAM_READWRITE | STREAM_SHARE_DENYWRITE | STREAM_TRUNC );
    1177             : 
    1178           0 :     if (!m_pFileStream)
    1179           0 :         return false;
    1180             : 
    1181           0 :     sal_uInt8 nDbaseType = dBaseIII;
    1182           0 :     Reference<XIndexAccess> xColumns(getColumns(),UNO_QUERY);
    1183           0 :     Reference<XPropertySet> xCol;
    1184           0 :     const OUString sPropType = OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE);
    1185             : 
    1186             :     try
    1187             :     {
    1188           0 :         const sal_Int32 nCount = xColumns->getCount();
    1189           0 :         for(sal_Int32 i=0;i<nCount;++i)
    1190             :         {
    1191           0 :             xColumns->getByIndex(i) >>= xCol;
    1192             :             OSL_ENSURE(xCol.is(),"This should be a column!");
    1193             : 
    1194           0 :             switch (getINT32(xCol->getPropertyValue(sPropType)))
    1195             :             {
    1196             :                 case DataType::DOUBLE:
    1197             :                 case DataType::INTEGER:
    1198             :                 case DataType::TIMESTAMP:
    1199             :                 case DataType::LONGVARBINARY:
    1200           0 :                     nDbaseType = VisualFoxPro;
    1201           0 :                     i = nCount; // no more columns need to be checked
    1202           0 :                     break;
    1203             :             } // switch (getINT32(xCol->getPropertyValue(sPropType)))
    1204             :         }
    1205             :     }
    1206           0 :     catch ( const Exception& e )
    1207             :     {
    1208             :         (void)e;
    1209             : 
    1210             :         try
    1211             :         {
    1212             :             // we have to drop the file because it is corrupted now
    1213           0 :             DropImpl();
    1214             :         }
    1215           0 :         catch(const Exception&) { }
    1216           0 :         throw;
    1217             :     }
    1218             : 
    1219             :     char aBuffer[21];               // write buffer
    1220           0 :     memset(aBuffer,0,sizeof(aBuffer));
    1221             : 
    1222           0 :     m_pFileStream->Seek(0L);
    1223           0 :     (*m_pFileStream).WriteUChar( nDbaseType );                              // dBase format
    1224           0 :     (*m_pFileStream).WriteUChar( (aDate.GetYear() % 100) );                 // current date
    1225             : 
    1226             : 
    1227           0 :     (*m_pFileStream).WriteUChar( aDate.GetMonth() );
    1228           0 :     (*m_pFileStream).WriteUChar( aDate.GetDay() );
    1229           0 :     (*m_pFileStream).WriteUInt32( 0 );                                             // number of data records
    1230           0 :     (*m_pFileStream).WriteUInt16( ((m_pColumns->getCount()+1) * 32 + 1) );  // header information,
    1231             :                                                                         // pColumns contains always an additional column
    1232           0 :     (*m_pFileStream).WriteUInt16( 0 );                                     // record length will be determined later
    1233           0 :     m_pFileStream->Write(aBuffer, 20);
    1234             : 
    1235           0 :     sal_uInt16 nRecLength = 1;                                              // Length 1 for deleted flag
    1236           0 :     sal_Int32  nMaxFieldLength = m_pConnection->getMetaData()->getMaxColumnNameLength();
    1237           0 :     OUString aName;
    1238           0 :     const OUString sPropName = OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME);
    1239           0 :     const OUString sPropPrec = OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRECISION);
    1240           0 :     const OUString sPropScale = OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCALE);
    1241             : 
    1242             :     try
    1243             :     {
    1244           0 :         const sal_Int32 nCount = xColumns->getCount();
    1245           0 :         for(sal_Int32 i=0;i<nCount;++i)
    1246             :         {
    1247           0 :             xColumns->getByIndex(i) >>= xCol;
    1248             :             OSL_ENSURE(xCol.is(),"This should be a column!");
    1249             : 
    1250           0 :             char cTyp( 'C' );
    1251             : 
    1252           0 :             xCol->getPropertyValue(sPropName) >>= aName;
    1253             : 
    1254           0 :             OString aCol;
    1255           0 :             if ( DBTypeConversion::convertUnicodeString( aName, aCol, m_eEncoding ) > nMaxFieldLength)
    1256             :             {
    1257           0 :                 throwInvalidColumnType( STR_INVALID_COLUMN_NAME_LENGTH, aName );
    1258             :             }
    1259             : 
    1260           0 :             (*m_pFileStream).WriteCharPtr( aCol.getStr() );
    1261           0 :             m_pFileStream->Write(aBuffer, 11 - aCol.getLength());
    1262             : 
    1263           0 :             sal_Int32 nPrecision = 0;
    1264           0 :             xCol->getPropertyValue(sPropPrec) >>= nPrecision;
    1265           0 :             sal_Int32 nScale = 0;
    1266           0 :             xCol->getPropertyValue(sPropScale) >>= nScale;
    1267             : 
    1268           0 :             bool bBinary = false;
    1269             : 
    1270           0 :             switch (getINT32(xCol->getPropertyValue(sPropType)))
    1271             :             {
    1272             :                 case DataType::CHAR:
    1273             :                 case DataType::VARCHAR:
    1274           0 :                     cTyp = 'C';
    1275           0 :                     break;
    1276             :                 case DataType::DOUBLE:
    1277           0 :                     if (getBOOL(xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISCURRENCY)))) // Currency will be treated separately
    1278           0 :                         cTyp = 'Y';
    1279             :                     else
    1280           0 :                         cTyp = 'B';
    1281           0 :                     break;
    1282             :                 case DataType::INTEGER:
    1283           0 :                     cTyp = 'I';
    1284           0 :                     break;
    1285             :                 case DataType::TINYINT:
    1286             :                 case DataType::SMALLINT:
    1287             :                 case DataType::BIGINT:
    1288             :                 case DataType::DECIMAL:
    1289             :                 case DataType::NUMERIC:
    1290             :                 case DataType::REAL:
    1291           0 :                     cTyp = 'N';                             // only dBase 3 format
    1292           0 :                     break;
    1293             :                 case DataType::TIMESTAMP:
    1294           0 :                     cTyp = 'T';
    1295           0 :                     break;
    1296             :                 case DataType::DATE:
    1297           0 :                     cTyp = 'D';
    1298           0 :                     break;
    1299             :                 case DataType::BIT:
    1300           0 :                     cTyp = 'L';
    1301           0 :                     break;
    1302             :                 case DataType::LONGVARBINARY:
    1303           0 :                     bBinary = true;
    1304             :                     // run through
    1305             :                 case DataType::LONGVARCHAR:
    1306           0 :                     cTyp = 'M';
    1307           0 :                     break;
    1308             :                 default:
    1309             :                     {
    1310           0 :                         throwInvalidColumnType(STR_INVALID_COLUMN_TYPE, aName);
    1311             :                     }
    1312             :             }
    1313             : 
    1314           0 :             (*m_pFileStream).WriteChar( cTyp );
    1315           0 :             if ( nDbaseType == VisualFoxPro )
    1316           0 :                 (*m_pFileStream).WriteUInt32( nRecLength-1 );
    1317             :             else
    1318           0 :                 m_pFileStream->Write(aBuffer, 4);
    1319             : 
    1320           0 :             switch(cTyp)
    1321             :             {
    1322             :                 case 'C':
    1323             :                     OSL_ENSURE(nPrecision < 255, "ODbaseTable::Create: Column zu lang!");
    1324           0 :                     if (nPrecision > 254)
    1325             :                     {
    1326           0 :                         throwInvalidColumnType(STR_INVALID_COLUMN_PRECISION, aName);
    1327             :                     }
    1328           0 :                     (*m_pFileStream).WriteUChar( std::min((unsigned)nPrecision, 255U) );      // field length
    1329           0 :                     nRecLength = nRecLength + (sal_uInt16)::std::min((sal_uInt16)nPrecision, (sal_uInt16)255UL);
    1330           0 :                     (*m_pFileStream).WriteUChar( 0 );                                                                // decimals
    1331           0 :                     break;
    1332             :                 case 'F':
    1333             :                 case 'N':
    1334             :                     OSL_ENSURE(nPrecision >=  nScale,
    1335             :                             "ODbaseTable::Create: Feldlaenge muss groesser Nachkommastellen sein!");
    1336           0 :                     if (nPrecision <  nScale)
    1337             :                     {
    1338           0 :                         throwInvalidColumnType(STR_INVALID_PRECISION_SCALE, aName);
    1339             :                     }
    1340           0 :                     if (getBOOL(xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISCURRENCY)))) // Currency will be treated separately
    1341             :                     {
    1342           0 :                         (*m_pFileStream).WriteUChar( 10 );          // standard length
    1343           0 :                         (*m_pFileStream).WriteUChar( 4 );
    1344           0 :                         nRecLength += 10;
    1345             :                     }
    1346             :                     else
    1347             :                     {
    1348           0 :                         sal_Int32 nPrec = SvDbaseConverter::ConvertPrecisionToDbase(nPrecision,nScale);
    1349             : 
    1350           0 :                         (*m_pFileStream).WriteUChar( ( nPrec) );
    1351           0 :                         (*m_pFileStream).WriteUChar( nScale );
    1352           0 :                         nRecLength += (sal_uInt16)nPrec;
    1353             :                     }
    1354           0 :                     break;
    1355             :                 case 'L':
    1356           0 :                     (*m_pFileStream).WriteUChar( 1 );
    1357           0 :                     (*m_pFileStream).WriteUChar( 0 );
    1358           0 :                     ++nRecLength;
    1359           0 :                     break;
    1360             :                 case 'I':
    1361           0 :                     (*m_pFileStream).WriteUChar( 4 );
    1362           0 :                     (*m_pFileStream).WriteUChar( 0 );
    1363           0 :                     nRecLength += 4;
    1364           0 :                     break;
    1365             :                 case 'Y':
    1366             :                 case 'B':
    1367             :                 case 'T':
    1368             :                 case 'D':
    1369           0 :                     (*m_pFileStream).WriteUChar( 8 );
    1370           0 :                     (*m_pFileStream).WriteUChar( 0 );
    1371           0 :                     nRecLength += 8;
    1372           0 :                     break;
    1373             :                 case 'M':
    1374           0 :                     bCreateMemo = true;
    1375           0 :                     (*m_pFileStream).WriteUChar( 10 );
    1376           0 :                     (*m_pFileStream).WriteUChar( 0 );
    1377           0 :                     nRecLength += 10;
    1378           0 :                     if ( bBinary )
    1379           0 :                         aBuffer[0] = 0x06;
    1380           0 :                     break;
    1381             :                 default:
    1382           0 :                     throwInvalidColumnType(STR_INVALID_COLUMN_TYPE, aName);
    1383             :             }
    1384           0 :             m_pFileStream->Write(aBuffer, 14);
    1385           0 :             aBuffer[0] = 0x00;
    1386           0 :         }
    1387             : 
    1388           0 :         (*m_pFileStream).WriteUChar( FIELD_DESCRIPTOR_TERMINATOR );              // end of header
    1389           0 :         (*m_pFileStream).WriteChar( (char)DBF_EOL );
    1390           0 :         m_pFileStream->Seek(10L);
    1391           0 :         (*m_pFileStream).WriteUInt16( nRecLength );                                     // set record length afterwards
    1392             : 
    1393           0 :         if (bCreateMemo)
    1394             :         {
    1395           0 :             m_pFileStream->Seek(0L);
    1396           0 :             if (nDbaseType == VisualFoxPro)
    1397           0 :                 (*m_pFileStream).WriteUChar( FoxProMemo );
    1398             :             else
    1399           0 :                 (*m_pFileStream).WriteUChar( dBaseIIIMemo );
    1400             :         } // if (bCreateMemo)
    1401             :     }
    1402           0 :     catch ( const Exception& e )
    1403             :     {
    1404             :         (void)e;
    1405             : 
    1406             :         try
    1407             :         {
    1408             :             // we have to drop the file because it is corrupted now
    1409           0 :             DropImpl();
    1410             :         }
    1411           0 :         catch(const Exception&) { }
    1412           0 :         throw;
    1413             :     }
    1414           0 :     return true;
    1415             : }
    1416             : 
    1417             : 
    1418             : // creates in principle dBase III file format
    1419           0 : bool ODbaseTable::CreateMemoFile(const INetURLObject& aFile)
    1420             : {
    1421             :     // filehandling macro for table creation
    1422           0 :     m_pMemoStream = createStream_simpleError( aFile.GetMainURL(INetURLObject::NO_DECODE),STREAM_READWRITE | STREAM_SHARE_DENYWRITE);
    1423             : 
    1424           0 :     if (!m_pMemoStream)
    1425           0 :         return false;
    1426             : 
    1427           0 :     m_pMemoStream->SetStreamSize(512);
    1428             : 
    1429           0 :     m_pMemoStream->Seek(0L);
    1430           0 :     (*m_pMemoStream).WriteUInt32( 1 );                  // pointer to the first free block
    1431             : 
    1432           0 :     m_pMemoStream->Flush();
    1433           0 :     delete m_pMemoStream;
    1434           0 :     m_pMemoStream = NULL;
    1435           0 :     return true;
    1436             : }
    1437             : 
    1438           0 : bool ODbaseTable::Drop_Static(const OUString& _sUrl, bool _bHasMemoFields, OCollection* _pIndexes )
    1439             : {
    1440           0 :     INetURLObject aURL;
    1441           0 :     aURL.SetURL(_sUrl);
    1442             : 
    1443           0 :     bool bDropped = ::utl::UCBContentHelper::Kill(aURL.GetMainURL(INetURLObject::NO_DECODE));
    1444             : 
    1445           0 :     if(bDropped)
    1446             :     {
    1447           0 :         if (_bHasMemoFields)
    1448             :         {  // delete the memo fields
    1449           0 :             aURL.setExtension("dbt");
    1450           0 :             bDropped = ::utl::UCBContentHelper::Kill(aURL.GetMainURL(INetURLObject::NO_DECODE));
    1451             :         }
    1452             : 
    1453           0 :         if(bDropped)
    1454             :         {
    1455           0 :             if(_pIndexes)
    1456             :             {
    1457             :                 try
    1458             :                 {
    1459           0 :                     sal_Int32 i = _pIndexes->getCount();
    1460           0 :                     while (i)
    1461             :                     {
    1462           0 :                         _pIndexes->dropByIndex(--i);
    1463             :                     }
    1464             :                 }
    1465           0 :                 catch(const SQLException&)
    1466             :                 {
    1467             :                 }
    1468             :             }
    1469           0 :             aURL.setExtension("inf");
    1470             : 
    1471             :             // as the inf file does not necessarily exist, we aren't allowed to use UCBContentHelper::Kill
    1472             :             try
    1473             :             {
    1474           0 :                 ::ucbhelper::Content aDeleteContent( aURL.GetMainURL( INetURLObject::NO_DECODE ), Reference< XCommandEnvironment >(), comphelper::getProcessComponentContext() );
    1475           0 :                 aDeleteContent.executeCommand( "delete", makeAny( true ) );
    1476             :             }
    1477           0 :             catch(const Exception&)
    1478             :             {
    1479             :                 // silently ignore this ....
    1480             :             }
    1481             :         }
    1482             :     }
    1483           0 :     return bDropped;
    1484             : }
    1485             : 
    1486           0 : bool ODbaseTable::DropImpl()
    1487             : {
    1488           0 :     FileClose();
    1489             : 
    1490           0 :     if(!m_pIndexes)
    1491           0 :         refreshIndexes(); // look for indexes which must be deleted as well
    1492             : 
    1493           0 :     bool bDropped = Drop_Static(getEntry(m_pConnection,m_Name),HasMemoFields(),m_pIndexes);
    1494           0 :     if(!bDropped)
    1495             :     {// we couldn't drop the table so we have to reopen it
    1496           0 :         construct();
    1497           0 :         if(m_pColumns)
    1498           0 :             m_pColumns->refresh();
    1499             :     }
    1500           0 :     return bDropped;
    1501             : }
    1502             : 
    1503             : 
    1504           2 : bool ODbaseTable::InsertRow(OValueRefVector& rRow, bool bFlush, const Reference<XIndexAccess>& _xCols)
    1505             : {
    1506             :     // fill buffer with blanks
    1507           2 :     if (!AllocBuffer())
    1508           0 :         return false;
    1509             : 
    1510           2 :     memset(m_pBuffer, 0, m_aHeader.db_slng);
    1511           2 :     m_pBuffer[0] = ' ';
    1512             : 
    1513             :     // Copy new row completely:
    1514             :     // ... and add at the end as new Record:
    1515           2 :     sal_Size nTempPos = m_nFilePos;
    1516             : 
    1517           2 :     m_nFilePos = (sal_Size)m_aHeader.db_anz + 1;
    1518           2 :     bool bInsertRow = UpdateBuffer( rRow, NULL, _xCols, true );
    1519           2 :     if ( bInsertRow )
    1520             :     {
    1521           2 :         sal_Size nFileSize = 0, nMemoFileSize = 0;
    1522             : 
    1523           2 :         nFileSize = lcl_getFileSize(*m_pFileStream);
    1524             : 
    1525           2 :         if (HasMemoFields() && m_pMemoStream)
    1526             :         {
    1527           2 :             m_pMemoStream->Seek(STREAM_SEEK_TO_END);
    1528           2 :             nMemoFileSize = m_pMemoStream->Tell();
    1529             :         }
    1530             : 
    1531           2 :         if (!WriteBuffer())
    1532             :         {
    1533           0 :             m_pFileStream->SetStreamSize(nFileSize);                // restore old size
    1534             : 
    1535           0 :             if (HasMemoFields() && m_pMemoStream)
    1536           0 :                 m_pMemoStream->SetStreamSize(nMemoFileSize);    // restore old size
    1537           0 :             m_nFilePos = nTempPos;              // restore file position
    1538             :         }
    1539             :         else
    1540             :         {
    1541           2 :             (*m_pFileStream).WriteChar( (char)DBF_EOL ); // write EOL
    1542             :             // raise number of datasets in the header:
    1543           2 :             m_pFileStream->Seek( 4L );
    1544           2 :             (*m_pFileStream).WriteUInt32( m_aHeader.db_anz + 1 );
    1545             : 
    1546             :             // if AppendOnly no flush!
    1547           2 :             if (bFlush)
    1548           2 :                 m_pFileStream->Flush();
    1549             : 
    1550             :             // raise number if successfully
    1551           2 :             m_aHeader.db_anz++;
    1552           2 :             *rRow.get()[0] = m_nFilePos;                                // set bookmark
    1553           2 :             m_nFilePos = nTempPos;
    1554             :         }
    1555             :     }
    1556             :     else
    1557           0 :         m_nFilePos = nTempPos;
    1558             : 
    1559           2 :     return bInsertRow;
    1560             : }
    1561             : 
    1562             : 
    1563           8 : bool ODbaseTable::UpdateRow(OValueRefVector& rRow, OValueRefRow& pOrgRow, const Reference<XIndexAccess>& _xCols)
    1564             : {
    1565             :     // fill buffer with blanks
    1566           8 :     if (!AllocBuffer())
    1567           0 :         return false;
    1568             : 
    1569             :     // position on desired record:
    1570           8 :     sal_Size nPos = m_aHeader.db_kopf + (long)(m_nFilePos-1) * m_aHeader.db_slng;
    1571           8 :     m_pFileStream->Seek(nPos);
    1572           8 :     m_pFileStream->Read((char*)m_pBuffer, m_aHeader.db_slng);
    1573             : 
    1574           8 :     sal_Size nMemoFileSize( 0 );
    1575           8 :     if (HasMemoFields() && m_pMemoStream)
    1576             :     {
    1577           0 :         m_pMemoStream->Seek(STREAM_SEEK_TO_END);
    1578           0 :         nMemoFileSize = m_pMemoStream->Tell();
    1579             :     }
    1580           8 :     if (!UpdateBuffer(rRow, pOrgRow, _xCols, false) || !WriteBuffer())
    1581             :     {
    1582           0 :         if (HasMemoFields() && m_pMemoStream)
    1583           0 :             m_pMemoStream->SetStreamSize(nMemoFileSize);    // restore old size
    1584             :     }
    1585             :     else
    1586             :     {
    1587           8 :         m_pFileStream->Flush();
    1588             :     }
    1589           8 :     return true;
    1590             : }
    1591             : 
    1592             : 
    1593           2 : bool ODbaseTable::DeleteRow(const OSQLColumns& _rCols)
    1594             : {
    1595             :     // Set the Delete-Flag (be it set or not):
    1596             :     // Position on desired record:
    1597           2 :     sal_Size nFilePos = m_aHeader.db_kopf + (long)(m_nFilePos-1) * m_aHeader.db_slng;
    1598           2 :     m_pFileStream->Seek(nFilePos);
    1599             : 
    1600           2 :     OValueRefRow aRow = new OValueRefVector(_rCols.get().size());
    1601             : 
    1602           2 :     if (!fetchRow(aRow,_rCols,true,true))
    1603           0 :         return false;
    1604             : 
    1605           4 :     Reference<XPropertySet> xCol;
    1606           4 :     OUString aColName;
    1607           2 :     ::comphelper::UStringMixEqual aCase(isCaseSensitive());
    1608          64 :     for (sal_uInt16 i = 0; i < m_pColumns->getCount(); i++)
    1609             :     {
    1610          62 :         Reference<XPropertySet> xIndex = isUniqueByColumnName(i);
    1611          62 :         if (xIndex.is())
    1612             :         {
    1613           0 :             xCol.set(m_pColumns->getByIndex(i), css::uno::UNO_QUERY);
    1614             :             OSL_ENSURE(xCol.is(),"ODbaseTable::DeleteRow column is null!");
    1615           0 :             if(xCol.is())
    1616             :             {
    1617           0 :                 xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME)) >>= aColName;
    1618             : 
    1619           0 :                 Reference<XUnoTunnel> xTunnel(xIndex,UNO_QUERY);
    1620             :                 OSL_ENSURE(xTunnel.is(),"No TunnelImplementation!");
    1621           0 :                 ODbaseIndex* pIndex = reinterpret_cast< ODbaseIndex* >( xTunnel->getSomething(ODbaseIndex::getUnoTunnelImplementationId()) );
    1622             :                 OSL_ENSURE(pIndex,"ODbaseTable::DeleteRow: No Index returned!");
    1623             : 
    1624           0 :                 OSQLColumns::Vector::const_iterator aIter = _rCols.get().begin();
    1625           0 :                 sal_Int32 nPos = 1;
    1626           0 :                 for(;aIter != _rCols.get().end();++aIter,++nPos)
    1627             :                 {
    1628           0 :                     if(aCase(getString((*aIter)->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_REALNAME))),aColName))
    1629           0 :                         break;
    1630             :                 }
    1631           0 :                 if (aIter == _rCols.get().end())
    1632           0 :                     continue;
    1633             : 
    1634           0 :                 pIndex->Delete(m_nFilePos,*(aRow->get())[nPos]);
    1635             :             }
    1636             :         }
    1637          62 :     }
    1638             : 
    1639           2 :     m_pFileStream->Seek(nFilePos);
    1640           2 :     (*m_pFileStream).WriteUChar( '*' ); // mark the row in the table as deleted
    1641           2 :     m_pFileStream->Flush();
    1642           4 :     return true;
    1643             : }
    1644             : 
    1645         212 : Reference<XPropertySet> ODbaseTable::isUniqueByColumnName(sal_Int32 _nColumnPos)
    1646             : {
    1647         212 :     if(!m_pIndexes)
    1648           0 :         refreshIndexes();
    1649         212 :     if(m_pIndexes->hasElements())
    1650             :     {
    1651           0 :         Reference<XPropertySet> xCol;
    1652           0 :         m_pColumns->getByIndex(_nColumnPos) >>= xCol;
    1653             :         OSL_ENSURE(xCol.is(),"ODbaseTable::isUniqueByColumnName column is null!");
    1654           0 :         OUString sColName;
    1655           0 :         xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME)) >>= sColName;
    1656             : 
    1657           0 :         Reference<XPropertySet> xIndex;
    1658           0 :         for(sal_Int32 i=0;i<m_pIndexes->getCount();++i)
    1659             :         {
    1660           0 :             xIndex.set(m_pIndexes->getByIndex(i), css::uno::UNO_QUERY);
    1661           0 :             if(xIndex.is() && getBOOL(xIndex->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISUNIQUE))))
    1662             :             {
    1663           0 :                 Reference<XNameAccess> xCols(Reference<XColumnsSupplier>(xIndex,UNO_QUERY)->getColumns());
    1664           0 :                 if(xCols->hasByName(sColName))
    1665           0 :                     return xIndex;
    1666             : 
    1667             :             }
    1668           0 :         }
    1669             :     }
    1670         212 :     return Reference<XPropertySet>();
    1671             : }
    1672             : 
    1673           0 : static double toDouble(const OString& rString)
    1674             : {
    1675           0 :     return ::rtl::math::stringToDouble( rString, '.', ',', NULL, NULL );
    1676             : }
    1677             : 
    1678             : 
    1679          10 : bool ODbaseTable::UpdateBuffer(OValueRefVector& rRow, OValueRefRow pOrgRow, const Reference<XIndexAccess>& _xCols, const bool bForceAllFields)
    1680             : {
    1681             :     OSL_ENSURE(m_pBuffer,"Buffer is NULL!");
    1682          10 :     if ( !m_pBuffer )
    1683           0 :         return false;
    1684          10 :     sal_Int32 nByteOffset  = 1;
    1685             : 
    1686             :     // Update fields:
    1687          10 :     Reference<XPropertySet> xCol;
    1688          20 :     Reference<XPropertySet> xIndex;
    1689             :     sal_uInt16 i;
    1690          20 :     OUString aColName;
    1691          10 :     const sal_Int32 nColumnCount = m_pColumns->getCount();
    1692          20 :     ::std::vector< Reference<XPropertySet> > aIndexedCols(nColumnCount);
    1693             : 
    1694          10 :     ::comphelper::UStringMixEqual aCase(isCaseSensitive());
    1695             : 
    1696          20 :     Reference<XIndexAccess> xColumns = m_pColumns;
    1697             :     // first search a key that exist already in the table
    1698         160 :     for (i = 0; i < nColumnCount; ++i)
    1699             :     {
    1700         150 :         sal_Int32 nPos = i;
    1701         150 :         if(_xCols != xColumns)
    1702             :         {
    1703           0 :             m_pColumns->getByIndex(i) >>= xCol;
    1704             :             OSL_ENSURE(xCol.is(),"ODbaseTable::UpdateBuffer column is null!");
    1705           0 :             xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME)) >>= aColName;
    1706             : 
    1707           0 :             for(nPos = 0;nPos<_xCols->getCount();++nPos)
    1708             :             {
    1709             :                 Reference<XPropertySet> xFindCol(
    1710           0 :                     _xCols->getByIndex(nPos), css::uno::UNO_QUERY);
    1711             :                 OSL_ENSURE(xFindCol.is(),"ODbaseTable::UpdateBuffer column is null!");
    1712           0 :                 if(aCase(getString(xFindCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME))),aColName))
    1713           0 :                     break;
    1714           0 :             }
    1715           0 :             if (nPos >= _xCols->getCount())
    1716           0 :                 continue;
    1717             :         }
    1718             : 
    1719         150 :         ++nPos;
    1720         150 :         xIndex = isUniqueByColumnName(i);
    1721         150 :         aIndexedCols[i] = xIndex;
    1722         150 :         if (xIndex.is())
    1723             :         {
    1724             :             // first check if the value is different to the old one and when if it conform to the index
    1725           0 :             if(pOrgRow.is() && (rRow.get()[nPos]->getValue().isNull() || rRow.get()[nPos] == (pOrgRow->get())[nPos]))
    1726           0 :                 continue;
    1727             :             else
    1728             :             {
    1729           0 :                 Reference<XUnoTunnel> xTunnel(xIndex,UNO_QUERY);
    1730             :                 OSL_ENSURE(xTunnel.is(),"No TunnelImplementation!");
    1731           0 :                 ODbaseIndex* pIndex = reinterpret_cast< ODbaseIndex* >( xTunnel->getSomething(ODbaseIndex::getUnoTunnelImplementationId()) );
    1732             :                 OSL_ENSURE(pIndex,"ODbaseTable::UpdateBuffer: No Index returned!");
    1733             : 
    1734           0 :                 if (pIndex->Find(0,*rRow.get()[nPos]))
    1735             :                 {
    1736             :                     // There is no unique value
    1737           0 :                     if ( aColName.isEmpty() )
    1738             :                     {
    1739           0 :                         m_pColumns->getByIndex(i) >>= xCol;
    1740             :                         OSL_ENSURE(xCol.is(),"ODbaseTable::UpdateBuffer column is null!");
    1741           0 :                         xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME)) >>= aColName;
    1742           0 :                         xCol.clear();
    1743             :                     } // if ( !aColName.getLength() )
    1744           0 :                     const OUString sError( getConnection()->getResources().getResourceStringWithSubstitution(
    1745             :                             STR_DUPLICATE_VALUE_IN_COLUMN
    1746             :                             ,"$columnname$", aColName
    1747           0 :                          ) );
    1748           0 :                     ::dbtools::throwGenericSQLException( sError, *this );
    1749           0 :                 }
    1750             :             }
    1751             :         }
    1752             :     }
    1753             : 
    1754             :     // when we are here there is no double key in the table
    1755             : 
    1756         160 :     for (i = 0; i < nColumnCount && nByteOffset <= m_nBufferSize ; ++i)
    1757             :     {
    1758             :         // Lengths for each data type:
    1759             :         OSL_ENSURE(i < m_aPrecisions.size(),"Illegal index!");
    1760         150 :         sal_Int32 nLen = 0;
    1761         150 :         sal_Int32 nType = 0;
    1762         150 :         sal_Int32 nScale = 0;
    1763         150 :         if ( i < m_aPrecisions.size() )
    1764             :         {
    1765         150 :             nLen    = m_aPrecisions[i];
    1766         150 :             nType   = m_aTypes[i];
    1767         150 :             nScale  = m_aScales[i];
    1768             :         }
    1769             :         else
    1770             :         {
    1771           0 :             m_pColumns->getByIndex(i) >>= xCol;
    1772           0 :             if ( xCol.is() )
    1773             :             {
    1774           0 :                 xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRECISION)) >>= nLen;
    1775           0 :                 xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE))      >>= nType;
    1776           0 :                 xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCALE))     >>= nScale;
    1777             :             }
    1778             :         }
    1779             : 
    1780         150 :         bool bSetZero = false;
    1781         150 :         switch (nType)
    1782             :         {
    1783             :             case DataType::INTEGER:
    1784             :             case DataType::DOUBLE:
    1785             :             case DataType::TIMESTAMP:
    1786           0 :                 bSetZero = true;
    1787             :                 //fall-through
    1788             :             case DataType::LONGVARBINARY:
    1789             :             case DataType::DATE:
    1790             :             case DataType::BIT:
    1791             :             case DataType::LONGVARCHAR:
    1792          74 :                 nLen = m_aRealFieldLengths[i];
    1793          74 :                 break;
    1794             :             case DataType::DECIMAL:
    1795          40 :                 nLen = SvDbaseConverter::ConvertPrecisionToDbase(nLen,nScale);
    1796          40 :                 break;  // The sign and the comma
    1797             :             default:
    1798          36 :                 break;
    1799             : 
    1800             :         } // switch (nType)
    1801             : 
    1802         150 :         sal_Int32 nPos = i;
    1803         150 :         if(_xCols != xColumns)
    1804             :         {
    1805           0 :             m_pColumns->getByIndex(i) >>= xCol;
    1806             :             OSL_ENSURE(xCol.is(),"ODbaseTable::UpdateBuffer column is null!");
    1807           0 :             xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME)) >>= aColName;
    1808           0 :             for(nPos = 0;nPos<_xCols->getCount();++nPos)
    1809             :             {
    1810             :                 Reference<XPropertySet> xFindCol(
    1811           0 :                     _xCols->getByIndex(nPos), css::uno::UNO_QUERY);
    1812           0 :                 if(aCase(getString(xFindCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME))),aColName))
    1813           0 :                     break;
    1814           0 :             }
    1815           0 :             if (nPos >= _xCols->getCount())
    1816             :             {
    1817           0 :                 nByteOffset += nLen;
    1818         140 :                 continue;
    1819             :             }
    1820             :         }
    1821             : 
    1822             : 
    1823             : 
    1824         150 :         ++nPos; // the row values start at 1
    1825         150 :         const ORowSetValue &thisColVal = rRow.get()[nPos]->get();
    1826         150 :         const bool thisColIsBound = thisColVal.isBound();
    1827         150 :         const bool thisColIsNull = !thisColIsBound || thisColVal.isNull();
    1828             :         // don't overwrite non-bound columns
    1829         150 :         if ( ! (bForceAllFields || thisColIsBound) )
    1830             :         {
    1831             :             // No - don't overwrite this field, it has not changed.
    1832          80 :             nByteOffset += nLen;
    1833          80 :             continue;
    1834             :         }
    1835          70 :         if (aIndexedCols[i].is())
    1836             :         {
    1837           0 :             Reference<XUnoTunnel> xTunnel(aIndexedCols[i],UNO_QUERY);
    1838             :             OSL_ENSURE(xTunnel.is(),"No TunnelImplementation!");
    1839           0 :             ODbaseIndex* pIndex = reinterpret_cast< ODbaseIndex* >( xTunnel->getSomething(ODbaseIndex::getUnoTunnelImplementationId()) );
    1840             :             OSL_ENSURE(pIndex,"ODbaseTable::UpdateBuffer: No Index returned!");
    1841             :             // Update !!
    1842           0 :             if (pOrgRow.is() && !thisColIsNull)
    1843           0 :                 pIndex->Update(m_nFilePos, *(pOrgRow->get())[nPos], thisColVal);
    1844             :             else
    1845           0 :                 pIndex->Insert(m_nFilePos, thisColVal);
    1846             :         }
    1847             : 
    1848          70 :         char* pData = (char *)(m_pBuffer + nByteOffset);
    1849          70 :         if (thisColIsNull)
    1850             :         {
    1851          60 :             if ( bSetZero )
    1852           0 :                 memset(pData,0,nLen);   // Clear to NULL char ('\0')
    1853             :             else
    1854          60 :                 memset(pData,' ',nLen); // Clear to space/blank ('\0x20')
    1855          60 :             nByteOffset += nLen;
    1856             :             OSL_ENSURE( nByteOffset <= m_nBufferSize ,"ByteOffset > m_nBufferSize!");
    1857          60 :             continue;
    1858             :         }
    1859             : 
    1860             :         try
    1861             :         {
    1862          10 :             switch (nType)
    1863             :             {
    1864             :                 case DataType::TIMESTAMP:
    1865             :                     {
    1866           0 :                         sal_Int32 nJulianDate = 0, nJulianTime = 0;
    1867           0 :                         lcl_CalcJulDate(nJulianDate,nJulianTime, thisColVal);
    1868             :                         // Exactly 8 bytes to copy:
    1869           0 :                         memcpy(pData,&nJulianDate,4);
    1870           0 :                         memcpy(pData+4,&nJulianTime,4);
    1871             :                     }
    1872           0 :                     break;
    1873             :                 case DataType::DATE:
    1874             :                 {
    1875           0 :                     ::com::sun::star::util::Date aDate;
    1876           0 :                     if(thisColVal.getTypeKind() == DataType::DOUBLE)
    1877           0 :                         aDate = ::dbtools::DBTypeConversion::toDate(thisColVal.getDouble());
    1878             :                     else
    1879           0 :                         aDate = thisColVal;
    1880             :                     char s[9];
    1881             :                     snprintf(s,
    1882             :                         sizeof(s),
    1883             :                         "%04d%02d%02d",
    1884             :                         (int)aDate.Year,
    1885             :                         (int)aDate.Month,
    1886           0 :                         (int)aDate.Day);
    1887             : 
    1888             :                     // Exactly 8 bytes to copy:
    1889           0 :                     strncpy(pData,s,sizeof s - 1);
    1890           0 :                 } break;
    1891             :                 case DataType::INTEGER:
    1892             :                     {
    1893           0 :                         sal_Int32 nValue = thisColVal;
    1894           0 :                         if (static_cast<size_t>(nLen) > sizeof(nValue))
    1895           0 :                             return false;
    1896           0 :                         memcpy(pData,&nValue,nLen);
    1897             :                     }
    1898           0 :                     break;
    1899             :                 case DataType::DOUBLE:
    1900             :                     {
    1901           0 :                         const double d = thisColVal;
    1902           0 :                         m_pColumns->getByIndex(i) >>= xCol;
    1903             : 
    1904           0 :                         if (getBOOL(xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISCURRENCY)))) // Currency is treated separately
    1905             :                         {
    1906           0 :                             sal_Int64 nValue = 0;
    1907           0 :                             if ( m_aScales[i] )
    1908           0 :                                 nValue = (sal_Int64)(d * pow(10.0,(int)m_aScales[i]));
    1909             :                             else
    1910           0 :                                 nValue = (sal_Int64)(d);
    1911           0 :                             if (static_cast<size_t>(nLen) > sizeof(nValue))
    1912           0 :                                 return false;
    1913           0 :                             memcpy(pData,&nValue,nLen);
    1914             :                         } // if (getBOOL(xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISCURRENCY)))) // Currency is treated separately
    1915             :                         else
    1916             :                         {
    1917           0 :                             if (static_cast<size_t>(nLen) > sizeof(d))
    1918           0 :                                 return false;
    1919           0 :                             memcpy(pData,&d,nLen);
    1920             :                         }
    1921             :                     }
    1922           0 :                     break;
    1923             :                 case DataType::DECIMAL:
    1924             :                 {
    1925           0 :                     memset(pData,' ',nLen); // Clear to NULL
    1926             : 
    1927           0 :                     const double n = thisColVal;
    1928             : 
    1929             :                     // one, because const_cast GetFormatPrecision on SvNumberFormat is not constant,
    1930             :                     // even though it really could and should be
    1931           0 :                     const OString aDefaultValue( ::rtl::math::doubleToString( n, rtl_math_StringFormat_F, nScale, '.', NULL, 0));
    1932           0 :                     const sal_Int32 nValueLen = aDefaultValue.getLength();
    1933           0 :                     if ( nValueLen <= nLen )
    1934             :                     {
    1935             :                         // Write value right-justified, padded with blanks to the left.
    1936           0 :                         memcpy(pData+nLen-nValueLen,aDefaultValue.getStr(),nValueLen);
    1937             :                         // write the resulting double back
    1938           0 :                         *rRow.get()[nPos] = toDouble(aDefaultValue);
    1939             :                     }
    1940             :                     else
    1941             :                     {
    1942           0 :                         m_pColumns->getByIndex(i) >>= xCol;
    1943             :                         OSL_ENSURE(xCol.is(),"ODbaseTable::UpdateBuffer column is null!");
    1944           0 :                         xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME)) >>= aColName;
    1945           0 :                         ::std::list< ::std::pair<const sal_Char* , OUString > > aStringToSubstitutes;
    1946           0 :                         aStringToSubstitutes.push_back(::std::pair<const sal_Char* , OUString >("$columnname$", aColName));
    1947           0 :                         aStringToSubstitutes.push_back(::std::pair<const sal_Char* , OUString >("$precision$", OUString::number(nLen)));
    1948           0 :                         aStringToSubstitutes.push_back(::std::pair<const sal_Char* , OUString >("$scale$", OUString::number(nScale)));
    1949           0 :                         aStringToSubstitutes.push_back(::std::pair<const sal_Char* , OUString >("$value$", OStringToOUString(aDefaultValue,RTL_TEXTENCODING_UTF8)));
    1950             : 
    1951           0 :                         const OUString sError( getConnection()->getResources().getResourceStringWithSubstitution(
    1952             :                                 STR_INVALID_COLUMN_DECIMAL_VALUE
    1953             :                                 ,aStringToSubstitutes
    1954           0 :                              ) );
    1955           0 :                         ::dbtools::throwGenericSQLException( sError, *this );
    1956           0 :                     }
    1957           0 :                 } break;
    1958             :                 case DataType::BIT:
    1959           0 :                     *pData = thisColVal.getBool() ? 'T' : 'F';
    1960           0 :                     break;
    1961             :                 case DataType::LONGVARBINARY:
    1962             :                 case DataType::LONGVARCHAR:
    1963             :                 {
    1964           2 :                     char cNext = pData[nLen]; // Mark's scratch and replaced by 0
    1965           2 :                     pData[nLen] = '\0';       // This is because the buffer is always a sign of greater ...
    1966             : 
    1967           2 :                     sal_Size nBlockNo = strtol((const char *)pData,NULL,10); // Block number read
    1968             : 
    1969             :                     // Next initial character restore again:
    1970           2 :                     pData[nLen] = cNext;
    1971           2 :                     if (!m_pMemoStream || !WriteMemo(thisColVal, nBlockNo))
    1972           0 :                         break;
    1973             : 
    1974           2 :                     OString aBlock(OString::number(nBlockNo));
    1975             :                     //align aBlock at the right of a nLen sequence, fill to the left with '0'
    1976           4 :                     OStringBuffer aStr;
    1977           2 :                     comphelper::string::padToLength(aStr, nLen - aBlock.getLength(), '0');
    1978           2 :                     aStr.append(aBlock);
    1979             : 
    1980             :                     // Copy characters:
    1981           4 :                     memcpy(pData, aStr.getStr(), nLen);
    1982           2 :                 }   break;
    1983             :                 default:
    1984             :                 {
    1985           8 :                     memset(pData,' ',nLen); // Clear to NULL
    1986             : 
    1987           8 :                     OUString sStringToWrite( thisColVal.getString() );
    1988             : 
    1989             :                     // convert the string, using the connection's encoding
    1990          16 :                     OString sEncoded;
    1991             : 
    1992           8 :                     DBTypeConversion::convertUnicodeStringToLength( sStringToWrite, sEncoded, nLen, m_eEncoding );
    1993          16 :                     memcpy( pData, sEncoded.getStr(), sEncoded.getLength() );
    1994             : 
    1995             :                 }
    1996           8 :                 break;
    1997             :             }
    1998             :         }
    1999           0 :         catch( const SQLException&  )
    2000             :         {
    2001           0 :             throw;
    2002             :         }
    2003           0 :         catch ( const Exception& )
    2004             :         {
    2005           0 :             m_pColumns->getByIndex(i) >>= xCol;
    2006             :             OSL_ENSURE( xCol.is(), "ODbaseTable::UpdateBuffer column is null!" );
    2007           0 :             if ( xCol.is() )
    2008           0 :                 xCol->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME)) >>= aColName;
    2009             : 
    2010           0 :             const OUString sError( getConnection()->getResources().getResourceStringWithSubstitution(
    2011             :                     STR_INVALID_COLUMN_VALUE,
    2012             :                     "$columnname$", aColName
    2013           0 :                  ) );
    2014           0 :             ::dbtools::throwGenericSQLException( sError, *this );
    2015             :         }
    2016             :         // And more ...
    2017          10 :         nByteOffset += nLen;
    2018             :         OSL_ENSURE( nByteOffset <= m_nBufferSize ,"ByteOffset > m_nBufferSize!");
    2019             :     }
    2020          20 :     return true;
    2021             : }
    2022             : 
    2023             : 
    2024           2 : bool ODbaseTable::WriteMemo(const ORowSetValue& aVariable, sal_Size& rBlockNr)
    2025             : {
    2026             :     // if the BlockNo 0 is given, the block will be appended at the end
    2027           2 :     sal_Size nSize = 0;
    2028           2 :     OString aStr;
    2029           4 :     ::com::sun::star::uno::Sequence<sal_Int8> aValue;
    2030             :     sal_uInt8 nHeader[4];
    2031           2 :     const bool bBinary = aVariable.getTypeKind() == DataType::LONGVARBINARY && m_aMemoHeader.db_typ == MemoFoxPro;
    2032           2 :     if ( bBinary )
    2033             :     {
    2034           0 :         aValue = aVariable.getSequence();
    2035           0 :         nSize = aValue.getLength();
    2036             :     }
    2037             :     else
    2038             :     {
    2039           2 :         nSize = DBTypeConversion::convertUnicodeString( aVariable.getString(), aStr, m_eEncoding );
    2040             :     }
    2041             : 
    2042             :     // append or overwrite
    2043           2 :     bool bAppend = rBlockNr == 0;
    2044             : 
    2045           2 :     if (!bAppend)
    2046             :     {
    2047           0 :         switch (m_aMemoHeader.db_typ)
    2048             :         {
    2049             :             case MemodBaseIII: // dBase III-Memofield, ends with 2 * Ctrl-Z
    2050           0 :                 bAppend = nSize > (512 - 2);
    2051           0 :                 break;
    2052             :             case MemoFoxPro:
    2053             :             case MemodBaseIV: // dBase IV-Memofield with length
    2054             :             {
    2055             :                 char sHeader[4];
    2056           0 :                 m_pMemoStream->Seek(rBlockNr * m_aMemoHeader.db_size);
    2057           0 :                 m_pMemoStream->SeekRel(4L);
    2058           0 :                 m_pMemoStream->Read(sHeader,4);
    2059             : 
    2060             :                 sal_Size nOldSize;
    2061           0 :                 if (m_aMemoHeader.db_typ == MemoFoxPro)
    2062           0 :                     nOldSize = ((((unsigned char)sHeader[0]) * 256 +
    2063           0 :                                  (unsigned char)sHeader[1]) * 256 +
    2064           0 :                                  (unsigned char)sHeader[2]) * 256 +
    2065           0 :                                  (unsigned char)sHeader[3];
    2066             :                 else
    2067           0 :                     nOldSize = ((((unsigned char)sHeader[3]) * 256 +
    2068           0 :                                  (unsigned char)sHeader[2]) * 256 +
    2069           0 :                                  (unsigned char)sHeader[1]) * 256 +
    2070           0 :                                  (unsigned char)sHeader[0]  - 8;
    2071             : 
    2072             :                 // fits the new length in the used blocks
    2073           0 :                 sal_Size nUsedBlocks = ((nSize + 8) / m_aMemoHeader.db_size) + (((nSize + 8) % m_aMemoHeader.db_size > 0) ? 1 : 0),
    2074           0 :                       nOldUsedBlocks = ((nOldSize + 8) / m_aMemoHeader.db_size) + (((nOldSize + 8) % m_aMemoHeader.db_size > 0) ? 1 : 0);
    2075           0 :                 bAppend = nUsedBlocks > nOldUsedBlocks;
    2076             :             }
    2077             :         }
    2078             :     }
    2079             : 
    2080           2 :     if (bAppend)
    2081             :     {
    2082           2 :         sal_Size nStreamSize = m_pMemoStream->Seek(STREAM_SEEK_TO_END);
    2083             :         // fill last block
    2084           2 :         rBlockNr = (nStreamSize / m_aMemoHeader.db_size) + ((nStreamSize % m_aMemoHeader.db_size) > 0 ? 1 : 0);
    2085             : 
    2086           2 :         m_pMemoStream->SetStreamSize(rBlockNr * m_aMemoHeader.db_size);
    2087           2 :         m_pMemoStream->Seek(STREAM_SEEK_TO_END);
    2088             :     }
    2089             :     else
    2090             :     {
    2091           0 :         m_pMemoStream->Seek(rBlockNr * m_aMemoHeader.db_size);
    2092             :     }
    2093             : 
    2094           2 :     switch (m_aMemoHeader.db_typ)
    2095             :     {
    2096             :         case MemodBaseIII: // dBase III-Memofield, ends with Ctrl-Z
    2097             :         {
    2098           2 :             const char cEOF = (char) DBF_EOL;
    2099           2 :             nSize++;
    2100           2 :             m_pMemoStream->Write( aStr.getStr(), aStr.getLength() );
    2101           2 :             (*m_pMemoStream).WriteChar( cEOF ).WriteChar( cEOF );
    2102           2 :         } break;
    2103             :         case MemoFoxPro:
    2104             :         case MemodBaseIV: // dBase IV-Memofeld with length
    2105             :         {
    2106           0 :             if ( MemodBaseIV == m_aMemoHeader.db_typ )
    2107           0 :                 (*m_pMemoStream).WriteUChar( 0xFF )
    2108           0 :                                 .WriteUChar( 0xFF )
    2109           0 :                                 .WriteUChar( 0x08 );
    2110             :             else
    2111           0 :                 (*m_pMemoStream).WriteUChar( 0x00 )
    2112           0 :                                 .WriteUChar( 0x00 )
    2113           0 :                                 .WriteUChar( 0x00 );
    2114             : 
    2115           0 :             sal_uInt32 nWriteSize = nSize;
    2116           0 :             if (m_aMemoHeader.db_typ == MemoFoxPro)
    2117             :             {
    2118           0 :                 if ( bBinary )
    2119           0 :                     (*m_pMemoStream).WriteUChar( 0x00 ); // Picture
    2120             :                 else
    2121           0 :                     (*m_pMemoStream).WriteUChar( 0x01 ); // Memo
    2122           0 :                 for (int i = 4; i > 0; nWriteSize >>= 8)
    2123           0 :                     nHeader[--i] = (sal_uInt8) (nWriteSize % 256);
    2124             :             }
    2125             :             else
    2126             :             {
    2127           0 :                 (*m_pMemoStream).WriteUChar( 0x00 );
    2128           0 :                 nWriteSize += 8;
    2129           0 :                 for (int i = 0; i < 4; nWriteSize >>= 8)
    2130           0 :                     nHeader[i++] = (sal_uInt8) (nWriteSize % 256);
    2131             :             }
    2132             : 
    2133           0 :             m_pMemoStream->Write(nHeader,4);
    2134           0 :             if ( bBinary )
    2135           0 :                 m_pMemoStream->Write( aValue.getConstArray(), aValue.getLength() );
    2136             :             else
    2137           0 :                 m_pMemoStream->Write( aStr.getStr(), aStr.getLength() );
    2138           0 :             m_pMemoStream->Flush();
    2139             :         }
    2140             :     }
    2141             : 
    2142             : 
    2143             :     // Write the new block number
    2144           2 :     if (bAppend)
    2145             :     {
    2146           2 :         sal_Size nStreamSize = m_pMemoStream->Seek(STREAM_SEEK_TO_END);
    2147           2 :         m_aMemoHeader.db_next = (nStreamSize / m_aMemoHeader.db_size) + ((nStreamSize % m_aMemoHeader.db_size) > 0 ? 1 : 0);
    2148             : 
    2149             :         // Write the new block number
    2150           2 :         m_pMemoStream->Seek(0L);
    2151           2 :         (*m_pMemoStream).WriteUInt32( m_aMemoHeader.db_next );
    2152           2 :         m_pMemoStream->Flush();
    2153             :     }
    2154           4 :     return true;
    2155             : }
    2156             : 
    2157             : 
    2158             : // XAlterTable
    2159           0 : void SAL_CALL ODbaseTable::alterColumnByName( const OUString& colName, const Reference< XPropertySet >& descriptor ) throw(SQLException, NoSuchElementException, RuntimeException, std::exception)
    2160             : {
    2161           0 :     ::osl::MutexGuard aGuard(m_aMutex);
    2162           0 :     checkDisposed(OTableDescriptor_BASE::rBHelper.bDisposed);
    2163             : 
    2164             : 
    2165           0 :     Reference<XDataDescriptorFactory> xOldColumn;
    2166           0 :     m_pColumns->getByName(colName) >>= xOldColumn;
    2167             : 
    2168             :     try
    2169             :     {
    2170           0 :         alterColumn(m_pColumns->findColumn(colName)-1,descriptor,xOldColumn);
    2171             :     }
    2172           0 :     catch (const css::lang::IndexOutOfBoundsException&)
    2173             :     {
    2174           0 :         throw NoSuchElementException(colName, *this);
    2175           0 :     }
    2176           0 : }
    2177             : 
    2178           0 : void SAL_CALL ODbaseTable::alterColumnByIndex( sal_Int32 index, const Reference< XPropertySet >& descriptor ) throw(SQLException, ::com::sun::star::lang::IndexOutOfBoundsException, RuntimeException, std::exception)
    2179             : {
    2180           0 :     ::osl::MutexGuard aGuard(m_aMutex);
    2181           0 :     checkDisposed(OTableDescriptor_BASE::rBHelper.bDisposed);
    2182             : 
    2183           0 :     if(index < 0 || index >= m_pColumns->getCount())
    2184           0 :         throw IndexOutOfBoundsException(OUString::number(index),*this);
    2185             : 
    2186           0 :     Reference<XDataDescriptorFactory> xOldColumn;
    2187           0 :     m_pColumns->getByIndex(index) >>= xOldColumn;
    2188           0 :     alterColumn(index,descriptor,xOldColumn);
    2189           0 : }
    2190             : 
    2191           0 : void ODbaseTable::alterColumn(sal_Int32 index,
    2192             :                               const Reference< XPropertySet >& descriptor ,
    2193             :                               const Reference< XDataDescriptorFactory >& xOldColumn )
    2194             : {
    2195           0 :     if(index < 0 || index >= m_pColumns->getCount())
    2196           0 :         throw IndexOutOfBoundsException(OUString::number(index),*this);
    2197             : 
    2198           0 :     ODbaseTable* pNewTable = NULL;
    2199             :     try
    2200             :     {
    2201             :         OSL_ENSURE(descriptor.is(),"ODbaseTable::alterColumn: descriptor can not be null!");
    2202             :         // creates a copy of the original column and copy all properties from descriptor in xCopyColumn
    2203           0 :         Reference<XPropertySet> xCopyColumn;
    2204           0 :         if(xOldColumn.is())
    2205           0 :             xCopyColumn = xOldColumn->createDataDescriptor();
    2206             :         else
    2207           0 :             xCopyColumn = new OColumn(getConnection()->getMetaData()->supportsMixedCaseQuotedIdentifiers());
    2208             : 
    2209           0 :         ::comphelper::copyProperties(descriptor,xCopyColumn);
    2210             : 
    2211             :         // creates a temp file
    2212             : 
    2213           0 :         OUString sTempName = createTempFile();
    2214             : 
    2215           0 :         pNewTable = new ODbaseTable(m_pTables,static_cast<ODbaseConnection*>(m_pConnection));
    2216           0 :         Reference<XPropertySet> xHoldTable = pNewTable;
    2217           0 :         pNewTable->setPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME),makeAny(sTempName));
    2218           0 :         Reference<XAppend> xAppend(pNewTable->getColumns(),UNO_QUERY);
    2219             :         OSL_ENSURE(xAppend.is(),"ODbaseTable::alterColumn: No XAppend interface!");
    2220             : 
    2221             :         // copy the structure
    2222           0 :         sal_Int32 i=0;
    2223           0 :         for(;i < index;++i)
    2224             :         {
    2225           0 :             Reference<XPropertySet> xProp;
    2226           0 :             m_pColumns->getByIndex(i) >>= xProp;
    2227           0 :             Reference<XDataDescriptorFactory> xColumn(xProp,UNO_QUERY);
    2228           0 :             Reference<XPropertySet> xCpy;
    2229           0 :             if(xColumn.is())
    2230           0 :                 xCpy = xColumn->createDataDescriptor();
    2231             :             else
    2232           0 :                 xCpy = new OColumn(getConnection()->getMetaData()->supportsMixedCaseQuotedIdentifiers());
    2233           0 :             ::comphelper::copyProperties(xProp,xCpy);
    2234           0 :             xAppend->appendByDescriptor(xCpy);
    2235           0 :         }
    2236           0 :         ++i; // now insert our new column
    2237           0 :         xAppend->appendByDescriptor(xCopyColumn);
    2238             : 
    2239           0 :         for(;i < m_pColumns->getCount();++i)
    2240             :         {
    2241           0 :             Reference<XPropertySet> xProp;
    2242           0 :             m_pColumns->getByIndex(i) >>= xProp;
    2243           0 :             Reference<XDataDescriptorFactory> xColumn(xProp,UNO_QUERY);
    2244           0 :             Reference<XPropertySet> xCpy;
    2245           0 :             if(xColumn.is())
    2246           0 :                 xCpy = xColumn->createDataDescriptor();
    2247             :             else
    2248           0 :                 xCpy = new OColumn(getConnection()->getMetaData()->supportsMixedCaseQuotedIdentifiers());
    2249           0 :             ::comphelper::copyProperties(xProp,xCpy);
    2250           0 :             xAppend->appendByDescriptor(xCpy);
    2251           0 :         }
    2252             : 
    2253             :         // construct the new table
    2254           0 :         if(!pNewTable->CreateImpl())
    2255             :         {
    2256           0 :             const OUString sError( getConnection()->getResources().getResourceStringWithSubstitution(
    2257             :                     STR_COLUMN_NOT_ALTERABLE,
    2258           0 :                     "$columnname$", ::comphelper::getString(descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME)))
    2259           0 :                  ) );
    2260           0 :             ::dbtools::throwGenericSQLException( sError, *this );
    2261             :         }
    2262             : 
    2263           0 :         pNewTable->construct();
    2264             : 
    2265             :         // copy the data
    2266           0 :         copyData(pNewTable,0);
    2267             : 
    2268             :         // now drop the old one
    2269           0 :         if( DropImpl() ) // we don't want to delete the memo columns too
    2270             :         {
    2271             :             try
    2272             :             {
    2273             :                 // rename the new one to the old one
    2274           0 :                 pNewTable->renameImpl(m_Name);
    2275             :             }
    2276           0 :             catch(const css::container::ElementExistException&)
    2277             :             {
    2278           0 :                 const OUString sError( getConnection()->getResources().getResourceStringWithSubstitution(
    2279             :                         STR_COULD_NOT_DELETE_FILE,
    2280             :                         "$filename$", m_Name
    2281           0 :                      ) );
    2282           0 :                 ::dbtools::throwGenericSQLException( sError, *this );
    2283             :             }
    2284             :             // release the temp file
    2285           0 :             pNewTable = NULL;
    2286           0 :             ::comphelper::disposeComponent(xHoldTable);
    2287             :         }
    2288             :         else
    2289             :         {
    2290           0 :             pNewTable = NULL;
    2291             :         }
    2292           0 :         FileClose();
    2293           0 :         construct();
    2294           0 :         if(m_pColumns)
    2295           0 :             m_pColumns->refresh();
    2296             : 
    2297             :     }
    2298           0 :     catch(const SQLException&)
    2299             :     {
    2300           0 :         throw;
    2301             :     }
    2302           0 :     catch(const Exception&)
    2303             :     {
    2304             :         SAL_WARN( "connectivity.drivers","ODbaseTable::alterColumn: Exception occurred!");
    2305           0 :         throw;
    2306             :     }
    2307           0 : }
    2308             : 
    2309           0 : Reference< XDatabaseMetaData> ODbaseTable::getMetaData() const
    2310             : {
    2311           0 :     return getConnection()->getMetaData();
    2312             : }
    2313             : 
    2314           0 : void SAL_CALL ODbaseTable::rename( const OUString& newName ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::container::ElementExistException, ::com::sun::star::uno::RuntimeException, std::exception)
    2315             : {
    2316           0 :     ::osl::MutexGuard aGuard(m_aMutex);
    2317           0 :     checkDisposed(OTableDescriptor_BASE::rBHelper.bDisposed);
    2318           0 :     if(m_pTables && m_pTables->hasByName(newName))
    2319           0 :         throw ElementExistException(newName,*this);
    2320             : 
    2321             : 
    2322           0 :     renameImpl(newName);
    2323             : 
    2324           0 :     ODbaseTable_BASE::rename(newName);
    2325             : 
    2326           0 :     construct();
    2327           0 :     if(m_pColumns)
    2328           0 :         m_pColumns->refresh();
    2329           0 : }
    2330             : namespace
    2331             : {
    2332           0 :     void renameFile(OConnection* _pConenction,const OUString& oldName,
    2333             :                     const OUString& newName,const OUString& _sExtension)
    2334             :     {
    2335           0 :         OUString aName = ODbaseTable::getEntry(_pConenction,oldName);
    2336           0 :         if(aName.isEmpty())
    2337             :         {
    2338           0 :             OUString aIdent = _pConenction->getContent()->getIdentifier()->getContentIdentifier();
    2339           0 :             if ( aIdent.lastIndexOf('/') != (aIdent.getLength()-1) )
    2340           0 :                 aIdent += "/";
    2341           0 :             aIdent += oldName;
    2342           0 :             aName = aIdent;
    2343             :         }
    2344           0 :         INetURLObject aURL;
    2345           0 :         aURL.SetURL(aName);
    2346             : 
    2347           0 :         aURL.setExtension( _sExtension );
    2348           0 :         OUString sNewName(newName + "." + _sExtension);
    2349             : 
    2350             :         try
    2351             :         {
    2352           0 :             Content aContent(aURL.GetMainURL(INetURLObject::NO_DECODE),Reference<XCommandEnvironment>(), comphelper::getProcessComponentContext());
    2353             : 
    2354           0 :             Sequence< PropertyValue > aProps( 1 );
    2355           0 :             aProps[0].Name      = "Title";
    2356           0 :             aProps[0].Handle    = -1; // n/a
    2357           0 :             aProps[0].Value     = makeAny( sNewName );
    2358           0 :             Sequence< Any > aValues;
    2359           0 :             aContent.executeCommand( "setPropertyValues",makeAny(aProps) ) >>= aValues;
    2360           0 :             if(aValues.getLength() && aValues[0].hasValue())
    2361           0 :                 throw Exception();
    2362             :         }
    2363           0 :         catch(const Exception&)
    2364             :         {
    2365           0 :             throw ElementExistException(newName);
    2366           0 :         }
    2367           0 :     }
    2368             : }
    2369             : 
    2370           0 : void SAL_CALL ODbaseTable::renameImpl( const OUString& newName ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::container::ElementExistException, ::com::sun::star::uno::RuntimeException)
    2371             : {
    2372           0 :     ::osl::MutexGuard aGuard(m_aMutex);
    2373             : 
    2374           0 :     FileClose();
    2375             : 
    2376             : 
    2377           0 :     renameFile(m_pConnection,m_Name,newName,m_pConnection->getExtension());
    2378           0 :     if ( HasMemoFields() )
    2379             :     {  // delete the memo fields
    2380           0 :         OUString sExt("dbt");
    2381           0 :         renameFile(m_pConnection,m_Name,newName,sExt);
    2382           0 :     }
    2383           0 : }
    2384             : 
    2385           0 : void ODbaseTable::addColumn(const Reference< XPropertySet >& _xNewColumn)
    2386             : {
    2387           0 :     OUString sTempName = createTempFile();
    2388             : 
    2389           0 :     ODbaseTable* pNewTable = new ODbaseTable(m_pTables,static_cast<ODbaseConnection*>(m_pConnection));
    2390           0 :     Reference< XPropertySet > xHold = pNewTable;
    2391           0 :     pNewTable->setPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME),makeAny(sTempName));
    2392             :     {
    2393           0 :         Reference<XAppend> xAppend(pNewTable->getColumns(),UNO_QUERY);
    2394           0 :         bool bCase = getConnection()->getMetaData()->supportsMixedCaseQuotedIdentifiers();
    2395             :         // copy the structure
    2396           0 :         for(sal_Int32 i=0;i < m_pColumns->getCount();++i)
    2397             :         {
    2398           0 :             Reference<XPropertySet> xProp;
    2399           0 :             m_pColumns->getByIndex(i) >>= xProp;
    2400           0 :             Reference<XDataDescriptorFactory> xColumn(xProp,UNO_QUERY);
    2401           0 :             Reference<XPropertySet> xCpy;
    2402           0 :             if(xColumn.is())
    2403           0 :                 xCpy = xColumn->createDataDescriptor();
    2404             :             else
    2405             :             {
    2406           0 :                 xCpy = new OColumn(bCase);
    2407           0 :                 ::comphelper::copyProperties(xProp,xCpy);
    2408             :             }
    2409             : 
    2410           0 :             xAppend->appendByDescriptor(xCpy);
    2411           0 :         }
    2412           0 :         Reference<XPropertySet> xCpy = new OColumn(bCase);
    2413           0 :         ::comphelper::copyProperties(_xNewColumn,xCpy);
    2414           0 :         xAppend->appendByDescriptor(xCpy);
    2415             :     }
    2416             : 
    2417             :     // construct the new table
    2418           0 :     if(!pNewTable->CreateImpl())
    2419             :     {
    2420           0 :         const OUString sError( getConnection()->getResources().getResourceStringWithSubstitution(
    2421             :                 STR_COLUMN_NOT_ADDABLE,
    2422           0 :                 "$columnname$", ::comphelper::getString(_xNewColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME)))
    2423           0 :              ) );
    2424           0 :         ::dbtools::throwGenericSQLException( sError, *this );
    2425             :     }
    2426             : 
    2427           0 :     bool bAlreadyDroped = false;
    2428             :     try
    2429             :     {
    2430           0 :         pNewTable->construct();
    2431             :         // copy the data
    2432           0 :         copyData(pNewTable,pNewTable->m_pColumns->getCount());
    2433             :         // drop the old table
    2434           0 :         if(DropImpl())
    2435             :         {
    2436           0 :             bAlreadyDroped = true;
    2437           0 :             pNewTable->renameImpl(m_Name);
    2438             :             // release the temp file
    2439             :         }
    2440           0 :         xHold = pNewTable = NULL;
    2441             : 
    2442           0 :         FileClose();
    2443           0 :         construct();
    2444           0 :         if(m_pColumns)
    2445           0 :             m_pColumns->refresh();
    2446             :     }
    2447           0 :     catch(const SQLException&)
    2448             :     {
    2449             :         // here we know that the old table wasn't dropped before
    2450           0 :         if(!bAlreadyDroped)
    2451           0 :             xHold = pNewTable = NULL;
    2452             : 
    2453           0 :         throw;
    2454           0 :     }
    2455           0 : }
    2456             : 
    2457           0 : void ODbaseTable::dropColumn(sal_Int32 _nPos)
    2458             : {
    2459           0 :     OUString sTempName = createTempFile();
    2460             : 
    2461           0 :     ODbaseTable* pNewTable = new ODbaseTable(m_pTables,static_cast<ODbaseConnection*>(m_pConnection));
    2462           0 :     Reference< XPropertySet > xHold = pNewTable;
    2463           0 :     pNewTable->setPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME),makeAny(sTempName));
    2464             :     {
    2465           0 :         Reference<XAppend> xAppend(pNewTable->getColumns(),UNO_QUERY);
    2466           0 :         bool bCase = getConnection()->getMetaData()->supportsMixedCaseQuotedIdentifiers();
    2467             :         // copy the structure
    2468           0 :         for(sal_Int32 i=0;i < m_pColumns->getCount();++i)
    2469             :         {
    2470           0 :             if(_nPos != i)
    2471             :             {
    2472           0 :                 Reference<XPropertySet> xProp;
    2473           0 :                 m_pColumns->getByIndex(i) >>= xProp;
    2474           0 :                 Reference<XDataDescriptorFactory> xColumn(xProp,UNO_QUERY);
    2475           0 :                 Reference<XPropertySet> xCpy;
    2476           0 :                 if(xColumn.is())
    2477           0 :                     xCpy = xColumn->createDataDescriptor();
    2478             :                 else
    2479             :                 {
    2480           0 :                     xCpy = new OColumn(bCase);
    2481           0 :                     ::comphelper::copyProperties(xProp,xCpy);
    2482             :                 }
    2483           0 :                 xAppend->appendByDescriptor(xCpy);
    2484             :             }
    2485           0 :         }
    2486             :     }
    2487             : 
    2488             :     // construct the new table
    2489           0 :     if(!pNewTable->CreateImpl())
    2490             :     {
    2491           0 :         xHold = pNewTable = NULL;
    2492           0 :         const OUString sError( getConnection()->getResources().getResourceStringWithSubstitution(
    2493             :                 STR_COLUMN_NOT_DROP,
    2494             :                 "$position$", OUString::number(_nPos)
    2495           0 :              ) );
    2496           0 :         ::dbtools::throwGenericSQLException( sError, *this );
    2497             :     }
    2498           0 :     pNewTable->construct();
    2499             :     // copy the data
    2500           0 :     copyData(pNewTable,_nPos);
    2501             :     // drop the old table
    2502           0 :     if(DropImpl())
    2503           0 :         pNewTable->renameImpl(m_Name);
    2504             :         // release the temp file
    2505             : 
    2506           0 :     xHold = pNewTable = NULL;
    2507             : 
    2508           0 :     FileClose();
    2509           0 :     construct();
    2510           0 : }
    2511             : 
    2512           0 : OUString ODbaseTable::createTempFile()
    2513             : {
    2514           0 :     OUString aIdent = m_pConnection->getContent()->getIdentifier()->getContentIdentifier();
    2515           0 :     if ( aIdent.lastIndexOf('/') != (aIdent.getLength()-1) )
    2516           0 :         aIdent += "/";
    2517             : 
    2518           0 :     OUString sTempName(aIdent);
    2519           0 :     OUString sExt(OUString(".") + m_pConnection->getExtension());
    2520           0 :     OUString sName(m_Name);
    2521           0 :     TempFile aTempFile(sName, true, &sExt, &sTempName);
    2522           0 :     if(!aTempFile.IsValid())
    2523           0 :         getConnection()->throwGenericSQLException(STR_COULD_NOT_ALTER_TABLE, *this);
    2524             : 
    2525           0 :     INetURLObject aURL;
    2526           0 :     aURL.SetSmartProtocol(INET_PROT_FILE);
    2527           0 :     aURL.SetURL(aTempFile.GetURL());
    2528             : 
    2529           0 :     OUString sNewName(aURL.getName().copy(0, aURL.getName().getLength() - sExt.getLength()));
    2530             : 
    2531           0 :     return sNewName;
    2532             : }
    2533             : 
    2534           0 : void ODbaseTable::copyData(ODbaseTable* _pNewTable,sal_Int32 _nPos)
    2535             : {
    2536           0 :     sal_Int32 nPos = _nPos + 1; // +1 because we always have the bookmark clumn as well
    2537           0 :     OValueRefRow aRow = new OValueRefVector(m_pColumns->getCount());
    2538           0 :     OValueRefRow aInsertRow;
    2539           0 :     if(_nPos)
    2540             :     {
    2541           0 :         aInsertRow = new OValueRefVector(_pNewTable->m_pColumns->getCount());
    2542           0 :         ::std::for_each(aInsertRow->get().begin(),aInsertRow->get().end(),TSetRefBound(true));
    2543             :     }
    2544             :     else
    2545           0 :         aInsertRow = aRow;
    2546             : 
    2547             :     // we only have to bind the values which we need to copy into the new table
    2548           0 :     ::std::for_each(aRow->get().begin(),aRow->get().end(),TSetRefBound(true));
    2549           0 :     if(_nPos && (_nPos < (sal_Int32)aRow->get().size()))
    2550           0 :         (aRow->get())[nPos]->setBound(false);
    2551             : 
    2552             : 
    2553           0 :     bool bOk = true;
    2554             :     sal_Int32 nCurPos;
    2555           0 :     OValueRefVector::Vector::iterator aIter;
    2556           0 :     for(sal_uInt32 nRowPos = 0; nRowPos < m_aHeader.db_anz;++nRowPos)
    2557             :     {
    2558           0 :         bOk = seekRow( IResultSetHelper::BOOKMARK, nRowPos+1, nCurPos );
    2559           0 :         if ( bOk )
    2560             :         {
    2561           0 :             bOk = fetchRow( aRow, *m_aColumns, true, true);
    2562           0 :             if ( bOk && !aRow->isDeleted() ) // copy only not deleted rows
    2563             :             {
    2564             :                 // special handling when pos == 0 then we don't have to distinguish between the two rows
    2565           0 :                 if(_nPos)
    2566             :                 {
    2567           0 :                     aIter = aRow->get().begin()+1;
    2568           0 :                     sal_Int32 nCount = 1;
    2569           0 :                     for(OValueRefVector::Vector::iterator aInsertIter = aInsertRow->get().begin()+1; aIter != aRow->get().end() && aInsertIter != aInsertRow->get().end();++aIter,++nCount)
    2570             :                     {
    2571           0 :                         if(nPos != nCount)
    2572             :                         {
    2573           0 :                             (*aInsertIter)->setValue( (*aIter)->getValue() );
    2574           0 :                             ++aInsertIter;
    2575             :                         }
    2576             :                     }
    2577             :                 }
    2578           0 :                 bOk = _pNewTable->InsertRow(*aInsertRow,true,_pNewTable->m_pColumns);
    2579             :                 SAL_WARN_IF(!bOk, "connectivity.drivers", "Row could not be inserted!");
    2580             :             }
    2581             :             else
    2582             :             {
    2583             :                 SAL_WARN_IF(!bOk, "connectivity.drivers", "Row could not be fetched!");
    2584             :             }
    2585             :         }
    2586             :         else
    2587             :         {
    2588             :             OSL_ASSERT(false);
    2589             :         }
    2590           0 :     } // for(sal_uInt32 nRowPos = 0; nRowPos < m_aHeader.db_anz;++nRowPos)
    2591           0 : }
    2592             : 
    2593           0 : void ODbaseTable::throwInvalidDbaseFormat()
    2594             : {
    2595           0 :     FileClose();
    2596             :     // no dbase file
    2597             : 
    2598           0 :     const OUString sError( getConnection()->getResources().getResourceStringWithSubstitution(
    2599             :                 STR_INVALID_DBASE_FILE,
    2600             :                 "$filename$", getEntry(m_pConnection,m_Name)
    2601           0 :              ) );
    2602           0 :     ::dbtools::throwGenericSQLException( sError, *this );
    2603           0 : }
    2604             : 
    2605          42 : void ODbaseTable::refreshHeader()
    2606             : {
    2607          42 :     if ( m_aHeader.db_anz == 0 )
    2608           0 :         readHeader();
    2609          42 : }
    2610             : 
    2611        4516 : bool ODbaseTable::seekRow(IResultSetHelper::Movement eCursorPosition, sal_Int32 nOffset, sal_Int32& nCurPos)
    2612             : {
    2613             :     // prepare positioning:
    2614             :     OSL_ENSURE(m_pFileStream,"ODbaseTable::seekRow: FileStream is NULL!");
    2615             : 
    2616        4516 :     sal_uInt32  nNumberOfRecords = (sal_uInt32)m_aHeader.db_anz;
    2617        4516 :     sal_uInt32 nTempPos = m_nFilePos;
    2618        4516 :     m_nFilePos = nCurPos;
    2619             : 
    2620        4516 :     switch(eCursorPosition)
    2621             :     {
    2622             :         case IResultSetHelper::NEXT:
    2623         260 :             ++m_nFilePos;
    2624         260 :             break;
    2625             :         case IResultSetHelper::PRIOR:
    2626           0 :             if (m_nFilePos > 0)
    2627           0 :                 --m_nFilePos;
    2628           0 :             break;
    2629             :         case IResultSetHelper::FIRST:
    2630           0 :             m_nFilePos = 1;
    2631           0 :             break;
    2632             :         case IResultSetHelper::LAST:
    2633           0 :             m_nFilePos = nNumberOfRecords;
    2634           0 :             break;
    2635             :         case IResultSetHelper::RELATIVE1:
    2636             :             m_nFilePos = (((sal_Int32)m_nFilePos) + nOffset < 0) ? 0L
    2637           0 :                             : (sal_uInt32)(((sal_Int32)m_nFilePos) + nOffset);
    2638           0 :             break;
    2639             :         case IResultSetHelper::ABSOLUTE1:
    2640             :         case IResultSetHelper::BOOKMARK:
    2641        4256 :             m_nFilePos = (sal_uInt32)nOffset;
    2642        4256 :             break;
    2643             :     }
    2644             : 
    2645        4516 :     if (m_nFilePos > (sal_Int32)nNumberOfRecords)
    2646           0 :         m_nFilePos = (sal_Int32)nNumberOfRecords + 1;
    2647             : 
    2648        4516 :     if (m_nFilePos == 0 || m_nFilePos == (sal_Int32)nNumberOfRecords + 1)
    2649             :         goto Error;
    2650             :     else
    2651             :     {
    2652        4504 :         sal_Size nEntryLen = m_aHeader.db_slng;
    2653             : 
    2654             :         OSL_ENSURE(m_nFilePos >= 1,"SdbDBFCursor::FileFetchRow: ungueltige Record-Position");
    2655        4504 :         sal_Size nPos = m_aHeader.db_kopf + (sal_Size)(m_nFilePos-1) * nEntryLen;
    2656             : 
    2657        4504 :         m_pFileStream->Seek(nPos);
    2658        4504 :         if (m_pFileStream->GetError() != ERRCODE_NONE)
    2659           0 :             goto Error;
    2660             : 
    2661        4504 :         sal_Size nRead = m_pFileStream->Read((char*)m_pBuffer, nEntryLen);
    2662        4504 :         if (nRead != nEntryLen)
    2663             :         {
    2664             :             SAL_WARN("connectivity.drivers", "ODbaseTable::seekRow: short read!");
    2665           0 :             goto Error;
    2666             :         }
    2667        4504 :         if (m_pFileStream->GetError() != ERRCODE_NONE)
    2668           0 :             goto Error;
    2669             :     }
    2670        4504 :     goto End;
    2671             : 
    2672             : Error:
    2673          12 :     switch(eCursorPosition)
    2674             :     {
    2675             :         case IResultSetHelper::PRIOR:
    2676             :         case IResultSetHelper::FIRST:
    2677           0 :             m_nFilePos = 0;
    2678           0 :             break;
    2679             :         case IResultSetHelper::LAST:
    2680             :         case IResultSetHelper::NEXT:
    2681             :         case IResultSetHelper::ABSOLUTE1:
    2682             :         case IResultSetHelper::RELATIVE1:
    2683          12 :             if (nOffset > 0)
    2684           0 :                 m_nFilePos = nNumberOfRecords + 1;
    2685          12 :             else if (nOffset < 0)
    2686           0 :                 m_nFilePos = 0;
    2687          12 :             break;
    2688             :         case IResultSetHelper::BOOKMARK:
    2689           0 :             m_nFilePos = nTempPos;   // last position
    2690             :     }
    2691          12 :     return false;
    2692             : 
    2693             : End:
    2694        4504 :     nCurPos = m_nFilePos;
    2695        4504 :     return true;
    2696             : }
    2697             : 
    2698       31664 : bool ODbaseTable::ReadMemo(sal_Size nBlockNo, ORowSetValue& aVariable)
    2699             : {
    2700       31664 :     bool bIsText = true;
    2701             : 
    2702       31664 :     m_pMemoStream->Seek(nBlockNo * m_aMemoHeader.db_size);
    2703       31664 :     switch (m_aMemoHeader.db_typ)
    2704             :     {
    2705             :         case MemodBaseIII: // dBase III-Memofield, ends with Ctrl-Z
    2706             :         {
    2707       31664 :             const char cEOF = (char) DBF_EOL;
    2708       31664 :             OStringBuffer aBStr;
    2709             :             static char aBuf[514];
    2710       31664 :             aBuf[512] = 0;          // avoid random value
    2711       31664 :             bool bReady = false;
    2712             : 
    2713       31664 :             do
    2714             :             {
    2715       31664 :                 m_pMemoStream->Read(&aBuf,512);
    2716             : 
    2717       31664 :                 sal_uInt16 i = 0;
    2718       31664 :                 while (aBuf[i] != cEOF && ++i < 512)
    2719             :                     ;
    2720       31664 :                 bReady = aBuf[i] == cEOF;
    2721             : 
    2722       31664 :                 aBuf[i] = 0;
    2723       31664 :                 aBStr.append(aBuf);
    2724             : 
    2725       31664 :             } while (!bReady && !m_pMemoStream->IsEof());
    2726             : 
    2727       63328 :             aVariable = OStringToOUString(aBStr.makeStringAndClear(),
    2728       63328 :                 m_eEncoding);
    2729             : 
    2730       31664 :         } break;
    2731             :         case MemoFoxPro:
    2732             :         case MemodBaseIV: // dBase IV-Memofield with length
    2733             :         {
    2734             :             char sHeader[4];
    2735           0 :             m_pMemoStream->Read(sHeader,4);
    2736             :             // Foxpro stores text and binary data
    2737           0 :             if (m_aMemoHeader.db_typ == MemoFoxPro)
    2738             :             {
    2739           0 :                 bIsText = sHeader[3] != 0;
    2740             :             }
    2741           0 :             else if (((sal_uInt8)sHeader[0]) != 0xFF || ((sal_uInt8)sHeader[1]) != 0xFF || ((sal_uInt8)sHeader[2]) != 0x08)
    2742             :             {
    2743           0 :                 return false;
    2744             :             }
    2745             : 
    2746           0 :             sal_uInt32 nLength(0);
    2747           0 :             (*m_pMemoStream).ReadUInt32( nLength );
    2748             : 
    2749           0 :             if (m_aMemoHeader.db_typ == MemodBaseIV)
    2750           0 :                 nLength -= 8;
    2751             : 
    2752           0 :             if ( nLength )
    2753             :             {
    2754           0 :                 if ( bIsText )
    2755             :                 {
    2756           0 :                     OStringBuffer aBuffer(read_uInt8s_ToOString(*m_pMemoStream, nLength));
    2757             :                     //pad it out with ' ' to expected length on short read
    2758           0 :                     sal_Int32 nRequested = sal::static_int_cast<sal_Int32>(nLength);
    2759           0 :                     comphelper::string::padToLength(aBuffer, nRequested, ' ');
    2760           0 :                     aVariable = OStringToOUString(aBuffer.makeStringAndClear(), m_eEncoding);
    2761             :                 } // if ( bIsText )
    2762             :                 else
    2763             :                 {
    2764           0 :                     ::com::sun::star::uno::Sequence< sal_Int8 > aData(nLength);
    2765           0 :                     m_pMemoStream->Read(aData.getArray(),nLength);
    2766           0 :                     aVariable = aData;
    2767             :                 }
    2768             :             } // if ( nLength )
    2769             :         }
    2770             :     }
    2771       31664 :     return true;
    2772             : }
    2773             : 
    2774          40 : bool ODbaseTable::AllocBuffer()
    2775             : {
    2776          40 :     sal_uInt16 nSize = m_aHeader.db_slng;
    2777             :     SAL_WARN_IF(nSize == 0, "connectivity.drivers", "Size too small");
    2778             : 
    2779          40 :     if (m_nBufferSize != nSize)
    2780             :     {
    2781          30 :         delete m_pBuffer;
    2782          30 :         m_pBuffer = NULL;
    2783             :     }
    2784             : 
    2785             :     // if there is no buffer available: allocate:
    2786          40 :     if (m_pBuffer == NULL && nSize > 0)
    2787             :     {
    2788          30 :         m_nBufferSize = nSize;
    2789          30 :         m_pBuffer = new sal_uInt8[m_nBufferSize+1];
    2790             :     }
    2791             : 
    2792          40 :     return m_pBuffer != NULL;
    2793             : }
    2794             : 
    2795          10 : bool ODbaseTable::WriteBuffer()
    2796             : {
    2797             :     OSL_ENSURE(m_nFilePos >= 1,"SdbDBFCursor::FileFetchRow: ungueltige Record-Position");
    2798             : 
    2799             :     // position on desired record:
    2800          10 :     sal_Size nPos = m_aHeader.db_kopf + (long)(m_nFilePos-1) * m_aHeader.db_slng;
    2801          10 :     m_pFileStream->Seek(nPos);
    2802          10 :     return m_pFileStream->Write((char*) m_pBuffer, m_aHeader.db_slng) > 0;
    2803             : }
    2804             : 
    2805          42 : sal_Int32 ODbaseTable::getCurrentLastPos() const
    2806             : {
    2807          42 :     return m_aHeader.db_anz;
    2808             : }
    2809             : 
    2810             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10