LibreOffice Module dbaccess (master) 1
WCPage.cxx
Go to the documentation of this file.
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
24#include <strings.hrc>
25#include <core_resource.hxx>
26#include <com/sun/star/sdb/CommandType.hpp>
27#include <com/sun/star/sdbcx/XTablesSupplier.hpp>
28#include <com/sun/star/sdb/application/CopyTableOperation.hpp>
31
32using namespace ::dbaui;
33using namespace ::dbtools;
34using namespace ::com::sun::star::uno;
35using namespace ::com::sun::star::beans;
36using namespace ::com::sun::star::container;
37using namespace ::com::sun::star::util;
38using namespace ::com::sun::star::sdb;
39using namespace ::com::sun::star::sdbc;
40using namespace ::com::sun::star::sdbcx;
41
42namespace CopyTableOperation = css::sdb::application::CopyTableOperation;
43
44OCopyTable::OCopyTable(weld::Container* pPage, OCopyTableWizard* pWizard)
45 : OWizardPage(pPage, pWizard, "dbaccess/ui/copytablepage.ui", "CopyTablePage")
46 , m_bPKeyAllowed(false)
47 , m_bUseHeaderAllowed(true)
48 , m_nOldOperation(0)
49 , m_xEdTableName(m_xBuilder->weld_entry("name"))
50 , m_xRB_DefData(m_xBuilder->weld_radio_button("defdata"))
51 , m_xRB_Def(m_xBuilder->weld_radio_button("def"))
52 , m_xRB_View(m_xBuilder->weld_radio_button("view"))
53 , m_xRB_AppendData(m_xBuilder->weld_radio_button("data"))
54 , m_xCB_UseHeaderLine(m_xBuilder->weld_check_button("firstline"))
55 , m_xCB_PrimaryColumn(m_xBuilder->weld_check_button("primarykey"))
56 , m_xFT_KeyName(m_xBuilder->weld_label("keynamelabel"))
57 , m_xEdKeyName(m_xBuilder->weld_entry("keyname"))
58{
59 if ( m_pParent->m_xDestConnection.is() )
60 {
61 if (!m_pParent->supportsViews())
62 m_xRB_View->set_sensitive(false);
63
64 m_xCB_UseHeaderLine->set_active(true);
65 m_bPKeyAllowed = m_pParent->supportsPrimaryKey();
66
67 m_xCB_PrimaryColumn->set_sensitive(m_bPKeyAllowed);
68
69 m_xRB_AppendData->connect_toggled( LINK( this, OCopyTable, RadioChangeHdl ) );
70 m_xRB_DefData->connect_toggled( LINK( this, OCopyTable, RadioChangeHdl ) );
71 m_xRB_Def->connect_toggled( LINK( this, OCopyTable, RadioChangeHdl ) );
72 m_xRB_View->connect_toggled( LINK( this, OCopyTable, RadioChangeHdl ) );
73
74 m_xCB_PrimaryColumn->connect_toggled(LINK( this, OCopyTable, KeyClickHdl ) );
75
76 m_xFT_KeyName->set_sensitive(false);
77 m_xEdKeyName->set_sensitive(false);
78 m_xEdKeyName->set_text(m_pParent->createUniqueName("ID"));
79
80 const sal_Int32 nMaxLen = m_pParent->getMaxColumnNameLength();
81 m_xEdKeyName->set_max_length(nMaxLen);
82 }
83
84 SetPageTitle(DBA_RES(STR_COPYTABLE_TITLE_COPY));
85}
86
88{
89}
90
92{
93 m_pParent->EnableNextButton(true);
94 m_xFT_KeyName->set_sensitive(false);
95 m_xCB_PrimaryColumn->set_sensitive(false);
96 m_xEdKeyName->set_sensitive(false);
97 m_pParent->setOperation(CopyTableOperation::AppendData);
98}
99
100IMPL_LINK(OCopyTable, RadioChangeHdl, weld::Toggleable&, rButton, void)
101{
102 if (!rButton.get_active())
103 return;
104 if (m_xRB_AppendData->get_active())
105 {
106 SetAppendDataRadio();
107 return;
108 }
109 m_pParent->EnableNextButton(!m_xRB_View->get_active());
110 bool bKey = m_bPKeyAllowed && !m_xRB_View->get_active();
111 m_xFT_KeyName->set_sensitive(bKey && m_xCB_PrimaryColumn->get_active());
112 m_xEdKeyName->set_sensitive(bKey && m_xCB_PrimaryColumn->get_active());
113 m_xCB_PrimaryColumn->set_sensitive(bKey);
114 m_xCB_UseHeaderLine->set_sensitive(m_bUseHeaderAllowed && IsOptionDefData());
115
116 // set type what to do
117 if( IsOptionDefData() )
118 m_pParent->setOperation( CopyTableOperation::CopyDefinitionAndData );
119 else if( IsOptionDef() )
120 m_pParent->setOperation( CopyTableOperation::CopyDefinitionOnly );
121 else if( IsOptionView() )
122 m_pParent->setOperation( CopyTableOperation::CreateAsView );
123}
124
125IMPL_LINK_NOARG( OCopyTable, KeyClickHdl, weld::Toggleable&, void )
126{
127 m_xEdKeyName->set_sensitive(m_xCB_PrimaryColumn->get_active());
128 m_xFT_KeyName->set_sensitive(m_xCB_PrimaryColumn->get_active());
129}
130
132{
133 m_pParent->m_bCreatePrimaryKeyColumn = m_bPKeyAllowed && m_xCB_PrimaryColumn->get_sensitive() && m_xCB_PrimaryColumn->get_active();
134 m_pParent->m_aKeyName = m_pParent->m_bCreatePrimaryKeyColumn ? m_xEdKeyName->get_text() : OUString();
135 m_pParent->setUseHeaderLine( m_xCB_UseHeaderLine->get_active() );
136
137 // first check if the table already exists in the database
138 if( m_pParent->getOperation() != CopyTableOperation::AppendData )
139 {
140 m_pParent->clearDestColumns();
141 DynamicTableOrQueryNameCheck aNameCheck( m_pParent->m_xDestConnection, CommandType::TABLE );
142 SQLExceptionInfo aErrorInfo;
143 if ( !aNameCheck.isNameValid( m_xEdTableName->get_text(), aErrorInfo ) )
144 {
145 aErrorInfo.append( SQLExceptionInfo::TYPE::SQLContext, DBA_RES( STR_SUGGEST_APPEND_TABLE_DATA ) );
146 m_pParent->showError(aErrorInfo.get());
147
148 return false;
149 }
150
151 // have to check the length of the table name
152 Reference< XDatabaseMetaData > xMeta = m_pParent->m_xDestConnection->getMetaData();
153 OUString sCatalog;
154 OUString sSchema;
155 OUString sTable;
156 ::dbtools::qualifiedNameComponents( xMeta,
157 m_xEdTableName->get_text(),
158 sCatalog,
159 sSchema,
160 sTable,
161 ::dbtools::EComposeRule::InDataManipulation);
162 sal_Int32 nMaxLength = xMeta->getMaxTableNameLength();
163 if ( nMaxLength && sTable.getLength() > nMaxLength )
164 {
165 m_pParent->showError(DBA_RES(STR_INVALID_TABLE_NAME_LENGTH));
166 return false;
167 }
168
169 // now we have to check if the name of the primary key already exists
170 if ( m_pParent->m_bCreatePrimaryKeyColumn
171 && m_pParent->m_aKeyName != m_pParent->createUniqueName(m_pParent->m_aKeyName) )
172 {
173 m_pParent->showError(DBA_RES(STR_WIZ_NAME_ALREADY_DEFINED) + " " + m_pParent->m_aKeyName);
174 return false;
175 }
176 }
177
178 if (m_xEdTableName->get_value_changed_from_saved())
179 { // table exists and name has changed
180 if ( m_pParent->getOperation() == CopyTableOperation::AppendData )
181 {
182 if(!checkAppendData())
183 return false;
184 }
185 else if ( m_nOldOperation == CopyTableOperation::AppendData )
186 {
187 m_xEdTableName->save_value();
188 return LeavePage();
189 }
190 }
191 else
192 { // table exist and is not new or doesn't exist and so on
193 if ( CopyTableOperation::AppendData == m_pParent->getOperation() )
194 {
195 if( !checkAppendData() )
196 return false;
197 }
198 }
199 m_pParent->m_sName = m_xEdTableName->get_text();
200 m_xEdTableName->save_value();
201
202 if(m_pParent->m_sName.isEmpty())
203 {
204 m_pParent->showError(DBA_RES(STR_INVALID_TABLE_NAME));
205 return false;
206 }
207
208 return true;
209}
210
212{
213 m_pParent->GetOKButton().set_sensitive(true);
214 m_nOldOperation = m_pParent->getOperation();
215 m_xEdTableName->grab_focus();
216 m_xCB_UseHeaderLine->set_active(m_pParent->UseHeaderLine());
217}
218
219OUString OCopyTable::GetTitle() const
220{
221 return DBA_RES(STR_WIZ_TABLE_COPY);
222}
223
225{
226 m_bFirstTime = false;
227
228 m_xEdTableName->set_text( m_pParent->m_sName );
229 m_xEdTableName->save_value();
230}
231
233{
234 m_pParent->clearDestColumns();
236 Reference< XTablesSupplier > xSup( m_pParent->m_xDestConnection, UNO_QUERY );
238 if (xSup.is())
239 xTables = xSup->getTables();
240 if (xTables.is() && xTables->hasByName(m_xEdTableName->get_text()))
241 {
242 const ODatabaseExport::TColumnVector& rSrcColumns = m_pParent->getSrcVector();
243 const sal_uInt32 nSrcSize = rSrcColumns.size();
244 m_pParent->m_vColumnPositions.resize( nSrcSize, ODatabaseExport::TPositions::value_type( COLUMN_POSITION_NOT_FOUND, COLUMN_POSITION_NOT_FOUND ) );
245 m_pParent->m_vColumnTypes.resize( nSrcSize , COLUMN_POSITION_NOT_FOUND );
246
247 // set new destination
248 xTables->getByName( m_xEdTableName->get_text() ) >>= xTable;
249 ObjectCopySource aTableCopySource( m_pParent->m_xDestConnection, xTable );
250 m_pParent->loadData( aTableCopySource, m_pParent->m_vDestColumns, m_pParent->m_aDestVec );
251 const ODatabaseExport::TColumnVector& rDestColumns = m_pParent->getDestVector();
252 const sal_uInt32 nMinSrcDestSize = std::min<sal_uInt32>(nSrcSize, rDestColumns.size());
253 sal_uInt32 i = 0;
254 for (auto const& column : rDestColumns)
255 {
256 if (i >= nMinSrcDestSize)
257 break;
258 bool bNotConvert = true;
259 m_pParent->m_vColumnPositions[i] = ODatabaseExport::TPositions::value_type(i+1,i+1);
260 TOTypeInfoSP pTypeInfo = m_pParent->convertType(column->second->getSpecialTypeInfo(),bNotConvert);
261 if ( !bNotConvert )
262 {
263 m_pParent->showColumnTypeNotSupported(column->first);
264 return false;
265 }
266
267 if ( pTypeInfo )
268 m_pParent->m_vColumnTypes[i] = pTypeInfo->nType;
269 else
270 m_pParent->m_vColumnTypes[i] = DataType::VARCHAR;
271 ++i;
272 }
273
274 }
275
276 if ( !xTable.is() )
277 {
278 m_pParent->showError(DBA_RES(STR_INVALID_TABLE_NAME));
279 return false;
280 }
281 return true;
282}
283
284void OCopyTable::setCreatePrimaryKey( bool _bDoCreate, const OUString& _rSuggestedName )
285{
286 bool bCreatePK = m_bPKeyAllowed && _bDoCreate;
287 m_xCB_PrimaryColumn->set_active( bCreatePK );
288 m_xEdKeyName->set_text( _rSuggestedName );
289
290 m_xFT_KeyName->set_sensitive( bCreatePK );
291 m_xEdKeyName->set_sensitive( bCreatePK );
292}
293
295{
296 // reselect the last action before
297 switch (m_pParent->getOperation())
298 {
299 case CopyTableOperation::CopyDefinitionAndData:
300 m_xRB_DefData->set_active(true);
301 RadioChangeHdl(*m_xRB_DefData);
302 break;
303 case CopyTableOperation::CopyDefinitionOnly:
304 m_xRB_Def->set_active(true);
305 RadioChangeHdl(*m_xRB_Def);
306 break;
307 case CopyTableOperation::AppendData:
308 m_xRB_AppendData->set_active(true);
310 break;
311 case CopyTableOperation::CreateAsView:
312 if (m_xRB_View->get_sensitive())
313 {
314 m_xRB_View->set_active(true);
315 RadioChangeHdl(*m_xRB_View);
316 }
317 else
318 {
319 m_xRB_DefData->set_active(true);
320 RadioChangeHdl(*m_xRB_DefData);
321 }
322 }
323}
324
325/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define COLUMN_POSITION_NOT_FOUND
Definition: DExport.hxx:48
OptionalString sSchema
OptionalString sCatalog
void SetPageTitle(const OUString &rPageTitle)
class implementing the IObjectNameCheck interface, and checking a given name for being valid as eithe...
virtual bool isNameValid(const OUString &_rObjectName, ::dbtools::SQLExceptionInfo &_out_rErrorToDisplay) const override
determines whether a given object name is valid
std::unique_ptr< weld::RadioButton > m_xRB_AppendData
Definition: WCPage.hxx:37
virtual ~OCopyTable() override
Definition: WCPage.cxx:87
sal_Int16 m_nOldOperation
Definition: WCPage.hxx:31
std::unique_ptr< weld::RadioButton > m_xRB_View
Definition: WCPage.hxx:36
virtual bool LeavePage() override
Definition: WCPage.cxx:131
std::unique_ptr< weld::Entry > m_xEdTableName
Definition: WCPage.hxx:33
bool m_bPKeyAllowed
Definition: WCPage.hxx:29
std::unique_ptr< weld::CheckButton > m_xCB_UseHeaderLine
Definition: WCPage.hxx:38
void setCreateStyleAction()
Definition: WCPage.cxx:294
bool checkAppendData()
Definition: WCPage.cxx:232
void SetAppendDataRadio()
Definition: WCPage.cxx:91
std::unique_ptr< weld::Label > m_xFT_KeyName
Definition: WCPage.hxx:40
virtual void Activate() override
Definition: WCPage.cxx:211
std::unique_ptr< weld::RadioButton > m_xRB_Def
Definition: WCPage.hxx:35
virtual void Reset() override
Definition: WCPage.cxx:224
void setCreatePrimaryKey(bool _bDoCreate, const OUString &_rSuggestedName)
Definition: WCPage.cxx:284
std::unique_ptr< weld::RadioButton > m_xRB_DefData
Definition: WCPage.hxx:34
std::unique_ptr< weld::Entry > m_xEdKeyName
Definition: WCPage.hxx:41
std::unique_ptr< weld::CheckButton > m_xCB_PrimaryColumn
Definition: WCPage.hxx:39
virtual OUString GetTitle() const override
Definition: WCPage.cxx:219
std::vector< TColumns::const_iterator > TColumnVector
Definition: DExport.hxx:58
const css::uno::Any & get() const
void append(TYPE _eType, const OUString &_rErrorMessage, const OUString &_rSQLState=OUString(), const sal_Int32 _nErrorCode=0)
#define DBA_RES(id)
IMPL_LINK_NOARG(OApplicationController, OnClipboardChanged, TransferableDataHelper *, void)
IMPL_LINK(OApplicationController, OnSelectContainer, void *, _pType, void)
std::shared_ptr< OTypeInfo > TOTypeInfoSP
Definition: TypeInfo.hxx:99
int i