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 : #include "WCPage.hxx"
21 : #include "WCopyTable.hxx"
22 :
23 : #include "defaultobjectnamecheck.hxx"
24 : #include <tools/debug.hxx>
25 : #include "dbaccess_helpid.hrc"
26 : #include "dbu_misc.hrc"
27 : #include "WizardPages.hrc"
28 : #include <com/sun/star/sdbc/XResultSet.hpp>
29 : #include <com/sun/star/sdb/CommandType.hpp>
30 : #include <com/sun/star/sdbc/XRow.hpp>
31 : #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
32 : #include <com/sun/star/sdbcx/XViewsSupplier.hpp>
33 : #include <com/sun/star/sdb/application/CopyTableOperation.hpp>
34 : #include <vcl/msgbox.hxx>
35 : #include <connectivity/dbexception.hxx>
36 : #include <connectivity/dbtools.hxx>
37 : #include "UITools.hxx"
38 : #include "moduledbu.hxx"
39 : #include <cppuhelper/exc_hlp.hxx>
40 :
41 : using namespace ::dbaui;
42 : using namespace ::dbtools;
43 : using namespace ::com::sun::star::uno;
44 : using namespace ::com::sun::star::beans;
45 : using namespace ::com::sun::star::container;
46 : using namespace ::com::sun::star::util;
47 : using namespace ::com::sun::star::sdb;
48 : using namespace ::com::sun::star::sdbc;
49 : using namespace ::com::sun::star::sdbcx;
50 :
51 : namespace CopyTableOperation = ::com::sun::star::sdb::application::CopyTableOperation;
52 :
53 : // Klasse OCopyTable
54 0 : OCopyTable::OCopyTable( Window * pParent )
55 : :OWizardPage( pParent, ModuleRes(TAB_WIZ_COPYTABLE) )
56 : ,m_ftTableName( this, ModuleRes( FT_TABLENAME ) )
57 : ,m_edTableName( this, ModuleRes( ET_TABLENAME ) )
58 : ,m_aFL_Options( this, ModuleRes( FL_OPTIONS ) )
59 : ,m_aRB_DefData( this, ModuleRes( RB_DEFDATA ) )
60 : ,m_aRB_Def( this, ModuleRes( RB_DEF ) )
61 : ,m_aRB_View( this, ModuleRes( RB_VIEW ) )
62 : ,m_aRB_AppendData( this, ModuleRes( RB_APPENDDATA ) )
63 : ,m_aCB_UseHeaderLine( this, ModuleRes( CB_USEHEADERLINE ) )
64 : ,m_aCB_PrimaryColumn( this, ModuleRes( CB_PRIMARY_COLUMN ) )
65 : ,m_aFT_KeyName( this, ModuleRes( FT_KEYNAME ) )
66 : ,m_edKeyName( this, ModuleRes( ET_KEYNAME ) )
67 : ,m_nOldOperation(0)
68 : ,m_pPage2(NULL)
69 : ,m_pPage3(NULL)
70 : ,m_bPKeyAllowed(false)
71 0 : ,m_bUseHeaderAllowed(sal_True)
72 : {
73 :
74 0 : m_edTableName.SetMaxTextLen(EDIT_NOLIMIT);
75 :
76 0 : if ( m_pParent->m_xDestConnection.is() )
77 : {
78 0 : if ( !m_pParent->supportsViews() )
79 0 : m_aRB_View.Disable();
80 :
81 0 : m_aCB_UseHeaderLine.Check(true);
82 0 : m_bPKeyAllowed = m_pParent->supportsPrimaryKey();
83 :
84 0 : m_aCB_PrimaryColumn.Enable(m_bPKeyAllowed);
85 :
86 0 : m_aRB_AppendData.SetClickHdl( LINK( this, OCopyTable, AppendDataClickHdl ) );
87 :
88 0 : m_aRB_DefData.SetClickHdl( LINK( this, OCopyTable, RadioChangeHdl ) );
89 0 : m_aRB_Def.SetClickHdl( LINK( this, OCopyTable, RadioChangeHdl ) );
90 0 : m_aRB_View.SetClickHdl( LINK( this, OCopyTable, RadioChangeHdl ) );
91 :
92 0 : m_aCB_PrimaryColumn.SetClickHdl(LINK( this, OCopyTable, KeyClickHdl ) );
93 :
94 0 : m_aFT_KeyName.Enable(false);
95 0 : m_edKeyName.Enable(false);
96 0 : OUString sKeyName("ID");
97 0 : sKeyName = m_pParent->createUniqueName(sKeyName);
98 0 : m_edKeyName.SetText(sKeyName);
99 :
100 0 : sal_Int32 nMaxLen = m_pParent->getMaxColumnNameLength();
101 0 : m_edKeyName.SetMaxTextLen(nMaxLen ? nMaxLen : EDIT_NOLIMIT);
102 : }
103 :
104 0 : FreeResource();
105 :
106 0 : SetText(ModuleRes(STR_COPYTABLE_TITLE_COPY));
107 0 : }
108 :
109 0 : OCopyTable::~OCopyTable()
110 : {
111 0 : }
112 :
113 0 : IMPL_LINK( OCopyTable, AppendDataClickHdl, Button*, /*pButton*/ )
114 : {
115 :
116 0 : SetAppendDataRadio();
117 0 : return 0;
118 : }
119 :
120 0 : void OCopyTable::SetAppendDataRadio()
121 : {
122 0 : m_pParent->EnableButton(OCopyTableWizard::WIZARD_NEXT,sal_True);
123 0 : m_aFT_KeyName.Enable(false);
124 0 : m_aCB_PrimaryColumn.Enable(false);
125 0 : m_edKeyName.Enable(false);
126 0 : m_pParent->setOperation(CopyTableOperation::AppendData);
127 0 : }
128 :
129 0 : IMPL_LINK( OCopyTable, RadioChangeHdl, Button*, pButton )
130 : {
131 0 : m_pParent->EnableButton(OCopyTableWizard::WIZARD_NEXT,pButton != &m_aRB_View);
132 0 : sal_Bool bKey = m_bPKeyAllowed && pButton != &m_aRB_View;
133 0 : m_aFT_KeyName.Enable(bKey && m_aCB_PrimaryColumn.IsChecked());
134 0 : m_edKeyName.Enable(bKey && m_aCB_PrimaryColumn.IsChecked());
135 0 : m_aCB_PrimaryColumn.Enable(bKey);
136 0 : m_aCB_UseHeaderLine.Enable(m_bUseHeaderAllowed && IsOptionDefData());
137 :
138 : // set typ what to do
139 0 : if( IsOptionDefData() )
140 0 : m_pParent->setOperation( CopyTableOperation::CopyDefinitionAndData );
141 0 : else if( IsOptionDef() )
142 0 : m_pParent->setOperation( CopyTableOperation::CopyDefinitionOnly );
143 0 : else if( IsOptionView() )
144 0 : m_pParent->setOperation( CopyTableOperation::CreateAsView );
145 :
146 0 : return 0;
147 : }
148 :
149 0 : IMPL_LINK( OCopyTable, KeyClickHdl, Button*, /*pButton*/ )
150 : {
151 0 : m_edKeyName.Enable(m_aCB_PrimaryColumn.IsChecked());
152 0 : m_aFT_KeyName.Enable(m_aCB_PrimaryColumn.IsChecked());
153 0 : return 0;
154 : }
155 :
156 0 : sal_Bool OCopyTable::LeavePage()
157 : {
158 0 : m_pParent->m_bCreatePrimaryKeyColumn = (m_bPKeyAllowed && m_aCB_PrimaryColumn.IsEnabled()) ? m_aCB_PrimaryColumn.IsChecked() : sal_False;
159 0 : m_pParent->m_aKeyName = m_pParent->m_bCreatePrimaryKeyColumn ? m_edKeyName.GetText() : OUString();
160 0 : m_pParent->setUseHeaderLine( m_aCB_UseHeaderLine.IsChecked() );
161 :
162 : // first check if the table already exists in the database
163 0 : if( m_pParent->getOperation() != CopyTableOperation::AppendData )
164 : {
165 0 : m_pParent->clearDestColumns();
166 0 : DynamicTableOrQueryNameCheck aNameCheck( m_pParent->m_xDestConnection, CommandType::TABLE );
167 0 : SQLExceptionInfo aErrorInfo;
168 0 : if ( !aNameCheck.isNameValid( m_edTableName.GetText(), aErrorInfo ) )
169 : {
170 0 : aErrorInfo.append( SQLExceptionInfo::SQL_CONTEXT, ModuleRes( STR_SUGGEST_APPEND_TABLE_DATA ) );
171 0 : m_pParent->showError(aErrorInfo.get());
172 :
173 0 : return sal_False;
174 : }
175 :
176 : // have to check the length of the table name
177 0 : Reference< XDatabaseMetaData > xMeta = m_pParent->m_xDestConnection->getMetaData();
178 0 : OUString sCatalog;
179 0 : OUString sSchema;
180 0 : OUString sTable;
181 : ::dbtools::qualifiedNameComponents( xMeta,
182 : m_edTableName.GetText(),
183 : sCatalog,
184 : sSchema,
185 : sTable,
186 0 : ::dbtools::eInDataManipulation);
187 0 : sal_Int32 nMaxLength = xMeta->getMaxTableNameLength();
188 0 : if ( nMaxLength && sTable.getLength() > nMaxLength )
189 : {
190 0 : OUString sError(ModuleRes(STR_INVALID_TABLE_NAME_LENGTH));
191 0 : m_pParent->showError(sError);
192 0 : return sal_False;
193 : }
194 :
195 : // now we have to check if the name of the primary key already exists
196 0 : if ( m_pParent->m_bCreatePrimaryKeyColumn
197 0 : && m_pParent->m_aKeyName != m_pParent->createUniqueName(m_pParent->m_aKeyName) )
198 : {
199 0 : OUString aInfoString( ModuleRes(STR_WIZ_PKEY_ALREADY_DEFINED) );
200 0 : aInfoString += " ";
201 0 : aInfoString += m_pParent->m_aKeyName;
202 0 : m_pParent->showError(aInfoString);
203 0 : return sal_False;
204 0 : }
205 : }
206 :
207 0 : if ( m_edTableName.GetSavedValue() != m_edTableName.GetText() )
208 : { // table exists and name has changed
209 0 : if ( m_pParent->getOperation() == CopyTableOperation::AppendData )
210 : {
211 0 : if(!checkAppendData())
212 0 : return sal_False;
213 : }
214 0 : else if ( m_nOldOperation == CopyTableOperation::AppendData )
215 : {
216 0 : m_edTableName.SaveValue();
217 0 : return LeavePage();
218 : }
219 : }
220 : else
221 : { // table exist and is not new or doesn't exist and so on
222 0 : if ( CopyTableOperation::AppendData == m_pParent->getOperation() )
223 : {
224 0 : if( !checkAppendData() )
225 0 : return sal_False;
226 : }
227 : }
228 0 : m_pParent->m_sName = m_edTableName.GetText();
229 0 : m_edTableName.SaveValue();
230 :
231 0 : if(m_pParent->m_sName.isEmpty())
232 : {
233 0 : OUString sError(ModuleRes(STR_INVALID_TABLE_NAME));
234 0 : m_pParent->showError(sError);
235 0 : return sal_False;
236 : }
237 :
238 0 : return sal_True;
239 : }
240 :
241 0 : void OCopyTable::ActivatePage()
242 : {
243 0 : m_pParent->GetOKButton().Enable( true );
244 0 : m_nOldOperation = m_pParent->getOperation();
245 0 : m_edTableName.GrabFocus();
246 0 : m_aCB_UseHeaderLine.Check(m_pParent->UseHeaderLine());
247 0 : }
248 :
249 0 : OUString OCopyTable::GetTitle() const
250 : {
251 0 : return ModuleRes(STR_WIZ_TABLE_COPY);
252 : }
253 :
254 0 : void OCopyTable::Reset()
255 : {
256 0 : m_bFirstTime = sal_False;
257 :
258 0 : m_edTableName.SetText( m_pParent->m_sName );
259 0 : m_edTableName.SaveValue();
260 0 : }
261 :
262 0 : sal_Bool OCopyTable::checkAppendData()
263 : {
264 0 : m_pParent->clearDestColumns();
265 0 : Reference< XPropertySet > xTable;
266 0 : Reference< XTablesSupplier > xSup( m_pParent->m_xDestConnection, UNO_QUERY );
267 0 : Reference<XNameAccess> xTables;
268 0 : if(xSup.is())
269 0 : xTables = xSup->getTables();
270 0 : if(xTables.is() && xTables->hasByName(m_edTableName.GetText()))
271 : {
272 0 : const ODatabaseExport::TColumnVector* pSrcColumns = m_pParent->getSrcVector();
273 0 : const sal_uInt32 nSrcSize = pSrcColumns->size();
274 0 : m_pParent->m_vColumnPos.resize( nSrcSize, ODatabaseExport::TPositions::value_type( COLUMN_POSITION_NOT_FOUND, COLUMN_POSITION_NOT_FOUND ) );
275 0 : m_pParent->m_vColumnTypes.resize( nSrcSize , COLUMN_POSITION_NOT_FOUND );
276 :
277 : // set new destination
278 0 : xTables->getByName( m_edTableName.GetText() ) >>= xTable;
279 0 : ObjectCopySource aTableCopySource( m_pParent->m_xDestConnection, xTable );
280 0 : m_pParent->loadData( aTableCopySource, m_pParent->m_vDestColumns, m_pParent->m_aDestVec );
281 0 : const ODatabaseExport::TColumnVector* pDestColumns = m_pParent->getDestVector();
282 0 : ODatabaseExport::TColumnVector::const_iterator aDestIter = pDestColumns->begin();
283 0 : ODatabaseExport::TColumnVector::const_iterator aDestEnd = pDestColumns->end();
284 0 : const sal_uInt32 nDestSize = pDestColumns->size();
285 : sal_Bool bNotConvert;
286 0 : sal_uInt32 i = 0;
287 0 : for(sal_Int32 nPos = 1;aDestIter != aDestEnd && i < nDestSize && i < nSrcSize;++aDestIter,++nPos,++i)
288 : {
289 0 : bNotConvert = sal_True;
290 0 : m_pParent->m_vColumnPos[i] = ODatabaseExport::TPositions::value_type(nPos,nPos);
291 0 : TOTypeInfoSP pTypeInfo = m_pParent->convertType((*aDestIter)->second->getSpecialTypeInfo(),bNotConvert);
292 0 : if ( !bNotConvert )
293 : {
294 0 : m_pParent->showColumnTypeNotSupported((*aDestIter)->first);
295 0 : return sal_False;
296 : }
297 :
298 0 : if ( pTypeInfo.get() )
299 0 : m_pParent->m_vColumnTypes[i] = pTypeInfo->nType;
300 : else
301 0 : m_pParent->m_vColumnTypes[i] = DataType::VARCHAR;
302 0 : }
303 :
304 : }
305 :
306 0 : if ( !xTable.is() )
307 : {
308 0 : OUString sError(ModuleRes(STR_INVALID_TABLE_NAME));
309 0 : m_pParent->showError(sError);
310 0 : return sal_False;
311 : }
312 0 : return sal_True;
313 : }
314 :
315 0 : void OCopyTable::setCreatePrimaryKey( bool _bDoCreate, const OUString& _rSuggestedName )
316 : {
317 0 : bool bCreatePK = m_bPKeyAllowed && _bDoCreate;
318 0 : m_aCB_PrimaryColumn.Check( bCreatePK );
319 0 : m_edKeyName.SetText( _rSuggestedName );
320 :
321 0 : m_aFT_KeyName.Enable( bCreatePK );
322 0 : m_edKeyName.Enable( bCreatePK );
323 0 : }
324 :
325 0 : void OCopyTable::setCreateStyleAction()
326 : {
327 : // reselect the last action before
328 0 : switch(m_pParent->getOperation())
329 : {
330 : case CopyTableOperation::CopyDefinitionAndData:
331 0 : m_aRB_DefData.Check(true);
332 0 : RadioChangeHdl(&m_aRB_DefData);
333 0 : break;
334 : case CopyTableOperation::CopyDefinitionOnly:
335 0 : m_aRB_Def.Check(true);
336 0 : RadioChangeHdl(&m_aRB_Def);
337 0 : break;
338 : case CopyTableOperation::AppendData:
339 0 : m_aRB_AppendData.Check(true);
340 0 : SetAppendDataRadio();
341 0 : break;
342 : case CopyTableOperation::CreateAsView:
343 0 : if ( m_aRB_View.IsEnabled() )
344 : {
345 0 : m_aRB_View.Check(true);
346 0 : RadioChangeHdl(&m_aRB_View);
347 : }
348 : else
349 : {
350 0 : m_aRB_DefData.Check(true);
351 0 : RadioChangeHdl(&m_aRB_DefData);
352 : }
353 : }
354 0 : }
355 :
356 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|