Version 3.6.0.2, tag libreoffice-3.6.0.2
[LibreOffice.git] / dbaccess / source / ui / misc / DExport.cxx
blobc58af20caf7301e86e518943ec8737b77b3e07b3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "DExport.hxx"
31 #include "moduledbu.hxx"
33 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
34 #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
35 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
36 #include <com/sun/star/sdbcx/XAppend.hpp>
37 #include <com/sun/star/sdbcx/KeyType.hpp>
38 #include <com/sun/star/sdbc/DataType.hpp>
39 #include <com/sun/star/sdbc/ColumnValue.hpp>
40 #include <com/sun/star/sdb/CommandType.hpp>
41 #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
42 #include <com/sun/star/sdbc/XRow.hpp>
43 #include <com/sun/star/util/NumberFormat.hpp>
44 #include <com/sun/star/util/XNumberFormatTypes.hpp>
45 #include "dbustrings.hrc"
46 #include "dbu_misc.hrc"
47 #include <connectivity/dbconversion.hxx>
48 #include <sfx2/sfxhtml.hxx>
49 #include <svl/numuno.hxx>
50 #include <connectivity/dbtools.hxx>
51 #include <comphelper/extract.hxx>
52 #include "TypeInfo.hxx"
53 #include "FieldDescriptions.hxx"
54 #include "UITools.hxx"
55 #include <unotools/configmgr.hxx>
56 #include <memory>
57 #include <o3tl/compat_functional.hxx>
58 #include <tools/debug.hxx>
59 #include <tools/diagnose_ex.h>
60 #include <i18npool/mslangid.hxx>
61 #include <com/sun/star/awt/FontDescriptor.hpp>
62 #include "WCopyTable.hxx"
63 #include "WExtendPages.hxx"
64 #include "WCPage.hxx"
65 #include <unotools/syslocale.hxx>
66 #include <svl/zforlist.hxx>
67 #include <connectivity/dbexception.hxx>
68 #include <connectivity/FValue.hxx>
69 #include <com/sun/star/sdbc/SQLWarning.hpp>
70 #include <com/sun/star/sdb/SQLContext.hpp>
71 #include <com/sun/star/sdb/application/CopyTableOperation.hpp>
72 #include "sqlmessage.hxx"
73 #include "UpdateHelperImpl.hxx"
74 #include <vcl/msgbox.hxx>
75 #include <cppuhelper/exc_hlp.hxx>
76 #include <rtl/logfile.hxx>
78 using namespace dbaui;
79 using namespace utl;
80 using namespace ::com::sun::star::uno;
81 using namespace ::com::sun::star::beans;
82 using namespace ::com::sun::star::container;
83 using namespace ::com::sun::star::util;
84 using namespace ::com::sun::star::sdbc;
85 using namespace ::com::sun::star::sdbcx;
86 using namespace ::com::sun::star::sdb;
87 using namespace ::com::sun::star::lang;
88 using namespace ::com::sun::star::awt;
90 namespace CopyTableOperation = ::com::sun::star::sdb::application::CopyTableOperation;
92 // ==========================================================================
93 // ODatabaseExport
94 // ==========================================================================
95 DBG_NAME(ODatabaseExport)
96 ODatabaseExport::ODatabaseExport(sal_Int32 nRows,
97 const TPositions &_rColumnPositions,
98 const Reference< XNumberFormatter >& _rxNumberF,
99 const Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rM,
100 const TColumnVector* pList,
101 const OTypeInfoMap* _pInfoMap,
102 sal_Bool _bAutoIncrementEnabled,
103 SvStream& _rInputStream)
104 :m_vColumns(_rColumnPositions)
105 ,m_aDestColumns(sal_True)
106 ,m_xFormatter(_rxNumberF)
107 ,m_xFactory(_rM)
108 ,m_pFormatter(NULL)
109 ,m_rInputStream( _rInputStream )
110 ,m_pTypeInfo()
111 ,m_pColumnList(pList)
112 ,m_pInfoMap(_pInfoMap)
113 ,m_nColumnPos(0)
114 ,m_nRows(1)
115 ,m_nRowCount(0)
116 ,m_nDefToken( osl_getThreadTextEncoding() )
117 ,m_bError(sal_False)
118 ,m_bInTbl(sal_False)
119 ,m_bHead(sal_True)
120 ,m_bDontAskAgain(sal_False)
121 ,m_bIsAutoIncrement(_bAutoIncrementEnabled)
122 ,m_bFoundTable(sal_False)
123 ,m_bCheckOnly(sal_False)
124 ,m_bAppendFirstLine(false)
126 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::ODatabaseExport" );
127 DBG_CTOR(ODatabaseExport,NULL);
129 m_nRows += nRows;
130 sal_Int32 nCount = 0;
131 for(sal_Int32 j=0;j < (sal_Int32)m_vColumns.size();++j)
132 if ( m_vColumns[j].first != COLUMN_POSITION_NOT_FOUND )
133 ++nCount;
135 m_vColumnSize.resize(nCount);
136 m_vNumberFormat.resize(nCount);
137 for(sal_Int32 i=0;i<nCount;++i)
139 m_vColumnSize[i] = 0;
140 m_vNumberFormat[i] = 0;
145 SvtSysLocale aSysLocale;
146 m_aLocale = aSysLocale.GetLocaleData().getLocale();
148 catch(Exception&)
152 SetColumnTypes(pList,_pInfoMap);
154 //---------------------------------------------------------------------------
155 ODatabaseExport::ODatabaseExport(const SharedConnection& _rxConnection,
156 const Reference< XNumberFormatter >& _rxNumberF,
157 const Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rM,
158 const TColumnVector* pList,
159 const OTypeInfoMap* _pInfoMap,
160 SvStream& _rInputStream)
161 :m_aDestColumns(_rxConnection->getMetaData().is() && _rxConnection->getMetaData()->supportsMixedCaseQuotedIdentifiers() == sal_True)
162 ,m_xConnection(_rxConnection)
163 ,m_xFormatter(_rxNumberF)
164 ,m_xFactory(_rM)
165 ,m_pFormatter(NULL)
166 ,m_rInputStream( _rInputStream )
167 ,m_pTypeInfo()
168 ,m_pColumnList(NULL)
169 ,m_pInfoMap(NULL)
170 ,m_nColumnPos(0)
171 ,m_nRows(1)
172 ,m_nRowCount(0)
173 ,m_nDefToken( osl_getThreadTextEncoding() )
174 ,m_bError(sal_False)
175 ,m_bInTbl(sal_False)
176 ,m_bHead(sal_True)
177 ,m_bDontAskAgain(sal_False)
178 ,m_bIsAutoIncrement(sal_False)
179 ,m_bFoundTable(sal_False)
180 ,m_bCheckOnly(sal_False)
181 ,m_bAppendFirstLine(false)
183 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::ODatabaseExport" );
184 DBG_CTOR(ODatabaseExport,NULL);
187 SvtSysLocale aSysLocale;
188 m_aLocale = aSysLocale.GetLocaleData().getLocale();
190 catch(Exception&)
194 Reference<XTablesSupplier> xTablesSup(m_xConnection,UNO_QUERY);
195 if(xTablesSup.is())
196 m_xTables = xTablesSup->getTables();
198 Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
199 Reference<XResultSet> xSet = xMeta.is() ? xMeta->getTypeInfo() : Reference<XResultSet>();
200 if(xSet.is())
202 ::connectivity::ORowSetValue aValue;
203 ::std::vector<sal_Int32> aTypes;
204 ::std::vector<sal_Bool> aNullable;
205 Reference<XResultSetMetaData> xResultSetMetaData = Reference<XResultSetMetaDataSupplier>(xSet,UNO_QUERY_THROW)->getMetaData();
206 Reference<XRow> xRow(xSet,UNO_QUERY_THROW);
207 while(xSet->next())
209 if ( aTypes.empty() )
211 sal_Int32 nCount = xResultSetMetaData->getColumnCount();
212 if ( nCount < 1 )
213 nCount = 18;
214 aTypes.reserve(nCount+1);
215 aNullable.reserve(nCount+1);
216 aTypes.push_back(-1);
217 aNullable.push_back(sal_False);
218 for (sal_Int32 j = 1; j <= nCount ; ++j)
220 aNullable.push_back(xResultSetMetaData->isNullable(j) != ColumnValue::NO_NULLS );
221 aTypes.push_back(xResultSetMetaData->getColumnType(j));
225 sal_Int32 nPos = 1;
226 OSL_ENSURE((nPos) < static_cast<sal_Int32>(aTypes.size()),"aTypes: Illegal index for vector");
227 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
228 ::rtl::OUString sTypeName = aValue;
229 ++nPos;
230 OSL_ENSURE((nPos) < static_cast<sal_Int32>(aTypes.size()),"aTypes: Illegal index for vector");
231 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
232 sal_Int32 nType = aValue;
233 ++nPos;
235 if( nType == DataType::VARCHAR )
237 m_pTypeInfo = TOTypeInfoSP(new OTypeInfo());
239 m_pTypeInfo->aTypeName = sTypeName;
240 m_pTypeInfo->nType = nType;
242 OSL_ENSURE((nPos) < static_cast<sal_Int32>(aTypes.size()),"aTypes: Illegal index for vector");
243 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
244 m_pTypeInfo->nPrecision = aValue;
245 ++nPos;
246 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
247 m_pTypeInfo->aLiteralPrefix = aValue;
248 ++nPos;
249 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
250 m_pTypeInfo->aLiteralSuffix = aValue;
251 ++nPos;
252 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
253 m_pTypeInfo->aCreateParams = aValue;
254 ++nPos;
255 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
256 m_pTypeInfo->bNullable = (sal_Int32)aValue == ColumnValue::NULLABLE;
257 ++nPos;
258 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
259 m_pTypeInfo->bCaseSensitive = (sal_Bool)aValue;
260 ++nPos;
261 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
262 m_pTypeInfo->nSearchType = aValue;
263 ++nPos;
264 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
265 m_pTypeInfo->bUnsigned = (sal_Bool)aValue;
266 ++nPos;
267 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
268 m_pTypeInfo->bCurrency = (sal_Bool)aValue;
269 ++nPos;
270 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
271 m_pTypeInfo->bAutoIncrement = (sal_Bool)aValue;
272 ++nPos;
273 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
274 m_pTypeInfo->aLocalTypeName = aValue;
275 ++nPos;
276 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
277 m_pTypeInfo->nMinimumScale = aValue;
278 ++nPos;
279 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
280 m_pTypeInfo->nMaximumScale = aValue;
282 // check if values are less than zero like it happens in a oracle jdbc driver
283 if( m_pTypeInfo->nPrecision < 0)
284 m_pTypeInfo->nPrecision = 0;
285 if( m_pTypeInfo->nMinimumScale < 0)
286 m_pTypeInfo->nMinimumScale = 0;
287 if( m_pTypeInfo->nMaximumScale < 0)
288 m_pTypeInfo->nMaximumScale = 0;
289 break;
293 if ( !m_pTypeInfo )
294 m_pTypeInfo = TOTypeInfoSP(new OTypeInfo());
295 SetColumnTypes(pList,_pInfoMap);
297 //---------------------------------------------------------------------------
298 ODatabaseExport::~ODatabaseExport()
300 DBG_DTOR(ODatabaseExport,NULL);
301 m_pFormatter = NULL;
302 ODatabaseExport::TColumns::iterator aIter = m_aDestColumns.begin();
303 ODatabaseExport::TColumns::iterator aEnd = m_aDestColumns.end();
305 for(;aIter != aEnd;++aIter)
306 delete aIter->second;
307 m_vDestVector.clear();
308 m_aDestColumns.clear();
310 // -----------------------------------------------------------------------------
311 void ODatabaseExport::insertValueIntoColumn()
313 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::insertValueIntoColumn" );
314 DBG_CHKTHIS(ODatabaseExport,NULL);
315 if(m_nColumnPos < sal_Int32(m_vDestVector.size()))
317 OFieldDescription* pField = m_vDestVector[m_nColumnPos]->second;
318 if(pField)
320 sal_Int32 nNewPos = m_bIsAutoIncrement ? m_nColumnPos+1 : m_nColumnPos;
321 OSL_ENSURE((nNewPos) < static_cast<sal_Int32>(m_vColumns.size()),"m_vColumns: Illegal index for vector");
323 if ( (nNewPos) < static_cast<sal_Int32>(m_vColumns.size() ) )
325 sal_Int32 nPos = m_vColumns[nNewPos].first;
326 if ( nPos != COLUMN_POSITION_NOT_FOUND )
328 if ( !m_sTextToken.Len() && pField->IsNullable() )
329 m_pUpdateHelper->updateNull(nPos,pField->GetType());
330 else
332 OSL_ENSURE((nNewPos) < static_cast<sal_Int32>(m_vColumnTypes.size()),"Illegal index for vector");
333 if (m_vColumnTypes[nNewPos] != DataType::VARCHAR && m_vColumnTypes[nNewPos] != DataType::CHAR && m_vColumnTypes[nNewPos] != DataType::LONGVARCHAR )
335 RTL_LOGFILE_CONTEXT_TRACE( aLogger, "ODatabaseExport::insertValueIntoColumn != DataType::VARCHAR" );
336 ensureFormatter();
337 sal_Int32 nNumberFormat = 0;
338 double fOutNumber = 0.0;
339 bool bNumberFormatError = false;
340 if ( m_pFormatter && m_sNumToken.Len() )
342 LanguageType eNumLang = LANGUAGE_NONE;
343 sal_uInt32 nNumberFormat2( nNumberFormat );
344 fOutNumber = SfxHTMLParser::GetTableDataOptionsValNum(nNumberFormat2,eNumLang,m_sTextToken,m_sNumToken,*m_pFormatter);
345 if ( eNumLang != LANGUAGE_NONE )
347 nNumberFormat2 = m_pFormatter->GetFormatForLanguageIfBuiltIn( nNumberFormat2, eNumLang );
348 m_pFormatter->IsNumberFormat( m_sTextToken, nNumberFormat2, fOutNumber );
350 nNumberFormat = static_cast<sal_Int32>(nNumberFormat2);
352 else
354 Reference< XNumberFormatsSupplier > xSupplier = m_xFormatter->getNumberFormatsSupplier();
355 Reference<XNumberFormatTypes> xNumType(xSupplier->getNumberFormats(),UNO_QUERY);
356 sal_Int16 nFormats[] = {
357 NumberFormat::DATETIME
358 ,NumberFormat::DATE
359 ,NumberFormat::TIME
360 ,NumberFormat::CURRENCY
361 ,NumberFormat::NUMBER
362 ,NumberFormat::LOGICAL
364 for (size_t i = 0; i < sizeof(nFormats)/sizeof(nFormats[0]); ++i)
368 nNumberFormat = m_xFormatter->detectNumberFormat(xNumType->getStandardFormat(nFormats[i],m_aLocale),m_sTextToken);
369 break;
371 catch(Exception&)
377 fOutNumber = m_xFormatter->convertStringToNumber(nNumberFormat,m_sTextToken);
379 catch(Exception&)
381 bNumberFormatError = true;
382 m_pUpdateHelper->updateString(nPos,m_sTextToken);
385 if ( !bNumberFormatError )
389 Reference< XNumberFormatsSupplier > xSupplier = m_xFormatter->getNumberFormatsSupplier();
390 Reference< XNumberFormats > xFormats = xSupplier->getNumberFormats();
391 Reference<XPropertySet> xProp = xFormats->getByKey(nNumberFormat);
392 sal_Int16 nType = 0;
393 xProp->getPropertyValue(PROPERTY_TYPE) >>= nType;
394 switch(nType)
396 case NumberFormat::DATE:
397 m_pUpdateHelper->updateDate(nPos,::dbtools::DBTypeConversion::toDate(fOutNumber,m_aNullDate));
398 break;
399 case NumberFormat::DATETIME:
400 m_pUpdateHelper->updateTimestamp(nPos,::dbtools::DBTypeConversion::toDateTime(fOutNumber,m_aNullDate));
401 break;
402 case NumberFormat::TIME:
403 m_pUpdateHelper->updateTime(nPos,::dbtools::DBTypeConversion::toTime(fOutNumber));
404 break;
405 default:
406 m_pUpdateHelper->updateDouble(nPos,fOutNumber);
409 catch(Exception&)
411 m_pUpdateHelper->updateString(nPos,m_sTextToken);
416 else
417 m_pUpdateHelper->updateString(nPos,m_sTextToken);
421 eraseTokens();
425 // -----------------------------------------------------------------------------
426 sal_Int16 ODatabaseExport::CheckString(const String& aCheckToken, sal_Int16 _nOldNumberFormat)
428 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::CheckString" );
429 DBG_CHKTHIS(ODatabaseExport,NULL);
430 double fOutNumber = 0.0;
431 sal_Int16 nNumberFormat = 0;
435 Reference< XNumberFormatsSupplier > xSupplier = m_xFormatter->getNumberFormatsSupplier();
436 Reference< XNumberFormats > xFormats = xSupplier->getNumberFormats();
438 ensureFormatter();
439 if ( m_pFormatter && m_sNumToken.Len() )
441 LanguageType eNumLang;
442 sal_uInt32 nFormatKey(0);
443 fOutNumber = SfxHTMLParser::GetTableDataOptionsValNum(nFormatKey,eNumLang,m_sTextToken,m_sNumToken,*m_pFormatter);
444 if ( eNumLang != LANGUAGE_NONE )
446 nFormatKey = m_pFormatter->GetFormatForLanguageIfBuiltIn( nFormatKey, eNumLang );
447 if ( !m_pFormatter->IsNumberFormat( m_sTextToken, nFormatKey, fOutNumber ) )
448 return NumberFormat::TEXT;
450 Reference<XPropertySet> xProp = xFormats->getByKey(nFormatKey);
451 xProp->getPropertyValue(PROPERTY_TYPE) >>= nNumberFormat;
453 else
455 Reference<XNumberFormatTypes> xNumType(xFormats,UNO_QUERY);
456 sal_Int32 nFormatKey = m_xFormatter->detectNumberFormat(xNumType->getStandardFormat(NumberFormat::ALL,m_aLocale),aCheckToken);
457 fOutNumber = m_xFormatter->convertStringToNumber(nFormatKey,aCheckToken);
459 Reference<XPropertySet> xProp = xFormats->getByKey(nFormatKey);
460 sal_Int16 nType = 0;
461 xProp->getPropertyValue(PROPERTY_TYPE) >>= nType;
463 switch(nType)
465 case NumberFormat::ALL:
466 nNumberFormat = NumberFormat::ALL;
467 break;
468 case NumberFormat::DEFINED:
469 nNumberFormat = NumberFormat::TEXT;
470 break;
471 case NumberFormat::DATE:
472 switch(_nOldNumberFormat)
474 case NumberFormat::DATETIME:
475 case NumberFormat::TEXT:
476 case NumberFormat::DATE:
477 nNumberFormat = _nOldNumberFormat;
478 break;
479 case NumberFormat::ALL:
480 nNumberFormat = NumberFormat::DATE;
481 break;
482 default:
483 nNumberFormat = NumberFormat::TEXT;
486 break;
487 case NumberFormat::TIME:
488 switch(_nOldNumberFormat)
490 case NumberFormat::DATETIME:
491 case NumberFormat::TEXT:
492 case NumberFormat::TIME:
493 nNumberFormat = _nOldNumberFormat;
494 break;
495 case NumberFormat::ALL:
496 nNumberFormat = NumberFormat::TIME;
497 break;
498 default:
499 nNumberFormat = NumberFormat::TEXT;
500 break;
502 break;
503 case NumberFormat::CURRENCY:
504 switch(_nOldNumberFormat)
506 case NumberFormat::NUMBER:
507 nNumberFormat = NumberFormat::CURRENCY;
508 break;
509 case NumberFormat::CURRENCY:
510 nNumberFormat = _nOldNumberFormat;
511 break;
512 case NumberFormat::ALL:
513 nNumberFormat = NumberFormat::CURRENCY;
514 break;
515 default:
516 nNumberFormat = NumberFormat::TEXT;
517 break;
519 break;
520 case NumberFormat::NUMBER:
521 case NumberFormat::SCIENTIFIC:
522 case NumberFormat::FRACTION:
523 case NumberFormat::PERCENT:
524 switch(_nOldNumberFormat)
526 case NumberFormat::NUMBER:
527 nNumberFormat = _nOldNumberFormat;
528 break;
529 case NumberFormat::CURRENCY:
530 nNumberFormat = NumberFormat::CURRENCY;
531 break;
532 case NumberFormat::ALL:
533 nNumberFormat = nType;
534 break;
535 default:
536 nNumberFormat = NumberFormat::TEXT;
537 break;
539 break;
540 case NumberFormat::TEXT:
541 case NumberFormat::UNDEFINED:
542 case NumberFormat::LOGICAL:
543 nNumberFormat = NumberFormat::TEXT; // Text "uberschreibt alles
544 break;
545 case NumberFormat::DATETIME:
546 switch(_nOldNumberFormat)
548 case NumberFormat::DATETIME:
549 case NumberFormat::TEXT:
550 case NumberFormat::TIME:
551 nNumberFormat = _nOldNumberFormat;
552 break;
553 case NumberFormat::ALL:
554 nNumberFormat = NumberFormat::DATETIME;
555 break;
556 default:
557 nNumberFormat = NumberFormat::TEXT;
558 break;
560 break;
561 default:
562 OSL_FAIL("ODatabaseExport: Unbekanntes Format");
566 catch(Exception&)
568 nNumberFormat = NumberFormat::TEXT; // Text "uberschreibt alles
571 return nNumberFormat;
573 // -----------------------------------------------------------------------------
574 void ODatabaseExport::SetColumnTypes(const TColumnVector* _pList,const OTypeInfoMap* _pInfoMap)
576 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::SetColumnTypes" );
577 DBG_CHKTHIS(ODatabaseExport,NULL);
578 if(_pList && _pInfoMap)
580 OSL_ENSURE(m_vNumberFormat.size() == m_vColumnSize.size() && m_vColumnSize.size() == _pList->size(),"Illegal columns in list");
581 Reference< XNumberFormatsSupplier > xSupplier = m_xFormatter->getNumberFormatsSupplier();
582 Reference< XNumberFormats > xFormats = xSupplier->getNumberFormats();
583 TColumnVector::const_iterator aIter = _pList->begin();
584 TColumnVector::const_iterator aEnd = _pList->end();
585 for(sal_Int32 i= 0;aIter != aEnd && (i) < static_cast<sal_Int32>(m_vNumberFormat.size()) && (i) < static_cast<sal_Int32>(m_vColumnSize.size()) ;++aIter,++i)
587 sal_Int32 nDataType;
588 sal_Int32 nLength(0),nScale(0);
589 sal_Int16 nType = m_vNumberFormat[i] & ~NumberFormat::DEFINED;
591 switch ( nType )
593 case NumberFormat::ALL:
594 nDataType = DataType::DOUBLE;
595 break;
596 case NumberFormat::DEFINED:
597 nDataType = DataType::VARCHAR;
598 nLength = ((m_vColumnSize[i] % 10 ) ? m_vColumnSize[i]/ 10 + 1: m_vColumnSize[i]/ 10) * 10;
599 break;
600 case NumberFormat::DATE:
601 nDataType = DataType::DATE;
602 break;
603 case NumberFormat::TIME:
604 nDataType = DataType::TIME;
605 break;
606 case NumberFormat::DATETIME:
607 nDataType = DataType::TIMESTAMP;
608 break;
609 case NumberFormat::CURRENCY:
610 nDataType = DataType::NUMERIC;
611 nScale = 4;
612 nLength = 19;
613 break;
614 case NumberFormat::NUMBER:
615 case NumberFormat::SCIENTIFIC:
616 case NumberFormat::FRACTION:
617 case NumberFormat::PERCENT:
618 nDataType = DataType::DOUBLE;
619 break;
620 case NumberFormat::TEXT:
621 case NumberFormat::UNDEFINED:
622 case NumberFormat::LOGICAL:
623 default:
624 nDataType = DataType::VARCHAR;
625 nLength = ((m_vColumnSize[i] % 10 ) ? m_vColumnSize[i]/ 10 + 1: m_vColumnSize[i]/ 10) * 10;
626 break;
628 OTypeInfoMap::const_iterator aFind = _pInfoMap->find(nDataType);
629 if(aFind != _pInfoMap->end())
631 (*aIter)->second->SetType(aFind->second);
632 (*aIter)->second->SetPrecision(::std::min<sal_Int32>(aFind->second->nPrecision,nLength));
633 (*aIter)->second->SetScale(::std::min<sal_Int32>(aFind->second->nMaximumScale,nScale));
635 sal_Int32 nFormatKey = ::dbtools::getDefaultNumberFormat( nDataType,
636 (*aIter)->second->GetScale(),
637 (*aIter)->second->IsCurrency(),
638 Reference< XNumberFormatTypes>(xFormats,UNO_QUERY),
639 m_aLocale);
641 (*aIter)->second->SetFormatKey(nFormatKey);
646 // -----------------------------------------------------------------------------
647 void ODatabaseExport::CreateDefaultColumn(const ::rtl::OUString& _rColumnName)
649 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::CreateDefaultColumn" );
650 DBG_CHKTHIS(ODatabaseExport,NULL);
651 Reference< XDatabaseMetaData> xDestMetaData(m_xConnection->getMetaData());
652 sal_Int32 nMaxNameLen(xDestMetaData->getMaxColumnNameLength());
653 ::rtl::OUString aAlias = _rColumnName;
654 if ( isSQL92CheckEnabled(m_xConnection) )
655 aAlias = ::dbtools::convertName2SQLName(_rColumnName,xDestMetaData->getExtraNameCharacters());
657 if(nMaxNameLen && aAlias.getLength() > nMaxNameLen)
658 aAlias = aAlias.copy(0, ::std::min<sal_Int32>( nMaxNameLen-1, aAlias.getLength() ) );
660 ::rtl::OUString sName(aAlias);
661 if(m_aDestColumns.find(sName) != m_aDestColumns.end())
663 sal_Int32 nPos = 0;
664 sal_Int32 nCount = 2;
665 while(m_aDestColumns.find(sName) != m_aDestColumns.end())
667 sName = aAlias;
668 sName += ::rtl::OUString::valueOf(++nPos);
669 if(nMaxNameLen && sName.getLength() > nMaxNameLen)
671 aAlias = aAlias.copy(0,::std::min<sal_Int32>( nMaxNameLen-nCount, aAlias.getLength() ));
672 sName = aAlias;
673 sName += ::rtl::OUString::valueOf(nPos);
674 ++nCount;
678 aAlias = sName;
679 // now create a column
680 OFieldDescription* pField = new OFieldDescription();
681 pField->SetType(m_pTypeInfo);
682 pField->SetName(aAlias);
683 pField->SetPrecision(::std::min<sal_Int32>((sal_Int32)255,m_pTypeInfo->nPrecision));
684 pField->SetScale(0);
685 pField->SetIsNullable(ColumnValue::NULLABLE);
686 pField->SetAutoIncrement(sal_False);
687 pField->SetPrimaryKey(sal_False);
688 pField->SetCurrency(sal_False);
690 TColumns::iterator aFind = m_aDestColumns.find( aAlias );
691 if ( aFind != m_aDestColumns.end() )
693 delete aFind->second;
694 m_aDestColumns.erase(aFind);
697 m_vDestVector.push_back(m_aDestColumns.insert(TColumns::value_type(aAlias,pField)).first);
699 // -----------------------------------------------------------------------------
700 sal_Bool ODatabaseExport::createRowSet()
702 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::createRowSet" );
703 DBG_CHKTHIS(ODatabaseExport,NULL);
704 m_pUpdateHelper.reset(new OParameterUpdateHelper(createPreparedStatment(m_xConnection->getMetaData(),m_xTable,m_vColumns)));
706 return m_pUpdateHelper.get() != NULL;
708 // -----------------------------------------------------------------------------
709 sal_Bool ODatabaseExport::executeWizard(const ::rtl::OUString& _rTableName,const Any& _aTextColor,const FontDescriptor& _rFont)
711 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::executeWizard" );
712 DBG_CHKTHIS(ODatabaseExport,NULL);
714 bool bHaveDefaultTable = !m_sDefaultTableName.isEmpty();
715 ::rtl::OUString sTableName( bHaveDefaultTable ? m_sDefaultTableName : _rTableName );
716 OCopyTableWizard aWizard(
717 NULL,
718 sTableName,
719 bHaveDefaultTable ? CopyTableOperation::AppendData : CopyTableOperation::CopyDefinitionAndData,
720 m_aDestColumns,
721 m_vDestVector,
722 m_xConnection,
723 m_xFormatter,
724 getTypeSelectionPageFactory(),
725 m_rInputStream,
726 m_xFactory
729 sal_Bool bError = sal_False;
732 if (aWizard.Execute())
734 switch(aWizard.getOperation())
736 case CopyTableOperation::CopyDefinitionAndData:
737 case CopyTableOperation::AppendData:
739 m_xTable = aWizard.createTable();
740 bError = !m_xTable.is();
741 if(m_xTable.is())
743 m_xTable->setPropertyValue(PROPERTY_FONT,makeAny(_rFont));
744 if(_aTextColor.hasValue())
745 m_xTable->setPropertyValue(PROPERTY_TEXTCOLOR,_aTextColor);
747 m_bIsAutoIncrement = aWizard.shouldCreatePrimaryKey();
748 m_vColumns = aWizard.GetColumnPositions();
749 m_vColumnTypes = aWizard.GetColumnTypes();
750 m_bAppendFirstLine = !aWizard.UseHeaderLine();
752 break;
753 default:
754 bError = sal_True; // there is no error but I have nothing more to do
757 else
758 bError = sal_True;
760 if(!bError)
761 bError = !createRowSet();
763 catch( const SQLException&)
765 ::dbaui::showError( ::dbtools::SQLExceptionInfo( ::cppu::getCaughtException() ), &aWizard, m_xFactory );
766 bError = sal_True;
768 catch( const Exception& )
770 DBG_UNHANDLED_EXCEPTION();
773 return bError;
775 //---------------------------------------------------------------------------------
776 void ODatabaseExport::showErrorDialog(const ::com::sun::star::sdbc::SQLException& e)
778 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::showErrorDialog" );
779 if(!m_bDontAskAgain)
781 String aMsg(e.Message);
782 aMsg += '\n';
783 aMsg += String( ModuleRes( STR_QRY_CONTINUE ) );
784 OSQLWarningBox aBox( NULL, aMsg, WB_YES_NO | WB_DEF_NO );
786 if (aBox.Execute() == RET_YES)
787 m_bDontAskAgain = sal_True;
788 else
789 m_bError = sal_True;
792 // -----------------------------------------------------------------------------
793 void ODatabaseExport::adjustFormat()
795 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::adjustFormat" );
796 if ( m_sTextToken.Len() )
798 sal_Int32 nNewPos = m_bIsAutoIncrement ? m_nColumnPos+1 : m_nColumnPos;
799 OSL_ENSURE((nNewPos) < static_cast<sal_Int32>(m_vColumns.size()),"Illegal index for vector");
800 if ( (nNewPos) < static_cast<sal_Int32>(m_vColumns.size()) )
802 sal_Int32 nColPos = m_vColumns[nNewPos].first;
803 if( nColPos != sal::static_int_cast< long >(CONTAINER_ENTRY_NOTFOUND))
805 --nColPos;
806 OSL_ENSURE((nColPos) < static_cast<sal_Int32>(m_vNumberFormat.size()),"m_vFormatKey: Illegal index for vector");
807 OSL_ENSURE((nColPos) < static_cast<sal_Int32>(m_vColumnSize.size()),"m_vColumnSize: Illegal index for vector");
808 m_vNumberFormat[nColPos] = CheckString(m_sTextToken,m_vNumberFormat[nColPos]);
809 m_vColumnSize[nColPos] = ::std::max<sal_Int32>((sal_Int32)m_vColumnSize[nColPos],(sal_Int32)m_sTextToken.Len());
812 eraseTokens();
815 // -----------------------------------------------------------------------------
816 void ODatabaseExport::eraseTokens()
818 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::eraseTokens" );
819 m_sTextToken.Erase();
820 m_sNumToken.Erase();
821 m_sValToken.Erase();
823 // -----------------------------------------------------------------------------
824 void ODatabaseExport::ensureFormatter()
826 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::ensureFormatter" );
827 if ( !m_pFormatter )
829 Reference< XNumberFormatsSupplier > xSupplier = m_xFormatter->getNumberFormatsSupplier();
830 Reference< XUnoTunnel > xTunnel(xSupplier,UNO_QUERY);
831 SvNumberFormatsSupplierObj* pSupplierImpl = (SvNumberFormatsSupplierObj*)sal::static_int_cast< sal_IntPtr >(xTunnel->getSomething(SvNumberFormatsSupplierObj::getUnoTunnelId()));
832 m_pFormatter = pSupplierImpl ? pSupplierImpl->GetNumberFormatter() : NULL;
833 Reference<XPropertySet> xNumberFormatSettings = xSupplier->getNumberFormatSettings();
834 xNumberFormatSettings->getPropertyValue(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("NullDate"))) >>= m_aNullDate;
837 // -----------------------------------------------------------------------------
838 Reference< XPreparedStatement > ODatabaseExport::createPreparedStatment( const Reference<XDatabaseMetaData>& _xMetaData
839 ,const Reference<XPropertySet>& _xDestTable
840 ,const TPositions& _rvColumns)
842 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::createPreparedStatment" );
843 ::rtl::OUString aSql(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("INSERT INTO ")));
844 ::rtl::OUString sComposedTableName = ::dbtools::composeTableName( _xMetaData, _xDestTable, ::dbtools::eInDataManipulation, false, false, true );
846 aSql += sComposedTableName;
847 aSql += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" ( "));
848 // set values and column names
849 ::rtl::OUString aValues(RTL_CONSTASCII_USTRINGPARAM(" VALUES ( "));
850 static ::rtl::OUString aPara(RTL_CONSTASCII_USTRINGPARAM("?,"));
851 static ::rtl::OUString aComma(RTL_CONSTASCII_USTRINGPARAM(","));
853 ::rtl::OUString aQuote;
854 if ( _xMetaData.is() )
855 aQuote = _xMetaData->getIdentifierQuoteString();
857 Reference<XColumnsSupplier> xDestColsSup(_xDestTable,UNO_QUERY_THROW);
859 // create sql string and set column types
860 Sequence< ::rtl::OUString> aDestColumnNames = xDestColsSup->getColumns()->getElementNames();
861 if ( aDestColumnNames.getLength() == 0 )
863 return Reference< XPreparedStatement > ();
865 const ::rtl::OUString* pIter = aDestColumnNames.getConstArray();
866 ::std::vector< ::rtl::OUString> aInsertList;
867 aInsertList.resize(aDestColumnNames.getLength()+1);
868 sal_Int32 i = 0;
869 for(sal_uInt32 j=0; j < aInsertList.size() ;++i,++j)
871 ODatabaseExport::TPositions::const_iterator aFind = ::std::find_if(_rvColumns.begin(),_rvColumns.end(),
872 ::o3tl::compose1(::std::bind2nd(::std::equal_to<sal_Int32>(),i+1),::o3tl::select2nd<ODatabaseExport::TPositions::value_type>()));
873 if ( _rvColumns.end() != aFind && aFind->second != sal::static_int_cast< long >(CONTAINER_ENTRY_NOTFOUND) && aFind->first != sal::static_int_cast< long >(CONTAINER_ENTRY_NOTFOUND) )
875 OSL_ENSURE((aFind->first) < static_cast<sal_Int32>(aInsertList.size()),"aInsertList: Illegal index for vector");
876 aInsertList[aFind->first] = ::dbtools::quoteName( aQuote,*(pIter+i));
880 i = 1;
881 // create the sql string
882 ::std::vector< ::rtl::OUString>::iterator aInsertEnd = aInsertList.end();
883 for (::std::vector< ::rtl::OUString>::iterator aInsertIter = aInsertList.begin(); aInsertIter != aInsertEnd; ++aInsertIter)
885 if ( !aInsertIter->isEmpty() )
887 aSql += *aInsertIter;
888 aSql += aComma;
889 aValues += aPara;
893 aSql = aSql.replaceAt(aSql.getLength()-1,1,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(")")));
894 aValues = aValues.replaceAt(aValues.getLength()-1,1,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(")")));
896 aSql += aValues;
897 // now create,fill and execute the prepared statement
898 return Reference< XPreparedStatement >(_xMetaData->getConnection()->prepareStatement(aSql));
900 // -----------------------------------------------------------------------------
903 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */