diff options
author | Oleksii Makhotin <alex@bitprox.com> | 2021-04-06 16:03:41 +0300 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2021-04-17 14:26:23 +0200 |
commit | 2a8e120db1c3175ff937cdbe6d0ade23dc380c01 (patch) | |
tree | 58c9d21d1f330791c614b96b0cd04aad6296cd27 | |
parent | b7d8ee083230964de2e1580c4639ee4cd307207f (diff) |
tdf#119962 Fix autoincrement for copied table
Change-Id: Ic916288c4177734c93c423ac3823993ca95a941d
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/113668
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
-rw-r--r-- | dbaccess/source/ui/uno/copytablewizard.cxx | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/dbaccess/source/ui/uno/copytablewizard.cxx b/dbaccess/source/ui/uno/copytablewizard.cxx index d128edacb371..50fa22f6dad4 100644 --- a/dbaccess/source/ui/uno/copytablewizard.cxx +++ b/dbaccess/source/ui/uno/copytablewizard.cxx @@ -1354,6 +1354,47 @@ void CopyTableWizard::impl_doCopy_nothrow() if ( xSourceResultSet.is() ) impl_copyRows_throw( xSourceResultSet, xTable ); + + // tdf#119962 + const Reference< XDatabaseMetaData > xDestMetaData( m_xDestConnection->getMetaData(), UNO_SET_THROW ); + const OUString sComposedTableName = ::dbtools::composeTableName( xDestMetaData, xTable, ::dbtools::EComposeRule::InDataManipulation, true ); + + OUString aSchema,aTable; + xTable->getPropertyValue("SchemaName") >>= aSchema; + xTable->getPropertyValue("Name") >>= aTable; + Any aCatalog = xTable->getPropertyValue("CatalogName"); + + const Reference< XResultSet > xResultPKCL(xDestMetaData->getPrimaryKeys(aCatalog,aSchema,aTable)); + Reference< XRow > xRowPKCL(xResultPKCL, UNO_QUERY_THROW); + OUString sPKCL; + if ( xRowPKCL.is() ) + { + if (xResultPKCL->next()) + { + sPKCL = xRowPKCL->getString(4); + } + } + + if (!sPKCL.isEmpty()) + { + OUString strSql = "SELECT MAX(\"" + sPKCL + "\") FROM " + sComposedTableName; + + Reference< XResultSet > xResultMAXNUM(m_xDestConnection->createStatement()->executeQuery(strSql)); + Reference< XRow > xRow(xResultMAXNUM, UNO_QUERY_THROW); + + sal_Int64 maxVal = -1L; + if (xResultMAXNUM->next()) + { + maxVal = xRow->getLong(1); + } + + if (maxVal > 0L) + { + strSql = "ALTER TABLE " + sComposedTableName + " ALTER \"" + sPKCL + "\" RESTART WITH " + OUString::number(maxVal + 1); + + m_xDestConnection->createStatement()->execute(strSql); + } + } } break; |