#include "ocelot.h" #include "db.h" #include #include #include #include #include mysql::mysql(std::string mysql_db, std::string mysql_host, std::string username, std::string password) { if(!conn.connect(mysql_db.c_str(), mysql_host.c_str(), username.c_str(), password.c_str(), 0)) { std::cout << "Could not connect to MySQL" << std::endl; return; } update_user_buffer = ""; update_torrent_buffer = ""; update_peer_buffer = ""; update_snatch_buffer = ""; } void mysql::load_torrents(std::unordered_map &torrents) { mysqlpp::Query query = conn.query("SELECT ID, info_hash, freetorrent, Snatched FROM torrents ORDER BY ID;"); if(mysqlpp::StoreQueryResult res = query.store()) { mysqlpp::String one("1"); // Hack to get around bug in mysql++3.0.0 size_t num_rows = res.num_rows(); for(size_t i = 0; i < num_rows; i++) { std::string info_hash; res[i][1].to_string(info_hash); torrent t; t.id = res[i][0]; if(res[i][2].compare(one) == 0) { t.free_torrent = true; } else { t.free_torrent = false; } t.balance = 0; t.completed = res[i][3]; t.last_selected_seeder = ""; torrents[info_hash] = t; } } } void mysql::load_users(std::unordered_map &users) { mysqlpp::Query query = conn.query("SELECT ID, can_leech, torrent_pass FROM users_main WHERE Enabled='1';"); if(mysqlpp::StoreQueryResult res = query.store()) { size_t num_rows = res.num_rows(); for(size_t i = 0; i < num_rows; i++) { std::string passkey; res[i][2].to_string(passkey); user u; u.id = res[i][0]; u.can_leech = res[i][1]; users[passkey] = u; } } } void mysql::load_whitelist(std::vector &whitelist) { mysqlpp::Query query = conn.query("SELECT peer_id FROM xbt_client_whitelist;"); if(mysqlpp::StoreQueryResult res = query.store()) { size_t num_rows = res.num_rows(); for(size_t i = 0; i