23#include <core_resource.hxx>
26#include <com/sun/star/lang/XMultiServiceFactory.hpp>
27#include <com/sun/star/sdb/application/XCopyTableWizard.hpp>
28#include <com/sun/star/sdb/application/CopyTableContinuation.hpp>
29#include <com/sun/star/sdb/application/CopyTableOperation.hpp>
30#include <com/sun/star/ucb/AlreadyInitializedException.hpp>
31#include <com/sun/star/lang/NotInitializedException.hpp>
32#include <com/sun/star/sdbc/XDataSource.hpp>
33#include <com/sun/star/sdbc/DataType.hpp>
34#include <com/sun/star/container/XNameAccess.hpp>
35#include <com/sun/star/container/XChild.hpp>
36#include <com/sun/star/task/InteractionHandler.hpp>
37#include <com/sun/star/frame/XModel.hpp>
38#include <com/sun/star/sdb/DatabaseContext.hpp>
39#include <com/sun/star/sdb/XDocumentDataSource.hpp>
40#include <com/sun/star/sdb/XCompletedConnection.hpp>
41#include <com/sun/star/sdb/CommandType.hpp>
42#include <com/sun/star/sdbcx/XTablesSupplier.hpp>
43#include <com/sun/star/sdb/XQueriesSupplier.hpp>
44#include <com/sun/star/lang/DisposedException.hpp>
45#include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
46#include <com/sun/star/sdbc/XParameters.hpp>
47#include <com/sun/star/sdbc/XRow.hpp>
48#include <com/sun/star/sdbcx/XRowLocate.hpp>
49#include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
50#include <com/sun/star/sdb/SQLContext.hpp>
51#include <com/sun/star/sdbc/DriverManager.hpp>
52#include <com/sun/star/sdbc/ConnectionPool.hpp>
64#include <rtl/ustrbuf.hxx>
75 using ::com::sun::star::uno::Reference;
76 using ::com::sun::star::uno::XInterface;
77 using ::com::sun::star::uno::UNO_QUERY;
78 using ::com::sun::star::uno::UNO_QUERY_THROW;
79 using ::com::sun::star::uno::UNO_SET_THROW;
80 using ::com::sun::star::uno::Exception;
81 using ::com::sun::star::uno::RuntimeException;
82 using ::com::sun::star::uno::Any;
83 using ::com::sun::star::uno::Sequence;
84 using ::com::sun::star::uno::XComponentContext;
85 using ::com::sun::star::beans::XPropertySetInfo;
86 using ::com::sun::star::lang::XMultiServiceFactory;
87 using ::com::sun::star::beans::Property;
88 using ::com::sun::star::sdb::application::XCopyTableWizard;
89 using ::com::sun::star::sdb::application::XCopyTableListener;
90 using ::com::sun::star::sdb::application::CopyTableRowEvent;
91 using ::com::sun::star::beans::Optional;
92 using ::com::sun::star::lang::IllegalArgumentException;
93 using ::com::sun::star::ucb::AlreadyInitializedException;
94 using ::com::sun::star::beans::XPropertySet;
95 using ::com::sun::star::lang::NotInitializedException;
96 using ::com::sun::star::lang::XServiceInfo;
97 using ::com::sun::star::sdbc::XConnection;
98 using ::com::sun::star::sdbc::XDataSource;
99 using ::com::sun::star::container::XNameAccess;
100 using ::com::sun::star::container::XChild;
101 using ::com::sun::star::task::InteractionHandler;
102 using ::com::sun::star::task::XInteractionHandler;
103 using ::com::sun::star::frame::XModel;
104 using ::com::sun::star::sdb::DatabaseContext;
105 using ::com::sun::star::sdb::XDatabaseContext;
106 using ::com::sun::star::sdb::XDocumentDataSource;
107 using ::com::sun::star::sdb::XCompletedConnection;
108 using ::com::sun::star::lang::WrappedTargetException;
109 using ::com::sun::star::sdbcx::XTablesSupplier;
110 using ::com::sun::star::sdb::XQueriesSupplier;
111 using ::com::sun::star::lang::DisposedException;
112 using ::com::sun::star::sdbc::XPreparedStatement;
113 using ::com::sun::star::sdb::XSingleSelectQueryComposer;
114 using ::com::sun::star::sdbc::XDatabaseMetaData;
115 using ::com::sun::star::sdbcx::XColumnsSupplier;
116 using ::com::sun::star::sdbc::XParameters;
117 using ::com::sun::star::sdbc::XResultSet;
118 using ::com::sun::star::sdbc::XRow;
119 using ::com::sun::star::sdbcx::XRowLocate;
120 using ::com::sun::star::sdbc::XResultSetMetaDataSupplier;
121 using ::com::sun::star::sdbc::XResultSetMetaData;
122 using ::com::sun::star::sdbc::SQLException;
123 using ::com::sun::star::sdb::SQLContext;
124 using ::com::sun::star::sdbc::ConnectionPool;
125 using ::com::sun::star::sdbc::XDriverManager;
126 using ::com::sun::star::sdbc::DriverManager;
127 using ::com::sun::star::beans::PropertyValue;
129 namespace CopyTableOperation = ::com::sun::star::sdb::application::CopyTableOperation;
130 namespace CopyTableContinuation = ::com::sun::star::sdb::application::CopyTableContinuation;
131 namespace CommandType = ::com::sun::star::sdb::CommandType;
132 namespace DataType = ::com::sun::star::sdbc::DataType;
144 class CopyTableWizard
154 virtual ::sal_Int16 SAL_CALL getOperation()
override;
155 virtual void SAL_CALL setOperation( ::sal_Int16 _operation )
override;
156 virtual OUString SAL_CALL getDestinationTableName()
override;
157 virtual void SAL_CALL setDestinationTableName(
const OUString& _destinationTableName )
override;
158 virtual Optional< OUString > SAL_CALL getCreatePrimaryKey()
override;
159 virtual void SAL_CALL setCreatePrimaryKey(
const Optional< OUString >& _newPrimaryKey )
override;
160 virtual sal_Bool SAL_CALL getUseHeaderLineAsColumnNames()
override;
161 virtual void SAL_CALL setUseHeaderLineAsColumnNames(
sal_Bool _bUseHeaderLineAsColumnNames )
override;
166 virtual void SAL_CALL setTitle(
const OUString& aTitle )
override;
167 virtual ::sal_Int16 SAL_CALL execute( )
override;
174 virtual ::cppu::IPropertyArrayHelper& SAL_CALL getInfoHelper()
override;
177 virtual ::cppu::IPropertyArrayHelper* createArrayHelper( )
const override;
184 virtual ~CopyTableWizard()
override;
188 virtual std::unique_ptr<weld::DialogController> createDialog(
const css::uno::Reference<css::awt::XWindow>& rParent)
override;
189 virtual void executedDialog( sal_Int16 _nExecutionResult )
override;
204 impl_getDialog_throw();
221 impl_ensureDataAccessDescriptor_throw(
223 const sal_Int16 _nArgPos,
231 std::unique_ptr< ICopyTableSourceObject >
232 impl_extractSourceObject_throw(
234 sal_Int32& _out_rCommandType
244 void impl_extractSourceResultSet_throw(
254 void impl_checkForUnsupportedSettings_throw(
275 impl_extractConnection_throw(
284 void impl_doCopy_nothrow();
297 impl_createSourceStatement_throw()
const;
301 void impl_copyRows_throw(
315 bool impl_processCopyError_nothrow(
316 const CopyTableRowEvent& _rEvent );
330 std::unique_ptr< ICopyTableSourceObject >
347class CopyTableAccessGuard
350 explicit CopyTableAccessGuard( CopyTableWizard& _rWizard )
355 throw NotInitializedException();
358 ~CopyTableAccessGuard()
369CopyTableWizard::CopyTableWizard(
const Reference< XComponentContext >& _rxORB )
372 ,
m_nOperation( CopyTableOperation::CopyDefinitionAndData )
382CopyTableWizard::~CopyTableWizard()
399OUString SAL_CALL CopyTableWizard::getImplementationName()
401 return "org.openoffice.comp.dbu.CopyTableWizard";
404css::uno::Sequence<OUString> SAL_CALL CopyTableWizard::getSupportedServiceNames()
406 return {
"com.sun.star.sdb.application.CopyTableWizard" };
409Reference< XPropertySetInfo > SAL_CALL CopyTableWizard::getPropertySetInfo()
411 Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
415::sal_Int16 SAL_CALL CopyTableWizard::getOperation()
417 CopyTableAccessGuard aGuard( *
this );
421void SAL_CALL CopyTableWizard::setOperation( ::sal_Int16 _operation )
423 CopyTableAccessGuard aGuard( *
this );
425 if ( ( _operation != CopyTableOperation::CopyDefinitionAndData )
426 && ( _operation != CopyTableOperation::CopyDefinitionOnly )
427 && ( _operation != CopyTableOperation::CreateAsView )
428 && ( _operation != CopyTableOperation::AppendData )
430 throw IllegalArgumentException( OUString(), *
this, 1 );
432 if ( ( _operation == CopyTableOperation::CreateAsView )
435 throw IllegalArgumentException(
436 DBA_RES( STR_CTW_NO_VIEWS_SUPPORT ),
444OUString SAL_CALL CopyTableWizard::getDestinationTableName()
446 CopyTableAccessGuard aGuard( *
this );
450void SAL_CALL CopyTableWizard::setDestinationTableName(
const OUString& _destinationTableName )
452 CopyTableAccessGuard aGuard( *
this );
456Optional< OUString > SAL_CALL CopyTableWizard::getCreatePrimaryKey()
458 CopyTableAccessGuard aGuard( *
this );
462void SAL_CALL CopyTableWizard::setCreatePrimaryKey(
const Optional< OUString >& _newPrimaryKey )
464 CopyTableAccessGuard aGuard( *
this );
466 if ( _newPrimaryKey.IsPresent && !OCopyTableWizard::supportsPrimaryKey(
m_xDestConnection ) )
467 throw IllegalArgumentException(
468 DBA_RES( STR_CTW_NO_PRIMARY_KEY_SUPPORT ),
476sal_Bool SAL_CALL CopyTableWizard::getUseHeaderLineAsColumnNames()
478 CopyTableAccessGuard aGuard( *
this );
482void SAL_CALL CopyTableWizard::setUseHeaderLineAsColumnNames(
sal_Bool _bUseHeaderLineAsColumnNames )
484 CopyTableAccessGuard aGuard( *
this );
488void SAL_CALL CopyTableWizard::addCopyTableListener(
const Reference< XCopyTableListener >& _rxListener )
490 CopyTableAccessGuard aGuard( *
this );
491 if ( _rxListener.is() )
495void SAL_CALL CopyTableWizard::removeCopyTableListener(
const Reference< XCopyTableListener >& _rxListener )
497 CopyTableAccessGuard aGuard( *
this );
498 if ( _rxListener.is() )
502void SAL_CALL CopyTableWizard::setTitle(
const OUString& _rTitle )
504 CopyTableAccessGuard aGuard( *
this );
505 CopyTableWizard_DialogBase::setTitle( _rTitle );
508::sal_Int16 SAL_CALL CopyTableWizard::execute( )
510 CopyTableAccessGuard aGuard( *
this );
513 sal_Int16 nExecutionResult = CopyTableWizard_DialogBase::execute();
517 return nExecutionResult;
520OCopyTableWizard& CopyTableWizard::impl_getDialog_throw()
522 OCopyTableWizard* pWizard =
dynamic_cast<OCopyTableWizard*
>(
m_xDialog.get());
524 throw DisposedException( OUString(), *
this );
528void CopyTableWizard::impl_attributesToDialog_nothrow( OCopyTableWizard& _rDialog )
const
537void CopyTableWizard::impl_dialogToAttributes_nothrow(
const OCopyTableWizard& _rDialog )
562 Reference< XInteractionHandler > lcl_getInteractionHandler_throw(
const Reference< XDataSource >& _rxDataSource,
const Reference< XInteractionHandler >& _rFallback )
564 Reference< XInteractionHandler > xHandler( _rFallback );
567 Reference< XModel > xDocumentModel;
568 Reference< XDocumentDataSource > xDocDataSource( _rxDataSource, UNO_QUERY );
569 if ( xDocDataSource.is() )
570 xDocumentModel.set( xDocDataSource->getDatabaseDocument(), UNO_QUERY_THROW );
573 if ( xDocumentModel.is() )
589 Reference< XInteractionHandler > lcl_getInteractionHandler_throw(
const Reference< XConnection >& _rxConnection,
const Reference< XInteractionHandler >& _rFallback )
592 Reference< XDataSource > xDataSource;
593 Reference< XChild > xAsChild( _rxConnection, UNO_QUERY );
595 xDataSource.set(xAsChild->getParent(), css::uno::UNO_QUERY);
597 if ( xDataSource.is() )
598 return lcl_getInteractionHandler_throw( xDataSource, _rFallback );
604Reference< XPropertySet > CopyTableWizard::impl_ensureDataAccessDescriptor_throw(
605 const Sequence< Any >& _rAllArgs,
const sal_Int16 _nArgPos,
SharedConnection& _out_rxConnection,
606 Reference< XInteractionHandler >& _out_rxDocInteractionHandler )
const
608 Reference< XPropertySet > xDescriptor;
609 _rAllArgs[ _nArgPos ] >>= xDescriptor;
612 bool bIsValid = xDescriptor.is();
617 Reference< XServiceInfo > xSI( xDescriptor, UNO_QUERY );
618 bIsValid = ( xSI.is()
619 && xSI->supportsService(
"com.sun.star.sdb.DataAccessDescriptor" ) );
625 _out_rxConnection = impl_extractConnection_throw( xDescriptor, _out_rxDocInteractionHandler );
626 bIsValid = _out_rxConnection.
is();
631 throw IllegalArgumentException(
632 DBA_RES( STR_CTW_INVALID_DATA_ACCESS_DESCRIPTOR ),
633 *
const_cast< CopyTableWizard*
>(
this ),
643 bool lcl_hasNonEmptyStringValue_throw(
const Reference< XPropertySet >& _rxDescriptor,
644 const Reference< XPropertySetInfo >& rxPSI,
const OUString& _rPropertyName )
647 if ( rxPSI->hasPropertyByName( _rPropertyName ) )
649 OSL_VERIFY( _rxDescriptor->getPropertyValue( _rPropertyName ) >>= sValue );
651 return !sValue.isEmpty();
655void CopyTableWizard::impl_checkForUnsupportedSettings_throw(
const Reference< XPropertySet >& _rxSourceDescriptor )
const
657 OSL_PRECOND( _rxSourceDescriptor.is(),
"CopyTableWizard::impl_checkForUnsupportedSettings_throw: illegal argument!" );
658 Reference< XPropertySetInfo > xPSI( _rxSourceDescriptor->getPropertySetInfo(), UNO_SET_THROW );
659 OUString sUnsupportedSetting;
661 const OUString aSettings[] = {
664 for (
const auto & aSetting : aSettings)
666 if ( lcl_hasNonEmptyStringValue_throw( _rxSourceDescriptor, xPSI, aSetting ) )
668 sUnsupportedSetting = aSetting;
673 if ( !sUnsupportedSetting.isEmpty() )
676 DBA_RES(STR_CTW_ERROR_UNSUPPORTED_SETTING).
677 replaceFirst(
"$name$", sUnsupportedSetting));
678 throw IllegalArgumentException(
680 *
const_cast< CopyTableWizard*
>(
this ),
687std::unique_ptr< ICopyTableSourceObject > CopyTableWizard::impl_extractSourceObject_throw(
const Reference< XPropertySet >& _rxDescriptor, sal_Int32& _out_rCommandType )
const
689 OSL_PRECOND( _rxDescriptor.is() &&
m_xSourceConnection.
is(),
"CopyTableWizard::impl_extractSourceObject_throw: illegal arguments!" );
691 Reference< XPropertySetInfo > xPSI( _rxDescriptor->getPropertySetInfo(), UNO_SET_THROW );
695 throw IllegalArgumentException(
"Expecting a table or query specification.",
697 *
const_cast< CopyTableWizard*
>(
this ), 1);
700 _out_rCommandType = CommandType::COMMAND;
701 OSL_VERIFY( _rxDescriptor->getPropertyValue(
PROPERTY_COMMAND ) >>= sCommand );
704 std::unique_ptr< ICopyTableSourceObject > pSourceObject;
705 Reference< XNameAccess > xContainer;
706 switch ( _out_rCommandType )
711 if ( xSuppTables.is() )
712 xContainer.set( xSuppTables->getTables(), UNO_SET_THROW );
718 if ( xSuppQueries.is() )
719 xContainer.set( xSuppQueries->getQueries(), UNO_SET_THROW );
723 throw IllegalArgumentException(
724 DBA_RES( STR_CTW_ONLY_TABLES_AND_QUERIES_SUPPORT ),
725 *
const_cast< CopyTableWizard*
>(
this ),
730 if ( xContainer.is() )
733 Reference< XPropertySet >( xContainer->getByName( sCommand ), UNO_QUERY_THROW ) ) );
742 throw IllegalArgumentException(
743 DBA_RES( STR_CTW_ERROR_NO_QUERY ),
744 *
const_cast< CopyTableWizard*
>(
this ),
750 return pSourceObject;
753void CopyTableWizard::impl_extractSourceResultSet_throw(
const Reference< XPropertySet >& i_rDescriptor )
755 Reference< XPropertySetInfo > xPSI( i_rDescriptor->getPropertySetInfo(), UNO_SET_THROW );
770 if ( bHasSelection && !bHasResultSet )
771 throw IllegalArgumentException(
"A result set is needed when specifying a selection to copy.",
778 if ( !xRowLocate.is() )
780 ::dbtools::throwGenericSQLException(
781 DBA_RES(STR_CTW_COPY_SOURCE_NEEDS_BOOKMARKS),
788SharedConnection CopyTableWizard::impl_extractConnection_throw(
const Reference< XPropertySet >& _rxDataSourceDescriptor,
789 Reference< XInteractionHandler >& _out_rxDocInteractionHandler )
const
793 OSL_PRECOND( _rxDataSourceDescriptor.is(),
"CopyTableWizard::impl_extractConnection_throw: no descriptor!" );
794 if ( !_rxDataSourceDescriptor.is() )
797 Reference< XInteractionHandler > xInteractionHandler;
801 Reference< XPropertySetInfo > xPSI( _rxDataSourceDescriptor->getPropertySetInfo(), UNO_SET_THROW );
806 Reference< XConnection > xPure;
810 if ( xConnection.
is() )
813 SAL_WARN_IF( !xInteractionHandler.is(),
"dbaccess.ui",
"CopyTableWizard::impl_extractConnection_throw: lcl_getInteractionHandler_throw returned nonsense!" );
818 OUString sDataSource, sDatabaseLocation;
825 Reference< XDatabaseContext > xDatabaseContext = DatabaseContext::create(
m_xContext );
826 Reference< XDataSource > xDataSource;
827 if ( !sDataSource.isEmpty() )
828 xDataSource.set( xDatabaseContext->getByName( sDataSource ), UNO_QUERY_THROW );
829 if ( !xDataSource.is() && !sDatabaseLocation.isEmpty() )
830 xDataSource.set( xDatabaseContext->getByName( sDatabaseLocation ), UNO_QUERY_THROW );
832 if ( xDataSource.is() )
836 SAL_WARN_IF( !xInteractionHandler.is(),
"dbaccess.ui",
"CopyTableWizard::impl_extractConnection_throw: lcl_getInteractionHandler_throw returned nonsense!" );
837 if ( xInteractionHandler.is() )
839 Reference< XCompletedConnection > xInteractiveConnection( xDataSource, UNO_QUERY );
840 if ( xInteractiveConnection.is() )
845 if ( !xConnection.
is() )
851 if ( xConnection.
is() )
855 OUString sConnectionResource;
856 Sequence< PropertyValue > aConnectionInfo;
862 Reference< XDriverManager > xDriverManager;
864 xDriverManager.set( ConnectionPool::create(
m_xContext ), UNO_QUERY_THROW );
865 }
catch(
const Exception& ) { }
866 if ( !xDriverManager.is() )
868 xDriverManager.set( DriverManager::create(
m_xContext ), UNO_QUERY_THROW );
870 if ( aConnectionInfo.hasElements() )
871 xConnection.
set( xDriverManager->getConnectionWithInfo( sConnectionResource, aConnectionInfo ), UNO_SET_THROW );
873 xConnection.
set( xDriverManager->getConnection( sConnectionResource ), UNO_SET_THROW );
878 _out_rxDocInteractionHandler = xInteractionHandler;
885 OSL_PRECOND(
m_xSourceConnection.
is(),
"CopyTableWizard::impl_createSourceStatement_throw: illegal call!" );
887 throw RuntimeException(
"CopyTableWizard::impl_createSourceStatement_throw: illegal call!", *
const_cast< CopyTableWizard*
>(
this ));
910 if ( xComposer.
is() )
912 xComposer->setQuery( sQueryCommand );
914 Reference< XParameters > xStatementParams( xStatement, UNO_QUERY );
915 OSL_ENSURE( xStatementParams.is(),
"CopyTableWizard::impl_createSourceStatement_throw: no access to the statement's parameters!" );
918 if ( xStatementParams.is() )
921 "CopyTableWizard::impl_createSourceStatement_throw: no interaction handler for the parameters request!" );
933 throw RuntimeException(
"No case matched, this should not have survived the initialization phase", *
const_cast< CopyTableWizard*
>(
this ));
944 ValueTransfer( std::vector< sal_Int32 > _rColTypes,
945 const Reference< XRow >& _rxSource,
const Reference< XParameters >& _rxDest )
952 template<
typename VALUE_TYPE >
953 void transferValue( sal_Int32 _nSourcePos, sal_Int32 _nDestPos,
954 VALUE_TYPE ( SAL_CALL XRow::*_pGetter )( sal_Int32 ),
955 void (SAL_CALL XParameters::*_pSetter)( sal_Int32, VALUE_TYPE ) )
964 template<
typename VALUE_TYPE >
965 void transferComplexValue( sal_Int32 _nSourcePos, sal_Int32 _nDestPos,
966 VALUE_TYPE ( SAL_CALL XRow::*_pGetter )( sal_Int32 ),
967 void (SAL_CALL XParameters::*_pSetter)( sal_Int32,
const VALUE_TYPE& ) )
969 const VALUE_TYPE
value( (
m_xSource.get()->*_pGetter)( _nSourcePos ) );
982bool CopyTableWizard::impl_processCopyError_nothrow(
const CopyTableRowEvent& _rEvent )
989 Reference< XCopyTableListener > xListener( aIter.
next() );
990 sal_Int16 nListenerChoice = xListener->copyRowError( _rEvent );
991 switch ( nListenerChoice )
993 case CopyTableContinuation::Proceed:
return true;
994 case CopyTableContinuation::CallNextHandler:
continue;
995 case CopyTableContinuation::Cancel:
return false;
996 case CopyTableContinuation::AskUser:
break;
999 SAL_WARN(
"dbaccess.ui",
"CopyTableWizard::impl_processCopyError_nothrow: invalid listener response!" );
1005 catch(
const Exception& )
1015 aError.Context = *
this;
1016 aError.Message =
DBA_RES(STR_ERROR_OCCURRED_WHILE_COPYING);
1019 if ( aInfo.isValid() )
1020 aError.NextException = _rEvent.Error;
1025 OSL_VERIFY( _rEvent.Error >>= aException );
1026 SQLContext aContext;
1027 aContext.Context = aException.Context;
1028 aContext.Message = aException.Message;
1029 aContext.Details = _rEvent.Error.getValueTypeName();
1030 aError.NextException <<= aContext;
1036 xRequest->addContinuation( xYes );
1037 xRequest->addContinuation( new ::comphelper::OInteractionDisapprove );
1040 "CopyTableWizard::impl_processCopyError_nothrow: we always should have an interaction handler!" );
1044 if ( xYes->wasSelected() )
1048 catch(
const Exception& )
1057void CopyTableWizard::impl_copyRows_throw(
const Reference< XResultSet >& _rxSourceResultSet,
1058 const Reference< XPropertySet >& _rxDestTable )
1060 OSL_PRECOND(
m_xDestConnection.
is(),
"CopyTableWizard::impl_copyRows_throw: illegal call!" );
1062 throw RuntimeException(
"m_xDestConnection is set to null, CopyTableWizard::impl_copyRows_throw: illegal call!", *
this );
1064 Reference< XDatabaseMetaData > xDestMetaData(
m_xDestConnection->getMetaData(), UNO_SET_THROW );
1066 const OCopyTableWizard& rWizard = impl_getDialog_throw();
1067 ODatabaseExport::TPositions aColumnPositions = rWizard.GetColumnPositions();
1068 const bool bShouldCreatePrimaryKey = rWizard.shouldCreatePrimaryKey();
1070 Reference< XRow > xRow ( _rxSourceResultSet, UNO_QUERY_THROW );
1071 Reference< XRowLocate > xRowLocate ( _rxSourceResultSet, UNO_QUERY_THROW );
1073 Reference< XResultSetMetaDataSupplier > xSuppResMeta( _rxSourceResultSet, UNO_QUERY_THROW );
1074 Reference< XResultSetMetaData> xMeta( xSuppResMeta->getMetaData() );
1077 sal_Int32
nCount = xMeta->getColumnCount();
1078 std::vector< sal_Int32 > aSourceColTypes;
1079 aSourceColTypes.reserve( nCount + 1 );
1080 aSourceColTypes.push_back( -1 );
1082 std::vector< sal_Int32 > aSourcePrec;
1083 aSourcePrec.reserve( nCount + 1 );
1084 aSourcePrec.push_back( -1 );
1086 for ( sal_Int32 k=1; k <=
nCount; ++k )
1088 aSourceColTypes.push_back( xMeta->getColumnType( k ) );
1089 aSourcePrec.push_back( xMeta->getPrecision( k ) );
1093 Reference< XPreparedStatement > xStatement( ODatabaseExport::createPreparedStatement( xDestMetaData, _rxDestTable, aColumnPositions ), UNO_SET_THROW );
1094 Reference< XParameters > xStatementParams( xStatement, UNO_QUERY_THROW );
1100 sal_Int32 nRowCount = 0;
1101 bool bContinue =
false;
1103 CopyTableRowEvent aCopyEvent;
1104 aCopyEvent.Source = *
this;
1105 aCopyEvent.SourceData = _rxSourceResultSet;
1110 if ( bSelectedRecordsOnly )
1112 if ( pSelectedRow != pSelEnd )
1116 bContinue = xRowLocate->moveToBookmark( *pSelectedRow );
1121 OSL_VERIFY( *pSelectedRow >>= nPos );
1122 bContinue = _rxSourceResultSet->absolute( nPos );
1128 bContinue = _rxSourceResultSet->next();
1137 aCopyEvent.Error.clear();
1140 bool bInsertedPrimaryKey =
false;
1144 sal_Int32 nSourceColumn( 1 );
1145 ValueTransfer aTransfer( aSourceColTypes, xRow, xStatementParams );
1147 for (
auto const& rColumnPos : aColumnPositions )
1149 sal_Int32 nDestColumn = rColumnPos.first;
1157 if ( bShouldCreatePrimaryKey && !bInsertedPrimaryKey )
1159 xStatementParams->setInt( 1, nRowCount );
1161 bInsertedPrimaryKey =
true;
1165 if ( ( nSourceColumn < 1 ) || (
o3tl::make_unsigned(nSourceColumn) >= aSourceColTypes.size() ) )
1167 ::dbtools::throwSQLException(
"Internal error: invalid column type index.",
1168 ::dbtools::StandardSQLState::INVALID_DESCRIPTOR_INDEX, *
this);
1171 switch ( aSourceColTypes[ nSourceColumn ] )
1173 case DataType::DOUBLE:
1174 case DataType::REAL:
1175 aTransfer.transferValue( nSourceColumn, nDestColumn, &XRow::getDouble, &XParameters::setDouble );
1178 case DataType::CHAR:
1179 case DataType::VARCHAR:
1180 case DataType::LONGVARCHAR:
1181 case DataType::DECIMAL:
1182 case DataType::NUMERIC:
1183 aTransfer.transferComplexValue( nSourceColumn, nDestColumn, &XRow::getString, &XParameters::setString );
1186 case DataType::BIGINT:
1187 aTransfer.transferValue( nSourceColumn, nDestColumn, &XRow::getLong, &XParameters::setLong );
1190 case DataType::FLOAT:
1191 aTransfer.transferValue( nSourceColumn, nDestColumn, &XRow::getFloat, &XParameters::setFloat );
1194 case DataType::LONGVARBINARY:
1195 case DataType::BINARY:
1196 case DataType::VARBINARY:
1197 aTransfer.transferComplexValue( nSourceColumn, nDestColumn, &XRow::getBytes, &XParameters::setBytes );
1200 case DataType::DATE:
1201 aTransfer.transferComplexValue( nSourceColumn, nDestColumn, &XRow::getDate, &XParameters::setDate );
1204 case DataType::TIME:
1205 aTransfer.transferComplexValue( nSourceColumn, nDestColumn, &XRow::getTime, &XParameters::setTime );
1208 case DataType::TIMESTAMP:
1209 aTransfer.transferComplexValue( nSourceColumn, nDestColumn, &XRow::getTimestamp, &XParameters::setTimestamp );
1213 if ( aSourcePrec[nSourceColumn] > 1 )
1215 aTransfer.transferComplexValue( nSourceColumn, nDestColumn, &XRow::getBytes, &XParameters::setBytes );
1219 case DataType::BOOLEAN:
1220 aTransfer.transferValue( nSourceColumn, nDestColumn, &XRow::getBoolean, &XParameters::setBoolean );
1223 case DataType::TINYINT:
1224 aTransfer.transferValue( nSourceColumn, nDestColumn, &XRow::getByte, &XParameters::setByte );
1227 case DataType::SMALLINT:
1228 aTransfer.transferValue( nSourceColumn, nDestColumn, &XRow::getShort, &XParameters::setShort );
1231 case DataType::INTEGER:
1232 aTransfer.transferValue( nSourceColumn, nDestColumn, &XRow::getInt, &XParameters::setInt );
1235 case DataType::BLOB:
1236 aTransfer.transferComplexValue( nSourceColumn, nDestColumn, &XRow::getBlob, &XParameters::setBlob );
1239 case DataType::CLOB:
1240 aTransfer.transferComplexValue( nSourceColumn, nDestColumn, &XRow::getClob, &XParameters::setClob );
1245 OUString aMessage(
DBA_RES( STR_CTW_UNSUPPORTED_COLUMN_TYPE ) );
1247 aMessage = aMessage.replaceFirst(
"$type$", OUString::number( aSourceColTypes[ nSourceColumn ] ) );
1248 aMessage = aMessage.replaceFirst(
"$pos$", OUString::number( nSourceColumn ) );
1250 ::dbtools::throwSQLException(
1252 ::dbtools::StandardSQLState::INVALID_SQL_DATA_TYPE,
1259 xStatement->executeUpdate();
1264 catch(
const Exception& )
1267 aCopyEvent.Error = ::cppu::getCaughtException();
1270 if ( aCopyEvent.Error.hasValue() )
1271 bContinue = impl_processCopyError_nothrow( aCopyEvent );
1276void CopyTableWizard::impl_doCopy_nothrow()
1282 OCopyTableWizard& rWizard( impl_getDialog_throw() );
1285 Reference< XPropertySet > xTable;
1287 switch ( rWizard.getOperation() )
1289 case CopyTableOperation::CopyDefinitionOnly:
1290 case CopyTableOperation::CopyDefinitionAndData:
1292 xTable = rWizard.createTable();
1296 SAL_WARN(
"dbaccess.ui",
"CopyTableWizard::impl_doCopy_nothrow: createTable should throw here, shouldn't it?" );
1300 if( CopyTableOperation::CopyDefinitionOnly == rWizard.getOperation() )
1306 case CopyTableOperation::AppendData:
1309 assert((rWizard.getOperation() == CopyTableOperation::CopyDefinitionAndData) ||
1310 (rWizard.getOperation() == CopyTableOperation::AppendData));
1311 assert((rWizard.getOperation() == CopyTableOperation::CopyDefinitionAndData) == xTable.is());
1314 assert(rWizard.getOperation() == CopyTableOperation::AppendData);
1315 xTable = rWizard.getTable();
1318 SAL_WARN(
"dbaccess.ui",
"CopyTableWizard::impl_doCopy_nothrow: getTable should throw here, shouldn't it?" );
1335 if ( bIsSameConnection && bIsTable )
1340 m_xDestConnection->createStatement()->executeUpdate( impl_getServerSideCopyStatement_throw(xTable) );
1343 catch(
const Exception& )
1351 xSourceStatement.
set( impl_createSourceStatement_throw(), UNO_SET_THROW );
1352 xSourceResultSet.
set( xSourceStatement->executeQuery(), UNO_SET_THROW );
1356 if ( xSourceResultSet.
is() )
1357 impl_copyRows_throw( xSourceResultSet, xTable );
1360 const Reference< XDatabaseMetaData > xDestMetaData(
m_xDestConnection->getMetaData(), UNO_SET_THROW );
1361 OUString sDatabaseDest = xDestMetaData->getDatabaseProductName().toAsciiLowerCase();
1363 const bool bShouldCreatePrimaryKey = rWizard.shouldCreatePrimaryKey();
1364 if ( !bShouldCreatePrimaryKey && (sDatabaseDest.indexOf(
"firebird") != -1) )
1366 const OUString sComposedTableName = ::dbtools::composeTableName( xDestMetaData, xTable, ::dbtools::EComposeRule::InDataManipulation,
true );
1368 OUString aSchema,aTable;
1369 xTable->getPropertyValue(
"SchemaName") >>= aSchema;
1370 xTable->getPropertyValue(
"Name") >>= aTable;
1371 Any aCatalog = xTable->getPropertyValue(
"CatalogName");
1373 const Reference< XResultSet > xResultPKCL(xDestMetaData->getPrimaryKeys(aCatalog,aSchema,aTable));
1374 Reference< XRow > xRowPKCL(xResultPKCL, UNO_QUERY_THROW);
1376 if ( xRowPKCL.is() )
1378 if (xResultPKCL->next())
1380 sPKCL = xRowPKCL->getString(4);
1384 if (!sPKCL.isEmpty())
1386 OUString strSql =
"SELECT MAX(\"" + sPKCL +
"\") FROM " + sComposedTableName;
1388 Reference< XResultSet > xResultMAXNUM(
m_xDestConnection->createStatement()->executeQuery(strSql));
1389 Reference< XRow > xRow(xResultMAXNUM, UNO_QUERY_THROW);
1391 sal_Int64 maxVal = -1L;
1392 if (xResultMAXNUM->next())
1394 maxVal = xRow->getLong(1);
1399 strSql =
"ALTER TABLE " + sComposedTableName +
" ALTER \"" + sPKCL +
"\" RESTART WITH " + OUString::number(maxVal + 1);
1408 case CopyTableOperation::CreateAsView:
1409 rWizard.createView();
1413 SAL_WARN(
"dbaccess.ui",
"CopyTableWizard::impl_doCopy_nothrow: What operation, please?" );
1417 catch(
const Exception& )
1419 aError = ::cppu::getCaughtException();
1423 SQLException aSQLError;
1424 if ( ( aError >>= aSQLError ) && ( aSQLError.ErrorCode == ::dbtools::ParameterInteractionCancelled ) )
1438 catch(
const Exception& )
1445OUString CopyTableWizard::impl_getServerSideCopyStatement_throw(
const Reference< XPropertySet >& _xTable)
1447 const Reference<XColumnsSupplier> xDestColsSup(_xTable,UNO_QUERY_THROW);
1448 const Sequence< OUString> aDestColumnNames = xDestColsSup->getColumns()->getElementNames();
1449 const Reference< XDatabaseMetaData > xDestMetaData(
m_xDestConnection->getMetaData(), UNO_SET_THROW );
1450 const OUString
sQuote = xDestMetaData->getIdentifierQuoteString();
1451 OUStringBuffer sColumns;
1453 for (
auto const & rColumnPositionPair : impl_getDialog_throw().GetColumnPositions() )
1457 if ( !sColumns.isEmpty() )
1458 sColumns.append(
",");
1459 sColumns.append(sQuote + aDestColumnNames[rColumnPositionPair.second - 1] + sQuote);
1462 const OUString sComposedTableName = ::dbtools::composeTableName( xDestMetaData, _xTable, ::dbtools::EComposeRule::InDataManipulation,
true );
1463 OUString sSql(
"INSERT INTO " + sComposedTableName +
" ( " + sColumns +
" ) " +
m_pSourceObject->getSelectStatement());
1468void SAL_CALL CopyTableWizard::initialize(
const Sequence< Any >& _rArguments )
1470 ::osl::MutexGuard aGuard( m_aMutex );
1471 if ( isInitialized() )
1472 throw AlreadyInitializedException( OUString(), *
this );
1474 sal_Int32 nArgCount( _rArguments.getLength() );
1475 if ( ( nArgCount != 2 ) && ( nArgCount != 3 ) )
1476 throw IllegalArgumentException(
1477 DBA_RES( STR_CTW_ILLEGAL_PARAMETER_COUNT ),
1484 if ( nArgCount == 3 )
1487 throw IllegalArgumentException(
1488 DBA_RES( STR_CTW_ERROR_INVALID_INTERACTIONHANDLER ),
1496 Reference< XInteractionHandler > xSourceDocHandler;
1497 Reference< XPropertySet > xSourceDescriptor( impl_ensureDataAccessDescriptor_throw( _rArguments, 0,
m_xSourceConnection, xSourceDocHandler ) );
1498 impl_checkForUnsupportedSettings_throw( xSourceDescriptor );
1500 impl_extractSourceResultSet_throw( xSourceDescriptor );
1502 Reference< XInteractionHandler > xDestDocHandler;
1503 impl_ensureDataAccessDescriptor_throw( _rArguments, 1,
m_xDestConnection, xDestDocHandler );
1509 if (xInteractionHandler.is())
1511 Any aParentWindow(xInteractionHandler->getPropertyValue(
"ParentWindow"));
1512 aParentWindow >>= m_xParent;
1516 catch(
const SQLException& ) {
throw; }
1517 catch(
const Exception& )
1519 throw WrappedTargetException(
1520 DBA_RES( STR_CTW_ERROR_DURING_INITIALIZATION ),
1522 ::cppu::getCaughtException()
1529 return *getArrayHelper();
1534 Sequence< Property > aProps;
1535 describeProperties( aProps );
1536 return new ::cppu::OPropertyArrayHelper( aProps );
1539std::unique_ptr<weld::DialogController> CopyTableWizard::createDialog(
const css::uno::Reference<css::awt::XWindow>& rParent)
1541 OSL_PRECOND( isInitialized(),
"CopyTableWizard::createDialog: not initialized!" );
1544 auto xWizard = std::make_unique<OCopyTableWizard>(
1554 impl_attributesToDialog_nothrow(*xWizard);
1559void CopyTableWizard::executedDialog( sal_Int16 _nExecutionResult )
1561 CopyTableWizard_DialogBase::executedDialog( _nExecutionResult );
1563 if ( _nExecutionResult == RET_OK )
1564 impl_doCopy_nothrow();
1568 impl_dialogToAttributes_nothrow( impl_getDialog_throw() );
1573extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
1575 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any>
const& )
1577 return cppu::acquire(new ::dbaui::CopyTableWizard(context));
#define COLUMN_POSITION_NOT_FOUND
Reference< XExecutableDialog > m_xDialog
static weld::Window * GetFrameWeld(const css::uno::Reference< css::awt::XWindow > &rWindow)
VALUE_TYPE getOrDefault(const OUString &_rValueName, const VALUE_TYPE &_rDefault) const
bool hasMoreElements() const
css::uno::Reference< ListenerT > const & next()
const css::uno::Reference< INTERFACE > & getTyped() const
void reset(const css::uno::Reference< INTERFACE > &_rxComponent, AssignmentMode _eMode=TakeOwnership)
bool set(const css::uno::BaseReference &_rRef, css::uno::UnoReference_Query _query)
Optional< OUString > m_aPrimaryKeyName
const Reference< XParameters > m_xDest
const Reference< XRow > m_xSource
::comphelper::OInterfaceContainerHelper3< XCopyTableListener > m_aCopyTableListeners
sal_Int16 m_nOverrideExecutionResult
SharedConnection m_xSourceConnection
std::unique_ptr< ICopyTableSourceObject > m_pSourceObject
Reference< XComponentContext > m_xContext
const std::vector< sal_Int32 > m_ColTypes
OUString m_sDestinationTable
Reference< XInteractionHandler > m_xInteractionHandler
CopyTableWizard & m_rWizard
Reference< XResultSet > m_xSourceResultSet
bool m_bSourceSelectionBookmarks
Sequence< Any > m_aSourceSelection
SharedConnection m_xDestConnection
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * org_openoffice_comp_dbu_CopyTableWizard_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
bool m_bUseHeaderLineAsColumnNames
OString exceptionToString(const css::uno::Any &caught)
#define TOOLS_WARN_EXCEPTION(area, stream)
#define DBG_UNHANDLED_EXCEPTION(...)
Reference< XSingleServiceFactory > xFactory
Sequence< PropertyValue > aArguments
#define SAL_WARN_IF(condition, area, stream)
#define SAL_WARN(area, stream)
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
::svt::OGenericUnoDialog CopyTableWizard_DialogBase
::utl::SharedUNOComponent< css::sdbc::XConnection > SharedConnection
::cppu::ImplInheritanceHelper< CopyTableWizard_DialogBase, XCopyTableWizard > CopyTableWizard_Base
::osl::Mutex & getMutex()
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
constexpr OUStringLiteral PROPERTY_COMMAND(u"Command")
constexpr OUStringLiteral PROPERTY_CONNECTION_RESOURCE(u"ConnectionResource")
constexpr OUStringLiteral PROPERTY_HAVING_CLAUSE(u"HavingClause")
constexpr OUStringLiteral PROPERTY_BOOKMARK_SELECTION(u"BookmarkSelection")
constexpr OUStringLiteral PROPERTY_CONNECTION_INFO(u"ConnectionInfo")
constexpr OUStringLiteral PROPERTY_RESULT_SET(u"ResultSet")
constexpr OUStringLiteral SERVICE_NAME_SINGLESELECTQUERYCOMPOSER
constexpr OUStringLiteral PROPERTY_GROUP_BY(u"GroupBy")
constexpr OUStringLiteral PROPERTY_SELECTION(u"Selection")
constexpr OUStringLiteral PROPERTY_FILTER(u"Filter")
constexpr OUStringLiteral PROPERTY_ACTIVE_CONNECTION(u"ActiveConnection")
constexpr OUStringLiteral PROPERTY_DATABASE_LOCATION(u"DatabaseLocation")
constexpr OUStringLiteral PROPERTY_DATASOURCENAME(u"DataSourceName")
constexpr OUStringLiteral PROPERTY_COMMAND_TYPE(u"CommandType")
constexpr OUStringLiteral PROPERTY_ORDER(u"Order")