ServidorTFG/msql_acces.cpp

92 lines
2.5 KiB
C++
Raw Normal View History

2020-05-08 12:43:46 +02:00
#include "msql_acces.h"
#include <iostream>
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;
}
2020-05-13 00:58:32 +02:00
std::list<std::string> 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 ");
2020-05-13 00:58:32 +02:00
sql::ResultSet *res = pstmt->executeQuery();
std::list<std::string> ret;
std::string aux;
while(res->next()){
aux="";
aux += res->getString(1);
aux += ":"+res->getString(2);
if(res->getBoolean(3)){
aux+=":t";
2020-05-13 00:58:32 +02:00
}else{
aux+=":f";
2020-05-13 00:58:32 +02:00
}
aux += ":"+res->getString(4);
ret.push_back(aux);
2020-05-13 00:58:32 +02:00
}
delete res;
return ret;
}
2020-05-15 21:39:23 +02:00
2020-05-20 00:10:05 +02:00
std::list<std::string> msql_acces::get_uinfo(){
sql::PreparedStatement *pstmt =
con->prepareStatement("select username,admin from users");
sql::ResultSet *res = pstmt->executeQuery();
std::list<std::string> 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;
}
2020-05-15 21:39:23 +02:00
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();
}
2020-05-16 20:52:50 +02:00
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;
}