Skip to content

Commit

Permalink
The minor extension made to the MySQL connector
Browse files Browse the repository at this point in the history
Also added the utility class to handle simple query cases.
  • Loading branch information
iagaponenko committed Aug 10, 2023
1 parent 94847e6 commit 42b7db6
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/mysql/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ target_sources(mysql PRIVATE
LocalInfile.cc
MySqlConfig.cc
MySqlConnection.cc
MySqlUtils.cc
RowBuffer.cc
SchemaFactory.cc
)
Expand Down
13 changes: 13 additions & 0 deletions src/mysql/MySqlConnection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,19 @@ bool MySqlConnection::selectDb(std::string const& dbName) {
return true;
}

std::vector<std::string> MySqlConnection::getColumnNames() const {
assert(_mysql);
assert(_mysql_res);
std::vector<std::string> names;
if (0 != mysql_field_count(_mysql)) {
auto fields = mysql_fetch_fields(_mysql_res);
for (unsigned int i = 0; i < mysql_num_fields(_mysql_res); i++) {
names.push_back(std::string(fields[i].name));
}
}
return names;
}

////////////////////////////////////////////////////////////////////////
// MySqlConnection
// private:
Expand Down
2 changes: 2 additions & 0 deletions src/mysql/MySqlConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <memory>
#include <mutex>
#include <string>
#include <vector>

// Third-party headers
#include "boost/utility.hpp"
Expand Down Expand Up @@ -80,6 +81,7 @@ class MySqlConnection : boost::noncopyable {
assert(_mysql);
return mysql_field_count(_mysql);
}
std::vector<std::string> getColumnNames() const;
unsigned int getErrno() const {
assert(_mysql);
return mysql_errno(_mysql);
Expand Down
95 changes: 95 additions & 0 deletions src/mysql/MySqlUtils.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// -*- LSST-C++ -*-
/*
* LSST Data Management System
*
* This product includes software developed by the
* LSST Project (http://www.lsst.org/).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the LSST License Statement and
* the GNU General Public License along with this program. If not,
* see <http://www.lsstcorp.org/LegalNotices/>.
*/

// Class header
#include "mysql/MySqlUtils.h"

// System headers
#include <string>

// Third party headers
#include <mysql/mysql.h>
#include <mysql/mysqld_error.h>
#include <mysql/errmsg.h>

// Qserv headers
#include "mysql/MySqlConfig.h"
#include "mysql/MySqlConnection.h"

using namespace std;
using json = nlohmann::json;

namespace {

string errInfo(lsst::qserv::mysql::MySqlConnection const& conn) {
return "errno: " + to_string(conn.getErrno()) + ", error: " + conn.getError();
}

} // anonymous namespace

namespace lsst::qserv::mysql {

json MySqlUtils::processList(MySqlConfig const& config, bool full) {
string const context = "MySqlUtils::" + string(__func__);
string const query = "SHOW" + string(full ? " FULL" : "") + " PROCESSLIST";

MySqlConnection conn(config);
if (!conn.connect()) {
string const err = context + " failed to connect to the worker database, " + ::errInfo(conn);
throw MySqlQueryError(err);
}
if (!conn.queryUnbuffered(query)) {
string const err = "failed to execute the query: '" + query + "', " + ::errInfo(conn);
throw MySqlQueryError(err);
}
json result;
result["queries"] = json::object({{"columns", json::array()}, {"rows", json::array()}});
int const numFields = conn.getResultFieldCount();
if (numFields > 0) {
result["queries"]["columns"] = conn.getColumnNames();
auto& rows = result["queries"]["rows"];
MYSQL_RES* mysqlResult = conn.getResult();
while (true) {
MYSQL_ROW mysqlRow = mysql_fetch_row(mysqlResult);
if (!mysqlRow) {
if (0 == conn.getErrno()) {
// End of iteration if no specific error was reported.
break;
}
string const err =
context + " failed to fetch next row for query: '" + query + "', " + ::errInfo(conn);
throw MySqlQueryError(err);
}
size_t const* lengths = mysql_fetch_lengths(mysqlResult);
json row = json::array();
for (int i = 0; i < numFields; i++) {
// Report the empty string for SQL NULL.
auto const length = lengths[i];
row.push_back(length == 0 ? string() : string(mysqlRow[i], length));
}
rows.push_back(row);
}
}
return result;
}

} // namespace lsst::qserv::mysql
71 changes: 71 additions & 0 deletions src/mysql/MySqlUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// -*- LSST-C++ -*-
/*
* LSST Data Management System
*
* This product includes software developed by the
* LSST Project (http://www.lsst.org/).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the LSST License Statement and
* the GNU General Public License along with this program. If not,
* see <http://www.lsstcorp.org/LegalNotices/>.
*/
#ifndef LSST_QSERV_MYSQL_MYSQLUTILS_H
#define LSST_QSERV_MYSQL_MYSQLUTILS_H

// System headers
#include <stdexcept>

// Third-party headers
#include "nlohmann/json.hpp"

/// Forward declarations.
namespace lsst::qserv::mysql {
class MySqlConfig;
} // namespace lsst::qserv::mysql

/// This header declarations.
namespace lsst::qserv::mysql {

/**
* Class MySqlQueryError represents exceptions to be throw on specific errors
* detected when attempting to execute the queries.
*/
class MySqlQueryError : public std::runtime_error {
using std::runtime_error::runtime_error;
};

/**
* Class MySqlUtils is the utility class providing a collection of useful queries reporting
* small result sets.
* @note Each tool of the collection does its own connection handling (opening/etc.).
*/
class MySqlUtils {
public:
/**
* Report info on the on-going queries using 'SHOW [FULL] PROCESSLIST'.
* @param A scope of the operaton depends on the user credentials privided
* in the configuration object. Normally, a subset of queries which belong
* to the specified user will be reported.
* @param config Configuration parameters of the MySQL connector.
* @param full The optional modifier which (if set) allows seeing the full text
* of the queries.
* @return A collection of queries encoded as the JSON object. Please, see the code
* for further details on the schema of the object.
* @throws MySqlQueryError on errors detected during query execution/processing.
*/
static nlohmann::json processList(MySqlConfig const& config, bool full = false);
};

} // namespace lsst::qserv::mysql

#endif // LSST_QSERV_MYSQL_MYSQLUTILS_H

0 comments on commit 42b7db6

Please sign in to comment.