Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enh(ccb_sql) : replacing msg by msg_fmt #34

Merged
merged 2 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 8 additions & 25 deletions src/ccb_sql/database/mysql_bind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,14 @@
#include <cstring>
#include <iostream>
#include "com/centreon/broker/database/mysql_column.hh"
#include "com/centreon/broker/exceptions/msg.hh"
#include "com/centreon/broker/io/events.hh"
#include "com/centreon/broker/mapping/entry.hh"
#include "com/centreon/broker/mysql.hh"

using namespace com::centreon::broker;
using namespace com::centreon::broker::database;

mysql_bind::mysql_bind() {
set_size(0);
}
mysql_bind::mysql_bind() { set_size(0); }

/**
* Constructor
Expand Down Expand Up @@ -70,9 +67,7 @@ void mysql_bind::set_size(int size, int length) {
}
}

bool mysql_bind::_prepared(int range) const {
return _typed[range];
}
bool mysql_bind::_prepared(int range) const { return _typed[range]; }

void mysql_bind::_prepare_type(int range, enum enum_field_types type) {
_typed[range] = true;
Expand Down Expand Up @@ -345,32 +340,20 @@ void mysql_bind::debug() {
}
}

bool mysql_bind::is_empty() const {
return _is_empty;
}
bool mysql_bind::is_empty() const { return _is_empty; }

MYSQL_BIND const* mysql_bind::get_bind() const {
return &_bind[0];
}
MYSQL_BIND const* mysql_bind::get_bind() const { return &_bind[0]; }

MYSQL_BIND* mysql_bind::get_bind() {
return &_bind[0];
}
MYSQL_BIND* mysql_bind::get_bind() { return &_bind[0]; }

int mysql_bind::get_size() const {
return _bind.size();
}
int mysql_bind::get_size() const { return _bind.size(); }

/**
* At the moment, the bind only carries one row. So this number is 0 or 1.
* And that does not implies that the result is so small.
*
* @return 1 or 0.
*/
int mysql_bind::get_rows_count() const {
return _is_empty ? 0 : 1;
}
int mysql_bind::get_rows_count() const { return _is_empty ? 0 : 1; }

void mysql_bind::set_empty(bool empty) {
_is_empty = empty;
}
void mysql_bind::set_empty(bool empty) { _is_empty = empty; }
28 changes: 8 additions & 20 deletions src/ccb_sql/database/mysql_column.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include <cstring>
#include <iostream>

#include "com/centreon/broker/exceptions/msg.hh"
#include "com/centreon/broker/log_v2.hh"

using namespace com::centreon::broker;
Expand Down Expand Up @@ -126,13 +125,9 @@ mysql_column& mysql_column::operator=(mysql_column const& other) {
return *this;
}

int mysql_column::get_type() const {
return _type;
}
int mysql_column::get_type() const { return _type; }

void* mysql_column::get_buffer() {
return _vector;
}
void* mysql_column::get_buffer() { return _vector; }

void mysql_column::set_length(int len) {
assert(_type == MYSQL_TYPE_STRING);
Expand All @@ -155,7 +150,8 @@ void mysql_column::set_value(std::string const& str) {
log_v2::sql()->warn(
"mysql_column: Text column too short to contain a string of {} "
"characters starting with '{}...'",
size, str.substr(0, 30));
size,
str.substr(0, 30));
size = tmp.size();
content = tmp.c_str();
}
Expand All @@ -166,21 +162,13 @@ void mysql_column::set_value(std::string const& str) {
strncpy(vector[0], content, _length[0] + 1);
}

bool mysql_column::is_null() const {
return _is_null[0];
}
bool mysql_column::is_null() const { return _is_null[0]; }

my_bool* mysql_column::is_null_buffer() {
return &_is_null[0];
}
my_bool* mysql_column::is_null_buffer() { return &_is_null[0]; }

my_bool* mysql_column::error_buffer() {
return &_error[0];
}
my_bool* mysql_column::error_buffer() { return &_error[0]; }

unsigned long* mysql_column::length_buffer() {
return &_length[0];
}
unsigned long* mysql_column::length_buffer() { return &_length[0]; }

