diff options
author | Lionel Elie Mamane <lionel@mamane.lu> | 2020-05-09 20:27:35 +0200 |
---|---|---|
committer | Lionel Elie Mamane <lionel@mamane.lu> | 2020-05-10 09:33:06 +0200 |
commit | 0b81aaa36b5b78e208c5cc2cd36b4906b8d636a6 (patch) | |
tree | 4305ee38b8d9fd152d4e8875d537adfd303dd183 /dbaccess/source/ui/misc | |
parent | b513686c11b3c50055a3182f20dc6ed4ea0ca40c (diff) |
CopyTableWizard: make code less confusing
createTable looked into the requested operation, and depending on
that, either created the table or fetched the existing one. For a
function named createTable, that made for confusing reading.
Split that into:
* createTable that creates the table
* getTable that fetches the existing table
* returnTable that automagically creates or fetches an existing
table, depending on the requested operation.
Change-Id: I91be67c24026c850530dcaef5ec95ab508e81434
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/93882
Tested-by: Jenkins
Reviewed-by: Lionel Elie Mamane <lionel@mamane.lu>
Diffstat (limited to 'dbaccess/source/ui/misc')
-rw-r--r-- | dbaccess/source/ui/misc/DExport.cxx | 2 | ||||
-rw-r--r-- | dbaccess/source/ui/misc/WCopyTable.cxx | 226 |
2 files changed, 123 insertions, 105 deletions
diff --git a/dbaccess/source/ui/misc/DExport.cxx b/dbaccess/source/ui/misc/DExport.cxx index 384703a6b573..cc24fe114c63 100644 --- a/dbaccess/source/ui/misc/DExport.cxx +++ b/dbaccess/source/ui/misc/DExport.cxx @@ -687,7 +687,7 @@ bool ODatabaseExport::executeWizard(const OUString& _rTableName, const Any& _aTe case CopyTableOperation::CopyDefinitionAndData: case CopyTableOperation::AppendData: { - m_xTable = aWizard.createTable(); + m_xTable = aWizard.returnTable(); bError = !m_xTable.is(); if(m_xTable.is()) { diff --git a/dbaccess/source/ui/misc/WCopyTable.cxx b/dbaccess/source/ui/misc/WCopyTable.cxx index 0a54f4982d0c..30ebf6f6a642 100644 --- a/dbaccess/source/ui/misc/WCopyTable.cxx +++ b/dbaccess/source/ui/misc/WCopyTable.cxx @@ -1135,6 +1135,28 @@ Reference< XPropertySet > OCopyTableWizard::createView() const return ::dbaui::createView( m_sName, m_xDestConnection, sCommand ); } +Reference< XPropertySet > OCopyTableWizard::returnTable() +{ + if ( getOperation() == CopyTableOperation::AppendData ) + return getTable(); + else + return createTable(); +} + +Reference< XPropertySet > OCopyTableWizard::getTable() +{ + Reference< XPropertySet > xTable; + + Reference<XTablesSupplier> xSup( m_xDestConnection, UNO_QUERY ); + Reference< XNameAccess > xTables; + if(xSup.is()) + xTables = xSup->getTables(); + if(xTables.is() && xTables->hasByName(m_sName)) + xTables->getByName(m_sName) >>= xTable; + + return xTable; +} + Reference< XPropertySet > OCopyTableWizard::createTable() { Reference< XPropertySet > xTable; @@ -1143,126 +1165,122 @@ Reference< XPropertySet > OCopyTableWizard::createTable() Reference< XNameAccess > xTables; if(xSup.is()) xTables = xSup->getTables(); - if ( getOperation() != CopyTableOperation::AppendData ) + Reference<XDataDescriptorFactory> xFact(xTables,UNO_QUERY); + OSL_ENSURE(xFact.is(),"No XDataDescriptorFactory available!"); + if(!xFact.is()) + return nullptr; + + xTable = xFact->createDataDescriptor(); + OSL_ENSURE(xTable.is(),"Could not create a new object!"); + if(!xTable.is()) + return nullptr; + + OUString sCatalog,sSchema,sTable; + Reference< XDatabaseMetaData> xMetaData = m_xDestConnection->getMetaData(); + ::dbtools::qualifiedNameComponents(xMetaData, + m_sName, + sCatalog, + sSchema, + sTable, + ::dbtools::EComposeRule::InDataManipulation); + + if ( sCatalog.isEmpty() && xMetaData->supportsCatalogsInTableDefinitions() ) { - Reference<XDataDescriptorFactory> xFact(xTables,UNO_QUERY); - OSL_ENSURE(xFact.is(),"No XDataDescriptorFactory available!"); - if(!xFact.is()) - return nullptr; - - xTable = xFact->createDataDescriptor(); - OSL_ENSURE(xTable.is(),"Could not create a new object!"); - if(!xTable.is()) - return nullptr; - - OUString sCatalog,sSchema,sTable; - Reference< XDatabaseMetaData> xMetaData = m_xDestConnection->getMetaData(); - ::dbtools::qualifiedNameComponents(xMetaData, - m_sName, - sCatalog, - sSchema, - sTable, - ::dbtools::EComposeRule::InDataManipulation); + sCatalog = m_xDestConnection->getCatalog(); + } - if ( sCatalog.isEmpty() && xMetaData->supportsCatalogsInTableDefinitions() ) + if ( sSchema.isEmpty() && xMetaData->supportsSchemasInTableDefinitions() ) + { + // query of current schema is quite inconsistent. In case of some + // DBMS's each user has their own schema. + sSchema = xMetaData->getUserName(); + // In case of mysql it is not that simple + if(xMetaData->getDatabaseProductName() == "MySQL") { - sCatalog = m_xDestConnection->getCatalog(); + Reference< XStatement > xSelect = m_xDestConnection->createStatement(); + Reference< XResultSet > xRs = xSelect->executeQuery("select database()"); + (void)xRs->next(); // first and only result + Reference< XRow > xRow( xRs, UNO_QUERY_THROW ); + sSchema = xRow->getString(1); } + } - if ( sSchema.isEmpty() && xMetaData->supportsSchemasInTableDefinitions() ) - { - // query of current schema is quite inconsistent. In case of some - // DBMS's each user has their own schema. - sSchema = xMetaData->getUserName(); - // In case of mysql it is not that simple - if(xMetaData->getDatabaseProductName() == "MySQL") - { - Reference< XStatement > xSelect = m_xDestConnection->createStatement(); - Reference< XResultSet > xRs = xSelect->executeQuery("select database()"); - (void)xRs->next(); // first and only result - Reference< XRow > xRow( xRs, UNO_QUERY_THROW ); - sSchema = xRow->getString(1); - } - } + xTable->setPropertyValue(PROPERTY_CATALOGNAME,makeAny(sCatalog)); + xTable->setPropertyValue(PROPERTY_SCHEMANAME,makeAny(sSchema)); + xTable->setPropertyValue(PROPERTY_NAME,makeAny(sTable)); - xTable->setPropertyValue(PROPERTY_CATALOGNAME,makeAny(sCatalog)); - xTable->setPropertyValue(PROPERTY_SCHEMANAME,makeAny(sSchema)); - xTable->setPropertyValue(PROPERTY_NAME,makeAny(sTable)); - - Reference< XColumnsSupplier > xSuppDestinationColumns( xTable, UNO_QUERY ); - // now append the columns - const ODatabaseExport::TColumnVector& rVec = getDestVector(); - appendColumns( xSuppDestinationColumns, &rVec ); - // now append the primary key - Reference<XKeysSupplier> xKeySup(xTable,UNO_QUERY); - appendKey(xKeySup, &rVec); - - Reference<XAppend> xAppend(xTables,UNO_QUERY); - if(xAppend.is()) - xAppend->appendByDescriptor(xTable); - - // xTable = NULL; - // we need to reget the table because after appending it, it is no longer valid - if(xTables->hasByName(m_sName)) - xTables->getByName(m_sName) >>= xTable; - else + Reference< XColumnsSupplier > xSuppDestinationColumns( xTable, UNO_QUERY ); + // now append the columns + const ODatabaseExport::TColumnVector& rVec = getDestVector(); + appendColumns( xSuppDestinationColumns, &rVec ); + // now append the primary key + Reference<XKeysSupplier> xKeySup(xTable,UNO_QUERY); + appendKey(xKeySup, &rVec); + + Reference<XAppend> xAppend(xTables,UNO_QUERY); + if(xAppend.is()) + xAppend->appendByDescriptor(xTable); + + // xTable = NULL; + // we need to reget the table because after appending it, it is no longer valid + if(xTables->hasByName(m_sName)) + xTables->getByName(m_sName) >>= xTable; + else + { + OUString sComposedName( + ::dbtools::composeTableName( m_xDestConnection->getMetaData(), xTable, ::dbtools::EComposeRule::InDataManipulation, false ) ); + if(xTables->hasByName(sComposedName)) { - OUString sComposedName( - ::dbtools::composeTableName( m_xDestConnection->getMetaData(), xTable, ::dbtools::EComposeRule::InDataManipulation, false ) ); - if(xTables->hasByName(sComposedName)) - { - xTables->getByName(sComposedName) >>= xTable; - m_sName = sComposedName; - } - else - xTable = nullptr; + xTables->getByName(sComposedName) >>= xTable; + m_sName = sComposedName; } - if(xTable.is()) + else + xTable = nullptr; + } + + if(xTable.is()) + { + xSuppDestinationColumns.set( xTable, UNO_QUERY_THROW ); + // insert new table name into table filter + ::dbaui::appendToFilter(m_xDestConnection, m_sName, GetComponentContext(), m_xAssistant.get()); + + // copy ui settings + m_rSourceObject.copyUISettingsTo( xTable ); + //copy filter and sorting + m_rSourceObject.copyFilterAndSortingTo(m_xDestConnection,xTable); + // set column mappings + Reference<XNameAccess> xNameAccess = xSuppDestinationColumns->getColumns(); + Sequence< OUString> aSeq = xNameAccess->getElementNames(); + const OUString* pIter = aSeq.getConstArray(); + const OUString* pEnd = pIter + aSeq.getLength(); + + for(sal_Int32 nNewPos=1;pIter != pEnd;++pIter,++nNewPos) { - xSuppDestinationColumns.set( xTable, UNO_QUERY_THROW ); - // insert new table name into table filter - ::dbaui::appendToFilter(m_xDestConnection, m_sName, GetComponentContext(), m_xAssistant.get()); - - // copy ui settings - m_rSourceObject.copyUISettingsTo( xTable ); - //copy filter and sorting - m_rSourceObject.copyFilterAndSortingTo(m_xDestConnection,xTable); - // set column mappings - Reference<XNameAccess> xNameAccess = xSuppDestinationColumns->getColumns(); - Sequence< OUString> aSeq = xNameAccess->getElementNames(); - const OUString* pIter = aSeq.getConstArray(); - const OUString* pEnd = pIter + aSeq.getLength(); - - for(sal_Int32 nNewPos=1;pIter != pEnd;++pIter,++nNewPos) + ODatabaseExport::TColumns::const_iterator aDestIter = m_vDestColumns.find(*pIter); + + if ( aDestIter != m_vDestColumns.end() ) { - ODatabaseExport::TColumns::const_iterator aDestIter = m_vDestColumns.find(*pIter); + ODatabaseExport::TColumnVector::const_iterator aFind = std::find(m_aDestVec.begin(),m_aDestVec.end(),aDestIter); + sal_Int32 nPos = (aFind - m_aDestVec.begin())+1; + + ODatabaseExport::TPositions::iterator aPosFind = std::find_if( + m_vColumnPositions.begin(), + m_vColumnPositions.end(), + [nPos] (const ODatabaseExport::TPositions::value_type& tPos) { + return tPos.first == nPos; + } + ); - if ( aDestIter != m_vDestColumns.end() ) + if ( m_vColumnPositions.end() != aPosFind ) { - ODatabaseExport::TColumnVector::const_iterator aFind = std::find(m_aDestVec.begin(),m_aDestVec.end(),aDestIter); - sal_Int32 nPos = (aFind - m_aDestVec.begin())+1; - - ODatabaseExport::TPositions::iterator aPosFind = std::find_if( - m_vColumnPositions.begin(), - m_vColumnPositions.end(), - [nPos] (const ODatabaseExport::TPositions::value_type& tPos) { - return tPos.first == nPos; - } - ); - - if ( m_vColumnPositions.end() != aPosFind ) - { - aPosFind->second = nNewPos; - OSL_ENSURE( m_vColumnTypes.size() > o3tl::make_unsigned( aPosFind - m_vColumnPositions.begin() ), - "Invalid index for vector!" ); - m_vColumnTypes[ aPosFind - m_vColumnPositions.begin() ] = (*aFind)->second->GetType(); - } + aPosFind->second = nNewPos; + OSL_ENSURE( m_vColumnTypes.size() > o3tl::make_unsigned( aPosFind - m_vColumnPositions.begin() ), + "Invalid index for vector!" ); + m_vColumnTypes[ aPosFind - m_vColumnPositions.begin() ] = (*aFind)->second->GetType(); } } } } - else if(xTables.is() && xTables->hasByName(m_sName)) - xTables->getByName(m_sName) >>= xTable; return xTable; } |