Kaydet (Commit) 991346d5 authored tarafından Jaskaran Singh's avatar Jaskaran Singh Kaydeden (comit) Markus Mohrhard

Add a way to resize ScDBData

Change-Id: I6e0e1a7f58873c012e0e8015ed69da38a32a44f4
Reviewed-on: https://gerrit.libreoffice.org/39323Tested-by: 's avatarJenkins <ci@libreoffice.org>
Reviewed-by: 's avatarMarkus Mohrhard <markus.mohrhard@googlemail.com>
üst 472737ad
...@@ -62,7 +62,7 @@ void ScDataProvidersTest::testCSVImport() ...@@ -62,7 +62,7 @@ void ScDataProvidersTest::testCSVImport()
OUString aCSVPath; OUString aCSVPath;
createCSVPath( "dataprovider.", aCSVPath ); createCSVPath( "dataprovider.", aCSVPath );
OUString aDBName = "TEST"; OUString aDBName = "TEST";
sc::ExternalDataMapper aExternalDataMapper (&getDocShell(), aCSVPath, aDBName, 0, 0, 0, 5, 5, success); sc::ExternalDataMapper aExternalDataMapper (&getDocShell(), aCSVPath, aDBName, 0, 0, 0, 5, 5, false, success);
aExternalDataMapper.StartImport(); aExternalDataMapper.StartImport();
Scheduler::ProcessEventsToIdle(); Scheduler::ProcessEventsToIdle();
CPPUNIT_ASSERT_EQUAL (-2012.0, m_pDoc->GetValue(0, 0, 0)); CPPUNIT_ASSERT_EQUAL (-2012.0, m_pDoc->GetValue(0, 0, 0));
......
...@@ -57,16 +57,20 @@ std::unique_ptr<SvStream> FetchStreamFromURL(const OUString& rURL, OStringBuffer ...@@ -57,16 +57,20 @@ std::unique_ptr<SvStream> FetchStreamFromURL(const OUString& rURL, OStringBuffer
} }
ExternalDataMapper::ExternalDataMapper(ScDocShell* pDocShell, const OUString& rURL, const OUString& rName, SCTAB nTab, ExternalDataMapper::ExternalDataMapper(ScDocShell* pDocShell, const OUString& rURL, const OUString& rName, SCTAB nTab,
SCCOL nCol1,SCROW nRow1, SCCOL nCol2, SCROW nRow2, bool& bSuccess): SCCOL nCol1,SCROW nRow1, SCCOL nCol2, SCROW nRow2, bool bAllowResize, bool& bSuccess):
maRange (ScRange(nCol1, nRow1, nTab, nCol2, nRow2, nTab)), maRange (ScRange(nCol1, nRow1, nTab, nCol2, nRow2, nTab)),
mpDocShell(pDocShell), mpDocShell(pDocShell),
mpDataProvider (new CSVDataProvider(mpDocShell, rURL, maRange)),
mpDBCollection (pDocShell->GetDocument().GetDBCollection()) mpDBCollection (pDocShell->GetDocument().GetDBCollection())
{ {
bSuccess = true; bSuccess = true;
ScDBCollection::NamedDBs& rNamedDBS = mpDBCollection->getNamedDBs(); ScDBCollection::NamedDBs& rNamedDBS = mpDBCollection->getNamedDBs();
if(!rNamedDBS.insert (new ScDBData (rName, nTab, nCol1, nRow1, nCol2, nRow2))) ScDBData* aDBData = new ScDBData (rName, nTab, nCol1, nRow1, nCol2, nRow2);
if(!rNamedDBS.insert (aDBData))
bSuccess = false; bSuccess = false;
mpDBDataManager = std::shared_ptr<ScDBDataManager>(new ScDBDataManager(aDBData, bAllowResize));
mpDBDataManager->SetDestinationRange(maRange);
mpDataProvider = std::unique_ptr<DataProvider> (new CSVDataProvider(mpDocShell, rURL, maRange, mpDBDataManager.get()));
} }
ExternalDataMapper::~ExternalDataMapper() ExternalDataMapper::~ExternalDataMapper()
...@@ -133,12 +137,13 @@ public: ...@@ -133,12 +137,13 @@ public:
} }
}; };
CSVFetchThread::CSVFetchThread(ScDocument& rDoc, const OUString& mrURL, size_t nColCount): CSVFetchThread::CSVFetchThread(ScDocument& rDoc, ScDBDataManager* pBDDataManager, const OUString& mrURL, size_t nColCount):
Thread("ReaderThread"), Thread("ReaderThread"),
mpStream(nullptr), mpStream(nullptr),
mrDocument(rDoc), mrDocument(rDoc),
maURL (mrURL), maURL (mrURL),
mnColCount(nColCount), mnColCount(nColCount),
mpDBDataManager(pBDDataManager),
mbTerminate(false) mbTerminate(false)
{ {
maConfig.delimiters.push_back(','); maConfig.delimiters.push_back(',');
...@@ -175,6 +180,7 @@ void CSVFetchThread::execute() ...@@ -175,6 +180,7 @@ void CSVFetchThread::execute()
{ {
LinesType aLines(10); LinesType aLines(10);
SCROW nCurRow = 0; SCROW nCurRow = 0;
SCCOL nCol = 0;
for (Line & rLine : aLines) for (Line & rLine : aLines)
{ {
rLine.maCells.clear(); rLine.maCells.clear();
...@@ -188,7 +194,7 @@ void CSVFetchThread::execute() ...@@ -188,7 +194,7 @@ void CSVFetchThread::execute()
return; return;
} }
SCCOL nCol = 0; nCol = 0;
const char* pLineHead = rLine.maLine.getStr(); const char* pLineHead = rLine.maLine.getStr();
for (auto& rCell : rLine.maCells) for (auto& rCell : rLine.maCells)
{ {
...@@ -204,6 +210,8 @@ void CSVFetchThread::execute() ...@@ -204,6 +210,8 @@ void CSVFetchThread::execute()
} }
nCurRow++; nCurRow++;
} }
mpDBDataManager->SetSourceRange(nCol, nCurRow);
} }
} }
...@@ -235,15 +243,17 @@ void CSVFetchThread::ResumeFetchStream() ...@@ -235,15 +243,17 @@ void CSVFetchThread::ResumeFetchStream()
maCondReadStream.set(); maCondReadStream.set();
} }
CSVDataProvider::CSVDataProvider(ScDocShell* pDocShell, const OUString& rURL, const ScRange& rRange): CSVDataProvider::CSVDataProvider(ScDocShell* pDocShell, const OUString& rURL, ScRange& rRange, ScDBDataManager* pBDDataManager):
maURL(rURL), maURL(rURL),
mrRange(rRange), mrRange(rRange),
mpDocShell(pDocShell), mpDocShell(pDocShell),
mpDocument(&pDocShell->GetDocument()), mpDocument(&pDocShell->GetDocument()),
mpDBDataManager(pBDDataManager),
mpLines(nullptr), mpLines(nullptr),
mnLineCount(0), mnLineCount(0),
mbImportUnderway(false) mbImportUnderway(false)
{ {
mpDBDataManager->SetDestinationRange(rRange);
} }
CSVDataProvider::~CSVDataProvider() CSVDataProvider::~CSVDataProvider()
...@@ -258,7 +268,7 @@ void CSVDataProvider::StartImport() ...@@ -258,7 +268,7 @@ void CSVDataProvider::StartImport()
if (!mxCSVFetchThread.is()) if (!mxCSVFetchThread.is())
{ {
ScDocument aDoc; ScDocument aDoc;
mxCSVFetchThread = new CSVFetchThread(aDoc, maURL, mrRange.aEnd.Col() - mrRange.aStart.Col() + 1); mxCSVFetchThread = new CSVFetchThread(aDoc, mpDBDataManager, maURL, mrRange.aEnd.Col() - mrRange.aStart.Col() + 1);
mxCSVFetchThread->launch(); mxCSVFetchThread->launch();
if (mxCSVFetchThread.is()) if (mxCSVFetchThread.is())
{ {
...@@ -302,6 +312,9 @@ Line CSVDataProvider::GetLine() ...@@ -302,6 +312,9 @@ Line CSVDataProvider::GetLine()
void CSVDataProvider::WriteToDoc(ScDocument& rDoc) void CSVDataProvider::WriteToDoc(ScDocument& rDoc)
{ {
if (mpDBDataManager->Resize())
mrRange = mpDBDataManager->GetDestinationRange();
double* pfValue; double* pfValue;
for (int nRow = mrRange.aStart.Row(); nRow < mrRange.aEnd.Row(); ++nRow) for (int nRow = mrRange.aStart.Row(); nRow < mrRange.aEnd.Row(); ++nRow)
{ {
...@@ -323,6 +336,77 @@ void CSVDataProvider::WriteToDoc(ScDocument& rDoc) ...@@ -323,6 +336,77 @@ void CSVDataProvider::WriteToDoc(ScDocument& rDoc)
} }
} }
ScDBDataManager::ScDBDataManager(ScDBData* pDBData, bool bAllowResize = false):
mpDBData(pDBData),
mbAllowResize(bAllowResize)
{
}
ScDBDataManager::~ScDBDataManager()
{
}
void ScDBDataManager::SetDatabase(ScDBData* pDbData)
{
mpDBData = pDbData;
}
bool ScDBDataManager::IsResizeAllowed()
{
return mbAllowResize;
}
bool ScDBDataManager::RequiresResize(SCROW& RowDifference, SCCOL& ColDifference)
{
SCROW nTotalSourceRows = maSourceRange.aStart.Row() - maSourceRange.aEnd.Row();
SCCOL nTotalSourceCols = maSourceRange.aStart.Col() - maSourceRange.aEnd.Col();
SCROW nTotalDestinationRows = maDestinationRange.aStart.Row() - maDestinationRange.aEnd.Row();
SCCOL nTotalDestinationCols = maDestinationRange.aStart.Col() - maDestinationRange.aEnd.Col();
RowDifference = nTotalSourceRows - nTotalDestinationRows;
ColDifference = nTotalSourceCols - nTotalDestinationCols;
if (nTotalSourceRows != nTotalDestinationRows || nTotalSourceCols != nTotalDestinationCols)
return true;
return false;
}
bool ScDBDataManager::Resize()
{
SCROW RowDifference =0;
SCCOL ColDifference = 0;
if (IsResizeAllowed() && RequiresResize(RowDifference, ColDifference))
{
maDestinationRange.aEnd = ScAddress(maDestinationRange.aEnd.Row() + RowDifference, maDestinationRange.aEnd.Col() + ColDifference, maDestinationRange.aEnd.Tab());
return true;
}
return false;
}
void ScDBDataManager::SetSourceRange(SCCOL nCol, SCROW nRow)
{
maSourceRange = ScRange(0, 0, 0, nCol, nRow, 0);
}
void ScDBDataManager::SetDestinationRange(ScRange& aRange)
{
maDestinationRange = aRange;
}
ScRange& ScDBDataManager::GetSourceRange()
{
return maSourceRange;
}
ScRange& ScDBDataManager::GetDestinationRange()
{
return maDestinationRange;
}
} }
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
...@@ -37,6 +37,7 @@ namespace sc { ...@@ -37,6 +37,7 @@ namespace sc {
class DataProvider; class DataProvider;
class CSVDataProvider; class CSVDataProvider;
class ScDBDataManager;
class SC_DLLPUBLIC ExternalDataMapper class SC_DLLPUBLIC ExternalDataMapper
{ {
...@@ -45,10 +46,11 @@ class SC_DLLPUBLIC ExternalDataMapper ...@@ -45,10 +46,11 @@ class SC_DLLPUBLIC ExternalDataMapper
std::unique_ptr<DataProvider> mpDataProvider; std::unique_ptr<DataProvider> mpDataProvider;
ScDocument maDocument; ScDocument maDocument;
ScDBCollection* mpDBCollection; ScDBCollection* mpDBCollection;
std::shared_ptr<ScDBDataManager> mpDBDataManager;
public: public:
ExternalDataMapper(ScDocShell* pDocShell, const OUString& rUrl, const OUString& rName, ExternalDataMapper(ScDocShell* pDocShell, const OUString& rUrl, const OUString& rName,
SCTAB nTab, SCCOL nCol1,SCROW nRow1, SCCOL nCOL2, SCROW nRow2, bool& bSuccess); SCTAB nTab, SCCOL nCol1,SCROW nRow1, SCCOL nCOL2, SCROW nRow2, bool bAllowResize, bool& bSuccess);
~ExternalDataMapper(); ~ExternalDataMapper();
...@@ -89,6 +91,7 @@ class CSVFetchThread : public salhelper::Thread ...@@ -89,6 +91,7 @@ class CSVFetchThread : public salhelper::Thread
ScDocument& mrDocument; ScDocument& mrDocument;
OUString maURL; OUString maURL;
size_t mnColCount; size_t mnColCount;
ScDBDataManager* mpDBDataManager;
bool mbTerminate; bool mbTerminate;
osl::Mutex maMtxTerminate; osl::Mutex maMtxTerminate;
...@@ -104,7 +107,7 @@ class CSVFetchThread : public salhelper::Thread ...@@ -104,7 +107,7 @@ class CSVFetchThread : public salhelper::Thread
virtual void execute() override; virtual void execute() override;
public: public:
CSVFetchThread(ScDocument& rDoc, const OUString&, size_t); CSVFetchThread(ScDocument& rDoc, ScDBDataManager*, const OUString&, size_t);
virtual ~CSVFetchThread() override; virtual ~CSVFetchThread() override;
void RequestTerminate(); void RequestTerminate();
...@@ -139,6 +142,7 @@ class CSVDataProvider : public DataProvider ...@@ -139,6 +142,7 @@ class CSVDataProvider : public DataProvider
rtl::Reference<CSVFetchThread> mxCSVFetchThread; rtl::Reference<CSVFetchThread> mxCSVFetchThread;
ScDocShell* mpDocShell; ScDocShell* mpDocShell;
ScDocument* mpDocument; ScDocument* mpDocument;
ScDBDataManager* mpDBDataManager;
LinesType* mpLines; LinesType* mpLines;
size_t mnLineCount; size_t mnLineCount;
...@@ -146,7 +150,7 @@ class CSVDataProvider : public DataProvider ...@@ -146,7 +150,7 @@ class CSVDataProvider : public DataProvider
public: public:
CSVDataProvider (ScDocShell* pDocShell, const OUString& rUrl, const ScRange& rRange); CSVDataProvider (ScDocShell* pDocShell, const OUString& rUrl, ScRange& rRange, ScDBDataManager*);
virtual ~CSVDataProvider() override; virtual ~CSVDataProvider() override;
virtual void StartImport() override; virtual void StartImport() override;
...@@ -161,6 +165,29 @@ public: ...@@ -161,6 +165,29 @@ public:
const OUString& GetURL() const override { return maURL; } const OUString& GetURL() const override { return maURL; }
}; };
class ScDBDataManager
{
ScDBData* mpDBData;
ScRange maSourceRange;
ScRange maDestinationRange;
bool mbAllowResize;
public:
ScDBDataManager(ScDBData*, bool);
~ScDBDataManager();
bool IsResizeAllowed();
bool Resize();
bool RequiresResize(SCROW&, SCCOL&);
void SetDatabase(ScDBData*);
void SetSourceRange(SCCOL, SCROW);
void SetDestinationRange(ScRange&);
ScRange& GetDestinationRange();
ScRange& GetSourceRange();
};
} }
#endif #endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment