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