dbaccess/source/ui/uno/copytablewizard.cxx |   41 +++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

New commits:
commit 2a8e120db1c3175ff937cdbe6d0ade23dc380c01
Author:     Oleksii Makhotin <a...@bitprox.com>
AuthorDate: Tue Apr 6 16:03:41 2021 +0300
Commit:     Noel Grandin <noel.gran...@collabora.co.uk>
CommitDate: Sat Apr 17 14:26:23 2021 +0200

    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.gran...@collabora.co.uk>

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;
 
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to