Skip to content

Commit

Permalink
Fix issue with method field
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin Rakush committed Feb 3, 2020
1 parent 4a5d986 commit 30351df
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 23 deletions.
5 changes: 3 additions & 2 deletions include/proxysql_restapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ class Restapi_Row {
unsigned int id;
bool is_active;
unsigned int interval_ms;
std::string method;
std::string uri;
std::string script;
std::string comment;
unsigned int version;
Restapi_Row(unsigned int _id, bool _is_active, unsigned int _in, const std::string& _uri, const std::string& _script, const std::string& _comment);
Restapi_Row(unsigned int _id, bool _is_active, unsigned int _in, const std::string& _method, const std::string& _uri, const std::string& _script, const std::string& _comment);
};

class ProxySQL_Restapi {
Expand All @@ -34,7 +35,7 @@ class ProxySQL_Restapi {
rwlock_t rwlock;
#endif
std::vector<Restapi_Row> Restapi_Rows;
void update_table(SQLite3_result *result);
void update_restapi_table(SQLite3_result *result);
void load_restapi_to_runtime();
void save_restapi_runtime_to_database(bool);
void flush_restapi__from_memory_to_disk();
Expand Down
3 changes: 0 additions & 3 deletions lib/ProxySQL_Admin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10518,9 +10518,6 @@ void ProxySQL_Admin::disk_upgrade_mysql_users() {
configdb->execute("PRAGMA foreign_keys = ON");
}

Restapi_Row::Restapi_Row(unsigned int _id, bool _is_active, unsigned int _in, const std::string& _uri, const std::string& _script, const std::string& _comment) :
id(_id), is_active(_is_active), interval_ms(_in), uri(_uri), script(_script), comment(_comment) {}

Scheduler_Row::Scheduler_Row(unsigned int _id, bool _is_active, unsigned int _in, char *_f, char *a1, char *a2, char *a3, char *a4, char *a5, char *_comment) {
int i;
id=_id;
Expand Down
17 changes: 12 additions & 5 deletions lib/ProxySQL_Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,10 @@ int ProxySQL_Config::Write_Restapi_to_configfile(std::string& data) {
addField(data, "id", r->fields[0], "");
addField(data, "active", r->fields[1], "");
addField(data, "interval_ms", r->fields[2], "");
addField(data, "uri", r->fields[3]);
addField(data, "script", r->fields[4]);
addField(data, "comment", r->fields[5]);
addField(data, "method", r->fields[3], "");
addField(data, "uri", r->fields[4]);
addField(data, "script", r->fields[5]);
addField(data, "comment", r->fields[6]);

data += "\t}";
isNext = true;
Expand All @@ -386,15 +387,15 @@ int ProxySQL_Config::Read_Restapi_from_configfile() {
int i;
int rows=0;
admindb->execute("PRAGMA foreign_keys = OFF");
char *q=(char *)"INSERT OR REPLACE INTO restapi (id, active, interval_ms, uri, script, comment) VALUES (%d, %d, %d, '%s', '%s', '%s')";
char *q=(char *)"INSERT OR REPLACE INTO restapi VALUES (%d, %d, %d, '%s', '%s', '%s', '%s')";
for (i=0; i< count; i++) {
const Setting &route = routes[i];
int id;
int active=1;
// variable for parsing interval_ms
int interval_ms=0;


std::string method;
std::string uri;
std::string script;
std::string comment="";
Expand All @@ -406,6 +407,10 @@ int ProxySQL_Config::Read_Restapi_from_configfile() {
}
route.lookupValue("active", active);
route.lookupValue("interval_ms", interval_ms);
if (route.lookupValue("method", method)==false) {
proxy_error("Admin: detected a restapi route in config file without a mandatory method\n");
continue;
}
if (route.lookupValue("uri", uri)==false) {
proxy_error("Admin: detected a restapi route in config file without a mandatory uri\n");
continue;
Expand All @@ -421,6 +426,7 @@ int ProxySQL_Config::Read_Restapi_from_configfile() {
strlen(std::to_string(id).c_str()) +
strlen(std::to_string(active).c_str()) +
strlen(std::to_string(interval_ms).c_str()) +
strlen(method.c_str()) +
strlen(uri.c_str()) +
strlen(script.c_str()) +
strlen(comment.c_str()) +
Expand All @@ -429,6 +435,7 @@ int ProxySQL_Config::Read_Restapi_from_configfile() {
sprintf(query, q,
id, active,
interval_ms,
method.c_str(),
uri.c_str(),
script.c_str(),
comment.c_str()
Expand Down
18 changes: 8 additions & 10 deletions lib/ProxySQL_Restapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

#include <sstream>

Restapi_Row::Restapi_Row(unsigned int _id, bool _is_active, unsigned int _in, const std::string& _method, const std::string& _uri, const std::string& _script, const std::string& _comment) :
id(_id), is_active(_is_active), interval_ms(_in), method(_method), uri(_uri), script(_script), comment(_comment) {}

ProxySQL_Restapi::ProxySQL_Restapi(SQLite3DB* db) {
assert(db);

Expand All @@ -20,7 +23,7 @@ ProxySQL_Restapi::ProxySQL_Restapi(SQLite3DB* db) {

ProxySQL_Restapi::~ProxySQL_Restapi() {}

void ProxySQL_Restapi::update_table(SQLite3_result *resultset) {
void ProxySQL_Restapi::update_restapi_table(SQLite3_result *resultset) {
#ifdef PA_PTHREAD_MUTEX
pthread_rwlock_wrlock(&rwlock);
#else
Expand All @@ -35,7 +38,7 @@ void ProxySQL_Restapi::update_table(SQLite3_result *resultset) {
is_active=true;
}
unsigned int interval_ms=strtoul(r->fields[2], NULL, 10);
Restapi_Rows.push_back({id, is_active, interval_ms, r->fields[3], r->fields[4], r->fields[5]});
Restapi_Rows.push_back({id, is_active, interval_ms, r->fields[3], r->fields[4], r->fields[5], r->fields[6]});
}

// increase version
Expand Down Expand Up @@ -76,7 +79,7 @@ void ProxySQL_Restapi::save_restapi_runtime_to_database(bool _runtime) {
for (auto r : Restapi_Rows) {
std::stringstream ss;
ss << "INSERT INTO " << table << " VALUES(" << r.id << "," << r.is_active << ","
<< r.interval_ms << ",'" << r.uri << "','" << r.script << "','" << r.comment << "')";
<< r.interval_ms << ",'" << r.method << "','" << r.uri << "','" << r.script << "','" << r.comment << "')";

admindb->execute(ss.str().c_str());
}
Expand All @@ -90,18 +93,13 @@ void ProxySQL_Restapi::save_restapi_runtime_to_database(bool _runtime) {

void ProxySQL_Restapi::load_restapi_to_runtime() {
char *error=NULL;
int cols=0;
int affected_rows=0;
SQLite3_result *resultset=NULL;
char *query=(char *)"SELECT * FROM restapi_routes";
admindb->execute_statement(query, &error , &cols , &affected_rows , &resultset);
std::unique_ptr<SQLite3_result> resultset = std::unique_ptr<SQLite3_result>(admindb->execute_statement(query, &error));
if (error) {
proxy_error("Error on %s : %s\n", query, error);
} else {
update_table(resultset);
update_restapi_table(resultset.get());
}
if (resultset) delete resultset;
resultset=NULL;
}


3 changes: 2 additions & 1 deletion test/tap/tests/proxysql_reference_select_config_file.cnf
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,9 @@ restapi:
id=1
active=1
interval_ms=1000
method=GET
uri="test"
script="/script.py"
script="./scripts/script.py"
comment="comment"
}
)
Expand Down
4 changes: 2 additions & 2 deletions test/tap/tests/select_config_file-t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ int main(int argc, char** argv) {
MYSQL_QUERY(mysql, "insert into proxysql_servers (hostname, port, weight, comment) values ('hostname', 3333, 12, 'comment');");
MYSQL_QUERY(mysql, "insert into scheduler (id, active, interval_ms, filename, arg1, arg2, arg3, arg4, arg5, comment) values "
" (1,1,1000,'filename','a1','a2','a3','a4','a5','comment');");
MYSQL_QUERY(mysql, "insert into restapi_routes (id, active, interval_ms, uri, script, comment) values "
" (1,1,1000,'test','/script.py','comment');");
MYSQL_QUERY(mysql, "insert into restapi_routes values "
" (1,1,1000,'GET', 'test','./scripts/script.py','comment');");
MYSQL_QUERY(mysql, "insert into mysql_aws_aurora_hostgroups (writer_hostgroup, reader_hostgroup, active, aurora_port, "
" domain_name, max_lag_ms, check_interval_ms, check_timeout_ms, writer_is_also_reader, new_reader_weight, "
" add_lag_ms, min_lag_ms, lag_num_checks, comment) "
Expand Down

0 comments on commit 30351df

Please sign in to comment.