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