1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef DBAUI_WIZ_COPYTABLEDIALOG_HXX
21 #define DBAUI_WIZ_COPYTABLEDIALOG_HXX
23 #include <com/sun/star/container/XNameAccess.hpp>
24 #include <com/sun/star/sdbc/XConnection.hpp>
25 #include <com/sun/star/sdbc/XResultSet.hpp>
26 #include <com/sun/star/sdbc/XResultSetMetaData.hpp>
27 #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
28 #include <com/sun/star/uno/XComponentContext.hpp>
29 #include <com/sun/star/beans/XPropertySet.hpp>
30 #include <comphelper/stl_types.hxx>
31 #include "TypeInfo.hxx"
32 #include <vcl/button.hxx>
33 #include <svtools/wizdlg.hxx>
34 #include "DExport.hxx"
35 #include "WTabPage.hxx"
36 #include "FieldDescriptions.hxx"
37 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
38 #include <com/sun/star/sdbcx/XKeysSupplier.hpp>
39 #include <com/sun/star/task/XInteractionHandler.hpp>
40 #include <vcl/lstbox.hxx>
46 typedef ::std::unary_function
< OUString
,bool> TColumnFindFunctorType
;
47 class TColumnFindFunctor
: public TColumnFindFunctorType
50 virtual bool operator()(const OUString
& _sColumnName
) const = 0;
53 ~TColumnFindFunctor() {}
56 class TExportColumnFindFunctor
: public TColumnFindFunctor
58 ODatabaseExport::TColumns
* m_pColumns
;
60 TExportColumnFindFunctor(ODatabaseExport::TColumns
* _pColumns
)
62 m_pColumns
= _pColumns
;
65 virtual ~TExportColumnFindFunctor() {}
67 inline bool operator()(const OUString
& _sColumnName
) const
69 return m_pColumns
->find(_sColumnName
) != m_pColumns
->end();
73 class TMultiListBoxEntryFindFunctor
: public TColumnFindFunctor
75 ::comphelper::TStringMixEqualFunctor m_aCase
;
76 ::std::vector
< OUString
>* m_pVector
;
78 TMultiListBoxEntryFindFunctor(::std::vector
< OUString
>* _pVector
,
79 const ::comphelper::TStringMixEqualFunctor
& _aCase
)
85 virtual ~TMultiListBoxEntryFindFunctor() {}
87 inline bool operator()(const OUString
& _sColumnName
) const
89 return ::std::find_if(m_pVector
->begin(),m_pVector
->end(),
90 ::std::bind2nd(m_aCase
, _sColumnName
)) != m_pVector
->end();
94 // ========================================================
95 // ICopyTableSourceObject
96 // ========================================================
97 /** interface to an object to copy to another DB, using the OCopyTableWizard
99 when the wizard is used to copy an object to another DB, it usually requires
100 a sdbcx-level or sdb-level object (a css.sdbcx.Table or css.sdb.Query, that is).
102 However, to also support copying tables from sdbc-level connections, we allow to
103 work with the object name only. This implies some less features (like copying the
104 UI settings of a table is not done), but still allows to copy definition and data.
106 class ICopyTableSourceObject
109 /// retrieves the fully qualified name of the object to copy
110 virtual OUString
getQualifiedObjectName() const = 0;
111 /// determines whether the object is a view
112 virtual bool isView() const = 0;
113 /** copies the UI settings of the object to the given target object. Might be
114 ignored by implementations which do not have Ui settings.
116 virtual void copyUISettingsTo( const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
>& _rxObject
) const = 0;
117 /// retrieves the column names of the to-be-copied object
118 virtual ::com::sun::star::uno::Sequence
< OUString
>
119 getColumnNames() const = 0;
120 /// retrieves the names of the primary keys of the to-be-copied object
121 virtual ::com::sun::star::uno::Sequence
< OUString
>
122 getPrimaryKeyColumnNames() const = 0;
123 /// creates a OFieldDescription for the given column of the to-be-copied object
124 virtual OFieldDescription
* createFieldDescription( const OUString
& _rColumnName
) const = 0;
125 /// returns the SELECT statement which can be used to retrieve the data of the to-be-copied object
126 virtual OUString
getSelectStatement() const = 0;
128 /** copies the filter and sorting
132 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;
134 /** returns the prepared statement which can be used to retrieve the data of the to-be-copied object
136 The default implementation of this method will simply prepare a statement with the return value
137 of ->getSelectStatement.
139 virtual ::utl::SharedUNOComponent
< ::com::sun::star::sdbc::XPreparedStatement
>
140 getPreparedSelectStatement() const = 0;
142 virtual ~ICopyTableSourceObject();
145 // ========================================================
147 // ========================================================
148 class ObjectCopySource
: public ICopyTableSourceObject
151 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
> m_xConnection
;
152 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XDatabaseMetaData
> m_xMetaData
;
153 ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
> m_xObject
;
154 ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySetInfo
> m_xObjectPSI
;
155 ::com::sun::star::uno::Reference
< ::com::sun::star::container::XNameAccess
> m_xObjectColumns
;
159 const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>& _rxConnection
,
160 const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
>& _rxObject
163 // ICopyTableSourceObject overridables
164 virtual OUString
getQualifiedObjectName() const;
165 virtual bool isView() const;
166 virtual void copyUISettingsTo( const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
>& _rxObject
) const;
167 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;
168 virtual ::com::sun::star::uno::Sequence
< OUString
>
169 getColumnNames() const;
170 virtual ::com::sun::star::uno::Sequence
< OUString
>
171 getPrimaryKeyColumnNames() const;
172 virtual OFieldDescription
* createFieldDescription( const OUString
& _rColumnName
) const;
173 virtual OUString
getSelectStatement() const;
174 virtual ::utl::SharedUNOComponent
< ::com::sun::star::sdbc::XPreparedStatement
>
175 getPreparedSelectStatement() const;
178 // ========================================================
179 // NamedTableCopySource
180 // ========================================================
181 class NamedTableCopySource
: 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 OUString m_sTableName
;
187 OUString m_sTableCatalog
;
188 OUString m_sTableSchema
;
189 OUString m_sTableBareName
;
190 ::std::vector
< OFieldDescription
> m_aColumnInfo
;
191 ::utl::SharedUNOComponent
< ::com::sun::star::sdbc::XPreparedStatement
> m_xStatement
;
194 NamedTableCopySource(
195 const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>& _rxConnection
,
196 const OUString
& _rTableName
199 // ICopyTableSourceObject overridables
200 virtual OUString
getQualifiedObjectName() const;
201 virtual bool isView() const;
202 virtual void copyUISettingsTo( const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
>& _rxObject
) const;
203 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;
204 virtual ::com::sun::star::uno::Sequence
< OUString
>
205 getColumnNames() const;
206 virtual ::com::sun::star::uno::Sequence
< OUString
>
207 getPrimaryKeyColumnNames() const;
208 virtual OFieldDescription
* createFieldDescription( const OUString
& _rColumnName
) const;
209 virtual OUString
getSelectStatement() const;
210 virtual ::utl::SharedUNOComponent
< ::com::sun::star::sdbc::XPreparedStatement
>
211 getPreparedSelectStatement() const;
214 void impl_ensureColumnInfo_throw();
215 ::utl::SharedUNOComponent
< ::com::sun::star::sdbc::XPreparedStatement
>
216 impl_ensureStatement_throw();
219 // ========================================================
221 // ========================================================
222 class OCopyTableWizard
: public WizardDialog
224 friend class OWizColumnSelect
;
225 friend class OWizTypeSelect
;
226 friend class OWizTypeSelectControl
;
227 friend class OCopyTable
;
228 friend class OWizNameMatching
;
231 DECLARE_STL_MAP(OUString
,OUString
,::comphelper::UStringMixLess
,TNameMapping
);
233 enum Wizard_Button_Style
243 ODatabaseExport::TColumns m_vDestColumns
; // contains the columns
244 ODatabaseExport::TColumnVector m_aDestVec
; // the order to insert the columns
245 ODatabaseExport::TColumns m_vSourceColumns
;
246 ODatabaseExport::TColumnVector m_vSourceVec
;
249 CancelButton m_pbCancel
;
254 OTypeInfoMap m_aTypeInfo
;
255 ::std::vector
<OTypeInfoMap::iterator
> m_aTypeInfoIndex
;
256 OTypeInfoMap m_aDestTypeInfo
;
257 ::std::vector
<OTypeInfoMap::iterator
> m_aDestTypeInfoIndex
;
258 TNameMapping m_mNameMapping
;
260 ODatabaseExport::TPositions m_vColumnPos
;
261 ::std::vector
<sal_Int32
> m_vColumnTypes
;
263 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
> m_xDestConnection
;
265 const ICopyTableSourceObject
& m_rSourceObject
;
267 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XNumberFormatter
> m_xFormatter
;
268 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
> m_xContext
;
269 ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
> m_xInteractionHandler
;
271 String m_sTypeNames
; // these type names are the ones out of the resource file
272 sal_uInt32 m_nPageCount
;
273 sal_Bool m_bDeleteSourceColumns
;
274 bool m_bInterConnectionCopy
; // are we copying between different connections?
276 ::com::sun::star::lang::Locale m_aLocale
;
277 OUString m_sName
; // for a table the name is composed
278 OUString m_sSourceName
;
280 TOTypeInfoSP m_pTypeInfo
; // default type
281 sal_Bool m_bAddPKFirstTime
;
282 sal_Int16 m_nOperation
;
283 Wizard_Button_Style m_ePressed
;
284 sal_Bool m_bCreatePrimaryKeyColumn
;
285 sal_Bool m_bUseHeaderLine
;
288 DECL_LINK( ImplPrevHdl
, void* );
289 DECL_LINK( ImplNextHdl
, void* );
290 DECL_LINK( ImplOKHdl
, void* );
291 DECL_LINK( ImplActivateHdl
, void* );
292 sal_Bool
CheckColumns(sal_Int32
& _rnBreakPos
);
293 void loadData( const ICopyTableSourceObject
& _rSourceObject
,
294 ODatabaseExport::TColumns
& _rColumns
,
295 ODatabaseExport::TColumnVector
& _rColVector
);
297 // need for table creation
298 void appendColumns( ::com::sun::star::uno::Reference
< ::com::sun::star::sdbcx::XColumnsSupplier
>& _rxColSup
, const ODatabaseExport::TColumnVector
* _pVec
, sal_Bool _bKeyColumns
= sal_False
) const;
299 void appendKey(::com::sun::star::uno::Reference
< ::com::sun::star::sdbcx::XKeysSupplier
>& _rxSup
,const ODatabaseExport::TColumnVector
* _pVec
) const;
300 // checks if the type is supported in the destination database
301 sal_Bool
supportsType(sal_Int32 _nDataType
,sal_Int32
& _rNewDataType
);
303 void impl_loadSourceData();
306 // used for copy tables or queries
309 const OUString
& _rDefaultName
,
310 sal_Int16 _nOperation
,
311 const ICopyTableSourceObject
& _rSourceObject
,
312 const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>& _xSourceConnection
,
313 const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>& _xConnection
,
314 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& _rxContext
,
315 const ::com::sun::star::uno::Reference
< ::com::sun::star::task::XInteractionHandler
>& _xInteractionHandler
318 // used for importing rtf/html sources
321 const OUString
& _rDefaultName
,
322 sal_Int16 _nOperation
,
323 const ODatabaseExport::TColumns
& _rDestColumns
,
324 const ODatabaseExport::TColumnVector
& _rSourceColVec
,
325 const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>& _xConnection
,
326 const ::com::sun::star::uno::Reference
< ::com::sun::star::util::XNumberFormatter
>& _xFormatter
,
327 TypeSelectionPageFactory _pTypeSelectionPageFactory
,
328 SvStream
& _rTypeSelectionPageArg
,
329 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& _rxContext
332 virtual ~OCopyTableWizard();
334 virtual long DeactivatePage();
335 OKButton
& GetOKButton() { return m_pbFinish
; }
336 Wizard_Button_Style
GetPressedButton() const { return m_ePressed
; }
337 void EnableButton(Wizard_Button_Style eStyle
,sal_Bool bEnable
);
338 void AddWizardPage(OWizardPage
* pPage
); // delete page from OCopyTableWizard
339 void RemoveWizardPage(OWizardPage
* pPage
); // Page goes again to user
340 void CheckButtons(); // checks which button can be disabled, enabled
342 // returns a vector where the position of a column and if the column is in the selection
343 // when not the value is COLUMN_POSITION_NOT_FOUND == (sal_uInt32)-1
344 ODatabaseExport::TPositions
GetColumnPositions() const { return m_vColumnPos
; }
345 ::std::vector
<sal_Int32
> GetColumnTypes() const { return m_vColumnTypes
; }
346 sal_Bool
UseHeaderLine() const { return m_bUseHeaderLine
; }
347 void setUseHeaderLine(sal_Bool _bUseHeaderLine
) { m_bUseHeaderLine
= _bUseHeaderLine
; }
349 void insertColumn(sal_Int32 _nPos
,OFieldDescription
* _pField
);
351 /** replaces a field description with another one. The name must not be known so far.
353 The pos inside the vector, 0 based.
357 The name of column to be replaced.
359 void replaceColumn(sal_Int32 _nPos
,OFieldDescription
* _pField
,const OUString
& _sOldName
);
361 /** returns whether a primary key should be created in the target database
363 sal_Bool
shouldCreatePrimaryKey() const;
364 void setCreatePrimaryKey( bool _bDoCreate
, const OUString
& _rSuggestedName
);
366 static bool supportsPrimaryKey( const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>& _rxConnection
);
367 bool supportsPrimaryKey() const { return supportsPrimaryKey( m_xDestConnection
); }
369 static bool supportsViews( const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>& _rxConnection
);
370 bool supportsViews() const { return supportsViews( m_xDestConnection
); }
372 /** returns the name of the primary key
374 The name of the primary key.
376 OUString
getPrimaryKeyName() const { return m_aKeyName
; }
378 TOTypeInfoSP
getTypeInfo(sal_Int32 _nPos
) const { return m_aTypeInfoIndex
[_nPos
]->second
; }
379 const OTypeInfoMap
* getTypeInfo() const { return &m_aTypeInfo
; }
381 TOTypeInfoSP
getDestTypeInfo(sal_Int32 _nPos
) const { return m_aDestTypeInfoIndex
[_nPos
]->second
; }
382 const OTypeInfoMap
* getDestTypeInfo() const { return &m_aDestTypeInfo
; }
384 ::com::sun::star::lang::Locale
GetLocale() const { return m_aLocale
; }
385 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XNumberFormatter
> GetFormatter() const { return m_xFormatter
; }
386 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
> GetComponentContext() const { return m_xContext
; }
388 const ODatabaseExport::TColumns
* getSourceColumns() const{ return &m_vSourceColumns
; }
389 const ODatabaseExport::TColumnVector
* getSrcVector() const { return &m_vSourceVec
; }
390 ODatabaseExport::TColumns
* getDestColumns() { return &m_vDestColumns
; }
391 const ODatabaseExport::TColumnVector
* getDestVector() const { return &m_aDestVec
; }
392 OUString
getName() const { return m_sName
; }
394 /** clears the dest vectors
396 void clearDestColumns();
398 ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
> createTable();
399 ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
> createView() const;
400 sal_Int32
getMaxColumnNameLength() const;
402 void setOperation( const sal_Int16 _nOperation
);
403 sal_Int16
getOperation() const;
405 OUString
convertColumnName( const TColumnFindFunctor
& _rCmpFunctor
,
406 const OUString
& _sColumnName
,
407 const OUString
& _sExtraChars
,
408 sal_Int32 _nMaxNameLen
);
409 TOTypeInfoSP
convertType(const TOTypeInfoSP
&_pType
,sal_Bool
& _bNotConvert
);
411 OUString
createUniqueName(const OUString
& _sName
);
413 // displays a error message that a column type is not supported
414 void showColumnTypeNotSupported(const OUString
& _rColumnName
);
416 void removeColumnNameFromNameMap(const OUString
& _sName
);
417 void showError(const OUString
& _sErrorMesage
);
418 void showError(const ::com::sun::star::uno::Any
& _aError
);
422 #endif // DBAUI_WIZ_COPYTABLEDIALOG_HXX
424 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */