Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include "DExport.hxx"
22 : #include "moduledbu.hxx"
23 :
24 : #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
25 : #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
26 : #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
27 : #include <com/sun/star/sdbcx/XAppend.hpp>
28 : #include <com/sun/star/sdbcx/KeyType.hpp>
29 : #include <com/sun/star/sdbc/DataType.hpp>
30 : #include <com/sun/star/sdbc/ColumnValue.hpp>
31 : #include <com/sun/star/sdb/CommandType.hpp>
32 : #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
33 : #include <com/sun/star/sdbc/XRow.hpp>
34 : #include <com/sun/star/util/NumberFormat.hpp>
35 : #include <com/sun/star/util/XNumberFormatTypes.hpp>
36 : #include "dbustrings.hrc"
37 : #include "dbu_misc.hrc"
38 : #include <connectivity/dbconversion.hxx>
39 : #include <sfx2/sfxhtml.hxx>
40 : #include <svl/numuno.hxx>
41 : #include <connectivity/dbtools.hxx>
42 : #include <comphelper/extract.hxx>
43 : #include "TypeInfo.hxx"
44 : #include "FieldDescriptions.hxx"
45 : #include "UITools.hxx"
46 : #include <unotools/configmgr.hxx>
47 : #include <memory>
48 : #include <o3tl/compat_functional.hxx>
49 : #include <tools/debug.hxx>
50 : #include <tools/diagnose_ex.h>
51 : #include <tools/contnr.hxx>
52 : #include <i18npool/mslangid.hxx>
53 : #include <com/sun/star/awt/FontDescriptor.hpp>
54 : #include "WCopyTable.hxx"
55 : #include "WExtendPages.hxx"
56 : #include "WCPage.hxx"
57 : #include <unotools/syslocale.hxx>
58 : #include <svl/zforlist.hxx>
59 : #include <connectivity/dbexception.hxx>
60 : #include <connectivity/FValue.hxx>
61 : #include <com/sun/star/sdbc/SQLWarning.hpp>
62 : #include <com/sun/star/sdb/SQLContext.hpp>
63 : #include <com/sun/star/sdb/application/CopyTableOperation.hpp>
64 : #include "sqlmessage.hxx"
65 : #include "UpdateHelperImpl.hxx"
66 : #include <vcl/msgbox.hxx>
67 : #include <cppuhelper/exc_hlp.hxx>
68 : #include <rtl/logfile.hxx>
69 :
70 : using namespace dbaui;
71 : using namespace utl;
72 : using namespace ::com::sun::star::uno;
73 : using namespace ::com::sun::star::beans;
74 : using namespace ::com::sun::star::container;
75 : using namespace ::com::sun::star::util;
76 : using namespace ::com::sun::star::sdbc;
77 : using namespace ::com::sun::star::sdbcx;
78 : using namespace ::com::sun::star::sdb;
79 : using namespace ::com::sun::star::lang;
80 : using namespace ::com::sun::star::awt;
81 :
82 : namespace CopyTableOperation = ::com::sun::star::sdb::application::CopyTableOperation;
83 :
84 : // ==========================================================================
85 : // ODatabaseExport
86 : // ==========================================================================
87 : DBG_NAME(ODatabaseExport)
88 0 : ODatabaseExport::ODatabaseExport(sal_Int32 nRows,
89 : const TPositions &_rColumnPositions,
90 : const Reference< XNumberFormatter >& _rxNumberF,
91 : const Reference< ::com::sun::star::uno::XComponentContext >& _rxContext,
92 : const TColumnVector* pList,
93 : const OTypeInfoMap* _pInfoMap,
94 : sal_Bool _bAutoIncrementEnabled,
95 : SvStream& _rInputStream)
96 : :m_vColumns(_rColumnPositions)
97 : ,m_aDestColumns(sal_True)
98 : ,m_xFormatter(_rxNumberF)
99 : ,m_xContext(_rxContext)
100 : ,m_pFormatter(NULL)
101 : ,m_rInputStream( _rInputStream )
102 : ,m_pTypeInfo()
103 : ,m_pColumnList(pList)
104 : ,m_pInfoMap(_pInfoMap)
105 : ,m_nColumnPos(0)
106 : ,m_nRows(1)
107 : ,m_nRowCount(0)
108 0 : ,m_nDefToken( osl_getThreadTextEncoding() )
109 : ,m_bError(sal_False)
110 : ,m_bInTbl(sal_False)
111 : ,m_bHead(sal_True)
112 : ,m_bDontAskAgain(sal_False)
113 : ,m_bIsAutoIncrement(_bAutoIncrementEnabled)
114 : ,m_bFoundTable(sal_False)
115 : ,m_bCheckOnly(sal_False)
116 0 : ,m_bAppendFirstLine(false)
117 : {
118 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::ODatabaseExport" );
119 : DBG_CTOR(ODatabaseExport,NULL);
120 :
121 0 : m_nRows += nRows;
122 0 : sal_Int32 nCount = 0;
123 0 : for(sal_Int32 j=0;j < (sal_Int32)m_vColumns.size();++j)
124 0 : if ( m_vColumns[j].first != COLUMN_POSITION_NOT_FOUND )
125 0 : ++nCount;
126 :
127 0 : m_vColumnSize.resize(nCount);
128 0 : m_vNumberFormat.resize(nCount);
129 0 : for(sal_Int32 i=0;i<nCount;++i)
130 : {
131 0 : m_vColumnSize[i] = 0;
132 0 : m_vNumberFormat[i] = 0;
133 : }
134 :
135 : try
136 : {
137 0 : SvtSysLocale aSysLocale;
138 0 : m_aLocale = aSysLocale.GetLanguageTag().getLocale();
139 : }
140 0 : catch(Exception&)
141 : {
142 : }
143 :
144 0 : SetColumnTypes(pList,_pInfoMap);
145 0 : }
146 : //---------------------------------------------------------------------------
147 0 : ODatabaseExport::ODatabaseExport(const SharedConnection& _rxConnection,
148 : const Reference< XNumberFormatter >& _rxNumberF,
149 : const Reference< ::com::sun::star::uno::XComponentContext >& _rxContext,
150 : const TColumnVector* pList,
151 : const OTypeInfoMap* _pInfoMap,
152 : SvStream& _rInputStream)
153 0 : :m_aDestColumns(_rxConnection->getMetaData().is() && _rxConnection->getMetaData()->supportsMixedCaseQuotedIdentifiers() == sal_True)
154 : ,m_xConnection(_rxConnection)
155 : ,m_xFormatter(_rxNumberF)
156 : ,m_xContext(_rxContext)
157 : ,m_pFormatter(NULL)
158 : ,m_rInputStream( _rInputStream )
159 : ,m_pTypeInfo()
160 : ,m_pColumnList(NULL)
161 : ,m_pInfoMap(NULL)
162 : ,m_nColumnPos(0)
163 : ,m_nRows(1)
164 : ,m_nRowCount(0)
165 0 : ,m_nDefToken( osl_getThreadTextEncoding() )
166 : ,m_bError(sal_False)
167 : ,m_bInTbl(sal_False)
168 : ,m_bHead(sal_True)
169 : ,m_bDontAskAgain(sal_False)
170 : ,m_bIsAutoIncrement(sal_False)
171 : ,m_bFoundTable(sal_False)
172 : ,m_bCheckOnly(sal_False)
173 0 : ,m_bAppendFirstLine(false)
174 : {
175 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::ODatabaseExport" );
176 : DBG_CTOR(ODatabaseExport,NULL);
177 : try
178 : {
179 0 : SvtSysLocale aSysLocale;
180 0 : m_aLocale = aSysLocale.GetLanguageTag().getLocale();
181 : }
182 0 : catch(Exception&)
183 : {
184 : }
185 :
186 0 : Reference<XTablesSupplier> xTablesSup(m_xConnection,UNO_QUERY);
187 0 : if(xTablesSup.is())
188 0 : m_xTables = xTablesSup->getTables();
189 :
190 0 : Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
191 0 : Reference<XResultSet> xSet = xMeta.is() ? xMeta->getTypeInfo() : Reference<XResultSet>();
192 0 : if(xSet.is())
193 : {
194 0 : ::connectivity::ORowSetValue aValue;
195 0 : ::std::vector<sal_Int32> aTypes;
196 0 : ::std::vector<sal_Bool> aNullable;
197 0 : Reference<XResultSetMetaData> xResultSetMetaData = Reference<XResultSetMetaDataSupplier>(xSet,UNO_QUERY_THROW)->getMetaData();
198 0 : Reference<XRow> xRow(xSet,UNO_QUERY_THROW);
199 0 : while(xSet->next())
200 : {
201 0 : if ( aTypes.empty() )
202 : {
203 0 : sal_Int32 nCount = xResultSetMetaData->getColumnCount();
204 0 : if ( nCount < 1 )
205 0 : nCount = 18;
206 0 : aTypes.reserve(nCount+1);
207 0 : aNullable.reserve(nCount+1);
208 0 : aTypes.push_back(-1);
209 0 : aNullable.push_back(sal_False);
210 0 : for (sal_Int32 j = 1; j <= nCount ; ++j)
211 : {
212 0 : aNullable.push_back(xResultSetMetaData->isNullable(j) != ColumnValue::NO_NULLS );
213 0 : aTypes.push_back(xResultSetMetaData->getColumnType(j));
214 : }
215 : }
216 :
217 0 : sal_Int32 nPos = 1;
218 : OSL_ENSURE((nPos) < static_cast<sal_Int32>(aTypes.size()),"aTypes: Illegal index for vector");
219 0 : aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
220 0 : ::rtl::OUString sTypeName = aValue;
221 0 : ++nPos;
222 : OSL_ENSURE((nPos) < static_cast<sal_Int32>(aTypes.size()),"aTypes: Illegal index for vector");
223 0 : aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
224 0 : sal_Int32 nType = aValue;
225 0 : ++nPos;
226 :
227 0 : if( nType == DataType::VARCHAR )
228 : {
229 0 : m_pTypeInfo = TOTypeInfoSP(new OTypeInfo());
230 :
231 0 : m_pTypeInfo->aTypeName = sTypeName;
232 0 : m_pTypeInfo->nType = nType;
233 :
234 : OSL_ENSURE((nPos) < static_cast<sal_Int32>(aTypes.size()),"aTypes: Illegal index for vector");
235 0 : aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
236 0 : m_pTypeInfo->nPrecision = aValue;
237 0 : ++nPos;
238 0 : aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
239 0 : m_pTypeInfo->aLiteralPrefix = aValue;
240 0 : ++nPos;
241 0 : aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
242 0 : m_pTypeInfo->aLiteralSuffix = aValue;
243 0 : ++nPos;
244 0 : aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
245 0 : m_pTypeInfo->aCreateParams = aValue;
246 0 : ++nPos;
247 0 : aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
248 0 : m_pTypeInfo->bNullable = (sal_Int32)aValue == ColumnValue::NULLABLE;
249 0 : ++nPos;
250 0 : aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
251 0 : m_pTypeInfo->bCaseSensitive = (sal_Bool)aValue;
252 0 : ++nPos;
253 0 : aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
254 0 : m_pTypeInfo->nSearchType = aValue;
255 0 : ++nPos;
256 0 : aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
257 0 : m_pTypeInfo->bUnsigned = (sal_Bool)aValue;
258 0 : ++nPos;
259 0 : aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
260 0 : m_pTypeInfo->bCurrency = (sal_Bool)aValue;
261 0 : ++nPos;
262 0 : aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
263 0 : m_pTypeInfo->bAutoIncrement = (sal_Bool)aValue;
264 0 : ++nPos;
265 0 : aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
266 0 : m_pTypeInfo->aLocalTypeName = aValue;
267 0 : ++nPos;
268 0 : aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
269 0 : m_pTypeInfo->nMinimumScale = aValue;
270 0 : ++nPos;
271 0 : aValue.fill(nPos,aTypes[nPos],aNullable[nPos],xRow);
272 0 : m_pTypeInfo->nMaximumScale = aValue;
273 :
274 : // check if values are less than zero like it happens in a oracle jdbc driver
275 0 : if( m_pTypeInfo->nPrecision < 0)
276 0 : m_pTypeInfo->nPrecision = 0;
277 0 : if( m_pTypeInfo->nMinimumScale < 0)
278 0 : m_pTypeInfo->nMinimumScale = 0;
279 0 : if( m_pTypeInfo->nMaximumScale < 0)
280 0 : m_pTypeInfo->nMaximumScale = 0;
281 : break;
282 : }
283 0 : }
284 : }
285 0 : if ( !m_pTypeInfo )
286 0 : m_pTypeInfo = TOTypeInfoSP(new OTypeInfo());
287 0 : SetColumnTypes(pList,_pInfoMap);
288 0 : }
289 : //---------------------------------------------------------------------------
290 0 : ODatabaseExport::~ODatabaseExport()
291 : {
292 : DBG_DTOR(ODatabaseExport,NULL);
293 0 : m_pFormatter = NULL;
294 0 : ODatabaseExport::TColumns::iterator aIter = m_aDestColumns.begin();
295 0 : ODatabaseExport::TColumns::iterator aEnd = m_aDestColumns.end();
296 :
297 0 : for(;aIter != aEnd;++aIter)
298 0 : delete aIter->second;
299 0 : m_vDestVector.clear();
300 0 : m_aDestColumns.clear();
301 0 : }
302 : // -----------------------------------------------------------------------------
303 0 : void ODatabaseExport::insertValueIntoColumn()
304 : {
305 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::insertValueIntoColumn" );
306 : DBG_CHKTHIS(ODatabaseExport,NULL);
307 0 : if(m_nColumnPos < sal_Int32(m_vDestVector.size()))
308 : {
309 0 : OFieldDescription* pField = m_vDestVector[m_nColumnPos]->second;
310 0 : if(pField)
311 : {
312 0 : sal_Int32 nNewPos = m_bIsAutoIncrement ? m_nColumnPos+1 : m_nColumnPos;
313 : OSL_ENSURE((nNewPos) < static_cast<sal_Int32>(m_vColumns.size()),"m_vColumns: Illegal index for vector");
314 :
315 0 : if ( (nNewPos) < static_cast<sal_Int32>(m_vColumns.size() ) )
316 : {
317 0 : sal_Int32 nPos = m_vColumns[nNewPos].first;
318 0 : if ( nPos != COLUMN_POSITION_NOT_FOUND )
319 : {
320 0 : if ( !m_sTextToken.Len() && pField->IsNullable() )
321 0 : m_pUpdateHelper->updateNull(nPos,pField->GetType());
322 : else
323 : {
324 : OSL_ENSURE((nNewPos) < static_cast<sal_Int32>(m_vColumnTypes.size()),"Illegal index for vector");
325 0 : if (m_vColumnTypes[nNewPos] != DataType::VARCHAR && m_vColumnTypes[nNewPos] != DataType::CHAR && m_vColumnTypes[nNewPos] != DataType::LONGVARCHAR )
326 : {
327 : RTL_LOGFILE_CONTEXT_TRACE( aLogger, "ODatabaseExport::insertValueIntoColumn != DataType::VARCHAR" );
328 0 : ensureFormatter();
329 0 : sal_Int32 nNumberFormat = 0;
330 0 : double fOutNumber = 0.0;
331 0 : bool bNumberFormatError = false;
332 0 : if ( m_pFormatter && m_sNumToken.Len() )
333 : {
334 0 : LanguageType eNumLang = LANGUAGE_NONE;
335 0 : sal_uInt32 nNumberFormat2( nNumberFormat );
336 0 : fOutNumber = SfxHTMLParser::GetTableDataOptionsValNum(nNumberFormat2,eNumLang,m_sTextToken,m_sNumToken,*m_pFormatter);
337 0 : if ( eNumLang != LANGUAGE_NONE )
338 : {
339 0 : nNumberFormat2 = m_pFormatter->GetFormatForLanguageIfBuiltIn( nNumberFormat2, eNumLang );
340 0 : m_pFormatter->IsNumberFormat( m_sTextToken, nNumberFormat2, fOutNumber );
341 : }
342 0 : nNumberFormat = static_cast<sal_Int32>(nNumberFormat2);
343 : }
344 : else
345 : {
346 0 : Reference< XNumberFormatsSupplier > xSupplier = m_xFormatter->getNumberFormatsSupplier();
347 0 : Reference<XNumberFormatTypes> xNumType(xSupplier->getNumberFormats(),UNO_QUERY);
348 : sal_Int16 nFormats[] = {
349 : NumberFormat::DATETIME
350 : ,NumberFormat::DATE
351 : ,NumberFormat::TIME
352 : ,NumberFormat::CURRENCY
353 : ,NumberFormat::NUMBER
354 : ,NumberFormat::LOGICAL
355 0 : };
356 0 : for (size_t i = 0; i < sizeof(nFormats)/sizeof(nFormats[0]); ++i)
357 : {
358 : try
359 : {
360 0 : nNumberFormat = m_xFormatter->detectNumberFormat(xNumType->getStandardFormat(nFormats[i],m_aLocale),m_sTextToken);
361 0 : break;
362 : }
363 0 : catch(Exception&)
364 : {
365 : }
366 : }
367 : try
368 : {
369 0 : fOutNumber = m_xFormatter->convertStringToNumber(nNumberFormat,m_sTextToken);
370 : }
371 0 : catch(Exception&)
372 : {
373 0 : bNumberFormatError = true;
374 0 : m_pUpdateHelper->updateString(nPos,m_sTextToken);
375 0 : }
376 : }
377 0 : if ( !bNumberFormatError )
378 : {
379 : try
380 : {
381 0 : Reference< XNumberFormatsSupplier > xSupplier = m_xFormatter->getNumberFormatsSupplier();
382 0 : Reference< XNumberFormats > xFormats = xSupplier->getNumberFormats();
383 0 : Reference<XPropertySet> xProp = xFormats->getByKey(nNumberFormat);
384 0 : sal_Int16 nType = 0;
385 0 : xProp->getPropertyValue(PROPERTY_TYPE) >>= nType;
386 0 : switch(nType)
387 : {
388 : case NumberFormat::DATE:
389 0 : m_pUpdateHelper->updateDate(nPos,::dbtools::DBTypeConversion::toDate(fOutNumber,m_aNullDate));
390 0 : break;
391 : case NumberFormat::DATETIME:
392 0 : m_pUpdateHelper->updateTimestamp(nPos,::dbtools::DBTypeConversion::toDateTime(fOutNumber,m_aNullDate));
393 0 : break;
394 : case NumberFormat::TIME:
395 0 : m_pUpdateHelper->updateTime(nPos,::dbtools::DBTypeConversion::toTime(fOutNumber));
396 0 : break;
397 : default:
398 0 : m_pUpdateHelper->updateDouble(nPos,fOutNumber);
399 0 : }
400 : }
401 0 : catch(Exception&)
402 : {
403 0 : m_pUpdateHelper->updateString(nPos,m_sTextToken);
404 : }
405 : }
406 :
407 : }
408 : else
409 0 : m_pUpdateHelper->updateString(nPos,m_sTextToken);
410 : }
411 : }
412 : }
413 0 : eraseTokens();
414 : }
415 : }
416 0 : }
417 : // -----------------------------------------------------------------------------
418 0 : sal_Int16 ODatabaseExport::CheckString(const String& aCheckToken, sal_Int16 _nOldNumberFormat)
419 : {
420 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::CheckString" );
421 : DBG_CHKTHIS(ODatabaseExport,NULL);
422 0 : double fOutNumber = 0.0;
423 0 : sal_Int16 nNumberFormat = 0;
424 :
425 : try
426 : {
427 0 : Reference< XNumberFormatsSupplier > xSupplier = m_xFormatter->getNumberFormatsSupplier();
428 0 : Reference< XNumberFormats > xFormats = xSupplier->getNumberFormats();
429 :
430 0 : ensureFormatter();
431 0 : if ( m_pFormatter && m_sNumToken.Len() )
432 : {
433 : LanguageType eNumLang;
434 0 : sal_uInt32 nFormatKey(0);
435 0 : fOutNumber = SfxHTMLParser::GetTableDataOptionsValNum(nFormatKey,eNumLang,m_sTextToken,m_sNumToken,*m_pFormatter);
436 0 : if ( eNumLang != LANGUAGE_NONE )
437 : {
438 0 : nFormatKey = m_pFormatter->GetFormatForLanguageIfBuiltIn( nFormatKey, eNumLang );
439 0 : if ( !m_pFormatter->IsNumberFormat( m_sTextToken, nFormatKey, fOutNumber ) )
440 0 : return NumberFormat::TEXT;
441 : }
442 0 : Reference<XPropertySet> xProp = xFormats->getByKey(nFormatKey);
443 0 : xProp->getPropertyValue(PROPERTY_TYPE) >>= nNumberFormat;
444 : }
445 : else
446 : {
447 0 : Reference<XNumberFormatTypes> xNumType(xFormats,UNO_QUERY);
448 0 : sal_Int32 nFormatKey = m_xFormatter->detectNumberFormat(xNumType->getStandardFormat(NumberFormat::ALL,m_aLocale),aCheckToken);
449 0 : fOutNumber = m_xFormatter->convertStringToNumber(nFormatKey,aCheckToken);
450 :
451 0 : Reference<XPropertySet> xProp = xFormats->getByKey(nFormatKey);
452 0 : sal_Int16 nType = 0;
453 0 : xProp->getPropertyValue(PROPERTY_TYPE) >>= nType;
454 :
455 0 : switch(nType)
456 : {
457 : case NumberFormat::ALL:
458 0 : nNumberFormat = NumberFormat::ALL;
459 0 : break;
460 : case NumberFormat::DEFINED:
461 0 : nNumberFormat = NumberFormat::TEXT;
462 0 : break;
463 : case NumberFormat::DATE:
464 0 : switch(_nOldNumberFormat)
465 : {
466 : case NumberFormat::DATETIME:
467 : case NumberFormat::TEXT:
468 : case NumberFormat::DATE:
469 0 : nNumberFormat = _nOldNumberFormat;
470 0 : break;
471 : case NumberFormat::ALL:
472 0 : nNumberFormat = NumberFormat::DATE;
473 0 : break;
474 : default:
475 0 : nNumberFormat = NumberFormat::TEXT;
476 :
477 : }
478 0 : break;
479 : case NumberFormat::TIME:
480 0 : switch(_nOldNumberFormat)
481 : {
482 : case NumberFormat::DATETIME:
483 : case NumberFormat::TEXT:
484 : case NumberFormat::TIME:
485 0 : nNumberFormat = _nOldNumberFormat;
486 0 : break;
487 : case NumberFormat::ALL:
488 0 : nNumberFormat = NumberFormat::TIME;
489 0 : break;
490 : default:
491 0 : nNumberFormat = NumberFormat::TEXT;
492 0 : break;
493 : }
494 0 : break;
495 : case NumberFormat::CURRENCY:
496 0 : switch(_nOldNumberFormat)
497 : {
498 : case NumberFormat::NUMBER:
499 0 : nNumberFormat = NumberFormat::CURRENCY;
500 0 : break;
501 : case NumberFormat::CURRENCY:
502 0 : nNumberFormat = _nOldNumberFormat;
503 0 : break;
504 : case NumberFormat::ALL:
505 0 : nNumberFormat = NumberFormat::CURRENCY;
506 0 : break;
507 : default:
508 0 : nNumberFormat = NumberFormat::TEXT;
509 0 : break;
510 : }
511 0 : break;
512 : case NumberFormat::NUMBER:
513 : case NumberFormat::SCIENTIFIC:
514 : case NumberFormat::FRACTION:
515 : case NumberFormat::PERCENT:
516 0 : switch(_nOldNumberFormat)
517 : {
518 : case NumberFormat::NUMBER:
519 0 : nNumberFormat = _nOldNumberFormat;
520 0 : break;
521 : case NumberFormat::CURRENCY:
522 0 : nNumberFormat = NumberFormat::CURRENCY;
523 0 : break;
524 : case NumberFormat::ALL:
525 0 : nNumberFormat = nType;
526 0 : break;
527 : default:
528 0 : nNumberFormat = NumberFormat::TEXT;
529 0 : break;
530 : }
531 0 : break;
532 : case NumberFormat::TEXT:
533 : case NumberFormat::UNDEFINED:
534 : case NumberFormat::LOGICAL:
535 0 : nNumberFormat = NumberFormat::TEXT; // Text "uberschreibt alles
536 0 : break;
537 : case NumberFormat::DATETIME:
538 0 : switch(_nOldNumberFormat)
539 : {
540 : case NumberFormat::DATETIME:
541 : case NumberFormat::TEXT:
542 : case NumberFormat::TIME:
543 0 : nNumberFormat = _nOldNumberFormat;
544 0 : break;
545 : case NumberFormat::ALL:
546 0 : nNumberFormat = NumberFormat::DATETIME;
547 0 : break;
548 : default:
549 0 : nNumberFormat = NumberFormat::TEXT;
550 0 : break;
551 : }
552 0 : break;
553 : default:
554 : OSL_FAIL("ODatabaseExport: Unbekanntes Format");
555 0 : }
556 0 : }
557 : }
558 0 : catch(Exception&)
559 : {
560 0 : nNumberFormat = NumberFormat::TEXT; // Text "uberschreibt alles
561 : }
562 :
563 0 : return nNumberFormat;
564 : }
565 : // -----------------------------------------------------------------------------
566 0 : void ODatabaseExport::SetColumnTypes(const TColumnVector* _pList,const OTypeInfoMap* _pInfoMap)
567 : {
568 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::SetColumnTypes" );
569 : DBG_CHKTHIS(ODatabaseExport,NULL);
570 0 : if(_pList && _pInfoMap)
571 : {
572 : OSL_ENSURE(m_vNumberFormat.size() == m_vColumnSize.size() && m_vColumnSize.size() == _pList->size(),"Illegal columns in list");
573 0 : Reference< XNumberFormatsSupplier > xSupplier = m_xFormatter->getNumberFormatsSupplier();
574 0 : Reference< XNumberFormats > xFormats = xSupplier->getNumberFormats();
575 0 : TColumnVector::const_iterator aIter = _pList->begin();
576 0 : TColumnVector::const_iterator aEnd = _pList->end();
577 0 : 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)
578 : {
579 : sal_Int32 nDataType;
580 0 : sal_Int32 nLength(0),nScale(0);
581 0 : sal_Int16 nType = m_vNumberFormat[i] & ~NumberFormat::DEFINED;
582 :
583 0 : switch ( nType )
584 : {
585 : case NumberFormat::ALL:
586 0 : nDataType = DataType::DOUBLE;
587 0 : break;
588 : case NumberFormat::DEFINED:
589 0 : nDataType = DataType::VARCHAR;
590 0 : nLength = ((m_vColumnSize[i] % 10 ) ? m_vColumnSize[i]/ 10 + 1: m_vColumnSize[i]/ 10) * 10;
591 0 : break;
592 : case NumberFormat::DATE:
593 0 : nDataType = DataType::DATE;
594 0 : break;
595 : case NumberFormat::TIME:
596 0 : nDataType = DataType::TIME;
597 0 : break;
598 : case NumberFormat::DATETIME:
599 0 : nDataType = DataType::TIMESTAMP;
600 0 : break;
601 : case NumberFormat::CURRENCY:
602 0 : nDataType = DataType::NUMERIC;
603 0 : nScale = 4;
604 0 : nLength = 19;
605 0 : break;
606 : case NumberFormat::NUMBER:
607 : case NumberFormat::SCIENTIFIC:
608 : case NumberFormat::FRACTION:
609 : case NumberFormat::PERCENT:
610 0 : nDataType = DataType::DOUBLE;
611 0 : break;
612 : case NumberFormat::TEXT:
613 : case NumberFormat::UNDEFINED:
614 : case NumberFormat::LOGICAL:
615 : default:
616 0 : nDataType = DataType::VARCHAR;
617 0 : nLength = ((m_vColumnSize[i] % 10 ) ? m_vColumnSize[i]/ 10 + 1: m_vColumnSize[i]/ 10) * 10;
618 0 : break;
619 : }
620 0 : OTypeInfoMap::const_iterator aFind = _pInfoMap->find(nDataType);
621 0 : if(aFind != _pInfoMap->end())
622 : {
623 0 : (*aIter)->second->SetType(aFind->second);
624 0 : (*aIter)->second->SetPrecision(::std::min<sal_Int32>(aFind->second->nPrecision,nLength));
625 0 : (*aIter)->second->SetScale(::std::min<sal_Int32>(aFind->second->nMaximumScale,nScale));
626 :
627 : sal_Int32 nFormatKey = ::dbtools::getDefaultNumberFormat( nDataType,
628 0 : (*aIter)->second->GetScale(),
629 0 : (*aIter)->second->IsCurrency(),
630 : Reference< XNumberFormatTypes>(xFormats,UNO_QUERY),
631 0 : m_aLocale);
632 :
633 0 : (*aIter)->second->SetFormatKey(nFormatKey);
634 : }
635 0 : }
636 : }
637 0 : }
638 : // -----------------------------------------------------------------------------
639 0 : void ODatabaseExport::CreateDefaultColumn(const ::rtl::OUString& _rColumnName)
640 : {
641 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::CreateDefaultColumn" );
642 : DBG_CHKTHIS(ODatabaseExport,NULL);
643 0 : Reference< XDatabaseMetaData> xDestMetaData(m_xConnection->getMetaData());
644 0 : sal_Int32 nMaxNameLen(xDestMetaData->getMaxColumnNameLength());
645 0 : ::rtl::OUString aAlias = _rColumnName;
646 0 : if ( isSQL92CheckEnabled(m_xConnection) )
647 0 : aAlias = ::dbtools::convertName2SQLName(_rColumnName,xDestMetaData->getExtraNameCharacters());
648 :
649 0 : if(nMaxNameLen && aAlias.getLength() > nMaxNameLen)
650 0 : aAlias = aAlias.copy(0, ::std::min<sal_Int32>( nMaxNameLen-1, aAlias.getLength() ) );
651 :
652 0 : ::rtl::OUString sName(aAlias);
653 0 : if(m_aDestColumns.find(sName) != m_aDestColumns.end())
654 : {
655 0 : sal_Int32 nPos = 0;
656 0 : sal_Int32 nCount = 2;
657 0 : while(m_aDestColumns.find(sName) != m_aDestColumns.end())
658 : {
659 0 : sName = aAlias;
660 0 : sName += ::rtl::OUString::valueOf(++nPos);
661 0 : if(nMaxNameLen && sName.getLength() > nMaxNameLen)
662 : {
663 0 : aAlias = aAlias.copy(0,::std::min<sal_Int32>( nMaxNameLen-nCount, aAlias.getLength() ));
664 0 : sName = aAlias;
665 0 : sName += ::rtl::OUString::valueOf(nPos);
666 0 : ++nCount;
667 : }
668 : }
669 : }
670 0 : aAlias = sName;
671 : // now create a column
672 0 : OFieldDescription* pField = new OFieldDescription();
673 0 : pField->SetType(m_pTypeInfo);
674 0 : pField->SetName(aAlias);
675 0 : pField->SetPrecision(::std::min<sal_Int32>((sal_Int32)255,m_pTypeInfo->nPrecision));
676 0 : pField->SetScale(0);
677 0 : pField->SetIsNullable(ColumnValue::NULLABLE);
678 0 : pField->SetAutoIncrement(sal_False);
679 0 : pField->SetPrimaryKey(sal_False);
680 0 : pField->SetCurrency(sal_False);
681 :
682 0 : TColumns::iterator aFind = m_aDestColumns.find( aAlias );
683 0 : if ( aFind != m_aDestColumns.end() )
684 : {
685 0 : delete aFind->second;
686 0 : m_aDestColumns.erase(aFind);
687 : }
688 :
689 0 : m_vDestVector.push_back(m_aDestColumns.insert(TColumns::value_type(aAlias,pField)).first);
690 0 : }
691 : // -----------------------------------------------------------------------------
692 0 : sal_Bool ODatabaseExport::createRowSet()
693 : {
694 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::createRowSet" );
695 : DBG_CHKTHIS(ODatabaseExport,NULL);
696 0 : m_pUpdateHelper.reset(new OParameterUpdateHelper(createPreparedStatment(m_xConnection->getMetaData(),m_xTable,m_vColumns)));
697 :
698 0 : return m_pUpdateHelper.get() != NULL;
699 : }
700 : // -----------------------------------------------------------------------------
701 0 : sal_Bool ODatabaseExport::executeWizard(const ::rtl::OUString& _rTableName,const Any& _aTextColor,const FontDescriptor& _rFont)
702 : {
703 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::executeWizard" );
704 : DBG_CHKTHIS(ODatabaseExport,NULL);
705 :
706 0 : bool bHaveDefaultTable = !m_sDefaultTableName.isEmpty();
707 0 : ::rtl::OUString sTableName( bHaveDefaultTable ? m_sDefaultTableName : _rTableName );
708 : OCopyTableWizard aWizard(
709 : NULL,
710 : sTableName,
711 : bHaveDefaultTable ? CopyTableOperation::AppendData : CopyTableOperation::CopyDefinitionAndData,
712 : m_aDestColumns,
713 : m_vDestVector,
714 : m_xConnection,
715 : m_xFormatter,
716 0 : getTypeSelectionPageFactory(),
717 : m_rInputStream,
718 : m_xContext
719 0 : );
720 :
721 0 : sal_Bool bError = sal_False;
722 : try
723 : {
724 0 : if (aWizard.Execute())
725 : {
726 0 : switch(aWizard.getOperation())
727 : {
728 : case CopyTableOperation::CopyDefinitionAndData:
729 : case CopyTableOperation::AppendData:
730 : {
731 0 : m_xTable = aWizard.createTable();
732 0 : bError = !m_xTable.is();
733 0 : if(m_xTable.is())
734 : {
735 0 : m_xTable->setPropertyValue(PROPERTY_FONT,makeAny(_rFont));
736 0 : if(_aTextColor.hasValue())
737 0 : m_xTable->setPropertyValue(PROPERTY_TEXTCOLOR,_aTextColor);
738 : }
739 0 : m_bIsAutoIncrement = aWizard.shouldCreatePrimaryKey();
740 0 : m_vColumns = aWizard.GetColumnPositions();
741 0 : m_vColumnTypes = aWizard.GetColumnTypes();
742 0 : m_bAppendFirstLine = !aWizard.UseHeaderLine();
743 : }
744 0 : break;
745 : default:
746 0 : bError = sal_True; // there is no error but I have nothing more to do
747 : }
748 : }
749 : else
750 0 : bError = sal_True;
751 :
752 0 : if(!bError)
753 0 : bError = !createRowSet();
754 : }
755 0 : catch( const SQLException&)
756 : {
757 0 : ::dbaui::showError( ::dbtools::SQLExceptionInfo( ::cppu::getCaughtException() ), &aWizard, m_xContext );
758 0 : bError = sal_True;
759 : }
760 0 : catch( const Exception& )
761 : {
762 : DBG_UNHANDLED_EXCEPTION();
763 : }
764 :
765 0 : return bError;
766 : }
767 : //---------------------------------------------------------------------------------
768 0 : void ODatabaseExport::showErrorDialog(const ::com::sun::star::sdbc::SQLException& e)
769 : {
770 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::showErrorDialog" );
771 0 : if(!m_bDontAskAgain)
772 : {
773 0 : String aMsg(e.Message);
774 0 : aMsg += '\n';
775 0 : aMsg += String( ModuleRes( STR_QRY_CONTINUE ) );
776 0 : OSQLWarningBox aBox( NULL, aMsg, WB_YES_NO | WB_DEF_NO );
777 :
778 0 : if (aBox.Execute() == RET_YES)
779 0 : m_bDontAskAgain = sal_True;
780 : else
781 0 : m_bError = sal_True;
782 : }
783 0 : }
784 : // -----------------------------------------------------------------------------
785 0 : void ODatabaseExport::adjustFormat()
786 : {
787 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::adjustFormat" );
788 0 : if ( m_sTextToken.Len() )
789 : {
790 0 : sal_Int32 nNewPos = m_bIsAutoIncrement ? m_nColumnPos+1 : m_nColumnPos;
791 : OSL_ENSURE((nNewPos) < static_cast<sal_Int32>(m_vColumns.size()),"Illegal index for vector");
792 0 : if ( (nNewPos) < static_cast<sal_Int32>(m_vColumns.size()) )
793 : {
794 0 : sal_Int32 nColPos = m_vColumns[nNewPos].first;
795 0 : if( nColPos != sal::static_int_cast< long >(CONTAINER_ENTRY_NOTFOUND))
796 : {
797 0 : --nColPos;
798 : OSL_ENSURE((nColPos) < static_cast<sal_Int32>(m_vNumberFormat.size()),"m_vFormatKey: Illegal index for vector");
799 : OSL_ENSURE((nColPos) < static_cast<sal_Int32>(m_vColumnSize.size()),"m_vColumnSize: Illegal index for vector");
800 0 : m_vNumberFormat[nColPos] = CheckString(m_sTextToken,m_vNumberFormat[nColPos]);
801 0 : m_vColumnSize[nColPos] = ::std::max<sal_Int32>((sal_Int32)m_vColumnSize[nColPos],(sal_Int32)m_sTextToken.Len());
802 : }
803 : }
804 0 : eraseTokens();
805 : }
806 0 : }
807 : // -----------------------------------------------------------------------------
808 0 : void ODatabaseExport::eraseTokens()
809 : {
810 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::eraseTokens" );
811 0 : m_sTextToken.Erase();
812 0 : m_sNumToken.Erase();
813 0 : m_sValToken.Erase();
814 0 : }
815 : // -----------------------------------------------------------------------------
816 0 : void ODatabaseExport::ensureFormatter()
817 : {
818 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::ensureFormatter" );
819 0 : if ( !m_pFormatter )
820 : {
821 0 : Reference< XNumberFormatsSupplier > xSupplier = m_xFormatter->getNumberFormatsSupplier();
822 0 : Reference< XUnoTunnel > xTunnel(xSupplier,UNO_QUERY);
823 0 : SvNumberFormatsSupplierObj* pSupplierImpl = (SvNumberFormatsSupplierObj*)sal::static_int_cast< sal_IntPtr >(xTunnel->getSomething(SvNumberFormatsSupplierObj::getUnoTunnelId()));
824 0 : m_pFormatter = pSupplierImpl ? pSupplierImpl->GetNumberFormatter() : NULL;
825 0 : Reference<XPropertySet> xNumberFormatSettings = xSupplier->getNumberFormatSettings();
826 0 : xNumberFormatSettings->getPropertyValue(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("NullDate"))) >>= m_aNullDate;
827 : }
828 0 : }
829 : // -----------------------------------------------------------------------------
830 0 : Reference< XPreparedStatement > ODatabaseExport::createPreparedStatment( const Reference<XDatabaseMetaData>& _xMetaData
831 : ,const Reference<XPropertySet>& _xDestTable
832 : ,const TPositions& _rvColumns)
833 : {
834 : RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "ODatabaseExport::createPreparedStatment" );
835 0 : ::rtl::OUString aSql(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("INSERT INTO ")));
836 0 : ::rtl::OUString sComposedTableName = ::dbtools::composeTableName( _xMetaData, _xDestTable, ::dbtools::eInDataManipulation, false, false, true );
837 :
838 0 : aSql += sComposedTableName;
839 0 : aSql += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" ( "));
840 : // set values and column names
841 0 : ::rtl::OUString aValues(RTL_CONSTASCII_USTRINGPARAM(" VALUES ( "));
842 0 : static ::rtl::OUString aPara(RTL_CONSTASCII_USTRINGPARAM("?,"));
843 0 : static ::rtl::OUString aComma(RTL_CONSTASCII_USTRINGPARAM(","));
844 :
845 0 : ::rtl::OUString aQuote;
846 0 : if ( _xMetaData.is() )
847 0 : aQuote = _xMetaData->getIdentifierQuoteString();
848 :
849 0 : Reference<XColumnsSupplier> xDestColsSup(_xDestTable,UNO_QUERY_THROW);
850 :
851 : // create sql string and set column types
852 0 : Sequence< ::rtl::OUString> aDestColumnNames = xDestColsSup->getColumns()->getElementNames();
853 0 : if ( aDestColumnNames.getLength() == 0 )
854 : {
855 0 : return Reference< XPreparedStatement > ();
856 : }
857 0 : const ::rtl::OUString* pIter = aDestColumnNames.getConstArray();
858 0 : ::std::vector< ::rtl::OUString> aInsertList;
859 0 : aInsertList.resize(aDestColumnNames.getLength()+1);
860 0 : sal_Int32 i = 0;
861 0 : for(sal_uInt32 j=0; j < aInsertList.size() ;++i,++j)
862 : {
863 : ODatabaseExport::TPositions::const_iterator aFind = ::std::find_if(_rvColumns.begin(),_rvColumns.end(),
864 0 : ::o3tl::compose1(::std::bind2nd(::std::equal_to<sal_Int32>(),i+1),::o3tl::select2nd<ODatabaseExport::TPositions::value_type>()));
865 0 : if ( _rvColumns.end() != aFind && aFind->second != sal::static_int_cast< long >(CONTAINER_ENTRY_NOTFOUND) && aFind->first != sal::static_int_cast< long >(CONTAINER_ENTRY_NOTFOUND) )
866 : {
867 : OSL_ENSURE((aFind->first) < static_cast<sal_Int32>(aInsertList.size()),"aInsertList: Illegal index for vector");
868 0 : aInsertList[aFind->first] = ::dbtools::quoteName( aQuote,*(pIter+i));
869 : }
870 : }
871 :
872 0 : i = 1;
873 : // create the sql string
874 0 : ::std::vector< ::rtl::OUString>::iterator aInsertEnd = aInsertList.end();
875 0 : for (::std::vector< ::rtl::OUString>::iterator aInsertIter = aInsertList.begin(); aInsertIter != aInsertEnd; ++aInsertIter)
876 : {
877 0 : if ( !aInsertIter->isEmpty() )
878 : {
879 0 : aSql += *aInsertIter;
880 0 : aSql += aComma;
881 0 : aValues += aPara;
882 : }
883 : }
884 :
885 0 : aSql = aSql.replaceAt(aSql.getLength()-1,1,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(")")));
886 0 : aValues = aValues.replaceAt(aValues.getLength()-1,1,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(")")));
887 :
888 0 : aSql += aValues;
889 : // now create,fill and execute the prepared statement
890 0 : return Reference< XPreparedStatement >(_xMetaData->getConnection()->prepareStatement(aSql));
891 : }
892 : // -----------------------------------------------------------------------------
893 :
894 :
895 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|