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>
23 #include <defaultobjectnamecheck.hxx>
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>
29 #include <connectivity/dbexception.hxx>
30 #include <connectivity/dbtools.hxx>
32 using namespace ::dbaui
;
33 using namespace ::dbtools
;
34 using namespace ::com::sun::star::uno
;
35 using namespace ::com::sun::star::beans
;
36 using namespace ::com::sun::star::container
;
37 using namespace ::com::sun::star::util
;
38 using namespace ::com::sun::star::sdb
;
39 using namespace ::com::sun::star::sdbc
;
40 using namespace ::com::sun::star::sdbcx
;
42 namespace CopyTableOperation
= css::sdb::application::CopyTableOperation
;
44 OCopyTable::OCopyTable(weld::Container
* pPage
, OCopyTableWizard
* pWizard
)
45 : OWizardPage(pPage
, pWizard
, "dbaccess/ui/copytablepage.ui", "CopyTablePage")
46 , m_bPKeyAllowed(false)
47 , m_bUseHeaderAllowed(true)
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"))
59 if ( m_pParent
->m_xDestConnection
.is() )
61 if (!m_pParent
->supportsViews())
62 m_xRB_View
->set_sensitive(false);
64 m_xCB_UseHeaderLine
->set_active(true);
65 m_bPKeyAllowed
= m_pParent
->supportsPrimaryKey();
67 m_xCB_PrimaryColumn
->set_sensitive(m_bPKeyAllowed
);
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
) );
74 m_xCB_PrimaryColumn
->connect_toggled(LINK( this, OCopyTable
, KeyClickHdl
) );
76 m_xFT_KeyName
->set_sensitive(false);
77 m_xEdKeyName
->set_sensitive(false);
78 m_xEdKeyName
->set_text(m_pParent
->createUniqueName("ID"));
80 const sal_Int32 nMaxLen
= m_pParent
->getMaxColumnNameLength();
81 m_xEdKeyName
->set_max_length(nMaxLen
);
84 SetPageTitle(DBA_RES(STR_COPYTABLE_TITLE_COPY
));
87 OCopyTable::~OCopyTable()
91 void OCopyTable::SetAppendDataRadio()
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
);
100 IMPL_LINK(OCopyTable
, RadioChangeHdl
, weld::Toggleable
&, rButton
, void)
102 if (!rButton
.get_active())
104 if (m_xRB_AppendData
->get_active())
106 SetAppendDataRadio();
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());
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
);
125 IMPL_LINK_NOARG( OCopyTable
, KeyClickHdl
, weld::Toggleable
&, void )
127 m_xEdKeyName
->set_sensitive(m_xCB_PrimaryColumn
->get_active());
128 m_xFT_KeyName
->set_sensitive(m_xCB_PrimaryColumn
->get_active());
131 bool OCopyTable::LeavePage()
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() );
137 // first check if the table already exists in the database
138 if( m_pParent
->getOperation() != CopyTableOperation::AppendData
)
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
) )
145 aErrorInfo
.append( SQLExceptionInfo::TYPE::SQLContext
, DBA_RES( STR_SUGGEST_APPEND_TABLE_DATA
) );
146 m_pParent
->showError(aErrorInfo
.get());
151 // have to check the length of the table name
152 Reference
< XDatabaseMetaData
> xMeta
= m_pParent
->m_xDestConnection
->getMetaData();
156 ::dbtools::qualifiedNameComponents( xMeta
,
157 m_xEdTableName
->get_text(),
161 ::dbtools::EComposeRule::InDataManipulation
);
162 sal_Int32 nMaxLength
= xMeta
->getMaxTableNameLength();
163 if ( nMaxLength
&& sTable
.getLength() > nMaxLength
)
165 m_pParent
->showError(DBA_RES(STR_INVALID_TABLE_NAME_LENGTH
));
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
) )
173 m_pParent
->showError(DBA_RES(STR_WIZ_NAME_ALREADY_DEFINED
) + " " + m_pParent
->m_aKeyName
);
178 if (m_xEdTableName
->get_value_changed_from_saved())
179 { // table exists and name has changed
180 if ( m_pParent
->getOperation() == CopyTableOperation::AppendData
)
182 if(!checkAppendData())
185 else if ( m_nOldOperation
== CopyTableOperation::AppendData
)
187 m_xEdTableName
->save_value();
192 { // table exist and is not new or doesn't exist and so on
193 if ( CopyTableOperation::AppendData
== m_pParent
->getOperation() )
195 if( !checkAppendData() )
199 m_pParent
->m_sName
= m_xEdTableName
->get_text();
200 m_xEdTableName
->save_value();
202 if(m_pParent
->m_sName
.isEmpty())
204 m_pParent
->showError(DBA_RES(STR_INVALID_TABLE_NAME
));
211 void OCopyTable::Activate()
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());
219 OUString
OCopyTable::GetTitle() const
221 return DBA_RES(STR_WIZ_TABLE_COPY
);
224 void OCopyTable::Reset()
226 m_bFirstTime
= false;
228 m_xEdTableName
->set_text( m_pParent
->m_sName
);
229 m_xEdTableName
->save_value();
232 bool OCopyTable::checkAppendData()
234 m_pParent
->clearDestColumns();
235 Reference
< XPropertySet
> xTable
;
236 Reference
< XTablesSupplier
> xSup( m_pParent
->m_xDestConnection
, UNO_QUERY
);
237 Reference
<XNameAccess
> xTables
;
239 xTables
= xSup
->getTables();
240 if (xTables
.is() && xTables
->hasByName(m_xEdTableName
->get_text()))
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
);
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());
254 for (auto const& column
: rDestColumns
)
256 if (i
>= nMinSrcDestSize
)
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
);
263 m_pParent
->showColumnTypeNotSupported(column
->first
);
268 m_pParent
->m_vColumnTypes
[i
] = pTypeInfo
->nType
;
270 m_pParent
->m_vColumnTypes
[i
] = DataType::VARCHAR
;
278 m_pParent
->showError(DBA_RES(STR_INVALID_TABLE_NAME
));
284 void OCopyTable::setCreatePrimaryKey( bool _bDoCreate
, const OUString
& _rSuggestedName
)
286 bool bCreatePK
= m_bPKeyAllowed
&& _bDoCreate
;
287 m_xCB_PrimaryColumn
->set_active( bCreatePK
);
288 m_xEdKeyName
->set_text( _rSuggestedName
);
290 m_xFT_KeyName
->set_sensitive( bCreatePK
);
291 m_xEdKeyName
->set_sensitive( bCreatePK
);
294 void OCopyTable::setCreateStyleAction()
296 // reselect the last action before
297 switch (m_pParent
->getOperation())
299 case CopyTableOperation::CopyDefinitionAndData
:
300 m_xRB_DefData
->set_active(true);
301 RadioChangeHdl(*m_xRB_DefData
);
303 case CopyTableOperation::CopyDefinitionOnly
:
304 m_xRB_Def
->set_active(true);
305 RadioChangeHdl(*m_xRB_Def
);
307 case CopyTableOperation::AppendData
:
308 m_xRB_AppendData
->set_active(true);
309 SetAppendDataRadio();
311 case CopyTableOperation::CreateAsView
:
312 if (m_xRB_View
->get_sensitive())
314 m_xRB_View
->set_active(true);
315 RadioChangeHdl(*m_xRB_View
);
319 m_xRB_DefData
->set_active(true);
320 RadioChangeHdl(*m_xRB_DefData
);
325 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */