Revert "tdf#117118 - Saves data automatically in Firebird embedded database"

This reverts commit 9227fbabe0a33134f56aefdd8ec16024f006a659.

Reason for revert: This patch generate a new bug, so I revert it until further investigation

Change-Id: I4d613db8cef5363c6b276b8646147443871c312a
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/157116
Tested-by: Jenkins
Reviewed-by: Xisco Fauli <xiscofauli@libreoffice.org>
diff --git a/connectivity/source/drivers/firebird/Connection.cxx b/connectivity/source/drivers/firebird/Connection.cxx
index 4e2bce4..5b8cdb7 100644
--- a/connectivity/source/drivers/firebird/Connection.cxx
+++ b/connectivity/source/drivers/firebird/Connection.cxx
@@ -339,6 +339,11 @@ void Connection::construct(const OUString& url, const Sequence< PropertyValue >&
    }
}

void Connection::notifyDatabaseModified()
{
    if (m_xParentDocument.is()) // Only true in embedded mode
        m_xParentDocument->setModified(true);
}

//----- XServiceInfo ---------------------------------------------------------
IMPLEMENT_SERVICE_INFO(Connection, "com.sun.star.sdbc.drivers.firebird.Connection",
@@ -817,7 +822,41 @@ void SAL_CALL Connection::documentEventOccured( const DocumentEvent& Event )
    if ( !(m_bIsEmbedded && m_xEmbeddedStorage.is()) )
        return;

    storeDatabase();
    SAL_INFO("connectivity.firebird", "Writing .fbk from running db");
    try
    {
        runBackupService(isc_action_svc_backup);
    }
    catch (const SQLException& e)
    {
        auto a = cppu::getCaughtException();
        throw WrappedTargetRuntimeException(e.Message, e.Context, a);
    }


    Reference< XStream > xDBStream(m_xEmbeddedStorage->openStreamElement(our_sFBKLocation,
                                                    ElementModes::WRITE));

    // TODO: verify the backup actually exists -- the backup service
    // can fail without giving any sane error messages / telling us
    // that it failed.
    using namespace ::comphelper;
    Reference< XComponentContext > xContext = comphelper::getProcessComponentContext();
    Reference< XInputStream > xInputStream;
    if (!xContext.is())
        return;

    xInputStream =
            OStorageHelper::GetInputStreamFromURL(m_sFBKPath, xContext);
    if (xInputStream.is())
        OStorageHelper::CopyInputToOutput( xInputStream,
                                    xDBStream->getOutputStream());

    // remove old fdb file if exists
    uno::Reference< ucb::XSimpleFileAccess > xFileAccess =
        ucb::SimpleFileAccess::create(xContext);
    if (xFileAccess->exists(m_sFirebirdURL))
        xFileAccess->kill(m_sFirebirdURL);
}
// XEventListener
void SAL_CALL Connection::disposing(const EventObject& /*rSource*/)
@@ -899,59 +938,13 @@ void Connection::disposing()
            evaluateStatusVector(status, u"isc_detach_database", *this);
        }
    }

    storeDatabase();
    // TODO: write to storage again?

    cppu::WeakComponentImplHelperBase::disposing();

    m_pDatabaseFileDir.reset();
}

void Connection::storeDatabase()
{
    MutexGuard aGuard(m_aMutex);

    if (m_bIsEmbedded && m_xEmbeddedStorage.is())
    {
        SAL_INFO("connectivity.firebird", "Writing .fbk from running db");
        try
        {
            runBackupService(isc_action_svc_backup);
        }
        catch (const SQLException& e)
        {
            auto a = cppu::getCaughtException();
            throw WrappedTargetRuntimeException(e.Message, e.Context, a);
        }

        Reference<XStream> xDBStream(
            m_xEmbeddedStorage->openStreamElement(our_sFBKLocation, ElementModes::WRITE));

        using namespace ::comphelper;
        Reference<XComponentContext> xContext = comphelper::getProcessComponentContext();
        Reference<XInputStream> xInputStream;
        if (!xContext.is())
            return;

        xInputStream = OStorageHelper::GetInputStreamFromURL(m_sFBKPath, xContext);
        if (xInputStream.is())
            OStorageHelper::CopyInputToOutput(xInputStream, xDBStream->getOutputStream());

        // remove old fdb and fbk files if exist
        uno::Reference<ucb::XSimpleFileAccess> xFileAccess
            = ucb::SimpleFileAccess::create(xContext);
        if (xFileAccess->exists(m_sFirebirdURL))
            xFileAccess->kill(m_sFirebirdURL);

        if (xFileAccess->exists(m_sFBKPath))
            xFileAccess->kill(m_sFBKPath);

        cppu::WeakComponentImplHelperBase::disposing();

        m_pDatabaseFileDir.reset();
    }
}

void Connection::disposeStatements()
{
    MutexGuard aGuard(m_aMutex);
diff --git a/connectivity/source/drivers/firebird/Connection.hxx b/connectivity/source/drivers/firebird/Connection.hxx
index 404f258..fa89643 100644
--- a/connectivity/source/drivers/firebird/Connection.hxx
+++ b/connectivity/source/drivers/firebird/Connection.hxx
@@ -171,6 +171,15 @@ namespace connectivity::firebird
            /// @throws css::sdbc::SQLException
            isc_tr_handle&  getTransaction();

            /**
              * Must be called anytime the underlying database is likely to have
              * changed.
              *
              * This is used to notify the database document of any changes, so
              * that the user is informed of any pending changes needing to be
              * saved.
              */
            void notifyDatabaseModified();

            /**
             * Create a new Blob tied to this connection. Blobs are tied to a
@@ -194,11 +203,6 @@ namespace connectivity::firebird
            css::uno::Reference< css::sdbcx::XTablesSupplier >
                createCatalog();

            /**
            * Backup and store embedded extracted database to the .odb file
            */
            void storeDatabase();

            // OComponentHelper
            virtual void SAL_CALL disposing() override;

diff --git a/connectivity/source/drivers/firebird/PreparedStatement.cxx b/connectivity/source/drivers/firebird/PreparedStatement.cxx
index 35847d0..608d05c 100644
--- a/connectivity/source/drivers/firebird/PreparedStatement.cxx
+++ b/connectivity/source/drivers/firebird/PreparedStatement.cxx
@@ -334,6 +334,9 @@ sal_Bool SAL_CALL OPreparedStatement::execute()
                                  m_aStatementHandle,
                                  m_pOutSqlda);

    if (getStatementChangeCount() > 0)
        m_pConnection->notifyDatabaseModified();

    return m_xResultSet.is();
    // TODO: implement handling of multiple ResultSets.
}
diff --git a/connectivity/source/drivers/firebird/Statement.cxx b/connectivity/source/drivers/firebird/Statement.cxx
index d135c4e..ed56b59 100644
--- a/connectivity/source/drivers/firebird/Statement.cxx
+++ b/connectivity/source/drivers/firebird/Statement.cxx
@@ -126,6 +126,11 @@ uno::Reference< XResultSet > SAL_CALL OStatement::executeQuery(const OUString& s
    if (isDDLStatement())
    {
        m_pConnection->commit();
        m_pConnection->notifyDatabaseModified();
    }
    else if (getStatementChangeCount() > 0)
    {
        m_pConnection->notifyDatabaseModified();
    }

    return m_xResultSet;