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 "dbu_misc.hrc"
22 : #include "dbustrings.hrc"
23 : #include "moduledbu.hxx"
24 : #include "sqlmessage.hxx"
25 : #include "UITools.hxx"
26 : #include "WColumnSelect.hxx"
27 : #include "WCopyTable.hxx"
28 : #include "WCPage.hxx"
29 : #include "WExtendPages.hxx"
30 : #include "WizardPages.hrc"
31 : #include "WNameMatch.hxx"
32 : #include "WTypeSelect.hxx"
33 :
34 : #include <com/sun/star/sdb/application/CopyTableOperation.hpp>
35 : #include <com/sun/star/sdb/SQLContext.hpp>
36 : #include <com/sun/star/sdbc/ColumnValue.hpp>
37 : #include <com/sun/star/sdbc/DataType.hpp>
38 : #include <com/sun/star/sdbc/XResultSet.hpp>
39 : #include <com/sun/star/sdbc/XRow.hpp>
40 : #include <com/sun/star/sdbcx/KeyType.hpp>
41 : #include <com/sun/star/sdbcx/XAppend.hpp>
42 : #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
43 : #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
44 : #include <com/sun/star/sdbcx/XKeysSupplier.hpp>
45 : #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
46 : #include <com/sun/star/sdbcx/XViewsSupplier.hpp>
47 : #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
48 : #include <com/sun/star/task/InteractionHandler.hpp>
49 :
50 : #include <comphelper/extract.hxx>
51 : #include <comphelper/processfactory.hxx>
52 : #include <comphelper/types.hxx>
53 : #include <comphelper/interaction.hxx>
54 : #include <connectivity/dbtools.hxx>
55 : #include <connectivity/dbmetadata.hxx>
56 : #include <connectivity/dbexception.hxx>
57 :
58 : #include <rtl/ustrbuf.hxx>
59 : #include <tools/debug.hxx>
60 : #include <tools/diagnose_ex.h>
61 : #include <vcl/lstbox.hxx>
62 : #include <vcl/msgbox.hxx>
63 : #include <vcl/waitobj.hxx>
64 :
65 : #include <functional>
66 : #include <o3tl/compat_functional.hxx>
67 :
68 : using namespace ::dbaui;
69 : using namespace ::com::sun::star::uno;
70 : using namespace ::com::sun::star::beans;
71 : using namespace ::com::sun::star::container;
72 : using namespace ::com::sun::star::util;
73 : using namespace ::com::sun::star::sdb;
74 : using namespace ::com::sun::star::sdbc;
75 : using namespace ::com::sun::star::sdbcx;
76 : using namespace ::com::sun::star::lang;
77 : using namespace ::com::sun::star::task;
78 : using namespace dbtools;
79 :
80 : namespace CopyTableOperation = ::com::sun::star::sdb::application::CopyTableOperation;
81 :
82 : #define MAX_PAGES 4 // max. number of pages, which are shown
83 :
84 : DBG_NAME(OCopyTableWizard)
85 : namespace
86 : {
87 : //....................................................................
88 0 : void clearColumns(ODatabaseExport::TColumns& _rColumns, ODatabaseExport::TColumnVector& _rColumnsVec)
89 : {
90 0 : ODatabaseExport::TColumns::iterator aIter = _rColumns.begin();
91 0 : ODatabaseExport::TColumns::iterator aEnd = _rColumns.end();
92 :
93 0 : for(;aIter != aEnd;++aIter)
94 0 : delete aIter->second;
95 :
96 0 : _rColumnsVec.clear();
97 0 : _rColumns.clear();
98 0 : }
99 : }
100 :
101 : //========================================================================
102 : //= ICopyTableSourceObject
103 : //========================================================================
104 : //------------------------------------------------------------------------
105 0 : ICopyTableSourceObject::~ICopyTableSourceObject()
106 : {
107 0 : }
108 :
109 : //========================================================================
110 : //= ObjectCopySource
111 : //========================================================================
112 : //------------------------------------------------------------------------
113 0 : ObjectCopySource::ObjectCopySource( const Reference< XConnection >& _rxConnection, const Reference< XPropertySet >& _rxObject )
114 : :m_xConnection( _rxConnection, UNO_SET_THROW )
115 0 : ,m_xMetaData( _rxConnection->getMetaData(), UNO_SET_THROW )
116 : ,m_xObject( _rxObject, UNO_SET_THROW )
117 0 : ,m_xObjectPSI( _rxObject->getPropertySetInfo(), UNO_SET_THROW )
118 0 : ,m_xObjectColumns( Reference< XColumnsSupplier >( _rxObject, UNO_QUERY_THROW )->getColumns(), UNO_SET_THROW )
119 : {
120 0 : }
121 :
122 : //------------------------------------------------------------------------
123 0 : OUString ObjectCopySource::getQualifiedObjectName() const
124 : {
125 0 : OUString sName;
126 :
127 0 : if ( !m_xObjectPSI->hasPropertyByName( PROPERTY_COMMAND ) )
128 0 : sName = ::dbtools::composeTableName( m_xMetaData, m_xObject, ::dbtools::eInDataManipulation, false, false, false );
129 : else
130 0 : m_xObject->getPropertyValue( PROPERTY_NAME ) >>= sName;
131 0 : return sName;
132 : }
133 :
134 : //------------------------------------------------------------------------
135 0 : bool ObjectCopySource::isView() const
136 : {
137 0 : bool bIsView = false;
138 : try
139 : {
140 0 : if ( m_xObjectPSI->hasPropertyByName( PROPERTY_TYPE ) )
141 : {
142 0 : OUString sObjectType;
143 0 : OSL_VERIFY( m_xObject->getPropertyValue( PROPERTY_TYPE ) >>= sObjectType );
144 0 : bIsView = sObjectType == "VIEW";
145 : }
146 : }
147 0 : catch( const Exception& )
148 : {
149 : DBG_UNHANDLED_EXCEPTION();
150 : }
151 0 : return bIsView;
152 : }
153 :
154 : //------------------------------------------------------------------------
155 0 : void ObjectCopySource::copyUISettingsTo( const Reference< XPropertySet >& _rxObject ) const
156 : {
157 : const OUString aCopyProperties[] = {
158 : OUString(PROPERTY_FONT), OUString(PROPERTY_ROW_HEIGHT), OUString(PROPERTY_TEXTCOLOR),OUString(PROPERTY_TEXTLINECOLOR),OUString(PROPERTY_TEXTEMPHASIS),OUString(PROPERTY_TEXTRELIEF)
159 0 : };
160 0 : for ( size_t i=0; i < sizeof( aCopyProperties ) / sizeof( aCopyProperties[0] ); ++i )
161 : {
162 0 : if ( m_xObjectPSI->hasPropertyByName( aCopyProperties[i] ) )
163 0 : _rxObject->setPropertyValue( aCopyProperties[i], m_xObject->getPropertyValue( aCopyProperties[i] ) );
164 0 : }
165 0 : }
166 : //------------------------------------------------------------------------
167 0 : void ObjectCopySource::copyFilterAndSortingTo( const Reference< XConnection >& _xConnection,const Reference< XPropertySet >& _rxObject ) const
168 : {
169 : ::std::pair< OUString, OUString > aProperties[] = {
170 : ::std::pair< OUString, OUString >(PROPERTY_FILTER,OUString(" AND "))
171 : ,::std::pair< OUString, OUString >(PROPERTY_ORDER,OUString(" ORDER BY "))
172 0 : };
173 :
174 0 : size_t i = 0;
175 :
176 : try
177 : {
178 0 : const String sSourceName = (::dbtools::composeTableNameForSelect(m_xConnection,m_xObject) + OUString("."));
179 0 : const OUString sTargetName = ::dbtools::composeTableNameForSelect(_xConnection,_rxObject);
180 0 : const String sTargetNameTemp = (sTargetName + OUString("."));
181 :
182 0 : OUString sStatement("SELECT * FROM ");
183 0 : sStatement += sTargetName;
184 0 : sStatement += OUString(" WHERE 0=1");
185 :
186 :
187 0 : for ( i=0; i < sizeof( aProperties ) / sizeof( aProperties[0] ); ++i )
188 : {
189 0 : if ( m_xObjectPSI->hasPropertyByName( aProperties[i].first ) )
190 : {
191 0 : OUString sFilter;
192 0 : m_xObject->getPropertyValue( aProperties[i].first ) >>= sFilter;
193 0 : if ( !sFilter.isEmpty() )
194 : {
195 0 : sStatement += aProperties[i].second;
196 0 : String sReplace = sFilter;
197 0 : sReplace.SearchAndReplace(sSourceName,sTargetNameTemp);
198 0 : sFilter = sReplace;
199 0 : _rxObject->setPropertyValue( aProperties[i].first, makeAny(sFilter) );
200 0 : sStatement += sFilter;
201 0 : }
202 : }
203 : }
204 :
205 0 : _xConnection->createStatement()->executeQuery(sStatement);
206 :
207 0 : if ( m_xObjectPSI->hasPropertyByName( PROPERTY_APPLYFILTER ) )
208 0 : _rxObject->setPropertyValue( PROPERTY_APPLYFILTER, m_xObject->getPropertyValue( PROPERTY_APPLYFILTER ) );
209 : }
210 0 : catch(Exception&)
211 : {
212 0 : }
213 0 : }
214 : //------------------------------------------------------------------------
215 0 : Sequence< OUString > ObjectCopySource::getColumnNames() const
216 : {
217 0 : return m_xObjectColumns->getElementNames();
218 : }
219 :
220 : //------------------------------------------------------------------------
221 0 : Sequence< OUString > ObjectCopySource::getPrimaryKeyColumnNames() const
222 : {
223 0 : const Reference<XNameAccess> xPrimaryKeyColumns = getPrimaryKeyColumns_throw(m_xObject);
224 0 : Sequence< OUString > aKeyColNames;
225 0 : if ( xPrimaryKeyColumns.is() )
226 0 : aKeyColNames = xPrimaryKeyColumns->getElementNames();
227 0 : return aKeyColNames;
228 : }
229 :
230 : //------------------------------------------------------------------------
231 0 : OFieldDescription* ObjectCopySource::createFieldDescription( const OUString& _rColumnName ) const
232 : {
233 0 : Reference< XPropertySet > xColumn( m_xObjectColumns->getByName( _rColumnName ), UNO_QUERY_THROW );
234 0 : return new OFieldDescription( xColumn );
235 : }
236 : //------------------------------------------------------------------------
237 0 : OUString ObjectCopySource::getSelectStatement() const
238 : {
239 0 : OUString sSelectStatement;
240 0 : if ( m_xObjectPSI->hasPropertyByName( PROPERTY_COMMAND ) )
241 : { // query
242 0 : OSL_VERIFY( m_xObject->getPropertyValue( PROPERTY_COMMAND ) >>= sSelectStatement );
243 : }
244 : else
245 : { // table
246 0 : OUStringBuffer aSQL;
247 0 : aSQL.appendAscii( "SELECT " );
248 :
249 : // we need to create the sql stmt with column names
250 : // otherwise it is possible that names don't match
251 0 : const OUString sQuote = m_xMetaData->getIdentifierQuoteString();
252 :
253 0 : Sequence< OUString > aColumnNames = getColumnNames();
254 0 : const OUString* pColumnName = aColumnNames.getConstArray();
255 0 : const OUString* pEnd = pColumnName + aColumnNames.getLength();
256 0 : for ( ; pColumnName != pEnd; )
257 : {
258 0 : aSQL.append( ::dbtools::quoteName( sQuote, *pColumnName++ ) );
259 :
260 0 : if ( pColumnName == pEnd )
261 0 : aSQL.appendAscii( " " );
262 : else
263 0 : aSQL.appendAscii( ", " );
264 : }
265 :
266 0 : aSQL.appendAscii( "FROM " );
267 0 : aSQL.append( ::dbtools::composeTableNameForSelect( m_xConnection, m_xObject ) );
268 :
269 0 : sSelectStatement = aSQL.makeStringAndClear();
270 : }
271 :
272 0 : return sSelectStatement;
273 : }
274 :
275 : //------------------------------------------------------------------------
276 0 : ::utl::SharedUNOComponent< XPreparedStatement > ObjectCopySource::getPreparedSelectStatement() const
277 : {
278 : ::utl::SharedUNOComponent< XPreparedStatement > xStatement(
279 0 : m_xConnection->prepareStatement( getSelectStatement() ),
280 : ::utl::SharedUNOComponent< XPreparedStatement >::TakeOwnership
281 0 : );
282 0 : return xStatement;
283 : }
284 :
285 : //========================================================================
286 : //= NamedTableCopySource
287 : //========================================================================
288 : //------------------------------------------------------------------------
289 0 : NamedTableCopySource::NamedTableCopySource( const Reference< XConnection >& _rxConnection, const OUString& _rTableName )
290 : :m_xConnection( _rxConnection, UNO_SET_THROW )
291 0 : ,m_xMetaData( _rxConnection->getMetaData(), UNO_SET_THROW )
292 : ,m_sTableName( _rTableName )
293 0 : ,m_aColumnInfo()
294 : {
295 0 : ::dbtools::qualifiedNameComponents( m_xMetaData, m_sTableName, m_sTableCatalog, m_sTableSchema, m_sTableBareName, ::dbtools::eComplete );
296 0 : impl_ensureColumnInfo_throw();
297 0 : }
298 :
299 : //------------------------------------------------------------------------
300 0 : OUString NamedTableCopySource::getQualifiedObjectName() const
301 : {
302 0 : return m_sTableName;
303 : }
304 :
305 : //------------------------------------------------------------------------
306 0 : bool NamedTableCopySource::isView() const
307 : {
308 0 : OUString sTableType;
309 : try
310 : {
311 0 : Reference< XResultSet > xTableDesc( m_xMetaData->getTables( makeAny( m_sTableCatalog ), m_sTableSchema, m_sTableBareName,
312 0 : Sequence< OUString >() ) );
313 0 : Reference< XRow > xTableDescRow( xTableDesc, UNO_QUERY_THROW );
314 0 : OSL_VERIFY( xTableDesc->next() );
315 0 : sTableType = xTableDescRow->getString( 4 );
316 0 : OSL_ENSURE( !xTableDescRow->wasNull(), "NamedTableCopySource::isView: invalid table type!" );
317 : }
318 0 : catch( const Exception& )
319 : {
320 : DBG_UNHANDLED_EXCEPTION();
321 : }
322 0 : return sTableType == "VIEW";
323 : }
324 :
325 : //------------------------------------------------------------------------
326 0 : void NamedTableCopySource::copyUISettingsTo( const Reference< XPropertySet >& /*_rxObject*/ ) const
327 : {
328 : // not supported: we do not have UI settings to copy
329 0 : }
330 : // -----------------------------------------------------------------------------
331 0 : void NamedTableCopySource::copyFilterAndSortingTo( const Reference< XConnection >& ,const Reference< XPropertySet >& /*_rxObject*/ ) const
332 : {
333 0 : }
334 : //------------------------------------------------------------------------
335 0 : void NamedTableCopySource::impl_ensureColumnInfo_throw()
336 : {
337 0 : if ( !m_aColumnInfo.empty() )
338 0 : return;
339 :
340 0 : Reference< XResultSetMetaDataSupplier > xStatementMetaSupp( impl_ensureStatement_throw().getTyped(), UNO_QUERY_THROW );
341 0 : Reference< XResultSetMetaData > xStatementMeta( xStatementMetaSupp->getMetaData(), UNO_SET_THROW );
342 :
343 0 : sal_Int32 nColCount( xStatementMeta->getColumnCount() );
344 0 : for ( sal_Int32 i = 1; i <= nColCount; ++i )
345 : {
346 0 : OFieldDescription aDesc;
347 :
348 0 : aDesc.SetName( xStatementMeta->getColumnName( i ) );
349 0 : aDesc.SetHelpText( xStatementMeta->getColumnLabel( i ) );
350 0 : aDesc.SetTypeValue( xStatementMeta->getColumnType( i ) );
351 0 : aDesc.SetTypeName( xStatementMeta->getColumnTypeName( i ) );
352 0 : aDesc.SetPrecision( xStatementMeta->getPrecision( i ) );
353 0 : aDesc.SetScale( xStatementMeta->getScale( i ) );
354 0 : aDesc.SetIsNullable( xStatementMeta->isNullable( i ) );
355 0 : aDesc.SetCurrency( xStatementMeta->isCurrency( i ) );
356 0 : aDesc.SetAutoIncrement( xStatementMeta->isAutoIncrement( i ) );
357 :
358 0 : m_aColumnInfo.push_back( aDesc );
359 0 : }
360 : }
361 :
362 : //------------------------------------------------------------------------
363 0 : ::utl::SharedUNOComponent< XPreparedStatement > NamedTableCopySource::impl_ensureStatement_throw()
364 : {
365 0 : if ( !m_xStatement.is() )
366 0 : m_xStatement.set( m_xConnection->prepareStatement( getSelectStatement() ), UNO_SET_THROW );
367 0 : return m_xStatement;
368 : }
369 :
370 : //------------------------------------------------------------------------
371 0 : Sequence< OUString > NamedTableCopySource::getColumnNames() const
372 : {
373 0 : Sequence< OUString > aNames( m_aColumnInfo.size() );
374 0 : for ( ::std::vector< OFieldDescription >::const_iterator col = m_aColumnInfo.begin();
375 0 : col != m_aColumnInfo.end();
376 : ++col
377 : )
378 0 : aNames[ col - m_aColumnInfo.begin() ] = col->GetName();
379 :
380 0 : return aNames;
381 : }
382 :
383 : //------------------------------------------------------------------------
384 0 : Sequence< OUString > NamedTableCopySource::getPrimaryKeyColumnNames() const
385 : {
386 0 : Sequence< OUString > aPKColNames;
387 :
388 : try
389 : {
390 0 : Reference< XResultSet > xPKDesc( m_xMetaData->getPrimaryKeys( makeAny( m_sTableCatalog ), m_sTableSchema, m_sTableBareName ) );
391 0 : Reference< XRow > xPKDescRow( xPKDesc, UNO_QUERY_THROW );
392 0 : while ( xPKDesc->next() )
393 : {
394 0 : sal_Int32 len( aPKColNames.getLength() );
395 0 : aPKColNames.realloc( len + 1 );
396 0 : aPKColNames[ len ] = xPKDescRow->getString( 4 ); // COLUMN_NAME
397 0 : }
398 : }
399 0 : catch( const Exception& )
400 : {
401 : DBG_UNHANDLED_EXCEPTION();
402 : }
403 :
404 0 : return aPKColNames;
405 : }
406 :
407 : //------------------------------------------------------------------------
408 0 : OFieldDescription* NamedTableCopySource::createFieldDescription( const OUString& _rColumnName ) const
409 : {
410 0 : for ( ::std::vector< OFieldDescription >::const_iterator col = m_aColumnInfo.begin();
411 0 : col != m_aColumnInfo.end();
412 : ++col
413 : )
414 0 : if ( col->GetName() == _rColumnName )
415 0 : return new OFieldDescription( *col );
416 :
417 0 : return NULL;
418 : }
419 : //------------------------------------------------------------------------
420 0 : OUString NamedTableCopySource::getSelectStatement() const
421 : {
422 0 : OUStringBuffer aSQL;
423 0 : aSQL.appendAscii( "SELECT * FROM " );
424 :
425 0 : aSQL.append( ::dbtools::composeTableNameForSelect( m_xConnection, m_sTableCatalog, m_sTableSchema, m_sTableBareName ) );
426 :
427 0 : return aSQL.makeStringAndClear();
428 : }
429 :
430 : //------------------------------------------------------------------------
431 0 : ::utl::SharedUNOComponent< XPreparedStatement > NamedTableCopySource::getPreparedSelectStatement() const
432 : {
433 0 : return const_cast< NamedTableCopySource* >( this )->impl_ensureStatement_throw();
434 : }
435 :
436 : // ========================================================
437 : // DummyCopySource
438 : // ========================================================
439 0 : class DummyCopySource : public ICopyTableSourceObject
440 : {
441 : public:
442 0 : DummyCopySource() { }
443 :
444 : static const DummyCopySource& Instance();
445 :
446 : // ICopyTableSourceObject overridables
447 : virtual OUString getQualifiedObjectName() const;
448 : virtual bool isView() const;
449 : virtual void copyUISettingsTo( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxObject ) const;
450 : virtual void copyFilterAndSortingTo(const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection, const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxObject ) const;
451 : virtual ::com::sun::star::uno::Sequence< OUString >
452 : getColumnNames() const;
453 : virtual ::com::sun::star::uno::Sequence< OUString >
454 : getPrimaryKeyColumnNames() const;
455 : virtual OFieldDescription* createFieldDescription( const OUString& _rColumnName ) const;
456 : virtual OUString getSelectStatement() const;
457 : virtual ::utl::SharedUNOComponent< XPreparedStatement >
458 : getPreparedSelectStatement() const;
459 : };
460 :
461 : //------------------------------------------------------------------------
462 0 : const DummyCopySource& DummyCopySource::Instance()
463 : {
464 0 : static DummyCopySource s_aTheInstance;
465 0 : return s_aTheInstance;
466 : }
467 :
468 : //------------------------------------------------------------------------
469 0 : OUString DummyCopySource::getQualifiedObjectName() const
470 : {
471 : SAL_WARN("dbaccess.ui", "DummyCopySource::getQualifiedObjectName: not to be called!" );
472 0 : return OUString();
473 : }
474 :
475 : //------------------------------------------------------------------------
476 0 : bool DummyCopySource::isView() const
477 : {
478 : SAL_WARN("dbaccess.ui", "DummyCopySource::isView: not to be called!" );
479 0 : return false;
480 : }
481 :
482 : //------------------------------------------------------------------------
483 0 : void DummyCopySource::copyUISettingsTo( const Reference< XPropertySet >& /*_rxObject*/ ) const
484 : {
485 : // no support
486 0 : }
487 : // -----------------------------------------------------------------------------
488 0 : void DummyCopySource::copyFilterAndSortingTo( const Reference< XConnection >& ,const Reference< XPropertySet >& /*_rxObject*/ ) const
489 : {
490 0 : }
491 : //------------------------------------------------------------------------
492 0 : Sequence< OUString > DummyCopySource::getColumnNames() const
493 : {
494 0 : return Sequence< OUString >();
495 : }
496 :
497 : //------------------------------------------------------------------------
498 0 : Sequence< OUString > DummyCopySource::getPrimaryKeyColumnNames() const
499 : {
500 : SAL_WARN("dbaccess.ui", "DummyCopySource::getPrimaryKeyColumnNames: not to be called!" );
501 0 : return Sequence< OUString >();
502 : }
503 :
504 : //------------------------------------------------------------------------
505 0 : OFieldDescription* DummyCopySource::createFieldDescription( const OUString& /*_rColumnName*/ ) const
506 : {
507 : SAL_WARN("dbaccess.ui", "DummyCopySource::createFieldDescription: not to be called!" );
508 0 : return NULL;
509 : }
510 : //------------------------------------------------------------------------
511 0 : OUString DummyCopySource::getSelectStatement() const
512 : {
513 : SAL_WARN("dbaccess.ui", "DummyCopySource::getSelectStatement: not to be called!" );
514 0 : return OUString();
515 : }
516 :
517 : //------------------------------------------------------------------------
518 0 : ::utl::SharedUNOComponent< XPreparedStatement > DummyCopySource::getPreparedSelectStatement() const
519 : {
520 : SAL_WARN("dbaccess.ui", "DummyCopySource::getPreparedSelectStatement: not to be called!" );
521 0 : return ::utl::SharedUNOComponent< XPreparedStatement >();
522 : }
523 :
524 : //------------------------------------------------------------------------
525 : namespace
526 : {
527 0 : bool lcl_canCreateViewFor_nothrow( const Reference< XConnection >& _rxConnection )
528 : {
529 0 : Reference< XViewsSupplier > xSup( _rxConnection, UNO_QUERY );
530 0 : Reference< XDataDescriptorFactory > xViewFac;
531 0 : if ( xSup.is() )
532 0 : xViewFac.set( xSup->getViews(), UNO_QUERY );
533 0 : return xViewFac.is();
534 : }
535 :
536 0 : bool lcl_sameConnection_throw( const Reference< XConnection >& _rxLHS, const Reference< XConnection >& _rxRHS )
537 : {
538 0 : Reference< XDatabaseMetaData > xMetaLHS( _rxLHS->getMetaData(), UNO_QUERY_THROW );
539 0 : Reference< XDatabaseMetaData > xMetaRHS( _rxRHS->getMetaData(), UNO_QUERY_THROW );
540 0 : return xMetaLHS->getURL().equals( xMetaRHS->getURL() );
541 : }
542 : }
543 :
544 : //========================================================================
545 : //= OCopyTableWizard
546 : //========================================================================
547 : //------------------------------------------------------------------------
548 0 : OCopyTableWizard::OCopyTableWizard( Window * pParent, const OUString& _rDefaultName, sal_Int16 _nOperation,
549 : const ICopyTableSourceObject& _rSourceObject, const Reference< XConnection >& _xSourceConnection,
550 : const Reference< XConnection >& _xConnection, const Reference< XComponentContext >& _rxContext,
551 : const Reference< XInteractionHandler>& _xInteractionHandler)
552 : : WizardDialog( pParent, ModuleRes(WIZ_RTFCOPYTABLE))
553 : ,m_pbHelp( this , ModuleRes(PB_HELP))
554 : ,m_pbCancel( this , ModuleRes(PB_CANCEL))
555 : ,m_pbPrev( this , ModuleRes(PB_PREV))
556 : ,m_pbNext( this , ModuleRes(PB_NEXT))
557 : ,m_pbFinish( this , ModuleRes(PB_OK))
558 0 : ,m_mNameMapping(_xConnection->getMetaData().is() && _xConnection->getMetaData()->supportsMixedCaseQuotedIdentifiers())
559 : ,m_xDestConnection( _xConnection )
560 : ,m_rSourceObject( _rSourceObject )
561 : ,m_xFormatter( getNumberFormatter( _xConnection, _rxContext ) )
562 : ,m_xContext(_rxContext)
563 : ,m_xInteractionHandler(_xInteractionHandler)
564 : ,m_sTypeNames(ModuleRes(STR_TABLEDESIGN_DBFIELDTYPES))
565 : ,m_nPageCount(0)
566 : ,m_bDeleteSourceColumns(sal_True)
567 0 : ,m_bInterConnectionCopy( _xSourceConnection != _xConnection )
568 : ,m_sName( _rDefaultName )
569 : ,m_nOperation( _nOperation )
570 : ,m_ePressed( WIZARD_NONE )
571 0 : ,m_bCreatePrimaryKeyColumn(sal_False)
572 : {
573 : SAL_INFO("dbaccess.ui", "OCopyTableWizard::OCopyTableWizard" );
574 : DBG_CTOR(OCopyTableWizard,NULL);
575 0 : construct();
576 :
577 : // extract table name
578 0 : OUString sInitialTableName( _rDefaultName );
579 : try
580 : {
581 0 : m_sSourceName = m_rSourceObject.getQualifiedObjectName();
582 : OSL_ENSURE( !m_sSourceName.isEmpty(), "OCopyTableWizard::OCopyTableWizard: unable to retrieve the source object's name!" );
583 :
584 0 : if ( sInitialTableName.isEmpty() )
585 0 : sInitialTableName = m_sSourceName;
586 :
587 0 : if ( m_sName.isEmpty() )
588 : {
589 0 : if ( _xSourceConnection == m_xDestConnection )
590 : {
591 0 : Reference< XTablesSupplier > xSup( m_xDestConnection, UNO_QUERY_THROW );
592 0 : m_sName = ::dbtools::createUniqueName( xSup->getTables(), sInitialTableName, sal_False );
593 : }
594 : else
595 0 : m_sName = sInitialTableName;
596 : }
597 : }
598 0 : catch ( const Exception& )
599 : {
600 0 : m_sName = sInitialTableName;
601 : }
602 :
603 0 : ::dbaui::fillTypeInfo( _xSourceConnection, m_sTypeNames, m_aTypeInfo, m_aTypeInfoIndex );
604 0 : ::dbaui::fillTypeInfo( m_xDestConnection, m_sTypeNames, m_aDestTypeInfo, m_aDestTypeInfoIndex );
605 0 : impl_loadSourceData();
606 :
607 0 : bool bAllowViews = true;
608 : // if the source is a, don't allow creating views
609 0 : if ( m_rSourceObject.isView() )
610 0 : bAllowViews = false;
611 : // no views if the target connection does not support creating them
612 0 : if ( !lcl_canCreateViewFor_nothrow( m_xDestConnection ) )
613 0 : bAllowViews = false;
614 : // no views if we're copying to a different database
615 0 : if ( !lcl_sameConnection_throw( _xSourceConnection, m_xDestConnection ) )
616 0 : bAllowViews = false;
617 :
618 0 : if ( m_bInterConnectionCopy )
619 : {
620 0 : Reference< XDatabaseMetaData > xSrcMeta = _xSourceConnection->getMetaData();
621 0 : OUString sCatalog;
622 0 : OUString sSchema;
623 0 : OUString sTable;
624 : ::dbtools::qualifiedNameComponents( xSrcMeta,
625 : m_sName,
626 : sCatalog,
627 : sSchema,
628 : sTable,
629 0 : ::dbtools::eInDataManipulation);
630 :
631 0 : m_sName = ::dbtools::composeTableName(m_xDestConnection->getMetaData(),sCatalog,sSchema,sTable,sal_False,::dbtools::eInTableDefinitions);
632 : }
633 :
634 0 : OCopyTable* pPage1( new OCopyTable( this ) );
635 0 : pPage1->disallowUseHeaderLine();
636 0 : if ( !bAllowViews )
637 0 : pPage1->disallowViews();
638 0 : pPage1->setCreateStyleAction();
639 0 : AddWizardPage(pPage1);
640 :
641 0 : AddWizardPage( new OWizNameMatching( this ) );
642 0 : AddWizardPage( new OWizColumnSelect( this ) );
643 0 : AddWizardPage( new OWizNormalExtend( this ) );
644 0 : ActivatePage();
645 0 : }
646 :
647 : // -----------------------------------------------------------------------------
648 0 : OCopyTableWizard::OCopyTableWizard( Window* pParent, const OUString& _rDefaultName, sal_Int16 _nOperation,
649 : const ODatabaseExport::TColumns& _rSourceColumns, const ODatabaseExport::TColumnVector& _rSourceColVec,
650 : const Reference< XConnection >& _xConnection, const Reference< XNumberFormatter >& _xFormatter,
651 : TypeSelectionPageFactory _pTypeSelectionPageFactory, SvStream& _rTypeSelectionPageArg, const Reference< XComponentContext >& _rxContext )
652 : :WizardDialog( pParent, ModuleRes(WIZ_RTFCOPYTABLE))
653 : ,m_vSourceColumns(_rSourceColumns)
654 : ,m_pbHelp( this , ModuleRes(PB_HELP))
655 : ,m_pbCancel( this , ModuleRes(PB_CANCEL))
656 : ,m_pbPrev( this , ModuleRes(PB_PREV))
657 : ,m_pbNext( this , ModuleRes(PB_NEXT))
658 : ,m_pbFinish( this , ModuleRes(PB_OK))
659 0 : ,m_mNameMapping(_xConnection->getMetaData().is() && _xConnection->getMetaData()->supportsMixedCaseQuotedIdentifiers())
660 : ,m_xDestConnection( _xConnection )
661 0 : ,m_rSourceObject( DummyCopySource::Instance() )
662 : ,m_xFormatter(_xFormatter)
663 : ,m_xContext(_rxContext)
664 : ,m_sTypeNames(ModuleRes(STR_TABLEDESIGN_DBFIELDTYPES))
665 : ,m_nPageCount(0)
666 : ,m_bDeleteSourceColumns(sal_False)
667 : ,m_bInterConnectionCopy( false )
668 : ,m_sName(_rDefaultName)
669 : ,m_nOperation( _nOperation )
670 : ,m_ePressed( WIZARD_NONE )
671 0 : ,m_bCreatePrimaryKeyColumn(sal_False)
672 : {
673 : SAL_INFO("dbaccess.ui", "OCopyTableWizard::OCopyTableWizard" );
674 : DBG_CTOR(OCopyTableWizard,NULL);
675 0 : construct();
676 0 : ODatabaseExport::TColumnVector::const_iterator aIter = _rSourceColVec.begin();
677 0 : ODatabaseExport::TColumnVector::const_iterator aEnd = _rSourceColVec.end();
678 0 : for (; aIter != aEnd ; ++aIter)
679 : {
680 0 : m_vSourceVec.push_back(m_vSourceColumns.find((*aIter)->first));
681 : }
682 :
683 0 : ::dbaui::fillTypeInfo( _xConnection, m_sTypeNames, m_aTypeInfo, m_aTypeInfoIndex );
684 0 : ::dbaui::fillTypeInfo( _xConnection, m_sTypeNames, m_aDestTypeInfo, m_aDestTypeInfoIndex );
685 :
686 0 : m_xInteractionHandler.set( InteractionHandler::createWithParent(m_xContext, 0), UNO_QUERY );
687 :
688 0 : OCopyTable* pPage1( new OCopyTable( this ) );
689 0 : pPage1->disallowViews();
690 0 : pPage1->setCreateStyleAction();
691 0 : AddWizardPage( pPage1 );
692 :
693 0 : AddWizardPage( new OWizNameMatching( this ) );
694 0 : AddWizardPage( new OWizColumnSelect( this ) );
695 0 : AddWizardPage( (*_pTypeSelectionPageFactory)( this, _rTypeSelectionPageArg ) );
696 :
697 0 : ActivatePage();
698 0 : }
699 :
700 : // -----------------------------------------------------------------------------
701 0 : void OCopyTableWizard::construct()
702 : {
703 : SAL_INFO("dbaccess.ui", "OCopyTableWizard::construct" );
704 0 : AddButton( &m_pbHelp, WIZARDDIALOG_BUTTON_STDOFFSET_X );
705 0 : AddButton( &m_pbCancel, WIZARDDIALOG_BUTTON_STDOFFSET_X );
706 0 : AddButton( &m_pbPrev );
707 0 : AddButton( &m_pbNext, WIZARDDIALOG_BUTTON_STDOFFSET_X );
708 0 : AddButton( &m_pbFinish );
709 :
710 0 : m_pbPrev.SetClickHdl( LINK( this, OCopyTableWizard, ImplPrevHdl ) );
711 0 : m_pbNext.SetClickHdl( LINK( this, OCopyTableWizard, ImplNextHdl ) );
712 0 : m_pbFinish.SetClickHdl( LINK( this, OCopyTableWizard, ImplOKHdl ) );
713 :
714 0 : SetActivatePageHdl( LINK( this, OCopyTableWizard, ImplActivateHdl ) );
715 :
716 0 : SetPrevButton( &m_pbPrev );
717 0 : SetNextButton( &m_pbNext );
718 :
719 0 : ShowButtonFixedLine( sal_True );
720 :
721 0 : m_pbNext.GrabFocus();
722 :
723 0 : if (m_vDestColumns.size())
724 : // source is a html or rtf table
725 0 : m_pbNext.SetStyle(m_pbFinish.GetStyle() | WB_DEFBUTTON);
726 : else
727 0 : m_pbFinish.SetStyle(m_pbFinish.GetStyle() | WB_DEFBUTTON);
728 :
729 0 : FreeResource();
730 :
731 0 : m_pTypeInfo = TOTypeInfoSP(new OTypeInfo());
732 0 : m_pTypeInfo->aUIName = m_sTypeNames.GetToken(TYPE_OTHER);
733 0 : m_bAddPKFirstTime = sal_True;
734 0 : }
735 : //------------------------------------------------------------------------
736 0 : OCopyTableWizard::~OCopyTableWizard()
737 : {
738 : DBG_DTOR(OCopyTableWizard,NULL);
739 : for ( ;; )
740 : {
741 0 : TabPage *pPage = GetPage(0);
742 0 : if ( pPage == NULL )
743 0 : break;
744 0 : RemovePage( pPage );
745 0 : delete pPage;
746 0 : }
747 :
748 0 : if ( m_bDeleteSourceColumns )
749 0 : clearColumns(m_vSourceColumns,m_vSourceVec);
750 :
751 0 : clearColumns(m_vDestColumns,m_aDestVec);
752 :
753 : // clear the type information
754 0 : m_aTypeInfoIndex.clear();
755 0 : m_aTypeInfo.clear();
756 0 : m_aDestTypeInfoIndex.clear();
757 0 : }
758 : // -----------------------------------------------------------------------
759 0 : IMPL_LINK_NOARG(OCopyTableWizard, ImplPrevHdl)
760 : {
761 0 : m_ePressed = WIZARD_PREV;
762 0 : if ( GetCurLevel() )
763 : {
764 0 : if ( getOperation() != CopyTableOperation::AppendData )
765 : {
766 0 : if(GetCurLevel() == 2)
767 0 : ShowPage(GetCurLevel()-2);
768 : else
769 0 : ShowPrevPage();
770 : }
771 : else
772 0 : ShowPrevPage();
773 : }
774 0 : return 0;
775 : }
776 :
777 : // -----------------------------------------------------------------------
778 :
779 0 : IMPL_LINK_NOARG(OCopyTableWizard, ImplNextHdl)
780 : {
781 0 : m_ePressed = WIZARD_NEXT;
782 0 : if ( GetCurLevel() < MAX_PAGES )
783 : {
784 0 : if ( getOperation() != CopyTableOperation::AppendData )
785 : {
786 0 : if(GetCurLevel() == 0)
787 0 : ShowPage(GetCurLevel()+2);
788 : else
789 0 : ShowNextPage();
790 : }
791 : else
792 0 : ShowNextPage();
793 : }
794 0 : return 0;
795 : }
796 : // -----------------------------------------------------------------------
797 0 : sal_Bool OCopyTableWizard::CheckColumns(sal_Int32& _rnBreakPos)
798 : {
799 : SAL_INFO("dbaccess.ui", "OCopyTableWizard::CheckColumns" );
800 0 : sal_Bool bRet = sal_True;
801 0 : m_vColumnPos.clear();
802 0 : m_vColumnTypes.clear();
803 :
804 : OSL_ENSURE( m_xDestConnection.is(), "OCopyTableWizard::CheckColumns: No connection!" );
805 : //////////////////////////////////////////////////////////////////////
806 : // If database is able to process PrimaryKeys, set PrimaryKey
807 0 : if ( m_xDestConnection.is() )
808 : {
809 0 : sal_Bool bPKeyAllowed = supportsPrimaryKey();
810 :
811 0 : sal_Bool bContainsColumns = !m_vDestColumns.empty();
812 :
813 0 : if ( bPKeyAllowed && shouldCreatePrimaryKey() )
814 : {
815 : // add extra column for the primary key
816 0 : TOTypeInfoSP pTypeInfo = queryPrimaryKeyType(m_aDestTypeInfo);
817 0 : if ( pTypeInfo.get() )
818 : {
819 0 : if ( m_bAddPKFirstTime )
820 : {
821 0 : OFieldDescription* pField = new OFieldDescription();
822 0 : pField->SetName(m_aKeyName);
823 0 : pField->FillFromTypeInfo(pTypeInfo,sal_True,sal_True);
824 0 : pField->SetPrimaryKey(sal_True);
825 0 : m_bAddPKFirstTime = sal_False;
826 0 : insertColumn(0,pField);
827 : }
828 0 : m_vColumnPos.push_back(ODatabaseExport::TPositions::value_type(1,1));
829 0 : m_vColumnTypes.push_back(pTypeInfo->nType);
830 0 : }
831 : }
832 :
833 0 : if ( bContainsColumns )
834 : { // we have dest columns so look for the matching column
835 0 : ODatabaseExport::TColumnVector::const_iterator aSrcIter = m_vSourceVec.begin();
836 0 : ODatabaseExport::TColumnVector::const_iterator aSrcEnd = m_vSourceVec.end();
837 0 : for(;aSrcIter != aSrcEnd;++aSrcIter)
838 : {
839 0 : ODatabaseExport::TColumns::iterator aDestIter = m_vDestColumns.find(m_mNameMapping[(*aSrcIter)->first]);
840 :
841 0 : if ( aDestIter != m_vDestColumns.end() )
842 : {
843 0 : ODatabaseExport::TColumnVector::const_iterator aFind = ::std::find(m_aDestVec.begin(),m_aDestVec.end(),aDestIter);
844 0 : sal_Int32 nPos = (aFind - m_aDestVec.begin())+1;
845 0 : m_vColumnPos.push_back(ODatabaseExport::TPositions::value_type(nPos,nPos));
846 0 : m_vColumnTypes.push_back((*aFind)->second->GetType());
847 : }
848 : else
849 : {
850 0 : m_vColumnPos.push_back( ODatabaseExport::TPositions::value_type( COLUMN_POSITION_NOT_FOUND, COLUMN_POSITION_NOT_FOUND ) );
851 0 : m_vColumnTypes.push_back(0);
852 : }
853 : }
854 : }
855 : else
856 : {
857 0 : Reference< XDatabaseMetaData > xMetaData( m_xDestConnection->getMetaData() );
858 0 : OUString sExtraChars = xMetaData->getExtraNameCharacters();
859 0 : sal_Int32 nMaxNameLen = getMaxColumnNameLength();
860 :
861 0 : ODatabaseExport::TColumnVector::const_iterator aSrcIter = m_vSourceVec.begin();
862 0 : ODatabaseExport::TColumnVector::const_iterator aSrcEnd = m_vSourceVec.end();
863 0 : for(_rnBreakPos=0;aSrcIter != aSrcEnd && bRet ;++aSrcIter,++_rnBreakPos)
864 : {
865 0 : OFieldDescription* pField = new OFieldDescription(*(*aSrcIter)->second);
866 0 : pField->SetName(convertColumnName(TExportColumnFindFunctor(&m_vDestColumns),(*aSrcIter)->first,sExtraChars,nMaxNameLen));
867 0 : TOTypeInfoSP pType = convertType((*aSrcIter)->second->getSpecialTypeInfo(),bRet);
868 0 : pField->SetType(pType);
869 0 : if ( !bPKeyAllowed )
870 0 : pField->SetPrimaryKey(sal_False);
871 :
872 : // now create a column
873 0 : insertColumn(m_vDestColumns.size(),pField);
874 0 : m_vColumnPos.push_back(ODatabaseExport::TPositions::value_type(m_vDestColumns.size(),m_vDestColumns.size()));
875 0 : m_vColumnTypes.push_back((*aSrcIter)->second->GetType());
876 0 : }
877 : }
878 : }
879 0 : return bRet;
880 : }
881 : // -----------------------------------------------------------------------
882 0 : IMPL_LINK_NOARG(OCopyTableWizard, ImplOKHdl)
883 : {
884 0 : m_ePressed = WIZARD_FINISH;
885 0 : sal_Bool bFinish = DeactivatePage() != 0;
886 :
887 0 : if(bFinish)
888 : {
889 0 : WaitObject aWait(this);
890 0 : switch(getOperation())
891 : {
892 : case CopyTableOperation::CopyDefinitionAndData:
893 : case CopyTableOperation::CopyDefinitionOnly:
894 : {
895 0 : sal_Bool bOnFirstPage = GetCurLevel() == 0;
896 0 : if ( bOnFirstPage )
897 : {
898 : // we came from the first page so we have to clear
899 : // all column information already collected
900 0 : clearDestColumns();
901 0 : m_mNameMapping.clear();
902 : }
903 0 : sal_Int32 nBreakPos = 0;
904 0 : sal_Bool bCheckOk = CheckColumns(nBreakPos);
905 0 : if ( bOnFirstPage && !bCheckOk )
906 : {
907 0 : showColumnTypeNotSupported(m_vSourceVec[nBreakPos-1]->first);
908 0 : OWizTypeSelect* pPage = static_cast<OWizTypeSelect*>(GetPage(3));
909 0 : if ( pPage )
910 : {
911 0 : m_mNameMapping.clear();
912 0 : pPage->setDisplayRow(nBreakPos);
913 0 : ShowPage(3);
914 0 : return 0;
915 : }
916 : }
917 0 : if ( m_xDestConnection.is() )
918 : {
919 0 : if ( supportsPrimaryKey() )
920 : {
921 : ODatabaseExport::TColumns::iterator aFind = ::std::find_if(m_vDestColumns.begin(),m_vDestColumns.end()
922 0 : ,::o3tl::compose1(::std::mem_fun(&OFieldDescription::IsPrimaryKey),::o3tl::select2nd<ODatabaseExport::TColumns::value_type>()));
923 0 : if ( aFind == m_vDestColumns.end() && m_xInteractionHandler.is() )
924 : {
925 :
926 0 : String sMsg(ModuleRes(STR_TABLEDESIGN_NO_PRIM_KEY));
927 0 : SQLContext aError;
928 0 : aError.Message = sMsg;
929 0 : ::rtl::Reference< ::comphelper::OInteractionRequest > xRequest( new ::comphelper::OInteractionRequest( makeAny( aError ) ) );
930 0 : ::rtl::Reference< ::comphelper::OInteractionApprove > xYes = new ::comphelper::OInteractionApprove;
931 0 : xRequest->addContinuation( xYes.get() );
932 0 : xRequest->addContinuation( new ::comphelper::OInteractionDisapprove );
933 0 : ::rtl::Reference< ::comphelper::OInteractionAbort > xAbort = new ::comphelper::OInteractionAbort;
934 0 : xRequest->addContinuation( xAbort.get() );
935 :
936 0 : m_xInteractionHandler->handle( xRequest.get() );
937 :
938 0 : if ( xYes->wasSelected() )
939 : {
940 0 : OCopyTable* pPage = static_cast<OCopyTable*>(GetPage(0));
941 0 : m_bCreatePrimaryKeyColumn = sal_True;
942 0 : m_aKeyName = pPage->GetKeyName();
943 0 : if ( m_aKeyName.isEmpty() )
944 0 : m_aKeyName = OUString( "ID" );
945 0 : m_aKeyName = createUniqueName( m_aKeyName );
946 0 : sal_Int32 nBreakPos2 = 0;
947 0 : CheckColumns(nBreakPos2);
948 : }
949 0 : else if ( xAbort->wasSelected() )
950 : {
951 0 : ShowPage(3);
952 0 : return 0;
953 0 : }
954 : }
955 : }
956 : }
957 0 : break;
958 : }
959 : case CopyTableOperation::AppendData:
960 : case CopyTableOperation::CreateAsView:
961 0 : break;
962 : default:
963 : {
964 : SAL_WARN("dbaccess.ui", "OCopyTableWizard::ImplOKHdl: invalid creation style!");
965 : }
966 : }
967 :
968 0 : EndDialog(RET_OK);
969 : }
970 0 : return bFinish;
971 : }
972 : //------------------------------------------------------------------------
973 0 : sal_Bool OCopyTableWizard::shouldCreatePrimaryKey() const
974 : {
975 : SAL_INFO("dbaccess.ui", "OCopyTableWizard::shouldCreatePrimaryKey" );
976 0 : return m_bCreatePrimaryKeyColumn;
977 : }
978 :
979 : // -----------------------------------------------------------------------
980 0 : void OCopyTableWizard::setCreatePrimaryKey( bool _bDoCreate, const OUString& _rSuggestedName )
981 : {
982 : SAL_INFO("dbaccess.ui", "OCopyTableWizard::setCreatePrimaryKey" );
983 0 : m_bCreatePrimaryKeyColumn = _bDoCreate;
984 0 : if ( !_rSuggestedName.isEmpty() )
985 0 : m_aKeyName = _rSuggestedName;
986 :
987 0 : OCopyTable* pSettingsPage = dynamic_cast< OCopyTable* >( GetPage( 0 ) );
988 : OSL_ENSURE( pSettingsPage, "OCopyTableWizard::setCreatePrimaryKey: page should have been added in the ctor!" );
989 0 : if ( pSettingsPage )
990 0 : pSettingsPage->setCreatePrimaryKey( _bDoCreate, _rSuggestedName );
991 0 : }
992 :
993 : // -----------------------------------------------------------------------
994 0 : IMPL_LINK_NOARG(OCopyTableWizard, ImplActivateHdl)
995 : {
996 0 : OWizardPage* pCurrent = (OWizardPage*)GetPage(GetCurLevel());
997 0 : if(pCurrent)
998 : {
999 0 : sal_Bool bFirstTime = pCurrent->IsFirstTime();
1000 0 : if(bFirstTime)
1001 0 : pCurrent->Reset();
1002 :
1003 0 : CheckButtons();
1004 :
1005 0 : SetText(pCurrent->GetTitle());
1006 :
1007 0 : Invalidate();
1008 : }
1009 0 : return 0;
1010 : }
1011 : // -----------------------------------------------------------------------
1012 0 : void OCopyTableWizard::CheckButtons()
1013 : {
1014 : SAL_INFO("dbaccess.ui", "OCopyTableWizard::CheckButtons" );
1015 0 : if(GetCurLevel() == 0) // the first page has no back button
1016 : {
1017 0 : if(m_nPageCount > 1)
1018 0 : m_pbNext.Enable(sal_True);
1019 : else
1020 0 : m_pbNext.Enable(sal_False);
1021 :
1022 0 : m_pbPrev.Enable(sal_False);
1023 : }
1024 0 : else if(GetCurLevel() == m_nPageCount-1) // the last page has no next button
1025 : {
1026 0 : m_pbNext.Enable(sal_False);
1027 0 : m_pbPrev.Enable(sal_True);
1028 : }
1029 : else
1030 : {
1031 0 : m_pbPrev.Enable(sal_True);
1032 : // next already has its state
1033 : }
1034 0 : }
1035 : // -----------------------------------------------------------------------
1036 0 : void OCopyTableWizard::EnableButton(Wizard_Button_Style eStyle,sal_Bool bEnable)
1037 : {
1038 : SAL_INFO("dbaccess.ui", "OCopyTableWizard::EnableButton" );
1039 : Button* pButton;
1040 0 : if(eStyle == WIZARD_NEXT)
1041 0 : pButton = &m_pbNext;
1042 0 : else if(eStyle == WIZARD_PREV)
1043 0 : pButton = &m_pbPrev;
1044 : else
1045 0 : pButton = &m_pbFinish;
1046 0 : pButton->Enable(bEnable);
1047 :
1048 0 : }
1049 : // -----------------------------------------------------------------------
1050 0 : long OCopyTableWizard::DeactivatePage()
1051 : {
1052 : SAL_INFO("dbaccess.ui", "OCopyTableWizard::DeactivatePage" );
1053 0 : OWizardPage* pPage = (OWizardPage*)GetPage(GetCurLevel());
1054 0 : return pPage ? pPage->LeavePage() : sal_False;
1055 : }
1056 : // -----------------------------------------------------------------------
1057 0 : void OCopyTableWizard::AddWizardPage(OWizardPage* pPage)
1058 : {
1059 : SAL_INFO("dbaccess.ui", "OCopyTableWizard::AddWizardPage" );
1060 0 : AddPage(pPage);
1061 0 : ++m_nPageCount;
1062 0 : }
1063 : // -----------------------------------------------------------------------------
1064 0 : void OCopyTableWizard::insertColumn(sal_Int32 _nPos,OFieldDescription* _pField)
1065 : {
1066 : SAL_INFO("dbaccess.ui", "OCopyTableWizard::insertColumn" );
1067 : OSL_ENSURE(_pField,"FieldDescrioption is null!");
1068 0 : if ( _pField )
1069 : {
1070 0 : ODatabaseExport::TColumns::iterator aFind = m_vDestColumns.find(_pField->GetName());
1071 0 : if ( aFind != m_vDestColumns.end() )
1072 : {
1073 0 : delete aFind->second;
1074 0 : m_vDestColumns.erase(aFind);
1075 : }
1076 :
1077 0 : m_aDestVec.insert(m_aDestVec.begin() + _nPos,
1078 0 : m_vDestColumns.insert(ODatabaseExport::TColumns::value_type(_pField->GetName(),_pField)).first);
1079 0 : m_mNameMapping[_pField->GetName()] = _pField->GetName();
1080 : }
1081 0 : }
1082 : // -----------------------------------------------------------------------------
1083 0 : void OCopyTableWizard::replaceColumn(sal_Int32 _nPos,OFieldDescription* _pField,const OUString& _sOldName)
1084 : {
1085 : SAL_INFO("dbaccess.ui", "OCopyTableWizard::replaceColumn" );
1086 : OSL_ENSURE(_pField,"FieldDescrioption is null!");
1087 0 : if ( _pField )
1088 : {
1089 0 : m_vDestColumns.erase(_sOldName);
1090 : OSL_ENSURE( m_vDestColumns.find(_pField->GetName()) == m_vDestColumns.end(),"Column with that name already exist!");
1091 :
1092 0 : m_aDestVec[_nPos] =
1093 0 : m_vDestColumns.insert(ODatabaseExport::TColumns::value_type(_pField->GetName(),_pField)).first;
1094 : }
1095 0 : }
1096 : // -----------------------------------------------------------------------------
1097 0 : void OCopyTableWizard::impl_loadSourceData()
1098 : {
1099 : SAL_INFO("dbaccess.ui", "OCopyTableWizard::impl_loadSourceData" );
1100 0 : loadData( m_rSourceObject, m_vSourceColumns, m_vSourceVec );
1101 0 : }
1102 :
1103 : // -----------------------------------------------------------------------------
1104 0 : void OCopyTableWizard::loadData( const ICopyTableSourceObject& _rSourceObject, ODatabaseExport::TColumns& _rColumns, ODatabaseExport::TColumnVector& _rColVector )
1105 : {
1106 : SAL_INFO("dbaccess.ui", "OCopyTableWizard::loadData" );
1107 0 : ODatabaseExport::TColumns::iterator colEnd = _rColumns.end();
1108 0 : for ( ODatabaseExport::TColumns::iterator col = _rColumns.begin(); col != colEnd; ++col )
1109 0 : delete col->second;
1110 :
1111 0 : _rColVector.clear();
1112 0 : _rColumns.clear();
1113 :
1114 0 : OFieldDescription* pActFieldDescr = NULL;
1115 0 : OUString sCreateParam("x");
1116 : //////////////////////////////////////////////////////////////////////
1117 : // ReadOnly-Flag
1118 : // On drop no line must be editable.
1119 : // On add only empty lines must be editable.
1120 : // On Add and Drop all lines can be edited.
1121 0 : Sequence< OUString > aColumns( _rSourceObject.getColumnNames() );
1122 0 : const OUString* pColumn = aColumns.getConstArray();
1123 0 : const OUString* pColumnEnd = pColumn + aColumns.getLength();
1124 :
1125 0 : for ( ; pColumn != pColumnEnd; ++pColumn )
1126 : {
1127 : // get the properties of the column
1128 0 : pActFieldDescr = _rSourceObject.createFieldDescription( *pColumn );
1129 : OSL_ENSURE( pActFieldDescr, "OCopyTableWizard::loadData: illegal field description!" );
1130 0 : if ( !pActFieldDescr )
1131 0 : continue;
1132 :
1133 0 : sal_Int32 nType = pActFieldDescr->GetType();
1134 0 : sal_Int32 nScale = pActFieldDescr->GetScale();
1135 0 : sal_Int32 nPrecision = pActFieldDescr->GetPrecision();
1136 0 : sal_Bool bAutoIncrement = pActFieldDescr->IsAutoIncrement();
1137 0 : OUString sTypeName = pActFieldDescr->GetTypeName();
1138 :
1139 : // search for type
1140 : sal_Bool bForce;
1141 0 : TOTypeInfoSP pTypeInfo = ::dbaui::getTypeInfoFromType(m_aTypeInfo,nType,sTypeName,sCreateParam,nPrecision,nScale,bAutoIncrement,bForce);
1142 0 : if ( !pTypeInfo.get() )
1143 0 : pTypeInfo = m_pTypeInfo;
1144 :
1145 0 : pActFieldDescr->FillFromTypeInfo(pTypeInfo,sal_True,sal_False);
1146 0 : _rColVector.push_back(_rColumns.insert(ODatabaseExport::TColumns::value_type(pActFieldDescr->GetName(),pActFieldDescr)).first);
1147 0 : }
1148 :
1149 : // determine which coumns belong to the primary key
1150 0 : Sequence< OUString > aPrimaryKeyColumns( _rSourceObject.getPrimaryKeyColumnNames() );
1151 0 : const OUString* pKeyColName = aPrimaryKeyColumns.getConstArray();
1152 0 : const OUString* pKeyColEnd = pKeyColName + aPrimaryKeyColumns.getLength();
1153 :
1154 0 : for( ; pKeyColName != pKeyColEnd; ++pKeyColName )
1155 : {
1156 0 : ODatabaseExport::TColumns::iterator keyPos = _rColumns.find( *pKeyColName );
1157 0 : if ( keyPos != _rColumns.end() )
1158 : {
1159 0 : keyPos->second->SetPrimaryKey( sal_True );
1160 0 : keyPos->second->SetIsNullable( ColumnValue::NO_NULLS );
1161 : }
1162 0 : }
1163 0 : }
1164 : // -----------------------------------------------------------------------------
1165 0 : void OCopyTableWizard::clearDestColumns()
1166 : {
1167 : SAL_INFO("dbaccess.ui", "OCopyTableWizard::clearDestColumns" );
1168 0 : clearColumns(m_vDestColumns,m_aDestVec);
1169 0 : m_bAddPKFirstTime = sal_True;
1170 0 : m_mNameMapping.clear();
1171 0 : }
1172 :
1173 : // -----------------------------------------------------------------------------
1174 0 : void OCopyTableWizard::appendColumns( Reference<XColumnsSupplier>& _rxColSup, const ODatabaseExport::TColumnVector* _pVec, sal_Bool _bKeyColumns) const
1175 : {
1176 : SAL_INFO("dbaccess.ui", "OCopyTableWizard::appendColumns" );
1177 : // now append the columns
1178 : OSL_ENSURE(_rxColSup.is(),"No columns supplier");
1179 0 : if(!_rxColSup.is())
1180 0 : return;
1181 0 : Reference<XNameAccess> xColumns = _rxColSup->getColumns();
1182 : OSL_ENSURE(xColumns.is(),"No columns");
1183 0 : Reference<XDataDescriptorFactory> xColumnFactory(xColumns,UNO_QUERY);
1184 :
1185 0 : Reference<XAppend> xAppend(xColumns,UNO_QUERY);
1186 : OSL_ENSURE(xAppend.is(),"No XAppend Interface!");
1187 :
1188 0 : ODatabaseExport::TColumnVector::const_iterator aIter = _pVec->begin();
1189 0 : ODatabaseExport::TColumnVector::const_iterator aEnd = _pVec->end();
1190 0 : for(;aIter != aEnd;++aIter)
1191 : {
1192 0 : OFieldDescription* pField = (*aIter)->second;
1193 0 : if(!pField)
1194 0 : continue;
1195 :
1196 0 : Reference<XPropertySet> xColumn;
1197 0 : if(pField->IsPrimaryKey() || !_bKeyColumns)
1198 0 : xColumn = xColumnFactory->createDataDescriptor();
1199 0 : if(xColumn.is())
1200 : {
1201 0 : if(!_bKeyColumns)
1202 0 : dbaui::setColumnProperties(xColumn,pField);
1203 : else
1204 0 : xColumn->setPropertyValue(PROPERTY_NAME,makeAny(pField->GetName()));
1205 :
1206 0 : xAppend->appendByDescriptor(xColumn);
1207 0 : xColumn = NULL;
1208 : // now only the settings are missing
1209 0 : if(xColumns->hasByName(pField->GetName()))
1210 : {
1211 0 : xColumn.set(xColumns->getByName(pField->GetName()),UNO_QUERY);
1212 : OSL_ENSURE(xColumn.is(),"OCopyTableWizard::appendColumns: Column is NULL!");
1213 0 : if ( xColumn.is() )
1214 0 : pField->copyColumnSettingsTo(xColumn);
1215 : }
1216 : else
1217 : {
1218 : SAL_WARN("dbaccess.ui", "OCopyTableWizard::appendColumns: invalid field name!");
1219 : }
1220 :
1221 : }
1222 0 : }
1223 : }
1224 : // -----------------------------------------------------------------------------
1225 0 : void OCopyTableWizard::appendKey( Reference<XKeysSupplier>& _rxSup, const ODatabaseExport::TColumnVector* _pVec) const
1226 : {
1227 : SAL_INFO("dbaccess.ui", "OCopyTableWizard::appendKey" );
1228 0 : if(!_rxSup.is())
1229 0 : return; // the database doesn't support keys
1230 : OSL_ENSURE(_rxSup.is(),"No XKeysSupplier!");
1231 0 : Reference<XDataDescriptorFactory> xKeyFactory(_rxSup->getKeys(),UNO_QUERY);
1232 : OSL_ENSURE(xKeyFactory.is(),"No XDataDescriptorFactory Interface!");
1233 0 : if ( !xKeyFactory.is() )
1234 0 : return;
1235 0 : Reference<XAppend> xAppend(xKeyFactory,UNO_QUERY);
1236 : OSL_ENSURE(xAppend.is(),"No XAppend Interface!");
1237 :
1238 0 : Reference<XPropertySet> xKey = xKeyFactory->createDataDescriptor();
1239 : OSL_ENSURE(xKey.is(),"Key is null!");
1240 0 : xKey->setPropertyValue(PROPERTY_TYPE,makeAny(KeyType::PRIMARY));
1241 :
1242 0 : Reference<XColumnsSupplier> xColSup(xKey,UNO_QUERY);
1243 0 : if(xColSup.is())
1244 : {
1245 0 : appendColumns(xColSup,_pVec,sal_True);
1246 0 : Reference<XNameAccess> xColumns = xColSup->getColumns();
1247 0 : if(xColumns.is() && xColumns->getElementNames().getLength())
1248 0 : xAppend->appendByDescriptor(xKey);
1249 0 : }
1250 :
1251 : }
1252 : // -----------------------------------------------------------------------------
1253 0 : Reference< XPropertySet > OCopyTableWizard::createView() const
1254 : {
1255 : SAL_INFO("dbaccess.ui", "OCopyTableWizard::createView" );
1256 0 : OUString sCommand( m_rSourceObject.getSelectStatement() );
1257 : OSL_ENSURE( !sCommand.isEmpty(), "OCopyTableWizard::createView: no statement in the source object!" );
1258 : // there are legitimate cases in which getSelectStatement does not provide a statement,
1259 : // but in all those cases, this method here should never be called.
1260 0 : return ::dbaui::createView( m_sName, m_xDestConnection, sCommand );
1261 : }
1262 : // -----------------------------------------------------------------------------
1263 0 : Reference< XPropertySet > OCopyTableWizard::createTable()
1264 : {
1265 : SAL_INFO("dbaccess.ui", "OCopyTableWizard::createTable" );
1266 0 : Reference< XPropertySet > xTable;
1267 :
1268 0 : Reference<XTablesSupplier> xSup( m_xDestConnection, UNO_QUERY );
1269 0 : Reference< XNameAccess > xTables;
1270 0 : if(xSup.is())
1271 0 : xTables = xSup->getTables();
1272 0 : if ( getOperation() != CopyTableOperation::AppendData )
1273 : {
1274 0 : Reference<XDataDescriptorFactory> xFact(xTables,UNO_QUERY);
1275 : OSL_ENSURE(xFact.is(),"No XDataDescriptorFactory available!");
1276 0 : if(!xFact.is())
1277 0 : return NULL;
1278 :
1279 0 : xTable = xFact->createDataDescriptor();
1280 : OSL_ENSURE(xTable.is(),"Could not create a new object!");
1281 0 : if(!xTable.is())
1282 0 : return NULL;
1283 :
1284 0 : OUString sCatalog,sSchema,sTable;
1285 0 : Reference< XDatabaseMetaData> xMetaData = m_xDestConnection->getMetaData();
1286 : ::dbtools::qualifiedNameComponents(xMetaData,
1287 : m_sName,
1288 : sCatalog,
1289 : sSchema,
1290 : sTable,
1291 0 : ::dbtools::eInDataManipulation);
1292 :
1293 0 : if ( sCatalog.isEmpty() && xMetaData->supportsCatalogsInTableDefinitions() )
1294 : {
1295 0 : sCatalog = m_xDestConnection->getCatalog();
1296 : }
1297 :
1298 0 : if ( sSchema.isEmpty() && xMetaData->supportsSchemasInTableDefinitions() )
1299 : {
1300 0 : sSchema = xMetaData->getUserName();
1301 : }
1302 :
1303 0 : xTable->setPropertyValue(PROPERTY_CATALOGNAME,makeAny(sCatalog));
1304 0 : xTable->setPropertyValue(PROPERTY_SCHEMANAME,makeAny(sSchema));
1305 0 : xTable->setPropertyValue(PROPERTY_NAME,makeAny(sTable));
1306 :
1307 0 : Reference< XColumnsSupplier > xSuppDestinationColumns( xTable, UNO_QUERY );
1308 : // now append the columns
1309 0 : const ODatabaseExport::TColumnVector* pVec = getDestVector();
1310 0 : appendColumns( xSuppDestinationColumns, pVec );
1311 : // now append the primary key
1312 0 : Reference<XKeysSupplier> xKeySup(xTable,UNO_QUERY);
1313 0 : appendKey(xKeySup,pVec);
1314 :
1315 0 : Reference<XAppend> xAppend(xTables,UNO_QUERY);
1316 0 : if(xAppend.is())
1317 0 : xAppend->appendByDescriptor(xTable);
1318 :
1319 : // xTable = NULL;
1320 : // we need to reget the table because after appending it it is no longer valid
1321 0 : if(xTables->hasByName(m_sName))
1322 0 : xTables->getByName(m_sName) >>= xTable;
1323 : else
1324 : {
1325 : OUString sComposedName(
1326 0 : ::dbtools::composeTableName( m_xDestConnection->getMetaData(), xTable, ::dbtools::eInDataManipulation, false, false, false ) );
1327 0 : if(xTables->hasByName(sComposedName))
1328 : {
1329 0 : xTables->getByName(sComposedName) >>= xTable;
1330 0 : m_sName = sComposedName;
1331 : }
1332 : else
1333 0 : xTable = NULL;
1334 : }
1335 0 : if(xTable.is())
1336 : {
1337 0 : xSuppDestinationColumns.set( xTable, UNO_QUERY_THROW );
1338 : // insert new table name into table filter
1339 0 : ::dbaui::appendToFilter( m_xDestConnection, m_sName, GetComponentContext(), this );
1340 :
1341 : // copy ui settings
1342 0 : m_rSourceObject.copyUISettingsTo( xTable );
1343 : //copy filter and sorting
1344 0 : m_rSourceObject.copyFilterAndSortingTo(m_xDestConnection,xTable);
1345 : // set column mappings
1346 0 : Reference<XNameAccess> xNameAccess = xSuppDestinationColumns->getColumns();
1347 0 : Sequence< OUString> aSeq = xNameAccess->getElementNames();
1348 0 : const OUString* pIter = aSeq.getConstArray();
1349 0 : const OUString* pEnd = pIter + aSeq.getLength();
1350 :
1351 0 : for(sal_Int32 nNewPos=1;pIter != pEnd;++pIter,++nNewPos)
1352 : {
1353 0 : ODatabaseExport::TColumns::const_iterator aDestIter = m_vDestColumns.find(*pIter);
1354 :
1355 0 : if ( aDestIter != m_vDestColumns.end() )
1356 : {
1357 0 : ODatabaseExport::TColumnVector::const_iterator aFind = ::std::find(m_aDestVec.begin(),m_aDestVec.end(),aDestIter);
1358 0 : sal_Int32 nPos = (aFind - m_aDestVec.begin())+1;
1359 :
1360 : ODatabaseExport::TPositions::iterator aPosFind = ::std::find_if(
1361 : m_vColumnPos.begin(),
1362 : m_vColumnPos.end(),
1363 0 : ::o3tl::compose1( ::std::bind2nd( ::std::equal_to< sal_Int32 >(), nPos ),
1364 : ::o3tl::select1st< ODatabaseExport::TPositions::value_type >()
1365 : )
1366 0 : );
1367 :
1368 0 : if ( m_vColumnPos.end() != aPosFind )
1369 : {
1370 0 : aPosFind->second = nNewPos;
1371 : OSL_ENSURE( m_vColumnTypes.size() > size_t( aPosFind - m_vColumnPos.begin() ),
1372 : "Invalid index for vector!" );
1373 0 : m_vColumnTypes[ aPosFind - m_vColumnPos.begin() ] = (*aFind)->second->GetType();
1374 : }
1375 : }
1376 0 : }
1377 0 : }
1378 : }
1379 0 : else if(xTables.is() && xTables->hasByName(m_sName))
1380 0 : xTables->getByName(m_sName) >>= xTable;
1381 :
1382 0 : return xTable;
1383 : }
1384 :
1385 : // -----------------------------------------------------------------------------
1386 0 : bool OCopyTableWizard::supportsPrimaryKey( const Reference< XConnection >& _rxConnection )
1387 : {
1388 : OSL_PRECOND( _rxConnection.is(), "OCopyTableWizard::supportsPrimaryKey: invalid connection!" );
1389 0 : if ( !_rxConnection.is() )
1390 0 : return false;
1391 :
1392 0 : ::dbtools::DatabaseMetaData aMetaData( _rxConnection );
1393 0 : return aMetaData.supportsPrimaryKeys();
1394 : }
1395 :
1396 : // -----------------------------------------------------------------------------
1397 0 : bool OCopyTableWizard::supportsViews( const Reference< XConnection >& _rxConnection )
1398 : {
1399 : SAL_INFO("dbaccess.ui", "OCopyTableWizard::supportsViews" );
1400 : OSL_PRECOND( _rxConnection.is(), "OCopyTableWizard::supportsViews: invalid connection!" );
1401 0 : if ( !_rxConnection.is() )
1402 0 : return false;
1403 :
1404 0 : bool bSupportsViews( false );
1405 : try
1406 : {
1407 0 : Reference< XDatabaseMetaData > xMetaData( _rxConnection->getMetaData(), UNO_SET_THROW );
1408 0 : Reference< XViewsSupplier > xViewSups( _rxConnection, UNO_QUERY );
1409 0 : bSupportsViews = xViewSups.is();
1410 0 : if ( !bSupportsViews )
1411 : {
1412 : try
1413 : {
1414 0 : Reference< XResultSet > xRs( xMetaData->getTableTypes(), UNO_SET_THROW );
1415 0 : Reference< XRow > xRow( xRs, UNO_QUERY_THROW );
1416 0 : while ( xRs->next() )
1417 : {
1418 0 : OUString sValue = xRow->getString( 1 );
1419 0 : if ( !xRow->wasNull() && sValue.equalsIgnoreAsciiCase("View") )
1420 : {
1421 0 : bSupportsViews = true;
1422 0 : break;
1423 : }
1424 0 : }
1425 : }
1426 0 : catch( const SQLException& )
1427 : {
1428 : DBG_UNHANDLED_EXCEPTION();
1429 : }
1430 0 : }
1431 : }
1432 0 : catch( const Exception& )
1433 : {
1434 : DBG_UNHANDLED_EXCEPTION();
1435 : }
1436 0 : return bSupportsViews;
1437 : }
1438 :
1439 : // -----------------------------------------------------------------------------
1440 0 : sal_Int32 OCopyTableWizard::getMaxColumnNameLength() const
1441 : {
1442 : SAL_INFO("dbaccess.ui", "OCopyTableWizard::getMaxColumnNameLength" );
1443 0 : sal_Int32 nLen = 0;
1444 0 : if ( m_xDestConnection.is() )
1445 : {
1446 : try
1447 : {
1448 0 : Reference< XDatabaseMetaData > xMetaData( m_xDestConnection->getMetaData(), UNO_SET_THROW );
1449 0 : nLen = xMetaData->getMaxColumnNameLength();
1450 : }
1451 0 : catch(const Exception&)
1452 : {
1453 : DBG_UNHANDLED_EXCEPTION();
1454 : }
1455 : }
1456 0 : return nLen;
1457 : }
1458 : // -----------------------------------------------------------------------------
1459 0 : void OCopyTableWizard::setOperation( const sal_Int16 _nOperation )
1460 : {
1461 : SAL_INFO("dbaccess.ui", "OCopyTableWizard::setOperation" );
1462 0 : m_nOperation = _nOperation;
1463 0 : }
1464 : // -----------------------------------------------------------------------------
1465 0 : sal_Int16 OCopyTableWizard::getOperation() const
1466 : {
1467 : SAL_INFO("dbaccess.ui", "OCopyTableWizard::getOperation" );
1468 0 : return m_nOperation;
1469 : }
1470 : // -----------------------------------------------------------------------------
1471 0 : OUString OCopyTableWizard::convertColumnName(const TColumnFindFunctor& _rCmpFunctor,
1472 : const OUString& _sColumnName,
1473 : const OUString& _sExtraChars,
1474 : sal_Int32 _nMaxNameLen)
1475 : {
1476 : SAL_INFO("dbaccess.ui", "OCopyTableWizard::convertColumnName" );
1477 0 : OUString sAlias = _sColumnName;
1478 0 : if ( isSQL92CheckEnabled( m_xDestConnection ) )
1479 0 : sAlias = ::dbtools::convertName2SQLName(_sColumnName,_sExtraChars);
1480 0 : if((_nMaxNameLen && sAlias.getLength() > _nMaxNameLen) || _rCmpFunctor(sAlias))
1481 : {
1482 0 : sal_Int32 nDiff = 1;
1483 0 : do
1484 : {
1485 0 : ++nDiff;
1486 0 : if(_nMaxNameLen && sAlias.getLength() >= _nMaxNameLen)
1487 0 : sAlias = sAlias.copy(0,sAlias.getLength() - (sAlias.getLength()-_nMaxNameLen+nDiff));
1488 :
1489 0 : OUString sName(sAlias);
1490 0 : sal_Int32 nPos = 1;
1491 0 : sName += OUString::valueOf(nPos);
1492 :
1493 0 : while(_rCmpFunctor(sName))
1494 : {
1495 0 : sName = sAlias;
1496 0 : sName += OUString::valueOf(++nPos);
1497 : }
1498 0 : sAlias = sName;
1499 : // we have to check again, it could happen that the name is already to long
1500 : }
1501 0 : while(_nMaxNameLen && sAlias.getLength() > _nMaxNameLen);
1502 : }
1503 : OSL_ENSURE(m_mNameMapping.find(_sColumnName) == m_mNameMapping.end(),"name doubled!");
1504 0 : m_mNameMapping[_sColumnName] = sAlias;
1505 0 : return sAlias;
1506 : }
1507 :
1508 : // -----------------------------------------------------------------------------
1509 0 : void OCopyTableWizard::removeColumnNameFromNameMap(const OUString& _sName)
1510 : {
1511 : SAL_INFO("dbaccess.ui", "OCopyTableWizard::removeColumnNameFromNameMap" );
1512 0 : m_mNameMapping.erase(_sName);
1513 0 : }
1514 :
1515 : // -----------------------------------------------------------------------------
1516 0 : sal_Bool OCopyTableWizard::supportsType(sal_Int32 _nDataType,sal_Int32& _rNewDataType)
1517 : {
1518 : SAL_INFO("dbaccess.ui", "OCopyTableWizard::supportsType" );
1519 0 : sal_Bool bRet = m_aDestTypeInfo.find(_nDataType) != m_aDestTypeInfo.end();
1520 0 : if ( bRet )
1521 0 : _rNewDataType = _nDataType;
1522 0 : return bRet;
1523 : }
1524 :
1525 : // -----------------------------------------------------------------------------
1526 0 : TOTypeInfoSP OCopyTableWizard::convertType(const TOTypeInfoSP& _pType,sal_Bool& _bNotConvert)
1527 : {
1528 : SAL_INFO("dbaccess.ui", "OCopyTableWizard::convertType" );
1529 0 : if ( !m_bInterConnectionCopy )
1530 : // no need to convert if the source and destination connection are the same
1531 0 : return _pType;
1532 :
1533 : sal_Bool bForce;
1534 0 : TOTypeInfoSP pType = ::dbaui::getTypeInfoFromType(m_aDestTypeInfo,_pType->nType,_pType->aTypeName,_pType->aCreateParams,_pType->nPrecision,_pType->nMaximumScale,_pType->bAutoIncrement,bForce);
1535 0 : if ( !pType.get() || bForce )
1536 : { // no type found so we have to find the correct one ourself
1537 0 : sal_Int32 nDefaultType = DataType::VARCHAR;
1538 0 : switch(_pType->nType)
1539 : {
1540 : case DataType::TINYINT:
1541 0 : if(supportsType(DataType::SMALLINT,nDefaultType))
1542 0 : break;
1543 : // run through
1544 : case DataType::SMALLINT:
1545 0 : if(supportsType(DataType::INTEGER,nDefaultType))
1546 0 : break;
1547 : // run through
1548 : case DataType::INTEGER:
1549 0 : if(supportsType(DataType::FLOAT,nDefaultType))
1550 0 : break;
1551 : // run through
1552 : case DataType::FLOAT:
1553 0 : if(supportsType(DataType::REAL,nDefaultType))
1554 0 : break;
1555 : // run through
1556 : case DataType::DATE:
1557 : case DataType::TIME:
1558 0 : if( DataType::DATE == _pType->nType || DataType::TIME == _pType->nType )
1559 : {
1560 0 : if(supportsType(DataType::TIMESTAMP,nDefaultType))
1561 0 : break;
1562 : }
1563 : // run through
1564 : case DataType::TIMESTAMP:
1565 : case DataType::REAL:
1566 : case DataType::BIGINT:
1567 0 : if ( supportsType(DataType::DOUBLE,nDefaultType) )
1568 0 : break;
1569 : // run through
1570 : case DataType::DOUBLE:
1571 0 : if ( supportsType(DataType::NUMERIC,nDefaultType) )
1572 0 : break;
1573 : // run through
1574 : case DataType::NUMERIC:
1575 0 : supportsType(DataType::DECIMAL,nDefaultType);
1576 0 : break;
1577 : case DataType::DECIMAL:
1578 0 : if ( supportsType(DataType::NUMERIC,nDefaultType) )
1579 0 : break;
1580 0 : if ( supportsType(DataType::DOUBLE,nDefaultType) )
1581 0 : break;
1582 0 : break;
1583 : case DataType::VARCHAR:
1584 0 : if ( supportsType(DataType::LONGVARCHAR,nDefaultType) )
1585 0 : break;
1586 0 : break;
1587 : case DataType::LONGVARCHAR:
1588 0 : if ( supportsType(DataType::CLOB,nDefaultType) )
1589 0 : break;
1590 0 : break;
1591 : case DataType::BINARY:
1592 0 : if ( supportsType(DataType::VARBINARY,nDefaultType) )
1593 0 : break;
1594 0 : break;
1595 : case DataType::VARBINARY:
1596 0 : if ( supportsType(DataType::LONGVARBINARY,nDefaultType) )
1597 0 : break;
1598 0 : break;
1599 : case DataType::LONGVARBINARY:
1600 0 : if ( supportsType(DataType::BLOB,nDefaultType) )
1601 0 : break;
1602 0 : if ( supportsType(DataType::LONGVARCHAR,nDefaultType) )
1603 0 : break;
1604 0 : if ( supportsType(DataType::CLOB,nDefaultType) )
1605 0 : break;
1606 0 : break;
1607 : default:
1608 0 : nDefaultType = DataType::VARCHAR;
1609 : }
1610 0 : pType = ::dbaui::getTypeInfoFromType(m_aDestTypeInfo,nDefaultType,_pType->aTypeName,_pType->aCreateParams,_pType->nPrecision,_pType->nMaximumScale,_pType->bAutoIncrement,bForce);
1611 0 : if ( !pType.get() )
1612 : {
1613 0 : _bNotConvert = sal_False;
1614 0 : OUString sCreate("x");
1615 0 : pType = ::dbaui::getTypeInfoFromType(m_aDestTypeInfo,DataType::VARCHAR,_pType->aTypeName,sCreate,50,0,sal_False,bForce);
1616 0 : if ( !pType.get() )
1617 0 : pType = m_pTypeInfo;
1618 : }
1619 0 : else if ( bForce )
1620 0 : _bNotConvert = sal_False;
1621 : }
1622 0 : return pType;
1623 : }
1624 : // -----------------------------------------------------------------------------
1625 0 : OUString OCopyTableWizard::createUniqueName(const OUString& _sName)
1626 : {
1627 : SAL_INFO("dbaccess.ui", "OCopyTableWizard::createUniqueName" );
1628 0 : OUString sName = _sName;
1629 0 : Sequence< OUString > aColumnNames( m_rSourceObject.getColumnNames() );
1630 0 : if ( aColumnNames.getLength() )
1631 0 : sName = ::dbtools::createUniqueName( aColumnNames, sName, sal_False );
1632 : else
1633 : {
1634 0 : if ( m_vSourceColumns.find(sName) != m_vSourceColumns.end())
1635 : {
1636 0 : sal_Int32 nPos = 0;
1637 0 : while(m_vSourceColumns.find(sName) != m_vSourceColumns.end())
1638 : {
1639 0 : sName = _sName;
1640 0 : sName += OUString::valueOf(++nPos);
1641 : }
1642 : }
1643 : }
1644 0 : return sName;
1645 : }
1646 : // -----------------------------------------------------------------------------
1647 0 : void OCopyTableWizard::showColumnTypeNotSupported(const OUString& _rColumnName)
1648 : {
1649 : SAL_INFO("dbaccess.ui", "OCopyTableWizard::showColumnTypeNotSupported" );
1650 0 : String sMessage( ModuleRes( STR_UNKNOWN_TYPE_FOUND ) );
1651 0 : sMessage.SearchAndReplaceAscii("#1",_rColumnName);
1652 0 : showError(sMessage);
1653 0 : }
1654 : //-------------------------------------------------------------------------------
1655 0 : void OCopyTableWizard::showError(const OUString& _sErrorMesage)
1656 : {
1657 0 : SQLExceptionInfo aInfo(_sErrorMesage);
1658 0 : showError(aInfo.get());
1659 0 : }
1660 : //-------------------------------------------------------------------------------
1661 0 : void OCopyTableWizard::showError(const Any& _aError)
1662 : {
1663 0 : if ( _aError.hasValue() && m_xInteractionHandler.is() )
1664 : {
1665 : try
1666 : {
1667 0 : ::rtl::Reference< ::comphelper::OInteractionRequest > xRequest( new ::comphelper::OInteractionRequest( _aError ) );
1668 0 : m_xInteractionHandler->handle( xRequest.get() );
1669 : }
1670 0 : catch( const Exception& )
1671 : {
1672 : DBG_UNHANDLED_EXCEPTION();
1673 : }
1674 : }
1675 12 : }
1676 :
1677 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|