1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: WCopyTable.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef DBAUI_WIZ_COPYTABLEDIALOG_HXX
32 #define DBAUI_WIZ_COPYTABLEDIALOG_HXX
34 #ifndef _COM_SUN_STAR_CONTAINER_XNAMEACCESS_HPP_
35 #include <com/sun/star/container/XNameAccess.hpp>
37 #ifndef _COM_SUN_STAR_SDBC_XCONNECTION_HPP_
38 #include <com/sun/star/sdbc/XConnection.hpp>
40 #ifndef _COM_SUN_STAR_SDBC_XRESULTSET_HPP_
41 #include <com/sun/star/sdbc/XResultSet.hpp>
43 #ifndef _COM_SUN_STAR_SDBC_XRESULTSETMETADATA_HPP_
44 #include <com/sun/star/sdbc/XResultSetMetaData.hpp>
46 #ifndef _COM_SUN_STAR_SDBC_XDATABASEMETADATA_HPP_
47 #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
49 #ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_
50 #include <com/sun/star/beans/XPropertySet.hpp>
52 #ifndef _COMPHELPER_STLTYPES_HXX_
53 #include <comphelper/stl_types.hxx>
55 #ifndef DBAUI_TYPEINFO_HXX
56 #include "TypeInfo.hxx"
58 #ifndef _SV_BUTTON_HXX
59 #include <vcl/button.hxx>
61 #ifndef _SVT_WIZDLG_HXX
62 #include <svtools/wizdlg.hxx>
64 #ifndef DBAUI_DATABASEEXPORT_HXX
65 #include "DExport.hxx"
67 #ifndef DBAUI_WIZ_TABBPAGE_HXX
68 #include "WTabPage.hxx"
70 #ifndef DBAUI_FIELDDESCRIPTIONS_HXX
71 #include "FieldDescriptions.hxx"
73 #ifndef _COM_SUN_STAR_SDBCX_XCOLUMNSSUPPLIER_HPP_
74 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
76 #ifndef _COM_SUN_STAR_SDBCX_XKEYSSUPPLIER_HPP_
77 #include <com/sun/star/sdbcx/XKeysSupplier.hpp>
79 #ifndef _SV_LSTBOX_HXX
80 #include <vcl/lstbox.hxx>
88 typedef ::std::unary_function
< ::rtl::OUString
,bool> TColumnFindFunctorType
;
89 class TColumnFindFunctor
: public TColumnFindFunctorType
92 virtual bool operator()(const ::rtl::OUString
& _sColumnName
) const = 0;
95 class TExportColumnFindFunctor
: public TColumnFindFunctor
97 ODatabaseExport::TColumns
* m_pColumns
;
99 TExportColumnFindFunctor(ODatabaseExport::TColumns
* _pColumns
)
101 m_pColumns
= _pColumns
;
103 inline bool operator()(const ::rtl::OUString
& _sColumnName
) const
105 return m_pColumns
->find(_sColumnName
) != m_pColumns
->end();
109 class TMultiListBoxEntryFindFunctor
: public TColumnFindFunctor
111 ::comphelper::TStringMixEqualFunctor m_aCase
;
112 ::std::vector
< ::rtl::OUString
>* m_pVector
;
114 TMultiListBoxEntryFindFunctor(::std::vector
< ::rtl::OUString
>* _pVector
,
115 const ::comphelper::TStringMixEqualFunctor
& _aCase
)
120 inline bool operator()(const ::rtl::OUString
& _sColumnName
) const
122 return ::std::find_if(m_pVector
->begin(),m_pVector
->end(),
123 ::std::bind2nd(m_aCase
, _sColumnName
)) != m_pVector
->end();
127 // ========================================================
128 // ICopyTableSourceObject
129 // ========================================================
130 /** interface to an object to copy to another DB, using the OCopyTableWizard
132 when the wizard is used to copy an object to another DB, it usually requires
133 a sdbcx-level or sdb-level object (a css.sdbcx.Table or css.sdb.Query, that is).
135 However, to also support copying tables from sdbc-level connections, we allow to
136 work with the object name only. This implies some less features (like copying the
137 UI settings of a table is not done), but still allows to copy definition and data.
139 class ICopyTableSourceObject
142 /// retrieves the fully qualified name of the object to copy
143 virtual ::rtl::OUString
getQualifiedObjectName() const = 0;
144 /// determines whether the object is a view
145 virtual bool isView() const = 0;
146 /** copies the UI settings of the object to the given target object. Might be
147 ignored by implementations which do not have Ui settings.
149 virtual void copyUISettingsTo( const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
>& _rxObject
) const = 0;
150 /// retrieves the column names of the to-be-copied object
151 virtual ::com::sun::star::uno::Sequence
< ::rtl::OUString
>
152 getColumnNames() const = 0;
153 /// retrieves the names of the primary keys of the to-be-copied object
154 virtual ::com::sun::star::uno::Sequence
< ::rtl::OUString
>
155 getPrimaryKeyColumnNames() const = 0;
156 /// creates a OFieldDescription for the given column of the to-be-copied object
157 virtual OFieldDescription
* createFieldDescription( const ::rtl::OUString
& _rColumnName
) const = 0;
158 /// returns the SELECT statement which can be used to retrieve the data of the to-be-copied object
159 virtual ::rtl::OUString
getSelectStatement() const = 0;
161 /** copies the filter and sorting
165 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 = 0;
167 /** returns the prepared statement which can be used to retrieve the data of the to-be-copied object
169 The default implementation of this method will simply prepare a statement with the return value
170 of ->getSelectStatement.
172 virtual ::utl::SharedUNOComponent
< ::com::sun::star::sdbc::XPreparedStatement
>
173 getPreparedSelectStatement() const = 0;
175 virtual ~ICopyTableSourceObject();
178 // ========================================================
180 // ========================================================
181 class ObjectCopySource
: public ICopyTableSourceObject
184 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
> m_xConnection
;
185 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XDatabaseMetaData
> m_xMetaData
;
186 ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
> m_xObject
;
187 ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySetInfo
> m_xObjectPSI
;
188 ::com::sun::star::uno::Reference
< ::com::sun::star::container::XNameAccess
> m_xObjectColumns
;
192 const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>& _rxConnection
,
193 const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
>& _rxObject
196 // ICopyTableSourceObject overridables
197 virtual ::rtl::OUString
getQualifiedObjectName() const;
198 virtual bool isView() const;
199 virtual void copyUISettingsTo( const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
>& _rxObject
) const;
200 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;
201 virtual ::com::sun::star::uno::Sequence
< ::rtl::OUString
>
202 getColumnNames() const;
203 virtual ::com::sun::star::uno::Sequence
< ::rtl::OUString
>
204 getPrimaryKeyColumnNames() const;
205 virtual OFieldDescription
* createFieldDescription( const ::rtl::OUString
& _rColumnName
) const;
206 virtual ::rtl::OUString
getSelectStatement() const;
207 virtual ::utl::SharedUNOComponent
< ::com::sun::star::sdbc::XPreparedStatement
>
208 getPreparedSelectStatement() const;
211 // ========================================================
212 // NamedTableCopySource
213 // ========================================================
214 class NamedTableCopySource
: public ICopyTableSourceObject
217 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
> m_xConnection
;
218 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XDatabaseMetaData
> m_xMetaData
;
219 ::rtl::OUString m_sTableName
;
220 ::rtl::OUString m_sTableCatalog
;
221 ::rtl::OUString m_sTableSchema
;
222 ::rtl::OUString m_sTableBareName
;
223 ::std::vector
< OFieldDescription
> m_aColumnInfo
;
224 ::utl::SharedUNOComponent
< ::com::sun::star::sdbc::XPreparedStatement
> m_xStatement
;
227 NamedTableCopySource(
228 const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>& _rxConnection
,
229 const ::rtl::OUString
& _rTableName
232 // ICopyTableSourceObject overridables
233 virtual ::rtl::OUString
getQualifiedObjectName() const;
234 virtual bool isView() const;
235 virtual void copyUISettingsTo( const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
>& _rxObject
) const;
236 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;
237 virtual ::com::sun::star::uno::Sequence
< ::rtl::OUString
>
238 getColumnNames() const;
239 virtual ::com::sun::star::uno::Sequence
< ::rtl::OUString
>
240 getPrimaryKeyColumnNames() const;
241 virtual OFieldDescription
* createFieldDescription( const ::rtl::OUString
& _rColumnName
) const;
242 virtual ::rtl::OUString
getSelectStatement() const;
243 virtual ::utl::SharedUNOComponent
< ::com::sun::star::sdbc::XPreparedStatement
>
244 getPreparedSelectStatement() const;
247 void impl_ensureColumnInfo_throw();
248 ::utl::SharedUNOComponent
< ::com::sun::star::sdbc::XPreparedStatement
>
249 impl_ensureStatement_throw();
252 // ========================================================
254 // ========================================================
255 class OCopyTableWizard
: public WizardDialog
257 friend class OWizColumnSelect
;
258 friend class OWizTypeSelect
;
259 friend class OWizTypeSelectControl
;
260 friend class OCopyTable
;
261 friend class OWizNameMatching
;
264 DECLARE_STL_MAP(::rtl::OUString
,::rtl::OUString
,::comphelper::UStringMixLess
,TNameMapping
);
266 enum Wizard_Button_Style
276 ODatabaseExport::TColumns m_vDestColumns
; // contains the columns
277 ODatabaseExport::TColumnVector m_aDestVec
; // the order to insert the columns
278 ODatabaseExport::TColumns m_vSourceColumns
;
279 ODatabaseExport::TColumnVector m_vSourceVec
;
282 CancelButton m_pbCancel
;
287 OTypeInfoMap m_aTypeInfo
;
288 ::std::vector
<OTypeInfoMap::iterator
> m_aTypeInfoIndex
;
289 OTypeInfoMap m_aDestTypeInfo
;
290 ::std::vector
<OTypeInfoMap::iterator
> m_aDestTypeInfoIndex
;
291 TNameMapping m_mNameMapping
;
293 ODatabaseExport::TPositions m_vColumnPos
;
294 ::std::vector
<sal_Int32
> m_vColumnTypes
;
296 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
> m_xDestConnection
;
298 const ICopyTableSourceObject
& m_rSourceObject
;
300 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XNumberFormatter
> m_xFormatter
;
301 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
> m_xFactory
;
303 String m_sTypeNames
; // these type names are the ones out of the resource file
304 sal_uInt32 m_nPageCount
;
305 sal_Bool m_bDeleteSourceColumns
;
306 bool m_bInterConnectionCopy
; // are we copying between different connections?
308 ::com::sun::star::lang::Locale m_aLocale
;
309 ::rtl::OUString m_sName
; // for a table the name is composed
310 ::rtl::OUString m_sSourceName
;
311 ::rtl::OUString m_aKeyName
;
312 TOTypeInfoSP m_pTypeInfo
; // default type
313 sal_Bool m_bAddPKFirstTime
;
314 sal_Int16 m_nOperation
;
315 Wizard_Button_Style m_ePressed
;
316 sal_Bool m_bCreatePrimaryKeyColumn
;
317 sal_Bool m_bUseHeaderLine
;
320 DECL_LINK( ImplPrevHdl
, PushButton
* );
321 DECL_LINK( ImplNextHdl
, PushButton
* );
322 DECL_LINK( ImplOKHdl
, OKButton
* );
323 DECL_LINK( ImplActivateHdl
, WizardDialog
* );
324 sal_Bool
CheckColumns(sal_Int32
& _rnBreakPos
);
325 void loadData( const ICopyTableSourceObject
& _rSourceObject
,
326 ODatabaseExport::TColumns
& _rColumns
,
327 ODatabaseExport::TColumnVector
& _rColVector
);
329 // need for table creation
330 void appendColumns( ::com::sun::star::uno::Reference
< ::com::sun::star::sdbcx::XColumnsSupplier
>& _rxColSup
, const ODatabaseExport::TColumnVector
* _pVec
, sal_Bool _bKeyColumns
= sal_False
) const;
331 void appendKey(::com::sun::star::uno::Reference
< ::com::sun::star::sdbcx::XKeysSupplier
>& _rxSup
,const ODatabaseExport::TColumnVector
* _pVec
) const;
332 // checks if the type is supported in the destination database
333 sal_Bool
supportsType(sal_Int32 _nDataType
,sal_Int32
& _rNewDataType
);
335 void impl_loadSourceData();
338 // used for copy tables or queries
341 const ::rtl::OUString
& _rDefaultName
,
342 sal_Int16 _nOperation
,
343 const ICopyTableSourceObject
& _rSourceObject
,
344 const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>& _xSourceConnection
,
345 const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>& _xConnection
,
346 const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& _rxORB
349 // used for importing rtf/html sources
352 const ::rtl::OUString
& _rDefaultName
,
353 sal_Int16 _nOperation
,
354 const ODatabaseExport::TColumns
& _rDestColumns
,
355 const ODatabaseExport::TColumnVector
& _rSourceColVec
,
356 const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>& _xConnection
,
357 const ::com::sun::star::uno::Reference
< ::com::sun::star::util::XNumberFormatter
>& _xFormatter
,
358 TypeSelectionPageFactory _pTypeSelectionPageFactory
,
359 SvStream
& _rTypeSelectionPageArg
,
360 const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& _rM
363 virtual ~OCopyTableWizard();
365 virtual long DeactivatePage();
366 OKButton
& GetOKButton() { return m_pbFinish
; }
367 Wizard_Button_Style
GetPressedButton() const { return m_ePressed
; }
368 void EnableButton(Wizard_Button_Style eStyle
,sal_Bool bEnable
);
369 void AddWizardPage(OWizardPage
* pPage
); // Page wird von OCopyTableWizard gelöscht
370 void RemoveWizardPage(OWizardPage
* pPage
); // Page goes again to user
371 void CheckButtons(); // checks which button can be disabled, enabled
373 // returns a vector where the position of a column and if the column is in the selection
374 // when not the value is COLUMN_POSITION_NOT_FOUND == (sal_uInt32)-1
375 ODatabaseExport::TPositions
GetColumnPositions() const { return m_vColumnPos
; }
376 ::std::vector
<sal_Int32
> GetColumnTypes() const { return m_vColumnTypes
; }
377 sal_Bool
UseHeaderLine() const { return m_bUseHeaderLine
; }
378 void setUseHeaderLine(sal_Bool _bUseHeaderLine
) { m_bUseHeaderLine
= _bUseHeaderLine
; }
380 void insertColumn(sal_Int32 _nPos
,OFieldDescription
* _pField
);
382 /** replaces a field description with another one. The name must not be known so far.
384 The pos inside the vector, 0 based.
388 The name of column to be replaced.
390 void replaceColumn(sal_Int32 _nPos
,OFieldDescription
* _pField
,const ::rtl::OUString
& _sOldName
);
392 /** returns whether a primary key should be created in the target database
394 sal_Bool
shouldCreatePrimaryKey() const;
395 void setCreatePrimaryKey( bool _bDoCreate
, const ::rtl::OUString
& _rSuggestedName
);
397 static bool supportsPrimaryKey( const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>& _rxConnection
);
398 bool supportsPrimaryKey() const { return supportsPrimaryKey( m_xDestConnection
); }
400 static bool supportsViews( const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>& _rxConnection
);
401 bool supportsViews() const { return supportsViews( m_xDestConnection
); }
403 /** returns the name of the primary key
405 The name of the primary key.
407 ::rtl::OUString
getPrimaryKeyName() const { return m_aKeyName
; }
409 TOTypeInfoSP
getTypeInfo(sal_Int32 _nPos
) const { return m_aTypeInfoIndex
[_nPos
]->second
; }
410 const OTypeInfoMap
* getTypeInfo() const { return &m_aTypeInfo
; }
412 TOTypeInfoSP
getDestTypeInfo(sal_Int32 _nPos
) const { return m_aDestTypeInfoIndex
[_nPos
]->second
; }
413 const OTypeInfoMap
* getDestTypeInfo() const { return &m_aDestTypeInfo
; }
415 ::com::sun::star::lang::Locale
GetLocale() const { return m_aLocale
; }
416 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XNumberFormatter
> GetFormatter() const { return m_xFormatter
; }
417 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
> GetFactory() const { return m_xFactory
; }
419 const ODatabaseExport::TColumns
* getSourceColumns() const{ return &m_vSourceColumns
; }
420 const ODatabaseExport::TColumnVector
* getSrcVector() const { return &m_vSourceVec
; }
421 ODatabaseExport::TColumns
* getDestColumns() { return &m_vDestColumns
; }
422 const ODatabaseExport::TColumnVector
* getDestVector() const { return &m_aDestVec
; }
423 ::rtl::OUString
getName() const { return m_sName
; }
425 /** clears the dest vectors
427 void clearDestColumns();
429 ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
> createTable();
430 ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
> createView() const;
431 sal_Int32
getMaxColumnNameLength() const;
433 void setOperation( const sal_Int16 _nOperation
);
434 sal_Int16
getOperation() const;
436 ::rtl::OUString
convertColumnName( const TColumnFindFunctor
& _rCmpFunctor
,
437 const ::rtl::OUString
& _sColumnName
,
438 const ::rtl::OUString
& _sExtraChars
,
439 sal_Int32 _nMaxNameLen
);
440 TOTypeInfoSP
convertType(const TOTypeInfoSP
&_pType
,sal_Bool
& _bNotConvert
);
442 ::rtl::OUString
createUniqueName(const ::rtl::OUString
& _sName
);
444 // displays a error message that a column type is not supported
445 void showColumnTypeNotSupported(const ::rtl::OUString
& _rColumnName
);
447 void removeColumnNameFromNameMap(const ::rtl::OUString
& _sName
);
451 #endif // DBAUI_WIZ_COPYTABLEDIALOG_HXX