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 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>
48 typedef ::std::unary_function
< OUString
,bool> TColumnFindFunctorType
;
49 class TColumnFindFunctor
: public TColumnFindFunctorType
52 virtual bool operator()(const OUString
& _sColumnName
) const = 0;
55 ~TColumnFindFunctor() {}
58 class TExportColumnFindFunctor
: public TColumnFindFunctor
60 ODatabaseExport::TColumns
* m_pColumns
;
62 TExportColumnFindFunctor(ODatabaseExport::TColumns
* _pColumns
)
64 m_pColumns
= _pColumns
;
67 virtual ~TExportColumnFindFunctor() {}
69 bool operator()(const OUString
& _sColumnName
) const 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
;
80 TMultiListBoxEntryFindFunctor(::std::vector
< OUString
>* _pVector
,
81 const ::comphelper::UStringMixEqual
& _aCase
)
87 virtual ~TMultiListBoxEntryFindFunctor() {}
89 bool operator()(const OUString
& _sColumnName
) const 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
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 css::uno::Reference
< css::beans::XPropertySet
>& _rxObject
) const = 0;
117 /// retrieves the column names of the to-be-copied object
118 virtual css::uno::Sequence
< OUString
>
119 getColumnNames() const = 0;
120 /// retrieves the names of the primary keys of the to-be-copied object
121 virtual css::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 css::uno::Reference
< css::sdbc::XConnection
>& _xConnection
,const css::uno::Reference
< css::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
< css::sdbc::XPreparedStatement
>
140 getPreparedSelectStatement() const = 0;
142 virtual ~ICopyTableSourceObject();
146 class ObjectCopySource
: public ICopyTableSourceObject
149 css::uno::Reference
< css::sdbc::XConnection
> m_xConnection
;
150 css::uno::Reference
< css::sdbc::XDatabaseMetaData
> m_xMetaData
;
151 css::uno::Reference
< css::beans::XPropertySet
> m_xObject
;
152 css::uno::Reference
< css::beans::XPropertySetInfo
> m_xObjectPSI
;
153 css::uno::Reference
< css::container::XNameAccess
> m_xObjectColumns
;
157 const css::uno::Reference
< css::sdbc::XConnection
>& _rxConnection
,
158 const css::uno::Reference
< css::beans::XPropertySet
>& _rxObject
161 // ICopyTableSourceObject overridables
162 virtual OUString
getQualifiedObjectName() const override
;
163 virtual bool isView() const override
;
164 virtual void copyUISettingsTo( const css::uno::Reference
< css::beans::XPropertySet
>& _rxObject
) const override
;
165 virtual void copyFilterAndSortingTo(const css::uno::Reference
< css::sdbc::XConnection
>& _xConnection
, const css::uno::Reference
< css::beans::XPropertySet
>& _rxObject
) const override
;
166 virtual css::uno::Sequence
< OUString
>
167 getColumnNames() const override
;
168 virtual css::uno::Sequence
< OUString
>
169 getPrimaryKeyColumnNames() const override
;
170 virtual OFieldDescription
* createFieldDescription( const OUString
& _rColumnName
) const override
;
171 virtual OUString
getSelectStatement() const override
;
172 virtual ::utl::SharedUNOComponent
< css::sdbc::XPreparedStatement
>
173 getPreparedSelectStatement() const override
;
176 // NamedTableCopySource
177 class NamedTableCopySource
: public ICopyTableSourceObject
180 css::uno::Reference
< css::sdbc::XConnection
> m_xConnection
;
181 css::uno::Reference
< css::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
< css::sdbc::XPreparedStatement
> m_xStatement
;
190 NamedTableCopySource(
191 const css::uno::Reference
< css::sdbc::XConnection
>& _rxConnection
,
192 const OUString
& _rTableName
195 // ICopyTableSourceObject overridables
196 virtual OUString
getQualifiedObjectName() const override
;
197 virtual bool isView() const override
;
198 virtual void copyUISettingsTo( const css::uno::Reference
< css::beans::XPropertySet
>& _rxObject
) const override
;
199 virtual void copyFilterAndSortingTo(const css::uno::Reference
< css::sdbc::XConnection
>& _xConnection
,const css::uno::Reference
< css::beans::XPropertySet
>& _rxObject
) const override
;
200 virtual css::uno::Sequence
< OUString
>
201 getColumnNames() const override
;
202 virtual css::uno::Sequence
< OUString
>
203 getPrimaryKeyColumnNames() const override
;
204 virtual OFieldDescription
* createFieldDescription( const OUString
& _rColumnName
) const override
;
205 virtual OUString
getSelectStatement() const override
;
206 virtual ::utl::SharedUNOComponent
< css::sdbc::XPreparedStatement
>
207 getPreparedSelectStatement() const override
;
210 void impl_ensureColumnInfo_throw();
211 ::utl::SharedUNOComponent
< css::sdbc::XPreparedStatement
>
212 impl_ensureStatement_throw();
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
;
225 typedef std::map
<OUString
, OUString
, ::comphelper::UStringMixLess
> TNameMapping
;
227 enum Wizard_Button_Style
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 css::uno::Reference
< css::sdbc::XConnection
> m_xDestConnection
;
259 const ICopyTableSourceObject
& m_rSourceObject
;
261 css::uno::Reference
< css::util::XNumberFormatter
> m_xFormatter
;
262 css::uno::Reference
< css::uno::XComponentContext
> m_xContext
;
263 css::uno::Reference
< css::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 css::lang::Locale m_aLocale
;
271 OUString m_sName
; // for a table the name is composed
272 OUString m_sSourceName
;
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
;
282 DECL_LINK_TYPED( ImplPrevHdl
, Button
*, void );
283 DECL_LINK_TYPED( ImplNextHdl
, Button
*, void);
284 DECL_LINK_TYPED( ImplOKHdl
, Button
*, void );
285 DECL_LINK_TYPED( ImplActivateHdl
, WizardDialog
*, void );
286 bool CheckColumns(sal_Int32
& _rnBreakPos
);
287 void loadData( const ICopyTableSourceObject
& _rSourceObject
,
288 ODatabaseExport::TColumns
& _rColumns
,
289 ODatabaseExport::TColumnVector
& _rColVector
);
291 // need for table creation
292 static void appendColumns( css::uno::Reference
< css::sdbcx::XColumnsSupplier
>& _rxColSup
, const ODatabaseExport::TColumnVector
* _pVec
, bool _bKeyColumns
= false );
293 static void appendKey(css::uno::Reference
< css::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();
300 // used for copy tables or queries
302 vcl::Window
* pParent
,
303 const OUString
& _rDefaultName
,
304 sal_Int16 _nOperation
,
305 const ICopyTableSourceObject
& _rSourceObject
,
306 const css::uno::Reference
< css::sdbc::XConnection
>& _xSourceConnection
,
307 const css::uno::Reference
< css::sdbc::XConnection
>& _xConnection
,
308 const css::uno::Reference
< css::uno::XComponentContext
>& _rxContext
,
309 const css::uno::Reference
< css::task::XInteractionHandler
>& _xInteractionHandler
312 // used for importing rtf/html sources
314 vcl::Window
* pParent
,
315 const OUString
& _rDefaultName
,
316 sal_Int16 _nOperation
,
317 const ODatabaseExport::TColumns
& _rDestColumns
,
318 const ODatabaseExport::TColumnVector
& _rSourceColVec
,
319 const css::uno::Reference
< css::sdbc::XConnection
>& _xConnection
,
320 const css::uno::Reference
< css::util::XNumberFormatter
>& _xFormatter
,
321 TypeSelectionPageFactory _pTypeSelectionPageFactory
,
322 SvStream
& _rTypeSelectionPageArg
,
323 const css::uno::Reference
< css::uno::XComponentContext
>& _rxContext
326 virtual ~OCopyTableWizard();
327 virtual void dispose() override
;
329 virtual bool DeactivatePage() override
;
330 OKButton
& GetOKButton() { return static_cast<OKButton
&>(*m_pbFinish
); }
331 Wizard_Button_Style
GetPressedButton() const { return m_ePressed
; }
332 void EnableNextButton(bool bEnable
);
333 void AddWizardPage(OWizardPage
* pPage
); // delete page from OCopyTableWizard
334 void CheckButtons(); // checks which button can be disabled, enabled
336 // returns a vector where the position of a column and if the column is in the selection
337 // when not the value is COLUMN_POSITION_NOT_FOUND == (sal_uInt32)-1
338 const ODatabaseExport::TPositions
& GetColumnPositions() const { return m_vColumnPos
; }
339 const ::std::vector
<sal_Int32
>& GetColumnTypes() const { return m_vColumnTypes
; }
340 bool UseHeaderLine() const { return m_bUseHeaderLine
; }
341 void setUseHeaderLine(bool _bUseHeaderLine
) { m_bUseHeaderLine
= _bUseHeaderLine
; }
343 void insertColumn(sal_Int32 _nPos
,OFieldDescription
* _pField
);
345 /** replaces a field description with another one. The name must not be known so far.
347 The pos inside the vector, 0 based.
351 The name of column to be replaced.
353 void replaceColumn(sal_Int32 _nPos
,OFieldDescription
* _pField
,const OUString
& _sOldName
);
355 /** returns whether a primary key should be created in the target database
357 bool shouldCreatePrimaryKey() const { return m_bCreatePrimaryKeyColumn
;}
358 void setCreatePrimaryKey( bool _bDoCreate
, const OUString
& _rSuggestedName
);
360 static bool supportsPrimaryKey( const css::uno::Reference
< css::sdbc::XConnection
>& _rxConnection
);
361 bool supportsPrimaryKey() const { return supportsPrimaryKey( m_xDestConnection
); }
363 static bool supportsViews( const css::uno::Reference
< css::sdbc::XConnection
>& _rxConnection
);
364 bool supportsViews() const { return supportsViews( m_xDestConnection
); }
366 /** returns the name of the primary key
368 The name of the primary key.
370 const OUString
& getPrimaryKeyName() const { return m_aKeyName
; }
372 const OTypeInfoMap
& getTypeInfo() const { return m_aTypeInfo
; }
374 TOTypeInfoSP
getDestTypeInfo(sal_Int32 _nPos
) const { return m_aDestTypeInfoIndex
[_nPos
]->second
; }
375 const OTypeInfoMap
& getDestTypeInfo() const { return m_aDestTypeInfo
; }
377 const css::lang::Locale
& GetLocale() const { return m_aLocale
; }
378 const css::uno::Reference
< css::util::XNumberFormatter
>& GetFormatter() const { return m_xFormatter
; }
379 const css::uno::Reference
< css::uno::XComponentContext
>& GetComponentContext() const { return m_xContext
; }
381 const ODatabaseExport::TColumns
& getSourceColumns() const{ return m_vSourceColumns
; }
382 const ODatabaseExport::TColumnVector
& getSrcVector() const { return m_vSourceVec
; }
383 ODatabaseExport::TColumns
& getDestColumns() { return m_vDestColumns
; }
384 const ODatabaseExport::TColumnVector
& getDestVector() const { return m_aDestVec
; }
385 const OUString
& getName() const { return m_sName
; }
387 /** clears the dest vectors
389 void clearDestColumns();
391 css::uno::Reference
< css::beans::XPropertySet
> createTable();
392 css::uno::Reference
< css::beans::XPropertySet
> createView() const;
393 sal_Int32
getMaxColumnNameLength() const;
395 void setOperation( const sal_Int16 _nOperation
);
396 sal_Int16
getOperation() const { return m_nOperation
;}
398 OUString
convertColumnName( const TColumnFindFunctor
& _rCmpFunctor
,
399 const OUString
& _sColumnName
,
400 const OUString
& _sExtraChars
,
401 sal_Int32 _nMaxNameLen
);
402 TOTypeInfoSP
convertType(const TOTypeInfoSP
&_pType
, bool& _bNotConvert
);
404 OUString
createUniqueName(const OUString
& _sName
);
406 // displays a error message that a column type is not supported
407 void showColumnTypeNotSupported(const OUString
& _rColumnName
);
409 void removeColumnNameFromNameMap(const OUString
& _sName
);
410 void showError(const OUString
& _sErrorMessage
);
411 void showError(const css::uno::Any
& _aError
);
415 #endif // INCLUDED_DBACCESS_SOURCE_UI_INC_WCOPYTABLE_HXX
417 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */