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

Generated by: LCOV version 1.10