Kaydet (Commit) 278b1de2 authored tarafından Vikas's avatar Vikas Kaydeden (comit) Markus Mohrhard

Added base as a data provider

Change-Id: Iaf23b70f303cd05bcf6450fed1246de894285526
Reviewed-on: https://gerrit.libreoffice.org/57447
Tested-by: Jenkins
Reviewed-by: 's avatarMarkus Mohrhard <markus.mohrhard@googlemail.com>
üst c494273a
...@@ -82,7 +82,15 @@ $(eval $(call gb_CppunitTest_use_components,sc_dataprovider,\ ...@@ -82,7 +82,15 @@ $(eval $(call gb_CppunitTest_use_components,sc_dataprovider,\
chart2/source/controller/chartcontroller \ chart2/source/controller/chartcontroller \
comphelper/util/comphelp \ comphelper/util/comphelp \
configmgr/source/configmgr \ configmgr/source/configmgr \
connectivity/source/cpool/dbpool2 \
connectivity/source/drivers/hsqldb/hsqldb \
connectivity/source/drivers/dbase/dbase \
$(if $(ENABLE_FIREBIRD_SDBC),connectivity/source/drivers/firebird/firebird_sdbc) \
connectivity/source/drivers/jdbc/jdbc \
connectivity/source/manager/sdbc2 \
dbaccess/source/filter/xml/dbaxml \
dbaccess/util/dba \ dbaccess/util/dba \
dbaccess/util/dbu \
embeddedobj/util/embobj \ embeddedobj/util/embobj \
eventattacher/source/evtatt \ eventattacher/source/evtatt \
filter/source/config/cache/filterconfig1 \ filter/source/config/cache/filterconfig1 \
...@@ -91,6 +99,7 @@ $(eval $(call gb_CppunitTest_use_components,sc_dataprovider,\ ...@@ -91,6 +99,7 @@ $(eval $(call gb_CppunitTest_use_components,sc_dataprovider,\
i18npool/source/search/i18nsearch \ i18npool/source/search/i18nsearch \
i18npool/util/i18npool \ i18npool/util/i18npool \
linguistic/source/lng \ linguistic/source/lng \
extensions/source/logging/log \
oox/util/oox \ oox/util/oox \
package/source/xstor/xstor \ package/source/xstor/xstor \
package/util/package2 \ package/util/package2 \
......
...@@ -401,6 +401,7 @@ $(eval $(call gb_Library_add_exception_objects,sc,\ ...@@ -401,6 +401,7 @@ $(eval $(call gb_Library_add_exception_objects,sc,\
sc/source/ui/dataprovider/datatransformation \ sc/source/ui/dataprovider/datatransformation \
sc/source/ui/dataprovider/htmldataprovider \ sc/source/ui/dataprovider/htmldataprovider \
sc/source/ui/dataprovider/xmldataprovider \ sc/source/ui/dataprovider/xmldataprovider \
sc/source/ui/dataprovider/sqldataprovider \
sc/source/ui/dbgui/asciiopt \ sc/source/ui/dbgui/asciiopt \
sc/source/ui/dbgui/consdlg \ sc/source/ui/dbgui/consdlg \
sc/source/ui/dbgui/csvcontrol \ sc/source/ui/dbgui/csvcontrol \
......
...@@ -31,12 +31,14 @@ public: ...@@ -31,12 +31,14 @@ public:
void testDataLargerThanDB(); void testDataLargerThanDB();
void testHTMLImport(); void testHTMLImport();
void testXMLImport(); void testXMLImport();
// void testBaseImport();
CPPUNIT_TEST_SUITE(ScDataProvidersTest); CPPUNIT_TEST_SUITE(ScDataProvidersTest);
CPPUNIT_TEST(testCSVImport); CPPUNIT_TEST(testCSVImport);
CPPUNIT_TEST(testDataLargerThanDB); CPPUNIT_TEST(testDataLargerThanDB);
CPPUNIT_TEST(testHTMLImport); CPPUNIT_TEST(testHTMLImport);
CPPUNIT_TEST(testXMLImport); CPPUNIT_TEST(testXMLImport);
// CPPUNIT_TEST(testBaseImport);
CPPUNIT_TEST_SUITE_END(); CPPUNIT_TEST_SUITE_END();
private: private:
...@@ -180,6 +182,29 @@ void ScDataProvidersTest::testXMLImport() ...@@ -180,6 +182,29 @@ void ScDataProvidersTest::testXMLImport()
CPPUNIT_ASSERT_EQUAL(OUString("test4"), m_pDoc->GetString(1, 4, 0)); CPPUNIT_ASSERT_EQUAL(OUString("test4"), m_pDoc->GetString(1, 4, 0));
} }
/*
void ScDataProvidersTest::testBaseImport()
{
ScDBData* pDBData = new ScDBData("testDB", 0, 0, 0, 10, 10);
bool bInserted = m_pDoc->GetDBCollection()->getNamedDBs().insert(pDBData);
CPPUNIT_ASSERT(bInserted);
sc::ExternalDataSource aDataSource("~/dummy.file", "org.libreoffice.calc.sql", m_pDoc);
aDataSource.setDBData("testDB");
aDataSource.setID("biblio@Bibliography");
m_pDoc->GetExternalDataMapper().insertDataSource(aDataSource);
auto& rDataSources = m_pDoc->GetExternalDataMapper().getDataSources();
CPPUNIT_ASSERT(!rDataSources.empty());
rDataSources[0].refresh(m_pDoc, true);
Scheduler::ProcessEventsToIdle();
CPPUNIT_ASSERT_EQUAL(OUString("ARJ00"), m_pDoc->GetString(0, 0, 0));
CPPUNIT_ASSERT_EQUAL(OUString("AVV00"), m_pDoc->GetString(1, 1, 0));
}
*/
ScDataProvidersTest::ScDataProvidersTest() : ScDataProvidersTest::ScDataProvidersTest() :
ScBootstrapFixture( "sc/qa/unit/data/dataprovider" ), ScBootstrapFixture( "sc/qa/unit/data/dataprovider" ),
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "htmldataprovider.hxx" #include "htmldataprovider.hxx"
#include "xmldataprovider.hxx" #include "xmldataprovider.hxx"
#include "sqldataprovider.hxx"
#include <datatransformation.hxx> #include <datatransformation.hxx>
using namespace com::sun::star; using namespace com::sun::star;
...@@ -286,6 +287,8 @@ std::shared_ptr<DataProvider> DataProviderFactory::getDataProvider(ScDocument* p ...@@ -286,6 +287,8 @@ std::shared_ptr<DataProvider> DataProviderFactory::getDataProvider(ScDocument* p
return std::shared_ptr<DataProvider>(new HTMLDataProvider(pDoc, rDataSource)); return std::shared_ptr<DataProvider>(new HTMLDataProvider(pDoc, rDataSource));
else if (rDataProvider == "org.libreoffice.calc.xml") else if (rDataProvider == "org.libreoffice.calc.xml")
return std::shared_ptr<DataProvider>(new XMLDataProvider(pDoc, rDataSource)); return std::shared_ptr<DataProvider>(new XMLDataProvider(pDoc, rDataSource));
else if (rDataProvider == "org.libreoffice.calc.sql")
return std::shared_ptr<DataProvider>(new SQLDataProvider(pDoc, rDataSource));
} }
else else
{ {
...@@ -302,6 +305,7 @@ std::vector<OUString> DataProviderFactory::getDataProviders() ...@@ -302,6 +305,7 @@ std::vector<OUString> DataProviderFactory::getDataProviders()
aDataProviders.emplace_back("org.libreoffice.calc.csv"); aDataProviders.emplace_back("org.libreoffice.calc.csv");
aDataProviders.emplace_back("org.libreoffice.calc.html"); aDataProviders.emplace_back("org.libreoffice.calc.html");
aDataProviders.emplace_back("org.libreoffice.calc.xml"); aDataProviders.emplace_back("org.libreoffice.calc.xml");
aDataProviders.emplace_back("org.libreoffice.calc.sql");
return aDataProviders; return aDataProviders;
} }
......
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "sqldataprovider.hxx"
#include <datatransformation.hxx>
#include <salhelper/thread.hxx>
#include <com/sun/star/container/XNameAccess.hpp>
#include <com/sun/star/sheet/DataImportMode.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/sdbcx/XTablesSupplier.hpp>
#include <com/sun/star/sdb/DatabaseContext.hpp>
#include <com/sun/star/sdb/XQueriesSupplier.hpp>
#include <com/sun/star/sdb/XCompletedConnection.hpp>
#include <com/sun/star/task/InteractionHandler.hpp>
#include <com/sun/star/sdbc/XRow.hpp>
#include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
#include <com/sun/star/sdbc/XResultSetMetaData.hpp>
#include <com/sun/star/sdbc/ResultSetType.hpp>
#include <com/sun/star/sdbc/DataType.hpp>
#include <com/sun/star/util/Date.hpp>
#include <comphelper/string.hxx>
#include <miscuno.hxx>
#include <dbdocutl.hxx>
using namespace css;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::sdbc;
using namespace ::com::sun::star::uno;
namespace sc
{
class SQLFetchThread : public salhelper::Thread
{
ScDocument& mrDocument;
OUString maURL;
OUString maID;
const std::vector<std::shared_ptr<sc::DataTransformation>> maDataTransformations;
std::function<void()> maImportFinishedHdl;
public:
SQLFetchThread(ScDocument& rDoc, const OUString&, const OUString& rID,
std::function<void()> aImportFinishedHdl,
const std::vector<std::shared_ptr<sc::DataTransformation>>& rTransformations);
virtual void execute() override;
};
SQLFetchThread::SQLFetchThread(
ScDocument& rDoc, const OUString& rURL, const OUString& rID,
std::function<void()> aImportFinishedHdl,
const std::vector<std::shared_ptr<sc::DataTransformation>>& rTransformations)
: salhelper::Thread("SQL Fetch Thread")
, mrDocument(rDoc)
, maURL(rURL)
, maID(rID)
, maDataTransformations(rTransformations)
, maImportFinishedHdl(aImportFinishedHdl)
{
}
void SQLFetchThread::execute()
{
sal_Int32 nIndex = maID.indexOf("@");
if (nIndex == -1)
return;
OUString aTable = maID.copy(0, nIndex);
OUString aDatabase = maID.copy(nIndex + 1);
try
{
uno::Reference<sdb::XDatabaseContext> xContext
= sdb::DatabaseContext::create(comphelper::getProcessComponentContext());
uno::Any aSourceAny = xContext->getByName(aDatabase);
uno::Reference<sdb::XCompletedConnection> xSource(
ScUnoHelpFunctions::AnyToInterface(aSourceAny), uno::UNO_QUERY);
if (!xSource.is())
return;
uno::Reference<task::XInteractionHandler> xHandler(
task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(),
nullptr),
uno::UNO_QUERY_THROW);
uno::Reference<sdbc::XConnection> xConnection = xSource->connectWithCompletion(xHandler);
uno::Reference<sdbc::XStatement> xStatement = xConnection->createStatement();
uno::Reference<sdbc::XResultSet> xResult
= xStatement->executeQuery("SELECT * FROM " + aTable);
if (xResult.is())
{
Reference<sdbc::XResultSetMetaDataSupplier> xMetaDataSupplier(xResult, UNO_QUERY);
Reference<sdbc::XResultSetMetaData> xMetaData = xMetaDataSupplier->getMetaData();
Reference<XRow> xRow(xResult, UNO_QUERY);
SCCOL nColCount = static_cast<SCCOL>(xMetaData->getColumnCount());
while (xResult->next())
{
SCROW nRow = static_cast<SCROW>(xResult->getRow());
for (SCCOL nCol = 0; nCol < nColCount; nCol++)
{
ScDatabaseDocUtil::PutData(&mrDocument, nCol, nRow - 1, 0, xRow, nCol + 1,
xMetaData->getColumnType(nCol + 1), false);
}
}
}
}
catch (uno::Exception&)
{
OSL_FAIL("exception in database");
}
for (auto& itr : maDataTransformations)
{
itr->Transform(mrDocument);
}
SolarMutexGuard aGuard;
maImportFinishedHdl();
}
SQLDataProvider::SQLDataProvider(ScDocument* pDoc, sc::ExternalDataSource& rDataSource)
: DataProvider(rDataSource)
, mpDocument(pDoc)
{
}
SQLDataProvider::~SQLDataProvider()
{
if (mxSQLFetchThread.is())
{
SolarMutexReleaser aReleaser;
mxSQLFetchThread->join();
}
}
void SQLDataProvider::Import()
{
// already importing data
if (mpDoc)
return;
mpDoc.reset(new ScDocument(SCDOCMODE_CLIP));
mpDoc->ResetClip(mpDocument, SCTAB(0));
mxSQLFetchThread = new SQLFetchThread(*mpDoc, mrDataSource.getURL(), mrDataSource.getID(),
std::bind(&SQLDataProvider::ImportFinished, this),
mrDataSource.getDataTransformation());
mxSQLFetchThread->launch();
if (mbDeterministic)
{
SolarMutexReleaser aReleaser;
mxSQLFetchThread->join();
}
}
void SQLDataProvider::ImportFinished()
{
mrDataSource.getDBManager()->WriteToDoc(*mpDoc);
mxSQLFetchThread.clear();
mpDoc.reset();
}
const OUString& SQLDataProvider::GetURL() const { return mrDataSource.getURL(); }
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#ifndef INCLUDED_SC_SOURCE_UI_DATAPROVIDER_SQLDATAPROVIDER_HXX
#define INCLUDED_SC_SOURCE_UI_DATAPROVIDER_SQLDATAPROVIDER_HXX
#include <dataprovider.hxx>
namespace sc
{
class SQLFetchThread;
class DataTransformation;
class SQLDataProvider : public DataProvider
{
private:
ScDocument* mpDocument;
rtl::Reference<SQLFetchThread> mxSQLFetchThread;
ScDocumentUniquePtr mpDoc;
public:
SQLDataProvider(ScDocument* pDoc, sc::ExternalDataSource& rDataSource);
virtual ~SQLDataProvider() override;
virtual void Import() override;
virtual const OUString& GetURL() const override;
void ImportFinished();
};
}
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
...@@ -14,6 +14,9 @@ $(eval $(call gb_Package_set_outdir,test_unittest,$(WORKDIR))) ...@@ -14,6 +14,9 @@ $(eval $(call gb_Package_set_outdir,test_unittest,$(WORKDIR)))
$(eval $(call gb_Package_add_file,test_unittest,unittest/registry/modifications.xcd,registry/modifications.xcd)) $(eval $(call gb_Package_add_file,test_unittest,unittest/registry/modifications.xcd,registry/modifications.xcd))
$(eval $(call gb_Package_add_file,test_unittest,unittest/user/wordbook/sl.dic,user/wordbook/sl.dic)) $(eval $(call gb_Package_add_file,test_unittest,unittest/user/wordbook/sl.dic,user/wordbook/sl.dic))
$(eval $(call gb_Package_add_file,test_unittest,unittest/user/wordbook/en-US.dic,user/wordbook/en-US.dic)) $(eval $(call gb_Package_add_file,test_unittest,unittest/user/wordbook/en-US.dic,user/wordbook/en-US.dic))
$(eval $(call gb_Package_add_file,test_unittest,unittest/user/database/biblio.odb,user/database/biblio.odb))
$(eval $(call gb_Package_add_file,test_unittest,unittest/user/database/biblio/biblio.dbf,user/database/biblio/biblio.dbf))
$(eval $(call gb_Package_add_file,test_unittest,unittest/user/database/biblio/biblio.dbt,user/database/biblio/biblio.dbt))
$(eval $(call gb_Package_add_file,test_unittest,unittest/user/wordbook/technical.dic,user/wordbook/technical.dic)) $(eval $(call gb_Package_add_file,test_unittest,unittest/user/wordbook/technical.dic,user/wordbook/technical.dic))
$(eval $(call gb_Package_add_file,test_unittest,unittest/user/wordbook/en-GB.dic,user/wordbook/en-GB.dic)) $(eval $(call gb_Package_add_file,test_unittest,unittest/user/wordbook/en-GB.dic,user/wordbook/en-GB.dic))
$(eval $(call gb_Package_add_file,test_unittest,unittest/user/autotext/en-US/template.bau,user/autotext/en-US/template.bau)) $(eval $(call gb_Package_add_file,test_unittest,unittest/user/autotext/en-US/template.bau,user/autotext/en-US/template.bau))
......
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