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