LCOV - code coverage report
Current view: top level - sc/source/ui/docshell - docsh8.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 40 553 7.2 %
Date: 2014-11-03 Functions: 4 14 28.6 %
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 <config_features.h>
      21             : 
      22             : #include <stdio.h>
      23             : #include <tools/urlobj.hxx>
      24             : #include <svl/converter.hxx>
      25             : #include <comphelper/processfactory.hxx>
      26             : #include <comphelper/string.hxx>
      27             : #include <comphelper/types.hxx>
      28             : #include <ucbhelper/content.hxx>
      29             : #include <svx/txenctab.hxx>
      30             : 
      31             : #if HAVE_FEATURE_DBCONNECTIVITY
      32             : #include <svx/dbcharsethelper.hxx>
      33             : #endif
      34             : 
      35             : #include <com/sun/star/sdb/CommandType.hpp>
      36             : #include <com/sun/star/sdbc/DataType.hpp>
      37             : #include <com/sun/star/sdbc/XConnection.hpp>
      38             : #include <com/sun/star/sdbc/XDriver.hpp>
      39             : #include <com/sun/star/sdbc/XDriverAccess.hpp>
      40             : #include <com/sun/star/sdbc/DriverManager.hpp>
      41             : #include <com/sun/star/sdbc/XResultSetUpdate.hpp>
      42             : #include <com/sun/star/sdbc/XRow.hpp>
      43             : #include <com/sun/star/sdbc/XRowSet.hpp>
      44             : #include <com/sun/star/sdbc/XRowUpdate.hpp>
      45             : #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
      46             : #include <com/sun/star/sdbcx/XAppend.hpp>
      47             : #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
      48             : #include <com/sun/star/sdbcx/XDataDefinitionSupplier.hpp>
      49             : #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
      50             : #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
      51             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      52             : #include <com/sun/star/beans/XPropertySet.hpp>
      53             : #include <com/sun/star/container/XEnumerationAccess.hpp>
      54             : #include <com/sun/star/lang/XComponent.hpp>
      55             : #include <com/sun/star/ucb/NameClash.hpp>
      56             : #include <com/sun/star/ucb/TransferInfo.hpp>
      57             : #include <com/sun/star/ucb/XCommandInfo.hpp>
      58             : 
      59             : #include "scerrors.hxx"
      60             : #include "docsh.hxx"
      61             : #include "filter.hxx"
      62             : #include "progress.hxx"
      63             : #include "formulacell.hxx"
      64             : #include "editutil.hxx"
      65             : #include "cellform.hxx"
      66             : #include "dbdocutl.hxx"
      67             : #include "dociter.hxx"
      68             : #include "globstr.hrc"
      69             : #include <svl/zformat.hxx>
      70             : #include <svl/intitem.hxx>
      71             : #include "patattr.hxx"
      72             : #include "scitems.hxx"
      73             : #include "docpool.hxx"
      74             : #include "segmenttree.hxx"
      75             : #include "docparam.hxx"
      76             : #include "cellvalue.hxx"
      77             : 
      78             : #include <vector>
      79             : #include <boost/unordered_set.hpp>
      80             : 
      81             : using namespace com::sun::star;
      82             : using ::std::vector;
      83             : 
      84             : #if HAVE_FEATURE_DBCONNECTIVITY
      85             : 
      86             : #define SC_SERVICE_ROWSET           "com.sun.star.sdb.RowSet"
      87             : 
      88             : //! move to a header file?
      89             : #define SC_DBPROP_ACTIVECONNECTION  "ActiveConnection"
      90             : #define SC_DBPROP_COMMAND           "Command"
      91             : #define SC_DBPROP_COMMANDTYPE       "CommandType"
      92             : #define SC_DBPROP_PROPCHANGE_NOTIFY "PropertyChangeNotificationEnabled"
      93             : 
      94             : #define SC_DBPROP_NAME              "Name"
      95             : #define SC_DBPROP_TYPE              "Type"
      96             : #define SC_DBPROP_PRECISION         "Precision"
      97             : #define SC_DBPROP_SCALE             "Scale"
      98             : 
      99             : #define SC_DBPROP_EXTENSION         "Extension"
     100             : #define SC_DBPROP_CHARSET           "CharSet"
     101             : 
     102             : namespace
     103             : {
     104           2 :     sal_uLong lcl_getDBaseConnection(uno::Reference<sdbc::XDriverManager2>& _rDrvMgr, uno::Reference<sdbc::XConnection>& _rConnection, OUString& _rTabName, const OUString& rFullFileName, rtl_TextEncoding eCharSet)
     105             :     {
     106           2 :         INetURLObject aURL;
     107           2 :         aURL.SetSmartProtocol( INET_PROT_FILE );
     108           2 :         aURL.SetSmartURL( rFullFileName );
     109           4 :         _rTabName = aURL.getBase( INetURLObject::LAST_SEGMENT, true,
     110           2 :                 INetURLObject::DECODE_UNAMBIGUOUS );
     111           4 :         OUString aExtension = aURL.getExtension();
     112           2 :         aURL.removeSegment();
     113           2 :         aURL.removeFinalSlash();
     114           4 :         OUString aPath = aURL.GetMainURL(INetURLObject::NO_DECODE);
     115           4 :         uno::Reference<uno::XComponentContext> xContext = comphelper::getProcessComponentContext();
     116             : 
     117           2 :         _rDrvMgr.set( sdbc::DriverManager::create( xContext ) );
     118             : 
     119             :         // get connection
     120             : 
     121           4 :         OUString aConnUrl("sdbc:dbase:");
     122           2 :         aConnUrl += aPath;
     123             : 
     124           4 :         svxform::ODataAccessCharsetHelper aHelper;
     125           4 :         ::std::vector< rtl_TextEncoding > aEncodings;
     126           2 :         aHelper.getSupportedTextEncodings( aEncodings );
     127           2 :         ::std::vector< rtl_TextEncoding >::iterator aIter = ::std::find(aEncodings.begin(),aEncodings.end(),(rtl_TextEncoding) eCharSet);
     128           2 :         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           4 :         OUString aCharSetStr;
     134           2 :         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           4 :         uno::Sequence<beans::PropertyValue> aProps(2);
     143           2 :         aProps[0].Name = SC_DBPROP_EXTENSION;
     144           2 :         aProps[0].Value <<= OUString( aExtension );
     145           2 :         aProps[1].Name = SC_DBPROP_CHARSET;
     146           2 :         aProps[1].Value <<= aCharSetStr;
     147             : 
     148           2 :         _rConnection = _rDrvMgr->getConnectionWithInfo( aConnUrl, aProps );
     149           4 :         return 0L;
     150             :     }
     151             : }
     152             : 
     153             : #endif // HAVE_FEATURE_DBCONNECTIVITY
     154             : 
     155             : // MoveFile/KillFile/IsDocument: similar to SfxContentHelper
     156             : 
     157           0 : bool ScDocShell::MoveFile( const INetURLObject& rSourceObj, const INetURLObject& rDestObj )
     158             : {
     159           0 :     bool bMoveData = true;
     160           0 :     bool bRet = true, bKillSource = false;
     161           0 :     if ( rSourceObj.GetProtocol() != rDestObj.GetProtocol() )
     162             :     {
     163           0 :         bMoveData = false;
     164           0 :         bKillSource = true;
     165             :     }
     166           0 :     OUString aName = rDestObj.getName();
     167           0 :     INetURLObject aDestPathObj = rDestObj;
     168           0 :     aDestPathObj.removeSegment();
     169           0 :     aDestPathObj.setFinalSlash();
     170             : 
     171             :     try
     172             :     {
     173             :         ::ucbhelper::Content aDestPath( aDestPathObj.GetMainURL(INetURLObject::NO_DECODE),
     174             :                             uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(),
     175           0 :                             comphelper::getProcessComponentContext() );
     176           0 :         uno::Reference< ::com::sun::star::ucb::XCommandInfo > xInfo = aDestPath.getCommands();
     177           0 :         OUString aTransferName = "transfer";
     178           0 :         if ( xInfo->hasCommandByName( aTransferName ) )
     179             :         {
     180             :             aDestPath.executeCommand( aTransferName, uno::makeAny(
     181             :                 ::com::sun::star::ucb::TransferInfo( bMoveData, rSourceObj.GetMainURL(INetURLObject::NO_DECODE), aName,
     182           0 :                                                        ::com::sun::star::ucb::NameClash::ERROR ) ) );
     183             :         }
     184             :         else
     185             :         {
     186             :             OSL_FAIL( "transfer command not available" );
     187           0 :         }
     188             :     }
     189           0 :     catch( uno::Exception& )
     190             :     {
     191             :         // ucb may throw different exceptions on failure now
     192           0 :         bRet = false;
     193             :     }
     194             : 
     195           0 :     if ( bKillSource )
     196           0 :         KillFile( rSourceObj );
     197             : 
     198           0 :     return bRet;
     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             : #if HAVE_FEATURE_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 // HAVE_FEATURE_DBCONNECTIVITY
     289             : 
     290           2 : sal_uLong ScDocShell::DBaseImport( const OUString& rFullFileName, rtl_TextEncoding eCharSet,
     291             :                                ScColWidthParam aColWidthParam[MAXCOLCOUNT], ScFlatBoolRowSegments& rRowHeightsRecalc )
     292             : {
     293             : #if !HAVE_FEATURE_DBCONNECTIVITY
     294             :     (void) rFullFileName;
     295             :     (void) eCharSet;
     296             :     (void) aColWidthParam;
     297             :     (void) rRowHeightsRecalc;
     298             : 
     299             :     return ERRCODE_IO_GENERAL;
     300             : #else
     301             : 
     302           2 :     sal_uLong nErr = eERR_OK;
     303             :     long i;
     304           2 :     long nColCount = 0;
     305             : 
     306             :     // Try to get the Text Encoding from the driver
     307           2 :     if( eCharSet == RTL_TEXTENCODING_IBM_850 )
     308           2 :         eCharSet = RTL_TEXTENCODING_DONTKNOW;
     309             : 
     310             :     try
     311             :     {
     312           2 :         OUString aTabName;
     313           2 :         uno::Reference<sdbc::XDriverManager2> xDrvMan;
     314           2 :         uno::Reference<sdbc::XConnection> xConnection;
     315           2 :         sal_uLong nRet = lcl_getDBaseConnection(xDrvMan,xConnection,aTabName,rFullFileName,eCharSet);
     316           2 :         if ( !xConnection.is() || !xDrvMan.is() )
     317           2 :             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 :         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 = 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 // HAVE_FEATURE_DBCONNECTIVITY
     466             : }
     467             : 
     468             : #if HAVE_FEATURE_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& rDoc = rDocShell.GetDocument();
     488           0 :     SvNumberFormatter* pNumFmt = rDoc.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 = rDoc.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(rDoc, 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 :                 rDoc.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 = rDoc.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 = rDoc.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             : #if 1
     690             :                     // Adjust length to predefined precision.
     691           0 :                     nLen = nLen + ( nPrecision - nPrec );
     692             : #else
     693             :     /* If the above override for (nPrecision < nPrec) was not in place then
     694             :      * nPrecision could be 0 and this would be the code path to correctly
     695             :      * calculate nLen. But as is, nPrecision is never 0 here, see CID#982304 */
     696             : 
     697             :                     // Adjust length to predefined precision.
     698             :                     if ( nPrecision )
     699             :                         nLen = nLen + ( nPrecision - nPrec );
     700             :                     else
     701             :                         nLen -= nPrec+1;    // also remove the decimal separator
     702             : #endif
     703             :                 }
     704             :             }
     705           0 :             if (nFieldLen < nLen)
     706             :             {
     707           0 :                 if (!bTypeDefined)
     708           0 :                     nFieldLen = nLen;
     709             :                 else
     710             :                 {
     711             :                     // Again a hairy case and conflict. Furthermore, the
     712             :                     // larger overall length may be a result of only a higher
     713             :                     // precision obtained from formats.
     714             :                     SAL_WARN( "sc", "lcl_GetColumnTypes: conflicting dBase field length for "
     715             :                             << aFieldName << " (" << nFieldLen << "<" << nLen << ")");
     716           0 :                     nFieldLen = nLen;
     717             :                 }
     718             :             }
     719           0 :             if ( !bPrecDefined )
     720           0 :                 nPrecision = nPrec;
     721           0 :             if ( nFieldLen == 0 )
     722           0 :                 nFieldLen = 1;
     723           0 :             else if ( nFieldLen > 19 )
     724           0 :                 nFieldLen = 19;     // dBaseIII numeric field length limit: 19
     725           0 :             if ( nPrecision && nFieldLen < nPrecision + 2 )
     726           0 :                 nFieldLen = nPrecision + 2;     // 0. must fit into
     727             :             // 538 MUST: Sdb internal representation adds 2 to the field length!
     728             :             // To give the user what he wants we must subtract it here.
     729             :              //! CAVEAT! There is no way to define a numeric field with a length
     730             :              //! of 1 and no decimals!
     731           0 :             if ( nFieldLen == 1 && nPrecision == 0 )
     732           0 :                 bSdbLenBad = true;
     733           0 :             nFieldLen = SvDbaseConverter::ConvertPrecisionToOdbc( nFieldLen, nPrecision );
     734           0 :             bSdbLenAdjusted = true;
     735             :         }
     736           0 :         if ( nFieldLen > 254 )
     737             :         {
     738           0 :             if ( nDbType == sdbc::DataType::VARCHAR )
     739             :             {   // Too long for a normal text field => memo field.
     740           0 :                 nDbType = sdbc::DataType::LONGVARCHAR;
     741           0 :                 nFieldLen = 10;
     742           0 :                 bHasMemo = true;
     743             :             }
     744             :             else
     745           0 :                 nFieldLen = 254;                    // bad luck..
     746             :         }
     747             : 
     748           0 :         pColNames[nField] = aFieldName;
     749           0 :         pColTypes[nField] = nDbType;
     750           0 :         pColLengths[nField] = nFieldLen;
     751           0 :         pColScales[nField] = nPrecision;
     752             : 
     753             :         // undo change to field length, reflect reality
     754           0 :         if ( bSdbLenAdjusted )
     755             :         {
     756           0 :             nFieldLen = SvDbaseConverter::ConvertPrecisionToDbase( nFieldLen, nPrecision );
     757           0 :             if ( bSdbLenBad && nFieldLen == 1 )
     758           0 :                 nFieldLen = 2;      // THIS is reality
     759             :         }
     760           0 :         ++nField;
     761           0 :     }
     762           0 : }
     763             : 
     764           0 : inline void lcl_getLongVarCharEditString( OUString& rString,
     765             :         const ScRefCellValue& rCell, ScFieldEditEngine& rEditEngine )
     766             : {
     767           0 :     if (!rCell.mpEditText)
     768           0 :         return;
     769             : 
     770           0 :     rEditEngine.SetText(*rCell.mpEditText);
     771           0 :     rString = rEditEngine.GetText( LINEEND_CRLF );
     772             : }
     773             : 
     774           0 : inline void lcl_getLongVarCharString(
     775             :     OUString& rString, ScDocument& rDoc, SCCOL nCol, SCROW nRow, SCTAB nTab, SvNumberFormatter& rNumFmt )
     776             : {
     777             :     Color* pColor;
     778           0 :     ScAddress aPos(nCol, nRow, nTab);
     779           0 :     sal_uInt32 nFormat = rDoc.GetNumberFormat(aPos);
     780           0 :     rString = ScCellFormat::GetString(rDoc, aPos, nFormat, &pColor, rNumFmt);
     781           0 : }
     782             : 
     783             : }
     784             : 
     785             : #endif // HAVE_FEATURE_DBCONNECTIVITY
     786             : 
     787           0 : sal_uLong ScDocShell::DBaseExport( const OUString& rFullFileName, rtl_TextEncoding eCharSet, bool& bHasMemo )
     788             : {
     789             : #if !HAVE_FEATURE_DBCONNECTIVITY
     790             :     (void) rFullFileName;
     791             :     (void) eCharSet;
     792             :     (void) bHasMemo;
     793             : 
     794             :     return ERRCODE_IO_GENERAL;
     795             : #else
     796             :     // remove the file so the dBase driver doesn't find an invalid file
     797           0 :     INetURLObject aDeleteObj( rFullFileName, INET_PROT_FILE );
     798           0 :     KillFile( aDeleteObj );
     799             : 
     800           0 :     sal_uLong nErr = eERR_OK;
     801           0 :     uno::Any aAny;
     802             : 
     803             :     SCCOL nFirstCol, nLastCol;
     804             :     SCROW  nFirstRow, nLastRow;
     805           0 :     SCTAB nTab = GetSaveTab();
     806           0 :     aDocument.GetDataStart( nTab, nFirstCol, nFirstRow );
     807           0 :     aDocument.GetCellArea( nTab, nLastCol, nLastRow );
     808           0 :     if ( nFirstCol > nLastCol )
     809           0 :         nFirstCol = nLastCol;
     810           0 :     if ( nFirstRow > nLastRow )
     811           0 :         nFirstRow = nLastRow;
     812           0 :     ScProgress aProgress( this, ScGlobal::GetRscString( STR_SAVE_DOC ),
     813           0 :                                                     nLastRow - nFirstRow );
     814           0 :     SvNumberFormatter* pNumFmt = aDocument.GetFormatTable();
     815             : 
     816           0 :     bool bHasFieldNames = true;
     817           0 :     for ( SCCOL nDocCol = nFirstCol; nDocCol <= nLastCol && bHasFieldNames; nDocCol++ )
     818             :     {   // nur Strings in erster Zeile => sind Feldnamen
     819           0 :         if ( !aDocument.HasStringData( nDocCol, nFirstRow, nTab ) )
     820           0 :             bHasFieldNames = false;
     821             :     }
     822             : 
     823           0 :     long nColCount = nLastCol - nFirstCol + 1;
     824           0 :     uno::Sequence<OUString> aColNames( nColCount );
     825           0 :     uno::Sequence<sal_Int32> aColTypes( nColCount );
     826           0 :     uno::Sequence<sal_Int32> aColLengths( nColCount );
     827           0 :     uno::Sequence<sal_Int32> aColScales( nColCount );
     828             : 
     829           0 :     ScRange aDataRange( nFirstCol, nFirstRow, nTab, nLastCol, nLastRow, nTab );
     830             :     lcl_GetColumnTypes( *this, aDataRange, bHasFieldNames,
     831             :                         aColNames.getArray(), aColTypes.getArray(),
     832             :                         aColLengths.getArray(), aColScales.getArray(),
     833           0 :                         bHasMemo, eCharSet );
     834             :     // also needed for exception catch
     835           0 :     SCROW nDocRow = 0;
     836           0 :     ScFieldEditEngine aEditEngine(&aDocument, aDocument.GetEditPool());
     837           0 :     OUString aString;
     838           0 :     OUString aTabName;
     839             : 
     840             :     try
     841             :     {
     842           0 :         uno::Reference<sdbc::XDriverManager2> xDrvMan;
     843           0 :         uno::Reference<sdbc::XConnection> xConnection;
     844           0 :         sal_uLong nRet = lcl_getDBaseConnection(xDrvMan,xConnection,aTabName,rFullFileName,eCharSet);
     845           0 :         if ( !xConnection.is() || !xDrvMan.is() )
     846           0 :             return nRet;
     847           0 :         ::utl::DisposableComponent aConnectionHelper(xConnection);
     848             : 
     849             :         // get dBase driver
     850           0 :         uno::Reference< sdbc::XDriverAccess> xAccess(xDrvMan,uno::UNO_QUERY);
     851           0 :         uno::Reference< sdbcx::XDataDefinitionSupplier > xDDSup( xAccess->getDriverByURL( xConnection->getMetaData()->getURL() ), uno::UNO_QUERY );
     852           0 :         if ( !xDDSup.is() )
     853           0 :             return SCERR_EXPORT_CONNECT;
     854             : 
     855             :         // create table
     856           0 :         uno::Reference<sdbcx::XTablesSupplier> xTablesSupp =xDDSup->getDataDefinitionByConnection( xConnection );
     857             :         OSL_ENSURE( xTablesSupp.is(), "can't get Data Definition" );
     858           0 :         if (!xTablesSupp.is()) return SCERR_EXPORT_CONNECT;
     859             : 
     860           0 :         uno::Reference<container::XNameAccess> xTables = xTablesSupp->getTables();
     861             :         OSL_ENSURE( xTables.is(), "can't get Tables" );
     862           0 :         if (!xTables.is()) return SCERR_EXPORT_CONNECT;
     863             : 
     864           0 :         uno::Reference<sdbcx::XDataDescriptorFactory> xTablesFact( xTables, uno::UNO_QUERY );
     865             :         OSL_ENSURE( xTablesFact.is(), "can't get tables factory" );
     866           0 :         if (!xTablesFact.is()) return SCERR_EXPORT_CONNECT;
     867             : 
     868           0 :         uno::Reference<sdbcx::XAppend> xTablesAppend( xTables, uno::UNO_QUERY );
     869             :         OSL_ENSURE( xTablesAppend.is(), "can't get tables XAppend" );
     870           0 :         if (!xTablesAppend.is()) return SCERR_EXPORT_CONNECT;
     871             : 
     872           0 :         uno::Reference<beans::XPropertySet> xTableDesc = xTablesFact->createDataDescriptor();
     873             :         OSL_ENSURE( xTableDesc.is(), "can't get table descriptor" );
     874           0 :         if (!xTableDesc.is()) return SCERR_EXPORT_CONNECT;
     875             : 
     876           0 :         aAny <<= OUString( aTabName );
     877           0 :         xTableDesc->setPropertyValue( OUString(SC_DBPROP_NAME), aAny );
     878             : 
     879             :         // create columns
     880             : 
     881           0 :         uno::Reference<sdbcx::XColumnsSupplier> xColumnsSupp( xTableDesc, uno::UNO_QUERY );
     882             :         OSL_ENSURE( xColumnsSupp.is(), "can't get columns supplier" );
     883           0 :         if (!xColumnsSupp.is()) return SCERR_EXPORT_CONNECT;
     884             : 
     885           0 :         uno::Reference<container::XNameAccess> xColumns = xColumnsSupp->getColumns();
     886             :         OSL_ENSURE( xColumns.is(), "can't get columns" );
     887           0 :         if (!xColumns.is()) return SCERR_EXPORT_CONNECT;
     888             : 
     889           0 :         uno::Reference<sdbcx::XDataDescriptorFactory> xColumnsFact( xColumns, uno::UNO_QUERY );
     890             :         OSL_ENSURE( xColumnsFact.is(), "can't get columns factory" );
     891           0 :         if (!xColumnsFact.is()) return SCERR_EXPORT_CONNECT;
     892             : 
     893           0 :         uno::Reference<sdbcx::XAppend> xColumnsAppend( xColumns, uno::UNO_QUERY );
     894             :         OSL_ENSURE( xColumnsAppend.is(), "can't get columns XAppend" );
     895           0 :         if (!xColumnsAppend.is()) return SCERR_EXPORT_CONNECT;
     896             : 
     897           0 :         const OUString* pColNames = aColNames.getConstArray();
     898           0 :         const sal_Int32* pColTypes     = aColTypes.getConstArray();
     899           0 :         const sal_Int32* pColLengths   = aColLengths.getConstArray();
     900           0 :         const sal_Int32* pColScales    = aColScales.getConstArray();
     901             :         long nCol;
     902             : 
     903           0 :         for (nCol=0; nCol<nColCount; nCol++)
     904             :         {
     905           0 :             uno::Reference<beans::XPropertySet> xColumnDesc = xColumnsFact->createDataDescriptor();
     906             :             OSL_ENSURE( xColumnDesc.is(), "can't get column descriptor" );
     907           0 :             if (!xColumnDesc.is()) return SCERR_EXPORT_CONNECT;
     908             : 
     909           0 :             aAny <<= pColNames[nCol];
     910           0 :             xColumnDesc->setPropertyValue( OUString(SC_DBPROP_NAME), aAny );
     911             : 
     912           0 :             aAny <<= pColTypes[nCol];
     913           0 :             xColumnDesc->setPropertyValue( OUString(SC_DBPROP_TYPE), aAny );
     914             : 
     915           0 :             aAny <<= pColLengths[nCol];
     916           0 :             xColumnDesc->setPropertyValue( OUString(SC_DBPROP_PRECISION), aAny );
     917             : 
     918           0 :             aAny <<= pColScales[nCol];
     919           0 :             xColumnDesc->setPropertyValue( OUString(SC_DBPROP_SCALE), aAny );
     920             : 
     921           0 :             xColumnsAppend->appendByDescriptor( xColumnDesc );
     922           0 :         }
     923             : 
     924           0 :         xTablesAppend->appendByDescriptor( xTableDesc );
     925             : 
     926             :         // get row set for writing
     927           0 :         uno::Reference<lang::XMultiServiceFactory> xFactory = comphelper::getProcessServiceFactory();
     928           0 :         uno::Reference<sdbc::XRowSet> xRowSet( xFactory->createInstance(
     929           0 :                             OUString( SC_SERVICE_ROWSET ) ),
     930           0 :                             uno::UNO_QUERY);
     931           0 :         ::utl::DisposableComponent aRowSetHelper(xRowSet);
     932           0 :         uno::Reference<beans::XPropertySet> xRowProp( xRowSet, uno::UNO_QUERY );
     933             :         OSL_ENSURE( xRowProp.is(), "can't get RowSet" );
     934           0 :         if (!xRowProp.is()) return SCERR_EXPORT_CONNECT;
     935             : 
     936           0 :         aAny <<= xConnection;
     937           0 :         xRowProp->setPropertyValue( OUString(SC_DBPROP_ACTIVECONNECTION), aAny );
     938             : 
     939           0 :         aAny <<= (sal_Int32) sdb::CommandType::TABLE;
     940           0 :         xRowProp->setPropertyValue( OUString(SC_DBPROP_COMMANDTYPE), aAny );
     941             : 
     942           0 :         aAny <<= OUString( aTabName );
     943           0 :         xRowProp->setPropertyValue( OUString(SC_DBPROP_COMMAND), aAny );
     944             : 
     945           0 :         xRowSet->execute();
     946             : 
     947             :         // write data rows
     948             : 
     949           0 :         uno::Reference<sdbc::XResultSetUpdate> xResultUpdate( xRowSet, uno::UNO_QUERY );
     950             :         OSL_ENSURE( xResultUpdate.is(), "can't get XResultSetUpdate" );
     951           0 :         if (!xResultUpdate.is()) return SCERR_EXPORT_CONNECT;
     952             : 
     953           0 :         uno::Reference<sdbc::XRowUpdate> xRowUpdate( xRowSet, uno::UNO_QUERY );
     954             :         OSL_ENSURE( xRowUpdate.is(), "can't get XRowUpdate" );
     955           0 :         if (!xRowUpdate.is()) return SCERR_EXPORT_CONNECT;
     956             : 
     957           0 :         SCROW nFirstDataRow = ( bHasFieldNames ? nFirstRow + 1 : nFirstRow );
     958             :         double fVal;
     959             : 
     960           0 :         for ( nDocRow = nFirstDataRow; nDocRow <= nLastRow; nDocRow++ )
     961             :         {
     962           0 :             xResultUpdate->moveToInsertRow();
     963             : 
     964           0 :             for (nCol=0; nCol<nColCount; nCol++)
     965             :             {
     966           0 :                 SCCOL nDocCol = sal::static_int_cast<SCCOL>( nFirstCol + nCol );
     967             : 
     968           0 :                 switch (pColTypes[nCol])
     969             :                 {
     970             :                     case sdbc::DataType::LONGVARCHAR:
     971             :                     {
     972           0 :                         ScRefCellValue aCell;
     973           0 :                         aCell.assign(aDocument, ScAddress(nDocCol, nDocRow, nTab));
     974           0 :                         if (!aCell.isEmpty())
     975             :                         {
     976           0 :                             if (aCell.meType == CELLTYPE_EDIT)
     977             :                             {   // Paragraphs erhalten
     978           0 :                                 lcl_getLongVarCharEditString(aString, aCell, aEditEngine);
     979             :                             }
     980             :                             else
     981             :                             {
     982             :                                 lcl_getLongVarCharString(
     983           0 :                                     aString, aDocument, nDocCol, nDocRow, nTab, *pNumFmt);
     984             :                             }
     985           0 :                             xRowUpdate->updateString( nCol+1, aString );
     986             :                         }
     987             :                         else
     988           0 :                             xRowUpdate->updateNull( nCol+1 );
     989             :                     }
     990           0 :                     break;
     991             : 
     992             :                     case sdbc::DataType::VARCHAR:
     993           0 :                         aString = aDocument.GetString(nDocCol, nDocRow, nTab);
     994           0 :                         xRowUpdate->updateString( nCol+1, aString );
     995           0 :                         if ( nErr == eERR_OK && pColLengths[nCol] < aString.getLength() )
     996           0 :                             nErr = SCWARN_EXPORT_DATALOST;
     997           0 :                         break;
     998             : 
     999             :                     case sdbc::DataType::DATE:
    1000             :                         {
    1001           0 :                             aDocument.GetValue( nDocCol, nDocRow, nTab, fVal );
    1002             :                             // zwischen 0 Wert und 0 kein Wert unterscheiden
    1003           0 :                             bool bIsNull = (fVal == 0.0);
    1004           0 :                             if ( bIsNull )
    1005           0 :                                 bIsNull = !aDocument.HasValueData( nDocCol, nDocRow, nTab );
    1006           0 :                             if ( bIsNull )
    1007             :                             {
    1008           0 :                                 xRowUpdate->updateNull( nCol+1 );
    1009           0 :                                 if ( nErr == eERR_OK &&
    1010           0 :                                         aDocument.HasStringData( nDocCol, nDocRow, nTab ) )
    1011           0 :                                     nErr = SCWARN_EXPORT_DATALOST;
    1012             :                             }
    1013             :                             else
    1014             :                             {
    1015           0 :                                 Date aDate = *(pNumFmt->GetNullDate());     // tools date
    1016           0 :                                 aDate += (long)fVal;                        //! approxfloor?
    1017           0 :                                 util::Date aUnoDate( aDate.GetDay(), aDate.GetMonth(), aDate.GetYear() );
    1018           0 :                                 xRowUpdate->updateDate( nCol+1, aUnoDate );
    1019             :                             }
    1020             :                         }
    1021           0 :                         break;
    1022             : 
    1023             :                     case sdbc::DataType::DECIMAL:
    1024             :                     case sdbc::DataType::BIT:
    1025           0 :                         aDocument.GetValue( nDocCol, nDocRow, nTab, fVal );
    1026           0 :                         if ( fVal == 0.0 && nErr == eERR_OK &&
    1027           0 :                                             aDocument.HasStringData( nDocCol, nDocRow, nTab ) )
    1028           0 :                             nErr = SCWARN_EXPORT_DATALOST;
    1029           0 :                         if ( pColTypes[nCol] == sdbc::DataType::BIT )
    1030           0 :                             xRowUpdate->updateBoolean( nCol+1, ( fVal != 0.0 ) );
    1031             :                         else
    1032           0 :                             xRowUpdate->updateDouble( nCol+1, fVal );
    1033           0 :                         break;
    1034             : 
    1035             :                     default:
    1036             :                         OSL_FAIL( "ScDocShell::DBaseExport: unknown FieldType" );
    1037           0 :                         if ( nErr == eERR_OK )
    1038           0 :                             nErr = SCWARN_EXPORT_DATALOST;
    1039           0 :                         aDocument.GetValue( nDocCol, nDocRow, nTab, fVal );
    1040           0 :                         xRowUpdate->updateDouble( nCol+1, fVal );
    1041             :                 }
    1042             :             }
    1043             : 
    1044           0 :             xResultUpdate->insertRow();
    1045             : 
    1046             :             //! error handling and recovery of old
    1047             :             //! ScDocShell::SbaSdbExport is still missing!
    1048             : 
    1049           0 :             if ( !aProgress.SetStateOnPercent( nDocRow - nFirstRow ) )
    1050             :             {   // UserBreak
    1051           0 :                 nErr = SCERR_EXPORT_DATA;
    1052           0 :                 break;
    1053             :             }
    1054             :         }
    1055             : 
    1056           0 :         comphelper::disposeComponent( xRowSet );
    1057           0 :         comphelper::disposeComponent( xConnection );
    1058             :     }
    1059           0 :     catch ( const sdbc::SQLException& aException )
    1060             :     {
    1061           0 :         sal_Int32 nError = aException.ErrorCode;
    1062             : #if OSL_DEBUG_LEVEL > 1
    1063             :         fprintf( stderr, "ScDocShell::DBaseExport: SQLException ErrorCode: %d, SQLState: %s, Message: %s\n",
    1064             :                 (int)nError, OUStringToOString( aException.SQLState,
    1065             :                     RTL_TEXTENCODING_UTF8).getStr(), OUStringToOString(
    1066             :                         aException.Message, RTL_TEXTENCODING_UTF8).getStr());
    1067             : #endif
    1068           0 :         if (nError == 22018 || nError == 22001)
    1069             :         {
    1070             :             // SQL error 22018: Character not in target encoding.
    1071             :             // SQL error 22001: String length exceeds field width (after encoding).
    1072           0 :             bool bEncErr = (nError == 22018);
    1073           0 :             bool bIsOctetTextEncoding = rtl_isOctetTextEncoding( eCharSet);
    1074             :             OSL_ENSURE( !bEncErr || bIsOctetTextEncoding, "ScDocShell::DBaseExport: encoding error and not an octect textencoding");
    1075           0 :             SCCOL nDocCol = nFirstCol;
    1076           0 :             const sal_Int32* pColTypes = aColTypes.getConstArray();
    1077           0 :             const sal_Int32* pColLengths = aColLengths.getConstArray();
    1078             :             ScHorizontalCellIterator aIter( &aDocument, nTab, nFirstCol,
    1079           0 :                     nDocRow, nLastCol, nDocRow);
    1080           0 :             ScRefCellValue* pCell = NULL;
    1081           0 :             bool bTest = true;
    1082           0 :             while (bTest && ((pCell = aIter.GetNext( nDocCol, nDocRow)) != NULL))
    1083             :             {
    1084           0 :                 SCCOL nCol = nDocCol - nFirstCol;
    1085           0 :                 switch (pColTypes[nCol])
    1086             :                 {
    1087             :                     case sdbc::DataType::LONGVARCHAR:
    1088             :                         {
    1089           0 :                             if (pCell->meType == CELLTYPE_EDIT)
    1090           0 :                                 lcl_getLongVarCharEditString(aString, *pCell, aEditEngine);
    1091             :                             else
    1092             :                                 lcl_getLongVarCharString(
    1093           0 :                                     aString, aDocument, nDocCol, nDocRow, nTab, *pNumFmt);
    1094             :                         }
    1095           0 :                         break;
    1096             : 
    1097             :                     case sdbc::DataType::VARCHAR:
    1098           0 :                         aString = aDocument.GetString(nDocCol, nDocRow, nTab);
    1099           0 :                         break;
    1100             : 
    1101             :                     // NOTE: length of DECIMAL fields doesn't need to be
    1102             :                     // checked here, the database driver adjusts the field
    1103             :                     // width accordingly.
    1104             : 
    1105             :                     default:
    1106           0 :                         bTest = false;
    1107             :                 }
    1108           0 :                 if (bTest)
    1109             :                 {
    1110             :                     sal_Int32 nLen;
    1111           0 :                     if (bIsOctetTextEncoding)
    1112             :                     {
    1113           0 :                         OUString aOUString( aString);
    1114           0 :                         OString aOString;
    1115           0 :                         if (!aOUString.convertToString( &aOString, eCharSet,
    1116             :                                     RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR |
    1117           0 :                                     RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR))
    1118             :                         {
    1119           0 :                             bTest = false;
    1120           0 :                             bEncErr = true;
    1121             :                         }
    1122           0 :                         nLen = aOString.getLength();
    1123             : #if OSL_DEBUG_LEVEL > 1
    1124             :                         if (!bTest)
    1125             :                             fprintf( stderr, "ScDocShell::DBaseExport encoding error, string with default replacements: ``%s''\n",
    1126             :                                     OUStringToOString( aOUString, eCharSet).getStr());
    1127             : #endif
    1128             :                     }
    1129             :                     else
    1130           0 :                         nLen = aString.getLength() * sizeof(sal_Unicode);
    1131           0 :                     if (!bEncErr &&
    1132           0 :                             pColTypes[nCol] != sdbc::DataType::LONGVARCHAR &&
    1133           0 :                             pColLengths[nCol] < nLen)
    1134             :                     {
    1135           0 :                         bTest = false;
    1136             : #if OSL_DEBUG_LEVEL > 1
    1137             :                         fprintf( stderr, "ScDocShell::DBaseExport: field width: %d, encoded length: %d\n",
    1138             :                                 (int)pColLengths[nCol], (int)nLen);
    1139             : #endif
    1140             :                     }
    1141             :                 }
    1142             :                 else
    1143           0 :                     bTest = true;
    1144             :             }
    1145           0 :             OUString sPosition( ScAddress( nDocCol, nDocRow, nTab).GetColRowString());
    1146           0 :             OUString sEncoding( SvxTextEncodingTable().GetTextString( eCharSet));
    1147           0 :             nErr = *new TwoStringErrorInfo( (bEncErr ? SCERR_EXPORT_ENCODING :
    1148             :                         SCERR_EXPORT_FIELDWIDTH), sPosition, sEncoding,
    1149           0 :                     ERRCODE_BUTTON_OK | ERRCODE_MSG_ERROR);
    1150             :         }
    1151           0 :         else if ( !aException.Message.isEmpty() )
    1152           0 :             nErr = *new StringErrorInfo( (SCERR_EXPORT_SQLEXCEPTION), aException.Message, ERRCODE_BUTTON_OK | ERRCODE_MSG_ERROR);
    1153             :         else
    1154           0 :             nErr = SCERR_EXPORT_DATA;
    1155             :     }
    1156           0 :     catch ( uno::Exception& )
    1157             :     {
    1158             :         OSL_FAIL("Unexpected exception in database");
    1159           0 :         nErr = ERRCODE_IO_GENERAL;
    1160             :     }
    1161             : 
    1162           0 :     return nErr;
    1163             : #endif // HAVE_FEATURE_DBCONNECTIVITY
    1164         228 : }
    1165             : 
    1166             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10