connectivity/source/commontools/dbtools.cxx | 9 ++ dbaccess/source/ui/dlg/dlgsave.cxx | 87 +++++++++++++++++++--------- include/connectivity/dbtools.hxx | 15 ++++ 3 files changed, 85 insertions(+), 26 deletions(-)
New commits: commit 3081701aa862c9001b4c0f240c562657c2cbd46b Author: prrvchr <[email protected]> AuthorDate: Wed Aug 27 15:55:26 2025 +0200 Commit: Noel Grandin <[email protected]> CommitDate: Sun Sep 28 13:53:44 2025 +0200 tdf#156776 Base need to save new table with unique name In Base if you create a new table with: - Insert -> Table Design... when saving this new table with: - File -> Save As If the underlying database supports catalogs and/or schemas, it will always be proposed as a table name: Table1 It is necessary to propose a table name that does not yet exist in the pre-selected catalog and/or schema. Change-Id: I4ca8cb8013f34e1c92211040c7ab0558d94c4243 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/190289 Tested-by: Jenkins Reviewed-by: Noel Grandin <[email protected]> diff --git a/connectivity/source/commontools/dbtools.cxx b/connectivity/source/commontools/dbtools.cxx index 28d0158aee71..63c6ac25257f 100644 --- a/connectivity/source/commontools/dbtools.cxx +++ b/connectivity/source/commontools/dbtools.cxx @@ -1405,6 +1405,15 @@ OUString createUniqueName(const Reference<XNameAccess>& _rxContainer,const OUStr return createUniqueName( aElementNames, _rBaseName, _bStartWithNumber ); } +OUString createUniqueName(const Reference<XTablesSupplier>& _rxSupplier,const OUString& _rBaseName, bool _bStartWithNumber) +{ + Reference< XNameAccess > xContainer; + OSL_ENSURE( _rxSupplier.is(), "createUniqueName: invalid connection!" ); + if ( _rxSupplier.is() ) + xContainer = _rxSupplier->getTables(); + return createUniqueName( xContainer, _rBaseName, _bStartWithNumber ); +} + void showError(const SQLExceptionInfo& _rInfo, const Reference< XWindow>& _xParent, const Reference< XComponentContext >& _rxContext) diff --git a/dbaccess/source/ui/dlg/dlgsave.cxx b/dbaccess/source/ui/dlg/dlgsave.cxx index b07eb152ec11..66fbd7d4deb4 100644 --- a/dbaccess/source/ui/dlg/dlgsave.cxx +++ b/dbaccess/source/ui/dlg/dlgsave.cxx @@ -22,6 +22,7 @@ #include <strings.hrc> #include <com/sun/star/sdb/CommandType.hpp> #include <com/sun/star/sdbc/XRow.hpp> +#include <com/sun/star/sdbcx/XTablesSupplier.hpp> #include <connectivity/dbtools.hxx> #include <UITools.hxx> #include <objectnamecheck.hxx> @@ -29,9 +30,11 @@ using namespace dbaui; using namespace dbtools; +using namespace ::com::sun::star::container; using namespace ::com::sun::star::uno; using namespace ::com::sun::star::sdb; using namespace ::com::sun::star::sdbc; +using namespace ::com::sun::star::sdbcx; IMPL_LINK(OSaveAsDlg, TextFilterHdl, OUString&, rTest, bool) @@ -114,48 +117,80 @@ OSaveAsDlg::OSaveAsDlg( weld::Window * pParent, break; case CommandType::TABLE: - OSL_ENSURE( m_xMetaData.is(), "OSaveAsDlg::OSaveAsDlg: no meta data for entering table names: this will crash!" ); + assert(m_xMetaData.is() && "OSaveAsDlg::OSaveAsDlg: no meta data for entering table names: this will crash!"); { m_xLabel->set_label(DBA_RES(STR_TBL_LABEL)); - if(m_xMetaData.is() && !m_xMetaData->supportsCatalogsInTableDefinitions()) { + OUString sCatalog, sSchema, sTable; + EComposeRule eComposeRule = ::dbtools::EComposeRule::InDataManipulation; + if (!m_xMetaData->supportsCatalogsInTableDefinitions()) { m_xCatalogLbl->hide(); m_xCatalog->hide(); } else { + sCatalog = _xConnection->getCatalog(); // now fill the catalogs lcl_fillComboList( *m_xCatalog, _xConnection, - &XDatabaseMetaData::getCatalogs, _xConnection->getCatalog() ); + &XDatabaseMetaData::getCatalogs, sCatalog ); } - if ( !m_xMetaData->supportsSchemasInTableDefinitions()) { + if (!m_xMetaData->supportsSchemasInTableDefinitions()) { m_xSchemaLbl->hide(); m_xSchema->hide(); } else { + Reference<XResultSet> xRes = m_xMetaData->getSchemas(); + if (xRes.is()) + { + Reference<XRow> xRow(xRes,UNO_QUERY); + while (xRes->next()) + { + sSchema = xRow->getString(1); + if (!xRow->wasNull()) + break; + } + Reference<XCloseable> xCloseable(xRes, UNO_QUERY); + if (xCloseable.is()) + xCloseable->close(); + } + if (sSchema.isEmpty()) + sSchema = m_xMetaData->getUserName(); + + // now fill the schemas lcl_fillComboList( *m_xSchema, _xConnection, - &XDatabaseMetaData::getSchemas, m_xMetaData->getUserName() ); + &XDatabaseMetaData::getSchemas, sSchema ); } - OSL_ENSURE(m_xMetaData.is(),"The metadata can not be null!"); - if(m_aName.indexOf('.') != -1) { - OUString sCatalog,sSchema,sTable; - ::dbtools::qualifiedNameComponents(m_xMetaData, - m_aName, - sCatalog, - sSchema, - sTable, - ::dbtools::EComposeRule::InDataManipulation); - - int nPos = m_xCatalog->find_text(sCatalog); - if (nPos != -1) - m_xCatalog->set_active(nPos); - - if ( !sSchema.isEmpty() ) { - nPos = m_xSchema->find_text(sSchema); - if (nPos != -1) - m_xSchema->set_active(nPos); + Reference< XTablesSupplier > xSupplier(_xConnection, UNO_QUERY); + if ( xSupplier.is() ) + { + // get the unique table name + OUString sTableName = ::dbtools::composeTableName(m_xMetaData, sCatalog, sSchema, "Table", false, eComposeRule); + // we create a unique full table name + OUString sUniqueName = ::dbtools::createUniqueName(xSupplier, sTableName, true); + // we extract just the table name in sTable + ::dbtools::qualifiedNameComponents(m_xMetaData, sUniqueName, sCatalog, sSchema, sTable, eComposeRule); + } else { + // fall back to the previous implementation + if (m_aName.indexOf('.') != -1) + { + ::dbtools::qualifiedNameComponents(m_xMetaData, m_aName, sCatalog, sSchema, sTable, eComposeRule); + int nPos; + + if ( !sCatalog.isEmpty() ) { + nPos = m_xCatalog->find_text(sCatalog); + if (nPos != -1) + m_xCatalog->set_active(nPos); + } + + if ( !sSchema.isEmpty() ) { + nPos = m_xSchema->find_text(sSchema); + if (nPos != -1) + m_xSchema->set_active(nPos); + } + } else { + sTable = m_aName; } - m_xTitle->set_text(sTable); - } else - m_xTitle->set_text(m_aName); + } + + m_xTitle->set_text(sTable); m_xTitle->select_region(0, -1); sal_Int32 nLength = m_xMetaData.is() ? m_xMetaData->getMaxTableNameLength() : 0; diff --git a/include/connectivity/dbtools.hxx b/include/connectivity/dbtools.hxx index 6fbb0eb0c4d0..1c9e641c897e 100644 --- a/include/connectivity/dbtools.hxx +++ b/include/connectivity/dbtools.hxx @@ -484,6 +484,21 @@ namespace dbtools OOO_DLLPUBLIC_DBTOOLS css::uno::Reference< css::sdbc::XDataSource> getDataSource(const OUString& _rsDataSourceName, const css::uno::Reference< css::uno::XComponentContext>& _rxContext); + /** search for a name that is NOT in the NameAcces + @param _rxSupplier + the connection's XTablesSupplier to search in + @param _rBaseName + the base name that should be used to create the new name + @param _bStartWithNumber + When <TRUE/> the name ends with number even when the name itself doesn't occur in the collection. + @return + A name which doesn't exist in the collection. + */ + OOO_DLLPUBLIC_DBTOOLS + OUString createUniqueName(const css::uno::Reference< css::sdbcx::XTablesSupplier>& _rxSupplier, + const OUString& _rBaseName, + bool _bStartWithNumber = true); + /** search for a name that is NOT in the NameAcces @param _rxContainer the NameAccess container to search in
