going back to the mysql c++ api
This commit is contained in:
parent
4500ebe0a0
commit
abb77e0d4c
3 changed files with 31 additions and 21 deletions
|
@ -245,7 +245,8 @@ if env["prereqs"]:
|
|||
|
||||
if env["forum_user_handler"]:
|
||||
env.ParseConfig("mysql_config --libs --cflags")
|
||||
env.Append(CPPDEFINES = ["HAVE_MYSQLPP"])
|
||||
if conf.CheckLibWithHeader("mysqlpp", "mysql++/mysql++.h", "C++"):
|
||||
env.Append(CPPDEFINES = ["HAVE_MYSQLPP"])
|
||||
|
||||
if env["python"]:
|
||||
env["python"] = (float(sys.version[:3]) >= 2.4) and conf.CheckPython() or Warning("Python >= 2.4 not found. Python extensions will be disabled.")
|
||||
|
|
|
@ -27,15 +27,16 @@ fuh::fuh(const config& c) {
|
|||
db_users_table_ = c["db_users_table"];
|
||||
db_extra_table_ = c["db_extra_table"];
|
||||
|
||||
conn = mysql_init(NULL);
|
||||
|
||||
if(!conn || !mysql_real_connect(conn, db_host_.c_str(), db_user_.c_str(), db_password_.c_str(), db_name_.c_str(), 0, NULL, 0)) {
|
||||
ERR_UH << "Could not connect to database: " << mysql_errno(conn) << ": " << mysql_error(conn) << std::endl;
|
||||
// Connect to the database
|
||||
try {
|
||||
conn_.connect(db_name_.c_str(), db_host_.c_str(), db_user_.c_str(), db_password_.c_str());
|
||||
} catch(...) {
|
||||
ERR_UH << "Could not connect to database: " << conn_.error() << std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fuh::~fuh() {
|
||||
mysql_close(conn);
|
||||
}
|
||||
|
||||
void fuh::add_user(const std::string& name, const std::string& mail, const std::string& password) {
|
||||
|
@ -136,7 +137,7 @@ bool fuh::user_exists(const std::string& name) {
|
|||
|
||||
// Make a test query for this username
|
||||
try {
|
||||
return mysql_fetch_row(db_query("SELECT username FROM " + db_users_table_ + " WHERE UPPER(username)=UPPER('" + name + "')"));
|
||||
return db_query("SELECT username FROM " + db_users_table_ + " WHERE UPPER(username)=UPPER('" + name + "')").num_rows() > 0;
|
||||
} catch (error e) {
|
||||
ERR_UH << "Could not execute test query for user '" << name << "' :" << e.message << std::endl;
|
||||
// If the database is down just let all usernames log in
|
||||
|
@ -246,24 +247,32 @@ void fuh::set_lastlogin(const std::string& user, const time_t& lastlogin) {
|
|||
}
|
||||
}
|
||||
|
||||
MYSQL_RES* fuh::db_query(const std::string& sql) {
|
||||
if(mysql_query(conn, sql.c_str())) {
|
||||
mysqlpp::StoreQueryResult fuh::db_query(const std::string& sql) {
|
||||
try {
|
||||
mysqlpp::Query query = conn_.query();
|
||||
query << sql;
|
||||
return query.store();
|
||||
} catch(...) {
|
||||
WRN_UH << "not connected to database, reconnecting..." << std::endl;
|
||||
//Try to reconnect and execute query again
|
||||
if(!mysql_real_connect(conn, db_host_.c_str(), db_user_.c_str(), db_password_.c_str(), db_name_.c_str(), 0, NULL, 0)
|
||||
|| mysql_query(conn, sql.c_str())) {
|
||||
ERR_UH << "Could not connect to database: " << mysql_errno(conn) << ": " << mysql_error(conn) << std::endl;
|
||||
//Try to reconnect
|
||||
try {
|
||||
conn_.connect(db_name_.c_str(), db_host_.c_str(), db_user_.c_str(), db_password_.c_str());
|
||||
|
||||
// Execute the query again
|
||||
mysqlpp::Query query = conn_.query();
|
||||
query << sql;
|
||||
return query.store();
|
||||
} catch(...) {
|
||||
ERR_UH << "Could not connect to database: " << conn_.error() << std::endl;
|
||||
throw error("Error querying database.");
|
||||
}
|
||||
}
|
||||
return mysql_store_result(conn);
|
||||
}
|
||||
|
||||
std::string fuh::db_query_to_string(const std::string& sql) {
|
||||
return std::string(mysql_fetch_row(db_query(sql))[0]);
|
||||
std::string fuh::db_query_to_string(const std::string& query) {
|
||||
return std::string(db_query(query).at(0).at(0));
|
||||
}
|
||||
|
||||
|
||||
std::string fuh::get_detail_for_user(const std::string& name, const std::string& detail) {
|
||||
return db_query_to_string("SELECT " + detail + " FROM " + db_users_table_ + " WHERE UPPER(username)=UPPER('" + name + "')");
|
||||
}
|
||||
|
@ -290,7 +299,7 @@ bool fuh::extra_row_exists(const std::string& name) {
|
|||
|
||||
// Make a test query for this username
|
||||
try {
|
||||
return mysql_fetch_row(db_query("SELECT username FROM " + db_extra_table_ + " WHERE UPPER(username)=UPPER('" + name + "')"));
|
||||
return db_query("SELECT username FROM " + db_extra_table_ + " WHERE UPPER(username)=UPPER('" + name + "')").num_rows() > 0;
|
||||
} catch (error e) {
|
||||
ERR_UH << "Could not execute test query for user '" << name << "' :" << e.message << std::endl;
|
||||
return false;
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
|
||||
#include <vector>
|
||||
|
||||
#include <mysql/mysql.h>
|
||||
#include <mysql++/mysql++.h>
|
||||
#include "../md5.hpp"
|
||||
|
||||
/**
|
||||
|
@ -95,11 +95,11 @@ class fuh : public user_handler {
|
|||
std::string db_name_, db_host_, db_user_, db_password_, db_users_table_, db_extra_table_;
|
||||
|
||||
// Throws user_handler::error
|
||||
MYSQL_RES* db_query(const std::string& query);
|
||||
mysqlpp::StoreQueryResult db_query(const std::string& query);
|
||||
|
||||
// Throws user_handler::error via db_query()
|
||||
std::string db_query_to_string(const std::string& query);
|
||||
MYSQL *conn;
|
||||
mysqlpp::Connection conn_;
|
||||
|
||||
// Query a detail for a particular user from the database
|
||||
std::string get_detail_for_user(const std::string& name, const std::string& detail);
|
||||
|
|
Loading…
Add table
Reference in a new issue