Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifndef INCLUDED_DBACCESS_SOURCE_UI_INC_WCOPYTABLE_HXX
21 : #define INCLUDED_DBACCESS_SOURCE_UI_INC_WCOPYTABLE_HXX
22 :
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>
44 :
45 : namespace dbaui
46 : {
47 :
48 : typedef ::std::unary_function< OUString,bool> TColumnFindFunctorType;
49 0 : class TColumnFindFunctor : public TColumnFindFunctorType
50 : {
51 : public:
52 : virtual bool operator()(const OUString& _sColumnName) const = 0;
53 :
54 : protected:
55 0 : ~TColumnFindFunctor() {}
56 : };
57 :
58 : class TExportColumnFindFunctor : public TColumnFindFunctor
59 : {
60 : ODatabaseExport::TColumns* m_pColumns;
61 : public:
62 0 : TExportColumnFindFunctor(ODatabaseExport::TColumns* _pColumns)
63 0 : {
64 0 : m_pColumns = _pColumns;
65 0 : }
66 :
67 0 : virtual ~TExportColumnFindFunctor() {}
68 :
69 0 : bool operator()(const OUString& _sColumnName) const SAL_OVERRIDE
70 : {
71 0 : return m_pColumns->find(_sColumnName) != m_pColumns->end();
72 : }
73 : };
74 :
75 : class TMultiListBoxEntryFindFunctor : public TColumnFindFunctor
76 : {
77 : ::comphelper::UStringMixEqual m_aCase;
78 : ::std::vector< OUString>* m_pVector;
79 : public:
80 0 : TMultiListBoxEntryFindFunctor(::std::vector< OUString>* _pVector,
81 : const ::comphelper::UStringMixEqual& _aCase)
82 : :m_aCase(_aCase)
83 0 : ,m_pVector(_pVector)
84 : {
85 0 : }
86 :
87 0 : virtual ~TMultiListBoxEntryFindFunctor() {}
88 :
89 0 : bool operator()(const OUString& _sColumnName) const SAL_OVERRIDE
90 : {
91 : return ::std::any_of(m_pVector->begin(),m_pVector->end(),
92 0 : ::std::bind2nd(m_aCase, _sColumnName));
93 : }
94 : };
95 :
96 : // ICopyTableSourceObject
97 : /** interface to an object to copy to another DB, using the OCopyTableWizard
98 :
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).
101 :
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.
105 : */
106 0 : class ICopyTableSourceObject
107 : {
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.
115 : */
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;
127 :
128 : /** copies the filter and sorting
129 : *
130 : * \return
131 : */
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;
133 :
134 : /** returns the prepared statement which can be used to retrieve the data of the to-be-copied object
135 :
136 : The default implementation of this method will simply prepare a statement with the return value
137 : of ->getSelectStatement.
138 : */
139 : virtual ::utl::SharedUNOComponent< ::com::sun::star::sdbc::XPreparedStatement >
140 : getPreparedSelectStatement() const = 0;
141 :
142 : virtual ~ICopyTableSourceObject();
143 : };
144 :
145 : // ObjectCopySource
146 0 : class ObjectCopySource : public ICopyTableSourceObject
147 : {
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;
154 :
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
159 : );
160 :
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;
174 : };
175 :
176 : // NamedTableCopySource
177 0 : class NamedTableCopySource : public ICopyTableSourceObject
178 : {
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;
188 :
189 : public:
190 : NamedTableCopySource(
191 : const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _rxConnection,
192 : const OUString& _rTableName
193 : );
194 :
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;
208 :
209 : private:
210 : void impl_ensureColumnInfo_throw();
211 : ::utl::SharedUNOComponent< ::com::sun::star::sdbc::XPreparedStatement >
212 : impl_ensureStatement_throw();
213 : };
214 :
215 : // Wizard Dialog
216 : class OCopyTableWizard : public WizardDialog
217 : {
218 : friend class OWizColumnSelect;
219 : friend class OWizTypeSelect;
220 : friend class OWizTypeSelectControl;
221 : friend class OCopyTable;
222 : friend class OWizNameMatching;
223 :
224 : public:
225 : typedef std::map<OUString, OUString, ::comphelper::UStringMixLess> TNameMapping;
226 :
227 : enum Wizard_Button_Style
228 : {
229 : WIZARD_NEXT,
230 : WIZARD_PREV,
231 : WIZARD_FINISH,
232 :
233 : WIZARD_NONE
234 : };
235 :
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;
241 :
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;
247 :
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;
253 :
254 : ODatabaseExport::TPositions m_vColumnPos;
255 : ::std::vector<sal_Int32> m_vColumnTypes;
256 :
257 : ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection > m_xDestConnection;
258 :
259 : const ICopyTableSourceObject& m_rSourceObject;
260 :
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;
264 :
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?
269 :
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;
280 :
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);
296 :
297 : void impl_loadSourceData();
298 :
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
310 : );
311 :
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
324 : );
325 :
326 : virtual ~OCopyTableWizard();
327 : virtual void dispose() SAL_OVERRIDE;
328 :
329 : virtual bool DeactivatePage() SAL_OVERRIDE;
330 0 : OKButton& GetOKButton() { return static_cast<OKButton&>(*m_pbFinish); }
331 0 : 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
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
339 0 : ODatabaseExport::TPositions GetColumnPositions() const { return m_vColumnPos; }
340 0 : ::std::vector<sal_Int32> GetColumnTypes() const { return m_vColumnTypes; }
341 0 : bool UseHeaderLine() const { return m_bUseHeaderLine; }
342 0 : void setUseHeaderLine(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 OUString& _sOldName);
355 :
356 : /** returns whether a primary key should be created in the target database
357 : */
358 0 : bool shouldCreatePrimaryKey() const { return m_bCreatePrimaryKeyColumn;}
359 : void setCreatePrimaryKey( bool _bDoCreate, const OUString& _rSuggestedName );
360 :
361 : static bool supportsPrimaryKey( const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _rxConnection );
362 0 : bool supportsPrimaryKey() const { return supportsPrimaryKey( m_xDestConnection ); }
363 :
364 : static bool supportsViews( const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _rxConnection );
365 0 : 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 : */
371 0 : OUString getPrimaryKeyName() const { return m_aKeyName; }
372 :
373 : TOTypeInfoSP getTypeInfo(sal_Int32 _nPos) const { return m_aTypeInfoIndex[_nPos]->second; }
374 0 : const OTypeInfoMap& getTypeInfo() const { return m_aTypeInfo; }
375 :
376 0 : TOTypeInfoSP getDestTypeInfo(sal_Int32 _nPos) const { return m_aDestTypeInfoIndex[_nPos]->second; }
377 0 : const OTypeInfoMap& getDestTypeInfo() const { return m_aDestTypeInfo; }
378 :
379 0 : ::com::sun::star::lang::Locale GetLocale() const { return m_aLocale; }
380 0 : ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter > GetFormatter() const { return m_xFormatter; }
381 0 : ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext> GetComponentContext() const { return m_xContext; }
382 :
383 0 : const ODatabaseExport::TColumns& getSourceColumns() const{ return m_vSourceColumns; }
384 0 : const ODatabaseExport::TColumnVector& getSrcVector() const { return m_vSourceVec; }
385 0 : ODatabaseExport::TColumns& getDestColumns() { return m_vDestColumns; }
386 0 : const ODatabaseExport::TColumnVector& getDestVector() const { return m_aDestVec; }
387 0 : 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 0 : sal_Int16 getOperation() const { return m_nOperation;}
399 :
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);
405 :
406 : OUString createUniqueName(const OUString& _sName);
407 :
408 : // displays a error message that a column type is not supported
409 : void showColumnTypeNotSupported(const OUString& _rColumnName);
410 :
411 : void removeColumnNameFromNameMap(const OUString& _sName);
412 : void showError(const OUString& _sErrorMessage);
413 : void showError(const ::com::sun::star::uno::Any& _aError);
414 : };
415 : }
416 :
417 : #endif // INCLUDED_DBACCESS_SOURCE_UI_INC_WCOPYTABLE_HXX
418 :
419 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|