Gtk-WARNING gtktreestore.c:1047: Invalid column number 1 added to iter
[LibreOffice.git] / dbaccess / source / ui / inc / WCopyTable.hxx
blobd3c0672f8e0bea26ccf8a8b5e6aed008f838c264
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 #pragma once
22 #include <com/sun/star/container/XNameAccess.hpp>
23 #include <com/sun/star/sdbc/XConnection.hpp>
24 #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
25 #include <com/sun/star/uno/XComponentContext.hpp>
26 #include <com/sun/star/beans/XPropertySet.hpp>
27 #include <comphelper/stl_types.hxx>
28 #include "TypeInfo.hxx"
29 #include <vcl/roadmapwizard.hxx>
30 #include "DExport.hxx"
31 #include "WTabPage.hxx"
32 #include "FieldDescriptions.hxx"
33 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
34 #include <com/sun/star/sdbcx/XKeysSupplier.hpp>
35 #include <com/sun/star/task/XInteractionHandler.hpp>
36 #include <map>
37 #include <algorithm>
39 namespace dbaui
42 class TColumnFindFunctor
44 public:
45 virtual bool operator()(const OUString& _sColumnName) const = 0;
47 protected:
48 ~TColumnFindFunctor() {}
51 class TExportColumnFindFunctor : public TColumnFindFunctor
53 ODatabaseExport::TColumns* m_pColumns;
54 public:
55 TExportColumnFindFunctor(ODatabaseExport::TColumns* _pColumns)
57 m_pColumns = _pColumns;
60 virtual ~TExportColumnFindFunctor() {}
62 bool operator()(const OUString& _sColumnName) const override
64 return m_pColumns->find(_sColumnName) != m_pColumns->end();
68 class TMultiListBoxEntryFindFunctor : public TColumnFindFunctor
70 ::comphelper::UStringMixEqual m_aCase;
71 std::vector< OUString>* m_pVector;
72 public:
73 TMultiListBoxEntryFindFunctor(std::vector< OUString>* _pVector,
74 const ::comphelper::UStringMixEqual& _aCase)
75 :m_aCase(_aCase)
76 ,m_pVector(_pVector)
80 virtual ~TMultiListBoxEntryFindFunctor() {}
82 bool operator()(const OUString& _sColumnName) const override
84 return std::any_of(m_pVector->begin(),m_pVector->end(),
85 [this, &_sColumnName](const OUString& lhs)
86 { return m_aCase(lhs, _sColumnName); });
90 // ICopyTableSourceObject
91 /** interface to an object to copy to another DB, using the OCopyTableWizard
93 when the wizard is used to copy an object to another DB, it usually requires
94 a sdbcx-level or sdb-level object (a css.sdbcx.Table or css.sdb.Query, that is).
96 However, to also support copying tables from sdbc-level connections, we allow to
97 work with the object name only. This implies some less features (like copying the
98 UI settings of a table is not done), but still allows to copy definition and data.
100 class ICopyTableSourceObject
102 public:
103 /// retrieves the fully qualified name of the object to copy
104 virtual OUString getQualifiedObjectName() const = 0;
105 /// determines whether the object is a view
106 virtual bool isView() const = 0;
107 /** copies the UI settings of the object to the given target object. Might be
108 ignored by implementations which do not have Ui settings.
110 virtual void copyUISettingsTo( const css::uno::Reference< css::beans::XPropertySet >& _rxObject ) const = 0;
111 /// retrieves the column names of the to-be-copied object
112 virtual css::uno::Sequence< OUString >
113 getColumnNames() const = 0;
114 /// retrieves the names of the primary keys of the to-be-copied object
115 virtual css::uno::Sequence< OUString >
116 getPrimaryKeyColumnNames() const = 0;
117 /// creates a OFieldDescription for the given column of the to-be-copied object
118 virtual OFieldDescription* createFieldDescription( const OUString& _rColumnName ) const = 0;
119 /// returns the SELECT statement which can be used to retrieve the data of the to-be-copied object
120 virtual OUString getSelectStatement() const = 0;
122 /** copies the filter and sorting
124 * \return
126 virtual void copyFilterAndSortingTo(const css::uno::Reference< css::sdbc::XConnection >& _xConnection,const css::uno::Reference< css::beans::XPropertySet >& _rxObject ) const = 0;
128 /** returns the prepared statement which can be used to retrieve the data of the to-be-copied object
130 The default implementation of this method will simply prepare a statement with the return value
131 of ->getSelectStatement.
133 virtual ::utl::SharedUNOComponent< css::sdbc::XPreparedStatement >
134 getPreparedSelectStatement() const = 0;
136 virtual ~ICopyTableSourceObject();
139 // ObjectCopySource
140 class ObjectCopySource : public ICopyTableSourceObject
142 private:
143 css::uno::Reference< css::sdbc::XConnection > m_xConnection;
144 css::uno::Reference< css::sdbc::XDatabaseMetaData > m_xMetaData;
145 css::uno::Reference< css::beans::XPropertySet > m_xObject;
146 css::uno::Reference< css::beans::XPropertySetInfo > m_xObjectPSI;
147 css::uno::Reference< css::container::XNameAccess > m_xObjectColumns;
149 public:
150 ObjectCopySource(
151 const css::uno::Reference< css::sdbc::XConnection >& _rxConnection,
152 const css::uno::Reference< css::beans::XPropertySet >& _rxObject
155 // ICopyTableSourceObject overridables
156 virtual OUString getQualifiedObjectName() const override;
157 virtual bool isView() const override;
158 virtual void copyUISettingsTo( const css::uno::Reference< css::beans::XPropertySet >& _rxObject ) const override;
159 virtual void copyFilterAndSortingTo(const css::uno::Reference< css::sdbc::XConnection >& _xConnection, const css::uno::Reference< css::beans::XPropertySet >& _rxObject ) const override;
160 virtual css::uno::Sequence< OUString >
161 getColumnNames() const override;
162 virtual css::uno::Sequence< OUString >
163 getPrimaryKeyColumnNames() const override;
164 virtual OFieldDescription* createFieldDescription( const OUString& _rColumnName ) const override;
165 virtual OUString getSelectStatement() const override;
166 virtual ::utl::SharedUNOComponent< css::sdbc::XPreparedStatement >
167 getPreparedSelectStatement() const override;
170 // NamedTableCopySource
171 class NamedTableCopySource : public ICopyTableSourceObject
173 private:
174 css::uno::Reference< css::sdbc::XConnection > m_xConnection;
175 css::uno::Reference< css::sdbc::XDatabaseMetaData > m_xMetaData;
176 OUString m_sTableName;
177 OUString m_sTableCatalog;
178 OUString m_sTableSchema;
179 OUString m_sTableBareName;
180 std::vector< OFieldDescription > m_aColumnInfo;
181 ::utl::SharedUNOComponent< css::sdbc::XPreparedStatement > m_xStatement;
183 public:
184 NamedTableCopySource(
185 const css::uno::Reference< css::sdbc::XConnection >& _rxConnection,
186 OUString _sTableName
189 // ICopyTableSourceObject overridables
190 virtual OUString getQualifiedObjectName() const override;
191 virtual bool isView() const override;
192 virtual void copyUISettingsTo( const css::uno::Reference< css::beans::XPropertySet >& _rxObject ) const override;
193 virtual void copyFilterAndSortingTo(const css::uno::Reference< css::sdbc::XConnection >& _xConnection,const css::uno::Reference< css::beans::XPropertySet >& _rxObject ) const override;
194 virtual css::uno::Sequence< OUString >
195 getColumnNames() const override;
196 virtual css::uno::Sequence< OUString >
197 getPrimaryKeyColumnNames() const override;
198 virtual OFieldDescription* createFieldDescription( const OUString& _rColumnName ) const override;
199 virtual OUString getSelectStatement() const override;
200 virtual ::utl::SharedUNOComponent< css::sdbc::XPreparedStatement >
201 getPreparedSelectStatement() const override;
203 private:
204 void impl_ensureColumnInfo_throw();
205 ::utl::SharedUNOComponent< css::sdbc::XPreparedStatement > const &
206 impl_ensureStatement_throw();
209 // Wizard Dialog
210 class OCopyTableWizard : public vcl::RoadmapWizardMachine
212 friend class OWizColumnSelect;
213 friend class OWizTypeSelect;
214 friend class OWizTypeSelectControl;
215 friend class OCopyTable;
216 friend class OWizNameMatching;
218 public:
219 typedef std::map<OUString, OUString, ::comphelper::UStringMixLess> TNameMapping;
221 enum Wizard_Button_Style
223 WIZARD_NEXT,
224 WIZARD_PREV,
225 WIZARD_FINISH,
227 WIZARD_NONE
230 private:
231 ODatabaseExport::TColumns m_vDestColumns; // contains the columns
232 ODatabaseExport::TColumnVector m_aDestVec; // the order to insert the columns
233 ODatabaseExport::TColumns m_vSourceColumns;
234 ODatabaseExport::TColumnVector m_vSourceVec;
236 OTypeInfoMap m_aTypeInfo;
237 std::vector<OTypeInfoMap::iterator> m_aTypeInfoIndex;
238 OTypeInfoMap m_aDestTypeInfo;
239 std::vector<OTypeInfoMap::iterator> m_aDestTypeInfoIndex;
240 TNameMapping m_mNameMapping;
242 ODatabaseExport::TPositions m_vColumnPositions;
243 std::vector<sal_Int32> m_vColumnTypes;
245 css::uno::Reference< css::sdbc::XConnection > m_xDestConnection;
247 const ICopyTableSourceObject& m_rSourceObject;
249 css::uno::Reference< css::util::XNumberFormatter > m_xFormatter;
250 css::uno::Reference< css::uno::XComponentContext> m_xContext;
251 css::uno::Reference< css::task::XInteractionHandler> m_xInteractionHandler;
253 OUString m_sTypeNames; // these type names are the ones out of the resource file
254 sal_uInt32 m_nPageCount;
255 bool m_bDeleteSourceColumns;
256 bool m_bInterConnectionCopy; // are we copying between different connections?
258 css::lang::Locale m_aLocale;
259 OUString m_sName; // for a table the name is composed
260 OUString m_sSourceName;
261 OUString m_aKeyName;
262 TOTypeInfoSP m_pTypeInfo; // default type
263 bool m_bAddPKFirstTime;
264 sal_Int16 m_nOperation;
265 Wizard_Button_Style m_ePressed;
266 bool m_bCreatePrimaryKeyColumn;
267 bool m_bUseHeaderLine;
269 private:
270 DECL_LINK( ImplPrevHdl, weld::Button&, void );
271 DECL_LINK( ImplNextHdl, weld::Button&, void);
272 DECL_LINK( ImplOKHdl, weld::Button&, void );
273 bool CheckColumns(sal_Int32& _rnBreakPos);
274 void loadData( const ICopyTableSourceObject& _rSourceObject,
275 ODatabaseExport::TColumns& _rColumns,
276 ODatabaseExport::TColumnVector& _rColVector );
277 void construct();
278 // need for table creation
279 static void appendColumns( css::uno::Reference< css::sdbcx::XColumnsSupplier> const & _rxColSup, const ODatabaseExport::TColumnVector* _pVec, bool _bKeyColumns = false );
280 static void appendKey(css::uno::Reference< css::sdbcx::XKeysSupplier> const & _rxSup,const ODatabaseExport::TColumnVector* _pVec);
281 // checks if the type is supported in the destination database
282 bool supportsType(sal_Int32 _nDataType,sal_Int32& _rNewDataType);
284 virtual std::unique_ptr<BuilderPage> createPage(vcl::WizardTypes::WizardState /*nState*/) override
286 assert(false);
287 return nullptr;
290 virtual void ActivatePage() override;
292 sal_uInt16 GetCurLevel() const { return getCurrentState(); }
294 weld::Container* CreatePageContainer();
296 public:
297 // used for copy tables or queries
298 OCopyTableWizard(
299 weld::Window * pParent,
300 const OUString& _rDefaultName,
301 sal_Int16 _nOperation,
302 const ICopyTableSourceObject& _rSourceObject,
303 const css::uno::Reference< css::sdbc::XConnection >& _xSourceConnection,
304 const css::uno::Reference< css::sdbc::XConnection >& _xConnection,
305 const css::uno::Reference< css::uno::XComponentContext >& _rxContext,
306 const css::uno::Reference< css::task::XInteractionHandler>& _xInteractionHandler
309 // used for importing rtf/html sources
310 OCopyTableWizard(
311 weld::Window* pParent,
312 OUString _sDefaultName,
313 sal_Int16 _nOperation,
314 ODatabaseExport::TColumns&& _rDestColumns,
315 const ODatabaseExport::TColumnVector& _rSourceColVec,
316 const css::uno::Reference< css::sdbc::XConnection >& _xConnection,
317 const css::uno::Reference< css::util::XNumberFormatter >& _xFormatter,
318 TypeSelectionPageFactory _pTypeSelectionPageFactory,
319 SvStream& _rTypeSelectionPageArg,
320 const css::uno::Reference< css::uno::XComponentContext >& _rxContext
323 virtual ~OCopyTableWizard() override;
325 virtual bool DeactivatePage() override;
326 weld::Button& GetOKButton() { return *m_xFinish; }
327 Wizard_Button_Style GetPressedButton() const { return m_ePressed; }
328 void EnableNextButton(bool bEnable);
329 void AddWizardPage(std::unique_ptr<OWizardPage> xPage); // delete page from OCopyTableWizard
330 void CheckButtons(); // checks which button can be disabled, enabled
332 // returns a vector where the position of a column and if the column is in the selection
333 // when not the value is COLUMN_POSITION_NOT_FOUND.
334 const ODatabaseExport::TPositions& GetColumnPositions() const { return m_vColumnPositions; }
335 const std::vector<sal_Int32>& GetColumnTypes() const { return m_vColumnTypes; }
336 bool UseHeaderLine() const { return m_bUseHeaderLine; }
337 void setUseHeaderLine(bool _bUseHeaderLine) { m_bUseHeaderLine = _bUseHeaderLine; }
339 void insertColumn(sal_Int32 _nPos,OFieldDescription* _pField);
341 /** replaces a field description with another one. The name must not be known so far.
342 @param _nPos
343 The pos inside the vector, 0 based.
344 @param _pField
345 The field to set.
346 @param _sOldName
347 The name of column to be replaced.
349 void replaceColumn(sal_Int32 _nPos,OFieldDescription* _pField,const OUString& _sOldName);
351 /** returns whether a primary key should be created in the target database
353 bool shouldCreatePrimaryKey() const { return m_bCreatePrimaryKeyColumn;}
354 void setCreatePrimaryKey( bool _bDoCreate, const OUString& _rSuggestedName );
356 static bool supportsPrimaryKey( const css::uno::Reference< css::sdbc::XConnection >& _rxConnection );
357 bool supportsPrimaryKey() const { return supportsPrimaryKey( m_xDestConnection ); }
359 static bool supportsViews( const css::uno::Reference< css::sdbc::XConnection >& _rxConnection );
360 bool supportsViews() const { return supportsViews( m_xDestConnection ); }
362 /** returns the name of the primary key
363 @return
364 The name of the primary key.
366 const OUString& getPrimaryKeyName() const { return m_aKeyName; }
368 const OTypeInfoMap& getTypeInfo() const { return m_aTypeInfo; }
370 TOTypeInfoSP const & getDestTypeInfo(sal_Int32 _nPos) const { return m_aDestTypeInfoIndex[_nPos]->second; }
371 const OTypeInfoMap& getDestTypeInfo() const { return m_aDestTypeInfo; }
373 const css::lang::Locale& GetLocale() const { return m_aLocale; }
374 const css::uno::Reference< css::util::XNumberFormatter >& GetFormatter() const { return m_xFormatter; }
375 const css::uno::Reference< css::uno::XComponentContext>& GetComponentContext() const { return m_xContext; }
377 const ODatabaseExport::TColumns& getSourceColumns() const{ return m_vSourceColumns; }
378 const ODatabaseExport::TColumnVector& getSrcVector() const { return m_vSourceVec; }
379 ODatabaseExport::TColumns& getDestColumns() { return m_vDestColumns; }
380 const ODatabaseExport::TColumnVector& getDestVector() const { return m_aDestVec; }
381 const OUString& getName() const { return m_sName; }
383 /** clears the dest vectors
385 void clearDestColumns();
387 css::uno::Reference< css::beans::XPropertySet > returnTable();
388 css::uno::Reference< css::beans::XPropertySet > getTable() const;
389 css::uno::Reference< css::beans::XPropertySet > createTable();
390 css::uno::Reference< css::beans::XPropertySet > createView() const;
391 sal_Int32 getMaxColumnNameLength() const;
393 void setOperation( const sal_Int16 _nOperation );
394 sal_Int16 getOperation() const { return m_nOperation;}
396 OUString convertColumnName( const TColumnFindFunctor& _rCmpFunctor,
397 const OUString& _sColumnName,
398 std::u16string_view _sExtraChars,
399 sal_Int32 _nMaxNameLen);
400 TOTypeInfoSP convertType(const TOTypeInfoSP&_pType, bool& _bNotConvert);
402 OUString createUniqueName(const OUString& _sName);
404 // displays an error message that a column type is not supported
405 void showColumnTypeNotSupported(std::u16string_view _rColumnName);
407 void removeColumnNameFromNameMap(const OUString& _sName);
408 void showError(const OUString& _sErrorMessage);
409 void showError(const css::uno::Any& _aError);
413 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */