diff options
author | Vikas <vikasmahato0@gmail.com> | 2018-07-14 19:35:35 +0530 |
---|---|---|
committer | Markus Mohrhard <markus.mohrhard@googlemail.com> | 2018-09-28 20:34:38 +0200 |
commit | 278b1de21f8395ab2a6c49377cf4aec4c16f05c6 (patch) | |
tree | 333bc2684a6a8173b5190fbccdbc90f036dfa09e /sc | |
parent | c494273adea0293bbcfc022adc0ea13d5c21e18d (diff) |
Added base as a data provider
Change-Id: Iaf23b70f303cd05bcf6450fed1246de894285526
Reviewed-on: https://gerrit.libreoffice.org/57447
Tested-by: Jenkins
Reviewed-by: Markus Mohrhard <markus.mohrhard@googlemail.com>
Diffstat (limited to 'sc')
-rw-r--r-- | sc/CppunitTest_sc_dataprovider.mk | 9 | ||||
-rw-r--r-- | sc/Library_sc.mk | 1 | ||||
-rw-r--r-- | sc/qa/unit/dataproviders_test.cxx | 25 | ||||
-rw-r--r-- | sc/source/ui/dataprovider/dataprovider.cxx | 4 | ||||
-rw-r--r-- | sc/source/ui/dataprovider/sqldataprovider.cxx | 180 | ||||
-rw-r--r-- | sc/source/ui/dataprovider/sqldataprovider.hxx | 42 |
6 files changed, 261 insertions, 0 deletions
diff --git a/sc/CppunitTest_sc_dataprovider.mk b/sc/CppunitTest_sc_dataprovider.mk index 71fa6ea30fed..8c449084ee8d 100644 --- a/sc/CppunitTest_sc_dataprovider.mk +++ b/sc/CppunitTest_sc_dataprovider.mk @@ -82,7 +82,15 @@ $(eval $(call gb_CppunitTest_use_components,sc_dataprovider,\ chart2/source/controller/chartcontroller \ comphelper/util/comphelp \ 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/dbu \ embeddedobj/util/embobj \ eventattacher/source/evtatt \ filter/source/config/cache/filterconfig1 \ @@ -91,6 +99,7 @@ $(eval $(call gb_CppunitTest_use_components,sc_dataprovider,\ i18npool/source/search/i18nsearch \ i18npool/util/i18npool \ linguistic/source/lng \ + extensions/source/logging/log \ oox/util/oox \ package/source/xstor/xstor \ package/util/package2 \ diff --git a/sc/Library_sc.mk b/sc/Library_sc.mk index dc2a2149f0a1..5695e729ffbe 100644 --- a/sc/Library_sc.mk +++ b/sc/Library_sc.mk @@ -401,6 +401,7 @@ $(eval $(call gb_Library_add_exception_objects,sc,\ sc/source/ui/dataprovider/datatransformation \ sc/source/ui/dataprovider/htmldataprovider \ sc/source/ui/dataprovider/xmldataprovider \ + sc/source/ui/dataprovider/sqldataprovider \ sc/source/ui/dbgui/asciiopt \ sc/source/ui/dbgui/consdlg \ sc/source/ui/dbgui/csvcontrol \ diff --git a/sc/qa/unit/dataproviders_test.cxx b/sc/qa/unit/dataproviders_test.cxx index aef3ec6abb05..e8da21fa0b2d 100644 --- a/sc/qa/unit/dataproviders_test.cxx +++ b/sc/qa/unit/dataproviders_test.cxx @@ -31,12 +31,14 @@ public: void testDataLargerThanDB(); void testHTMLImport(); void testXMLImport(); + // void testBaseImport(); CPPUNIT_TEST_SUITE(ScDataProvidersTest); CPPUNIT_TEST(testCSVImport); CPPUNIT_TEST(testDataLargerThanDB); CPPUNIT_TEST(testHTMLImport); CPPUNIT_TEST(testXMLImport); + // CPPUNIT_TEST(testBaseImport); CPPUNIT_TEST_SUITE_END(); private: @@ -180,6 +182,29 @@ void ScDataProvidersTest::testXMLImport() 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() : ScBootstrapFixture( "sc/qa/unit/data/dataprovider" ), diff --git a/sc/source/ui/dataprovider/dataprovider.cxx b/sc/source/ui/dataprovider/dataprovider.cxx index 01408c778fa9..6a7016fd43f6 100644 --- a/sc/source/ui/dataprovider/dataprovider.cxx +++ b/sc/source/ui/dataprovider/dataprovider.cxx @@ -17,6 +17,7 @@ #include "htmldataprovider.hxx" #include "xmldataprovider.hxx" +#include "sqldataprovider.hxx" #include <datatransformation.hxx> using namespace com::sun::star; @@ -286,6 +287,8 @@ std::shared_ptr<DataProvider> DataProviderFactory::getDataProvider(ScDocument* p return std::shared_ptr<DataProvider>(new HTMLDataProvider(pDoc, rDataSource)); else if (rDataProvider == "org.libreoffice.calc.xml") 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 { @@ -302,6 +305,7 @@ std::vector<OUString> DataProviderFactory::getDataProviders() aDataProviders.emplace_back("org.libreoffice.calc.csv"); aDataProviders.emplace_back("org.libreoffice.calc.html"); aDataProviders.emplace_back("org.libreoffice.calc.xml"); + aDataProviders.emplace_back("org.libreoffice.calc.sql"); return aDataProviders; } diff --git a/sc/source/ui/dataprovider/sqldataprovider.cxx b/sc/source/ui/dataprovider/sqldataprovider.cxx new file mode 100644 index 000000000000..014dff82af82 --- /dev/null +++ b/sc/source/ui/dataprovider/sqldataprovider.cxx @@ -0,0 +1,180 @@ +/* -*- 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: */ diff --git a/sc/source/ui/dataprovider/sqldataprovider.hxx b/sc/source/ui/dataprovider/sqldataprovider.hxx new file mode 100644 index 000000000000..a79930141ec1 --- /dev/null +++ b/sc/source/ui/dataprovider/sqldataprovider.hxx @@ -0,0 +1,42 @@ +/* -*- 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: */ |