From cd9c7ad00bb7736928e47557d3c14a06d680fb9d Mon Sep 17 00:00:00 2001 From: HanzZ Date: Sat, 12 Mar 2011 00:55:40 +0100 Subject: [PATCH] setUser/getUser with SQLite3 --- examples/usermanager/main.cpp | 1 + include/transport/sqlite3backend.h | 4 ++ src/config.cpp | 1 + src/sqlite3backend.cpp | 77 ++++++++++++++++++++++++++++-- src/userregistration.cpp | 3 +- 5 files changed, 79 insertions(+), 7 deletions(-) diff --git a/examples/usermanager/main.cpp b/examples/usermanager/main.cpp index d2a9b5da..8856a497 100644 --- a/examples/usermanager/main.cpp +++ b/examples/usermanager/main.cpp @@ -28,6 +28,7 @@ int main(void) UserManager userManager(&transport); UserRegistration userRegistration(&transport, &userManager, &sql); + logger.setUserRegistration(&userRegistration); transport.connect(); eventLoop.run(); diff --git a/include/transport/sqlite3backend.h b/include/transport/sqlite3backend.h index c6ee0acb..8f7004bc 100644 --- a/include/transport/sqlite3backend.h +++ b/include/transport/sqlite3backend.h @@ -85,6 +85,10 @@ class SQLite3Backend : public StorageBackend sqlite3 *m_db; Config *m_config; std::string m_prefix; + + // statements + sqlite3_stmt *m_setUser; + sqlite3_stmt *m_getUser; }; } diff --git a/src/config.cpp b/src/config.cpp index 4657b199..2ad0a32b 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -42,6 +42,7 @@ bool Config::load(const std::string &configfile, boost::program_options::options ("registration.instructions", value()->default_value(""), "Instructions showed to user in registration form") ("registration.username_field", value()->default_value(""), "Label for username field") ("registration.username_mask", value()->default_value(""), "Username mask") + ("registration.encoding", value()->default_value("en"), "Default encoding in registration form") ("database.database", value()->default_value(""), "Database used to store data") ("database.prefix", value()->default_value(""), "Prefix of tables in database") ; diff --git a/src/sqlite3backend.cpp b/src/sqlite3backend.cpp index bfeb042b..ee6e0b37 100644 --- a/src/sqlite3backend.cpp +++ b/src/sqlite3backend.cpp @@ -22,6 +22,24 @@ #include #define SQLITE_DB_VERSION 3 +#define CHECK_DB_RESPONSE(stmt) \ + if(stmt) { \ + sqlite3_exec(m_db, "ROLLBACK;", NULL, NULL, NULL); \ + return 0; \ + } + +// Prepare the SQL statement +#define PREP_STMT(sql, str) \ + if(sqlite3_prepare_v2(m_db, std::string(str).c_str(), -1, &sql, NULL)) { \ + onStorageError(str, (sqlite3_errmsg(m_db) == NULL ? "" : sqlite3_errmsg(m_db))); \ + return false; \ + } + +// Finalize the prepared statement +#define FINALIZE_STMT(prep) \ + if(prep != NULL) { \ + sqlite3_finalize(prep); \ + } using namespace boost; @@ -35,16 +53,33 @@ SQLite3Backend::SQLite3Backend(Config *config) { SQLite3Backend::~SQLite3Backend(){ if (m_db) { + // Would be nice to use this: + // + // sqlite3_stmt *pStmt; + // while((pStmt = sqlite3_next_stmt(db, 0)) != 0 ) { + // sqlite3_finalize(pStmt); + // } + // + // But requires SQLite3 >= 3.6.0 beta + + FINALIZE_STMT(m_setUser); sqlite3_close(m_db); } } bool SQLite3Backend::connect() { if (sqlite3_open(CONFIG_STRING(m_config, "database.database").c_str(), &m_db)) { - sqlite3_close(m_db); - return false; + sqlite3_close(m_db); + return false; } - return createDatabase(); + + if (createDatabase() == false) + return false; + + PREP_STMT(m_setUser, "INSERT INTO " + m_prefix + "users (jid, uin, password, language, encoding, last_login, vip) VALUES (?, ?, ?, ?, ?, DATETIME('NOW'), ?)"); + PREP_STMT(m_getUser, "SELECT id, jid, uin, password, encoding, language, vip FROM " + m_prefix + "users WHERE jid=?"); + + return true; } bool SQLite3Backend::createDatabase() { @@ -116,11 +151,42 @@ bool SQLite3Backend::exec(const std::string &query) { } void SQLite3Backend::setUser(const UserInfo &user) { - + sqlite3_reset(m_setUser); + sqlite3_bind_text(m_setUser, 1, user.jid.c_str(), -1, SQLITE_STATIC); + sqlite3_bind_text(m_setUser, 2, user.uin.c_str(), -1, SQLITE_STATIC); + sqlite3_bind_text(m_setUser, 3, user.password.c_str(), -1, SQLITE_STATIC); + sqlite3_bind_text(m_setUser, 4, user.language.c_str(), -1, SQLITE_STATIC); + sqlite3_bind_text(m_setUser, 5, user.encoding.c_str(), -1, SQLITE_STATIC); + sqlite3_bind_int (m_setUser, 6, user.vip); + + if(sqlite3_step(m_setUser) != SQLITE_DONE) { + onStorageError("setUser query", (sqlite3_errmsg(m_db) == NULL ? "" : sqlite3_errmsg(m_db))); + } } bool SQLite3Backend::getUser(const std::string &barejid, UserInfo &user) { - return true; +// SELECT id, jid, uin, password, encoding, language, vip FROM " + m_prefix + "users WHERE jid=? + sqlite3_reset(m_getUser); + sqlite3_bind_text(m_getUser, 1, barejid.c_str(), -1, SQLITE_TRANSIENT); + + int ret; + while((ret = sqlite3_step(m_getUser)) == SQLITE_ROW) { + user.id = sqlite3_column_int(m_getUser, 0); + user.jid = (const char *) sqlite3_column_text(m_getUser, 1); + user.uin = (const char *) sqlite3_column_text(m_getUser, 2); + std::cout << user.uin << "\n"; + user.password = (const char *) sqlite3_column_text(m_getUser, 3); + user.language = (const char *) sqlite3_column_text(m_getUser, 4); + user.encoding = (const char *) sqlite3_column_text(m_getUser, 5); + user.vip = sqlite3_column_int(m_getUser, 6); + return true; + } + + if (ret != SQLITE_DONE) { + onStorageError("getUser query", (sqlite3_errmsg(m_db) == NULL ? "" : sqlite3_errmsg(m_db))); + } + + return false; } void SQLite3Backend::setUserOnline(long id, bool online) { @@ -132,6 +198,7 @@ bool SQLite3Backend::getBuddies(long id, std::list &roster) { } bool SQLite3Backend::removeUser(long id) { + return true; } diff --git a/src/userregistration.cpp b/src/userregistration.cpp index 14f952f7..0254df90 100644 --- a/src/userregistration.cpp +++ b/src/userregistration.cpp @@ -147,7 +147,7 @@ bool UserRegistration::handleGetRequest(const Swift::JID& from, const Swift::JID std::string instructions = CONFIG_STRING(m_config, "registration.instructions"); reg->setInstructions(instructions); - reg->setRegistered(res.id != -1); + reg->setRegistered(registered); reg->setUsername(res.uin); if (CONFIG_STRING(m_config, "service.protocol") != "twitter" && CONFIG_STRING(m_config, "service.protocol") != "bonjour") reg->setPassword(res.password); @@ -349,7 +349,6 @@ bool UserRegistration::handleSetRequest(const Swift::JID& from, const Swift::JID res.language = language; res.encoding = encoding; res.vip = 0; - registerUser(res); } else {