Commit 71ecdcaf authored by Thomas Braun's avatar Thomas Braun
Browse files

Merge branch 'add-raii-class-for-sql-query-result' into 'main'

Add RAII class for MYSQL query result



Close #128

See merge request !155
parents 246ccc21 bfe6a10c
Loading
Loading
Loading
Loading
Loading
+462 −653

File changed.

Preview size limit exceeded, changes collapsed.

+2 −1
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@
#include "common.h"
#include "DatabaseConnectionPool.h"
#include "DatabaseConnectionHandle.h"
#include "QueryResult.h"

#include <random>

@@ -1547,7 +1548,7 @@ public:

  public:
    void simple_query(std::string sql_query, const char *method, DatabaseConnectionHandle &dch);
    MYSQL_RES *query(std::string sql_query, const char *method, DatabaseConnectionHandle &dch);
    QueryResult query(std::string sql_query, const char *method, DatabaseConnectionHandle &dch);

    // TODO make private
    DatabaseConnectionPoolPtr dcp;
+12 −18
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@
#include "DatabaseConnectionHandle.h"
#include "DatabaseConnectionPool.h"
#include "Logging.h"
#include "QueryResult.h"

#include <errmsg.h>

@@ -473,7 +474,7 @@ void DataBase::simple_query(std::string sql_query, const char *method, DatabaseC
// description : 	Execute a SQL query and return the result.
//
//-----------------------------------------------------------------------------
MYSQL_RES *DataBase::query(std::string sql_query, const char *method, DatabaseConnectionHandle &dch)
QueryResult DataBase::query(std::string sql_query, const char *method, DatabaseConnectionHandle &dch)
{
    MYSQL_RES *result;

@@ -493,7 +494,7 @@ MYSQL_RES *DataBase::query(std::string sql_query, const char *method, DatabaseCo
        Tango::Except::throw_exception(DB_SQLError, o.str(), o2.str());
    }

    return result;
    return QueryResult{result};
}

//+------------------------------------------------------------------
@@ -508,7 +509,6 @@ void DataBase::purge_property(
    const char *table, const char *field, const char *object, const char *name, DatabaseConnectionHandle &dch)
{
    TangoSys_MemStream sql_query;
    MYSQL_RES *result;
    MYSQL_ROW row2;

    sql_query.str("");
@@ -516,8 +516,8 @@ void DataBase::purge_property(
              << name << "\" ORDER by date";

    DEBUG_STREAM << "DataBase::purge_property(): sql_query " << sql_query.str() << std::endl;
    result = query(sql_query.str(), "purge_property()", dch);
    int nb_item = mysql_num_rows(result);
    auto result = query(sql_query.str(), "purge_property()", dch);
    int nb_item = mysql_num_rows(result.res());

    if(nb_item > historyDepth)
    {
@@ -526,14 +526,12 @@ void DataBase::purge_property(
        DEBUG_STREAM << "Purging " << toDelete << " elements" << std::endl;
        for(int j = 0; j < toDelete; j++)
        {
            row2 = mysql_fetch_row(result);
            row2 = mysql_fetch_row(result.res());
            sql_query.str("");
            sql_query << "DELETE FROM " << table << " WHERE id='" << row2[0] << "'";
            simple_query(sql_query.str(), "purge_property()", dch);
        }
    }

    mysql_free_result(result);
}

//+------------------------------------------------------------------
@@ -552,7 +550,6 @@ void DataBase::purge_att_property(const char *table,
                                  DatabaseConnectionHandle &dch)
{
    TangoSys_MemStream sql_query;
    MYSQL_RES *result;
    MYSQL_ROW row2;

    // TANGO_LOG << "purge_att_property(" << object << "," << attribute << "," << name << ")" << std::endl;
@@ -561,8 +558,8 @@ void DataBase::purge_att_property(const char *table,
    sql_query << "SELECT DISTINCT id,date FROM " << table << " WHERE " << field << "=\"" << object << "\" AND name=\""
              << name << "\" AND attribute=\"" << attribute << "\" ORDER by date";

    result = query(sql_query.str(), "purge_att_property()", dch);
    int nb_item = mysql_num_rows(result);
    auto result = query(sql_query.str(), "purge_att_property()", dch);
    int nb_item = mysql_num_rows(result.res());

    if(nb_item > historyDepth)
    {
@@ -570,13 +567,12 @@ void DataBase::purge_att_property(const char *table,
        int toDelete = nb_item - historyDepth;
        for(int j = 0; j < toDelete; j++)
        {
            row2 = mysql_fetch_row(result);
            row2 = mysql_fetch_row(result.res());
            sql_query.str("");
            sql_query << "DELETE FROM " << table << " WHERE id='" << row2[0] << "'";
            simple_query(sql_query.str(), "purge_att_property()", dch);
        }
    }
    mysql_free_result(result);
}

//+------------------------------------------------------------------
@@ -595,7 +591,6 @@ void DataBase::purge_pipe_property(const char *table,
                                   DatabaseConnectionHandle &dch)
{
    TangoSys_MemStream sql_query;
    MYSQL_RES *result;
    MYSQL_ROW row2;

    // TANGO_LOG << "purge_pipe_property(" << object << "," << pipe << "," << name << ")" << std::endl;
@@ -604,8 +599,8 @@ void DataBase::purge_pipe_property(const char *table,
    sql_query << "SELECT DISTINCT id,date FROM " << table << " WHERE " << field << "=\"" << object << "\" AND name=\""
              << name << "\" AND pipe=\"" << pipe << "\" ORDER by date";

    result = query(sql_query.str(), "purge_pipe_property()", dch);
    int nb_item = mysql_num_rows(result);
    auto result = query(sql_query.str(), "purge_pipe_property()", dch);
    int nb_item = mysql_num_rows(result.res());

    if(nb_item > historyDepth)
    {
@@ -613,13 +608,12 @@ void DataBase::purge_pipe_property(const char *table,
        int toDelete = nb_item - historyDepth;
        for(int j = 0; j < toDelete; j++)
        {
            row2 = mysql_fetch_row(result);
            row2 = mysql_fetch_row(result.res());
            sql_query.str("");
            sql_query << "DELETE FROM " << table << " WHERE id='" << row2[0] << "'";
            simple_query(sql_query.str(), "purge_pipe_property()", dch);
        }
    }
    mysql_free_result(result);
}

//+------------------------------------------------------------------

QueryResult.h

0 → 100644
+33 −0
Original line number Diff line number Diff line
#ifndef QUERY_RESULT_H
#define QUERY_RESULT_H

#include "common.h"

class QueryResult
{
  public:
    QueryResult(MYSQL_RES *res) :
        m_res{res}
    {
    }

    ~QueryResult()
    {
        mysql_free_result(m_res);
    }

    MYSQL_RES *res()
    {
        return m_res;
    }

    QueryResult(QueryResult &) = delete;
    QueryResult(QueryResult &&) = delete;
    QueryResult &operator=(const QueryResult &) = delete;
    QueryResult &operator=(QueryResult &&) = delete;

  private:
    MYSQL_RES *m_res;
};

#endif // QUERY_RESULT_H
+2 −2
Original line number Diff line number Diff line
@@ -93,7 +93,7 @@ class Fixture
        {
            try
            {
                dp = new Tango::DeviceProxy("sys/database/2");
                dp = std::make_unique<Tango::DeviceProxy>("sys/database/2");

                dp->ping();
            }
@@ -115,7 +115,7 @@ class Fixture
        ExecuteInShell("./stop_tdb.sh");
    }

    Tango::DeviceProxy *dp;
    std::unique_ptr<Tango::DeviceProxy> dp;
};

class AddAndExportDeviceFixture : public Fixture