connectivity/Library_mysqlc.mk                        |    2 
 connectivity/source/drivers/mysqlc/mysqlc_catalog.cxx |   16 +-
 connectivity/source/drivers/mysqlc/mysqlc_view.cxx    |  113 ++++++++++++++
 connectivity/source/drivers/mysqlc/mysqlc_view.hxx    |   72 +++++++++
 connectivity/source/drivers/mysqlc/mysqlc_views.cxx   |  140 ++++++++++++++++++
 connectivity/source/drivers/mysqlc/mysqlc_views.hxx   |   53 ++++++
 6 files changed, 394 insertions(+), 2 deletions(-)

New commits:
commit 52443996eff721e612ac4afc1eb1a53bb8a3e06f
Author:     Julien Nabet <serval2...@yahoo.fr>
AuthorDate: Sun Jan 16 17:42:31 2022 +0100
Commit:     Julien Nabet <serval2...@yahoo.fr>
CommitDate: Sun Jan 16 23:38:25 2022 +0100

    Mysql/MariaDB: implement edit VIEW with GUI or SQL
    
    Delete View works (and the list is updated)
    Modifying View works
    
    But, there are at least 2 things to fix:
    - adding a view still works but the list isn't updated without manual 
refresh)
    - renaming view doesn't work (a popup error message appear)
    
    Change-Id: I48ffb322910ca64d51a161424f265de387bdd7ad
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/128483
    Tested-by: Jenkins
    Reviewed-by: Julien Nabet <serval2...@yahoo.fr>

diff --git a/connectivity/Library_mysqlc.mk b/connectivity/Library_mysqlc.mk
index 467562c066f7..af46fb9ba137 100644
--- a/connectivity/Library_mysqlc.mk
+++ b/connectivity/Library_mysqlc.mk
@@ -69,6 +69,8 @@ $(eval $(call gb_Library_add_exception_objects,mysqlc,\
        connectivity/source/drivers/mysqlc/mysqlc_types \
        connectivity/source/drivers/mysqlc/mysqlc_user \
        connectivity/source/drivers/mysqlc/mysqlc_users \
+       connectivity/source/drivers/mysqlc/mysqlc_view \
+       connectivity/source/drivers/mysqlc/mysqlc_views \
 ))
 
 $(eval $(call 
gb_Library_set_componentfile,mysqlc,connectivity/source/drivers/mysqlc/mysqlc,services))
diff --git a/connectivity/source/drivers/mysqlc/mysqlc_catalog.cxx 
b/connectivity/source/drivers/mysqlc/mysqlc_catalog.cxx
index e9b7569fb1aa..035c7761f5e3 100644
--- a/connectivity/source/drivers/mysqlc/mysqlc_catalog.cxx
+++ b/connectivity/source/drivers/mysqlc/mysqlc_catalog.cxx
@@ -9,6 +9,7 @@
 
 #include "mysqlc_catalog.hxx"
 #include "mysqlc_tables.hxx"
+#include "mysqlc_views.hxx"
 #include "mysqlc_users.hxx"
 
 #include <com/sun/star/sdbc/XRow.hpp>
@@ -44,8 +45,19 @@ void Catalog::refreshTables()
 
 void Catalog::refreshViews()
 {
-    // TODO: implement me.
-    // Sets m_pViews (OCatalog)
+    uno::Reference<XResultSet> xViews = m_xMetaData->getTables(Any(), "%", 
"%", { "VIEW" });
+
+    if (!xViews.is())
+        return;
+
+    ::std::vector<OUString> aViewNames;
+
+    fillNames(xViews, aViewNames);
+
+    if (!m_pViews)
+        m_pViews.reset(new Views(m_xConnection, *this, m_aMutex, aViewNames));
+    else
+        m_pViews->reFill(aViewNames);
 }
 
 //----- IRefreshableGroups ---------------------------------------------------