void mysql_column::set_type(int type) {
_type = type;
Expand Down
35 changes: 14 additions & 21 deletions src/ccb_sql/database/mysql_result.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
#include "com/centreon/broker/database/mysql_result.hh"
#include <cstdlib>
#include <iostream>
#include "com/centreon/broker/exceptions/msg.hh"
#include "com/centreon/exceptions/msg_fmt.hh"

using namespace com::centreon::exceptions;
using namespace com::centreon::broker;
using namespace com::centreon::broker::database;

Expand Down Expand Up @@ -92,9 +93,7 @@ void mysql_result::set(MYSQL_RES* result) {
*
* @return A pointer to the result.
*/
MYSQL_RES* mysql_result::get() {
return _result.get();
}
MYSQL_RES* mysql_result::get() { return _result.get(); }

/**
* Accessor to a column boolean value
Expand All @@ -110,7 +109,7 @@ bool mysql_result::value_as_bool(int idx) {
else if (_row)
retval = _row[idx] ? strtol(_row[idx], nullptr, 10) : 0;
else
throw exceptions::msg() << "mysql: No row fetched in result";
throw msg_fmt("mysql: No row fetched in result");
return retval;
}

Expand All @@ -128,7 +127,7 @@ std::string mysql_result::value_as_str(int idx) {
else if (_row)
retval = (_row[idx]) ? _row[idx] : "";
else
throw exceptions::msg() << "mysql: No row fetched in result";
throw msg_fmt("mysql: No row fetched in result");
return retval;
}

Expand All @@ -146,7 +145,7 @@ float mysql_result::value_as_f32(int idx) {
else if (_row)
retval = _row[idx] ? atof(_row[idx]) : 0;
else
throw exceptions::msg() << "mysql: No row fetched in result";
throw msg_fmt("mysql: No row fetched in result");
return retval;
}

Expand All @@ -164,7 +163,7 @@ double mysql_result::value_as_f64(int idx) {
else if (_row)
retval = _row[idx] ? atof(_row[idx]) : 0;
else
throw exceptions::msg() << "mysql: No row fetched in result";
throw msg_fmt("mysql: No row fetched in result");
return retval;
}

Expand All @@ -182,7 +181,7 @@ int mysql_result::value_as_i32(int idx) {
else if (_row)
retval = _row[idx] ? strtol(_row[idx], nullptr, 10) : 0;
else
throw exceptions::msg() << "mysql: No row fetched in result";
throw msg_fmt("mysql: No row fetched in result");
return retval;
}

Expand All @@ -200,7 +199,7 @@ uint32_t mysql_result::value_as_u32(int idx) {
else if (_row)
retval = _row[idx] ? strtoul(_row[idx], nullptr, 10) : 0;
else
throw exceptions::msg() << "mysql: No row fetched in result";
throw msg_fmt("mysql: No row fetched in result");
return retval;
}

Expand All @@ -218,7 +217,7 @@ unsigned long long mysql_result::value_as_u64(int idx) {
else if (_row)
retval = _row[idx] ? strtoull(_row[idx], nullptr, 10) : 0;
else
throw exceptions::msg() << "mysql: No row fetched in result";
throw msg_fmt("mysql: No row fetched in result");
return retval;
}

Expand All @@ -236,7 +235,7 @@ bool mysql_result::value_is_null(int idx) {
else if (_row)
retval = (_row[idx] == nullptr);
else
throw exceptions::msg() << "mysql: No row fetched in result";
throw msg_fmt("mysql: No row fetched in result");
return retval;
}

Expand Down Expand Up @@ -278,9 +277,7 @@ void mysql_result::set_bind(std::unique_ptr<database::mysql_bind>&& bind) {
*
* @param row The row to affect
*/
void mysql_result::set_row(MYSQL_ROW row) {
_row = row;
}
void mysql_result::set_row(MYSQL_ROW row) { _row = row; }

/**
* Accessord to the bind
Expand All @@ -296,18 +293,14 @@ std::unique_ptr<database::mysql_bind>& mysql_result::get_bind() {
*
* @return an integer
*/
int mysql_result::get_statement_id() const {
return _statement_id;
}
int mysql_result::get_statement_id() const { return _statement_id; }

/**
* Accessor to the parent connection.
*
* @return A pointer the the connection.
*/
mysql_connection* mysql_result::get_connection() {
return _parent;
}
mysql_connection* mysql_result::get_connection() { return _parent; }

int mysql_result::get_num_fields() const {
return mysql_num_fields(_result.get());
Expand Down
47 changes: 23 additions & 24 deletions src/ccb_sql/database/mysql_stmt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
#include <cmath>
#include <functional>

#include "com/centreon/broker/exceptions/msg.hh"
#include "com/centreon/exceptions/msg_fmt.hh"
#include "com/centreon/broker/io/events.hh"
#include "com/centreon/broker/logging/logging.hh"
#include "com/centreon/broker/mapping/entry.hh"

using namespace com::centreon::exceptions;
using namespace com::centreon::broker;
using namespace com::centreon::broker::database;

Expand All @@ -42,7 +43,8 @@ mysql_stmt::mysql_stmt(std::string const& query, bool named) {
char open(0);
int size(0);
for (std::string::const_iterator it(query.begin()), end(query.end());
it != end; ++it) {
it != end;
++it) {
if (in_string) {
if (*it == '\\') {
q.push_back(*it);
Expand Down Expand Up @@ -97,7 +99,7 @@ mysql_stmt::mysql_stmt(std::string const& query, bool named) {

mysql_stmt::mysql_stmt(std::string const& query,
mysql_bind_mapping const& bind_mapping)
: _id(std::hash<std::string>{}(query)),
: _id(std::hash<std::string> {}(query)),
_query(query),
_bind_mapping(bind_mapping) {
if (bind_mapping.empty())
Expand Down Expand Up @@ -130,7 +132,8 @@ int mysql_stmt::_compute_param_count(std::string const& query) {
int retval(0);
bool in_string(false), jocker(false);
for (std::string::const_iterator it(query.begin()), end(query.end());
it != end; ++it) {
it != end;
++it) {
if (!in_string) {
if (*it == '?')
++retval;
Expand All @@ -148,13 +151,9 @@ int mysql_stmt::_compute_param_count(std::string const& query) {
return retval;
}

bool mysql_stmt::prepared() const {
return _id != 0;
}
bool mysql_stmt::prepared() const { return _id != 0; }

int mysql_stmt::get_id() const {
return _id;
}
int mysql_stmt::get_id() const { return _id; }

std::unique_ptr<database::mysql_bind> mysql_stmt::get_bind() {
return std::move(_bind);
Expand All @@ -165,7 +164,8 @@ void mysql_stmt::operator<<(io::data const& d) {
io::event_info const* info(io::events::instance().get_event_info(d.type()));
if (info) {
for (mapping::entry const* current_entry(info->get_mapping());
!current_entry->is_null(); ++current_entry) {
!current_entry->is_null();
++current_entry) {
char const* entry_name = current_entry->get_name_v2();
if (entry_name && entry_name[0]) {
std::string field(":");
Expand Down Expand Up @@ -239,7 +239,7 @@ void mysql_stmt::operator<<(io::data const& d) {
bind_value_as_u32(field, v);
break;
case mapping::entry::invalid_on_minus_one:
if (v == (uint32_t)-1)
if (v == (uint32_t) - 1)
bind_value_as_null(field);
else
bind_value_as_u32(field, v);
Expand All @@ -249,16 +249,19 @@ void mysql_stmt::operator<<(io::data const& d) {
}
} break;
default: // Error in one of the mappings.
throw(exceptions::msg() << "invalid mapping for object "
<< "of type '" << info->get_name()
<< "': " << current_entry->get_type()
<< " is not a known type ID");
throw msg_fmt(
"invalid mapping for object of type '{}': {} is not a known "
"type ID",
info->get_name(),
current_entry->get_type());
};
}
}
} else
throw(exceptions::msg() << "cannot bind object of type " << d.type()
<< " to database query: mapping does not exist");
throw msg_fmt(
"cannot bind object of type {} to database query: mapping does not "
"exist",
d.type());
}

void mysql_stmt::bind_value_as_i32(int range, int value) {
Expand Down Expand Up @@ -527,10 +530,6 @@ void mysql_stmt::bind_value_as_null(std::string const& name) {
}
}

std::string const& mysql_stmt::get_query() const {
return _query;
}
std::string const& mysql_stmt::get_query() const { return _query; }

int mysql_stmt::get_param_count() const {
return _param_count;
}
int mysql_stmt::get_param_count() const { return _param_count; }
Loading