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