Revert "going back to the mysql c++ api"

This reverts commit 2009-01-28T20:38:29Z!thomas.baumhauer@gmail.com.
This commit is contained in:
Gunter Labes 2009-02-01 11:46:52 +00:00
parent 7e47078b14
commit e282467aad
3 changed files with 21 additions and 31 deletions

View file

@ -245,8 +245,7 @@ if env["prereqs"]:
if env["forum_user_handler"]:
env.ParseConfig("mysql_config --libs --cflags")
if conf.CheckLibWithHeader("mysqlpp", "mysql++/mysql++.h", "C++"):
env.Append(CPPDEFINES = ["HAVE_MYSQLPP"])
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.")

View file

@ -27,16 +27,15 @@ fuh::fuh(const config& c) {
db_users_table_ = c["db_users_table"];
db_extra_table_ = c["db_extra_table"];
// 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;
}
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;
}
}
fuh::~fuh() {
mysql_close(conn);
}
void fuh::add_user(const std::string& name, const std::string& mail, const std::string& password) {
@ -137,7 +136,7 @@ bool fuh::user_exists(const std::string& name) {
// Make a test query for this username
try {
return db_query("SELECT username FROM " + db_users_table_ + " WHERE UPPER(username)=UPPER('" + name + "')").num_rows() > 0;
return mysql_fetch_row(db_query("SELECT username FROM " + db_users_table_ + " WHERE UPPER(username)=UPPER('" + name + "')"));
} 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
@ -247,32 +246,24 @@ void fuh::set_lastlogin(const std::string& user, const time_t& lastlogin) {
}
}
mysqlpp::StoreQueryResult fuh::db_query(const std::string& sql) {
try {
mysqlpp::Query query = conn_.query();
query << sql;
return query.store();
} catch(...) {
MYSQL_RES* fuh::db_query(const std::string& sql) {
if(mysql_query(conn, sql.c_str())) {
WRN_UH << "not connected to database, reconnecting..." << 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;
//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;
throw error("Error querying database.");
}
}
return mysql_store_result(conn);
}
std::string fuh::db_query_to_string(const std::string& query) {
return std::string(db_query(query).at(0).at(0));
std::string fuh::db_query_to_string(const std::string& sql) {
return std::string(mysql_fetch_row(db_query(sql))[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 + "')");
}
@ -299,7 +290,7 @@ bool fuh::extra_row_exists(const std::string& name) {
// Make a test query for this username
try {
return db_query("SELECT username FROM " + db_extra_table_ + " WHERE UPPER(username)=UPPER('" + name + "')").num_rows() > 0;
return mysql_fetch_row(db_query("SELECT username FROM " + db_extra_table_ + " WHERE UPPER(username)=UPPER('" + name + "')"));
} catch (error e) {
ERR_UH << "Could not execute test query for user '" << name << "' :" << e.message << std::endl;
return false;

View file

@ -19,7 +19,7 @@
#include <vector>
#include <mysql++/mysql++.h>
#include <mysql/mysql.h>
#include "../md5.hpp"
/**
@ -97,11 +97,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
mysqlpp::StoreQueryResult db_query(const std::string& query);
MYSQL_RES* db_query(const std::string& query);
// Throws user_handler::error via db_query()
std::string db_query_to_string(const std::string& query);
mysqlpp::Connection conn_;
MYSQL *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);