sc/inc/datauno.hxx                      |   12 +++++------
 sc/source/filter/inc/workbookhelper.hxx |    5 +---
 sc/source/filter/oox/tablebuffer.cxx    |   13 ++++--------
 sc/source/filter/oox/workbookhelper.cxx |   33 ++++++++++++++++++++------------
 sc/source/ui/inc/dbdocfun.hxx           |    2 -
 5 files changed, 35 insertions(+), 30 deletions(-)

New commits:
commit c3e1743287c5c8b968eb9f45975b44f6adb65ea4
Author:     Deepanshu Sharma <129deepanshusha...@gmail.com>
AuthorDate: Thu Mar 13 15:08:47 2025 +0530
Commit:     Noel Grandin <noel.gran...@collabora.co.uk>
CommitDate: Fri Mar 14 09:03:36 2025 +0100

    tdf#48140 Replace XDatabaseRange with ScDatabaseRangeObj
    
    Co-authored-by: Oromidayo Owolabi <owolabioromiday...@gmail.com>
    
    Change-Id: Idcea0e26edad071cb7af05c89022f477564136ca
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/182858
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk>

diff --git a/sc/inc/datauno.hxx b/sc/inc/datauno.hxx
index 9159e0af1718..f415dd0f0df1 100644
--- a/sc/inc/datauno.hxx
+++ b/sc/inc/datauno.hxx
@@ -22,7 +22,7 @@
 #include "global.hxx"
 #include "queryparam.hxx"
 #include "subtotalparam.hxx"
-
+#include "dapiuno.hxx"
 #include <com/sun/star/sheet/GeneralFunction.hpp>
 #include <com/sun/star/sheet/XCellRangeReferrer.hpp>
 #include <com/sun/star/sheet/XSheetFilterDescriptor.hpp>
@@ -370,7 +370,7 @@ public:
 
 //  ScDataPilotFilterDescriptor - FilterDescriptor of a DataPilotDescriptors
 
