xref: /trunk/main/dbaccess/source/ui/inc/WCopyTable.hxx (revision 3ce6c2dd3860afd353ceca3849779821e9b9f6f5)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef DBAUI_WIZ_COPYTABLEDIALOG_HXX
25 #define DBAUI_WIZ_COPYTABLEDIALOG_HXX
26 
27 #include <com/sun/star/container/XNameAccess.hpp>
28 #include <com/sun/star/sdbc/XConnection.hpp>
29 #include <com/sun/star/sdbc/XResultSet.hpp>
30 #include <com/sun/star/sdbc/XResultSetMetaData.hpp>
31 #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
32 #include <com/sun/star/beans/XPropertySet.hpp>
33 #include <comphelper/stl_types.hxx>
34 #include "TypeInfo.hxx"
35 #include <vcl/button.hxx>
36 #include <svtools/wizdlg.hxx>
37 #include "DExport.hxx"
38 #include "WTabPage.hxx"
39 #include "FieldDescriptions.hxx"
40 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
41 #include <com/sun/star/sdbcx/XKeysSupplier.hpp>
42 #include <com/sun/star/task/XInteractionHandler.hpp>
43 #include <vcl/lstbox.hxx>
44 #include <functional>
45 
46 namespace dbaui
47 {
48 
49     class TColumnFindFunctor
50     {
51     public:
52         typedef ::rtl::OUString argument_type;
53         typedef bool result_type;
54         virtual bool operator()(const ::rtl::OUString& _sColumnName) const = 0;
55     };
56 
57     class TExportColumnFindFunctor : public TColumnFindFunctor
58     {
59         ODatabaseExport::TColumns* m_pColumns;
60     public:
TExportColumnFindFunctor(ODatabaseExport::TColumns * _pColumns)61         TExportColumnFindFunctor(ODatabaseExport::TColumns* _pColumns)
62         {
63             m_pColumns = _pColumns;
64         }
operator ()(const::rtl::OUString & _sColumnName) const65         inline bool operator()(const ::rtl::OUString& _sColumnName) const
66         {
67             return m_pColumns->find(_sColumnName) != m_pColumns->end();
68         }
69     };
70 
71     class TMultiListBoxEntryFindFunctor : public TColumnFindFunctor
72     {
73         ::comphelper::TStringMixEqualFunctor m_aCase;
74         ::std::vector< ::rtl::OUString>* m_pVector;
75     public:
TMultiListBoxEntryFindFunctor(::std::vector<::rtl::OUString> * _pVector,const::comphelper::TStringMixEqualFunctor & _aCase)76         TMultiListBoxEntryFindFunctor(::std::vector< ::rtl::OUString>* _pVector,
77                                     const ::comphelper::TStringMixEqualFunctor& _aCase)
78             :m_aCase(_aCase)
79             ,m_pVector(_pVector)
80         {
81         }
operator ()(const::rtl::OUString & _sColumnName) const82         inline bool operator()(const ::rtl::OUString& _sColumnName) const
83         {
84             return ::std::find_if(m_pVector->begin(),m_pVector->end(),
85                 ::std::bind(m_aCase, ::std::placeholders::_1, _sColumnName)) != m_pVector->end();
86         }
87     };
88 
89     // ========================================================
90     // ICopyTableSourceObject
91     // ========================================================
92     /** interface to an object to copy to another DB, using the OCopyTableWizard
93 
94         when the wizard is used to copy an object to another DB, it usually requires
95         a sdbcx-level or sdb-level object (a css.sdbcx.Table or css.sdb.Query, that is).
96 
97         However, to also support copying tables from sdbc-level connections, we allow to
98         work with the object name only. This implies some less features (like copying the
99         UI settings of a table is not done), but still allows to copy definition and data.
100     */
101     class ICopyTableSourceObject
102     {
103     public:
104         /// retrieves the fully qualified name of the object to copy
105         virtual ::rtl::OUString     getQualifiedObjectName() const = 0;
106         /// determines whether the object is a view
107         virtual bool                isView() const = 0;
108         /** copies the UI settings of the object to the given target object. Might be
109             ignored by implementations which do not have Ui settings.
110         */
111         virtual void                copyUISettingsTo( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxObject ) const = 0;
112         /// retrieves the column names of the to-be-copied object
113         virtual ::com::sun::star::uno::Sequence< ::rtl::OUString >
114                                     getColumnNames() const = 0;
115         /// retrieves the names of the primary keys of the to-be-copied object
116         virtual ::com::sun::star::uno::Sequence< ::rtl::OUString >
117                                     getPrimaryKeyColumnNames() const = 0;
118         /// creates a OFieldDescription for the given column of the to-be-copied object
119         virtual OFieldDescription*  createFieldDescription( const ::rtl::OUString& _rColumnName ) const = 0;
120         /// returns the SELECT statement which can be used to retrieve the data of the to-be-copied object
121         virtual ::rtl::OUString     getSelectStatement() const = 0;
122 
123         /** copies the filter and sorting
124         *
125         * \return
126         */
127         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;
128 
129         /** returns the prepared statement which can be used to retrieve the data of the to-be-copied object
130 
131             The default implementation of this method will simply prepare a statement with the return value
132             of ->getSelectStatement.
133         */
134         virtual ::utl::SharedUNOComponent< ::com::sun::star::sdbc::XPreparedStatement >
135                                     getPreparedSelectStatement() const = 0;
136 
137         virtual ~ICopyTableSourceObject();
138     };
139 
140     // ========================================================
141     // ObjectCopySource
142     // ========================================================
143     class ObjectCopySource : public ICopyTableSourceObject
144     {
145     private:
146         ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >         m_xConnection;
147         ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDatabaseMetaData >   m_xMetaData;
148         ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >       m_xObject;
149         ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySetInfo >   m_xObjectPSI;
150         ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess >    m_xObjectColumns;
151 
152     public:
153         ObjectCopySource(
154             const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _rxConnection,
155             const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxObject
156         );
157 
158         // ICopyTableSourceObject overridables
159         virtual ::rtl::OUString     getQualifiedObjectName() const;
160         virtual bool                isView() const;
161         virtual void                copyUISettingsTo( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxObject ) const;
162         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;
163         virtual ::com::sun::star::uno::Sequence< ::rtl::OUString >
164                                     getColumnNames() const;
165         virtual ::com::sun::star::uno::Sequence< ::rtl::OUString >
166                                     getPrimaryKeyColumnNames() const;
167         virtual OFieldDescription*  createFieldDescription( const ::rtl::OUString& _rColumnName ) const;
168         virtual ::rtl::OUString     getSelectStatement() const;
169         virtual ::utl::SharedUNOComponent< ::com::sun::star::sdbc::XPreparedStatement >
170                                     getPreparedSelectStatement() const;
171     };
172 
173     // ========================================================
174     // NamedTableCopySource
175     // ========================================================
176     class NamedTableCopySource : public ICopyTableSourceObject
177     {
178     private:
179         ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >         m_xConnection;
180         ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDatabaseMetaData >   m_xMetaData;
181         ::rtl::OUString                                                                 m_sTableName;
182         ::rtl::OUString                                                                 m_sTableCatalog;
183         ::rtl::OUString                                                                 m_sTableSchema;
184         ::rtl::OUString                                                                 m_sTableBareName;
185         ::std::vector< OFieldDescription >                                              m_aColumnInfo;
186         ::utl::SharedUNOComponent< ::com::sun::star::sdbc::XPreparedStatement >         m_xStatement;
187 
188     public:
189         NamedTableCopySource(
190             const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _rxConnection,
191             const ::rtl::OUString& _rTableName
192         );
193 
194         // ICopyTableSourceObject overridables
195         virtual ::rtl::OUString     getQualifiedObjectName() const;
196         virtual bool                isView() const;
197         virtual void                copyUISettingsTo( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxObject ) const;
198         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;
199         virtual ::com::sun::star::uno::Sequence< ::rtl::OUString >
200                                     getColumnNames() const;
201         virtual ::com::sun::star::uno::Sequence< ::rtl::OUString >
202                                     getPrimaryKeyColumnNames() const;
203         virtual OFieldDescription*  createFieldDescription( const ::rtl::OUString& _rColumnName ) const;
204         virtual ::rtl::OUString     getSelectStatement() const;
205         virtual ::utl::SharedUNOComponent< ::com::sun::star::sdbc::XPreparedStatement >
206                                     getPreparedSelectStatement() const;
207 
208     private:
209         void    impl_ensureColumnInfo_throw();
210         ::utl::SharedUNOComponent< ::com::sun::star::sdbc::XPreparedStatement >
211                 impl_ensureStatement_throw();
212     };
213 
214     // ========================================================
215     // Wizard Dialog
216     // ========================================================
217     class OCopyTableWizard : public WizardDialog
218     {
219         friend class        OWizColumnSelect;
220         friend class        OWizTypeSelect;
221         friend class        OWizTypeSelectControl;
222         friend class        OCopyTable;
223         friend class        OWizNameMatching;
224 
225     public:
226         DECLARE_STL_MAP(::rtl::OUString,::rtl::OUString,::comphelper::UStringMixLess,TNameMapping);
227 
228         enum Wizard_Button_Style
229         {
230             WIZARD_NEXT,
231             WIZARD_PREV,
232             WIZARD_FINISH,
233 
234             WIZARD_NONE
235         };
236 
237     private:
238         ODatabaseExport::TColumns       m_vDestColumns; // contains the columns
239         ODatabaseExport::TColumnVector  m_aDestVec;     // the order to insert the columns
240         ODatabaseExport::TColumns       m_vSourceColumns;
241         ODatabaseExport::TColumnVector  m_vSourceVec;
242 
243         HelpButton              m_pbHelp;
244         CancelButton            m_pbCancel;
245         PushButton              m_pbPrev;
246         PushButton              m_pbNext;
247         OKButton                m_pbFinish;
248 
249         OTypeInfoMap                            m_aTypeInfo;
250         ::std::vector<OTypeInfoMap::iterator>   m_aTypeInfoIndex;
251         OTypeInfoMap                            m_aDestTypeInfo;
252         ::std::vector<OTypeInfoMap::iterator>   m_aDestTypeInfoIndex;
253         TNameMapping                            m_mNameMapping;
254 
255         ODatabaseExport::TPositions             m_vColumnPos;
256         ::std::vector<sal_Int32>                m_vColumnTypes;
257 
258         ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >         m_xDestConnection;
259 
260         const ICopyTableSourceObject&                                                   m_rSourceObject;
261 
262         ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter >    m_xFormatter;
263         ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory> m_xFactory;
264         ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler>  m_xInteractionHandler;
265 
266         String                  m_sTypeNames;       // these type names are the ones out of the resource file
267         sal_uInt32              m_nPageCount;
268         sal_Bool                m_bDeleteSourceColumns;
269         bool                    m_bInterConnectionCopy;    // are we copying between different connections?
270 
271         ::com::sun::star::lang::Locale  m_aLocale;
272         ::rtl::OUString                 m_sName;    // for a table the name is composed
273         ::rtl::OUString                 m_sSourceName;
274         ::rtl::OUString                 m_aKeyName;
275         TOTypeInfoSP                    m_pTypeInfo; // default type
276         sal_Bool                        m_bAddPKFirstTime;
277         sal_Int16                       m_nOperation;
278         Wizard_Button_Style             m_ePressed;
279         sal_Bool                        m_bCreatePrimaryKeyColumn;
280         sal_Bool                        m_bUseHeaderLine;
281 
282     private:
283         DECL_LINK( ImplPrevHdl  , PushButton* );
284         DECL_LINK( ImplNextHdl  , PushButton* );
285         DECL_LINK( ImplOKHdl    , OKButton* );
286         DECL_LINK( ImplActivateHdl, WizardDialog* );
287         sal_Bool CheckColumns(sal_Int32& _rnBreakPos);
288         void loadData( const ICopyTableSourceObject& _rSourceObject,
289                        ODatabaseExport::TColumns& _rColumns,
290                        ODatabaseExport::TColumnVector& _rColVector );
291         void construct();
292         // need for table creation
293         void appendColumns( ::com::sun::star::uno::Reference< ::com::sun::star::sdbcx::XColumnsSupplier>& _rxColSup, const ODatabaseExport::TColumnVector* _pVec, sal_Bool _bKeyColumns = sal_False ) const;
294         void appendKey(::com::sun::star::uno::Reference< ::com::sun::star::sdbcx::XKeysSupplier>& _rxSup,const ODatabaseExport::TColumnVector* _pVec) const;
295         // checks if the type is supported in the destination database
296         sal_Bool supportsType(sal_Int32 _nDataType,sal_Int32& _rNewDataType);
297 
298         void    impl_loadSourceData();
299 
300     public:
301         // used for copy tables or queries
302         OCopyTableWizard(
303             Window * pParent,
304             const ::rtl::OUString& _rDefaultName,
305             sal_Int16 _nOperation,
306             const ICopyTableSourceObject&                                                           _rSourceObject,
307             const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >&          _xSourceConnection,
308             const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >&          _xConnection,
309             const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxORB,
310             const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler>&   _xInteractionHandler
311         );
312 
313         // used for importing rtf/html sources
314         OCopyTableWizard(
315             Window* pParent,
316             const ::rtl::OUString& _rDefaultName,
317             sal_Int16 _nOperation,
318             const ODatabaseExport::TColumns& _rDestColumns,
319             const ODatabaseExport::TColumnVector& _rSourceColVec,
320             const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection,
321             const ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter >& _xFormatter,
322             TypeSelectionPageFactory _pTypeSelectionPageFactory,
323             SvStream& _rTypeSelectionPageArg,
324             const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rM
325         );
326 
327         virtual ~OCopyTableWizard();
328 
329         virtual long        DeactivatePage();
GetOKButton()330         OKButton&           GetOKButton() { return m_pbFinish; }
GetPressedButton() const331         Wizard_Button_Style GetPressedButton() const { return m_ePressed; }
332         void                EnableButton(Wizard_Button_Style eStyle,sal_Bool bEnable);
333         void                AddWizardPage(OWizardPage* pPage); // Page wird von OCopyTableWizard gel�scht
334         void                RemoveWizardPage(OWizardPage* pPage); // Page goes again to user
335         void                CheckButtons(); // checks which button can be disabled, enabled
336 
337         // returns a vector where the position of a column and if the column is in the selection
338         // when not the value is COLUMN_POSITION_NOT_FOUND == (sal_uInt32)-1
GetColumnPositions() const339         ODatabaseExport::TPositions GetColumnPositions()    const { return m_vColumnPos; }
GetColumnTypes() const340         ::std::vector<sal_Int32>    GetColumnTypes()        const { return m_vColumnTypes; }
UseHeaderLine() const341         sal_Bool                    UseHeaderLine()         const { return m_bUseHeaderLine; }
setUseHeaderLine(sal_Bool _bUseHeaderLine)342         void                        setUseHeaderLine(sal_Bool _bUseHeaderLine) { m_bUseHeaderLine = _bUseHeaderLine; }
343 
344         void insertColumn(sal_Int32 _nPos,OFieldDescription* _pField);
345 
346         /** replaces a field description with another one. The name must not be known so far.
347             @param  _nPos
348                 The pos inside the vector, 0 based.
349             @param  _pField
350                 The field to set.
351             @param  _sOldName
352                 The name of column to be replaced.
353         */
354         void replaceColumn(sal_Int32 _nPos,OFieldDescription* _pField,const ::rtl::OUString& _sOldName);
355 
356         /** returns whether a primary key should be created in the target database
357         */
358         sal_Bool        shouldCreatePrimaryKey() const;
359         void            setCreatePrimaryKey( bool _bDoCreate, const ::rtl::OUString& _rSuggestedName );
360 
361         static bool     supportsPrimaryKey( const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _rxConnection );
supportsPrimaryKey() const362         bool            supportsPrimaryKey() const { return supportsPrimaryKey( m_xDestConnection ); }
363 
364         static bool     supportsViews( const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _rxConnection );
supportsViews() const365         bool            supportsViews() const { return supportsViews( m_xDestConnection ); }
366 
367         /** returns the name of the primary key
368             @return
369                 The name of the primary key.
370         */
getPrimaryKeyName() const371         ::rtl::OUString getPrimaryKeyName() const { return m_aKeyName; }
372 
getTypeInfo(sal_Int32 _nPos) const373         TOTypeInfoSP        getTypeInfo(sal_Int32 _nPos)        const { return m_aTypeInfoIndex[_nPos]->second; }
getTypeInfo() const374         const OTypeInfoMap* getTypeInfo()                       const { return &m_aTypeInfo; }
375 
getDestTypeInfo(sal_Int32 _nPos) const376         TOTypeInfoSP        getDestTypeInfo(sal_Int32 _nPos)    const { return m_aDestTypeInfoIndex[_nPos]->second; }
getDestTypeInfo() const377         const OTypeInfoMap* getDestTypeInfo()                   const { return &m_aDestTypeInfo; }
378 
GetLocale() const379         ::com::sun::star::lang::Locale  GetLocale() const { return m_aLocale; }
GetFormatter() const380         ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter > GetFormatter() const { return m_xFormatter; }
GetFactory() const381         ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory> GetFactory() const { return m_xFactory; }
382 
getSourceColumns() const383         const ODatabaseExport::TColumns*        getSourceColumns() const{ return &m_vSourceColumns; }
getSrcVector() const384         const ODatabaseExport::TColumnVector*   getSrcVector() const    { return &m_vSourceVec; }
getDestColumns()385         ODatabaseExport::TColumns*              getDestColumns()        { return &m_vDestColumns; }
getDestVector() const386         const ODatabaseExport::TColumnVector*   getDestVector() const   { return &m_aDestVec; }
getName() const387         ::rtl::OUString getName() const { return m_sName; }
388 
389         /** clears the dest vectors
390         */
391         void clearDestColumns();
392 
393         ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > createTable();
394         ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > createView() const;
395         sal_Int32 getMaxColumnNameLength() const;
396 
397         void setOperation( const sal_Int16 _nOperation );
398         sal_Int16 getOperation() const;
399 
400         ::rtl::OUString convertColumnName(  const TColumnFindFunctor&   _rCmpFunctor,
401                                             const ::rtl::OUString&  _sColumnName,
402                                             const ::rtl::OUString&  _sExtraChars,
403                                             sal_Int32               _nMaxNameLen);
404         TOTypeInfoSP convertType(const TOTypeInfoSP&_pType,sal_Bool& _bNotConvert);
405 
406         ::rtl::OUString createUniqueName(const ::rtl::OUString& _sName);
407 
408         // displays a error message that a column type is not supported
409         void showColumnTypeNotSupported(const ::rtl::OUString& _rColumnName);
410 
411         void removeColumnNameFromNameMap(const ::rtl::OUString& _sName);
412         void showError(const ::rtl::OUString& _sErrorMesage);
413         void showError(const ::com::sun::star::uno::Any& _aError);
414     };
415 }
416 
417 #endif // DBAUI_WIZ_COPYTABLEDIALOG_HXX
418