merge the formfield patch from ooo-build
[ooovba.git] / dbaccess / source / ui / misc / DExport.cxx
blob8f559e2a4a711626d862ee7a5e488e31ab5b0e54
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: DExport.cxx,v $
10 * $Revision: 1.40.32.2 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dbaccess.hxx"
34 #include "DExport.hxx"
35 #include "moduledbu.hxx"
37 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
38 #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
39 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
40 #include <com/sun/star/sdbcx/XAppend.hpp>
41 #include <com/sun/star/sdbcx/KeyType.hpp>
42 #include <com/sun/star/sdbc/DataType.hpp>
43 #include <com/sun/star/sdbc/ColumnValue.hpp>
44 #include <com/sun/star/sdb/CommandType.hpp>
45 #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
46 #include <com/sun/star/sdbc/XRow.hpp>
47 #include <com/sun/star/util/NumberFormat.hpp>
48 #include <com/sun/star/util/XNumberFormatTypes.hpp>
49 #include "dbustrings.hrc"
50 #include "dbu_misc.hrc"
51 #include <connectivity/dbconversion.hxx>
52 #include <sfx2/sfxhtml.hxx>
53 #include <svtools/numuno.hxx>
54 #include <connectivity/dbtools.hxx>
55 #include <comphelper/extract.hxx>
56 #include "TypeInfo.hxx"
57 #include "FieldDescriptions.hxx"
58 #include "UITools.hxx"
59 #include <unotools/configmgr.hxx>
60 #include <memory>
61 #include <tools/debug.hxx>
62 #include <tools/diagnose_ex.h>
63 #include <i18npool/mslangid.hxx>
64 #include <com/sun/star/awt/FontDescriptor.hpp>
65 #include "WCopyTable.hxx"
66 #include "WExtendPages.hxx"
67 #include "WCPage.hxx"
68 #include <svtools/syslocale.hxx>
69 #include <svtools/zforlist.hxx>
70 #include <connectivity/dbexception.hxx>
71 #include <connectivity/FValue.hxx>
72 #include <com/sun/star/sdbc/SQLWarning.hpp>
73 #include <com/sun/star/sdb/SQLContext.hpp>
74 #include <com/sun/star/sdb/application/CopyTableOperation.hpp>
75 #include "sqlmessage.hxx"
76 #include "UpdateHelperImpl.hxx"
77 #include <vcl/msgbox.hxx>
78 #include <cppuhelper/exc_hlp.hxx>
79 #include <rtl/logfile.hxx>
81 using namespace dbaui;
82 using namespace utl;
83 using namespace ::com::sun::star::uno;
84 using namespace ::com::sun::star::beans;
85 using namespace ::com::sun::star::container;
86 using namespace ::com::sun::star::util;
87 using namespace ::com::sun::star::sdbc;
88 using namespace ::com::sun::star::sdbcx;
89 using namespace ::com::sun::star::sdb;
90 using namespace ::com::sun::star::lang;
91 using namespace ::com::sun::star::awt;
93 namespace CopyTableOperation = ::com::sun::star::sdb::application::CopyTableOperation;
95 // ==========================================================================
96 // ODatabaseExport
97 // ==========================================================================
98 DBG_NAME(ODatabaseExport)
99 ODatabaseExport::ODatabaseExport(sal_Int32 nRows,
100 const TPositions &_rColumnPositions,
101 const Reference< XNumberFormatter >& _rxNumberF,
102 const Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rM,
103 const TColumnVector* pList,
104 const OTypeInfoMap* _pInfoMap,
105 sal_Bool _bAutoIncrementEnabled,
106 SvStream& _rInputStream)
107 :m_vColumns(_rColumnPositions)
108 ,m_aDestColumns(sal_True)
109 ,m_xFormatter(_rxNumberF)
110 ,m_xFactory(_rM)
111 ,m_pFormatter(NULL)
112 ,m_rInputStream( _rInputStream )
113 ,m_pTypeInfo()
114 ,m_pColumnList(pList)
115 ,m_pInfoMap(_pInfoMap)
116 ,m_nColumnPos(0)
117 ,m_nRows(1)
118 ,m_nRowCount(0)
119 ,m_nDefToken( gsl_getSystemTextEncoding() )
120 ,m_bError(FALSE)
121 ,m_bInTbl(FALSE)
122 ,m_bHead(TRUE)
123 ,m_bDontAskAgain(FALSE)
124 ,m_bIsAutoIncrement(_bAutoIncrementEnabled)
125 ,m_bFoundTable(sal_False)
126 ,m_bCheckOnly(sal_False)
127 ,m_bAppendFirstLine(false)
129 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::ODatabaseExport" );
130 DBG_CTOR(ODatabaseExport,NULL);
132 m_nRows += nRows;
133 sal_Int32 nCount = 0;
134 for(sal_Int32 j=0;j < (sal_Int32)m_vColumns.size();++j)
135 if ( m_vColumns[j].first != COLUMN_POSITION_NOT_FOUND )
136 ++nCount;
138 m_vColumnSize.resize(nCount);
139 m_vNumberFormat.resize(nCount);
140 for(sal_Int32 i=0;i<nCount;++i)
142 m_vColumnSize[i] = 0;
143 m_vNumberFormat[i] = 0;
148 SvtSysLocale aSysLocale;
149 m_aLocale = aSysLocale.GetLocaleData().getLocale();
151 catch(Exception&)
155 SetColumnTypes(pList,_pInfoMap);
157 //---------------------------------------------------------------------------
158 ODatabaseExport::ODatabaseExport(const SharedConnection& _rxConnection,
159 const Reference< XNumberFormatter >& _rxNumberF,
160 const Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rM,
161 const TColumnVector* pList,
162 const OTypeInfoMap* _pInfoMap,
163 SvStream& _rInputStream)
164 :m_aDestColumns(_rxConnection->getMetaData().is() && _rxConnection->getMetaData()->supportsMixedCaseQuotedIdentifiers() == sal_True)
165 ,m_xConnection(_rxConnection)
166 ,m_xFormatter(_rxNumberF)
167 ,m_xFactory(_rM)
168 ,m_pFormatter(NULL)
169 ,m_rInputStream( _rInputStream )
170 ,m_pTypeInfo()
171 ,m_pColumnList(NULL)
172 ,m_pInfoMap(NULL)
173 ,m_nColumnPos(0)
174 ,m_nRows(1)
175 ,m_nRowCount(0)
176 ,m_nDefToken( gsl_getSystemTextEncoding() )
177 ,m_bError(sal_False)
178 ,m_bInTbl(sal_False)
179 ,m_bHead(TRUE)
180 ,m_bDontAskAgain(sal_False)
181 ,m_bIsAutoIncrement(sal_False)
182 ,m_bFoundTable(sal_False)
183 ,m_bCheckOnly(sal_False)
184 ,m_bAppendFirstLine(false)
186 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::ODatabaseExport" );
187 DBG_CTOR(ODatabaseExport,NULL);
190 SvtSysLocale aSysLocale;
191 m_aLocale = aSysLocale.GetLocaleData().getLocale();
193 catch(Exception&)
197 Reference<XTablesSupplier> xTablesSup(m_xConnection,UNO_QUERY);
198 if(xTablesSup.is())
199 m_xTables = xTablesSup->getTables();
201 Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
202 Reference<XResultSet> xSet = xMeta.is() ? xMeta->getTypeInfo() : Reference<XResultSet>();
203 if(xSet.is())
205 ::connectivity::ORowSetValue aValue;
206 ::std::vector<sal_Int32> aTypes;
207 ::std::vector<sal_Bool> aNullable;
208 Reference<XResultSetMetaData> xResultSetMetaData = Reference<XResultSetMetaDataSupplier>(xSet,UNO_QUERY_THROW)->getMetaData();
209 Reference<XRow> xRow(xSet,UNO_QUERY_THROW);
210 while(xSet->next())
212 if ( aTypes.empty() )
214 sal_Int32 nCount = xResultSetMetaData->getColumnCount();
215 if ( nCount < 1 )
216 nCount = 18;
217 aTypes.reserve(nCount+1);
218 aNullable.reserve(nCount+1);
219 aTypes.push_back(-1);
220 aNullable.push_back(sal_False);
221 for (sal_Int32 j = 1; j <= nCount ; ++j)
223 aNullable.push_back(xResultSetMetaData->isNullable(j) != ColumnValue::NO_NULLS );
224 aTypes.push_back(xResultSetMetaData->getColumnType(j));
228 sal_Int32 nPos = 1;
229 OSL_ENSURE((nPos) < static_cast<sal_Int32>(aTypes.size()),"aTypes: Illegal index for vector");
230 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
231 ::rtl::OUString sTypeName = aValue;
232 ++nPos;
233 OSL_ENSURE((nPos) < static_cast<sal_Int32>(aTypes.size()),"aTypes: Illegal index for vector");
234 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
235 sal_Int32 nType = aValue;
236 ++nPos;
238 if( nType == DataType::VARCHAR )
240 m_pTypeInfo = TOTypeInfoSP(new OTypeInfo());
242 m_pTypeInfo->aTypeName = sTypeName;
243 m_pTypeInfo->nType = nType;
245 OSL_ENSURE((nPos) < static_cast<sal_Int32>(aTypes.size()),"aTypes: Illegal index for vector");
246 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
247 m_pTypeInfo->nPrecision = aValue;
248 ++nPos;
249 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
250 m_pTypeInfo->aLiteralPrefix = aValue;
251 ++nPos;
252 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
253 m_pTypeInfo->aLiteralSuffix = aValue;
254 ++nPos;
255 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
256 m_pTypeInfo->aCreateParams = aValue;
257 ++nPos;
258 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
259 m_pTypeInfo->bNullable = (sal_Int32)aValue == ColumnValue::NULLABLE;
260 ++nPos;
261 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
262 m_pTypeInfo->bCaseSensitive = (sal_Bool)aValue;
263 ++nPos;
264 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
265 m_pTypeInfo->nSearchType = aValue;
266 ++nPos;
267 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
268 m_pTypeInfo->bUnsigned = (sal_Bool)aValue;
269 ++nPos;
270 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
271 m_pTypeInfo->bCurrency = (sal_Bool)aValue;
272 ++nPos;
273 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
274 m_pTypeInfo->bAutoIncrement = (sal_Bool)aValue;
275 ++nPos;
276 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
277 m_pTypeInfo->aLocalTypeName = aValue;
278 ++nPos;
279 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
280 m_pTypeInfo->nMinimumScale = aValue;
281 ++nPos;
282 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
283 m_pTypeInfo->nMaximumScale = aValue;
285 // check if values are less than zero like it happens in a oracle jdbc driver
286 if( m_pTypeInfo->nPrecision < 0)
287 m_pTypeInfo->nPrecision = 0;
288 if( m_pTypeInfo->nMinimumScale < 0)
289 m_pTypeInfo->nMinimumScale = 0;
290 if( m_pTypeInfo->nMaximumScale < 0)
291 m_pTypeInfo->nMaximumScale = 0;
292 break;
295 } // if(xSet.is())
296 if ( !m_pTypeInfo )
297 m_pTypeInfo = TOTypeInfoSP(new OTypeInfo());
298 SetColumnTypes(pList,_pInfoMap);
300 //---------------------------------------------------------------------------
301 ODatabaseExport::~ODatabaseExport()
303 DBG_DTOR(ODatabaseExport,NULL);
304 m_pFormatter = NULL;
305 ODatabaseExport::TColumns::iterator aIter = m_aDestColumns.begin();
306 ODatabaseExport::TColumns::iterator aEnd = m_aDestColumns.end();
308 for(;aIter != aEnd;++aIter)
309 delete aIter->second;
310 m_vDestVector.clear();
311 m_aDestColumns.clear();
313 // -----------------------------------------------------------------------------
314 void ODatabaseExport::insertValueIntoColumn()
316 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::insertValueIntoColumn" );
317 DBG_CHKTHIS(ODatabaseExport,NULL);
318 if(m_nColumnPos < sal_Int32(m_vDestVector.size()))
320 OFieldDescription* pField = m_vDestVector[m_nColumnPos]->second;
321 if(pField)
323 sal_Int32 nNewPos = m_bIsAutoIncrement ? m_nColumnPos+1 : m_nColumnPos;
324 OSL_ENSURE((nNewPos) < static_cast<sal_Int32>(m_vColumns.size()),"m_vColumns: Illegal index for vector");
326 if ( (nNewPos) < static_cast<sal_Int32>(m_vColumns.size() ) )
328 sal_Int32 nPos = m_vColumns[nNewPos].first;
329 if ( nPos != COLUMN_POSITION_NOT_FOUND )
331 // if(m_nDefToken != LANGUAGE_DONTKNOW) // falls Sprache anders als Systemsprache
332 // m_pNF->ChangeIntl((LanguageType)m_nDefToken);
334 if ( !m_sTextToken.Len() && pField->IsNullable() )
335 m_pUpdateHelper->updateNull(nPos,pField->GetType());
336 else
338 sal_Int32 nNumberFormat = 0;
339 double fOutNumber = 0.0;
340 OSL_ENSURE((nNewPos) < static_cast<sal_Int32>(m_vColumnTypes.size()),"Illegal index for vector");
341 if (m_vColumnTypes[nNewPos] != DataType::VARCHAR && m_vColumnTypes[nNewPos] != DataType::CHAR && m_vColumnTypes[nNewPos] != DataType::LONGVARCHAR )
343 RTL_LOGFILE_CONTEXT_TRACE( aLogger, "ODatabaseExport::insertValueIntoColumn != DataType::VARCHAR" );
344 ensureFormatter();
345 bool bNumberFormatError = false;
346 if ( m_pFormatter && m_sNumToken.Len() )
348 LanguageType eNumLang = LANGUAGE_NONE;
349 sal_uInt32 nNumberFormat2( nNumberFormat );
350 fOutNumber = SfxHTMLParser::GetTableDataOptionsValNum(nNumberFormat2,eNumLang,m_sTextToken,m_sNumToken,*m_pFormatter);
351 if ( eNumLang != LANGUAGE_NONE )
353 nNumberFormat2 = m_pFormatter->GetFormatForLanguageIfBuiltIn( nNumberFormat2, eNumLang );
354 m_pFormatter->IsNumberFormat( m_sTextToken, nNumberFormat2, fOutNumber );
356 nNumberFormat = static_cast<sal_Int32>(nNumberFormat2);
358 else
360 Reference< XNumberFormatsSupplier > xSupplier = m_xFormatter->getNumberFormatsSupplier();
361 Reference<XNumberFormatTypes> xNumType(xSupplier->getNumberFormats(),UNO_QUERY);
362 sal_Int16 nFormats[] = {
363 NumberFormat::DATETIME
364 ,NumberFormat::DATE
365 ,NumberFormat::TIME
366 ,NumberFormat::CURRENCY
367 ,NumberFormat::NUMBER
368 ,NumberFormat::LOGICAL
370 for (size_t i = 0; i < sizeof(nFormats)/sizeof(nFormats[0]); ++i)
374 nNumberFormat = m_xFormatter->detectNumberFormat(xNumType->getStandardFormat(nFormats[i],m_aLocale),m_sTextToken);
375 break;
377 catch(Exception&)
383 fOutNumber = m_xFormatter->convertStringToNumber(nNumberFormat,m_sTextToken);
385 catch(Exception&)
387 bNumberFormatError = true;
388 m_pUpdateHelper->updateString(nPos,m_sTextToken);
391 if ( !bNumberFormatError )
395 Reference< XNumberFormatsSupplier > xSupplier = m_xFormatter->getNumberFormatsSupplier();
396 Reference< XNumberFormats > xFormats = xSupplier->getNumberFormats();
397 Reference<XPropertySet> xProp = xFormats->getByKey(nNumberFormat);
398 sal_Int16 nType = 0;
399 xProp->getPropertyValue(PROPERTY_TYPE) >>= nType;
400 switch(nType)
402 case NumberFormat::DATE:
403 m_pUpdateHelper->updateDate(nPos,::dbtools::DBTypeConversion::toDate(fOutNumber,m_aNullDate));
404 break;
405 case NumberFormat::DATETIME:
406 m_pUpdateHelper->updateTimestamp(nPos,::dbtools::DBTypeConversion::toDateTime(fOutNumber,m_aNullDate));
407 break;
408 case NumberFormat::TIME:
409 m_pUpdateHelper->updateTime(nPos,::dbtools::DBTypeConversion::toTime(fOutNumber));
410 break;
411 default:
412 m_pUpdateHelper->updateDouble(nPos,fOutNumber);
415 catch(Exception&)
417 m_pUpdateHelper->updateString(nPos,m_sTextToken);
422 else
423 m_pUpdateHelper->updateString(nPos,m_sTextToken);
427 eraseTokens();
431 // -----------------------------------------------------------------------------
432 sal_Int16 ODatabaseExport::CheckString(const String& aCheckToken, sal_Int16 _nOldNumberFormat)
434 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::CheckString" );
435 DBG_CHKTHIS(ODatabaseExport,NULL);
436 double fOutNumber = 0.0;
437 sal_Int16 nNumberFormat = 0;
441 Reference< XNumberFormatsSupplier > xSupplier = m_xFormatter->getNumberFormatsSupplier();
442 Reference< XNumberFormats > xFormats = xSupplier->getNumberFormats();
444 ensureFormatter();
445 if ( m_pFormatter && m_sNumToken.Len() )
447 LanguageType eNumLang;
448 sal_uInt32 nFormatKey(0);
449 fOutNumber = SfxHTMLParser::GetTableDataOptionsValNum(nFormatKey,eNumLang,m_sTextToken,m_sNumToken,*m_pFormatter);
450 if ( eNumLang != LANGUAGE_NONE )
452 nFormatKey = m_pFormatter->GetFormatForLanguageIfBuiltIn( nFormatKey, eNumLang );
453 m_pFormatter->IsNumberFormat( m_sTextToken, nFormatKey, fOutNumber );
455 Reference<XPropertySet> xProp = xFormats->getByKey(nFormatKey);
456 xProp->getPropertyValue(PROPERTY_TYPE) >>= nNumberFormat;
458 else
460 Reference<XNumberFormatTypes> xNumType(xFormats,UNO_QUERY);
461 sal_Int32 nFormatKey = m_xFormatter->detectNumberFormat(xNumType->getStandardFormat(NumberFormat::ALL,m_aLocale),aCheckToken);
462 fOutNumber = m_xFormatter->convertStringToNumber(nFormatKey,aCheckToken);
464 Reference<XPropertySet> xProp = xFormats->getByKey(nFormatKey);
465 sal_Int16 nType = 0;
466 xProp->getPropertyValue(PROPERTY_TYPE) >>= nType;
468 switch(nType)
470 case NumberFormat::ALL:
471 nNumberFormat = NumberFormat::ALL;
472 break;
473 case NumberFormat::DEFINED:
474 nNumberFormat = NumberFormat::TEXT;
475 break;
476 case NumberFormat::DATE:
477 switch(_nOldNumberFormat)
479 case NumberFormat::DATETIME:
480 case NumberFormat::TEXT:
481 case NumberFormat::DATE:
482 break;
483 case NumberFormat::ALL:
484 nNumberFormat = NumberFormat::DATE;
485 break;
486 default:
487 nNumberFormat = NumberFormat::TEXT;
490 break;
491 case NumberFormat::TIME:
492 switch(_nOldNumberFormat)
494 case NumberFormat::DATETIME:
495 case NumberFormat::TEXT:
496 case NumberFormat::TIME:
497 break;
498 case NumberFormat::ALL:
499 nNumberFormat = NumberFormat::TIME;
500 break;
501 default:
502 nNumberFormat = NumberFormat::TEXT;
503 break;
505 break;
506 case NumberFormat::CURRENCY:
507 switch(_nOldNumberFormat)
509 case NumberFormat::NUMBER:
510 nNumberFormat = NumberFormat::CURRENCY;
511 break;
512 case NumberFormat::CURRENCY:
513 break;
514 case NumberFormat::ALL:
515 nNumberFormat = NumberFormat::CURRENCY;
516 break;
517 default:
518 nNumberFormat = NumberFormat::TEXT;
519 break;
521 break;
522 case NumberFormat::NUMBER:
523 case NumberFormat::SCIENTIFIC:
524 case NumberFormat::FRACTION:
525 case NumberFormat::PERCENT:
526 switch(_nOldNumberFormat)
528 case NumberFormat::NUMBER:
529 break;
530 case NumberFormat::CURRENCY:
531 nNumberFormat = NumberFormat::CURRENCY;
532 break;
533 case NumberFormat::ALL:
534 nNumberFormat = nType;
535 break;
536 default:
537 nNumberFormat = NumberFormat::TEXT;
538 break;
540 break;
541 case NumberFormat::TEXT:
542 case NumberFormat::UNDEFINED:
543 case NumberFormat::LOGICAL:
544 nNumberFormat = NumberFormat::TEXT; // Text "uberschreibt alles
545 break;
546 case NumberFormat::DATETIME:
547 switch(_nOldNumberFormat)
549 case NumberFormat::DATETIME:
550 case NumberFormat::TEXT:
551 case NumberFormat::TIME:
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_ENSURE(0,"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.getLength() != 0 );
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 = TRUE;
788 else
789 m_bError = TRUE;
790 } // if(!m_bDontAskAgain)
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 ::std::compose1(::std::bind2nd(::std::equal_to<sal_Int32>(),i+1),::std::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->getLength() )
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 // -----------------------------------------------------------------------------