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