#include "msql_acces.h" #include #include #include #include msql_acces::msql_acces() { driver = get_driver_instance(); con = driver->connect("tcp://127.0.0.1:3306", "root", "1234"); con->setSchema("bin_database"); } std::string msql_acces::get_passwd(std::string username){ sql::PreparedStatement *pstmt = con->prepareStatement("select passwd from users where username=?"); pstmt->setString(1,username); sql::ResultSet *res = pstmt->executeQuery(); std::string ret; while(res->next()) ret = res->getString("passwd"); delete res; return ret; } bool msql_acces::get_admin(std::string username){ sql::PreparedStatement *pstmt = con->prepareStatement("select admin from users where username=?"); pstmt->setString(1,username); sql::ResultSet *res = pstmt->executeQuery(); bool ret=false; while(res->next()) ret = res->getBoolean("admin"); delete res; return ret; } std::list msql_acces::get_pinfo(){ sql::PreparedStatement *pstmt = con->prepareStatement("select packages.name, packages.created, packages.uses, users.username " "from packages " "join users on users.id=packages.user "); sql::ResultSet *res = pstmt->executeQuery(); std::list ret; std::string aux; while(res->next()){ aux=""; aux += res->getString(1); aux += ":"+res->getString(2); if(res->getBoolean(3)){ aux+=":t"; }else{ aux+=":f"; } aux += ":"+res->getString(4); ret.push_back(aux); } delete res; return ret; } std::list msql_acces::get_uinfo(){ sql::PreparedStatement *pstmt = con->prepareStatement("select username,admin from users"); sql::ResultSet *res = pstmt->executeQuery(); std::list ret; std::string aux; while(res->next()){ aux=""; aux += res->getString(1); //aux += ":"+res->getString(2); if(res->getBoolean(2)){ aux+=":t"; }else{ aux+=":f"; } ret.push_back(aux); } delete res; return ret; } void msql_acces::write_install(std::string package, std::string user){ sql::PreparedStatement *pstmt = con->prepareStatement("insert into packages(name,user) values(?,(select id from users where username=?))"); pstmt->setString(1,package); pstmt->setString(2,user); pstmt->executeUpdate(); } void msql_acces::write_remove(std::string package){ sql::PreparedStatement *pstmt = con->prepareStatement("delete from packages where name=?"); pstmt->setString(1,package); pstmt->executeUpdate(); } bool msql_acces::get_package_exists(std::string package){ sql::PreparedStatement *pstmt = con->prepareStatement("select count(id) from packages where name=?"); pstmt->setString(1,package); sql::ResultSet *res = pstmt->executeQuery(); int ret=0; while(res->next()) ret = res->getInt(1); return ret>0; } void msql_acces::create_user(std::string user, std::string pass, bool admin){ sql::PreparedStatement *pstmt = con->prepareStatement("insert into users(username, passwd, admin) values(?, ?, ?)"); unsigned char hash[SHA512_DIGEST_LENGTH]; SHA512(reinterpret_cast(pass.data()), strlen(pass.data()), hash); unsigned char encodedData[200]; EVP_EncodeBlock(encodedData, hash, sizeof (hash)); pstmt->setString(1,user); pstmt->setString(2,std::string(reinterpret_cast(encodedData))); pstmt->setBoolean(3,admin); pstmt->executeQuery(); } void msql_acces::remove_user(std::string user){ sql::PreparedStatement *pstmt = con->prepareStatement("delete from users where username=?"); pstmt->setString(1,user); pstmt->executeUpdate(); }