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