xref: /trunk/main/dbaccess/source/ui/misc/DExport.cxx (revision 3ce6c2dd3860afd353ceca3849779821e9b9f6f5)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_dbui.hxx"
26 
27 #include "DExport.hxx"
28 #include "moduledbu.hxx"
29 
30 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
31 #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
32 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
33 #include <com/sun/star/sdbcx/XAppend.hpp>
34 #include <com/sun/star/sdbcx/KeyType.hpp>
35 #include <com/sun/star/sdbc/DataType.hpp>
36 #include <com/sun/star/sdbc/ColumnValue.hpp>
37 #include <com/sun/star/sdb/CommandType.hpp>
38 #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
39 #include <com/sun/star/sdbc/XRow.hpp>
40 #include <com/sun/star/util/NumberFormat.hpp>
41 #include <com/sun/star/util/XNumberFormatTypes.hpp>
42 #include "dbustrings.hrc"
43 #include "dbu_misc.hrc"
44 #include <connectivity/dbconversion.hxx>
45 #include <sfx2/sfxhtml.hxx>
46 #include <svl/numuno.hxx>
47 #include <connectivity/dbtools.hxx>
48 #include <comphelper/extract.hxx>
49 #include "TypeInfo.hxx"
50 #include "FieldDescriptions.hxx"
51 #include "UITools.hxx"
52 #include <unotools/configmgr.hxx>
53 #include <memory>
54 #include <tools/debug.hxx>
55 #include <tools/diagnose_ex.h>
56 #include <i18npool/mslangid.hxx>
57 #include <com/sun/star/awt/FontDescriptor.hpp>
58 #include "WCopyTable.hxx"
59 #include "WExtendPages.hxx"
60 #include "WCPage.hxx"
61 #include <unotools/syslocale.hxx>
62 #include <svl/zforlist.hxx>
63 #include <connectivity/dbexception.hxx>
64 #include <connectivity/FValue.hxx>
65 #include <com/sun/star/sdbc/SQLWarning.hpp>
66 #include <com/sun/star/sdb/SQLContext.hpp>
67 #include <com/sun/star/sdb/application/CopyTableOperation.hpp>
68 #include "sqlmessage.hxx"
69 #include "UpdateHelperImpl.hxx"
70 #include <vcl/msgbox.hxx>
71 #include <cppuhelper/exc_hlp.hxx>
72 #include <rtl/logfile.hxx>
73 #include <functional>
74 
75 using namespace dbaui;
76 using namespace utl;
77 using namespace ::com::sun::star::uno;
78 using namespace ::com::sun::star::beans;
79 using namespace ::com::sun::star::container;
80 using namespace ::com::sun::star::util;
81 using namespace ::com::sun::star::sdbc;
82 using namespace ::com::sun::star::sdbcx;
83 using namespace ::com::sun::star::sdb;
84 using namespace ::com::sun::star::lang;
85 using namespace ::com::sun::star::awt;
86 
87 namespace CopyTableOperation = ::com::sun::star::sdb::application::CopyTableOperation;
88 
89 // ==========================================================================
90 // ODatabaseExport
91 // ==========================================================================
DBG_NAME(ODatabaseExport)92 DBG_NAME(ODatabaseExport)
93 ODatabaseExport::ODatabaseExport(sal_Int32 nRows,
94                                  const TPositions &_rColumnPositions,
95                                  const Reference< XNumberFormatter >& _rxNumberF,
96                                  const Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rM,
97                                  const TColumnVector* pList,
98                                  const OTypeInfoMap* _pInfoMap,
99                                  sal_Bool _bAutoIncrementEnabled,
100                                  SvStream& _rInputStream)
101     :m_vColumns(_rColumnPositions)
102     ,m_aDestColumns(sal_True)
103     ,m_xFormatter(_rxNumberF)
104     ,m_xFactory(_rM)
105     ,m_pFormatter(NULL)
106     ,m_rInputStream( _rInputStream )
107     ,m_pTypeInfo()
108     ,m_pColumnList(pList)
109     ,m_pInfoMap(_pInfoMap)
110     ,m_nColumnPos(0)
111     ,m_nRows(1)
112     ,m_nRowCount(0)
113     ,m_nDefToken( gsl_getSystemTextEncoding() )
114     ,m_bError(sal_False)
115     ,m_bInTbl(sal_False)
116     ,m_bHead(sal_True)
117     ,m_bDontAskAgain(sal_False)
118     ,m_bIsAutoIncrement(_bAutoIncrementEnabled)
119     ,m_bFoundTable(sal_False)
120     ,m_bCheckOnly(sal_False)
121     ,m_bAppendFirstLine(false)
122 {
123     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::ODatabaseExport" );
124     DBG_CTOR(ODatabaseExport,NULL);
125 
126     m_nRows += nRows;
127     sal_Int32 nCount = 0;
128     for(sal_Int32 j=0;j < (sal_Int32)m_vColumns.size();++j)
129         if ( m_vColumns[j].first != COLUMN_POSITION_NOT_FOUND )
130             ++nCount;
131 
132     m_vColumnSize.resize(nCount);
133     m_vNumberFormat.resize(nCount);
134     for(sal_Int32 i=0;i<nCount;++i)
135     {
136         m_vColumnSize[i] = 0;
137         m_vNumberFormat[i] = 0;
138     }
139 
140     try
141     {
142         SvtSysLocale aSysLocale;
143         m_aLocale = aSysLocale.GetLocaleData().getLocale();
144     }
145     catch(Exception&)
146     {
147     }
148 
149     SetColumnTypes(pList,_pInfoMap);
150 }
151 //---------------------------------------------------------------------------
ODatabaseExport(const SharedConnection & _rxConnection,const Reference<XNumberFormatter> & _rxNumberF,const Reference<::com::sun::star::lang::XMultiServiceFactory> & _rM,const TColumnVector * pList,const OTypeInfoMap * _pInfoMap,SvStream & _rInputStream)152 ODatabaseExport::ODatabaseExport(const SharedConnection& _rxConnection,
153                                  const Reference< XNumberFormatter >& _rxNumberF,
154                                  const Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rM,
155                                  const TColumnVector* pList,
156                                  const OTypeInfoMap* _pInfoMap,
157                                  SvStream& _rInputStream)
158     :m_aDestColumns(_rxConnection->getMetaData().is() && _rxConnection->getMetaData()->supportsMixedCaseQuotedIdentifiers() == sal_True)
159     ,m_xConnection(_rxConnection)
160     ,m_xFormatter(_rxNumberF)
161     ,m_xFactory(_rM)
162     ,m_pFormatter(NULL)
163     ,m_rInputStream( _rInputStream )
164     ,m_pTypeInfo()
165     ,m_pColumnList(NULL)
166     ,m_pInfoMap(NULL)
167     ,m_nColumnPos(0)
168     ,m_nRows(1)
169     ,m_nRowCount(0)
170     ,m_nDefToken( gsl_getSystemTextEncoding() )
171     ,m_bError(sal_False)
172     ,m_bInTbl(sal_False)
173     ,m_bHead(sal_True)
174     ,m_bDontAskAgain(sal_False)
175     ,m_bIsAutoIncrement(sal_False)
176     ,m_bFoundTable(sal_False)
177     ,m_bCheckOnly(sal_False)
178     ,m_bAppendFirstLine(false)
179 {
180     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::ODatabaseExport" );
181     DBG_CTOR(ODatabaseExport,NULL);
182     try
183     {
184         SvtSysLocale aSysLocale;
185         m_aLocale = aSysLocale.GetLocaleData().getLocale();
186     }
187     catch(Exception&)
188     {
189     }
190 
191     Reference<XTablesSupplier> xTablesSup(m_xConnection,UNO_QUERY);
192     if(xTablesSup.is())
193         m_xTables = xTablesSup->getTables();
194 
195     Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
196     Reference<XResultSet> xSet = xMeta.is() ? xMeta->getTypeInfo() : Reference<XResultSet>();
197     if(xSet.is())
198     {
199         ::connectivity::ORowSetValue aValue;
200         ::std::vector<sal_Int32> aTypes;
201         ::std::vector<sal_Bool> aNullable;
202         Reference<XResultSetMetaData> xResultSetMetaData = Reference<XResultSetMetaDataSupplier>(xSet,UNO_QUERY_THROW)->getMetaData();
203         Reference<XRow> xRow(xSet,UNO_QUERY_THROW);
204         while(xSet->next())
205         {
206             if ( aTypes.empty() )
207             {
208                 sal_Int32 nCount = xResultSetMetaData->getColumnCount();
209                 if ( nCount < 1 )
210                     nCount = 18;
211                 aTypes.reserve(nCount+1);
212                 aNullable.reserve(nCount+1);
213                 aTypes.push_back(-1);
214                 aNullable.push_back(sal_False);
215                 for (sal_Int32 j = 1; j <= nCount ; ++j)
216                 {
217                     aNullable.push_back(xResultSetMetaData->isNullable(j) != ColumnValue::NO_NULLS );
218                     aTypes.push_back(xResultSetMetaData->getColumnType(j));
219                 }
220             }
221 
222             sal_Int32 nPos = 1;
223             OSL_ENSURE((nPos) < static_cast<sal_Int32>(aTypes.size()),"aTypes: Illegal index for vector");
224             aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
225             ::rtl::OUString sTypeName = aValue;
226             ++nPos;
227             OSL_ENSURE((nPos) < static_cast<sal_Int32>(aTypes.size()),"aTypes: Illegal index for vector");
228             aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
229             sal_Int32 nType = aValue;
230             ++nPos;
231 
232             if( nType == DataType::VARCHAR )
233             {
234                 m_pTypeInfo                 = TOTypeInfoSP(new OTypeInfo());
235 
236                 m_pTypeInfo->aTypeName      = sTypeName;
237                 m_pTypeInfo->nType          = nType;
238 
239                 OSL_ENSURE((nPos) < static_cast<sal_Int32>(aTypes.size()),"aTypes: Illegal index for vector");
240                 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
241                 m_pTypeInfo->nPrecision     = aValue;
242                 ++nPos;
243                 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
244                 m_pTypeInfo->aLiteralPrefix = aValue;
245                 ++nPos;
246                 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
247                 m_pTypeInfo->aLiteralSuffix = aValue;
248                 ++nPos;
249                 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
250                 m_pTypeInfo->aCreateParams  = aValue;
251                 ++nPos;
252                 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
253                 m_pTypeInfo->bNullable      = (sal_Int32)aValue == ColumnValue::NULLABLE;
254                 ++nPos;
255                 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
256                 m_pTypeInfo->bCaseSensitive = (sal_Bool)aValue;
257                 ++nPos;
258                 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
259                 m_pTypeInfo->nSearchType        = aValue;
260                 ++nPos;
261                 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
262                 m_pTypeInfo->bUnsigned      = (sal_Bool)aValue;
263                 ++nPos;
264                 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
265                 m_pTypeInfo->bCurrency      = (sal_Bool)aValue;
266                 ++nPos;
267                 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
268                 m_pTypeInfo->bAutoIncrement = (sal_Bool)aValue;
269                 ++nPos;
270                 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
271                 m_pTypeInfo->aLocalTypeName = aValue;
272                 ++nPos;
273                 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
274                 m_pTypeInfo->nMinimumScale  = aValue;
275                 ++nPos;
276                 aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
277                 m_pTypeInfo->nMaximumScale  = aValue;
278 
279                 // check if values are less than zero like it happens in a oracle jdbc driver
280                 if( m_pTypeInfo->nPrecision < 0)
281                     m_pTypeInfo->nPrecision = 0;
282                 if( m_pTypeInfo->nMinimumScale < 0)
283                     m_pTypeInfo->nMinimumScale = 0;
284                 if( m_pTypeInfo->nMaximumScale < 0)
285                     m_pTypeInfo->nMaximumScale = 0;
286                 break;
287             }
288         }
289     } // if(xSet.is())
290     if ( !m_pTypeInfo )
291         m_pTypeInfo = TOTypeInfoSP(new OTypeInfo());
292     SetColumnTypes(pList,_pInfoMap);
293 }
294 //---------------------------------------------------------------------------
~ODatabaseExport()295 ODatabaseExport::~ODatabaseExport()
296 {
297     DBG_DTOR(ODatabaseExport,NULL);
298     m_pFormatter = NULL;
299     ODatabaseExport::TColumns::iterator aIter = m_aDestColumns.begin();
300     ODatabaseExport::TColumns::iterator aEnd  = m_aDestColumns.end();
301 
302     for(;aIter != aEnd;++aIter)
303         delete aIter->second;
304     m_vDestVector.clear();
305     m_aDestColumns.clear();
306 }
307 // -----------------------------------------------------------------------------
insertValueIntoColumn()308 void ODatabaseExport::insertValueIntoColumn()
309 {
310     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::insertValueIntoColumn" );
311     DBG_CHKTHIS(ODatabaseExport,NULL);
312     if(m_nColumnPos < sal_Int32(m_vDestVector.size()))
313     {
314         OFieldDescription* pField = m_vDestVector[m_nColumnPos]->second;
315         if(pField)
316         {
317             sal_Int32 nNewPos = m_bIsAutoIncrement ? m_nColumnPos+1 : m_nColumnPos;
318             OSL_ENSURE((nNewPos) < static_cast<sal_Int32>(m_vColumns.size()),"m_vColumns: Illegal index for vector");
319 
320             if ( (nNewPos) < static_cast<sal_Int32>(m_vColumns.size() ) )
321             {
322                 sal_Int32 nPos = m_vColumns[nNewPos].first;
323                 if ( nPos != COLUMN_POSITION_NOT_FOUND )
324                 {
325     //                  if(m_nDefToken != LANGUAGE_DONTKNOW) // falls Sprache anders als Systemsprache
326     //                      m_pNF->ChangeIntl((LanguageType)m_nDefToken);
327 
328                     if ( !m_sTextToken.Len() && pField->IsNullable() )
329                         m_pUpdateHelper->updateNull(nPos,pField->GetType());
330                     else
331                     {
332                         sal_Int32 nNumberFormat = 0;
333                         double fOutNumber = 0.0;
334                         OSL_ENSURE((nNewPos) < static_cast<sal_Int32>(m_vColumnTypes.size()),"Illegal index for vector");
335                         if (m_vColumnTypes[nNewPos] != DataType::VARCHAR && m_vColumnTypes[nNewPos] != DataType::CHAR && m_vColumnTypes[nNewPos] != DataType::LONGVARCHAR )
336                         {
337                             RTL_LOGFILE_CONTEXT_TRACE( aLogger, "ODatabaseExport::insertValueIntoColumn != DataType::VARCHAR" );
338                             ensureFormatter();
339                             bool bNumberFormatError = false;
340                             if ( m_pFormatter && m_sNumToken.Len() )
341                             {
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 )
346                                 {
347                                     nNumberFormat2 = m_pFormatter->GetFormatForLanguageIfBuiltIn( nNumberFormat2, eNumLang );
348                                     m_pFormatter->IsNumberFormat( m_sTextToken, nNumberFormat2, fOutNumber );
349                                 }
350                                 nNumberFormat = static_cast<sal_Int32>(nNumberFormat2);
351                             }
352                             else
353                             {
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
363                                 };
364                                 for (size_t i = 0; i < sizeof(nFormats)/sizeof(nFormats[0]); ++i)
365                                 {
366                                     try
367                                     {
368                                         nNumberFormat = m_xFormatter->detectNumberFormat(xNumType->getStandardFormat(nFormats[i],m_aLocale),m_sTextToken);
369                                         break;
370                                     }
371                                     catch(Exception&)
372                                     {
373                                     }
374                                 }
375                                 try
376                                 {
377                                     fOutNumber = m_xFormatter->convertStringToNumber(nNumberFormat,m_sTextToken);
378                                 }
379                                 catch(Exception&)
380                                 {
381                                     bNumberFormatError = true;
382                                     m_pUpdateHelper->updateString(nPos,m_sTextToken);
383                                 }
384                             }
385                             if ( !bNumberFormatError )
386                             {
387                                 try
388                                 {
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)
395                                     {
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);
407                                     }
408                                 }
409                                 catch(Exception&)
410                                 {
411                                     m_pUpdateHelper->updateString(nPos,m_sTextToken);
412                                 }
413                             }
414 
415                         }
416                         else
417                             m_pUpdateHelper->updateString(nPos,m_sTextToken);
418                     }
419                 }
420             }
421             eraseTokens();
422         }
423     }
424 }
425 // -----------------------------------------------------------------------------
CheckString(const String & aCheckToken,sal_Int16 _nOldNumberFormat)426 sal_Int16 ODatabaseExport::CheckString(const String& aCheckToken, sal_Int16 _nOldNumberFormat)
427 {
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;
432 
433     try
434     {
435         Reference< XNumberFormatsSupplier > xSupplier = m_xFormatter->getNumberFormatsSupplier();
436         Reference< XNumberFormats >         xFormats = xSupplier->getNumberFormats();
437 
438         ensureFormatter();
439         if ( m_pFormatter && m_sNumToken.Len() )
440         {
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 )
445             {
446                 nFormatKey = m_pFormatter->GetFormatForLanguageIfBuiltIn( nFormatKey, eNumLang );
447                 if ( !m_pFormatter->IsNumberFormat( m_sTextToken, nFormatKey, fOutNumber ) )
448                     return NumberFormat::TEXT;
449             }
450             Reference<XPropertySet> xProp = xFormats->getByKey(nFormatKey);
451             xProp->getPropertyValue(PROPERTY_TYPE) >>= nNumberFormat;
452         }
453         else
454         {
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);
458 
459             Reference<XPropertySet> xProp = xFormats->getByKey(nFormatKey);
460             sal_Int16 nType = 0;
461             xProp->getPropertyValue(PROPERTY_TYPE) >>= nType;
462 
463             switch(nType)
464             {
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)
473                     {
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;
484 
485                     }
486                     break;
487                 case NumberFormat::TIME:
488                     switch(_nOldNumberFormat)
489                     {
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;
501                     }
502                     break;
503                 case NumberFormat::CURRENCY:
504                     switch(_nOldNumberFormat)
505                     {
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;
518                     }
519                     break;
520                 case NumberFormat::NUMBER:
521                 case NumberFormat::SCIENTIFIC:
522                 case NumberFormat::FRACTION:
523                 case NumberFormat::PERCENT:
524                     switch(_nOldNumberFormat)
525                     {
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;
538                     }
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)
547                     {
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;
559                     }
560                     break;
561                 default:
562                     OSL_ENSURE(0,"ODatabaseExport: Unbekanntes Format");
563             }
564         }
565     }
566     catch(Exception&)
567     {
568         nNumberFormat = NumberFormat::TEXT; // Text "uberschreibt alles
569     }
570 
571     return nNumberFormat;
572 }
573 // -----------------------------------------------------------------------------
SetColumnTypes(const TColumnVector * _pList,const OTypeInfoMap * _pInfoMap)574 void ODatabaseExport::SetColumnTypes(const TColumnVector* _pList,const OTypeInfoMap* _pInfoMap)
575 {
576     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::SetColumnTypes" );
577     DBG_CHKTHIS(ODatabaseExport,NULL);
578     if(_pList && _pInfoMap)
579     {
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)
586         {
587             sal_Int32 nDataType;
588             sal_Int32 nLength(0),nScale(0);
589             sal_Int16 nType = m_vNumberFormat[i] & ~NumberFormat::DEFINED;
590 
591             switch ( nType )
592             {
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;
627             }
628             OTypeInfoMap::const_iterator aFind = _pInfoMap->find(nDataType);
629             if(aFind != _pInfoMap->end())
630             {
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));
634 
635                 sal_Int32 nFormatKey = ::dbtools::getDefaultNumberFormat( nDataType,
636                                     (*aIter)->second->GetScale(),
637                                     (*aIter)->second->IsCurrency(),
638                                     Reference< XNumberFormatTypes>(xFormats,UNO_QUERY),
639                                     m_aLocale);
640 
641                 (*aIter)->second->SetFormatKey(nFormatKey);
642             }
643         }
644     }
645 }
646 // -----------------------------------------------------------------------------
CreateDefaultColumn(const::rtl::OUString & _rColumnName)647 void ODatabaseExport::CreateDefaultColumn(const ::rtl::OUString& _rColumnName)
648 {
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());
656 
657     if(nMaxNameLen && aAlias.getLength() > nMaxNameLen)
658         aAlias = aAlias.copy(0, ::std::min<sal_Int32>( nMaxNameLen-1, aAlias.getLength() ) );
659 
660     ::rtl::OUString sName(aAlias);
661     if(m_aDestColumns.find(sName) != m_aDestColumns.end())
662     {
663         sal_Int32 nPos = 0;
664         sal_Int32 nCount = 2;
665         while(m_aDestColumns.find(sName) != m_aDestColumns.end())
666         {
667             sName = aAlias;
668             sName += ::rtl::OUString::valueOf(++nPos);
669             if(nMaxNameLen && sName.getLength() > nMaxNameLen)
670             {
671                 aAlias = aAlias.copy(0,::std::min<sal_Int32>( nMaxNameLen-nCount, aAlias.getLength() ));
672                 sName = aAlias;
673                 sName += ::rtl::OUString::valueOf(nPos);
674                 ++nCount;
675             }
676         }
677     }
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);
689 
690     TColumns::iterator aFind = m_aDestColumns.find( aAlias );
691     if ( aFind != m_aDestColumns.end() )
692     {
693         delete aFind->second;
694         m_aDestColumns.erase(aFind);
695     }
696 
697     m_vDestVector.push_back(m_aDestColumns.insert(TColumns::value_type(aAlias,pField)).first);
698 }
699 // -----------------------------------------------------------------------------
createRowSet()700 sal_Bool ODatabaseExport::createRowSet()
701 {
702     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::createRowSet" );
703     DBG_CHKTHIS(ODatabaseExport,NULL);
704     m_pUpdateHelper.reset(new OParameterUpdateHelper(createPreparedStatement(m_xConnection->getMetaData(),m_xTable,m_vColumns)));
705 
706     return m_pUpdateHelper.get() != NULL;
707 }
708 // -----------------------------------------------------------------------------
executeWizard(const::rtl::OUString & _rTableName,const Any & _aTextColor,const FontDescriptor & _rFont)709 sal_Bool ODatabaseExport::executeWizard(const ::rtl::OUString& _rTableName,const Any& _aTextColor,const FontDescriptor& _rFont)
710 {
711     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::executeWizard" );
712     DBG_CHKTHIS(ODatabaseExport,NULL);
713 
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
727     );
728 
729     sal_Bool bError = sal_False;
730     try
731     {
732         if (aWizard.Execute())
733         {
734             switch(aWizard.getOperation())
735             {
736                 case CopyTableOperation::CopyDefinitionAndData:
737                 case CopyTableOperation::AppendData:
738                     {
739                         m_xTable = aWizard.createTable();
740                         bError = !m_xTable.is();
741                         if(m_xTable.is())
742                         {
743                             m_xTable->setPropertyValue(PROPERTY_FONT,makeAny(_rFont));
744                             if(_aTextColor.hasValue())
745                                 m_xTable->setPropertyValue(PROPERTY_TEXTCOLOR,_aTextColor);
746                         }
747                         m_bIsAutoIncrement  = aWizard.shouldCreatePrimaryKey();
748                         m_vColumns          = aWizard.GetColumnPositions();
749                         m_vColumnTypes      = aWizard.GetColumnTypes();
750                         m_bAppendFirstLine  = !aWizard.UseHeaderLine();
751                     }
752                     break;
753                 default:
754                     bError = sal_True; // there is no error but I have nothing more to do
755             }
756         }
757         else
758             bError = sal_True;
759 
760         if(!bError)
761             bError = !createRowSet();
762     }
763     catch( const SQLException&)
764     {
765         ::dbaui::showError( ::dbtools::SQLExceptionInfo( ::cppu::getCaughtException() ), &aWizard, m_xFactory );
766         bError = sal_True;
767     }
768     catch( const Exception& )
769     {
770         DBG_UNHANDLED_EXCEPTION();
771     }
772 
773     return bError;
774 }
775 //---------------------------------------------------------------------------------
showErrorDialog(const::com::sun::star::sdbc::SQLException & e)776 void ODatabaseExport::showErrorDialog(const ::com::sun::star::sdbc::SQLException& e)
777 {
778     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::showErrorDialog" );
779     if(!m_bDontAskAgain)
780     {
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 );
785 
786         if (aBox.Execute() == RET_YES)
787             m_bDontAskAgain = sal_True;
788         else
789             m_bError = sal_True;
790     } // if(!m_bDontAskAgain)
791 }
792 // -----------------------------------------------------------------------------
adjustFormat()793 void ODatabaseExport::adjustFormat()
794 {
795     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::adjustFormat" );
796     if ( m_sTextToken.Len() )
797     {
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()) )
801         {
802             sal_Int32 nColPos = m_vColumns[nNewPos].first;
803             if( nColPos != sal::static_int_cast< long >(CONTAINER_ENTRY_NOTFOUND))
804             {
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());
810             }
811         }
812         eraseTokens();
813     }
814 }
815 // -----------------------------------------------------------------------------
eraseTokens()816 void ODatabaseExport::eraseTokens()
817 {
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();
822 }
823 // -----------------------------------------------------------------------------
ensureFormatter()824 void ODatabaseExport::ensureFormatter()
825 {
826     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::ensureFormatter" );
827     if ( !m_pFormatter )
828     {
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;
835     }
836 }
837 // -----------------------------------------------------------------------------
createPreparedStatement(const Reference<XDatabaseMetaData> & _xMetaData,const Reference<XPropertySet> & _xDestTable,const TPositions & _rvColumns)838 Reference< XPreparedStatement > ODatabaseExport::createPreparedStatement( const Reference<XDatabaseMetaData>& _xMetaData
839                                                        ,const Reference<XPropertySet>& _xDestTable
840                                                        ,const TPositions& _rvColumns)
841 {
842     RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::createPreparedStatement" );
843     ::rtl::OUString aSql(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("INSERT INTO ")));
844     ::rtl::OUString sComposedTableName = ::dbtools::composeTableName( _xMetaData, _xDestTable, ::dbtools::eInDataManipulation, false, false, true );
845 
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(","));
852 
853     ::rtl::OUString aQuote;
854     if ( _xMetaData.is() )
855         aQuote = _xMetaData->getIdentifierQuoteString();
856 
857     Reference<XColumnsSupplier> xDestColsSup(_xDestTable,UNO_QUERY_THROW);
858 
859     // create sql string and set column types
860     Sequence< ::rtl::OUString> aDestColumnNames = xDestColsSup->getColumns()->getElementNames();
861     if ( aDestColumnNames.getLength() == 0 )
862     {
863         return Reference< XPreparedStatement > ();
864     }
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)
870     {
871         ODatabaseExport::TPositions::const_iterator aFind = ::std::find_if(_rvColumns.begin(),_rvColumns.end(),
872             ::std::compose1(::std::bind(::std::equal_to<sal_Int32>(), ::std::placeholders::_1,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) )
874         {
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));
877         }
878     }
879 
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)
884     {
885         if ( aInsertIter->getLength() )
886         {
887             aSql += *aInsertIter;
888             aSql += aComma;
889             aValues += aPara;
890         }
891     }
892 
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(")")));
895 
896     aSql += aValues;
897     // now create,fill and execute the prepared statement
898     return Reference< XPreparedStatement >(_xMetaData->getConnection()->prepareStatement(aSql));
899 }
900 // -----------------------------------------------------------------------------
901