1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "WCopyTable.hxx"
22 #include "WColumnSelect.hxx"
23 #include "WExtendPages.hxx"
25 #include "defaultobjectnamecheck.hxx"
26 #include <tools/debug.hxx>
27 #include "dbaccess_helpid.hrc"
28 #include "dbu_misc.hrc"
29 #include <com/sun/star/sdbc/XResultSet.hpp>
30 #include <com/sun/star/sdb/CommandType.hpp>
31 #include <com/sun/star/sdbc/XRow.hpp>
32 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
33 #include <com/sun/star/sdbcx/XViewsSupplier.hpp>
34 #include <com/sun/star/sdb/application/CopyTableOperation.hpp>
35 #include <vcl/msgbox.hxx>
36 #include <connectivity/dbexception.hxx>
37 #include <connectivity/dbtools.hxx>
38 #include "UITools.hxx"
39 #include "moduledbu.hxx"
40 #include <cppuhelper/exc_hlp.hxx>
42 using namespace ::dbaui
;
43 using namespace ::dbtools
;
44 using namespace ::com::sun::star::uno
;
45 using namespace ::com::sun::star::beans
;
46 using namespace ::com::sun::star::container
;
47 using namespace ::com::sun::star::util
;
48 using namespace ::com::sun::star::sdb
;
49 using namespace ::com::sun::star::sdbc
;
50 using namespace ::com::sun::star::sdbcx
;
52 namespace CopyTableOperation
= ::com::sun::star::sdb::application::CopyTableOperation
;
55 OCopyTable::OCopyTable(vcl::Window
* pParent
)
56 : OWizardPage(pParent
, "CopyTablePage", "dbaccess/ui/copytablepage.ui")
60 , m_bPKeyAllowed(false)
61 , m_bUseHeaderAllowed(true)
63 get(m_pEdTableName
, "name");
64 get(m_pRB_DefData
, "defdata");
65 get(m_pRB_Def
, "def");
66 get(m_pRB_View
, "view");
67 get(m_pRB_AppendData
, "data");
68 get(m_pCB_UseHeaderLine
, "firstline");
69 get(m_pCB_PrimaryColumn
, "primarykey");
70 get(m_pFT_KeyName
, "keynamelabel");
71 get(m_pEdKeyName
, "keyname");
73 m_pEdTableName
->SetMaxTextLen(EDIT_NOLIMIT
);
75 if ( m_pParent
->m_xDestConnection
.is() )
77 if ( !m_pParent
->supportsViews() )
78 m_pRB_View
->Disable();
80 m_pCB_UseHeaderLine
->Check(true);
81 m_bPKeyAllowed
= m_pParent
->supportsPrimaryKey();
83 m_pCB_PrimaryColumn
->Enable(m_bPKeyAllowed
);
85 m_pRB_AppendData
->SetClickHdl( LINK( this, OCopyTable
, AppendDataClickHdl
) );
87 m_pRB_DefData
->SetClickHdl( LINK( this, OCopyTable
, RadioChangeHdl
) );
88 m_pRB_Def
->SetClickHdl( LINK( this, OCopyTable
, RadioChangeHdl
) );
89 m_pRB_View
->SetClickHdl( LINK( this, OCopyTable
, RadioChangeHdl
) );
91 m_pCB_PrimaryColumn
->SetClickHdl(LINK( this, OCopyTable
, KeyClickHdl
) );
93 m_pFT_KeyName
->Enable(false);
94 m_pEdKeyName
->Enable(false);
95 OUString
sKeyName("ID");
96 sKeyName
= m_pParent
->createUniqueName(sKeyName
);
97 m_pEdKeyName
->SetText(sKeyName
);
99 sal_Int32 nMaxLen
= m_pParent
->getMaxColumnNameLength();
100 m_pEdKeyName
->SetMaxTextLen(nMaxLen
? nMaxLen
: EDIT_NOLIMIT
);
103 SetText(ModuleRes(STR_COPYTABLE_TITLE_COPY
));
106 OCopyTable::~OCopyTable()
111 void OCopyTable::dispose()
113 m_pEdTableName
.clear();
114 m_pRB_DefData
.clear();
117 m_pRB_AppendData
.clear();
118 m_pCB_UseHeaderLine
.clear();
119 m_pCB_PrimaryColumn
.clear();
120 m_pFT_KeyName
.clear();
121 m_pEdKeyName
.clear();
124 OWizardPage::dispose();
127 IMPL_LINK( OCopyTable
, AppendDataClickHdl
, Button
*, /*pButton*/ )
130 SetAppendDataRadio();
134 void OCopyTable::SetAppendDataRadio()
136 m_pParent
->EnableButton(OCopyTableWizard::WIZARD_NEXT
,true);
137 m_pFT_KeyName
->Enable(false);
138 m_pCB_PrimaryColumn
->Enable(false);
139 m_pEdKeyName
->Enable(false);
140 m_pParent
->setOperation(CopyTableOperation::AppendData
);
143 IMPL_LINK( OCopyTable
, RadioChangeHdl
, Button
*, pButton
)
145 m_pParent
->EnableButton(OCopyTableWizard::WIZARD_NEXT
,pButton
!= m_pRB_View
);
146 bool bKey
= m_bPKeyAllowed
&& pButton
!= m_pRB_View
;
147 m_pFT_KeyName
->Enable(bKey
&& m_pCB_PrimaryColumn
->IsChecked());
148 m_pEdKeyName
->Enable(bKey
&& m_pCB_PrimaryColumn
->IsChecked());
149 m_pCB_PrimaryColumn
->Enable(bKey
);
150 m_pCB_UseHeaderLine
->Enable(m_bUseHeaderAllowed
&& IsOptionDefData());
152 // set typ what to do
153 if( IsOptionDefData() )
154 m_pParent
->setOperation( CopyTableOperation::CopyDefinitionAndData
);
155 else if( IsOptionDef() )
156 m_pParent
->setOperation( CopyTableOperation::CopyDefinitionOnly
);
157 else if( IsOptionView() )
158 m_pParent
->setOperation( CopyTableOperation::CreateAsView
);
163 IMPL_LINK( OCopyTable
, KeyClickHdl
, Button
*, /*pButton*/ )
165 m_pEdKeyName
->Enable(m_pCB_PrimaryColumn
->IsChecked());
166 m_pFT_KeyName
->Enable(m_pCB_PrimaryColumn
->IsChecked());
170 bool OCopyTable::LeavePage()
172 m_pParent
->m_bCreatePrimaryKeyColumn
= m_bPKeyAllowed
&& m_pCB_PrimaryColumn
->IsEnabled() && m_pCB_PrimaryColumn
->IsChecked();
173 m_pParent
->m_aKeyName
= m_pParent
->m_bCreatePrimaryKeyColumn
? m_pEdKeyName
->GetText() : OUString();
174 m_pParent
->setUseHeaderLine( m_pCB_UseHeaderLine
->IsChecked() );
176 // first check if the table already exists in the database
177 if( m_pParent
->getOperation() != CopyTableOperation::AppendData
)
179 m_pParent
->clearDestColumns();
180 DynamicTableOrQueryNameCheck
aNameCheck( m_pParent
->m_xDestConnection
, CommandType::TABLE
);
181 SQLExceptionInfo aErrorInfo
;
182 if ( !aNameCheck
.isNameValid( m_pEdTableName
->GetText(), aErrorInfo
) )
184 aErrorInfo
.append( SQLExceptionInfo::SQL_CONTEXT
, ModuleRes( STR_SUGGEST_APPEND_TABLE_DATA
) );
185 m_pParent
->showError(aErrorInfo
.get());
190 // have to check the length of the table name
191 Reference
< XDatabaseMetaData
> xMeta
= m_pParent
->m_xDestConnection
->getMetaData();
195 ::dbtools::qualifiedNameComponents( xMeta
,
196 m_pEdTableName
->GetText(),
200 ::dbtools::eInDataManipulation
);
201 sal_Int32 nMaxLength
= xMeta
->getMaxTableNameLength();
202 if ( nMaxLength
&& sTable
.getLength() > nMaxLength
)
204 OUString
sError(ModuleRes(STR_INVALID_TABLE_NAME_LENGTH
));
205 m_pParent
->showError(sError
);
209 // now we have to check if the name of the primary key already exists
210 if ( m_pParent
->m_bCreatePrimaryKeyColumn
211 && m_pParent
->m_aKeyName
!= m_pParent
->createUniqueName(m_pParent
->m_aKeyName
) )
213 OUString
aInfoString( ModuleRes(STR_WIZ_PKEY_ALREADY_DEFINED
) );
215 aInfoString
+= m_pParent
->m_aKeyName
;
216 m_pParent
->showError(aInfoString
);
221 if ( m_pEdTableName
->IsValueChangedFromSaved() )
222 { // table exists and name has changed
223 if ( m_pParent
->getOperation() == CopyTableOperation::AppendData
)
225 if(!checkAppendData())
228 else if ( m_nOldOperation
== CopyTableOperation::AppendData
)
230 m_pEdTableName
->SaveValue();
235 { // table exist and is not new or doesn't exist and so on
236 if ( CopyTableOperation::AppendData
== m_pParent
->getOperation() )
238 if( !checkAppendData() )
242 m_pParent
->m_sName
= m_pEdTableName
->GetText();
243 m_pEdTableName
->SaveValue();
245 if(m_pParent
->m_sName
.isEmpty())
247 OUString
sError(ModuleRes(STR_INVALID_TABLE_NAME
));
248 m_pParent
->showError(sError
);
255 void OCopyTable::ActivatePage()
257 m_pParent
->GetOKButton().Enable( true );
258 m_nOldOperation
= m_pParent
->getOperation();
259 m_pEdTableName
->GrabFocus();
260 m_pCB_UseHeaderLine
->Check(m_pParent
->UseHeaderLine());
263 OUString
OCopyTable::GetTitle() const
265 return ModuleRes(STR_WIZ_TABLE_COPY
);
268 void OCopyTable::Reset()
270 m_bFirstTime
= false;
272 m_pEdTableName
->SetText( m_pParent
->m_sName
);
273 m_pEdTableName
->SaveValue();
276 bool OCopyTable::checkAppendData()
278 m_pParent
->clearDestColumns();
279 Reference
< XPropertySet
> xTable
;
280 Reference
< XTablesSupplier
> xSup( m_pParent
->m_xDestConnection
, UNO_QUERY
);
281 Reference
<XNameAccess
> xTables
;
283 xTables
= xSup
->getTables();
284 if(xTables
.is() && xTables
->hasByName(m_pEdTableName
->GetText()))
286 const ODatabaseExport::TColumnVector
& rSrcColumns
= m_pParent
->getSrcVector();
287 const sal_uInt32 nSrcSize
= rSrcColumns
.size();
288 m_pParent
->m_vColumnPos
.resize( nSrcSize
, ODatabaseExport::TPositions::value_type( COLUMN_POSITION_NOT_FOUND
, COLUMN_POSITION_NOT_FOUND
) );
289 m_pParent
->m_vColumnTypes
.resize( nSrcSize
, COLUMN_POSITION_NOT_FOUND
);
291 // set new destination
292 xTables
->getByName( m_pEdTableName
->GetText() ) >>= xTable
;
293 ObjectCopySource
aTableCopySource( m_pParent
->m_xDestConnection
, xTable
);
294 m_pParent
->loadData( aTableCopySource
, m_pParent
->m_vDestColumns
, m_pParent
->m_aDestVec
);
295 const ODatabaseExport::TColumnVector
& rDestColumns
= m_pParent
->getDestVector();
296 ODatabaseExport::TColumnVector::const_iterator aDestIter
= rDestColumns
.begin();
297 ODatabaseExport::TColumnVector::const_iterator aDestEnd
= rDestColumns
.end();
298 const sal_uInt32 nDestSize
= rDestColumns
.size();
300 for(sal_Int32 nPos
= 1;aDestIter
!= aDestEnd
&& i
< nDestSize
&& i
< nSrcSize
;++aDestIter
,++nPos
,++i
)
302 bool bNotConvert
= true;
303 m_pParent
->m_vColumnPos
[i
] = ODatabaseExport::TPositions::value_type(nPos
,nPos
);
304 TOTypeInfoSP pTypeInfo
= m_pParent
->convertType((*aDestIter
)->second
->getSpecialTypeInfo(),bNotConvert
);
307 m_pParent
->showColumnTypeNotSupported((*aDestIter
)->first
);
311 if ( pTypeInfo
.get() )
312 m_pParent
->m_vColumnTypes
[i
] = pTypeInfo
->nType
;
314 m_pParent
->m_vColumnTypes
[i
] = DataType::VARCHAR
;
321 OUString
sError(ModuleRes(STR_INVALID_TABLE_NAME
));
322 m_pParent
->showError(sError
);
328 void OCopyTable::setCreatePrimaryKey( bool _bDoCreate
, const OUString
& _rSuggestedName
)
330 bool bCreatePK
= m_bPKeyAllowed
&& _bDoCreate
;
331 m_pCB_PrimaryColumn
->Check( bCreatePK
);
332 m_pEdKeyName
->SetText( _rSuggestedName
);
334 m_pFT_KeyName
->Enable( bCreatePK
);
335 m_pEdKeyName
->Enable( bCreatePK
);
338 void OCopyTable::setCreateStyleAction()
340 // reselect the last action before
341 switch(m_pParent
->getOperation())
343 case CopyTableOperation::CopyDefinitionAndData
:
344 m_pRB_DefData
->Check(true);
345 RadioChangeHdl(m_pRB_DefData
);
347 case CopyTableOperation::CopyDefinitionOnly
:
348 m_pRB_Def
->Check(true);
349 RadioChangeHdl(m_pRB_Def
);
351 case CopyTableOperation::AppendData
:
352 m_pRB_AppendData
->Check(true);
353 SetAppendDataRadio();
355 case CopyTableOperation::CreateAsView
:
356 if ( m_pRB_View
->IsEnabled() )
358 m_pRB_View
->Check(true);
359 RadioChangeHdl(m_pRB_View
);
363 m_pRB_DefData
->Check(true);
364 RadioChangeHdl(m_pRB_DefData
);
369 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */