Skip to content

Commit

Permalink
Merge pull request #30 from italiangrid/cleanup
Browse files Browse the repository at this point in the history
Cleanup frontend codebase
  • Loading branch information
enricovianello authored Apr 13, 2021
2 parents 957086b + 6421145 commit 15cf4a7
Show file tree
Hide file tree
Showing 100 changed files with 491 additions and 3,448 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ Makefile.in
!/m4/wsdl2h.m4
!/m4/xmlrpc-c.m4
src/autogen
.vscode
.vscode
.devcontainer
3 changes: 1 addition & 2 deletions etc/storm-frontend-server.conf.template.in
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,4 @@ db.passwd = password


# WSDL to be returned to a GET request
wsdl.file = @datadir@/wsdl/srm.v2.2.wsdl

wsdl.file = @datarootdir@/wsdl/srm.v2.2.wsdl
11 changes: 11 additions & 0 deletions m4/acinclude.m4
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,15 @@ AC_DEFUN([AC_COMPILER],
CXXFLAGS="${CXXFLAGS} --coverage"
LDFLAGS="${LDFLAGS} --coverage"
fi
AC_ARG_WITH(asan,
[ --with-asan Enable address sanitizer],
[ac_with_asan="yes"],
[ac_with_asan="no"])
if test "x$ac_with_asan" = "xyes" ; then
CFLAGS="${CFLAGS} -fsanitize=address"
CXXFLAGS="${CXXFLAGS} -fsanitize=address"
LDFLAGS="${LDFLAGS} -fsanitize=address"
fi
])
4 changes: 2 additions & 2 deletions m4/wsdl2h.m4
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ AC_DEFUN([AC_WSDL2H],
dnl
dnl @<:@ becomes [
dnl @:>@ becomes ]
wsdl2h_version=$($WSDL2H -help 2>&1 | grep -o '@<:@0-9@:>@\.@<:@0-9@:>@\.@<:@0-9@:>@*$')
wsdl2h_version=$($WSDL2H -help 2>&1 | grep 'wsdl2h release' | grep -o '@<:@0-9@:>@\.@<:@0-9@:>@\.@<:@0-9@:>@*$' | head -1)
normalized_version=$(printf "%02d%02d%02d" $(echo $wsdl2h_version | tr '.' ' '))
Expand All @@ -57,7 +57,7 @@ AC_DEFUN([AC_WSDL2H],
AC_MSG_ERROR([unsupported wsdl2h version: $wsdl2h_version])
fi
AC_MSG_RESULT([yes. wsdlh version $wsdl2h_version detected.])
AC_MSG_RESULT([$wsdl2h_version])
AC_SUBST(WSDL2H)
AC_SUBST(WSDL2H_FLAGS)
])
4 changes: 2 additions & 2 deletions src/db/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ libstormdb_la_SOURCES = \
storm_mysql_ifce.cpp \
mysql_query.cpp \
mysql_query.hpp \
storm_mysql.h
storm_mysql.hpp

libstormdb_la_CFLAGS= @MYSQL_CFLAGS@
GENERATED_GSOAP_INCLUDES = $(top_srcdir)/wsdl/gsoap-autogen
GENERATED_GSOAP_INCLUDES = $(top_builddir)/wsdl/gsoap-autogen

AM_CPPFLAGS = \
-I$(top_srcdir)/src/frontend \
Expand Down
154 changes: 32 additions & 122 deletions src/db/mysql_query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,142 +13,56 @@
* limitations under the License.
*/

#include <new>

#include "srmlogit.h"

#include "mysql_query.hpp"
#include "srmlogit.h"
#include "srm_server.h"