-class ScDataPilotFilterDescriptor final : public ScFilterDescriptorBase
+class SC_DLLPUBLIC ScDataPilotFilterDescriptor final : public 
ScFilterDescriptorBase
 {
 private:
     rtl::Reference<ScDataPilotDescriptorBase>  mxParent;
@@ -407,7 +407,7 @@ private:
     void                    Refreshed_Impl();
 
 public:
-    ScDatabaseRangeObj(ScDocShell* pDocSh, OUString aNm);
+    SC_DLLPUBLIC ScDatabaseRangeObj(ScDocShell* pDocSh, OUString aNm);
     SC_DLLPUBLIC ScDatabaseRangeObj(ScDocShell* pDocSh, const SCTAB nTab);
     virtual ~ScDatabaseRangeObj() override;
 
@@ -424,7 +424,7 @@ public:
     virtual void SAL_CALL   setName( const OUString& aName ) override;
 
                             // XDatabaseRange
-    virtual css::table::CellRangeAddress SAL_CALL getDataArea() override;
+    SC_DLLPUBLIC virtual css::table::CellRangeAddress SAL_CALL getDataArea() 
override;
     virtual void SAL_CALL   setDataArea( const css::table::CellRangeAddress& 
aDataArea ) override;
     virtual css::uno::Sequence< css::beans::PropertyValue > SAL_CALL
                             getSortDescriptor() override;
@@ -449,9 +449,9 @@ public:
                             // XPropertySet
     virtual css::uno::Reference< css::beans::XPropertySetInfo >
                             SAL_CALL getPropertySetInfo() override;
-    virtual void SAL_CALL   setPropertyValue( const OUString& aPropertyName,
+    SC_DLLPUBLIC virtual void SAL_CALL   setPropertyValue( const OUString& 
aPropertyName,
                                     const css::uno::Any& aValue ) override;
-    virtual css::uno::Any SAL_CALL getPropertyValue(
+    SC_DLLPUBLIC virtual css::uno::Any SAL_CALL getPropertyValue(
                                     const OUString& PropertyName ) override;
     virtual void SAL_CALL   addPropertyChangeListener( const OUString& 
aPropertyName,
                                     const css::uno::Reference<
diff --git a/sc/source/filter/inc/workbookhelper.hxx 
b/sc/source/filter/inc/workbookhelper.hxx
index e76285ca37a2..c9eb4c6f8951 100644
--- a/sc/source/filter/inc/workbookhelper.hxx
+++ b/sc/source/filter/inc/workbookhelper.hxx
@@ -59,7 +59,7 @@ class ScEditEngineDefaulter;
 class ScDBData;
 class ScRangeData;
 class ScModelObj;
-
+class ScDatabaseRangeObj;
 namespace oox::xls {
 
 enum class FontClassification : sal_uInt8
@@ -232,8 +232,7 @@ public:
     /** Creates and returns a database range on-the-fly in the Calc document.
         The range will not be buffered in the global table buffer.
         @param orName  (in/out-parameter) Returns the resulting used name. */
-    css::uno::Reference< css::sheet::XDatabaseRange >
-                        createDatabaseRangeObject(
+    rtl::Reference<ScDatabaseRangeObj> createDatabaseRangeObject(
                             OUString& orName,
                             const ScRange& rRangeAddr ) const;
 
diff --git a/sc/source/filter/oox/tablebuffer.cxx 
b/sc/source/filter/oox/tablebuffer.cxx
index d43f32806a8c..132c0ee7e3aa 100644
--- a/sc/source/filter/oox/tablebuffer.cxx
+++ b/sc/source/filter/oox/tablebuffer.cxx
@@ -33,7 +33,7 @@
 #include <addressconverter.hxx>
 #include <biffhelper.hxx>
 #include <docuno.hxx>
-
+#include <datauno.hxx>
 namespace oox::xls {
 
 using namespace ::com::sun::star::sheet;
@@ -98,32 +98,29 @@ void Table::finalizeImport()
     {
         maDBRangeName = maModel.maDisplayName;
 
-        Reference< XDatabaseRange > xDatabaseRange(
-            createDatabaseRangeObject( maDBRangeName, maModel.maRange ), 
UNO_SET_THROW);
+        rtl::Reference<ScDatabaseRangeObj> xDatabaseRange = 
createDatabaseRangeObject(maDBRangeName, maModel.maRange );
         ::css::table::CellRangeAddress aAddressRange = 
xDatabaseRange->getDataArea();
         maDestRange = ScRange( aAddressRange.StartColumn, 
aAddressRange.StartRow, aAddressRange.Sheet,
                                aAddressRange.EndColumn, aAddressRange.EndRow, 
aAddressRange.Sheet );
 
-        PropertySet aPropSet( xDatabaseRange );
-
         // Default HasHeader is true at ScDBData.
         if (maModel.mnHeaderRows != 1)
         {
             SAL_WARN_IF( maModel.mnHeaderRows > 1, "sc.filter",
                     "Table HeaderRows > 1 not supported: " << 
maModel.mnHeaderRows);
             if (maModel.mnHeaderRows == 0)
-                aPropSet.setProperty( PROP_ContainsHeader, false);
+                xDatabaseRange->setPropertyValue( u"ContainsHeader"_ustr, 
css::uno::Any(false));
         }
 
         if (maModel.mnTotalsRows > 0)
         {
             SAL_WARN_IF( maModel.mnTotalsRows > 1, "sc.filter",
                     "Table TotalsRows > 1 not supported: " << 
maModel.mnTotalsRows);
-            aPropSet.setProperty( PROP_TotalsRow, true);
+            xDatabaseRange->setPropertyValue( u"TotalsRow"_ustr, 
css::uno::Any(true));
         }
 
         // get formula token index of the database range
-        if( !aPropSet.getProperty( mnTokenIndex, PROP_TokenIndex ) )
+        if( !(xDatabaseRange->getPropertyValue(u"TokenIndex"_ustr) >>= 
mnTokenIndex))
             mnTokenIndex = -1;
     }
     catch( Exception& )
diff --git a/sc/source/filter/oox/workbookhelper.cxx 
b/sc/source/filter/oox/workbookhelper.cxx
index 4ac887c26c40..b124763b4a4d 100644
--- a/sc/source/filter/oox/workbookhelper.cxx
+++ b/sc/source/filter/oox/workbookhelper.cxx
@@ -31,6 +31,7 @@
 #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
 #include <com/sun/star/document/XViewDataSupplier.hpp>
 #include <comphelper/servicehelper.hxx>
+#include <comphelper/diagnose_ex.hxx>
 #include <o3tl/any.hxx>
 #include <osl/thread.h>
 #include <osl/diagnose.h>
@@ -60,6 +61,7 @@
 #include <docsh.hxx>
 #include <document.hxx>
 #include <docuno.hxx>
+#include <dbdocfun.hxx>
 #include <rangenam.hxx>
 #include <tokenarray.hxx>
 #include <tokenuno.hxx>
@@ -159,7 +161,7 @@ public:
     /** Creates and returns a defined name on the-fly in the correct Calc 
sheet. */
     WorkbookHelper::RangeDataRet createLocalNamedRangeObject(OUString& orName, 
sal_Int32 nIndex, sal_Int32 nNameFlags, sal_Int32 nTab);
     /** Creates and returns a database range on-the-fly in the Calc document. 
*/
-    Reference< XDatabaseRange > createDatabaseRangeObject( OUString& orName, 
const ScRange& rRangeAddr );
+    rtl::Reference<ScDatabaseRangeObj> createDatabaseRangeObject( OUString& 
orName, const ScRange& rRangeAddr );
     /** Creates and returns an unnamed database range on-the-fly in the Calc 
document. */
     Reference< XDatabaseRange > createUnnamedDatabaseRangeObject( const 
ScRange& rRangeAddr );
     /** Finds the (already existing) database range of the given formula token 
index. */
@@ -444,31 +446,38 @@ WorkbookHelper::RangeDataRet 
WorkbookGlobals::createLocalNamedRangeObject(
     return aScRangeData;
 }
 
-Reference< XDatabaseRange > WorkbookGlobals::createDatabaseRangeObject( 
OUString& orName, const ScRange& rRangeAddr )
+rtl::Reference<ScDatabaseRangeObj> WorkbookGlobals::createDatabaseRangeObject( 
OUString& orName, const ScRange& rRangeAddr )
 {
     // validate cell range
     ScRange aDestRange = rRangeAddr;
     bool bValidRange = getAddressConverter().validateCellRange( aDestRange, 
true, true );
 
+    ScDocShell* pDocSh = getScDocument().GetDocumentShell();
     // create database range and insert it into the Calc document
-    Reference< XDatabaseRange > xDatabaseRange;
-    if( bValidRange && !orName.isEmpty() ) try
+    if( bValidRange && !orName.isEmpty() && pDocSh) try
     {
         // find an unused name
-        PropertySet aDocProps(( Reference< css::beans::XPropertySet >(mxDoc) 
));
-        Reference< XDatabaseRanges > xDatabaseRanges( 
aDocProps.getAnyProperty( PROP_DatabaseRanges ), UNO_QUERY_THROW );
-        orName = ContainerHelper::getUnusedName( xDatabaseRanges, orName, '_' 
);
+        OUString aNewName = orName;
+        sal_Int32 nIndex = -1;
+        ScDBCollection* pNames = pDocSh->GetDocument().GetDBCollection();
+        while (pNames && 
pNames->getNamedDBs().findByUpperName(ScGlobal::getCharClass().uppercase(aNewName))
 != nullptr )
+            aNewName = orName + OUStringChar('_') + OUString::number( nIndex++ 
);
+        orName = aNewName;
         // create the database range
         CellRangeAddress aApiRange( aDestRange.aStart.Tab(), 
aDestRange.aStart.Col(), aDestRange.aStart.Row(),
                                     aDestRange.aEnd.Col(), 
aDestRange.aEnd.Row() );
-        xDatabaseRanges->addNewByName( orName, aApiRange );
-        xDatabaseRange.set( xDatabaseRanges->getByName( orName ), UNO_QUERY );
+        ScDBDocFunc aFunc(*pDocSh);
+        ScRange aNameRange( static_cast<SCCOL>(aApiRange.StartColumn), 
static_cast<SCROW>(aApiRange.StartRow), aApiRange.Sheet,
+                            static_cast<SCCOL>(aApiRange.EndColumn),   
static_cast<SCROW>(aApiRange.EndRow),   aApiRange.Sheet );
+        if(!( aFunc.AddDBRange( orName, aNameRange ) ))
+            throw RuntimeException(u"Could not add database range"_ustr);
+        return new ScDatabaseRangeObj(pDocSh, orName);
     }
     catch( Exception& )
     {
+        DBG_UNHANDLED_EXCEPTION("sc");
     }
-    OSL_ENSURE( xDatabaseRange.is(), 
"WorkbookGlobals::createDatabaseRangeObject - cannot create database range" );
-    return xDatabaseRange;
+    return {};
 }
 
 Reference< XDatabaseRange > WorkbookGlobals::createUnnamedDatabaseRangeObject( 
const ScRange& rRangeAddr )
@@ -914,7 +923,7 @@ WorkbookHelper::RangeDataRet 
WorkbookHelper::createLocalNamedRangeObject(OUStrin
     return mrBookGlob.createLocalNamedRangeObject(orName, nIndex, nNameFlags, 
nTab);
 }
 
-Reference< XDatabaseRange > WorkbookHelper::createDatabaseRangeObject( 
OUString& orName, const ScRange& rRangeAddr ) const
+rtl::Reference<ScDatabaseRangeObj> WorkbookHelper::createDatabaseRangeObject( 
OUString& orName, const ScRange& rRangeAddr ) const
 {
     return mrBookGlob.createDatabaseRangeObject( orName, rRangeAddr );
 }
diff --git a/sc/source/ui/inc/dbdocfun.hxx b/sc/source/ui/inc/dbdocfun.hxx
index 780e523b5193..cabfab1526dc 100644
--- a/sc/source/ui/inc/dbdocfun.hxx
+++ b/sc/source/ui/inc/dbdocfun.hxx
@@ -69,7 +69,7 @@ public:
     void            DoSubTotals( SCTAB nTab, const ScSubTotalParam& rParam,
                                     bool bRecord, bool bApi );
 
-    bool AddDBRange( const OUString& rName, const ScRange& rRange );
+    SC_DLLPUBLIC bool AddDBRange( const OUString& rName, const ScRange& rRange 
);
     bool DeleteDBRange( const OUString& rName );
     bool RenameDBRange( const OUString& rOld, const OUString& rNew );
     void ModifyDBData( const ScDBData& rNewData );  // Name unveraendert

Reply via email to