Bump version to 5.0-14
[LibreOffice.git] / dbaccess / source / ui / inc / WCopyTable.hxx
blob6ce0038320e8cf7ba6a34b3fc7ac03f7076b27c3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 INCLUDED_DBACCESS_SOURCE_UI_INC_WCOPYTABLE_HXX
21 #define INCLUDED_DBACCESS_SOURCE_UI_INC_WCOPYTABLE_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>
41 #include <functional>
42 #include <map>
43 #include <algorithm>
45 namespace dbaui
48 typedef ::std::unary_function< OUString,bool> TColumnFindFunctorType;
49 class TColumnFindFunctor : public TColumnFindFunctorType
51 public:
52 virtual bool operator()(const OUString& _sColumnName) const = 0;
54 protected:
55 ~TColumnFindFunctor() {}
58 class TExportColumnFindFunctor : public TColumnFindFunctor
60 ODatabaseExport::TColumns* m_pColumns;
61 public:
62 TExportColumnFindFunctor(ODatabaseExport::TColumns* _pColumns)
64 m_pColumns = _pColumns;
67 virtual ~TExportColumnFindFunctor() {}
69 bool operator()(const OUString& _sColumnName) const SAL_OVERRIDE
71 return m_pColumns->find(_sColumnName) != m_pColumns->end();
75 class TMultiListBoxEntryFindFunctor : public TColumnFindFunctor
77 ::comphelper::UStringMixEqual m_aCase;
78 ::std::vector< OUString>* m_pVector;
79 public:
80 TMultiListBoxEntryFindFunctor(::std::vector< OUString>* _pVector,
81 const ::comphelper::UStringMixEqual& _aCase)
82 :m_aCase(_aCase)
83 ,m_pVector(_pVector)
87 virtual ~TMultiListBoxEntryFindFunctor() {}
89 bool operator()(const OUString& _sColumnName) const SAL_OVERRIDE
91 return ::std::any_of(m_pVector->begin(),m_pVector->end(),
92 ::std::bind2nd(m_aCase, _sColumnName));
96 // ICopyTableSourceObject
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
108 public:
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
130 * \return
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 // ObjectCopySource
146 class ObjectCopySource : public ICopyTableSourceObject
148 private:
149 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection > m_xConnection;
150 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDatabaseMetaData > m_xMetaData;
151 ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > m_xObject;
152 ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySetInfo > m_xObjectPSI;
153 ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess > m_xObjectColumns;
155 public:
156 ObjectCopySource(
157 const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _rxConnection,
158 const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxObject
161 // ICopyTableSourceObject overridables
162 virtual OUString getQualifiedObjectName() const SAL_OVERRIDE;
163 virtual bool isView() const SAL_OVERRIDE;
164 virtual void copyUISettingsTo( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxObject ) const SAL_OVERRIDE;
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 SAL_OVERRIDE;
166 virtual ::com::sun::star::uno::Sequence< OUString >
167 getColumnNames() const SAL_OVERRIDE;
168 virtual ::com::sun::star::uno::Sequence< OUString >
169 getPrimaryKeyColumnNames() const SAL_OVERRIDE;
170 virtual OFieldDescription* createFieldDescription( const OUString& _rColumnName ) const SAL_OVERRIDE;
171 virtual OUString getSelectStatement() const SAL_OVERRIDE;
172 virtual ::utl::SharedUNOComponent< ::com::sun::star::sdbc::XPreparedStatement >
173 getPreparedSelectStatement() const SAL_OVERRIDE;
176 // NamedTableCopySource
177 class NamedTableCopySource : public ICopyTableSourceObject
179 private:
180 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection > m_xConnection;
181 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDatabaseMetaData > m_xMetaData;
182 OUString m_sTableName;
183 OUString m_sTableCatalog;
184 OUString m_sTableSchema;
185 OUString m_sTableBareName;
186 ::std::vector< OFieldDescription > m_aColumnInfo;
187 ::utl::SharedUNOComponent< ::com::sun::star::sdbc::XPreparedStatement > m_xStatement;
189 public:
190 NamedTableCopySource(
191 const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _rxConnection,
192 const OUString& _rTableName
195 // ICopyTableSourceObject overridables
196 virtual OUString getQualifiedObjectName() const SAL_OVERRIDE;
197 virtual bool isView() const SAL_OVERRIDE;
198 virtual void copyUISettingsTo( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _rxObject ) const SAL_OVERRIDE;
199 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 SAL_OVERRIDE;
200 virtual ::com::sun::star::uno::Sequence< OUString >
201 getColumnNames() const SAL_OVERRIDE;
202 virtual ::com::sun::star::uno::Sequence< OUString >
203 getPrimaryKeyColumnNames() const SAL_OVERRIDE;
204 virtual OFieldDescription* createFieldDescription( const OUString& _rColumnName ) const SAL_OVERRIDE;
205 virtual OUString getSelectStatement() const SAL_OVERRIDE;
206 virtual ::utl::SharedUNOComponent< ::com::sun::star::sdbc::XPreparedStatement >
207 getPreparedSelectStatement() const SAL_OVERRIDE;
209 private:
210 void impl_ensureColumnInfo_throw();
211 ::utl::SharedUNOComponent< ::com::sun::star::sdbc::XPreparedStatement >
212 impl_ensureStatement_throw();
215 // Wizard Dialog
216 class OCopyTableWizard : public WizardDialog
218 friend class OWizColumnSelect;
219 friend class OWizTypeSelect;
220 friend class OWizTypeSelectControl;
221 friend class OCopyTable;
222 friend class OWizNameMatching;
224 public:
225 typedef std::map<OUString, OUString, ::comphelper::UStringMixLess> TNameMapping;
227 enum Wizard_Button_Style
229 WIZARD_NEXT,
230 WIZARD_PREV,
231 WIZARD_FINISH,
233 WIZARD_NONE
236 private:
237 ODatabaseExport::TColumns m_vDestColumns; // contains the columns
238 ODatabaseExport::TColumnVector m_aDestVec; // the order to insert the columns
239 ODatabaseExport::TColumns m_vSourceColumns;
240 ODatabaseExport::TColumnVector m_vSourceVec;
242 VclPtr<HelpButton> m_pbHelp;
243 VclPtr<CancelButton> m_pbCancel;
244 VclPtr<PushButton> m_pbPrev;
245 VclPtr<PushButton> m_pbNext;
246 VclPtr<PushButton> m_pbFinish;
248 OTypeInfoMap m_aTypeInfo;
249 ::std::vector<OTypeInfoMap::iterator> m_aTypeInfoIndex;
250 OTypeInfoMap m_aDestTypeInfo;
251 ::std::vector<OTypeInfoMap::iterator> m_aDestTypeInfoIndex;
252 TNameMapping m_mNameMapping;
254 ODatabaseExport::TPositions m_vColumnPos;
255 ::std::vector<sal_Int32> m_vColumnTypes;
257 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection > m_xDestConnection;
259 const ICopyTableSourceObject& m_rSourceObject;
261 ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter > m_xFormatter;
262 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext> m_xContext;
263 ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler> m_xInteractionHandler;
265 OUString m_sTypeNames; // these type names are the ones out of the resource file
266 sal_uInt32 m_nPageCount;
267 bool m_bDeleteSourceColumns;
268 bool m_bInterConnectionCopy; // are we copying between different connections?
270 ::com::sun::star::lang::Locale m_aLocale;
271 OUString m_sName; // for a table the name is composed
272 OUString m_sSourceName;
273 OUString m_aKeyName;
274 TOTypeInfoSP m_pTypeInfo; // default type
275 bool m_bAddPKFirstTime;
276 sal_Int16 m_nOperation;
277 Wizard_Button_Style m_ePressed;
278 bool m_bCreatePrimaryKeyColumn;
279 bool m_bUseHeaderLine;
281 private:
282 DECL_LINK( ImplPrevHdl , void* );
283 DECL_LINK( ImplNextHdl , void* );
284 DECL_LINK( ImplOKHdl , void* );
285 DECL_LINK( ImplActivateHdl, void* );
286 bool CheckColumns(sal_Int32& _rnBreakPos);
287 void loadData( const ICopyTableSourceObject& _rSourceObject,
288 ODatabaseExport::TColumns& _rColumns,
289 ODatabaseExport::TColumnVector& _rColVector );
290 void construct();
291 // need for table creation
292 static void appendColumns( ::com::sun::star::uno::Reference< ::com::sun::star::sdbcx::XColumnsSupplier>& _rxColSup, const ODatabaseExport::TColumnVector* _pVec, bool _bKeyColumns = false );
293 static void appendKey(::com::sun::star::uno::Reference< ::com::sun::star::sdbcx::XKeysSupplier>& _rxSup,const ODatabaseExport::TColumnVector* _pVec);
294 // checks if the type is supported in the destination database
295 bool supportsType(sal_Int32 _nDataType,sal_Int32& _rNewDataType);
297 void impl_loadSourceData();
299 public:
300 // used for copy tables or queries
301 OCopyTableWizard(
302 vcl::Window * pParent,
303 const OUString& _rDefaultName,
304 sal_Int16 _nOperation,
305 const ICopyTableSourceObject& _rSourceObject,
306 const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xSourceConnection,
307 const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection,
308 const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& _rxContext,
309 const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler>& _xInteractionHandler
312 // used for importing rtf/html sources
313 OCopyTableWizard(
314 vcl::Window* pParent,
315 const OUString& _rDefaultName,
316 sal_Int16 _nOperation,
317 const ODatabaseExport::TColumns& _rDestColumns,
318 const ODatabaseExport::TColumnVector& _rSourceColVec,
319 const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _xConnection,
320 const ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter >& _xFormatter,
321 TypeSelectionPageFactory _pTypeSelectionPageFactory,
322 SvStream& _rTypeSelectionPageArg,
323 const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& _rxContext
326 virtual ~OCopyTableWizard();
327 virtual void dispose() SAL_OVERRIDE;
329 virtual bool DeactivatePage() SAL_OVERRIDE;
330 OKButton& GetOKButton() { return static_cast<OKButton&>(*m_pbFinish); }
331 Wizard_Button_Style GetPressedButton() const { return m_ePressed; }
332 void EnableButton(Wizard_Button_Style eStyle, bool bEnable);
333 void AddWizardPage(OWizardPage* pPage); // delete page from OCopyTableWizard
334 void RemoveWizardPage(OWizardPage* pPage); // Page goes again to user
335 void CheckButtons(); // checks which button can be disabled, enabled
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
339 ODatabaseExport::TPositions GetColumnPositions() const { return m_vColumnPos; }
340 ::std::vector<sal_Int32> GetColumnTypes() const { return m_vColumnTypes; }
341 bool UseHeaderLine() const { return m_bUseHeaderLine; }
342 void setUseHeaderLine(bool _bUseHeaderLine) { m_bUseHeaderLine = _bUseHeaderLine; }
344 void insertColumn(sal_Int32 _nPos,OFieldDescription* _pField);
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.
354 void replaceColumn(sal_Int32 _nPos,OFieldDescription* _pField,const OUString& _sOldName);
356 /** returns whether a primary key should be created in the target database
358 bool shouldCreatePrimaryKey() const { return m_bCreatePrimaryKeyColumn;}
359 void setCreatePrimaryKey( bool _bDoCreate, const OUString& _rSuggestedName );
361 static bool supportsPrimaryKey( const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _rxConnection );
362 bool supportsPrimaryKey() const { return supportsPrimaryKey( m_xDestConnection ); }
364 static bool supportsViews( const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _rxConnection );
365 bool supportsViews() const { return supportsViews( m_xDestConnection ); }
367 /** returns the name of the primary key
368 @return
369 The name of the primary key.
371 OUString getPrimaryKeyName() const { return m_aKeyName; }
373 TOTypeInfoSP getTypeInfo(sal_Int32 _nPos) const { return m_aTypeInfoIndex[_nPos]->second; }
374 const OTypeInfoMap& getTypeInfo() const { return m_aTypeInfo; }
376 TOTypeInfoSP getDestTypeInfo(sal_Int32 _nPos) const { return m_aDestTypeInfoIndex[_nPos]->second; }
377 const OTypeInfoMap& getDestTypeInfo() const { return m_aDestTypeInfo; }
379 ::com::sun::star::lang::Locale GetLocale() const { return m_aLocale; }
380 ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter > GetFormatter() const { return m_xFormatter; }
381 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext> GetComponentContext() const { return m_xContext; }
383 const ODatabaseExport::TColumns& getSourceColumns() const{ return m_vSourceColumns; }
384 const ODatabaseExport::TColumnVector& getSrcVector() const { return m_vSourceVec; }
385 ODatabaseExport::TColumns& getDestColumns() { return m_vDestColumns; }
386 const ODatabaseExport::TColumnVector& getDestVector() const { return m_aDestVec; }
387 OUString getName() const { return m_sName; }
389 /** clears the dest vectors
391 void clearDestColumns();
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;
397 void setOperation( const sal_Int16 _nOperation );
398 sal_Int16 getOperation() const { return m_nOperation;}
400 OUString convertColumnName( const TColumnFindFunctor& _rCmpFunctor,
401 const OUString& _sColumnName,
402 const OUString& _sExtraChars,
403 sal_Int32 _nMaxNameLen);
404 TOTypeInfoSP convertType(const TOTypeInfoSP&_pType, bool& _bNotConvert);
406 OUString createUniqueName(const OUString& _sName);
408 // displays a error message that a column type is not supported
409 void showColumnTypeNotSupported(const OUString& _rColumnName);
411 void removeColumnNameFromNameMap(const OUString& _sName);
412 void showError(const OUString& _sErrorMessage);
413 void showError(const ::com::sun::star::uno::Any& _aError);
417 #endif // INCLUDED_DBACCESS_SOURCE_UI_INC_WCOPYTABLE_HXX
419 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */