LCOV - code coverage report
Current view: top level - sc/source/ui/docshell - docsh8.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 554 0.0 %
Date: 2014-04-14 Functions: 0 12 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10