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