static std::pair<MYSQL_FIELD *, MYSQL_RES *> * _query_init(struct srm_dbfd *dbfd, std::string query) {
static MYSQL_RES* _query_init(srm_dbfd *dbfd, std::string const& query)
{
static const char * const func = "_query_init";

srmlogit(STORM_LOG_DEBUG, func, "Executing query ``%s''\n", query.c_str());

if (mysql_query((MYSQL*)&dbfd->mysql, query.c_str())) {
srmlogit(STORM_LOG_ERROR, func, "mysql_query error: %s. Query: ``%s''\n", mysql_error(
(MYSQL*)&dbfd->mysql), query.c_str());
throw storm_db::mysql_exception((MYSQL*)&dbfd->mysql);
if (mysql_query(dbfd->mysql, query.c_str())) {
srmlogit(STORM_LOG_ERROR, func, "mysql_query error: %s. Query: ``%s''\n",
mysql_error(dbfd->mysql), query.c_str());
throw storm_db::mysql_exception(dbfd->mysql);
}

MYSQL_RES *res;
MYSQL_FIELD *fields;
if (0 != mysql_field_count((MYSQL*)&dbfd->mysql)) { // The stmt has no result.
res = mysql_store_result((MYSQL*)&dbfd->mysql);
MYSQL_RES* res = NULL;
if (0 != mysql_field_count(dbfd->mysql)) { // The stmt has no result.
res = mysql_store_result(dbfd->mysql);
if (NULL == res) { // Error getting the result of the query.
srmlogit(STORM_LOG_ERROR, func, "mysql_store_res error: %s\n",
mysql_error((MYSQL*)&dbfd->mysql));
throw storm_db::mysql_exception((MYSQL*)&dbfd->mysql);
mysql_error(dbfd->mysql));
throw storm_db::mysql_exception(dbfd->mysql);
}
fields = mysql_fetch_fields(res);
} else {
fields = NULL;
res = NULL;
}

return new std::pair<MYSQL_FIELD *, MYSQL_RES *> (fields, res);
return res;
}

namespace storm_db {

/**
* Exec a statement and fill a supplied map of vector containing
* the results of the query. Keys of the map are the column ID of
* the result. Value of the map are vector containing the
* corresponding row value. The element @c n of the vector
* corresponding to the "XXX" key of the map, is the value of such
* column at the row @c n.
*
* @param dbfd a pointer to a srm_dbfd struct
* @param query the query string
* @param results a map<string, vector<string> > reference.
*/
void map_exec_query(struct srm_dbfd *dbfd, const std::string &query,
std::map<std::string, std::vector<std::string> > &results) {
std::pair<MYSQL_FIELD *, MYSQL_RES *> *p = _query_init(dbfd, query);
MYSQL_FIELD *fields = p->first;
MYSQL_RES *res = p->second;

int num_row = mysql_num_rows(res);
int num_fields = mysql_num_fields(res);
if (0 < num_row) {
for (int i = 0; i < num_fields; i++)
results[fields[i].name].reserve(num_row);

MYSQL_ROW row;
while (NULL != (row = mysql_fetch_row(res)))
for (int i = 0; i < num_fields; i++)
results[fields[i].name].push_back((row[i] == NULL ? "" : row[i]));
}
delete p;
if (NULL != p->second)
mysql_free_result(p->second);
}

/**
* Exec a statement and return a map of vector containing the
* results of the query. Keys of the map are the column ID of the
* result. Value of the map are vector containing the corresponding row value. The element @c n of the vector corresponding to the "XXX" key of the map, is the value of such column at the row @c n.
*
* @param dbfd a pointer to a srm_dbfd struct
* @param query the query string
* @return a pointer to a map of @c string,vector.
* The pointer should never be null, and can be empty
* if the query has empty result (like INSERT stmt)
* The client must call @c delete to free memory
*/
std::map<std::string, std::vector<std::string> > * map_exec_query(struct srm_dbfd *dbfd, const std::string &query) {
std::map<std::string, std::vector<std::string> > *dbmap = new std::map<std::string, std::vector<std::string> > ();
if (NULL == dbmap)
throw std::bad_alloc();
std::vector<std::map<std::string, std::string> > vector_exec_query(srm_dbfd *dbfd, std::string const& query)
{
std::vector<std::map<std::string, std::string> > result;

map_exec_query(dbfd, query, *dbmap);
MYSQL_RES* const res = _query_init(dbfd, query);

return dbmap;
}

void vector_exec_query(struct srm_dbfd *dbfd, const std::string &query,
std::vector<std::map<std::string, std::string> > &results) {
std::pair<MYSQL_FIELD *, MYSQL_RES *> *p = _query_init(dbfd, query);
MYSQL_FIELD *fields = p->first;
MYSQL_RES *res = p->second;
MYSQL_ROW row;

int num_row = mysql_num_rows(res);
int num_fields = mysql_num_fields(res);
if (num_row > 0) {
for (int j = 0;NULL != (row = mysql_fetch_row(res)); j++) {
std::map<std::string, std::string> x;
for (int i = 0; i < num_fields; i++)
if (res) {
MYSQL_FIELD* const fields = mysql_fetch_fields(res);
int const num_fields = mysql_num_fields(res);
for (MYSQL_ROW row = mysql_fetch_row(res); row != NULL; row = mysql_fetch_row(res)) {
std::map<std::string, std::string> x;
for (int i = 0; i < num_fields; i++) {
x[fields[i].name] = (NULL != row[i] ? row[i] : "");

results.push_back(x);
}
result.push_back(x);
}
mysql_free_result(res);
}
if (NULL != p->second)
mysql_free_result(p->second);
delete p;
}
/**
* Exec a statement and return a vector of map containing the
* results of the query. The element @c n of the vector contains
* the {@c n}-th row of the result. The element is a map ID
* <pre><-></pre> value.
*
* @param dbfd a pointer to a srm_dbfd struct
* @param query the query string
* @return a pointer to a vector of map<string,string>.
* The pointer should never be null, but can be empty.
* The client must call @c delete to free memory
*/
std::vector<std::map<std::string, std::string> > * vector_exec_query(struct srm_dbfd *dbfd, const std::string &query) {

std::vector<std::map<std::string, std::string> > *dbvec = new std::vector<std::map<std::string, std::string> > ();

vector_exec_query(dbfd, query, *dbvec);

return dbvec;
return result;
}

/** \fn ID_exec_query
Expand All @@ -165,17 +79,13 @@ std::vector<std::map<std::string, std::string> > * vector_exec_query(struct srm_
* {@c 0} if the previous statement does not use
* an AUTO_INCREMENT value.
*/
int ID_exec_query(struct srm_dbfd *dbfd, std::string query) {
int ID_exec_query(srm_dbfd *dbfd, std::string const& query) {

std::pair<MYSQL_FIELD *, MYSQL_RES *> *p = _query_init(dbfd, query);
MYSQL_RES* p = _query_init(dbfd, query);

if (NULL != p) {
if (NULL != p->second)
mysql_free_result(p->second);
delete p;
}
mysql_free_result(p);

return mysql_insert_id((MYSQL*)&dbfd->mysql);
return mysql_insert_id(dbfd->mysql);
}

} // namespace storm_db
Expand Down
33 changes: 13 additions & 20 deletions src/db/mysql_query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,30 @@
#include <string>
#include <vector>

#include <exception>
#include <stdexcept>

#include <mysql/mysql.h>
#include "storm_functions.h"

class srm_dbfd;

namespace storm_db {

class mysql_exception : public std::exception {
class mysql_exception : public std::runtime_error {
public:
mysql_exception(MYSQL* mysql) {
_errno = mysql_errno(mysql);
errmsg = mysql_error(mysql);
};
~mysql_exception() throw() {};
const char* what() const throw() { return errmsg.c_str(); };

int get_mysql_errno() const { return _errno;};
std::string get_mysql_error() const { return errmsg; };
mysql_exception(MYSQL* mysql)
: std::runtime_error(mysql_error(mysql)),
_errno(mysql_errno(mysql))
{
}
int get_mysql_errno() const { return _errno;}
std::string get_mysql_error() const { return what(); }

private:
int _errno;
std::string errmsg;
};

std::vector< std::map<std::string, std::string> > vector_exec_query(srm_dbfd* dbfd, std::string const& query);

void map_exec_query(struct srm_dbfd *dbfd, const std::string &query, std::map<std::string, std::vector<std::string> >& results);
std::map<std::string, std::vector<std::string> > * map_exec_query(struct srm_dbfd *dbfd, std::string query);

void vector_exec_query(struct srm_dbfd *dbfd, const std::string &query, std::vector< std::map<std::string, std::string> >& results);
std::vector< std::map<std::string, std::string> > * vector_exec_query(struct srm_dbfd *dbfd, std::string query);

int ID_exec_query(struct srm_dbfd *dbfd, std::string query);
int ID_exec_query(srm_dbfd *dbfd, std::string const& query);
}
#endif // __MYSQL_QUERY_HPP
Loading

0 comments on commit 15cf4a7

Please sign in to comment.