| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | /* |
| 3 | * This file is part of the LibreOffice project. |
| 4 | * |
| 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 | */ |
| 9 | |
| 10 | #include "sqldataprovider.hxx" |
| 11 | #include <datatransformation.hxx> |
| 12 | #include <salhelper/thread.hxx> |
| 13 | #include <com/sun/star/sdb/DatabaseContext.hpp> |
| 14 | #include <com/sun/star/sdb/XCompletedConnection.hpp> |
| 15 | #include <com/sun/star/task/InteractionHandler.hpp> |
| 16 | #include <com/sun/star/sdbc/XRow.hpp> |
| 17 | #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp> |
| 18 | #include <com/sun/star/sdbc/XResultSetMetaData.hpp> |
| 19 | #include <dbdocutl.hxx> |
| 20 | #include <datamapper.hxx> |
| 21 | #include <utility> |
| 22 | #include <vcl/svapp.hxx> |
| 23 | #include <comphelper/processfactory.hxx> |
| 24 | #include <comphelper/diagnose_ex.hxx> |
| 25 | |
| 26 | using namespace css; |
| 27 | using namespace ::com::sun::star::sdbc; |
| 28 | using namespace ::com::sun::star::uno; |
| 29 | |
| 30 | namespace sc |
| 31 | { |
| 32 | class SQLFetchThread : public salhelper::Thread |
| 33 | { |
| 34 | ScDocument& mrDocument; |
| 35 | OUString maID; |
| 36 | const std::vector<std::shared_ptr<sc::DataTransformation>> maDataTransformations; |
| 37 | std::function<void()> maImportFinishedHdl; |
| 38 | |
| 39 | public: |
| 40 | SQLFetchThread(ScDocument& rDoc, const OUString& rID, std::function<void()> aImportFinishedHdl, |
| 41 | std::vector<std::shared_ptr<sc::DataTransformation>>&& rTransformations); |
| 42 | |
| 43 | virtual void execute() override; |
| 44 | }; |
| 45 | |
| 46 | SQLFetchThread::SQLFetchThread( |
| 47 | ScDocument& rDoc, const OUString& rID, std::function<void()> aImportFinishedHdl, |
| 48 | std::vector<std::shared_ptr<sc::DataTransformation>>&& rTransformations) |
| 49 | : salhelper::Thread("SQL Fetch Thread") |
| 50 | , mrDocument(rDoc) |
| 51 | , maID(rID) |
| 52 | , maDataTransformations(std::move(rTransformations)) |
| 53 | , maImportFinishedHdl(std::move(aImportFinishedHdl)) |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | void SQLFetchThread::execute() |
| 58 | { |
| 59 | sal_Int32 nIndex = maID.indexOf("@"); |
| 60 | if (nIndex == -1) |
| 61 | return; |
| 62 | |
| 63 | OUString aTable = maID.copy(0, nIndex); |
| 64 | OUString aDatabase = maID.copy(nIndex + 1); |
| 65 | |
| 66 | try |
| 67 | { |
| 68 | uno::Reference<sdb::XDatabaseContext> xContext |
| 69 | = sdb::DatabaseContext::create(comphelper::getProcessComponentContext()); |
| 70 | uno::Any aSourceAny = xContext->getByName(aDatabase); |
| 71 | |
| 72 | uno::Reference<sdb::XCompletedConnection> xSource(aSourceAny, uno::UNO_QUERY); |
| 73 | if (!xSource.is()) |
| 74 | return; |
| 75 | |
| 76 | uno::Reference<task::XInteractionHandler> xHandler( |
| 77 | task::InteractionHandler::createWithParent(comphelper::getProcessComponentContext(), |
| 78 | nullptr), |
| 79 | uno::UNO_QUERY_THROW); |
| 80 | |
| 81 | uno::Reference<sdbc::XConnection> xConnection = xSource->connectWithCompletion(xHandler); |
| 82 | |
| 83 | uno::Reference<sdbc::XStatement> xStatement = xConnection->createStatement(); |
| 84 | |
| 85 | uno::Reference<sdbc::XResultSet> xResult |
| 86 | = xStatement->executeQuery("SELECT * FROM " + aTable); |
| 87 | |
| 88 | if (xResult.is()) |
| 89 | { |
| 90 | Reference<sdbc::XResultSetMetaDataSupplier> xMetaDataSupplier(xResult, UNO_QUERY); |
| 91 | |
| 92 | Reference<sdbc::XResultSetMetaData> xMetaData = xMetaDataSupplier->getMetaData(); |
| 93 | |
| 94 | Reference<XRow> xRow(xResult, UNO_QUERY); |
| 95 | |
| 96 | SCCOL nColCount = static_cast<SCCOL>(xMetaData->getColumnCount()); |
| 97 | |
| 98 | while (xResult->next()) |
| 99 | { |
| 100 | SCROW nRow = static_cast<SCROW>(xResult->getRow()); |
| 101 | |
| 102 | for (SCCOL nCol = 0; nCol < nColCount; nCol++) |
| 103 | { |
| 104 | ScDatabaseDocUtil::PutData(mrDocument, nCol, nRow - 1, 0, xRow, nCol + 1, |
| 105 | xMetaData->getColumnType(nCol + 1), false); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | catch (uno::Exception&) |
| 111 | { |
| 112 | TOOLS_WARN_EXCEPTION("sc", "exception in database"); |
| 113 | } |
| 114 | |
| 115 | for (auto& itr : maDataTransformations) |
| 116 | { |
| 117 | itr->Transform(mrDocument); |
| 118 | } |
| 119 | |
| 120 | SolarMutexGuard aGuard; |
| 121 | maImportFinishedHdl(); |
| 122 | } |
| 123 | |
| 124 | SQLDataProvider::SQLDataProvider(ScDocument* pDoc, sc::ExternalDataSource& rDataSource) |
| 125 | : DataProvider(rDataSource) |
| 126 | , mpDocument(pDoc) |
| 127 | { |
| 128 | } |
| 129 | |
| 130 | SQLDataProvider::~SQLDataProvider() |
| 131 | { |
| 132 | if (mxSQLFetchThread.is()) |
| 133 | { |
| 134 | SolarMutexReleaser aReleaser; |
| 135 | mxSQLFetchThread->join(); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | void SQLDataProvider::Import() |
| 140 | { |
| 141 | // already importing data |
| 142 | if (mpDoc) |
| 143 | return; |
| 144 | |
| 145 | mpDoc.reset(new ScDocument(SCDOCMODE_CLIP)); |
| 146 | mpDoc->ResetClip(mpDocument, SCTAB(0)); |
| 147 | mxSQLFetchThread |
| 148 | = new SQLFetchThread(*mpDoc, mrDataSource.getID(), [this]() { this->ImportFinished(); }, |
| 149 | std::vector(mrDataSource.getDataTransformation())); |
| 150 | |
| 151 | if (mbDeterministic) |
| 152 | { |
| 153 | SolarMutexReleaser aReleaser; |
| 154 | mxSQLFetchThread->execute(); |
| 155 | } |
| 156 | else |
| 157 | { |
| 158 | mxSQLFetchThread->launch(); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | void SQLDataProvider::ImportFinished() |
| 163 | { |
| 164 | mrDataSource.getDBManager()->WriteToDoc(*mpDoc); |
| 165 | mxSQLFetchThread.clear(); |
| 166 | mpDoc.reset(); |
| 167 | } |
| 168 | |
| 169 | const OUString& SQLDataProvider::GetURL() const { return mrDataSource.getURL(); } |
| 170 | } |
| 171 | |
| 172 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |