From b3b934dd4a8549f932a78a1358bd064913813f49 Mon Sep 17 00:00:00 2001 From: Alexander MAZUROV Date: Sat, 25 Oct 2008 19:34:26 +0200 Subject: [PATCH] First commit --- .cproject | 977 +++++++++++++++++++++++++++++++++++++++++++ .project | 82 ++++ include/cdb-db/cdb-db.h | 43 ++ include/cdb-db/cdb-profile.h | 47 +++ src/cdb-db.cc | 75 ++++ src/cdb-profile-impl.h | 38 ++ src/cdb-profile.cc | 14 + 7 files changed, 1276 insertions(+) create mode 100644 .cproject create mode 100644 .project create mode 100644 include/cdb-db/cdb-db.h create mode 100644 include/cdb-db/cdb-profile.h create mode 100644 src/cdb-db.cc create mode 100644 src/cdb-profile-impl.h create mode 100644 src/cdb-profile.cc diff --git a/.cproject b/.cproject new file mode 100644 index 0000000..b867f8d --- /dev/null +++ b/.cproject @@ -0,0 +1,977 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..ebea01b --- /dev/null +++ b/.project @@ -0,0 +1,82 @@ + + + libcdb-db + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + ?name? + + + + org.eclipse.cdt.make.core.append_environment + true + + + org.eclipse.cdt.make.core.autoBuildTarget + all + + + org.eclipse.cdt.make.core.buildArguments + + + + org.eclipse.cdt.make.core.buildCommand + make + + + org.eclipse.cdt.make.core.buildLocation + ${workspace_loc:/libcdb-mysql/Debug} + + + org.eclipse.cdt.make.core.cleanBuildTarget + clean + + + org.eclipse.cdt.make.core.contents + org.eclipse.cdt.make.core.activeConfigSettings + + + org.eclipse.cdt.make.core.enableAutoBuild + false + + + org.eclipse.cdt.make.core.enableCleanBuild + true + + + org.eclipse.cdt.make.core.enableFullBuild + true + + + org.eclipse.cdt.make.core.fullBuildTarget + all + + + org.eclipse.cdt.make.core.stopOnError + true + + + org.eclipse.cdt.make.core.useDefaultBuildCmd + true + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + + + + + + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.core.cnature + + diff --git a/include/cdb-db/cdb-db.h b/include/cdb-db/cdb-db.h new file mode 100644 index 0000000..a93a0d5 --- /dev/null +++ b/include/cdb-db/cdb-db.h @@ -0,0 +1,43 @@ +#ifndef CDB_DB_CDBDB_H_ +#define CDBDB_CDBDB_H_ + +#include +#include + +DECLARE_string(db_host); +DECLARE_string(db_name); +DECLARE_int32(db_port); +DECLARE_string(db_user); +DECLARE_string(db_passwd); +// ============================================================================= +// Forward declaration +// ============================================================================= +namespace mysqlpp { + class Connection; +} +namespace cdb_db{ + class CdbProfile; +} +// ============================================================================= +namespace cdb_db{ +// ============================================================================= +typedef std::vector CdbProfilesList; +// ============================================================================= +class CdbDb{ +public: +// ----------------------------------------------------------------------------- + CdbDb(); + ~CdbDb(); +// ----------------------------------------------------------------------------- + bool connect(); + CdbProfilesList* profiles(); +// ----------------------------------------------------------------------------- +private: + CdbProfilesList* profiles_; + mysqlpp::Connection* connection_; + +}; +// ============================================================================= +} +// ============================================================================= +#endif // CDB_DB_CDBDB_H_ diff --git a/include/cdb-db/cdb-profile.h b/include/cdb-db/cdb-profile.h new file mode 100644 index 0000000..3df41cc --- /dev/null +++ b/include/cdb-db/cdb-profile.h @@ -0,0 +1,47 @@ +/* + * cdb-profile.h + * + * Created on: Oct 24, 2008 + * Author: Alexander MAZUROV (alexander.mazurov@gmail.com) + */ + +#ifndef CDB_DB_CDBPROFILE_H_ +#define CDB_DB_CDBPROFILE_H_ + +#include +#include +#include +#include +#include + +namespace cdb_db{ +// ============================================================================= +namespace{ + template inline bool fromString(const std::string& value,T& result) + { + std::stringstream input(value); + if ( !(input >> result)) return false; + return true; + } +} +// ============================================================================= +class CdbProfile { +public: + const std::string& obj() const { return obj_;} + const time_t& mtime() const { return mtime_;} + template bool property(const std::string& path); +protected: + virtual bool property(const std::string& path, const std::string&value) = 0; + virtual bool property(const std::string& path, const std::vector& values) = 0; + CdbProfile(const std::string& obj,const time_t& mtime):obj_(obj),mtime_(mtime){} + //void set_obj(const std::string& obj){obj_ = obj;} + //void set_mtime(){mtime_ = mtime;} +private: + std::string obj_; + time_t mtime_; + +}; +// ============================================================================= +} +// ============================================================================= +#endif /* CDB_DB_CDBPROFILE_H_ */ diff --git a/src/cdb-db.cc b/src/cdb-db.cc new file mode 100644 index 0000000..98cccae --- /dev/null +++ b/src/cdb-db.cc @@ -0,0 +1,75 @@ +#include "cdb-db/cdb-db.h" + +#include +#include + +#include "cdb-profile-impl.h" +// ============================================================================= +DEFINE_string(db_host,"localhost","Host of mysql"); +DEFINE_int32(db_port,3306,"Port of cdb's database"); +DEFINE_string(db_name,"cdb","Database name where cdb information stored"); +DEFINE_string(db_user,"root","User name for access database"); +DEFINE_string(db_passwd,"","User's password"); +// ============================================================================= +using namespace std; +using namespace mysqlpp; +// ============================================================================= +namespace cdb_db{ +// ============================================================================= +CdbDb::CdbDb(){ + connection_ = new Connection(false); + profiles_ = NULL; +} +// ============================================================================= +CdbDb::~CdbDb(){ + connection_ = new Connection(false); + if (NULL != profiles_){ + for(CdbProfilesList::const_iterator current = profiles_->begin(); + current != profiles_->end();current++) { + delete *current; + } + delete profiles_; + } +} +// ============================================================================= +bool CdbDb::connect() +{ + VLOG(1)<<"Connect to database:" + << "db_name=" << FLAGS_db_name + << ", host=" << FLAGS_db_host + << ", user=" << FLAGS_db_user + << ", password=" << FLAGS_db_passwd + << ", port=" << FLAGS_db_port; + if (!connection_->connect(FLAGS_db_name.c_str(),FLAGS_db_host.c_str(), + FLAGS_db_user.c_str(), FLAGS_db_passwd.c_str(), FLAGS_db_port)){ + LOG(ERROR)<error(); + return false; + } + VLOG(1)<<"...connected"; + return true; +} +// ============================================================================= +CdbProfilesList* CdbDb::profiles() +{ + if (NULL == profiles_){ + profiles_ = new CdbProfilesList(); + Query query = connection_->query( + "select obj,mtime FROM objtimes ORDER BY mtime DESC" + ); + StoreQueryResult res = query.store(); + if (!res){ + LOG(FATAL) << "Failed to get profiles. " << query.error(); + } + for (size_t i = 0; i < res.num_rows(); ++i) { + DateTime datetime(res[i]["mtime"]); + profiles_->push_back( + new CdbProfileImpl(connection_, + string(res[i]["obj"]), + time_t(datetime))); + } + } + return profiles_; +} +// ============================================================================= +} +// ============================================================================= diff --git a/src/cdb-profile-impl.h b/src/cdb-profile-impl.h new file mode 100644 index 0000000..74cf0b8 --- /dev/null +++ b/src/cdb-profile-impl.h @@ -0,0 +1,38 @@ +/* + * cdb-profile-impl.h + * + * Created on: Oct 24, 2008 + * Author: Alexander MAZUROV (alexander.mazurov@gmail.com) + */ + +#ifndef CDB_DB_CDBPROFILEIMPL_H_ +#define CDB_DB_CDBPROFILEIMPL_H_ +// ============================================================================= +#include "cdb-db/cdb-profile.h" +// ============================================================================= +namespace mysqlpp +{ + class Connection; +} +// ============================================================================= +namespace cdb_db{ +// ============================================================================= +class CdbProfileImpl : public CdbProfile +{ +public: + CdbProfileImpl(mysqlpp::Connection* connection, + const std::string& obj, const time_t& mtime) + :CdbProfile(obj,mtime),connection_(connection){} +private: + bool property(const std::string& path, const std::string&value) + { return true;} + bool property(const std::string& path, const std::vector& values) + { return true;} + +private: + mysqlpp::Connection* connection_; +}; +// ============================================================================= +} + +#endif /* CDB_DB_CDBPROFILEIMPL_H_ */ diff --git a/src/cdb-profile.cc b/src/cdb-profile.cc new file mode 100644 index 0000000..fb98cb1 --- /dev/null +++ b/src/cdb-profile.cc @@ -0,0 +1,14 @@ +/* + * cdb-profile.cc + * + * Created on: Oct 24, 2008 + * Author: Alexander MAZUROV (alexander.mazurov@gmail.com) + */ +// ============================================================================= +#include "cdb-db/cdb-profile.h" +// ============================================================================= +namespace cdb_db{ +// ============================================================================= + +// ============================================================================= +} -- 2.11.4.GIT