LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/sc/source/ui/docshell - docsh8.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 1 565 0.2 %
Date: 2013-07-09 Functions: 2 14 14.3 %
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 <stdio.h>
      21             : #include <tools/urlobj.hxx>
      22             : #include <svl/converter.hxx>
      23             : #include <comphelper/processfactory.hxx>
      24             : #include <comphelper/string.hxx>
      25             : #include <comphelper/types.hxx>
      26             : #include <ucbhelper/content.hxx>
      27             : #include <svx/txenctab.hxx>
      28             : 
      29             : #ifndef DISABLE_DBCONNECTIVITY
      30             : #include <svx/dbcharsethelper.hxx>
      31             : #endif
      32             : 
      33             : #include <com/sun/star/sdb/CommandType.hpp>
      34             : #include <com/sun/star/sdbc/DataType.hpp>
      35             : #include <com/sun/star/sdbc/XConnection.hpp>
      36             : #include <com/sun/star/sdbc/XDriver.hpp>
      37             : #include <com/sun/star/sdbc/XDriverAccess.hpp>
      38             : #include <com/sun/star/sdbc/DriverManager.hpp>
      39             : #include <com/sun/star/sdbc/XResultSetUpdate.hpp>
      40             : #include <com/sun/star/sdbc/XRow.hpp>
      41             : #include <com/sun/star/sdbc/XRowSet.hpp>
      42             : #include <com/sun/star/sdbc/XRowUpdate.hpp>
      43             : #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
      44             : #include <com/sun/star/sdbcx/XAppend.hpp>
      45             : #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
      46             : #include <com/sun/star/sdbcx/XDataDefinitionSupplier.hpp>
      47             : #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
      48             : #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
      49             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      50             : #include <com/sun/star/beans/XPropertySet.hpp>
      51             : #include <com/sun/star/container/XEnumerationAccess.hpp>
      52             : #include <com/sun/star/lang/XComponent.hpp>
      53             : #include <com/sun/star/ucb/NameClash.hpp>
      54             : #include <com/sun/star/ucb/TransferInfo.hpp>
      55             : #include <com/sun/star/ucb/XCommandInfo.hpp>
      56             : 
      57             : #include "scerrors.hxx"
      58             : #include "docsh.hxx"
      59             : #include "filter.hxx"
      60             : #include "progress.hxx"
      61             : #include "formulacell.hxx"
      62             : #include "editutil.hxx"
      63             : #include "cellform.hxx"
      64             : #include "dbdocutl.hxx"
      65             : #include "dociter.hxx"
      66             : #include "globstr.hrc"
      67             : #include "svl/zformat.hxx"
      68             : #include "svl/intitem.hxx"
      69             : #include "patattr.hxx"
      70             : #include "scitems.hxx"
      71             : #include "docpool.hxx"
      72             : #include "segmenttree.hxx"
      73             : #include "docparam.hxx"
      74             : #include "cellvalue.hxx"
      75             : 
      76             : #include <vector>
      77             : #include <boost/unordered_set.hpp>
      78             : 
      79             : using namespace com::sun::star;
      80             : using ::std::vector;
      81             : 
      82             : // -----------------------------------------------------------------------
      83             : 
      84             : #define SC_SERVICE_ROWSET           "com.sun.star.sdb.RowSet"
      85             : 
      86             : //! move to a header file?
      87             : #define SC_DBPROP_ACTIVECONNECTION  "ActiveConnection"
      88             : #define SC_DBPROP_COMMAND           "Command"
      89             : #define SC_DBPROP_COMMANDTYPE       "CommandType"
      90             : #define SC_DBPROP_PROPCHANGE_NOTIFY "PropertyChangeNotificationEnabled"
      91             : 
      92             : #define SC_DBPROP_NAME              "Name"
      93             : #define SC_DBPROP_TYPE              "Type"
      94             : #define SC_DBPROP_PRECISION         "Precision"
      95             : #define SC_DBPROP_SCALE             "Scale"
      96             : 
      97             : #define SC_DBPROP_EXTENSION         "Extension"
      98             : #define SC_DBPROP_CHARSET           "CharSet"
      99             : 
     100             : #ifndef DISABLE_DBCONNECTIVITY
     101             : 
     102             : namespace
     103             : {
     104           0 :     sal_uLong lcl_getDBaseConnection(uno::Reference<sdbc::XDriverManager2>& _rDrvMgr,uno::Reference<sdbc::XConnection>& _rConnection,String& _rTabName,const String& rFullFileName,rtl_TextEncoding eCharSet)
     105             :     {
     106           0 :         INetURLObject aURL;
     107           0 :         aURL.SetSmartProtocol( INET_PROT_FILE );
     108           0 :         aURL.SetSmartURL( rFullFileName );
     109           0 :         _rTabName = aURL.getBase( INetURLObject::LAST_SEGMENT, true,
     110           0 :                 INetURLObject::DECODE_UNAMBIGUOUS );
     111           0 :         String aExtension = aURL.getExtension();
     112           0 :         aURL.removeSegment();
     113           0 :         aURL.removeFinalSlash();
     114           0 :         String aPath = aURL.GetMainURL(INetURLObject::NO_DECODE);
     115           0 :         uno::Reference<uno::XComponentContext> xContext = comphelper::getProcessComponentContext();
     116             : 
     117           0 :         _rDrvMgr.set( sdbc::DriverManager::create( xContext ) );
     118             : 
     119             :         // get connection
     120             : 
     121           0 :         String aConnUrl = OUString("sdbc:dbase:");
     122           0 :         aConnUrl += aPath;
     123             : 
     124           0 :         svxform::ODataAccessCharsetHelper aHelper;
     125           0 :         ::std::vector< rtl_TextEncoding > aEncodings;
     126           0 :         aHelper.getSupportedTextEncodings( aEncodings );
     127           0 :         ::std::vector< rtl_TextEncoding >::iterator aIter = ::std::find(aEncodings.begin(),aEncodings.end(),(rtl_TextEncoding) eCharSet);
     128           0 :         if ( aIter == aEncodings.end() )
     129             :         {
     130             :             OSL_FAIL( "DBaseImport: dbtools::OCharsetMap doesn't know text encoding" );
     131           0 :             return SCERR_IMPORT_CONNECT;
     132             :         } // if ( aIter == aMap.end() )
     133           0 :         OUString aCharSetStr;
     134           0 :         if ( RTL_TEXTENCODING_DONTKNOW != *aIter )
     135             :         {   // it's not the virtual "system charset"
     136           0 :             const char* pIanaName = rtl_getMimeCharsetFromTextEncoding( *aIter );
     137             :             OSL_ENSURE( pIanaName, "invalid mime name!" );
     138           0 :             if ( pIanaName )
     139           0 :                 aCharSetStr = OUString::createFromAscii( pIanaName );
     140             :         }
     141             : 
     142           0 :         uno::Sequence<beans::PropertyValue> aProps(2);
     143           0 :         aProps[0].Name = OUString(SC_DBPROP_EXTENSION);
     144           0 :         aProps[0].Value <<= OUString( aExtension );
     145           0 :         aProps[1].Name = OUString(SC_DBPROP_CHARSET);
     146           0 :         aProps[1].Value <<= aCharSetStr;
     147             : 
     148           0 :         _rConnection = _rDrvMgr->getConnectionWithInfo( aConnUrl, aProps );
     149           0 :         return 0L;
     150             :     }
     151             : }
     152             : 
     153             : #endif // !DISABLE_DBCONNECTIVITY
     154             : 
     155             : // -----------------------------------------------------------------------
     156             : // MoveFile/KillFile/IsDocument: similar to SfxContentHelper
     157             : 
     158           0 : bool ScDocShell::MoveFile( const INetURLObject& rSourceObj, const INetURLObject& rDestObj )
     159             : {
     160           0 :     bool bMoveData = true;
     161           0 :     bool bRet = true, bKillSource = false;
     162           0 :     if ( rSourceObj.GetProtocol() != rDestObj.GetProtocol() )
     163             :     {
     164           0 :         bMoveData = false;
     165           0 :         bKillSource = true;
     166             :     }
     167           0 :     String aName = rDestObj.getName();
     168           0 :     INetURLObject aDestPathObj = rDestObj;
     169           0 :     aDestPathObj.removeSegment();
     170           0 :     aDestPathObj.setFinalSlash();
     171             : 
     172             :     try
     173             :     {
     174             :         ::ucbhelper::Content aDestPath( aDestPathObj.GetMainURL(INetURLObject::NO_DECODE),
     175             :                             uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(),
     176           0 :                             comphelper::getProcessComponentContext() );
     177           0 :         uno::Reference< ::com::sun::star::ucb::XCommandInfo > xInfo = aDestPath.getCommands();
     178           0 :         OUString aTransferName = OUString( "transfer" );
     179           0 :         if ( xInfo->hasCommandByName( aTransferName ) )
     180             :         {
     181             :             aDestPath.executeCommand( aTransferName, uno::makeAny(
     182             :                 ::com::sun::star::ucb::TransferInfo( bMoveData, rSourceObj.GetMainURL(INetURLObject::NO_DECODE), aName,
     183           0 :                                                        ::com::sun::star::ucb::NameClash::ERROR ) ) );
     184             :         }
     185             :         else
     186             :         {
     187             :             OSL_FAIL( "transfer command not available" );
     188           0 :         }
     189             :     }
     190           0 :     catch( uno::Exception& )
     191             :     {
     192             :         // ucb may throw different exceptions on failure now
     193           0 :         bRet = false;
     194             :     }
     195             : 
     196           0 :     if ( bKillSource )
     197           0 :         KillFile( rSourceObj );
     198             : 
     199           0 :     return bRet;
     200             : }
     201             : 
     202             : 
     203           0 : bool ScDocShell::KillFile( const INetURLObject& rURL )
     204             : {
     205           0 :     bool bRet = true;
     206             :     try
     207             :     {
     208             :         ::ucbhelper::Content aCnt( rURL.GetMainURL(INetURLObject::NO_DECODE),
     209             :                         uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(),
     210           0 :                         comphelper::getProcessComponentContext() );
     211             :         aCnt.executeCommand( OUString( "delete" ),
     212           0 :                                 comphelper::makeBoolAny( sal_True ) );
     213             :     }
     214           0 :     catch( uno::Exception& )
     215             :     {
     216             :         // ucb may throw different exceptions on failure now
     217           0 :         bRet = false;
     218             :     }
     219             : 
     220           0 :     return bRet;
     221             : }
     222             : 
     223           0 : bool ScDocShell::IsDocument( const INetURLObject& rURL )
     224             : {
     225           0 :     bool bRet = false;
     226             :     try
     227             :     {
     228             :         ::ucbhelper::Content aCnt( rURL.GetMainURL(INetURLObject::NO_DECODE),
     229             :                         uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(),
     230           0 :                         comphelper::getProcessComponentContext() );
     231           0 :         bRet = aCnt.isDocument();
     232             :     }
     233           0 :     catch( uno::Exception& )
     234             :     {
     235             :         // ucb may throw different exceptions on failure now - warning only
     236             :         OSL_FAIL( "Any other exception" );
     237             :     }
     238             : 
     239           0 :     return bRet;
     240             : }
     241             : 
     242             : // -----------------------------------------------------------------------
     243             : 
     244             : #ifndef DISABLE_DBCONNECTIVITY
     245             : 
     246           0 : static void lcl_setScalesToColumns(ScDocument& rDoc, const vector<long>& rScales)
     247             : {
     248           0 :     SvNumberFormatter* pFormatter = rDoc.GetFormatTable();
     249           0 :     if (!pFormatter)
     250           0 :         return;
     251             : 
     252           0 :     SCCOL nColCount = static_cast<SCCOL>(rScales.size());
     253           0 :     for (SCCOL i = 0; i < nColCount; ++i)
     254             :     {
     255           0 :         if (rScales[i] < 0)
     256           0 :             continue;
     257             : 
     258             :         sal_uInt32 nOldFormat;
     259           0 :         rDoc.GetNumberFormat(static_cast<SCCOL>(i), 0, 0, nOldFormat);
     260           0 :         const SvNumberformat* pOldEntry = pFormatter->GetEntry(nOldFormat);
     261           0 :         if (!pOldEntry)
     262           0 :             continue;
     263             : 
     264           0 :         LanguageType eLang = pOldEntry->GetLanguage();
     265             :         bool bThousand, bNegRed;
     266             :         sal_uInt16 nPrecision, nLeading;
     267           0 :         pOldEntry->GetFormatSpecialInfo(bThousand, bNegRed, nPrecision, nLeading);
     268             : 
     269           0 :         nPrecision = static_cast<sal_uInt16>(rScales[i]);
     270             :         OUString aNewPicture = pFormatter->GenerateFormat(nOldFormat, eLang,
     271           0 :                                                           bThousand, bNegRed, nPrecision, nLeading);
     272             : 
     273           0 :         sal_uInt32 nNewFormat = pFormatter->GetEntryKey(aNewPicture, eLang);
     274           0 :         if (nNewFormat == NUMBERFORMAT_ENTRY_NOT_FOUND)
     275             :         {
     276           0 :             sal_Int32 nErrPos = 0;
     277           0 :             short nNewType = 0;
     278             :             bool bOk = pFormatter->PutEntry(
     279           0 :                 aNewPicture, nErrPos, nNewType, nNewFormat, eLang);
     280             : 
     281           0 :             if (!bOk)
     282           0 :                 continue;
     283             :         }
     284             : 
     285           0 :         ScPatternAttr aNewAttrs( rDoc.GetPool() );
     286           0 :         SfxItemSet& rSet = aNewAttrs.GetItemSet();
     287           0 :         rSet.Put( SfxUInt32Item(ATTR_VALUE_FORMAT, nNewFormat) );
     288           0 :         rDoc.ApplyPatternAreaTab(static_cast<SCCOL>(i), 0, static_cast<SCCOL>(i), MAXROW, 0, aNewAttrs);
     289           0 :     }
     290             : }
     291             : 
     292             : #endif // !DISABLE_DBCONNECTIVITY
     293             : 
     294           0 : sal_uLong ScDocShell::DBaseImport( const String& rFullFileName, CharSet eCharSet,
     295             :                                ScColWidthParam aColWidthParam[MAXCOLCOUNT], ScFlatBoolRowSegments& rRowHeightsRecalc )
     296             : {
     297             : #ifdef DISABLE_DBCONNECTIVITY
     298             :     (void) rFullFileName;
     299             :     (void) eCharSet;
     300             :     (void) aColWidthParam;
     301             :     (void) rRowHeightsRecalc;
     302             : 
     303             :     return ERRCODE_IO_GENERAL;
     304             : #else
     305             : 
     306           0 :     sal_uLong nErr = eERR_OK;
     307             :     long i;
     308           0 :     long nColCount = 0;
     309             : 
     310             :     // Try to get the Text Encoding from the driver
     311           0 :     if( eCharSet == RTL_TEXTENCODING_IBM_850 )
     312           0 :         eCharSet = RTL_TEXTENCODING_DONTKNOW;
     313             : 
     314             :     try
     315             :     {
     316           0 :         String aTabName;
     317           0 :         uno::Reference<sdbc::XDriverManager2> xDrvMan;
     318           0 :         uno::Reference<sdbc::XConnection> xConnection;
     319           0 :         sal_uLong nRet = lcl_getDBaseConnection(xDrvMan,xConnection,aTabName,rFullFileName,eCharSet);
     320           0 :         if ( !xConnection.is() || !xDrvMan.is() )
     321           0 :             return nRet;
     322           0 :         ::utl::DisposableComponent aConnectionHelper(xConnection);
     323             : 
     324           0 :         ScProgress aProgress( this, ScGlobal::GetRscString( STR_LOAD_DOC ), 0 );
     325           0 :         uno::Reference<lang::XMultiServiceFactory> xFactory = comphelper::getProcessServiceFactory();
     326           0 :         uno::Reference<sdbc::XRowSet> xRowSet( xFactory->createInstance(
     327           0 :                             OUString( SC_SERVICE_ROWSET ) ),
     328           0 :                             uno::UNO_QUERY);
     329           0 :         ::utl::DisposableComponent aRowSetHelper(xRowSet);
     330           0 :         uno::Reference<beans::XPropertySet> xRowProp( xRowSet, uno::UNO_QUERY );
     331             :         OSL_ENSURE( xRowProp.is(), "can't get RowSet" );
     332           0 :         if (!xRowProp.is()) return SCERR_IMPORT_CONNECT;
     333             : 
     334           0 :         sal_Int32 nType = sdb::CommandType::TABLE;
     335           0 :         uno::Any aAny;
     336             : 
     337           0 :         aAny <<= xConnection;
     338           0 :         xRowProp->setPropertyValue( OUString(SC_DBPROP_ACTIVECONNECTION), aAny );
     339             : 
     340           0 :         aAny <<= nType;
     341           0 :         xRowProp->setPropertyValue( OUString(SC_DBPROP_COMMANDTYPE), aAny );
     342             : 
     343           0 :         aAny <<= OUString( aTabName );
     344           0 :         xRowProp->setPropertyValue( OUString(SC_DBPROP_COMMAND), aAny );
     345             : 
     346           0 :         aAny <<= false;
     347           0 :         xRowProp->setPropertyValue( OUString(SC_DBPROP_PROPCHANGE_NOTIFY), aAny );
     348             : 
     349           0 :         xRowSet->execute();
     350             : 
     351           0 :         uno::Reference<sdbc::XResultSetMetaData> xMeta;
     352           0 :         uno::Reference<sdbc::XResultSetMetaDataSupplier> xMetaSupp( xRowSet, uno::UNO_QUERY );
     353           0 :         if ( xMetaSupp.is() )
     354           0 :             xMeta = xMetaSupp->getMetaData();
     355           0 :         if ( xMeta.is() )
     356           0 :             nColCount = xMeta->getColumnCount();    // this is the number of real columns
     357             : 
     358           0 :         if ( nColCount > MAXCOL+1 )
     359             :         {
     360           0 :             nColCount = MAXCOL+1;
     361           0 :             nErr = SCWARN_IMPORT_COLUMN_OVERFLOW;    // warning
     362             :         }
     363             : 
     364           0 :         uno::Reference<sdbc::XRow> xRow( xRowSet, uno::UNO_QUERY );
     365             :         OSL_ENSURE( xRow.is(), "can't get Row" );
     366           0 :         if (!xRow.is()) return SCERR_IMPORT_CONNECT;
     367             : 
     368             :         // currency flag is not needed for dBase
     369           0 :         uno::Sequence<sal_Int32> aColTypes( nColCount );    // column types
     370           0 :         sal_Int32* pTypeArr = aColTypes.getArray();
     371           0 :         for (i=0; i<nColCount; i++)
     372           0 :             pTypeArr[i] = xMeta->getColumnType( i+1 );
     373             : 
     374             :         //  read column names
     375             :         //! add type descriptions
     376             : 
     377           0 :         aProgress.SetState( 0 );
     378             : 
     379           0 :         vector<long> aScales(nColCount, -1);
     380           0 :         for (i=0; i<nColCount; i++)
     381             :         {
     382           0 :             OUString aHeader = xMeta->getColumnLabel( i+1 );
     383             : 
     384           0 :             switch ( pTypeArr[i] )
     385             :             {
     386             :                 case sdbc::DataType::BIT:
     387           0 :                     aHeader += ",L";
     388           0 :                     break;
     389             :                 case sdbc::DataType::DATE:
     390           0 :                     aHeader += ",D";
     391           0 :                     break;
     392             :                 case sdbc::DataType::LONGVARCHAR:
     393           0 :                     aHeader += ",M";
     394           0 :                     break;
     395             :                 case sdbc::DataType::VARCHAR:
     396           0 :                     aHeader += ",C," + OUString::number( xMeta->getColumnDisplaySize( i+1 ) );
     397           0 :                     break;
     398             :                 case sdbc::DataType::DECIMAL:
     399             :                     {
     400           0 :                         long nPrec = xMeta->getPrecision( i+1 );
     401           0 :                         long nScale = xMeta->getScale( i+1 );
     402           0 :                         aHeader += ",N," +
     403             :                                     OUString::number(
     404             :                                         SvDbaseConverter::ConvertPrecisionToDbase(
     405           0 :                                             nPrec, nScale ) ) +
     406           0 :                                     "," +
     407           0 :                                     OUString::number( nScale );
     408           0 :                         aScales[i] = nScale;
     409             :                     }
     410           0 :                     break;
     411             :             }
     412             : 
     413           0 :             aDocument.SetString( static_cast<SCCOL>(i), 0, 0, aHeader );
     414           0 :         }
     415             : 
     416           0 :         lcl_setScalesToColumns(aDocument, aScales);
     417             : 
     418           0 :         SCROW nRow = 1;     // 0 is column titles
     419           0 :         sal_Bool bEnd = false;
     420           0 :         while ( !bEnd && xRowSet->next() )
     421             :         {
     422           0 :             if ( nRow <= MAXROW )
     423             :             {
     424           0 :                 bool bSimpleRow = true;
     425           0 :                 SCCOL nCol = 0;
     426           0 :                 for (i=0; i<nColCount; i++)
     427             :                 {
     428           0 :                     ScDatabaseDocUtil::StrData aStrData;
     429             :                     ScDatabaseDocUtil::PutData( &aDocument, nCol, nRow, 0,
     430           0 :                                                 xRow, i+1, pTypeArr[i], false,
     431           0 :                                                 &aStrData );
     432             : 
     433           0 :                     if (aStrData.mnStrLength > aColWidthParam[nCol].mnMaxTextLen)
     434             :                     {
     435           0 :                         aColWidthParam[nCol].mnMaxTextLen = aStrData.mnStrLength;
     436           0 :                         aColWidthParam[nCol].mnMaxTextRow = nRow;
     437             :                     }
     438             : 
     439           0 :                     if (!aStrData.mbSimpleText)
     440             :                     {
     441           0 :                         bSimpleRow = false;
     442           0 :                         aColWidthParam[nCol].mbSimpleText = false;
     443             :                     }
     444             : 
     445           0 :                     ++nCol;
     446             :                 }
     447           0 :                 if (!bSimpleRow)
     448           0 :                     rRowHeightsRecalc.setTrue(nRow, nRow);
     449           0 :                 ++nRow;
     450             :             }
     451             :             else        // past the end of the spreadsheet
     452             :             {
     453           0 :                 bEnd = sal_True;                            // don't continue
     454           0 :                 nErr = SCWARN_IMPORT_RANGE_OVERFLOW;    // warning message
     455             :             }
     456           0 :         }
     457             :     }
     458           0 :     catch ( sdbc::SQLException& )
     459             :     {
     460           0 :         nErr = SCERR_IMPORT_CONNECT;
     461             :     }
     462           0 :     catch ( uno::Exception& )
     463             :     {
     464             :         OSL_FAIL("Unexpected exception in database");
     465           0 :         nErr = ERRCODE_IO_GENERAL;
     466             :     }
     467             : 
     468           0 :     return nErr;
     469             : #endif // !DISABLE_DBCONNECTIVITY
     470             : }
     471             : 
     472             : #ifndef DISABLE_DBCONNECTIVITY
     473             : 
     474             : namespace {
     475             : 
     476           0 : inline bool IsAsciiDigit( sal_Unicode c )
     477             : {
     478           0 :     return 0x30 <= c && c <= 0x39;
     479             : }
     480             : 
     481           0 : inline bool IsAsciiAlpha( sal_Unicode c )
     482             : {
     483           0 :     return (0x41 <= c && c <= 0x5a) || (0x61 <= c && c <= 0x7a);
     484             : }
     485             : 
     486           0 : void lcl_GetColumnTypes(
     487             :     ScDocShell& rDocShell, const ScRange& rDataRange, bool bHasFieldNames,
     488             :     OUString* pColNames, sal_Int32* pColTypes, sal_Int32* pColLengths,
     489             :     sal_Int32* pColScales, bool& bHasMemo, CharSet eCharSet )
     490             : {
     491             :     //  updating of column titles didn't work in 5.2 and isn't always wanted
     492             :     //  (saving normally shouldn't modify the document)
     493             :     //! read flag from configuration
     494           0 :     bool bUpdateTitles = false;
     495             : 
     496           0 :     ScDocument* pDoc = rDocShell.GetDocument();
     497           0 :     SvNumberFormatter* pNumFmt = pDoc->GetFormatTable();
     498             : 
     499           0 :     SCTAB nTab = rDataRange.aStart.Tab();
     500           0 :     SCCOL nFirstCol = rDataRange.aStart.Col();
     501           0 :     SCROW nFirstRow = rDataRange.aStart.Row();
     502           0 :     SCCOL nLastCol = rDataRange.aEnd.Col();
     503           0 :     SCROW nLastRow = rDataRange.aEnd.Row();
     504             : 
     505             :     typedef boost::unordered_set<OUString, OUStringHash> StrSetType;
     506           0 :     StrSetType aFieldNames;
     507             : 
     508           0 :     long nField = 0;
     509           0 :     SCROW nFirstDataRow = ( bHasFieldNames ? nFirstRow + 1 : nFirstRow );
     510           0 :     for ( SCCOL nCol = nFirstCol; nCol <= nLastCol; nCol++ )
     511             :     {
     512           0 :         bool bTypeDefined = false;
     513           0 :         bool bPrecDefined = false;
     514           0 :         sal_Int32 nFieldLen = 0;
     515           0 :         sal_Int32 nPrecision = 0;
     516           0 :         sal_Int32 nDbType = sdbc::DataType::SQLNULL;
     517           0 :         String aFieldName, aString;
     518             : 
     519             :         // Feldname[,Type[,Width[,Prec]]]
     520             :         // Typ etc.: L; D; C[,W]; N[,W[,P]]
     521           0 :         if ( bHasFieldNames )
     522             :         {
     523           0 :             aString = pDoc->GetString(nCol, nFirstRow, nTab);
     524           0 :             aString.ToUpperAscii();
     525           0 :             xub_StrLen nToken = comphelper::string::getTokenCount(aString, ',');
     526           0 :             if ( nToken > 1 )
     527             :             {
     528           0 :                 aFieldName = aString.GetToken( 0, ',' );
     529           0 :                 aString = comphelper::string::remove(aString, ' ');
     530           0 :                 switch ( aString.GetToken( 1, ',' ).GetChar(0) )
     531             :                 {
     532             :                     case 'L' :
     533           0 :                         nDbType = sdbc::DataType::BIT;
     534           0 :                         nFieldLen = 1;
     535           0 :                         bTypeDefined = sal_True;
     536           0 :                         bPrecDefined = sal_True;
     537           0 :                         break;
     538             :                     case 'D' :
     539           0 :                         nDbType = sdbc::DataType::DATE;
     540           0 :                         nFieldLen = 8;
     541           0 :                         bTypeDefined = sal_True;
     542           0 :                         bPrecDefined = sal_True;
     543           0 :                         break;
     544             :                     case 'M' :
     545           0 :                         nDbType = sdbc::DataType::LONGVARCHAR;
     546           0 :                         nFieldLen = 10;
     547           0 :                         bTypeDefined = sal_True;
     548           0 :                         bPrecDefined = sal_True;
     549           0 :                         bHasMemo = sal_True;
     550           0 :                         break;
     551             :                     case 'C' :
     552           0 :                         nDbType = sdbc::DataType::VARCHAR;
     553           0 :                         bTypeDefined = sal_True;
     554           0 :                         bPrecDefined = sal_True;
     555           0 :                         break;
     556             :                     case 'N' :
     557           0 :                         nDbType = sdbc::DataType::DECIMAL;
     558           0 :                         break;
     559             :                 }
     560           0 :                 if ( bTypeDefined && !nFieldLen && nToken > 2 )
     561             :                 {
     562           0 :                     nFieldLen = aString.GetToken( 2, ',' ).ToInt32();
     563           0 :                     if ( !bPrecDefined && nToken > 3 )
     564             :                     {
     565           0 :                         String aTmp( aString.GetToken( 3, ',' ) );
     566           0 :                         if ( CharClass::isAsciiNumeric(aTmp) )
     567             :                         {
     568           0 :                             nPrecision = aTmp.ToInt32();
     569           0 :                             bPrecDefined = sal_True;
     570           0 :                         }
     571             :                     }
     572             :                 }
     573             :             }
     574             :             else
     575           0 :                 aFieldName = aString;
     576             : 
     577             :             // Feldnamen pruefen und ggbf. gueltigen Feldnamen erzeugen.
     578             :             // Erstes Zeichen muss Buchstabe sein,
     579             :             // weitere nur alphanumerisch und Unterstrich erlaubt,
     580             :             // "_DBASELOCK" ist reserviert (obsolet weil erstes Zeichen kein Buchstabe),
     581             :             // keine doppelten Namen.
     582           0 :             if ( !IsAsciiAlpha( aFieldName.GetChar(0) ) )
     583           0 :                 aFieldName.Insert( 'N', 0 );
     584           0 :             String aTmpStr;
     585             :             sal_Unicode c;
     586           0 :             for ( const sal_Unicode* p = aFieldName.GetBuffer(); ( c = *p ) != 0; p++ )
     587             :             {
     588           0 :                 if ( IsAsciiAlpha( c ) || IsAsciiDigit( c ) || c == '_' )
     589           0 :                     aTmpStr += c;
     590             :                 else
     591           0 :                     aTmpStr += '_';
     592             :             }
     593           0 :             aFieldName = aTmpStr;
     594           0 :             if ( aFieldName.Len() > 10 )
     595           0 :                 aFieldName.Erase( 10 );
     596             : 
     597           0 :             if (!aFieldNames.insert(aFieldName).second)
     598             :             {   // doppelter Feldname, numerisch erweitern
     599           0 :                 sal_uInt16 nSub = 1;
     600           0 :                 String aFixPart( aFieldName );
     601           0 :                 do
     602             :                 {
     603           0 :                     ++nSub;
     604           0 :                     String aVarPart = OUString::number( nSub );
     605           0 :                     if ( aFixPart.Len() + aVarPart.Len() > 10 )
     606           0 :                         aFixPart.Erase( 10 - aVarPart.Len() );
     607           0 :                     aFieldName = aFixPart;
     608           0 :                     aFieldName += aVarPart;
     609           0 :                 } while (!aFieldNames.insert(aFieldName).second);
     610           0 :             }
     611             :         }
     612             :         else
     613             :         {
     614           0 :             aFieldName = 'N';
     615           0 :             aFieldName += OUString::number(nCol+1);
     616             :         }
     617             : 
     618           0 :         if ( !bTypeDefined )
     619             :         {   // Feldtyp
     620           0 :             ScRefCellValue aCell;
     621           0 :             aCell.assign(*pDoc, ScAddress(nCol, nFirstDataRow, nTab));
     622           0 :             if (aCell.isEmpty() || aCell.hasString())
     623           0 :                 nDbType = sdbc::DataType::VARCHAR;
     624             :             else
     625             :             {
     626             :                 sal_uInt32 nFormat;
     627           0 :                 pDoc->GetNumberFormat( nCol, nFirstDataRow, nTab, nFormat );
     628           0 :                 switch ( pNumFmt->GetType( nFormat ) )
     629             :                 {
     630             :                     case NUMBERFORMAT_LOGICAL :
     631           0 :                         nDbType = sdbc::DataType::BIT;
     632           0 :                         nFieldLen = 1;
     633           0 :                         break;
     634             :                     case NUMBERFORMAT_DATE :
     635           0 :                         nDbType = sdbc::DataType::DATE;
     636           0 :                         nFieldLen = 8;
     637           0 :                         break;
     638             :                     case NUMBERFORMAT_TIME :
     639             :                     case NUMBERFORMAT_DATETIME :
     640           0 :                         nDbType = sdbc::DataType::VARCHAR;
     641           0 :                         break;
     642             :                     default:
     643           0 :                         nDbType = sdbc::DataType::DECIMAL;
     644             :                 }
     645           0 :             }
     646             :         }
     647           0 :         bool bSdbLenAdjusted = false;
     648           0 :         bool bSdbLenBad = false;
     649             :         // Feldlaenge
     650           0 :         if ( nDbType == sdbc::DataType::VARCHAR && !nFieldLen )
     651             :         {   // maximale Feldbreite bestimmen
     652             :             nFieldLen = pDoc->GetMaxStringLen( nTab, nCol, nFirstDataRow,
     653           0 :                 nLastRow, eCharSet );
     654           0 :             if ( nFieldLen == 0 )
     655           0 :                 nFieldLen = 1;
     656             :         }
     657           0 :         else if ( nDbType == sdbc::DataType::DECIMAL )
     658             :         {   // maximale Feldbreite und Nachkommastellen bestimmen
     659             :             xub_StrLen nLen;
     660             :             sal_uInt16 nPrec;
     661             :             nLen = pDoc->GetMaxNumberStringLen( nPrec, nTab, nCol,
     662           0 :                 nFirstDataRow, nLastRow );
     663             :             // dBaseIII Limit Nachkommastellen: 15
     664           0 :             if ( nPrecision > 15 )
     665           0 :                 nPrecision = 15;
     666           0 :             if ( nPrec > 15 )
     667           0 :                 nPrec = 15;
     668           0 :             if ( bPrecDefined && nPrecision != nPrec )
     669             :             {   // Laenge auf vorgegebene Nachkommastellen anpassen
     670           0 :                 if ( nPrecision )
     671           0 :                     nLen = sal::static_int_cast<xub_StrLen>( nLen + ( nPrecision - nPrec ) );
     672             :                 else
     673           0 :                     nLen -= nPrec+1;            // auch den . mit raus
     674             :             }
     675           0 :             if ( nLen > nFieldLen && !bTypeDefined )
     676           0 :                 nFieldLen = nLen;
     677           0 :             if ( !bPrecDefined )
     678           0 :                 nPrecision = nPrec;
     679           0 :             if ( nFieldLen == 0 )
     680           0 :                 nFieldLen = 1;
     681           0 :             else if ( nFieldLen > 19 )
     682           0 :                 nFieldLen = 19;     // dBaseIII Limit Feldlaenge numerisch: 19
     683           0 :             if ( nPrecision && nFieldLen < nPrecision + 2 )
     684           0 :                 nFieldLen = nPrecision + 2;     // 0. muss mit reinpassen
     685             :             // 538 MUST: Sdb internal representation adds 2 to the field length!
     686             :             // To give the user what he wants we must substract it here.
     687             :              //! CAVEAT! There is no way to define a numeric field with a length
     688             :              //! of 1 and no decimals!
     689           0 :             if ( nFieldLen == 1 && nPrecision == 0 )
     690           0 :                 bSdbLenBad = sal_True;
     691           0 :             nFieldLen = SvDbaseConverter::ConvertPrecisionToOdbc( nFieldLen, nPrecision );
     692           0 :             bSdbLenAdjusted = sal_True;
     693             :         }
     694           0 :         if ( nFieldLen > 254 )
     695             :         {
     696           0 :             if ( nDbType == sdbc::DataType::VARCHAR )
     697             :             {   // zu lang fuer normales Textfeld => Memofeld
     698           0 :                 nDbType = sdbc::DataType::LONGVARCHAR;
     699           0 :                 nFieldLen = 10;
     700           0 :                 bHasMemo = sal_True;
     701             :             }
     702             :             else
     703           0 :                 nFieldLen = 254;                    // dumm gelaufen..
     704             :         }
     705             : 
     706           0 :         pColNames[nField] = aFieldName;
     707           0 :         pColTypes[nField] = nDbType;
     708           0 :         pColLengths[nField] = nFieldLen;
     709           0 :         pColScales[nField] = nPrecision;
     710             : 
     711             :         // undo change to field length, reflect reality
     712           0 :         if ( bSdbLenAdjusted )
     713             :         {
     714           0 :             nFieldLen = SvDbaseConverter::ConvertPrecisionToDbase( nFieldLen, nPrecision );
     715           0 :             if ( bSdbLenBad && nFieldLen == 1 )
     716           0 :                 nFieldLen = 2;      // THIS is reality
     717             :         }
     718           0 :         if ( bUpdateTitles )
     719             :         {   // Angabe anpassen und ausgeben
     720           0 :             OUString aOutString = aFieldName;
     721           0 :             switch ( nDbType )
     722             :             {
     723             :                 case sdbc::DataType::BIT :
     724           0 :                     aOutString += ",L";
     725           0 :                     break;
     726             :                 case sdbc::DataType::DATE :
     727           0 :                     aOutString += ",D";
     728           0 :                     break;
     729             :                 case sdbc::DataType::LONGVARCHAR :
     730           0 :                     aOutString += ",M";
     731           0 :                     break;
     732             :                 case sdbc::DataType::VARCHAR :
     733           0 :                     aOutString += ",C," + OUString::number( nFieldLen );
     734           0 :                     break;
     735             :                 case sdbc::DataType::DECIMAL :
     736           0 :                     aOutString += ",N,"  + OUString::number( nFieldLen ) +
     737           0 :                                   "," + OUString::number( nPrecision );
     738           0 :                     break;
     739             :             }
     740           0 :             if ( !aOutString.equalsIgnoreAsciiCase( aString ) )
     741             :             {
     742           0 :                 pDoc->SetString( nCol, nFirstRow, nTab, aOutString );
     743           0 :                 rDocShell.PostPaint( nCol, nFirstRow, nTab, nCol, nFirstRow, nTab, PAINT_GRID );
     744           0 :             }
     745             :         }
     746           0 :         ++nField;
     747           0 :     }
     748           0 : }
     749             : 
     750           0 : inline void lcl_getLongVarCharEditString( OUString& rString,
     751             :         const ScRefCellValue& rCell, ScFieldEditEngine& rEditEngine )
     752             : {
     753           0 :     if (!rCell.mpEditText)
     754           0 :         return;
     755             : 
     756           0 :     rEditEngine.SetText(*rCell.mpEditText);
     757           0 :     rString = rEditEngine.GetText( LINEEND_CRLF );
     758             : }
     759             : 
     760           0 : inline void lcl_getLongVarCharString(
     761             :     OUString& rString, ScDocument& rDoc, SCCOL nCol, SCROW nRow, SCTAB nTab, SvNumberFormatter& rNumFmt )
     762             : {
     763             :     Color* pColor;
     764           0 :     ScAddress aPos(nCol, nRow, nTab);
     765           0 :     sal_uInt32 nFormat = rDoc.GetNumberFormat(aPos);
     766           0 :     rString = ScCellFormat::GetString(rDoc, aPos, nFormat, &pColor, rNumFmt);
     767           0 : }
     768             : 
     769             : }
     770             : 
     771             : #endif // !DISABLE_DBCONNECTIVITY
     772             : 
     773           0 : sal_uLong ScDocShell::DBaseExport( const OUString& rFullFileName, CharSet eCharSet, bool& bHasMemo )
     774             : {
     775             : #ifdef DISABLE_DBCONNECTIVITY
     776             :     (void) rFullFileName;
     777             :     (void) eCharSet;
     778             :     (void) bHasMemo;
     779             : 
     780             :     return ERRCODE_IO_GENERAL;
     781             : #else
     782             :     // remove the file so the dBase driver doesn't find an invalid file
     783           0 :     INetURLObject aDeleteObj( rFullFileName, INET_PROT_FILE );
     784           0 :     KillFile( aDeleteObj );
     785             : 
     786           0 :     sal_uLong nErr = eERR_OK;
     787           0 :     uno::Any aAny;
     788             : 
     789             :     SCCOL nFirstCol, nLastCol;
     790             :     SCROW  nFirstRow, nLastRow;
     791           0 :     SCTAB nTab = GetSaveTab();
     792           0 :     aDocument.GetDataStart( nTab, nFirstCol, nFirstRow );
     793           0 :     aDocument.GetCellArea( nTab, nLastCol, nLastRow );
     794           0 :     if ( nFirstCol > nLastCol )
     795           0 :         nFirstCol = nLastCol;
     796           0 :     if ( nFirstRow > nLastRow )
     797           0 :         nFirstRow = nLastRow;
     798           0 :     ScProgress aProgress( this, ScGlobal::GetRscString( STR_SAVE_DOC ),
     799           0 :                                                     nLastRow - nFirstRow );
     800           0 :     SvNumberFormatter* pNumFmt = aDocument.GetFormatTable();
     801             : 
     802           0 :     sal_Bool bHasFieldNames = sal_True;
     803           0 :     for ( SCCOL nDocCol = nFirstCol; nDocCol <= nLastCol && bHasFieldNames; nDocCol++ )
     804             :     {   // nur Strings in erster Zeile => sind Feldnamen
     805           0 :         if ( !aDocument.HasStringData( nDocCol, nFirstRow, nTab ) )
     806           0 :             bHasFieldNames = false;
     807             :     }
     808             : 
     809           0 :     long nColCount = nLastCol - nFirstCol + 1;
     810           0 :     uno::Sequence<OUString> aColNames( nColCount );
     811           0 :     uno::Sequence<sal_Int32> aColTypes( nColCount );
     812           0 :     uno::Sequence<sal_Int32> aColLengths( nColCount );
     813           0 :     uno::Sequence<sal_Int32> aColScales( nColCount );
     814             : 
     815           0 :     ScRange aDataRange( nFirstCol, nFirstRow, nTab, nLastCol, nLastRow, nTab );
     816             :     lcl_GetColumnTypes( *this, aDataRange, bHasFieldNames,
     817             :                         aColNames.getArray(), aColTypes.getArray(),
     818             :                         aColLengths.getArray(), aColScales.getArray(),
     819           0 :                         bHasMemo, eCharSet );
     820             :     // also needed for exception catch
     821           0 :     SCROW nDocRow = 0;
     822           0 :     ScFieldEditEngine aEditEngine(&aDocument, aDocument.GetEditPool());
     823           0 :     OUString aString;
     824           0 :     String aTabName;
     825             : 
     826             :     try
     827             :     {
     828           0 :         uno::Reference<sdbc::XDriverManager2> xDrvMan;
     829           0 :         uno::Reference<sdbc::XConnection> xConnection;
     830           0 :         sal_uLong nRet = lcl_getDBaseConnection(xDrvMan,xConnection,aTabName,rFullFileName,eCharSet);
     831           0 :         if ( !xConnection.is() || !xDrvMan.is() )
     832           0 :             return nRet;
     833           0 :         ::utl::DisposableComponent aConnectionHelper(xConnection);
     834             : 
     835             :         // get dBase driver
     836           0 :         uno::Reference< sdbc::XDriverAccess> xAccess(xDrvMan,uno::UNO_QUERY);
     837           0 :         uno::Reference< sdbcx::XDataDefinitionSupplier > xDDSup( xAccess->getDriverByURL( xConnection->getMetaData()->getURL() ), uno::UNO_QUERY );
     838           0 :         if ( !xDDSup.is() )
     839           0 :             return SCERR_EXPORT_CONNECT;
     840             : 
     841             :         // create table
     842           0 :         uno::Reference<sdbcx::XTablesSupplier> xTablesSupp =xDDSup->getDataDefinitionByConnection( xConnection );
     843             :         OSL_ENSURE( xTablesSupp.is(), "can't get Data Definition" );
     844           0 :         if (!xTablesSupp.is()) return SCERR_EXPORT_CONNECT;
     845             : 
     846           0 :         uno::Reference<container::XNameAccess> xTables = xTablesSupp->getTables();
     847             :         OSL_ENSURE( xTables.is(), "can't get Tables" );
     848           0 :         if (!xTables.is()) return SCERR_EXPORT_CONNECT;
     849             : 
     850           0 :         uno::Reference<sdbcx::XDataDescriptorFactory> xTablesFact( xTables, uno::UNO_QUERY );
     851             :         OSL_ENSURE( xTablesFact.is(), "can't get tables factory" );
     852           0 :         if (!xTablesFact.is()) return SCERR_EXPORT_CONNECT;
     853             : 
     854           0 :         uno::Reference<sdbcx::XAppend> xTablesAppend( xTables, uno::UNO_QUERY );
     855             :         OSL_ENSURE( xTablesAppend.is(), "can't get tables XAppend" );
     856           0 :         if (!xTablesAppend.is()) return SCERR_EXPORT_CONNECT;
     857             : 
     858           0 :         uno::Reference<beans::XPropertySet> xTableDesc = xTablesFact->createDataDescriptor();
     859             :         OSL_ENSURE( xTableDesc.is(), "can't get table descriptor" );
     860           0 :         if (!xTableDesc.is()) return SCERR_EXPORT_CONNECT;
     861             : 
     862           0 :         aAny <<= OUString( aTabName );
     863           0 :         xTableDesc->setPropertyValue( OUString(SC_DBPROP_NAME), aAny );
     864             : 
     865             :         // create columns
     866             : 
     867           0 :         uno::Reference<sdbcx::XColumnsSupplier> xColumnsSupp( xTableDesc, uno::UNO_QUERY );
     868             :         OSL_ENSURE( xColumnsSupp.is(), "can't get columns supplier" );
     869           0 :         if (!xColumnsSupp.is()) return SCERR_EXPORT_CONNECT;
     870             : 
     871           0 :         uno::Reference<container::XNameAccess> xColumns = xColumnsSupp->getColumns();
     872             :         OSL_ENSURE( xColumns.is(), "can't get columns" );
     873           0 :         if (!xColumns.is()) return SCERR_EXPORT_CONNECT;
     874             : 
     875           0 :         uno::Reference<sdbcx::XDataDescriptorFactory> xColumnsFact( xColumns, uno::UNO_QUERY );
     876             :         OSL_ENSURE( xColumnsFact.is(), "can't get columns factory" );
     877           0 :         if (!xColumnsFact.is()) return SCERR_EXPORT_CONNECT;
     878             : 
     879           0 :         uno::Reference<sdbcx::XAppend> xColumnsAppend( xColumns, uno::UNO_QUERY );
     880             :         OSL_ENSURE( xColumnsAppend.is(), "can't get columns XAppend" );
     881           0 :         if (!xColumnsAppend.is()) return SCERR_EXPORT_CONNECT;
     882             : 
     883           0 :         const OUString* pColNames = aColNames.getConstArray();
     884           0 :         const sal_Int32* pColTypes     = aColTypes.getConstArray();
     885           0 :         const sal_Int32* pColLengths   = aColLengths.getConstArray();
     886           0 :         const sal_Int32* pColScales    = aColScales.getConstArray();
     887             :         long nCol;
     888             : 
     889           0 :         for (nCol=0; nCol<nColCount; nCol++)
     890             :         {
     891           0 :             uno::Reference<beans::XPropertySet> xColumnDesc = xColumnsFact->createDataDescriptor();
     892             :             OSL_ENSURE( xColumnDesc.is(), "can't get column descriptor" );
     893           0 :             if (!xColumnDesc.is()) return SCERR_EXPORT_CONNECT;
     894             : 
     895           0 :             aAny <<= pColNames[nCol];
     896           0 :             xColumnDesc->setPropertyValue( OUString(SC_DBPROP_NAME), aAny );
     897             : 
     898           0 :             aAny <<= pColTypes[nCol];
     899           0 :             xColumnDesc->setPropertyValue( OUString(SC_DBPROP_TYPE), aAny );
     900             : 
     901           0 :             aAny <<= pColLengths[nCol];
     902           0 :             xColumnDesc->setPropertyValue( OUString(SC_DBPROP_PRECISION), aAny );
     903             : 
     904           0 :             aAny <<= pColScales[nCol];
     905           0 :             xColumnDesc->setPropertyValue( OUString(SC_DBPROP_SCALE), aAny );
     906             : 
     907           0 :             xColumnsAppend->appendByDescriptor( xColumnDesc );
     908           0 :         }
     909             : 
     910           0 :         xTablesAppend->appendByDescriptor( xTableDesc );
     911             : 
     912             :         // get row set for writing
     913           0 :         uno::Reference<lang::XMultiServiceFactory> xFactory = comphelper::getProcessServiceFactory();
     914           0 :         uno::Reference<sdbc::XRowSet> xRowSet( xFactory->createInstance(
     915           0 :                             OUString( SC_SERVICE_ROWSET ) ),
     916           0 :                             uno::UNO_QUERY);
     917           0 :         ::utl::DisposableComponent aRowSetHelper(xRowSet);
     918           0 :         uno::Reference<beans::XPropertySet> xRowProp( xRowSet, uno::UNO_QUERY );
     919             :         OSL_ENSURE( xRowProp.is(), "can't get RowSet" );
     920           0 :         if (!xRowProp.is()) return SCERR_EXPORT_CONNECT;
     921             : 
     922           0 :         aAny <<= xConnection;
     923           0 :         xRowProp->setPropertyValue( OUString(SC_DBPROP_ACTIVECONNECTION), aAny );
     924             : 
     925           0 :         aAny <<= (sal_Int32) sdb::CommandType::TABLE;
     926           0 :         xRowProp->setPropertyValue( OUString(SC_DBPROP_COMMANDTYPE), aAny );
     927             : 
     928           0 :         aAny <<= OUString( aTabName );
     929           0 :         xRowProp->setPropertyValue( OUString(SC_DBPROP_COMMAND), aAny );
     930             : 
     931           0 :         xRowSet->execute();
     932             : 
     933             :         // write data rows
     934             : 
     935           0 :         uno::Reference<sdbc::XResultSetUpdate> xResultUpdate( xRowSet, uno::UNO_QUERY );
     936             :         OSL_ENSURE( xResultUpdate.is(), "can't get XResultSetUpdate" );
     937           0 :         if (!xResultUpdate.is()) return SCERR_EXPORT_CONNECT;
     938             : 
     939           0 :         uno::Reference<sdbc::XRowUpdate> xRowUpdate( xRowSet, uno::UNO_QUERY );
     940             :         OSL_ENSURE( xRowUpdate.is(), "can't get XRowUpdate" );
     941           0 :         if (!xRowUpdate.is()) return SCERR_EXPORT_CONNECT;
     942             : 
     943           0 :         SCROW nFirstDataRow = ( bHasFieldNames ? nFirstRow + 1 : nFirstRow );
     944             :         double fVal;
     945             : 
     946           0 :         for ( nDocRow = nFirstDataRow; nDocRow <= nLastRow; nDocRow++ )
     947             :         {
     948           0 :             xResultUpdate->moveToInsertRow();
     949             : 
     950           0 :             for (nCol=0; nCol<nColCount; nCol++)
     951             :             {
     952           0 :                 SCCOL nDocCol = sal::static_int_cast<SCCOL>( nFirstCol + nCol );
     953             : 
     954           0 :                 switch (pColTypes[nCol])
     955             :                 {
     956             :                     case sdbc::DataType::LONGVARCHAR:
     957             :                     {
     958           0 :                         ScRefCellValue aCell;
     959           0 :                         aCell.assign(aDocument, ScAddress(nDocCol, nDocRow, nTab));
     960           0 :                         if (!aCell.isEmpty())
     961             :                         {
     962           0 :                             if (aCell.meType == CELLTYPE_EDIT)
     963             :                             {   // Paragraphs erhalten
     964           0 :                                 lcl_getLongVarCharEditString(aString, aCell, aEditEngine);
     965             :                             }
     966             :                             else
     967             :                             {
     968             :                                 lcl_getLongVarCharString(
     969           0 :                                     aString, aDocument, nDocCol, nDocRow, nTab, *pNumFmt);
     970             :                             }
     971           0 :                             xRowUpdate->updateString( nCol+1, aString );
     972             :                         }
     973             :                         else
     974           0 :                             xRowUpdate->updateNull( nCol+1 );
     975             :                     }
     976           0 :                     break;
     977             : 
     978             :                     case sdbc::DataType::VARCHAR:
     979           0 :                         aString = aDocument.GetString(nDocCol, nDocRow, nTab);
     980           0 :                         xRowUpdate->updateString( nCol+1, aString );
     981           0 :                         if ( nErr == eERR_OK && pColLengths[nCol] < aString.getLength() )
     982           0 :                             nErr = SCWARN_EXPORT_DATALOST;
     983           0 :                         break;
     984             : 
     985             :                     case sdbc::DataType::DATE:
     986             :                         {
     987           0 :                             aDocument.GetValue( nDocCol, nDocRow, nTab, fVal );
     988             :                             // zwischen 0 Wert und 0 kein Wert unterscheiden
     989           0 :                             sal_Bool bIsNull = (fVal == 0.0);
     990           0 :                             if ( bIsNull )
     991           0 :                                 bIsNull = !aDocument.HasValueData( nDocCol, nDocRow, nTab );
     992           0 :                             if ( bIsNull )
     993             :                             {
     994           0 :                                 xRowUpdate->updateNull( nCol+1 );
     995           0 :                                 if ( nErr == eERR_OK &&
     996           0 :                                         aDocument.HasStringData( nDocCol, nDocRow, nTab ) )
     997           0 :                                     nErr = SCWARN_EXPORT_DATALOST;
     998             :                             }
     999             :                             else
    1000             :                             {
    1001           0 :                                 Date aDate = *(pNumFmt->GetNullDate());     // tools date
    1002           0 :                                 aDate += (long)fVal;                        //! approxfloor?
    1003           0 :                                 util::Date aUnoDate( aDate.GetDay(), aDate.GetMonth(), aDate.GetYear() );
    1004           0 :                                 xRowUpdate->updateDate( nCol+1, aUnoDate );
    1005             :                             }
    1006             :                         }
    1007           0 :                         break;
    1008             : 
    1009             :                     case sdbc::DataType::DECIMAL:
    1010             :                     case sdbc::DataType::BIT:
    1011           0 :                         aDocument.GetValue( nDocCol, nDocRow, nTab, fVal );
    1012           0 :                         if ( fVal == 0.0 && nErr == eERR_OK &&
    1013           0 :                                             aDocument.HasStringData( nDocCol, nDocRow, nTab ) )
    1014           0 :                             nErr = SCWARN_EXPORT_DATALOST;
    1015           0 :                         if ( pColTypes[nCol] == sdbc::DataType::BIT )
    1016           0 :                             xRowUpdate->updateBoolean( nCol+1, ( fVal != 0.0 ) );
    1017             :                         else
    1018           0 :                             xRowUpdate->updateDouble( nCol+1, fVal );
    1019           0 :                         break;
    1020             : 
    1021             :                     default:
    1022             :                         OSL_FAIL( "ScDocShell::DBaseExport: unknown FieldType" );
    1023           0 :                         if ( nErr == eERR_OK )
    1024           0 :                             nErr = SCWARN_EXPORT_DATALOST;
    1025           0 :                         aDocument.GetValue( nDocCol, nDocRow, nTab, fVal );
    1026           0 :                         xRowUpdate->updateDouble( nCol+1, fVal );
    1027             :                 }
    1028             :             }
    1029             : 
    1030           0 :             xResultUpdate->insertRow();
    1031             : 
    1032             :             //! error handling and recovery of old
    1033             :             //! ScDocShell::SbaSdbExport is still missing!
    1034             : 
    1035           0 :             if ( !aProgress.SetStateOnPercent( nDocRow - nFirstRow ) )
    1036             :             {   // UserBreak
    1037           0 :                 nErr = SCERR_EXPORT_DATA;
    1038           0 :                 break;
    1039             :             }
    1040             :         }
    1041             : 
    1042           0 :         comphelper::disposeComponent( xRowSet );
    1043           0 :         comphelper::disposeComponent( xConnection );
    1044             :     }
    1045           0 :     catch ( const sdbc::SQLException& aException )
    1046             :     {
    1047           0 :         sal_Int32 nError = aException.ErrorCode;
    1048             : #if OSL_DEBUG_LEVEL > 1
    1049             :         fprintf( stderr, "ScDocShell::DBaseExport: SQLException ErrorCode: %d, SQLState: %s, Message: %s\n",
    1050             :                 (int)nError, OUStringToOString( aException.SQLState,
    1051             :                     RTL_TEXTENCODING_UTF8).getStr(), OUStringToOString(
    1052             :                         aException.Message, RTL_TEXTENCODING_UTF8).getStr());
    1053             : #endif
    1054           0 :         if (nError == 22018 || nError == 22001)
    1055             :         {
    1056             :             // SQL error 22018: Character not in target encoding.
    1057             :             // SQL error 22001: String length exceeds field width (after encoding).
    1058           0 :             bool bEncErr = (nError == 22018);
    1059           0 :             bool bIsOctetTextEncoding = rtl_isOctetTextEncoding( eCharSet);
    1060             :             OSL_ENSURE( !bEncErr || bIsOctetTextEncoding, "ScDocShell::DBaseExport: encoding error and not an octect textencoding");
    1061           0 :             SCCOL nDocCol = nFirstCol;
    1062           0 :             const sal_Int32* pColTypes = aColTypes.getConstArray();
    1063           0 :             const sal_Int32* pColLengths = aColLengths.getConstArray();
    1064             :             ScHorizontalCellIterator aIter( &aDocument, nTab, nFirstCol,
    1065           0 :                     nDocRow, nLastCol, nDocRow);
    1066           0 :             ScRefCellValue* pCell = NULL;
    1067           0 :             bool bTest = true;
    1068           0 :             while (bTest && ((pCell = aIter.GetNext( nDocCol, nDocRow)) != NULL))
    1069             :             {
    1070           0 :                 SCCOL nCol = nDocCol - nFirstCol;
    1071           0 :                 switch (pColTypes[nCol])
    1072             :                 {
    1073             :                     case sdbc::DataType::LONGVARCHAR:
    1074             :                         {
    1075           0 :                             if (pCell->meType == CELLTYPE_EDIT)
    1076           0 :                                 lcl_getLongVarCharEditString(aString, *pCell, aEditEngine);
    1077             :                             else
    1078             :                                 lcl_getLongVarCharString(
    1079           0 :                                     aString, aDocument, nDocCol, nDocRow, nTab, *pNumFmt);
    1080             :                         }
    1081           0 :                         break;
    1082             : 
    1083             :                     case sdbc::DataType::VARCHAR:
    1084           0 :                         aString = aDocument.GetString(nDocCol, nDocRow, nTab);
    1085           0 :                         break;
    1086             : 
    1087             :                     // NOTE: length of DECIMAL fields doesn't need to be
    1088             :                     // checked here, the database driver adjusts the field
    1089             :                     // width accordingly.
    1090             : 
    1091             :                     default:
    1092           0 :                         bTest = false;
    1093             :                 }
    1094           0 :                 if (bTest)
    1095             :                 {
    1096             :                     sal_Int32 nLen;
    1097           0 :                     if (bIsOctetTextEncoding)
    1098             :                     {
    1099           0 :                         OUString aOUString( aString);
    1100           0 :                         OString aOString;
    1101           0 :                         if (!aOUString.convertToString( &aOString, eCharSet,
    1102             :                                     RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR |
    1103           0 :                                     RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR))
    1104             :                         {
    1105           0 :                             bTest = false;
    1106           0 :                             bEncErr = true;
    1107             :                         }
    1108           0 :                         nLen = aOString.getLength();
    1109             : #if OSL_DEBUG_LEVEL > 1
    1110             :                         if (!bTest)
    1111             :                             fprintf( stderr, "ScDocShell::DBaseExport encoding error, string with default replacements: ``%s''\n",
    1112             :                                     OUStringToOString( aOUString, eCharSet).getStr());
    1113             : #endif
    1114             :                     }
    1115             :                     else
    1116           0 :                         nLen = aString.getLength() * sizeof(sal_Unicode);
    1117           0 :                     if (!bEncErr &&
    1118           0 :                             pColTypes[nCol] != sdbc::DataType::LONGVARCHAR &&
    1119           0 :                             pColLengths[nCol] < nLen)
    1120             :                     {
    1121           0 :                         bTest = false;
    1122             : #if OSL_DEBUG_LEVEL > 1
    1123             :                         fprintf( stderr, "ScDocShell::DBaseExport: field width: %d, encoded length: %d\n",
    1124             :                                 (int)pColLengths[nCol], (int)nLen);
    1125             : #endif
    1126             :                     }
    1127             :                 }
    1128             :                 else
    1129           0 :                     bTest = true;
    1130             :             }
    1131           0 :             String sPosition( ScAddress( nDocCol, nDocRow, nTab).GetColRowString());
    1132           0 :             String sEncoding( SvxTextEncodingTable().GetTextString( eCharSet));
    1133           0 :             nErr = *new TwoStringErrorInfo( (bEncErr ? SCERR_EXPORT_ENCODING :
    1134             :                         SCERR_EXPORT_FIELDWIDTH), sPosition, sEncoding,
    1135           0 :                     ERRCODE_BUTTON_OK | ERRCODE_MSG_ERROR);
    1136             :         }
    1137           0 :         else if ( !aException.Message.isEmpty() )
    1138           0 :             nErr = *new StringErrorInfo( (SCERR_EXPORT_SQLEXCEPTION), aException.Message, ERRCODE_BUTTON_OK | ERRCODE_MSG_ERROR);
    1139             :         else
    1140           0 :             nErr = SCERR_EXPORT_DATA;
    1141             :     }
    1142           0 :     catch ( uno::Exception& )
    1143             :     {
    1144             :         OSL_FAIL("Unexpected exception in database");
    1145           0 :         nErr = ERRCODE_IO_GENERAL;
    1146             :     }
    1147             : 
    1148           0 :     return nErr;
    1149             : #endif // !DISABLE_DBCONNECTIVITY
    1150          93 : }
    1151             : 
    1152             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10