Update ooo320-m1
[ooovba.git] / dbaccess / source / ui / misc / WCopyTable.cxx
blob79ac5958e6d96cb18de98be06e97405e3c63a3e2
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: WCopyTable.cxx,v $
10 * $Revision: 1.58 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dbaccess.hxx"
34 #include "dbu_misc.hrc"
35 #include "dbustrings.hrc"
36 #include "moduledbu.hxx"
37 #include "sqlmessage.hxx"
38 #include "UITools.hxx"
39 #include "WColumnSelect.hxx"
40 #include "WCopyTable.hxx"
41 #include "WCPage.hxx"
42 #include "WExtendPages.hxx"
43 #include "WizardPages.hrc"
44 #include "WNameMatch.hxx"
45 #include "WTypeSelect.hxx"
47 /** === begin UNO includes === **/
48 #include <com/sun/star/sdb/application/CopyTableOperation.hpp>
49 #include <com/sun/star/sdbc/ColumnValue.hpp>
50 #include <com/sun/star/sdbc/DataType.hpp>
51 #include <com/sun/star/sdbc/XResultSet.hpp>
52 #include <com/sun/star/sdbc/XRow.hpp>
53 #include <com/sun/star/sdbcx/KeyType.hpp>
54 #include <com/sun/star/sdbcx/XAppend.hpp>
55 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
56 #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
57 #include <com/sun/star/sdbcx/XKeysSupplier.hpp>
58 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
59 #include <com/sun/star/sdbcx/XViewsSupplier.hpp>
60 #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
61 /** === end UNO includes === **/
63 #include <comphelper/extract.hxx>
64 #include <comphelper/types.hxx>
65 #include <connectivity/dbtools.hxx>
67 #include <rtl/logfile.hxx>
68 #include <rtl/ustrbuf.hxx>
69 #include <tools/debug.hxx>
70 #include <tools/diagnose_ex.h>
71 #include <vcl/lstbox.hxx>
72 #include <vcl/msgbox.hxx>
73 #include <vcl/waitobj.hxx>
75 #include <functional>
77 using namespace ::dbaui;
78 using namespace ::com::sun::star::uno;
79 using namespace ::com::sun::star::beans;
80 using namespace ::com::sun::star::container;
81 using namespace ::com::sun::star::util;
82 using namespace ::com::sun::star::sdbc;
83 using namespace ::com::sun::star::sdbcx;
84 using namespace ::com::sun::star::lang;
86 namespace CopyTableOperation = ::com::sun::star::sdb::application::CopyTableOperation;
88 #define MAX_PAGES 4 // max. Pages die angezeigt werden
90 DBG_NAME(OCopyTableWizard)
91 namespace
93 //....................................................................
94 void clearColumns(ODatabaseExport::TColumns& _rColumns, ODatabaseExport::TColumnVector& _rColumnsVec)
96 ODatabaseExport::TColumns::iterator aIter = _rColumns.begin();
97 ODatabaseExport::TColumns::iterator aEnd = _rColumns.end();
99 for(;aIter != aEnd;++aIter)
100 delete aIter->second;
102 _rColumnsVec.clear();
103 _rColumns.clear();
107 //========================================================================
108 //= ICopyTableSourceObject
109 //========================================================================
110 //------------------------------------------------------------------------
111 ICopyTableSourceObject::~ICopyTableSourceObject()
115 //========================================================================
116 //= ObjectCopySource
117 //========================================================================
118 //------------------------------------------------------------------------
119 ObjectCopySource::ObjectCopySource( const Reference< XConnection >& _rxConnection, const Reference< XPropertySet >& _rxObject )
120 :m_xConnection( _rxConnection, UNO_SET_THROW )
121 ,m_xMetaData( _rxConnection->getMetaData(), UNO_SET_THROW )
122 ,m_xObject( _rxObject, UNO_SET_THROW )
123 ,m_xObjectPSI( _rxObject->getPropertySetInfo(), UNO_SET_THROW )
124 ,m_xObjectColumns( Reference< XColumnsSupplier >( _rxObject, UNO_QUERY_THROW )->getColumns(), UNO_SET_THROW )
128 //------------------------------------------------------------------------
129 ::rtl::OUString ObjectCopySource::getQualifiedObjectName() const
131 ::rtl::OUString sName;
133 if ( !m_xObjectPSI->hasPropertyByName( PROPERTY_COMMAND ) )
134 sName = ::dbtools::composeTableName( m_xMetaData, m_xObject, ::dbtools::eInDataManipulation, false, false, false );
135 else
136 m_xObject->getPropertyValue( PROPERTY_NAME ) >>= sName;
137 return sName;
140 //------------------------------------------------------------------------
141 bool ObjectCopySource::isView() const
143 bool bIsView = false;
146 if ( m_xObjectPSI->hasPropertyByName( PROPERTY_TYPE ) )
148 ::rtl::OUString sObjectType;
149 OSL_VERIFY( m_xObject->getPropertyValue( PROPERTY_TYPE ) >>= sObjectType );
150 bIsView = sObjectType.equalsAscii( "VIEW" );
153 catch( const Exception& )
155 DBG_UNHANDLED_EXCEPTION();
157 return bIsView;
160 //------------------------------------------------------------------------
161 void ObjectCopySource::copyUISettingsTo( const Reference< XPropertySet >& _rxObject ) const
163 const ::rtl::OUString aCopyProperties[] = {
164 PROPERTY_FONT, PROPERTY_ROW_HEIGHT, PROPERTY_TEXTCOLOR,PROPERTY_TEXTLINECOLOR,PROPERTY_TEXTEMPHASIS,PROPERTY_TEXTRELIEF
166 for ( size_t i=0; i < sizeof( aCopyProperties ) / sizeof( aCopyProperties[0] ); ++i )
168 if ( m_xObjectPSI->hasPropertyByName( aCopyProperties[i] ) )
169 _rxObject->setPropertyValue( aCopyProperties[i], m_xObject->getPropertyValue( aCopyProperties[i] ) );
172 //------------------------------------------------------------------------
173 void ObjectCopySource::copyFilterAndSortingTo( const Reference< XConnection >& _xConnection,const Reference< XPropertySet >& _rxObject ) const
175 ::std::pair< ::rtl::OUString, ::rtl::OUString > aProperties[] = {
176 ::std::pair< ::rtl::OUString, ::rtl::OUString >(PROPERTY_FILTER,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" AND ")))
177 ,::std::pair< ::rtl::OUString, ::rtl::OUString >(PROPERTY_ORDER,::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" ORDER BY ")))
180 size_t i = 0;
184 const String sSourceName = (::dbtools::composeTableNameForSelect(m_xConnection,m_xObject) + ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(".")));
185 const ::rtl::OUString sTargetName = ::dbtools::composeTableNameForSelect(_xConnection,_rxObject);
186 const String sTargetNameTemp = (sTargetName + ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(".")));
188 ::rtl::OUString sStatement(RTL_CONSTASCII_USTRINGPARAM("SELECT * FROM "));
189 sStatement += sTargetName;
190 sStatement += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" WHERE 0=1"));
193 for ( i=0; i < sizeof( aProperties ) / sizeof( aProperties[0] ); ++i )
195 if ( m_xObjectPSI->hasPropertyByName( aProperties[i].first ) )
197 ::rtl::OUString sFilter;
198 m_xObject->getPropertyValue( aProperties[i].first ) >>= sFilter;
199 if ( sFilter.getLength() )
201 sStatement += aProperties[i].second;
202 String sReplace = sFilter;
203 sReplace.SearchAndReplace(sSourceName,sTargetNameTemp);
204 sFilter = sReplace;
205 _rxObject->setPropertyValue( aProperties[i].first, makeAny(sFilter) );
206 sStatement += sFilter;
211 _xConnection->createStatement()->executeQuery(sStatement);
213 if ( m_xObjectPSI->hasPropertyByName( PROPERTY_APPLYFILTER ) )
214 _rxObject->setPropertyValue( PROPERTY_APPLYFILTER, m_xObject->getPropertyValue( PROPERTY_APPLYFILTER ) );
216 catch(Exception&)
220 //------------------------------------------------------------------------
221 Sequence< ::rtl::OUString > ObjectCopySource::getColumnNames() const
223 return m_xObjectColumns->getElementNames();
226 //------------------------------------------------------------------------
227 Sequence< ::rtl::OUString > ObjectCopySource::getPrimaryKeyColumnNames() const
229 Reference<XKeysSupplier> xSup(m_xObject,UNO_QUERY);
230 Reference< XIndexAccess> xKeys;
231 if(xSup.is() )
232 xKeys = xSup->getKeys();
234 ::std::vector< Reference< XNameAccess > > aPrimaryKeyColumns( ::dbaui::getKeyColumns( xKeys, KeyType::PRIMARY ) );
235 OSL_ENSURE( ( aPrimaryKeyColumns.size() == 1 ) || aPrimaryKeyColumns.empty(),
236 "ObjectCopySource::getPrimaryKeyColumnNames: more than one primary key?!" );
238 Reference< XNameAccess > xKeyCols;
239 if ( !aPrimaryKeyColumns.empty() )
240 xKeyCols = aPrimaryKeyColumns[0];
242 Sequence< ::rtl::OUString > aKeyColNames;
243 if ( xKeyCols.is() )
244 aKeyColNames = xKeyCols->getElementNames();
245 return aKeyColNames;
248 //------------------------------------------------------------------------
249 OFieldDescription* ObjectCopySource::createFieldDescription( const ::rtl::OUString& _rColumnName ) const
251 Reference< XPropertySet > xColumn( m_xObjectColumns->getByName( _rColumnName ), UNO_QUERY_THROW );
252 return new OFieldDescription( xColumn );
254 //------------------------------------------------------------------------
255 ::rtl::OUString ObjectCopySource::getSelectStatement() const
257 ::rtl::OUString sSelectStatement;
258 if ( m_xObjectPSI->hasPropertyByName( PROPERTY_COMMAND ) )
259 { // query
260 OSL_VERIFY( m_xObject->getPropertyValue( PROPERTY_COMMAND ) >>= sSelectStatement );
262 else
263 { // table
264 ::rtl::OUStringBuffer aSQL;
265 aSQL.appendAscii( "SELECT " );
267 // we need to create the sql stmt with column names
268 // otherwise it is possible that names don't match
269 const ::rtl::OUString sQuote = m_xMetaData->getIdentifierQuoteString();
271 Sequence< ::rtl::OUString > aColumnNames = getColumnNames();
272 const ::rtl::OUString* pColumnName = aColumnNames.getConstArray();
273 const ::rtl::OUString* pEnd = pColumnName + aColumnNames.getLength();
274 for ( ; pColumnName != pEnd; )
276 aSQL.append( ::dbtools::quoteName( sQuote, *pColumnName++ ) );
278 if ( pColumnName == pEnd )
279 aSQL.appendAscii( " " );
280 else
281 aSQL.appendAscii( ", " );
284 aSQL.appendAscii( "FROM " );
285 aSQL.append( ::dbtools::composeTableNameForSelect( m_xConnection, m_xObject ) );
287 sSelectStatement = aSQL.makeStringAndClear();
290 return sSelectStatement;
293 //------------------------------------------------------------------------
294 ::utl::SharedUNOComponent< XPreparedStatement > ObjectCopySource::getPreparedSelectStatement() const
296 ::utl::SharedUNOComponent< XPreparedStatement > xStatement(
297 m_xConnection->prepareStatement( getSelectStatement() ),
298 ::utl::SharedUNOComponent< XPreparedStatement >::TakeOwnership
300 return xStatement;
303 //========================================================================
304 //= NamedTableCopySource
305 //========================================================================
306 //------------------------------------------------------------------------
307 NamedTableCopySource::NamedTableCopySource( const Reference< XConnection >& _rxConnection, const ::rtl::OUString& _rTableName )
308 :m_xConnection( _rxConnection, UNO_SET_THROW )
309 ,m_xMetaData( _rxConnection->getMetaData(), UNO_SET_THROW )
310 ,m_sTableName( _rTableName )
311 ,m_aColumnInfo()
313 ::dbtools::qualifiedNameComponents( m_xMetaData, m_sTableName, m_sTableCatalog, m_sTableSchema, m_sTableBareName, ::dbtools::eComplete );
314 impl_ensureColumnInfo_throw();
317 //------------------------------------------------------------------------
318 ::rtl::OUString NamedTableCopySource::getQualifiedObjectName() const
320 return m_sTableName;
323 //------------------------------------------------------------------------
324 bool NamedTableCopySource::isView() const
326 ::rtl::OUString sTableType;
329 Reference< XResultSet > xTableDesc( m_xMetaData->getTables( makeAny( m_sTableCatalog ), m_sTableSchema, m_sTableBareName,
330 Sequence< ::rtl::OUString >() ) );
331 Reference< XRow > xTableDescRow( xTableDesc, UNO_QUERY_THROW );
332 OSL_VERIFY( xTableDesc->next() );
333 sTableType = xTableDescRow->getString( 4 );
334 OSL_ENSURE( !xTableDescRow->wasNull(), "NamedTableCopySource::isView: invalid table type!" );
336 catch( const Exception& )
338 DBG_UNHANDLED_EXCEPTION();
340 return sTableType.equalsAscii( "VIEW" );
343 //------------------------------------------------------------------------
344 void NamedTableCopySource::copyUISettingsTo( const Reference< XPropertySet >& /*_rxObject*/ ) const
346 // not supported: we do not have UI settings to copy
348 // -----------------------------------------------------------------------------
349 void NamedTableCopySource::copyFilterAndSortingTo( const Reference< XConnection >& ,const Reference< XPropertySet >& /*_rxObject*/ ) const
352 //------------------------------------------------------------------------
353 void NamedTableCopySource::impl_ensureColumnInfo_throw()
355 if ( !m_aColumnInfo.empty() )
356 return;
358 Reference< XResultSetMetaDataSupplier > xStatementMetaSupp( impl_ensureStatement_throw().getTyped(), UNO_QUERY_THROW );
359 Reference< XResultSetMetaData > xStatementMeta( xStatementMetaSupp->getMetaData(), UNO_SET_THROW );
361 sal_Int32 nColCount( xStatementMeta->getColumnCount() );
362 for ( sal_Int32 i = 1; i <= nColCount; ++i )
364 OFieldDescription aDesc;
366 aDesc.SetName( xStatementMeta->getColumnName( i ) );
367 aDesc.SetDescription( xStatementMeta->getColumnLabel( i ) );
368 aDesc.SetTypeValue( xStatementMeta->getColumnType( i ) );
369 aDesc.SetTypeName( xStatementMeta->getColumnTypeName( i ) );
370 aDesc.SetPrecision( xStatementMeta->getPrecision( i ) );
371 aDesc.SetScale( xStatementMeta->getScale( i ) );
372 aDesc.SetIsNullable( xStatementMeta->isNullable( i ) );
373 aDesc.SetCurrency( xStatementMeta->isCurrency( i ) );
374 aDesc.SetAutoIncrement( xStatementMeta->isAutoIncrement( i ) );
376 m_aColumnInfo.push_back( aDesc );
380 //------------------------------------------------------------------------
381 ::utl::SharedUNOComponent< XPreparedStatement > NamedTableCopySource::impl_ensureStatement_throw()
383 if ( !m_xStatement.is() )
384 m_xStatement.set( m_xConnection->prepareStatement( getSelectStatement() ), UNO_SET_THROW );
385 return m_xStatement;
388 //------------------------------------------------------------------------
389 Sequence< ::rtl::OUString > NamedTableCopySource::getColumnNames() const
391 Sequence< ::rtl::OUString > aNames( m_aColumnInfo.size() );
392 for ( ::std::vector< OFieldDescription >::const_iterator col = m_aColumnInfo.begin();
393 col != m_aColumnInfo.end();
394 ++col
396 aNames[ col - m_aColumnInfo.begin() ] = col->GetName();
398 return aNames;
401 //------------------------------------------------------------------------
402 Sequence< ::rtl::OUString > NamedTableCopySource::getPrimaryKeyColumnNames() const
404 Sequence< ::rtl::OUString > aPKColNames;
408 Reference< XResultSet > xPKDesc( m_xMetaData->getPrimaryKeys( makeAny( m_sTableCatalog ), m_sTableSchema, m_sTableBareName ) );
409 Reference< XRow > xPKDescRow( xPKDesc, UNO_QUERY_THROW );
410 while ( xPKDesc->next() )
412 sal_Int32 len( aPKColNames.getLength() );
413 aPKColNames.realloc( len + 1 );
414 aPKColNames[ len ] = xPKDescRow->getString( 4 ); // COLUMN_NAME
417 catch( const Exception& )
419 DBG_UNHANDLED_EXCEPTION();
422 return aPKColNames;
425 //------------------------------------------------------------------------
426 OFieldDescription* NamedTableCopySource::createFieldDescription( const ::rtl::OUString& _rColumnName ) const
428 for ( ::std::vector< OFieldDescription >::const_iterator col = m_aColumnInfo.begin();
429 col != m_aColumnInfo.end();
430 ++col
432 if ( col->GetName() == _rColumnName )
433 return new OFieldDescription( *col );
435 return NULL;
437 //------------------------------------------------------------------------
438 ::rtl::OUString NamedTableCopySource::getSelectStatement() const
440 ::rtl::OUStringBuffer aSQL;
441 aSQL.appendAscii( "SELECT * FROM " );
443 aSQL.append( ::dbtools::composeTableNameForSelect( m_xConnection, m_sTableCatalog, m_sTableSchema, m_sTableBareName ) );
445 return aSQL.makeStringAndClear();
448 //------------------------------------------------------------------------
449 ::utl::SharedUNOComponent< XPreparedStatement > NamedTableCopySource::getPreparedSelectStatement() const
451 return const_cast< NamedTableCopySource* >( this )->impl_ensureStatement_throw();
454 // ========================================================
455 // DummyCopySource
456 // ========================================================
457 class DummyCopySource : public ICopyTableSourceObject
459 public:
460 DummyCopySource() { }
462 static const DummyCopySource& Instance();
464 // ICopyTableSourceObject overridables
465 virtual ::rtl::OUString getQualifiedObjectName() const;
466 virtual bool isView() const;
467 virtual void copyUISettingsTo( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxObject ) const;
468 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;
469 virtual ::com::sun::star::uno::Sequence< ::rtl::OUString >
470 getColumnNames() const;
471 virtual ::com::sun::star::uno::Sequence< ::rtl::OUString >
472 getPrimaryKeyColumnNames() const;
473 virtual OFieldDescription* createFieldDescription( const ::rtl::OUString& _rColumnName ) const;
474 virtual ::rtl::OUString getSelectStatement() const;
475 virtual ::utl::SharedUNOComponent< XPreparedStatement >
476 getPreparedSelectStatement() const;
479 //------------------------------------------------------------------------
480 const DummyCopySource& DummyCopySource::Instance()
482 static DummyCopySource s_aTheInstance;
483 return s_aTheInstance;
486 //------------------------------------------------------------------------
487 ::rtl::OUString DummyCopySource::getQualifiedObjectName() const
489 OSL_ENSURE( false, "DummyCopySource::getQualifiedObjectName: not to be called!" );
490 return ::rtl::OUString();
493 //------------------------------------------------------------------------
494 bool DummyCopySource::isView() const
496 OSL_ENSURE( false, "DummyCopySource::isView: not to be called!" );
497 return false;
500 //------------------------------------------------------------------------
501 void DummyCopySource::copyUISettingsTo( const Reference< XPropertySet >& /*_rxObject*/ ) const
503 // no support
505 // -----------------------------------------------------------------------------
506 void DummyCopySource::copyFilterAndSortingTo( const Reference< XConnection >& ,const Reference< XPropertySet >& /*_rxObject*/ ) const
509 //------------------------------------------------------------------------
510 Sequence< ::rtl::OUString > DummyCopySource::getColumnNames() const
512 return Sequence< ::rtl::OUString >();
515 //------------------------------------------------------------------------
516 Sequence< ::rtl::OUString > DummyCopySource::getPrimaryKeyColumnNames() const
518 OSL_ENSURE( false, "DummyCopySource::getPrimaryKeyColumnNames: not to be called!" );
519 return Sequence< ::rtl::OUString >();
522 //------------------------------------------------------------------------
523 OFieldDescription* DummyCopySource::createFieldDescription( const ::rtl::OUString& /*_rColumnName*/ ) const
525 OSL_ENSURE( false, "DummyCopySource::createFieldDescription: not to be called!" );
526 return NULL;
528 //------------------------------------------------------------------------
529 ::rtl::OUString DummyCopySource::getSelectStatement() const
531 OSL_ENSURE( false, "DummyCopySource::getSelectStatement: not to be called!" );
532 return ::rtl::OUString();
535 //------------------------------------------------------------------------
536 ::utl::SharedUNOComponent< XPreparedStatement > DummyCopySource::getPreparedSelectStatement() const
538 OSL_ENSURE( false, "DummyCopySource::getPreparedSelectStatement: not to be called!" );
539 return ::utl::SharedUNOComponent< XPreparedStatement >();
542 //------------------------------------------------------------------------
543 namespace
545 bool lcl_canCreateViewFor_nothrow( const Reference< XConnection >& _rxConnection )
547 Reference< XViewsSupplier > xSup( _rxConnection, UNO_QUERY );
548 Reference< XDataDescriptorFactory > xViewFac;
549 if ( xSup.is() )
550 xViewFac.set( xSup->getViews(), UNO_QUERY );
551 return xViewFac.is();
554 bool lcl_sameConnection_throw( const Reference< XConnection >& _rxLHS, const Reference< XConnection >& _rxRHS )
556 Reference< XDatabaseMetaData > xMetaLHS( _rxLHS->getMetaData(), UNO_QUERY_THROW );
557 Reference< XDatabaseMetaData > xMetaRHS( _rxRHS->getMetaData(), UNO_QUERY_THROW );
558 return xMetaLHS->getURL().equals( xMetaRHS->getURL() );
562 //========================================================================
563 //= OCopyTableWizard
564 //========================================================================
565 //------------------------------------------------------------------------
566 OCopyTableWizard::OCopyTableWizard( Window * pParent, const ::rtl::OUString& _rDefaultName, sal_Int16 _nOperation,
567 const ICopyTableSourceObject& _rSourceObject, const Reference< XConnection >& _xSourceConnection,
568 const Reference< XConnection >& _xConnection, const Reference< XMultiServiceFactory >& _rxORB )
569 : WizardDialog( pParent, ModuleRes(WIZ_RTFCOPYTABLE))
570 ,m_pbHelp( this , ModuleRes(PB_HELP))
571 ,m_pbCancel( this , ModuleRes(PB_CANCEL))
572 ,m_pbPrev( this , ModuleRes(PB_PREV))
573 ,m_pbNext( this , ModuleRes(PB_NEXT))
574 ,m_pbFinish( this , ModuleRes(PB_OK))
575 ,m_mNameMapping(_xConnection->getMetaData().is() && _xConnection->getMetaData()->supportsMixedCaseQuotedIdentifiers())
576 ,m_xDestConnection( _xConnection )
577 ,m_rSourceObject( _rSourceObject )
578 ,m_xFormatter( getNumberFormatter( _xConnection, _rxORB ) )
579 ,m_xFactory(_rxORB)
580 ,m_sTypeNames(ModuleRes(STR_TABLEDESIGN_DBFIELDTYPES))
581 ,m_nPageCount(0)
582 ,m_bDeleteSourceColumns(sal_True)
583 ,m_bInterConnectionCopy( _xSourceConnection != _xConnection )
584 ,m_sName( _rDefaultName )
585 ,m_nOperation( _nOperation )
586 ,m_ePressed( WIZARD_NONE )
587 ,m_bCreatePrimaryKeyColumn(sal_False)
589 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OCopyTableWizard::OCopyTableWizard" );
590 DBG_CTOR(OCopyTableWizard,NULL);
591 construct();
593 // extract table name
594 ::rtl::OUString sInitialTableName( _rDefaultName );
597 m_sSourceName = m_rSourceObject.getQualifiedObjectName();
598 OSL_ENSURE( m_sSourceName.getLength() > 0, "OCopyTableWizard::OCopyTableWizard: unable to retrieve the source object's name!" );
600 if ( !sInitialTableName.getLength() )
601 sInitialTableName = m_sSourceName;
603 if ( !m_sName.getLength() )
605 if ( _xSourceConnection == m_xDestConnection )
607 Reference< XTablesSupplier > xSup( m_xDestConnection, UNO_QUERY_THROW );
608 m_sName = ::dbtools::createUniqueName( xSup->getTables(), sInitialTableName, sal_False );
610 else
611 m_sName = sInitialTableName;
614 catch ( const Exception& )
616 m_sName = sInitialTableName;
619 ::dbaui::fillTypeInfo( _xSourceConnection, m_sTypeNames, m_aTypeInfo, m_aTypeInfoIndex );
620 ::dbaui::fillTypeInfo( m_xDestConnection, m_sTypeNames, m_aDestTypeInfo, m_aDestTypeInfoIndex );
621 impl_loadSourceData();
623 bool bAllowViews = true;
624 // if the source is a, don't allow creating views #100644# (oj)
625 // (fs: Hmm? A SELECT * FROM <view> would be created, where #100644# claims this is nonsense. Why?
626 if ( m_rSourceObject.isView() )
627 bAllowViews = false;
628 // no views if the target connection does not support creating them
629 if ( !lcl_canCreateViewFor_nothrow( m_xDestConnection ) )
630 bAllowViews = false;
631 // no views if we're copying to a different database
632 if ( !lcl_sameConnection_throw( _xSourceConnection, m_xDestConnection ) )
633 bAllowViews = false;
635 OCopyTable* pPage1( new OCopyTable( this ) );
636 pPage1->disallowUseHeaderLine();
637 if ( !bAllowViews )
638 pPage1->disallowViews();
639 pPage1->setCreateStyleAction();
640 AddWizardPage(pPage1);
642 AddWizardPage( new OWizNameMatching( this ) );
643 AddWizardPage( new OWizColumnSelect( this ) );
644 AddWizardPage( new OWizNormalExtend( this ) );
645 ActivatePage();
648 // -----------------------------------------------------------------------------
649 OCopyTableWizard::OCopyTableWizard( Window* pParent, const ::rtl::OUString& _rDefaultName, sal_Int16 _nOperation,
650 const ODatabaseExport::TColumns& _rSourceColumns, const ODatabaseExport::TColumnVector& _rSourceColVec,
651 const Reference< XConnection >& _xConnection, const Reference< XNumberFormatter >& _xFormatter,
652 TypeSelectionPageFactory _pTypeSelectionPageFactory, SvStream& _rTypeSelectionPageArg, const Reference< XMultiServiceFactory >& _rM )
653 :WizardDialog( pParent, ModuleRes(WIZ_RTFCOPYTABLE))
654 ,m_vSourceColumns(_rSourceColumns)
655 ,m_pbHelp( this , ModuleRes(PB_HELP))
656 ,m_pbCancel( this , ModuleRes(PB_CANCEL))
657 ,m_pbPrev( this , ModuleRes(PB_PREV))
658 ,m_pbNext( this , ModuleRes(PB_NEXT))
659 ,m_pbFinish( this , ModuleRes(PB_OK))
660 ,m_mNameMapping(_xConnection->getMetaData().is() && _xConnection->getMetaData()->supportsMixedCaseQuotedIdentifiers())
661 ,m_xDestConnection( _xConnection )
662 ,m_rSourceObject( DummyCopySource::Instance() )
663 ,m_xFormatter(_xFormatter)
664 ,m_xFactory(_rM)
665 ,m_sTypeNames(ModuleRes(STR_TABLEDESIGN_DBFIELDTYPES))
666 ,m_nPageCount(0)
667 ,m_bDeleteSourceColumns(sal_False)
668 ,m_bInterConnectionCopy( false )
669 ,m_sName(_rDefaultName)
670 ,m_nOperation( _nOperation )
671 ,m_ePressed( WIZARD_NONE )
672 ,m_bCreatePrimaryKeyColumn(sal_False)
674 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OCopyTableWizard::OCopyTableWizard" );
675 DBG_CTOR(OCopyTableWizard,NULL);
676 construct();
677 ODatabaseExport::TColumnVector::const_iterator aIter = _rSourceColVec.begin();
678 ODatabaseExport::TColumnVector::const_iterator aEnd = _rSourceColVec.end();
679 for (; aIter != aEnd ; ++aIter)
681 m_vSourceVec.push_back(m_vSourceColumns.find((*aIter)->first));
684 ::dbaui::fillTypeInfo( _xConnection, m_sTypeNames, m_aTypeInfo, m_aTypeInfoIndex );
685 ::dbaui::fillTypeInfo( _xConnection, m_sTypeNames, m_aDestTypeInfo, m_aDestTypeInfoIndex );
687 OCopyTable* pPage1( new OCopyTable( this ) );
688 pPage1->disallowViews();
689 pPage1->setCreateStyleAction();
690 AddWizardPage( pPage1 );
692 AddWizardPage( new OWizNameMatching( this ) );
693 AddWizardPage( new OWizColumnSelect( this ) );
694 AddWizardPage( (*_pTypeSelectionPageFactory)( this, _rTypeSelectionPageArg ) );
696 ActivatePage();
699 // -----------------------------------------------------------------------------
700 void OCopyTableWizard::construct()
702 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OCopyTableWizard::construct" );
703 AddButton( &m_pbHelp, WIZARDDIALOG_BUTTON_STDOFFSET_X );
704 AddButton( &m_pbCancel, WIZARDDIALOG_BUTTON_STDOFFSET_X );
705 AddButton( &m_pbPrev );
706 AddButton( &m_pbNext, WIZARDDIALOG_BUTTON_STDOFFSET_X );
707 AddButton( &m_pbFinish );
709 m_pbPrev.SetClickHdl( LINK( this, OCopyTableWizard, ImplPrevHdl ) );
710 m_pbNext.SetClickHdl( LINK( this, OCopyTableWizard, ImplNextHdl ) );
711 m_pbFinish.SetClickHdl( LINK( this, OCopyTableWizard, ImplOKHdl ) );
713 SetActivatePageHdl( LINK( this, OCopyTableWizard, ImplActivateHdl ) );
715 SetPrevButton( &m_pbPrev );
716 SetNextButton( &m_pbNext );
718 ShowButtonFixedLine( sal_True );
720 m_pbNext.GrabFocus();
722 if (m_vDestColumns.size())
723 // source is a html or rtf table
724 m_pbNext.SetStyle(m_pbFinish.GetStyle() | WB_DEFBUTTON);
725 else
726 m_pbFinish.SetStyle(m_pbFinish.GetStyle() | WB_DEFBUTTON);
728 FreeResource();
730 m_pTypeInfo = TOTypeInfoSP(new OTypeInfo());
731 m_pTypeInfo->aUIName = m_sTypeNames.GetToken(TYPE_OTHER);
732 m_bAddPKFirstTime = sal_True;
734 //------------------------------------------------------------------------
735 OCopyTableWizard::~OCopyTableWizard()
737 DBG_DTOR(OCopyTableWizard,NULL);
738 for ( ;; )
740 TabPage *pPage = GetPage(0);
741 if ( pPage == NULL )
742 break;
743 RemovePage( pPage );
744 delete pPage;
747 if ( m_bDeleteSourceColumns )
748 clearColumns(m_vSourceColumns,m_vSourceVec);
750 clearColumns(m_vDestColumns,m_aDestVec);
752 // clear the type information
753 m_aTypeInfoIndex.clear();
754 m_aTypeInfo.clear();
755 m_aDestTypeInfoIndex.clear();
757 // -----------------------------------------------------------------------
758 IMPL_LINK( OCopyTableWizard, ImplPrevHdl, PushButton*, EMPTYARG )
760 m_ePressed = WIZARD_PREV;
761 if ( GetCurLevel() )
763 if ( getOperation() != CopyTableOperation::AppendData )
765 if(GetCurLevel() == 2)
766 ShowPage(GetCurLevel()-2);
767 else
768 ShowPrevPage();
770 else
771 ShowPrevPage();
773 return 0;
776 // -----------------------------------------------------------------------
778 IMPL_LINK( OCopyTableWizard, ImplNextHdl, PushButton*, EMPTYARG )
780 m_ePressed = WIZARD_NEXT;
781 if ( GetCurLevel() < MAX_PAGES )
783 if ( getOperation() != CopyTableOperation::AppendData )
785 if(GetCurLevel() == 0)
786 ShowPage(GetCurLevel()+2);
787 else
788 ShowNextPage();
790 else
791 ShowNextPage();
793 return 0;
795 // -----------------------------------------------------------------------
796 sal_Bool OCopyTableWizard::CheckColumns(sal_Int32& _rnBreakPos)
798 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OCopyTableWizard::CheckColumns" );
799 sal_Bool bRet = sal_True;
800 m_vColumnPos.clear();
801 m_vColumnTypes.clear();
803 OSL_ENSURE( m_xDestConnection.is(), "OCopyTableWizard::CheckColumns: No connection!" );
804 //////////////////////////////////////////////////////////////////////
805 // Wenn Datenbank PrimaryKeys verarbeiten kann, PrimaryKey anlegen
806 if ( m_xDestConnection.is() )
808 sal_Bool bPKeyAllowed = supportsPrimaryKey();
810 sal_Bool bContainsColumns = !m_vDestColumns.empty();
812 if ( bPKeyAllowed && shouldCreatePrimaryKey() )
814 // add extra column for the primary key
815 TOTypeInfoSP pTypeInfo = queryPrimaryKeyType(m_aDestTypeInfo);
816 if ( pTypeInfo.get() )
818 if ( m_bAddPKFirstTime )
820 OFieldDescription* pField = new OFieldDescription();
821 pField->SetName(m_aKeyName);
822 pField->FillFromTypeInfo(pTypeInfo,sal_True,sal_True);
823 pField->SetPrimaryKey(sal_True);
824 m_bAddPKFirstTime = sal_False;
825 insertColumn(0,pField);
827 m_vColumnPos.push_back(ODatabaseExport::TPositions::value_type(1,1));
828 m_vColumnTypes.push_back(pTypeInfo->nType);
832 if ( bContainsColumns )
833 { // we have dest columns so look for the matching column
834 ODatabaseExport::TColumnVector::const_iterator aSrcIter = m_vSourceVec.begin();
835 ODatabaseExport::TColumnVector::const_iterator aSrcEnd = m_vSourceVec.end();
836 for(;aSrcIter != aSrcEnd;++aSrcIter)
838 ODatabaseExport::TColumns::iterator aDestIter = m_vDestColumns.find(m_mNameMapping[(*aSrcIter)->first]);
840 if ( aDestIter != m_vDestColumns.end() )
842 ODatabaseExport::TColumnVector::const_iterator aFind = ::std::find(m_aDestVec.begin(),m_aDestVec.end(),aDestIter);
843 sal_Int32 nPos = (aFind - m_aDestVec.begin())+1;
844 m_vColumnPos.push_back(ODatabaseExport::TPositions::value_type(nPos,nPos));
845 m_vColumnTypes.push_back((*aFind)->second->GetType());
847 else
849 m_vColumnPos.push_back( ODatabaseExport::TPositions::value_type( COLUMN_POSITION_NOT_FOUND, COLUMN_POSITION_NOT_FOUND ) );
850 m_vColumnTypes.push_back(0);
854 else
856 Reference< XDatabaseMetaData > xMetaData( m_xDestConnection->getMetaData() );
857 ::rtl::OUString sExtraChars = xMetaData->getExtraNameCharacters();
858 sal_Int32 nMaxNameLen = getMaxColumnNameLength();
860 ODatabaseExport::TColumnVector::const_iterator aSrcIter = m_vSourceVec.begin();
861 ODatabaseExport::TColumnVector::const_iterator aSrcEnd = m_vSourceVec.end();
862 for(_rnBreakPos=0;aSrcIter != aSrcEnd && bRet ;++aSrcIter,++_rnBreakPos)
864 OFieldDescription* pField = new OFieldDescription(*(*aSrcIter)->second);
865 pField->SetName(convertColumnName(TExportColumnFindFunctor(&m_vDestColumns),(*aSrcIter)->first,sExtraChars,nMaxNameLen));
866 TOTypeInfoSP pType = convertType((*aSrcIter)->second->getSpecialTypeInfo(),bRet);
867 pField->SetType(pType);
868 if ( !bPKeyAllowed )
869 pField->SetPrimaryKey(sal_False);
871 // now create a column
872 insertColumn(m_vDestColumns.size(),pField);
873 m_vColumnPos.push_back(ODatabaseExport::TPositions::value_type(m_vDestColumns.size(),m_vDestColumns.size()));
874 m_vColumnTypes.push_back((*aSrcIter)->second->GetType());
878 return bRet;
880 // -----------------------------------------------------------------------
881 IMPL_LINK( OCopyTableWizard, ImplOKHdl, OKButton*, EMPTYARG )
883 m_ePressed = WIZARD_FINISH;
884 sal_Bool bFinish = DeactivatePage() != 0;
886 if(bFinish)
888 WaitObject aWait(this);
889 switch(getOperation())
891 case CopyTableOperation::CopyDefinitionAndData:
892 case CopyTableOperation::CopyDefinitionOnly:
894 sal_Bool bOnFirstPage = GetCurLevel() == 0;
895 if ( bOnFirstPage )
897 // we came from the first page so we have to clear
898 // all column information already collected
899 clearDestColumns();
900 m_mNameMapping.clear();
902 sal_Int32 nBreakPos = 0;
903 sal_Bool bCheckOk = CheckColumns(nBreakPos);
904 if ( bOnFirstPage && !bCheckOk )
906 showColumnTypeNotSupported(m_vSourceVec[nBreakPos-1]->first);
907 OWizTypeSelect* pPage = static_cast<OWizTypeSelect*>(GetPage(3));
908 if ( pPage )
910 pPage->setDisplayRow(nBreakPos);
911 ShowPage(3);
912 return 0;
915 if ( m_xDestConnection.is() )
917 if ( supportsPrimaryKey() )
919 ODatabaseExport::TColumns::iterator aFind = ::std::find_if(m_vDestColumns.begin(),m_vDestColumns.end()
920 ,::std::compose1(::std::mem_fun(&OFieldDescription::IsPrimaryKey),::std::select2nd<ODatabaseExport::TColumns::value_type>()));
921 if ( aFind == m_vDestColumns.end() )
923 String sTitle(ModuleRes(STR_TABLEDESIGN_NO_PRIM_KEY_HEAD));
924 String sMsg(ModuleRes(STR_TABLEDESIGN_NO_PRIM_KEY));
925 OSQLMessageBox aBox(this, sTitle,sMsg, WB_YES_NO_CANCEL | WB_DEF_YES);
927 INT16 nReturn = aBox.Execute();
929 switch(nReturn )
931 case RET_YES:
933 OCopyTable* pPage = static_cast<OCopyTable*>(GetPage(0));
934 m_bCreatePrimaryKeyColumn = sal_True;
935 m_aKeyName = pPage->GetKeyName();
936 if ( !m_aKeyName.getLength() )
937 m_aKeyName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ID" ) );
938 m_aKeyName = createUniqueName( m_aKeyName );
939 sal_Int32 nBreakPos2 = 0;
940 CheckColumns(nBreakPos2);
941 break;
943 case RET_CANCEL:
944 ShowPage(3);
945 return 0;
946 default:
952 break;
954 case CopyTableOperation::AppendData:
955 case CopyTableOperation::CreateAsView:
956 break;
957 default:
959 OSL_ENSURE(sal_False, "OCopyTableWizard::ImplOKHdl: invalid creation style!");
963 EndDialog(RET_OK);
965 return bFinish;
967 //------------------------------------------------------------------------
968 sal_Bool OCopyTableWizard::shouldCreatePrimaryKey() const
970 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OCopyTableWizard::shouldCreatePrimaryKey" );
971 return m_bCreatePrimaryKeyColumn;
974 // -----------------------------------------------------------------------
975 void OCopyTableWizard::setCreatePrimaryKey( bool _bDoCreate, const ::rtl::OUString& _rSuggestedName )
977 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OCopyTableWizard::setCreatePrimaryKey" );
978 m_bCreatePrimaryKeyColumn = _bDoCreate;
979 if ( _rSuggestedName.getLength() )
980 m_aKeyName = _rSuggestedName;
982 OCopyTable* pSettingsPage = dynamic_cast< OCopyTable* >( GetPage( 0 ) );
983 OSL_ENSURE( pSettingsPage, "OCopyTableWizard::setCreatePrimaryKey: page should have been added in the ctor!" );
984 if ( pSettingsPage )
985 pSettingsPage->setCreatePrimaryKey( _bDoCreate, _rSuggestedName );
988 // -----------------------------------------------------------------------
989 IMPL_LINK( OCopyTableWizard, ImplActivateHdl, WizardDialog*, EMPTYARG )
991 OWizardPage* pCurrent = (OWizardPage*)GetPage(GetCurLevel());
992 if(pCurrent)
994 sal_Bool bFirstTime = pCurrent->IsFirstTime();
995 if(bFirstTime)
996 pCurrent->Reset();
998 CheckButtons();
1000 SetText(pCurrent->GetTitle());
1002 Invalidate();
1004 return 0;
1006 // -----------------------------------------------------------------------
1007 void OCopyTableWizard::CheckButtons()
1009 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OCopyTableWizard::CheckButtons" );
1010 if(GetCurLevel() == 0) // erste Seite hat kein PrevButton
1012 if(m_nPageCount > 1)
1013 m_pbNext.Enable(sal_True);
1014 else
1015 m_pbNext.Enable(sal_False);
1017 m_pbPrev.Enable(sal_False);
1019 else if(GetCurLevel() == m_nPageCount-1) // letzte Seite hat keinen Next Button
1021 m_pbNext.Enable(sal_False);
1022 m_pbPrev.Enable(sal_True);
1024 else
1026 m_pbPrev.Enable(sal_True);
1027 // next has already his state
1030 // -----------------------------------------------------------------------
1031 void OCopyTableWizard::EnableButton(Wizard_Button_Style eStyle,sal_Bool bEnable)
1033 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OCopyTableWizard::EnableButton" );
1034 // CheckButtons();
1035 Button* pButton;
1036 if(eStyle == WIZARD_NEXT)
1037 pButton = &m_pbNext;
1038 else if(eStyle == WIZARD_PREV)
1039 pButton = &m_pbPrev;
1040 else
1041 pButton = &m_pbFinish;
1042 pButton->Enable(bEnable);
1045 // -----------------------------------------------------------------------
1046 long OCopyTableWizard::DeactivatePage()
1048 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OCopyTableWizard::DeactivatePage" );
1049 OWizardPage* pPage = (OWizardPage*)GetPage(GetCurLevel());
1050 return pPage ? pPage->LeavePage() : sal_False;
1052 // -----------------------------------------------------------------------
1053 void OCopyTableWizard::AddWizardPage(OWizardPage* pPage)
1055 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OCopyTableWizard::AddWizardPage" );
1056 AddPage(pPage);
1057 ++m_nPageCount;
1059 // -----------------------------------------------------------------------------
1060 void OCopyTableWizard::insertColumn(sal_Int32 _nPos,OFieldDescription* _pField)
1062 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OCopyTableWizard::insertColumn" );
1063 OSL_ENSURE(_pField,"FieldDescrioption is null!");
1064 if ( _pField )
1066 ODatabaseExport::TColumns::iterator aFind = m_vDestColumns.find(_pField->GetName());
1067 if ( aFind != m_vDestColumns.end() )
1069 delete aFind->second;
1070 m_vDestColumns.erase(aFind);
1073 m_aDestVec.insert(m_aDestVec.begin() + _nPos,
1074 m_vDestColumns.insert(ODatabaseExport::TColumns::value_type(_pField->GetName(),_pField)).first);
1077 // -----------------------------------------------------------------------------
1078 void OCopyTableWizard::replaceColumn(sal_Int32 _nPos,OFieldDescription* _pField,const ::rtl::OUString& _sOldName)
1080 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OCopyTableWizard::replaceColumn" );
1081 OSL_ENSURE(_pField,"FieldDescrioption is null!");
1082 if ( _pField )
1084 m_vDestColumns.erase(_sOldName);
1085 OSL_ENSURE( m_vDestColumns.find(_pField->GetName()) == m_vDestColumns.end(),"Column with that name already exist!");
1087 m_aDestVec[_nPos] =
1088 m_vDestColumns.insert(ODatabaseExport::TColumns::value_type(_pField->GetName(),_pField)).first;
1091 // -----------------------------------------------------------------------------
1092 void OCopyTableWizard::impl_loadSourceData()
1094 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OCopyTableWizard::impl_loadSourceData" );
1095 loadData( m_rSourceObject, m_vSourceColumns, m_vSourceVec );
1098 // -----------------------------------------------------------------------------
1099 void OCopyTableWizard::loadData( const ICopyTableSourceObject& _rSourceObject, ODatabaseExport::TColumns& _rColumns, ODatabaseExport::TColumnVector& _rColVector )
1101 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OCopyTableWizard::loadData" );
1102 ODatabaseExport::TColumns::iterator colEnd = _rColumns.end();
1103 for ( ODatabaseExport::TColumns::iterator col = _rColumns.begin(); col != colEnd; ++col )
1104 delete col->second;
1106 _rColVector.clear();
1107 _rColumns.clear();
1109 OFieldDescription* pActFieldDescr = NULL;
1110 String aType;
1111 ::rtl::OUString sCreateParam(RTL_CONSTASCII_USTRINGPARAM("x"));
1112 //////////////////////////////////////////////////////////////////////
1113 // ReadOnly-Flag
1114 // Bei Drop darf keine Zeile editierbar sein.
1115 // Bei Add duerfen nur die leeren Zeilen editierbar sein.
1116 // Bei Add und Drop koennen alle Zeilen editiert werden.
1117 Sequence< ::rtl::OUString > aColumns( _rSourceObject.getColumnNames() );
1118 const ::rtl::OUString* pColumn = aColumns.getConstArray();
1119 const ::rtl::OUString* pColumnEnd = pColumn + aColumns.getLength();
1121 for ( ; pColumn != pColumnEnd; ++pColumn )
1123 // get the properties of the column
1124 pActFieldDescr = _rSourceObject.createFieldDescription( *pColumn );
1125 OSL_ENSURE( pActFieldDescr, "OCopyTableWizard::loadData: illegal field description!" );
1126 if ( !pActFieldDescr )
1127 continue;
1129 sal_Int32 nType = pActFieldDescr->GetType();
1130 sal_Int32 nScale = pActFieldDescr->GetScale();
1131 sal_Int32 nPrecision = pActFieldDescr->GetPrecision();
1132 sal_Bool bAutoIncrement = pActFieldDescr->IsAutoIncrement();
1133 ::rtl::OUString sTypeName = pActFieldDescr->GetTypeName();
1135 // search for type
1136 sal_Bool bForce;
1137 TOTypeInfoSP pTypeInfo = ::dbaui::getTypeInfoFromType(m_aTypeInfo,nType,sTypeName,sCreateParam,nPrecision,nScale,bAutoIncrement,bForce);
1138 if ( !pTypeInfo.get() )
1139 pTypeInfo = m_pTypeInfo;
1141 pActFieldDescr->FillFromTypeInfo(pTypeInfo,sal_True,sal_False);
1142 _rColVector.push_back(_rColumns.insert(ODatabaseExport::TColumns::value_type(pActFieldDescr->GetName(),pActFieldDescr)).first);
1145 // determine which coumns belong to the primary key
1146 Sequence< ::rtl::OUString > aPrimaryKeyColumns( _rSourceObject.getPrimaryKeyColumnNames() );
1147 const ::rtl::OUString* pKeyColName = aPrimaryKeyColumns.getConstArray();
1148 const ::rtl::OUString* pKeyColEnd = pKeyColName + aPrimaryKeyColumns.getLength();
1150 for( ; pKeyColName != pKeyColEnd; ++pKeyColName )
1152 ODatabaseExport::TColumns::iterator keyPos = _rColumns.find( *pKeyColName );
1153 if ( keyPos != _rColumns.end() )
1155 keyPos->second->SetPrimaryKey( sal_True );
1156 keyPos->second->SetIsNullable( ColumnValue::NO_NULLS );
1160 // -----------------------------------------------------------------------------
1161 void OCopyTableWizard::clearDestColumns()
1163 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OCopyTableWizard::clearDestColumns" );
1164 clearColumns(m_vDestColumns,m_aDestVec);
1167 // -----------------------------------------------------------------------------
1168 void OCopyTableWizard::appendColumns( Reference<XColumnsSupplier>& _rxColSup, const ODatabaseExport::TColumnVector* _pVec, sal_Bool _bKeyColumns) const
1170 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OCopyTableWizard::appendColumns" );
1171 // now append the columns
1172 OSL_ENSURE(_rxColSup.is(),"No columns supplier");
1173 if(!_rxColSup.is())
1174 return;
1175 Reference<XNameAccess> xColumns = _rxColSup->getColumns();
1176 OSL_ENSURE(xColumns.is(),"No columns");
1177 Reference<XDataDescriptorFactory> xColumnFactory(xColumns,UNO_QUERY);
1179 Reference<XAppend> xAppend(xColumns,UNO_QUERY);
1180 OSL_ENSURE(xAppend.is(),"No XAppend Interface!");
1182 ODatabaseExport::TColumnVector::const_iterator aIter = _pVec->begin();
1183 ODatabaseExport::TColumnVector::const_iterator aEnd = _pVec->end();
1184 for(;aIter != aEnd;++aIter)
1186 OFieldDescription* pField = (*aIter)->second;
1187 if(!pField)
1188 continue;
1190 Reference<XPropertySet> xColumn;
1191 if(pField->IsPrimaryKey() || !_bKeyColumns)
1192 xColumn = xColumnFactory->createDataDescriptor();
1193 if(xColumn.is())
1195 if(!_bKeyColumns)
1196 dbaui::setColumnProperties(xColumn,pField);
1197 else
1198 xColumn->setPropertyValue(PROPERTY_NAME,makeAny(pField->GetName()));
1200 xAppend->appendByDescriptor(xColumn);
1201 xColumn = NULL;
1202 // now only the settings are missing
1203 if(xColumns->hasByName(pField->GetName()))
1205 xColumn.set(xColumns->getByName(pField->GetName()),UNO_QUERY);
1206 OSL_ENSURE(xColumn.is(),"OCopyTableWizard::appendColumns: Column is NULL!");
1207 if ( xColumn.is() )
1208 pField->copyColumnSettingsTo(xColumn);
1210 else
1212 OSL_ENSURE(sal_False, "OCopyTableWizard::appendColumns: invalid field name!");
1218 // -----------------------------------------------------------------------------
1219 void OCopyTableWizard::appendKey( Reference<XKeysSupplier>& _rxSup, const ODatabaseExport::TColumnVector* _pVec) const
1221 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OCopyTableWizard::appendKey" );
1222 if(!_rxSup.is())
1223 return; // the database doesn't support keys
1224 OSL_ENSURE(_rxSup.is(),"No XKeysSupplier!");
1225 Reference<XDataDescriptorFactory> xKeyFactory(_rxSup->getKeys(),UNO_QUERY);
1226 OSL_ENSURE(xKeyFactory.is(),"No XDataDescriptorFactory Interface!");
1227 if ( !xKeyFactory.is() )
1228 return;
1229 Reference<XAppend> xAppend(xKeyFactory,UNO_QUERY);
1230 OSL_ENSURE(xAppend.is(),"No XAppend Interface!");
1232 Reference<XPropertySet> xKey = xKeyFactory->createDataDescriptor();
1233 OSL_ENSURE(xKey.is(),"Key is null!");
1234 xKey->setPropertyValue(PROPERTY_TYPE,makeAny(KeyType::PRIMARY));
1236 Reference<XColumnsSupplier> xColSup(xKey,UNO_QUERY);
1237 if(xColSup.is())
1239 appendColumns(xColSup,_pVec,sal_True);
1240 Reference<XNameAccess> xColumns = xColSup->getColumns();
1241 if(xColumns.is() && xColumns->getElementNames().getLength())
1242 xAppend->appendByDescriptor(xKey);
1246 // -----------------------------------------------------------------------------
1247 Reference< XPropertySet > OCopyTableWizard::createView() const
1249 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OCopyTableWizard::createView" );
1250 ::rtl::OUString sCommand( m_rSourceObject.getSelectStatement() );
1251 OSL_ENSURE( sCommand.getLength(), "OCopyTableWizard::createView: no statement in the source object!" );
1252 // there are legitimate cases in which getSelectStatement does not provide a statement,
1253 // but in all those cases, this method here should never be called.
1254 return ::dbaui::createView( m_sName, m_xDestConnection, sCommand );
1256 // -----------------------------------------------------------------------------
1257 Reference< XPropertySet > OCopyTableWizard::createTable()
1259 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OCopyTableWizard::createTable" );
1260 Reference< XPropertySet > xTable;
1262 Reference<XTablesSupplier> xSup( m_xDestConnection, UNO_QUERY );
1263 Reference< XNameAccess > xTables;
1264 if(xSup.is())
1265 xTables = xSup->getTables();
1266 if ( getOperation() != CopyTableOperation::AppendData )
1268 Reference<XDataDescriptorFactory> xFact(xTables,UNO_QUERY);
1269 OSL_ENSURE(xFact.is(),"No XDataDescriptorFactory available!");
1270 if(!xFact.is())
1271 return NULL;
1273 xTable = xFact->createDataDescriptor();
1274 OSL_ENSURE(xTable.is(),"Could not create a new object!");
1275 if(!xTable.is())
1276 return NULL;
1278 ::rtl::OUString sCatalog,sSchema,sTable;
1279 Reference< XDatabaseMetaData> xMetaData = m_xDestConnection->getMetaData();
1280 ::dbtools::qualifiedNameComponents(xMetaData,
1281 m_sName,
1282 sCatalog,
1283 sSchema,
1284 sTable,
1285 ::dbtools::eInDataManipulation);
1287 if ( !sCatalog.getLength() && xMetaData->supportsCatalogsInTableDefinitions() )
1289 sCatalog = m_xDestConnection->getCatalog();
1292 if ( !sSchema.getLength() && xMetaData->supportsSchemasInTableDefinitions() )
1294 sSchema = xMetaData->getUserName();
1297 xTable->setPropertyValue(PROPERTY_CATALOGNAME,makeAny(sCatalog));
1298 xTable->setPropertyValue(PROPERTY_SCHEMANAME,makeAny(sSchema));
1299 xTable->setPropertyValue(PROPERTY_NAME,makeAny(sTable));
1301 Reference< XColumnsSupplier > xSuppDestinationColumns( xTable, UNO_QUERY );
1302 // now append the columns
1303 const ODatabaseExport::TColumnVector* pVec = getDestVector();
1304 appendColumns( xSuppDestinationColumns, pVec );
1305 // now append the primary key
1306 Reference<XKeysSupplier> xKeySup(xTable,UNO_QUERY);
1307 appendKey(xKeySup,pVec);
1309 Reference<XAppend> xAppend(xTables,UNO_QUERY);
1310 if(xAppend.is())
1311 xAppend->appendByDescriptor(xTable);
1313 // xTable = NULL;
1314 // we need to reget the table because after appending it it is no longer valid
1315 if(xTables->hasByName(m_sName))
1316 xTables->getByName(m_sName) >>= xTable;
1317 else
1319 ::rtl::OUString sComposedName(
1320 ::dbtools::composeTableName( m_xDestConnection->getMetaData(), xTable, ::dbtools::eInDataManipulation, false, false, false ) );
1321 if(xTables->hasByName(sComposedName))
1323 xTables->getByName(sComposedName) >>= xTable;
1324 m_sName = sComposedName;
1326 else
1327 xTable = NULL;
1329 if(xTable.is())
1331 xSuppDestinationColumns.set( xTable, UNO_QUERY_THROW );
1332 // insert new table name into table filter
1333 ::dbaui::appendToFilter( m_xDestConnection, m_sName, GetFactory(), this );
1335 // copy ui settings
1336 m_rSourceObject.copyUISettingsTo( xTable );
1337 //copy filter and sorting
1338 m_rSourceObject.copyFilterAndSortingTo(m_xDestConnection,xTable);
1339 // set column mappings
1340 Reference<XNameAccess> xNameAccess = xSuppDestinationColumns->getColumns();
1341 Sequence< ::rtl::OUString> aSeq = xNameAccess->getElementNames();
1342 const ::rtl::OUString* pIter = aSeq.getConstArray();
1343 const ::rtl::OUString* pEnd = pIter + aSeq.getLength();
1345 ::std::vector<int> aAlreadyFound(m_vColumnPos.size(),0);
1347 for(sal_Int32 nNewPos=1;pIter != pEnd;++pIter,++nNewPos)
1349 ODatabaseExport::TColumns::const_iterator aDestIter = m_vDestColumns.find(*pIter);
1351 if ( aDestIter != m_vDestColumns.end() )
1353 ODatabaseExport::TColumnVector::const_iterator aFind = ::std::find(m_aDestVec.begin(),m_aDestVec.end(),aDestIter);
1354 sal_Int32 nPos = (aFind - m_aDestVec.begin())+1;
1356 ODatabaseExport::TPositions::iterator aPosFind = ::std::find_if(
1357 m_vColumnPos.begin(),
1358 m_vColumnPos.end(),
1359 ::std::compose1( ::std::bind2nd( ::std::equal_to< sal_Int32 >(), nPos ),
1360 ::std::select1st< ODatabaseExport::TPositions::value_type >()
1364 if ( m_vColumnPos.end() != aPosFind )
1366 aPosFind->second = nNewPos;
1367 OSL_ENSURE( m_vColumnTypes.size() > size_t( aPosFind - m_vColumnPos.begin() ),
1368 "Invalid index for vector!" );
1369 m_vColumnTypes[ aPosFind - m_vColumnPos.begin() ] = (*aFind)->second->GetType();
1375 else if(xTables.is() && xTables->hasByName(m_sName))
1376 xTables->getByName(m_sName) >>= xTable;
1378 return xTable;
1381 // -----------------------------------------------------------------------------
1382 bool OCopyTableWizard::supportsPrimaryKey( const Reference< XConnection >& _rxConnection )
1384 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OCopyTableWizard::supportsPrimaryKey" );
1385 OSL_PRECOND( _rxConnection.is(), "OCopyTableWizard::supportsPrimaryKey: invalid connection!" );
1387 bool bSupports( false );
1388 if ( _rxConnection.is() )
1392 Reference< XDatabaseMetaData > xMetaData( _rxConnection->getMetaData(), UNO_QUERY_THROW );
1393 bSupports = xMetaData->supportsCoreSQLGrammar();
1395 catch(const Exception&)
1397 DBG_UNHANDLED_EXCEPTION();
1400 return bSupports;
1403 // -----------------------------------------------------------------------------
1404 bool OCopyTableWizard::supportsViews( const Reference< XConnection >& _rxConnection )
1406 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OCopyTableWizard::supportsViews" );
1407 OSL_PRECOND( _rxConnection.is(), "OCopyTableWizard::supportsViews: invalid connection!" );
1408 if ( !_rxConnection.is() )
1409 return false;
1411 bool bSupportsViews( false );
1414 Reference< XDatabaseMetaData > xMetaData( _rxConnection->getMetaData(), UNO_SET_THROW );
1415 Reference< XViewsSupplier > xViewSups( _rxConnection, UNO_QUERY );
1416 bSupportsViews = xViewSups.is();
1417 if ( !bSupportsViews )
1421 Reference< XResultSet > xRs( xMetaData->getTableTypes(), UNO_SET_THROW );
1422 Reference< XRow > xRow( xRs, UNO_QUERY_THROW );
1423 while ( xRs->next() )
1425 ::rtl::OUString sValue = xRow->getString( 1 );
1426 if ( !xRow->wasNull() && sValue.equalsIgnoreAsciiCaseAscii( "View" ) )
1428 bSupportsViews = true;
1429 break;
1433 catch( const SQLException& )
1435 DBG_UNHANDLED_EXCEPTION();
1439 catch( const Exception& )
1441 DBG_UNHANDLED_EXCEPTION();
1443 return bSupportsViews;
1446 // -----------------------------------------------------------------------------
1447 sal_Int32 OCopyTableWizard::getMaxColumnNameLength() const
1449 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OCopyTableWizard::getMaxColumnNameLength" );
1450 sal_Int32 nLen = 0;
1451 if ( m_xDestConnection.is() )
1455 Reference< XDatabaseMetaData > xMetaData( m_xDestConnection->getMetaData(), UNO_SET_THROW );
1456 nLen = xMetaData->getMaxColumnNameLength();
1458 catch(const Exception&)
1460 DBG_UNHANDLED_EXCEPTION();
1463 return nLen;
1465 // -----------------------------------------------------------------------------
1466 void OCopyTableWizard::setOperation( const sal_Int16 _nOperation )
1468 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OCopyTableWizard::setOperation" );
1469 m_nOperation = _nOperation;
1471 // -----------------------------------------------------------------------------
1472 sal_Int16 OCopyTableWizard::getOperation() const
1474 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OCopyTableWizard::getOperation" );
1475 return m_nOperation;
1477 // -----------------------------------------------------------------------------
1478 ::rtl::OUString OCopyTableWizard::convertColumnName(const TColumnFindFunctor& _rCmpFunctor,
1479 const ::rtl::OUString& _sColumnName,
1480 const ::rtl::OUString& _sExtraChars,
1481 sal_Int32 _nMaxNameLen)
1483 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OCopyTableWizard::convertColumnName" );
1484 ::rtl::OUString sAlias = _sColumnName;
1485 if ( isSQL92CheckEnabled( m_xDestConnection ) )
1486 sAlias = ::dbtools::convertName2SQLName(_sColumnName,_sExtraChars);
1487 if((_nMaxNameLen && sAlias.getLength() > _nMaxNameLen) || _rCmpFunctor(sAlias))
1489 sal_Int32 nDiff = 1;
1492 ++nDiff;
1493 if(_nMaxNameLen && sAlias.getLength() >= _nMaxNameLen)
1494 sAlias = sAlias.copy(0,sAlias.getLength() - (sAlias.getLength()-_nMaxNameLen+nDiff));
1496 ::rtl::OUString sName(sAlias);
1497 sal_Int32 nPos = 1;
1498 sName += ::rtl::OUString::valueOf(nPos);
1500 while(_rCmpFunctor(sName))
1502 sName = sAlias;
1503 sName += ::rtl::OUString::valueOf(++nPos);
1505 sAlias = sName;
1506 // we have to check again, it could happen that the name is already to long
1508 while(_nMaxNameLen && sAlias.getLength() > _nMaxNameLen);
1510 OSL_ENSURE(m_mNameMapping.find(_sColumnName) == m_mNameMapping.end(),"name doubled!");
1511 m_mNameMapping[_sColumnName] = sAlias;
1512 return sAlias;
1515 // -----------------------------------------------------------------------------
1516 void OCopyTableWizard::removeColumnNameFromNameMap(const ::rtl::OUString& _sName)
1518 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OCopyTableWizard::removeColumnNameFromNameMap" );
1519 m_mNameMapping.erase(_sName);
1522 // -----------------------------------------------------------------------------
1523 sal_Bool OCopyTableWizard::supportsType(sal_Int32 _nDataType,sal_Int32& _rNewDataType)
1525 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OCopyTableWizard::supportsType" );
1526 sal_Bool bRet = m_aDestTypeInfo.find(_nDataType) != m_aDestTypeInfo.end();
1527 if ( bRet )
1528 _rNewDataType = _nDataType;
1529 return bRet;
1532 // -----------------------------------------------------------------------------
1533 TOTypeInfoSP OCopyTableWizard::convertType(const TOTypeInfoSP& _pType,sal_Bool& _bNotConvert)
1535 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OCopyTableWizard::convertType" );
1536 if ( !m_bInterConnectionCopy )
1537 // no need to convert if the source and destination connection are the same
1538 return _pType;
1540 sal_Bool bForce;
1541 TOTypeInfoSP pType = ::dbaui::getTypeInfoFromType(m_aDestTypeInfo,_pType->nType,_pType->aTypeName,_pType->aCreateParams,_pType->nPrecision,_pType->nMaximumScale,_pType->bAutoIncrement,bForce);
1542 if ( !pType.get() || bForce )
1543 { // no type found so we have to find the correct one ourself
1544 sal_Int32 nDefaultType = DataType::VARCHAR;
1545 switch(_pType->nType)
1547 case DataType::TINYINT:
1548 if(supportsType(DataType::SMALLINT,nDefaultType))
1549 break;
1550 // run through
1551 case DataType::SMALLINT:
1552 if(supportsType(DataType::INTEGER,nDefaultType))
1553 break;
1554 // run through
1555 case DataType::INTEGER:
1556 if(supportsType(DataType::FLOAT,nDefaultType))
1557 break;
1558 // run through
1559 case DataType::FLOAT:
1560 if(supportsType(DataType::REAL,nDefaultType))
1561 break;
1562 // run through
1563 case DataType::DATE:
1564 case DataType::TIME:
1565 if( DataType::DATE == _pType->nType || DataType::TIME == _pType->nType )
1567 if(supportsType(DataType::TIMESTAMP,nDefaultType))
1568 break;
1570 // run through
1571 case DataType::TIMESTAMP:
1572 case DataType::REAL:
1573 case DataType::BIGINT:
1574 if ( supportsType(DataType::DOUBLE,nDefaultType) )
1575 break;
1576 // run through
1577 case DataType::DOUBLE:
1578 if ( supportsType(DataType::NUMERIC,nDefaultType) )
1579 break;
1580 // run through
1581 case DataType::NUMERIC:
1582 supportsType(DataType::DECIMAL,nDefaultType);
1583 break;
1584 case DataType::DECIMAL:
1585 if ( supportsType(DataType::NUMERIC,nDefaultType) )
1586 break;
1587 if ( supportsType(DataType::DOUBLE,nDefaultType) )
1588 break;
1589 break;
1590 case DataType::VARCHAR:
1591 if ( supportsType(DataType::LONGVARCHAR,nDefaultType) )
1592 break;
1593 break;
1594 default:
1595 nDefaultType = DataType::VARCHAR;
1597 pType = ::dbaui::getTypeInfoFromType(m_aDestTypeInfo,nDefaultType,_pType->aTypeName,_pType->aCreateParams,_pType->nPrecision,_pType->nMaximumScale,_pType->bAutoIncrement,bForce);
1598 if ( !pType.get() )
1600 _bNotConvert = sal_False;
1601 ::rtl::OUString sCreate(RTL_CONSTASCII_USTRINGPARAM("x"));
1602 pType = ::dbaui::getTypeInfoFromType(m_aDestTypeInfo,DataType::VARCHAR,_pType->aTypeName,sCreate,50,0,sal_False,bForce);
1603 if ( !pType.get() )
1604 pType = m_pTypeInfo;
1606 else if ( bForce )
1607 _bNotConvert = sal_False;
1609 return pType;
1611 // -----------------------------------------------------------------------------
1612 ::rtl::OUString OCopyTableWizard::createUniqueName(const ::rtl::OUString& _sName)
1614 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OCopyTableWizard::createUniqueName" );
1615 ::rtl::OUString sName = _sName;
1616 Sequence< ::rtl::OUString > aColumnNames( m_rSourceObject.getColumnNames() );
1617 if ( aColumnNames.getLength() )
1618 sName = ::dbtools::createUniqueName( aColumnNames, sName, sal_False );
1619 else
1621 if ( m_vSourceColumns.find(sName) != m_vSourceColumns.end())
1623 sal_Int32 nPos = 0;
1624 while(m_vSourceColumns.find(sName) != m_vSourceColumns.end())
1626 sName = _sName;
1627 sName += ::rtl::OUString::valueOf(++nPos);
1631 return sName;
1633 // -----------------------------------------------------------------------------
1634 void OCopyTableWizard::showColumnTypeNotSupported(const ::rtl::OUString& _rColumnName)
1636 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "misc", "Ocke.Janssen@sun.com", "OCopyTableWizard::showColumnTypeNotSupported" );
1637 String sMessage( ModuleRes( STR_UNKNOWN_TYPE_FOUND ) );
1638 sMessage.SearchAndReplaceAscii("#1",_rColumnName);
1640 OSQLWarningBox( this, sMessage ).Execute();
1642 //-------------------------------------------------------------------------------