Related: tdf#108057 Use autoincrement for automatically added primary key

This partially undoes commit 24940e2c37
(#100599# #95927# check if row is readonly and disable autoincrement
column for auto primarykey, 2002-06-27), enabling autoincrement again.

Make sure to use the largest type that supports autoincrement.

Change-Id: I4dfdbfb0c3d9b94c7634082b029c6e66aa01c782
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/165027
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
This commit is contained in:
Mike Kaganski 2024-03-19 16:48:19 +05:00
parent 1e66905c84
commit c07548447a
3 changed files with 31 additions and 19 deletions

View file

@ -1052,29 +1052,40 @@ void setEvalDateFormatForFormatter(Reference< css::util::XNumberFormatter > cons
} }
} }
static bool TypeIsGreater(const TOTypeInfoSP& lhs, const TOTypeInfoSP& rhs)
{
assert(lhs);
if (!rhs)
return true;
if (lhs->nNumPrecRadix == rhs->nNumPrecRadix)
return lhs->nPrecision > rhs->nPrecision;
if (lhs->nPrecision == rhs->nPrecision)
return lhs->nNumPrecRadix > rhs->nNumPrecRadix;
if ((lhs->nNumPrecRadix > rhs->nNumPrecRadix) == (lhs->nPrecision > rhs->nPrecision))
return lhs->nPrecision > rhs->nPrecision;
return std::pow(lhs->nNumPrecRadix, lhs->nPrecision)
> std::pow(rhs->nNumPrecRadix, rhs->nPrecision);
}
TOTypeInfoSP queryPrimaryKeyType(const OTypeInfoMap& _rTypeInfo) TOTypeInfoSP queryPrimaryKeyType(const OTypeInfoMap& _rTypeInfo)
{ {
TOTypeInfoSP pTypeInfo; TOTypeInfoSP pTypeInfo, pFallback;
// first we search for a type which supports autoIncrement // first we search for a largest type which supports autoIncrement
for (auto const& elem : _rTypeInfo) for (auto const& elem : _rTypeInfo)
{ {
// OJ: we don't want to set an autoincrement column to be key if (elem.second->bAutoIncrement && TypeIsGreater(elem.second, pTypeInfo))
// because we don't have the possibility to know how to create pTypeInfo = elem.second;
// such auto increment column later on if (pTypeInfo)
// so until we know how to do it, we create a column without autoincrement continue;
// therefore we have searched if (elem.second->nType == DataType::INTEGER)
if ( elem.second->nType == DataType::INTEGER ) pFallback = elem.second; // default alternative
{ else if (!pFallback && elem.second->nType == DataType::DOUBLE)
pTypeInfo = elem.second; // alternative pFallback = elem.second; // alternative
break; else if (!pFallback && elem.second->nType == DataType::REAL)
} pFallback = elem.second; // alternative
else if ( !pTypeInfo && elem.second->nType == DataType::DOUBLE )
pTypeInfo = elem.second; // alternative
else if ( !pTypeInfo && elem.second->nType == DataType::REAL )
pTypeInfo = elem.second; // alternative
} }
if ( !pTypeInfo ) // just a fallback if ( !pTypeInfo ) // just a fallback
pTypeInfo = queryTypeInfoByType(DataType::VARCHAR,_rTypeInfo); pTypeInfo = pFallback ? pFallback : queryTypeInfoByType(DataType::VARCHAR, _rTypeInfo);
OSL_ENSURE(pTypeInfo,"checkColumns: can't find a type which is usable as a key!"); OSL_ENSURE(pTypeInfo,"checkColumns: can't find a type which is usable as a key!");
return pTypeInfo; return pTypeInfo;

View file

@ -737,6 +737,7 @@ bool OCopyTableWizard::CheckColumns(sal_Int32& _rnBreakPos)
OFieldDescription* pField = new OFieldDescription(); OFieldDescription* pField = new OFieldDescription();
pField->SetName(m_aKeyName); pField->SetName(m_aKeyName);
pField->FillFromTypeInfo(pTypeInfo,true,true); pField->FillFromTypeInfo(pTypeInfo,true,true);
pField->SetAutoIncrement(pTypeInfo->bAutoIncrement);
pField->SetPrimaryKey(true); pField->SetPrimaryKey(true);
m_bAddPKFirstTime = false; m_bAddPKFirstTime = false;
insertColumn(0,pField); insertColumn(0,pField);

View file

@ -903,15 +903,15 @@ bool OTableController::checkColumns(bool _bNew)
{ {
case RET_YES: case RET_YES:
{ {
auto pNewRow = std::make_shared<OTableRow>();
TOTypeInfoSP pTypeInfo = ::dbaui::queryPrimaryKeyType(m_aTypeInfo); TOTypeInfoSP pTypeInfo = ::dbaui::queryPrimaryKeyType(m_aTypeInfo);
if ( !pTypeInfo ) if ( !pTypeInfo )
break; break;
auto pNewRow = std::make_shared<OTableRow>();
pNewRow->SetFieldType( pTypeInfo ); pNewRow->SetFieldType( pTypeInfo );
OFieldDescription* pActFieldDescr = pNewRow->GetActFieldDescr(); OFieldDescription* pActFieldDescr = pNewRow->GetActFieldDescr();
pActFieldDescr->SetAutoIncrement(false); pActFieldDescr->SetAutoIncrement(pTypeInfo->bAutoIncrement);
pActFieldDescr->SetIsNullable(ColumnValue::NO_NULLS); pActFieldDescr->SetIsNullable(ColumnValue::NO_NULLS);
pActFieldDescr->SetName( createUniqueName("ID" )); pActFieldDescr->SetName( createUniqueName("ID" ));