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