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