[Libreoffice-commits] core.git: connectivity/Library_writer.mk connectivity/source

Miklos Vajna vmiklos at collabora.co.uk
Mon Jul 17 15:32:38 UTC 2017


 connectivity/Library_writer.mk                           |    1 
 connectivity/source/drivers/writer/WConnection.cxx       |    4 
 connectivity/source/drivers/writer/WDatabaseMetaData.cxx |  325 +++++++++++++++
 connectivity/source/inc/writer/WDatabaseMetaData.hxx     |   52 ++
 4 files changed, 381 insertions(+), 1 deletion(-)

New commits:
commit d7ac239793905564d2754edc52611930b6ba2cdc
Author: Miklos Vajna <vmiklos at collabora.co.uk>
Date:   Mon Jul 17 12:24:53 2017 +0200

    connectivity writer driver: initial DatabaseMetaData implementation
    
    With this the list of tables is visible in Data Sources.
    
    Change-Id: I7ee4955c54a76c57f854dba19fd1a6a8ee606089
    Reviewed-on: https://gerrit.libreoffice.org/40056
    Tested-by: Jenkins <ci at libreoffice.org>
    Reviewed-by: Miklos Vajna <vmiklos at collabora.co.uk>

diff --git a/connectivity/Library_writer.mk b/connectivity/Library_writer.mk
index 95ae50cfcad6..07304afebc3e 100644
--- a/connectivity/Library_writer.mk
+++ b/connectivity/Library_writer.mk
@@ -38,6 +38,7 @@ $(eval $(call gb_Library_use_libraries,writer,\
 
 $(eval $(call gb_Library_add_exception_objects,writer,\
 	connectivity/source/drivers/writer/WConnection \
+	connectivity/source/drivers/writer/WDatabaseMetaData \
 	connectivity/source/drivers/writer/WDriver \
 	connectivity/source/drivers/writer/Wservices \
 ))
diff --git a/connectivity/source/drivers/writer/WConnection.cxx b/connectivity/source/drivers/writer/WConnection.cxx
index 212e29400bea..836f664e5346 100644
--- a/connectivity/source/drivers/writer/WConnection.cxx
+++ b/connectivity/source/drivers/writer/WConnection.cxx
@@ -18,6 +18,7 @@
  */
 
 #include "writer/WConnection.hxx"
+#include "writer/WDatabaseMetaData.hxx"
 #include "writer/WDriver.hxx"
 #include "resource/sharedresources.hxx"
 #include "resource/common_res.hrc"
@@ -194,7 +195,8 @@ uno::Reference< sdbc::XDatabaseMetaData > SAL_CALL OWriterConnection::getMetaDat
     uno::Reference< sdbc::XDatabaseMetaData > xMetaData = m_xMetaData;
     if (!xMetaData.is())
     {
-        SAL_WARN("connectivity.writer", "TODO implement OWriterConnection::getMetaData()");
+        xMetaData = new OWriterDatabaseMetaData(this);
+        m_xMetaData = xMetaData;
     }
 
     return xMetaData;
diff --git a/connectivity/source/drivers/writer/WDatabaseMetaData.cxx b/connectivity/source/drivers/writer/WDatabaseMetaData.cxx
new file mode 100644
index 000000000000..66104f0a0e9b
--- /dev/null
+++ b/connectivity/source/drivers/writer/WDatabaseMetaData.cxx
@@ -0,0 +1,325 @@
+/* -*- 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/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ *   Licensed to the Apache Software Foundation (ASF) under one or more
+ *   contributor license agreements. See the NOTICE file distributed
+ *   with this work for additional information regarding copyright
+ *   ownership. The ASF licenses this file to you under the Apache
+ *   License, Version 2.0 (the "License"); you may not use this file
+ *   except in compliance with the License. You may obtain a copy of
+ *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#include "writer/WDatabaseMetaData.hxx"
+#include "writer/WConnection.hxx"
+#include <com/sun/star/sdbc/DataType.hpp>
+#include <com/sun/star/sdbc/ResultSetType.hpp>
+#include <com/sun/star/sdbc/ColumnValue.hpp>
+#include <com/sun/star/beans/XPropertySet.hpp>
+#include <com/sun/star/sdbc/ResultSetConcurrency.hpp>
+#include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
+#include <com/sun/star/sdbcx/XIndexesSupplier.hpp>
+#include <com/sun/star/text/XTextDocument.hpp>
+#include <com/sun/star/text/XTextTablesSupplier.hpp>
+#include "FDatabaseMetaDataResultSet.hxx"
+#include <com/sun/star/lang/XUnoTunnel.hpp>
+#include <comphelper/types.hxx>
+
+using namespace connectivity::file;
+using namespace ::com::sun::star;
+
+namespace connectivity
+{
+namespace writer
+{
+
+OWriterDatabaseMetaData::OWriterDatabaseMetaData(OConnection* pConnection) :ODatabaseMetaData(pConnection)
+{
+}
+
+OWriterDatabaseMetaData::~OWriterDatabaseMetaData()
+{
+}
+
+uno::Reference<sdbc::XResultSet> OWriterDatabaseMetaData::impl_getTypeInfo_throw()
+{
+    ::osl::MutexGuard aGuard(m_aMutex);
+
+    ODatabaseMetaDataResultSet* pResult = new ODatabaseMetaDataResultSet(::connectivity::ODatabaseMetaDataResultSet::eTypeInfo);
+    uno::Reference<sdbc::XResultSet> xRef = pResult;
+
+    static ODatabaseMetaDataResultSet::ORows aRows;
+    if (aRows.empty())
+    {
+        ODatabaseMetaDataResultSet::ORow aRow;
+
+        aRows.reserve(6);
+        aRow.reserve(18);
+
+        aRow.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
+        aRow.push_back(new ORowSetValueDecorator(OUString("VARCHAR")));
+        aRow.push_back(new ORowSetValueDecorator(sdbc::DataType::VARCHAR));
+        aRow.push_back(new ORowSetValueDecorator((sal_Int32)65535));
+        aRow.push_back(ODatabaseMetaDataResultSet::getQuoteValue());
+        aRow.push_back(ODatabaseMetaDataResultSet::getQuoteValue());
+        aRow.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
+        aRow.push_back(ODatabaseMetaDataResultSet::get1Value()); // ORowSetValue((sal_Int32)ColumnValue::NULLABLE)
+        aRow.push_back(ODatabaseMetaDataResultSet::get1Value());
+        aRow.push_back(new ORowSetValueDecorator((sal_Int32)sdbc::ColumnSearch::CHAR));
+        aRow.push_back(ODatabaseMetaDataResultSet::get1Value());
+        aRow.push_back(ODatabaseMetaDataResultSet::get0Value());
+        aRow.push_back(ODatabaseMetaDataResultSet::get0Value());
+        aRow.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
+        aRow.push_back(ODatabaseMetaDataResultSet::get0Value());
+        aRow.push_back(ODatabaseMetaDataResultSet::get0Value());
+        aRow.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
+        aRow.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
+        aRow.push_back(new ORowSetValueDecorator((sal_Int32)10));
+        aRows.push_back(aRow);
+
+        aRow[1] = new ORowSetValueDecorator(OUString("DECIMAL"));
+        aRow[2] = new ORowSetValueDecorator(sdbc::DataType::DECIMAL);
+        aRow[3] = ODatabaseMetaDataResultSet::get0Value();
+        aRow[9] = ODatabaseMetaDataResultSet::getBasicValue();
+        aRow[15] = ODatabaseMetaDataResultSet::get0Value();
+        aRows.push_back(aRow);
+
+        aRow[1] = new ORowSetValueDecorator(OUString("BOOL"));
+        aRow[2] = new ORowSetValueDecorator(sdbc::DataType::BIT);
+        aRow[3] = new ORowSetValueDecorator((sal_Int32)20);
+        aRow[9] = ODatabaseMetaDataResultSet::getBasicValue();
+        aRow[15] = new ORowSetValueDecorator((sal_Int32)15);
+        aRows.push_back(aRow);
+
+        aRow[1] = new ORowSetValueDecorator(OUString("DATE"));
+        aRow[2] = new ORowSetValueDecorator(sdbc::DataType::DATE);
+        aRow[3] = ODatabaseMetaDataResultSet::get0Value();
+        aRow[9] = ODatabaseMetaDataResultSet::getBasicValue();
+        aRow[15] = ODatabaseMetaDataResultSet::get0Value();
+        aRows.push_back(aRow);
+
+        aRow[1] = new ORowSetValueDecorator(OUString("TIME"));
+        aRow[2] = new ORowSetValueDecorator(sdbc::DataType::TIME);
+        aRow[3] = ODatabaseMetaDataResultSet::get0Value();
+        aRow[9] = ODatabaseMetaDataResultSet::getBasicValue();
+        aRow[15] = ODatabaseMetaDataResultSet::get0Value();
+        aRows.push_back(aRow);
+
+        aRow[1] = new ORowSetValueDecorator(OUString("TIMESTAMP"));
+        aRow[2] = new ORowSetValueDecorator(sdbc::DataType::TIMESTAMP);
+        aRow[3] = ODatabaseMetaDataResultSet::get0Value();
+        aRow[9] = ODatabaseMetaDataResultSet::getBasicValue();
+        aRow[15] = ODatabaseMetaDataResultSet::get0Value();
+        aRows.push_back(aRow);
+    }
+
+    pResult->setRows(aRows);
+    return xRef;
+}
+
+
+uno::Reference<sdbc::XResultSet> SAL_CALL OWriterDatabaseMetaData::getColumns(
+    const uno::Any& /*catalog*/, const OUString& /*schemaPattern*/, const OUString& tableNamePattern,
+    const OUString& columnNamePattern)
+{
+    ::osl::MutexGuard aGuard(m_aMutex);
+
+    uno::Reference<sdbcx::XTablesSupplier> xTables = m_pConnection->createCatalog();
+    if (!xTables.is())
+        throw sdbc::SQLException();
+
+    uno::Reference<container::XNameAccess> xNames = xTables->getTables();
+    if (!xNames.is())
+        throw sdbc::SQLException();
+
+    ODatabaseMetaDataResultSet::ORows aRows;
+    ODatabaseMetaDataResultSet::ORow  aRow(19);
+
+    aRow[10] = new ORowSetValueDecorator((sal_Int32)10);
+
+    uno::Sequence<OUString> aTabNames(xNames->getElementNames());
+    const OUString* pTabIter = aTabNames.getConstArray();
+    const OUString* pTabEnd      = pTabIter + aTabNames.getLength();
+    for (; pTabIter != pTabEnd; ++pTabIter)
+    {
+        if (match(tableNamePattern,*pTabIter,'\0'))
+        {
+            uno::Reference<sdbcx::XColumnsSupplier> xTable(xNames->getByName(*pTabIter), uno::UNO_QUERY_THROW);
+            aRow[3] = new ORowSetValueDecorator(*pTabIter);
+
+            uno::Reference<container::XNameAccess> xColumns = xTable->getColumns();
+            if (!xColumns.is())
+                throw sdbc::SQLException();
+
+            uno::Sequence<OUString> aColNames(xColumns->getElementNames());
+
+            const OUString* pColumnIter = aColNames.getConstArray();
+            const OUString* pEnd = pColumnIter + aColNames.getLength();
+            uno::Reference<beans::XPropertySet> xColumn;
+            for (sal_Int32 i=1; pColumnIter != pEnd; ++pColumnIter,++i)
+            {
+                if (match(columnNamePattern,*pColumnIter,'\0'))
+                {
+                    aRow[4]  = new ORowSetValueDecorator(*pColumnIter);
+
+                    xColumns->getByName(*pColumnIter) >>= xColumn;
+                    OSL_ENSURE(xColumn.is(),"Columns contains a column who isn't a fastpropertyset!");
+                    aRow[5] = new ORowSetValueDecorator(::comphelper::getINT32(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE))));
+                    aRow[6] = new ORowSetValueDecorator(::comphelper::getString(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPENAME))));
+                    aRow[7] = new ORowSetValueDecorator(::comphelper::getINT32(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_PRECISION))));
+                    //  aRow[8] = xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPENAME));
+                    aRow[9] = new ORowSetValueDecorator(::comphelper::getINT32(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCALE))));
+                    aRow[11] = new ORowSetValueDecorator(::comphelper::getINT32(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISNULLABLE))));
+                    //  aRow[12] = xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPENAME));
+                    aRow[13] = new ORowSetValueDecorator(::comphelper::getString(xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DEFAULTVALUE))));
+                    //  aRow[14] = xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPENAME));
+                    //  aRow[15] = xColumn->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPENAME));
+                    switch (sal_Int32(aRow[5]->getValue()))
+                    {
+                    case sdbc::DataType::CHAR:
+                    case sdbc::DataType::VARCHAR:
+                        aRow[16] = new ORowSetValueDecorator((sal_Int32)254);
+                        break;
+                    case sdbc::DataType::LONGVARCHAR:
+                        aRow[16] = new ORowSetValueDecorator((sal_Int32)65535);
+                        break;
+                    default:
+                        aRow[16] = new ORowSetValueDecorator((sal_Int32)0);
+                    }
+                    aRow[17] = new ORowSetValueDecorator(i);
+                    switch (sal_Int32(aRow[11]->getValue()))
+                    {
+                    case sdbc::ColumnValue::NO_NULLS:
+                        aRow[18]  = new ORowSetValueDecorator(OUString("NO"));
+                        break;
+                    case sdbc::ColumnValue::NULLABLE:
+                        aRow[18]  = new ORowSetValueDecorator(OUString("YES"));
+                        break;
+                    default:
+                        aRow[18]  = new ORowSetValueDecorator(OUString());
+                    }
+                    aRows.push_back(aRow);
+                }
+            }
+        }
+    }
+
+    ODatabaseMetaDataResultSet* pResult = new ODatabaseMetaDataResultSet(::connectivity::ODatabaseMetaDataResultSet::eColumns);
+    uno::Reference<sdbc::XResultSet> xRef = pResult;
+    pResult->setRows(aRows);
+
+    return xRef;
+}
+
+
+OUString SAL_CALL OWriterDatabaseMetaData::getURL()
+{
+    ::osl::MutexGuard aGuard(m_aMutex);
+
+    return "sdbc:writer:" + m_pConnection->getURL();
+}
+
+
+sal_Int32 SAL_CALL OWriterDatabaseMetaData::getMaxBinaryLiteralLength()
+{
+    return SAL_MAX_INT32;
+}
+
+
+sal_Int32 SAL_CALL OWriterDatabaseMetaData::getMaxCharLiteralLength()
+{
+    return SAL_MAX_INT32;
+}
+
+sal_Int32 SAL_CALL OWriterDatabaseMetaData::getMaxColumnNameLength()
+{
+    return SAL_MAX_INT32;
+}
+
+sal_Int32 SAL_CALL OWriterDatabaseMetaData::getMaxColumnsInIndex()
+{
+    return 1;
+}
+
+sal_Int32 SAL_CALL OWriterDatabaseMetaData::getMaxColumnsInTable()
+{
+    return 256;
+}
+
+uno::Reference<sdbc::XResultSet> SAL_CALL OWriterDatabaseMetaData::getTables(
+    const uno::Any& /*catalog*/, const OUString& /*schemaPattern*/,
+    const OUString& tableNamePattern, const uno::Sequence< OUString >& types)
+{
+    ::osl::MutexGuard aGuard(m_aMutex);
+
+    ODatabaseMetaDataResultSet* pResult = new ODatabaseMetaDataResultSet(ODatabaseMetaDataResultSet::eTables);
+    uno::Reference<sdbc::XResultSet> xRef = pResult;
+
+    // check if ORowSetValue type is given
+    // when no types are given then we have to return all tables e.g. TABLE
+
+    OUString aTable("TABLE");
+
+    bool bTableFound = true;
+    sal_Int32 nLength = types.getLength();
+    if (nLength)
+    {
+        bTableFound = false;
+
+        const OUString* pIter = types.getConstArray();
+        const OUString* pEnd = pIter + nLength;
+        for (; pIter != pEnd; ++pIter)
+        {
+            if (*pIter == aTable)
+            {
+                bTableFound = true;
+                break;
+            }
+        }
+    }
+    if (!bTableFound)
+        return xRef;
+
+    // get the table names from the document
+
+    OWriterConnection::ODocHolder aDocHolder(static_cast<OWriterConnection*>(m_pConnection));
+    uno::Reference<text::XTextTablesSupplier> xDoc(aDocHolder.getDoc(), uno::UNO_QUERY);
+    if (!xDoc.is())
+        throw sdbc::SQLException();
+    uno::Reference<container::XNameAccess> xTables = xDoc->getTextTables();
+    if (!xTables.is())
+        throw sdbc::SQLException();
+    uno::Sequence<OUString> aTableNames = xTables->getElementNames();
+
+    ODatabaseMetaDataResultSet::ORows aRows;
+    sal_Int32 nTableCount = aTableNames.getLength();
+    for (sal_Int32 nTable=0; nTable<nTableCount; nTable++)
+    {
+        OUString aName = aTableNames[nTable];
+        if (match(tableNamePattern,aName,'\0'))
+        {
+            ODatabaseMetaDataResultSet::ORow aRow { nullptr, nullptr, nullptr };
+            aRow.reserve(6);
+            aRow.push_back(new ORowSetValueDecorator(aName));
+            aRow.push_back(new ORowSetValueDecorator(aTable));
+            aRow.push_back(ODatabaseMetaDataResultSet::getEmptyValue());
+            aRows.push_back(aRow);
+        }
+    }
+
+    pResult->setRows(aRows);
+
+    return xRef;
+}
+
+} // namespace writer
+} // namespace connectivity
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/connectivity/source/inc/writer/WDatabaseMetaData.hxx b/connectivity/source/inc/writer/WDatabaseMetaData.hxx
new file mode 100644
index 000000000000..45f868366a6f
--- /dev/null
+++ b/connectivity/source/inc/writer/WDatabaseMetaData.hxx
@@ -0,0 +1,52 @@
+/* -*- 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/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ *   Licensed to the Apache Software Foundation (ASF) under one or more
+ *   contributor license agreements. See the NOTICE file distributed
+ *   with this work for additional information regarding copyright
+ *   ownership. The ASF licenses this file to you under the Apache
+ *   License, Version 2.0 (the "License"); you may not use this file
+ *   except in compliance with the License. You may obtain a copy of
+ *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#ifndef INCLUDED_CONNECTIVITY_SOURCE_INC_WRITER_WDATABASEMETADATA_HXX
+#define INCLUDED_CONNECTIVITY_SOURCE_INC_WRITER_WDATABASEMETADATA_HXX
+
+#include "file/FDatabaseMetaData.hxx"
+
+namespace connectivity
+{
+namespace writer
+{
+
+class OWriterDatabaseMetaData : public file::ODatabaseMetaData
+{
+    virtual css::uno::Reference< css::sdbc::XResultSet > impl_getTypeInfo_throw() override;
+    virtual OUString SAL_CALL getURL() override;
+    virtual css::uno::Reference< css::sdbc::XResultSet > SAL_CALL getColumns(const css::uno::Any& catalog, const OUString& schemaPattern, const OUString& tableNamePattern, const OUString& columnNamePattern) override;
+    virtual sal_Int32 SAL_CALL getMaxBinaryLiteralLength() override;
+    virtual sal_Int32 SAL_CALL getMaxCharLiteralLength() override;
+    virtual sal_Int32 SAL_CALL getMaxColumnNameLength() override;
+    virtual sal_Int32 SAL_CALL getMaxColumnsInIndex() override;
+    virtual sal_Int32 SAL_CALL getMaxColumnsInTable() override;
+    virtual css::uno::Reference< css::sdbc::XResultSet > SAL_CALL getTables(const css::uno::Any& catalog, const OUString& schemaPattern, const OUString& tableNamePattern, const css::uno::Sequence< OUString >& types) override;
+protected:
+    virtual ~OWriterDatabaseMetaData() override;
+public:
+    OWriterDatabaseMetaData(file::OConnection* pConnection);
+};
+
+} // namespace writer
+} // namespace connectivity
+
+#endif // INCLUDED_CONNECTIVITY_SOURCE_INC_WRITER_WDATABASEMETADATA_HXX
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */


More information about the Libreoffice-commits mailing list