bool ipValidate( database *database, string IP, size_t memberID ) { socketTCP socket("0.0.0.0"); string domain; // Resolve domain/ip IP = socket.hostnameLookup(IP); // Current Connect Client IP domain = socket.ipLookup( IP ); // Resolve the clients IP // Get any IP/Host matches for this member's ID / Connected IP/Domain StoreQueryResult IPs = database->ipsRead( memberID, IP ); StoreQueryResult Domains = database->ipsRead( memberID, domain ); // Did we find any? if(!IPs.empty()) return true; if(!Domains.empty()) return true; if(!IP.size()) return false; // Nope, so we need to get all IPs/Domains for this user from the database // Remove all wildcards and check the domain for matches Domains = database->ipsRead( memberID ); StoreQueryResult::iterator addressIT; string testName; string resolvedIP; string::iterator sIT; Row row; for( addressIT = Domains.begin(); addressIT != Domains.end(); addressIT++ ) { row = *addressIT; // Get an address to test testName = (string) row["ip"]; // Resolve the name from the database, and try it against the connected IP resolvedIP = socket.hostnameLookup( testName ); if( ! IP.compare( resolvedIP ) ) return true; // Remove wildcards sIT = remove( testName.begin(), testName.end(), '*' ); if(sIT != testName.end()) testName.erase(sIT); // Check against the connected IP if( IP.find( testName ) != string::npos ) return true; // Check against the connected domain name if( domain.find( testName ) != string::npos ) return true; } return false; }
/** @brief Returns the list of products (and requested quantity) of an order @return A std::map where the key is product ID and the value the quantity */ map<int, int>& Order::products() { map<int, int> *prd = NULL; // get an instance of the database Database& db = Database::instance(); try { // ask Database for a valid connection to mySQL Connection *conn = db.getConnection(); // obtain an instance of mysqlpp::Query and init it Query q = conn->query(); q << "SELECT * FROM order_details WHERE oid = " << valueForKey(KEY_ORD_OID); StoreQueryResult res = q.store(); if (!res.empty()) { prd = new map<int, int>; StoreQueryResult::const_iterator it; for (it = res.begin(); it != res.end(); it++){ Row row = *it; int key = atoi(row["pid"]); int qty = atoi(row["qty"]); (*prd)[key] = qty; } } } catch (std::exception &e) { cerr << "an error occurred: " << e.what() << endl; } return *prd; }
/** @brief Return the list of all orders of a given user @param[in] pp An instance of User @return A vector of Order */ vector<Order *> & Order::ordersForUser(User & pp) { vector<Order *> *orders = new vector<Order *>; // get an instance of the database Database& db = Database::instance(); try { // ask Database for a valid connection to mySQL Connection *conn = db.getConnection(); // obtain an instance of mysqlpp::Query and init it Query q = conn->query(); q << "SELECT * FROM orders WHERE uid = " << pp.uniqueID() << " ORDER BY oid, date"; StoreQueryResult res = q.store(); if (!res.empty()) { orders->reserve(res.num_rows()); StoreQueryResult::const_iterator it; for (it = res.begin(); it != res.end(); it++){ Row row = *it; orders->push_back(new Order(row)); } } } catch (std::exception &e) { cerr << "an error occurred: " << e.what() << endl; } return *orders; }
bool IRCBot::testPrepare() { StoreQueryResult ircBots = _db->ircBotReadAll(); StoreQueryResult::iterator ircIT; stringstream ircs; size_t sleepTime = 500, count = 0; Row irc, ircBot; _ircMii *botMii; if(ircBots.empty()) return false; // loop through and start each server test for( ircIT = ircBots.begin(); ircIT != ircBots.end(); ++ircIT, ++count ) { ircBot = *ircIT; irc = _db->ircRead( (size_t) ircBot["ircID"] ); if(irc.empty()) return false; botMii = _Miis->ircMiiLoad( (string) irc["nick"] ); if(botMii) { botMii->ircBotRowLoad(); botMii->silentSet(); botTestAdd( botMii, 0 ); } else { stringstream ircs; ircs << "Bot " << ( (string) irc["nick"] ) << " is not online."; speak("Test", ircs, _testChannel); } Sleep(sleepTime); // Every 4, sleep triple time if(count == 4) { Sleep(sleepTime*2); count=0; } } // Nothing to do? if(_testMiis.empty()) { stringstream ircs; ircs << "No bots to test."; speak("Test", ircs, _testChannel); return false; } return true; }
int DocDb::getGlobalSourceIdFromName(string name) { CHECK_CONNECTED(m_wrapper->isConnected); try { string idAsString; StoreQueryResult res; res = m_wrapper->getGlobalSourceIdQuery->store(name); if (res.empty()) return -1; idAsString = string(res[0][0]); return std::stoi(idAsString); } catch (const Exception &ex) { printException(ex); return -1; } }
int DocDb::getSourceIdFromName(int packageId,string name,int parameter_count) { CHECK_CONNECTED(m_wrapper->isConnected); try { string idAsString; StoreQueryResult res; res = m_wrapper->getSourceIdQuery->store(packageId, name, parameter_count); if (res.empty()) return -1; idAsString = string(res[0][0]); return std::stoi(idAsString); } catch (const Exception &ex) { printException(ex); return -1; } }
bool MasterServer::get_fileinfo(const string &fid, FileInfo &fileinfo) { //查找cache map<string, FileInfo>::iterator it = m_fileinfo_cache.find(fid); if(it != m_fileinfo_cache.end()) { fileinfo = it->second; return true; } //查找数据库 if(m_db_connection == NULL) return false; char sql_str[1024]; snprintf(sql_str, 1024, "select fid,name,size,chunkid,chunkip,chunkport,findex,foffset from SFS.fileinfo_%s where fid='%s'" ,fid.substr(0,2).c_str(), fid.c_str()); Query query = m_db_connection->query(sql_str); StoreQueryResult res = query.store(); if (!res || res.empty()) return false; size_t i; for(i=0; i<res.num_rows(); ++i) { ChunkPath chunk_path; fileinfo.fid = res[i]["fid"].c_str(); fileinfo.name = res[i]["name"].c_str(); fileinfo.size = atoi(res[i]["size"].c_str()); chunk_path.id = res[i]["chunkid"].c_str(); chunk_path.ip = res[i]["chunkip"].c_str(); chunk_path.port = atoi(res[i]["chunkport"].c_str()); chunk_path.index = atoi(res[i]["findex"].c_str()); chunk_path.offset = atoi(res[i]["foffset"].c_str()); fileinfo.add_chunkpath(chunk_path); } //添加到cache m_fileinfo_cache.insert(std::make_pair(fileinfo.fid, fileinfo)); return true; }
/** @brief Returns a product with given ID @param[in] aPid The requested product ID @return A pointer to the requested Product, NULL if not found */ Product * Product::productByID(int aPid) { // get an instance of the database Database &db = Database::instance(); // ask Database for a valid connection to mySQL Connection *conn = db.getConnection(); // obtain an instance of mysqlpp::Query and init it Query q = conn->query(); q << SQL_PRODUCT_PROXY << aPid; StoreQueryResult res = q.store(); if (!res.empty()) { StoreQueryResult::const_iterator it = res.begin(); Row row = *it; return new Product(row); } return NULL; }
/** @brief Fetch a category by specifing its ID @param[in] aCid Category ID to be fetched @return An instance of category */ Category *Category::categoryByID(int aCid) { Category *cat = NULL; // get an instance of the database Database &db = Database::instance(); // obtain an instance of mysqlpp::Query and init it Query q = db.getConnection()->query(); q << SQL_CATEGORY_BYID << aCid; StoreQueryResult res = q.store(); if (!res.empty()) { cat = new Category(); cat->setIntForKey(KEY_CAT_CID, aCid); cat->setValueForKey(KEY_CAT_NAME, (string) res[0][KEY_CAT_NAME]); return cat; } return NULL; }
/** @brief Returns the list of available categories. @return Vector of pointer to Category */ vector<Category *> &Category::catalog() { // get an instance of the database Database& db = Database::instance(); vector<Category *> *catalog = NULL; try { // ask Database for a valid connection to mySQL Connection *conn = db.getConnection(); // obtain an instance of mysqlpp::Query and init it Query q = conn->query(SQL_CATEGORY_CAT); StoreQueryResult res = q.store(); if (!res.empty()) { catalog = new vector<Category *>; catalog->reserve(res.num_rows()); for (size_t i = 0; i < res.num_rows(); ++i) { Category *c = new Category(); c->setValueForKey(KEY_CAT_CID, (string) res[i][KEY_CAT_CID]); c->setValueForKey(KEY_CAT_NAME, (string) res[i][KEY_CAT_NAME]); catalog->push_back(c); } } } catch (const mysqlpp::BadQuery& e) { // Something went wrong with the SQL query. cerr << "Query failed: " << e.what() << endl; } catch (const Exception& er) { cerr << "Error: " << er.what() << endl; } return *catalog; }
/** @brief Return the list of products of a given category @param[in] aCid The category ID @return A vector of ProductProxy @note Pass zero as category ID to get all products */ vector<ProductProxy *> & ProductProxy::catalog(int aCid) { // get an instance of the database Database& db = Database::instance(); vector<ProductProxy *> *catalog = NULL; try { // ask Database for a valid connection to mySQL Connection *conn = db.getConnection(); // obtain an instance of mysqlpp::Query and init it Query q = conn->query(); q << SQL_CATALOG_PROXY; if (aCid != 0) q << "WHERE cid = " << aCid; q << " ORDER BY pid, category, name"; StoreQueryResult res = q.store(); if (!res.empty()) { catalog = new vector<ProductProxy *>; catalog->reserve(res.num_rows()); for (size_t i = 0; i < res.num_rows(); ++i) { catalog->push_back(new ProductProxy((int) res[i][0])); } } } catch (const mysqlpp::BadQuery& e) { // Something went wrong with the SQL query. cerr << "Query failed: " << e.what() << endl; } catch (const Exception& er) { cerr << "Error: " << er.what() << endl; } return *catalog; }
/** @brief Obtain a real instance of the Product Concrete creation of an instance of Product class; when client programmer tries to access to some ProductProxy methods, such as getPrice(), a call to this method ensures that the product is instantiated only we really needed. @return A pointer to class Product */ Product *ProductProxy::getProduct() throw (InvalidArgument) { if (!_theProduct) { // get an instance of the database Database &db = Database::instance(); // ask Database for a valid connection to mySQL and obtain a Query Query q = db.getConnection()->query(); // obtain an instance of mysqlpp::Query and init it q << SQL_PRODUCT_PROXY << _pid; StoreQueryResult res = q.store(); if (res.empty()) throw InvalidArgument("PID"); StoreQueryResult::const_iterator it = res.begin(); Row row = *it; _theProduct = new Product(row); } return _theProduct; }
int CViewMng::init(Connection *conn) { CoCycle *cycle = g_cycle; if(conn == NULL) return -1; mDBConn = conn; StoreQueryResult::const_iterator it; Row row ; Query query = mDBConn->query(); query << "select id, ltrim(rtrim(name)), ltrim(rtrim(remark)), " << "interruptable, type from t_view_set"; StoreQueryResult res = query.store(); if(res.empty()) return -2; static char buf[2048]; CodeConverter cc = CodeConverter("utf-8","gb2312"); for (it = res.begin(); it != res.end(); ++it) { row = *it; ViewSet *viewSet = cycle->mPool->newObj<ViewSet>(); assert(viewSet != NULL); viewSet->id = row[0]; memset(buf, 0, sizeof(buf)); cc.convert((char *)row[1].c_str(), row[1].length(), buf, sizeof(buf)); viewSet->name = buf; memset(buf, 0, sizeof(buf)); cc.convert((char *)row[2].c_str(), row[2].length(), buf, sizeof(buf)); viewSet->remark = buf; int interruptable = row[3]; if(interruptable == 0) viewSet->interruptable = true; else viewSet->interruptable = false; viewSet->type = row[4]; insertViewSet(viewSet); } query << "select a.id, ltrim(rtrim(a.name)), ltrim(rtrim(a.content)), " << " ltrim(rtrim(b.name)) from t_view as a " << "left join t_view_set as b on a.view_set_id = b.id"; res = query.store(); if(res.empty()) return -3; for (it = res.begin(); it != res.end(); ++it) { row = *it; View *view = cycle->mPool->newObj<View>(); assert(view != NULL); view->id = row[0]; memset(buf, 0, sizeof(buf)); cc.convert((char *)row[1].c_str(), row[1].length(), buf, sizeof(buf)); view->name = buf; memset(buf, 0, sizeof(buf)); cc.convert((char *)row[2].c_str(), row[2].length(), buf, sizeof(buf)); view->content = buf; insertView(row[3].c_str(), view); } query << "select a.id, ltrim(rtrim(a.name)), ltrim(rtrim(a.title)), " << " ltrim(rtrim(a.action)), a.type, ltrim(rtrim(b.name)) " <<"from t_click_action as a left join t_view_set as b " << "on a.view_set_id = b.id"; res = query.store(); if(res.empty()) return -4; for (it = res.begin(); it != res.end(); ++it) { row = *it; ClickAction *clickAction = cycle->mPool->newObj<ClickAction>(); assert(clickAction != NULL); clickAction->id = row[0]; memset(buf, 0, sizeof(buf)); cc.convert((char *)row[1].c_str(), row[1].length(), buf, sizeof(buf)); clickAction->name = buf; memset(buf, 0, sizeof(buf)); cc.convert((char *)row[2].c_str(), row[2].length(), buf, sizeof(buf)); clickAction->title = buf; memset(buf, 0, sizeof(buf)); cc.convert((char *)row[3].c_str(), row[3].length(), buf, sizeof(buf)); clickAction->action = buf; clickAction->type = row[4]; insertClickAction(row[5].c_str(), clickAction); } return 0; }
bool AdUserFeatureCacheI::HasNewFile(){ stringstream sql; sql << " SELECT local_file, md5 FROM user_feature_version " << " WHERE id = (SELECT max(id) FROM user_feature_version) " << " AND valid = 1 "; StoreQueryResult res; try{ const char* DB_HOST = "10.3.23.69"; const char* DB_NAME = "ad_strategy"; const char* DB_USER = "******"; const char* DB_PASSWORD = "******"; Connection conn(DB_NAME, DB_HOST, DB_USER, DB_PASSWORD); Query query(&conn, true, sql.str().c_str()); res = query.store(); } catch (Ice::Exception& e){ MCE_WARN("AdUserFeatureCacheI::HasNewVersion user_feature_version ice error! " << e.what()); return false; } catch (std::exception& e){ MCE_WARN("AdUserFeatureCacheI::HasNewVersion user_feature_version std error! " << e.what()); return false; } if (!res){ MCE_WARN("Query Result Error!"); return false; } else if (res.empty()){ MCE_INFO("nothing in db!"); return false; } else if (res.size() > 1){ MCE_WARN("There dirty data in user_feature_version."); return false; } string fileName = lexical_cast<string>(res.at(0)["local_file"]); string md5Value = lexical_cast<string>(res.at(0)["md5"]); //Loading Data of the newFileName now, not finished. if (isLoading_){ MCE_INFO("The File: " << newFileName_ << " is loading."); return false; } /*1). Never Load Data Before. initOK_ == false *2). Already there is data in cache, and new file is ready. * newFileName != data's fileName in cache, * and the newFile content md5 != data's content md5 in cache. */ if ((!initOK_) || ( (fileName != userFeatureDataPtr_->GetFileName()) && (md5Value != userFeatureDataPtr_->GetMd5Value()) ) ){ IceUtil::RWRecMutex::WLock lock(VERSION_MUTEX); { newFileName_ = fileName; newMd5Value_ = md5Value;; } return true; } return false; }