#include "msql_acces.h" #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; } 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; }