diff --git a/connectivity/source/drivers/mysqlc/mysqlc_view.cxx 
b/connectivity/source/drivers/mysqlc/mysqlc_view.cxx
new file mode 100644
index 000000000000..a586e20a9a13
--- /dev/null
+++ b/connectivity/source/drivers/mysqlc/mysqlc_view.cxx
@@ -0,0 +1,113 @@
+/* -*- 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 "mysqlc_view.hxx"
+
+#include <propertyids.hxx>
+
+#include <com/sun/star/lang/WrappedTargetException.hpp>
+#include <com/sun/star/sdbc/XRow.hpp>
+#include <com/sun/star/sdbc/SQLException.hpp>
+
+namespace connectivity::mysqlc
+{
+using ::com::sun::star::uno::Reference;
+using ::com::sun::star::uno::UNO_QUERY_THROW;
+using ::com::sun::star::uno::Exception;
+using ::com::sun::star::uno::RuntimeException;
+using ::com::sun::star::uno::Any;
+using ::com::sun::star::sdbc::SQLException;
+using ::com::sun::star::sdbc::XConnection;
+using ::com::sun::star::lang::WrappedTargetException;
+using ::com::sun::star::sdbc::XResultSet;
+using ::com::sun::star::sdbc::XStatement;
+using ::com::sun::star::lang::DisposedException;
+using ::com::sun::star::sdbc::XRow;
+
+View::View(const Reference<XConnection>& _rxConnection, bool _bCaseSensitive,
+           const OUString& _rSchemaName, const OUString& _rName)
+    : View_Base(_bCaseSensitive, _rName, _rxConnection->getMetaData(), 
OUString(), _rSchemaName,
+                OUString())
+    , m_xConnection(_rxConnection)
+{
+}
+
+View::~View() {}
+
+void SAL_CALL View::acquire() noexcept { View_Base::acquire(); };
+void SAL_CALL View::release() noexcept { View_Base::release(); };
+css::uno::Any SAL_CALL View::queryInterface(const css::uno::Type& _rType)
+{
+    css::uno::Any aReturn = View_Base::queryInterface(_rType);
+    if (!aReturn.hasValue())
+        aReturn = View_IBASE::queryInterface(_rType);
+    return aReturn;
+}
+
+css::uno::Sequence<css::uno::Type> SAL_CALL View::getTypes()
+{
+    return ::comphelper::concatSequences(View_Base::getTypes(), 
View_IBASE::getTypes());
+}
+
+css::uno::Sequence<sal_Int8> SAL_CALL View::getImplementationId()
+{
+    return css::uno::Sequence<sal_Int8>();
+}
+
+void SAL_CALL View::alterCommand(const OUString& _rNewCommand)
+{
+    OUString aCommand = "ALTER VIEW " + m_SchemaName + "." + m_Name + " AS " + 
_rNewCommand;
+    m_xMetaData->getConnection()->createStatement()->execute(aCommand);
+}
+
+void SAL_CALL View::getFastPropertyValue(Any& _rValue, sal_Int32 _nHandle) 
const
+{
+    if (_nHandle == PROPERTY_ID_COMMAND)
+    {
+        // retrieve the very current command, don't rely on the base classes 
cached value
+        // (which we initialized empty, anyway)
+        _rValue <<= impl_getCommand();
+        return;
+    }
+
+    View_Base::getFastPropertyValue(_rValue, _nHandle);
+}
+
+OUString View::impl_getCommand() const
+{
+    OUString aCommand("SELECT VIEW_DEFINITION FROM INFORMATION_SCHEMA.VIEWS 
WHERE TABLE_SCHEMA = '"
+                      + m_SchemaName + "' AND TABLE_NAME = '" + m_Name + "'");
+    //::utl::SharedUNOComponent< XStatement > xStatement; xStatement.set( 
m_xConnection->createStatement(), UNO_QUERY_THROW );
+    Reference<XResultSet> xResult(
+        
m_xMetaData->getConnection()->createStatement()->executeQuery(aCommand),
+        css::uno::UNO_SET_THROW);
+    if (!xResult->next())
+    {
+        // hmm. There is no view the name as we know it. Can only mean some 
other instance
+        // dropped this view meanwhile...
+        std::abort();
+    }
+
+    Reference<XRow> xRow(xResult, UNO_QUERY_THROW);
+    return xRow->getString(1);
+}
+
+} // namespace connectivity::mysqlc
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/connectivity/source/drivers/mysqlc/mysqlc_view.hxx 
b/connectivity/source/drivers/mysqlc/mysqlc_view.hxx
new file mode 100644
index 000000000000..8450152499c0
--- /dev/null
+++ b/connectivity/source/drivers/mysqlc/mysqlc_view.hxx
@@ -0,0 +1,72 @@
+/* -*- 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 .
+ */
+
+#pragma once
+
+#include <connectivity/sdbcx/VView.hxx>
+
+#include <com/sun/star/sdbcx/XAlterView.hpp>
+#include <com/sun/star/sdbc/XConnection.hpp>
+
+#include <comphelper/sequence.hxx>
+#include <cppuhelper/implbase1.hxx>
+
+namespace connectivity::mysqlc
+{
+typedef ::connectivity::sdbcx::OView View_Base;
+typedef ::cppu::ImplHelper1<css::sdbcx::XAlterView> View_IBASE;
+
+class View : public View_Base, public View_IBASE
+{
+public:
+    View(const css::uno::Reference<css::sdbc::XConnection>& _rxConnection, 
bool _bCaseSensitive,
+         const OUString& _rSchemaName, const OUString& _rName);
+
+    // UNO
+    virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type& aType) 
override;
+    virtual void SAL_CALL acquire() noexcept override;
+    virtual void SAL_CALL release() noexcept override;
+
+    virtual css::uno::Sequence<css::uno::Type> SAL_CALL getTypes() override;
+    virtual css::uno::Sequence<sal_Int8> SAL_CALL getImplementationId() 
override;
+
+    // XAlterView
+    virtual void SAL_CALL alterCommand(const OUString& NewCommand) override;
+
+protected:
+    virtual ~View() override;
+
+protected:
+    // OPropertyContainer
+    virtual void SAL_CALL getFastPropertyValue(css::uno::Any& _rValue,
+                                               sal_Int32 _nHandle) const 
override;
+
+private:
+    /** retrieves the current command of the View */
+    OUString impl_getCommand() const;
+
+private:
+    css::uno::Reference<css::sdbc::XConnection> m_xConnection;
+
+    using View_Base::getFastPropertyValue;
+};
+
+} // namespace connectivity::mysqlc
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/connectivity/source/drivers/mysqlc/mysqlc_views.cxx 
b/connectivity/source/drivers/mysqlc/mysqlc_views.cxx
new file mode 100644
index 000000000000..bdc2f718fd6f
--- /dev/null
+++ b/connectivity/source/drivers/mysqlc/mysqlc_views.cxx
@@ -0,0 +1,140 @@
+/* -*- 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 "mysqlc_tables.hxx"
+#include "mysqlc_views.hxx"
+#include "mysqlc_view.hxx"
+#include "mysqlc_catalog.hxx"
+#include <connectivity/dbtools.hxx>
+#include <comphelper/types.hxx>
+#include <TConnection.hxx>
+
+using namespace ::comphelper;
+
+using namespace ::cppu;
+using namespace connectivity;
+using namespace connectivity::mysqlc;
+using namespace css::uno;
+using namespace css::beans;
+using namespace css::sdbcx;
+using namespace css::sdbc;
+using namespace css::container;
+using namespace css::lang;
+using namespace dbtools;
+typedef connectivity::sdbcx::OCollection OCollection_TYPE;
+
+Views::Views(const Reference<XConnection>& _rxConnection, ::cppu::OWeakObject& 
_rParent,
+             ::osl::Mutex& _rMutex, const ::std::vector<OUString>& _rVector)
+    : sdbcx::OCollection(_rParent, true, _rMutex, _rVector)
+    , m_xConnection(_rxConnection)
+    , m_xMetaData(_rxConnection->getMetaData())
+    , m_bInDrop(false)
+{
+}
+
+sdbcx::ObjectType Views::createObject(const OUString& _rName)
+{
+    OUString sCatalog, sSchema, sTable;
+    ::dbtools::qualifiedNameComponents(m_xMetaData, _rName, sCatalog, sSchema, 
sTable,
+                                       
::dbtools::EComposeRule::InDataManipulation);
+    return new View(m_xConnection, isCaseSensitive(), sSchema, sTable);
+}
+
+void Views::impl_refresh() { static_cast<Catalog&>(m_rParent).refreshViews(); }
+
+void Views::disposing()
+{
+    m_xMetaData.clear();
+    OCollection::disposing();
+}
+
+Reference<XPropertySet> Views::createDescriptor()
+{
+    return new connectivity::sdbcx::OView(true, m_xMetaData);
+}
+
+// XAppend
+sdbcx::ObjectType Views::appendObject(const OUString& _rForName,
+                                      const Reference<XPropertySet>& 
descriptor)
+{
+    createView(descriptor);
+    return createObject(_rForName);
+}
+
+// XDrop
+void Views::dropObject(sal_Int32 _nPos, const OUString& /*_sElementName*/)
+{
+    if (m_bInDrop)
+        return;
+
+    Reference<XInterface> xObject(getObject(_nPos));
+    bool bIsNew = connectivity::sdbcx::ODescriptor::isNew(xObject);
+    if (!bIsNew)
+    {
+        OUString aSql("DROP VIEW");
+
+        Reference<XPropertySet> xProp(xObject, UNO_QUERY);
+        aSql += ::dbtools::composeTableName(m_xMetaData, xProp,
+                                            
::dbtools::EComposeRule::InTableDefinitions, true);
+
+        Reference<XConnection> xConnection = m_xMetaData->getConnection();
+        Reference<XStatement> xStmt = xConnection->createStatement();
+        xStmt->execute(aSql);
+        ::comphelper::disposeComponent(xStmt);
+    }
+}
+
+void Views::dropByNameImpl(const OUString& elementName)
+{
+    m_bInDrop = true;
+    OCollection_TYPE::dropByName(elementName);
+    m_bInDrop = false;
+}
+
+void Views::createView(const Reference<XPropertySet>& descriptor)
+{
+    Reference<XConnection> xConnection = m_xMetaData->getConnection();
+
+    OUString sCommand;
+    
descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_COMMAND))
+        >>= sCommand;
+
+    OUString aSql = "CREATE VIEW "
+                    + ::dbtools::composeTableName(m_xMetaData, descriptor,
+                                                  
::dbtools::EComposeRule::InTableDefinitions, true)
+                    + " AS " + sCommand;
+
+    Reference<XStatement> xStmt = xConnection->createStatement();
+    if (xStmt.is())
+    {
+        xStmt->execute(aSql);
+        ::comphelper::disposeComponent(xStmt);
+    }
+    /*  TODO find a way to refresh view to make the new one appear right away
+    // insert the new view also in the tables collection
+    Tables* pTables = 
static_cast<Tables*>(static_cast<Catalog&>(m_rParent).getPrivateTables());
+    if ( pTables)
+    {
+        OUString sName = ::dbtools::composeTableName( m_xMetaData, descriptor, 
::dbtools::EComposeRule::InDataManipulation, false );
+        pTables->appendNew(sName);
+    }
+*/
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/connectivity/source/drivers/mysqlc/mysqlc_views.hxx 
b/connectivity/source/drivers/mysqlc/mysqlc_views.hxx
new file mode 100644
index 000000000000..708e1b24eec5
--- /dev/null
+++ b/connectivity/source/drivers/mysqlc/mysqlc_views.hxx
@@ -0,0 +1,53 @@
+/* -*- 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 .
+ */
+#pragma once
+
+#include <connectivity/sdbcx/VCollection.hxx>
+#include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
+namespace connectivity::mysqlc
+{
+class Views final : public sdbcx::OCollection
+{
+    css::uno::Reference<css::sdbc::XConnection> m_xConnection;
+    css::uno::Reference<css::sdbc::XDatabaseMetaData> m_xMetaData;
+    bool m_bInDrop;
+
+    virtual sdbcx::ObjectType createObject(const OUString& _rName) override;
+    virtual void impl_refresh() override;
+    virtual css::uno::Reference<css::beans::XPropertySet> createDescriptor() 
override;
+    virtual sdbcx::ObjectType
+    appendObject(const OUString& _rForName,
+                 const css::uno::Reference<css::beans::XPropertySet>& 
descriptor) override;
+    virtual void dropObject(sal_Int32 _nPos, const OUString& _sElementName) 
override;
+
+    void createView(const css::uno::Reference<css::beans::XPropertySet>& 
descriptor);
+
+public:
+    Views(const css::uno::Reference<css::sdbc::XConnection>& _rxConnection,
+          ::cppu::OWeakObject& _rParent, ::osl::Mutex& _rMutex,
+          const ::std::vector<OUString>& _rVector);
+
+    // only the name is identical to ::cppu::OComponentHelper
+    virtual void disposing() override;
+
+    void dropByNameImpl(const OUString& elementName);
+};
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Reply via email to