bool Storage::CheckIfStationExsist(string uid) { int count = 0; TRY sql_ << "SELECT count(*) FROM station WHERE uid = :uid", into(count), use(uid); CATCH_MSG("[db/Storage] existsStationByUID: ") return count > 0; }
entity::Station Storage::GetStationByUID(string uid) { entity::Station station; TRY sql_ << "SELECT uid, name, url, refresh_time FROM station WHERE uid = :uid", into(station.uid), into(station.name), into(station.url), into(station.refresh_time), use(uid); CATCH_MSG("[db/Storage] GetStationByUID: ") return station; }
void Storage::UpdateStationLastUpdateIfNeeded( string station_uid, tm last_update) { time_t station_last_update = mktime(&GetStationLastUpdate(station_uid)); time_t measure_last_update = mktime(&last_update); if (measure_last_update > station_last_update) { TRY sql_ << "UPDATE station SET last_update = :last_update " "WHERE uid = :uid", use(last_update), use(station_uid); CATCH_MSG("[db/Storage] UpdateStationLastUpdateIfNeeded: ") }
command peer_disconnect::execute_command(soci::session& db, peer_hit_ptr hit) { using soci::use; const char* update = "update hits set disconnect_time=:time where disconnect_time is NULL and guid=:guid"; string time = to_simple_string(posix_time::second_clock::universal_time()); command cmd = (db.prepare << update, use(time, "time"), use(utils::obj_to_string(hit->peer_id), "guid")); cmd.execute(true); return cmd; }
soci::session& tracer::ensure_db() { string create_table = utils::read_whole_file("scripts/create_table_script.sql"); string check = utils::read_whole_file("scripts/check_table_script.sql"); string create_index = utils::read_whole_file("scripts/create_index_script.sql"); using soci::into; string c = check; int table_exsist = 0; (*db_) << check, into(table_exsist); if(table_exsist == 0) { (*db_) << create_table; //(*db_) << create_index; } return *db_; }
command peer_hit::execute_command(soci::session& db, peer_hit_ptr hit) { try { log::write("Creating DB entry"); using soci::use; std::string insert = utils::read_whole_file("scripts/insert_script.sql"); const gregorian::date& d = hit->time.date(); const posix_time::time_duration& t = hit->time.time_of_day(); const string& ip = hit->ip.to_string(); const string& local_ip = utils::get_local_ip(); const string& guid = utils::obj_to_string(hit->peer_id); const string& t_hash = utils::obj_to_string(hit->t_hash); const string& p_hash = utils::obj_to_string(hit->p_hash); const string& b_hash = utils::obj_to_string(hit->b_hash); command cmd = ( db.prepare << insert, use(ip, "ip"), use(local_ip, "self_ip"), use((unsigned short)d.year(),"year"), use(d.month().as_number(), "month"), use(d.day().as_number(), "day"), use(t.hours(), "hour"), use(t.minutes(), "minute"), use(t.seconds(), "second"), use(guid, "guid"), use(hit->client, "client"), use(t_hash, "t_hash"), use(p_hash, "p_hash"), use(b_hash, "b_hash"), use(hit->port, "port"), use(hit->local_port, "self_port"), use(hit->filename, "filename"), use(hit->path, "path"), use(hit->filesize, "filesize"), use(hit->block_size, "block_size"), use(hit->piece_size, "piece_size"), use(hit->block, "block"), use(hit->piece, "piece"), use(std::string("qbt3.1.9"), "sig") ); log::write("Writing DB entry"); cmd.execute(true); log::write("Entry written"); return cmd; }catch(std::exception e){ log::write(e); log::write("Reconnecting"); db.reconnect(); } }
shared_ptr<Corpus> get(const size_t &id) { shared_ptr<Corpus> corpus = make_shared<Corpus>(); session << getConfig().get<string>("repository.getCorpus"), soci::into(*corpus), use(id); if (!session.got_data()) { return shared_ptr<Corpus>(nullptr); } return corpus; };