15#ifndef CCGL_DB_MONGOC_H
16#define CCGL_DB_MONGOC_H
64 mongoc_client_t*
GetConn() {
return conn_; }
70 mongoc_collection_t*
GetCollection(
string const& dbname,
string const& collectionname);
73 mongoc_gridfs_t*
GetGridFs(
string const& dbname,
string const& gfsname);
85 void GetGridFsFileNames(
string const& dbname,
string const& gfsname, vector<string>& gfs_exists);
90 mongoc_client_t* conn_;
112 mongoc_database_t* db_;
129 mongoc_cursor_t*
ExecuteQuery(
const bson_t* b,
const bson_t* opts =
nullptr);
134 mongoc_collection_t* collection_;
153 mongoc_gridfs_file_t*
GetFile(
string const& gfilename, mongoc_gridfs_t* gfs = NULL,
157 bool RemoveFile(
string const& gfilename, mongoc_gridfs_t* gfs = NULL,
161 void GetFileNames(vector<string>& files_existed, mongoc_gridfs_t* gfs = NULL);
168 bool GetStreamData(
string const& gfilename,
char*& databuf, vint& datalength,
169 mongoc_gridfs_t* gfs = NULL,
174 const bson_t* p, mongoc_gridfs_t* gfs = NULL);
177 mongoc_gridfs_t* gfs_;
182 const string& prefix =
string());
192 const bson_value_t* vv = bson_iter_value(iter);
193 if (vv->value_type == BSON_TYPE_INT32) {
194 numericvalue =
static_cast<T
>(vv->value.v_int32);
195 }
else if (vv->value_type == BSON_TYPE_INT64) {
196 numericvalue =
static_cast<T
>(vv->value.v_int64);
197 }
else if (vv->value_type == BSON_TYPE_DOUBLE) {
198 numericvalue =
static_cast<T
>(vv->value.v_double);
199 }
else if (vv->value_type == BSON_TYPE_UTF8) {
200 string tmp = vv->value.v_utf8.str;
201 if (tmp.find_first_of(
"0123456789") == string::npos) {
202 cout <<
"bson iterator isn't or not contains a numeric value." << endl;
206 numericvalue =
static_cast<T
>(strtod(tmp.c_str(), &end));
208 cout <<
"bson iterator isn't or not contains a numeric value." << endl;
225 if (bson_iter_init(&iter, bmeta) && bson_iter_find(&iter, key)) {
228 StatusMessage((
"WARNING: GetNumericFromBson, Failed in get value of: " +
string(key) +
"\n").c_str());
Base class for classes that cannot be copied.
Definition: basic.h:385
A simple wrapper of the class of MongoDB Client mongoc_client_t.
Definition: db_mongoc.h:46
MongoClient(mongoc_client_t *conn)
Constructor using mongoc_client_t*.
void GetDatabaseNames(vector< string > &dbnames)
Get existing database names.
mongoc_database_t * GetDatabase(string const &dbname)
Get existing or newly created mongoc_database_t instance.
mongoc_collection_t * GetCollection(string const &dbname, string const &collectionname)
Get mongoc_collection_t instance.
mongoc_client_t * GetConn()
Get mongoc_client_t instance.
Definition: db_mongoc.h:64
~MongoClient()
Destructor.
MongoClient(const char *host, vuint16_t port)
Constructor using IP address and port number.
void Destroy()
Destroy explicitly.
mongoc_gridfs_t * GetGridFs(string const &dbname, string const &gfsname)
Get mongoc_gridfs_t instance.
static MongoClient * Init(const char *host, vuint16_t port)
Initialization of MongoClient with the validation check of database.
void GetGridFsFileNames(string const &dbname, string const &gfsname, vector< string > &gfs_exists)
Get GridFs file names in MongoDB database.
MongoGridFs * GridFs(string const &dbname, string const &gfsname)
Get MongoGridFs instance.
void GetCollectionNames(string const &dbname, vector< string > &collnames)
Get collection names in MongoDB database.
A simple wrapper of the class of MongoDB Collection mongoc_collection_t.
Definition: db_mongoc.h:120
mongoc_cursor_t * ExecuteQuery(const bson_t *b, const bson_t *opts=nullptr)
Execute query.
MongoCollection(mongoc_collection_t *coll)
Constructor by a mongoc_collection_t pointer.
~MongoCollection()
Destructor.
vint QueryRecordsCount()
Query the records number.
A simple wrapper of the class of MongoDB database mongoc_database_t.
Definition: db_mongoc.h:97
MongoDatabase(mongoc_database_t *db)
Constructor by a mongoc_database_t pointer.
void GetCollectionNames(vector< string > &collnames)
Get collection names in current database.
MongoDatabase(mongoc_client_t *conn, string &dbname)
Constructor by mongodb client (mongoc_client_t pointer) and database name.
~MongoDatabase()
Destructor.
A simple wrapper of the class of MongoDB database mongoc_gridfs_t.
Definition: db_mongoc.h:141
void GetFileNames(vector< string > &files_existed, mongoc_gridfs_t *gfs=NULL)
Get GridFS file names.
bool RemoveFile(string const &gfilename, mongoc_gridfs_t *gfs=NULL, STRING_MAP opts=STRING_MAP())
Remove GridFS all matching files and their data chunks.
~MongoGridFs()
Destructor.
mongoc_gridfs_file_t * GetFile(string const &gfilename, mongoc_gridfs_t *gfs=NULL, const STRING_MAP &opts=STRING_MAP())
Get GridFS file by name.
MongoGridFs(mongoc_gridfs_t *gfs=NULL)
Constructor by a mongoc_gridfs_t pointer or NULL.
bool GetStreamData(string const &gfilename, char *&databuf, vint &datalength, mongoc_gridfs_t *gfs=NULL, const STRING_MAP *opts=nullptr)
Get stream data of a given GridFS file name.
bool WriteStreamData(const string &gfilename, char *&buf, vint length, const bson_t *p, mongoc_gridfs_t *gfs=NULL)
Write stream data to a GridFS file.
bson_t * GetFileMetadata(string const &gfilename, mongoc_gridfs_t *gfs=NULL, STRING_MAP opts=STRING_MAP())
Get metadata of a given GridFS file name, remember to destory bson_t after use.
mongoc_gridfs_t * GetGridFs()
Get the current instance of mongoc_gridfs_t
Definition: db_mongoc.h:150
bool GetNumericFromBson(bson_t *bmeta, const char *key, T &numericvalue)
Get numeric value from bson_t according to a given key.
Definition: db_mongoc.h:223
void AppendStringOptionsToBson(bson_t *bson_opts, const STRING_MAP &opts, const string &prefix=string())
Append options to bson_t
string GetStringFromBsonIterator(bson_iter_t *iter)
Get String from the iterator (bson_iter_t) of bson_t
bool GetBoolFromBsonIterator(bson_iter_t *iter)
Get Bool from the iterator (bson_iter_t) of bson_t
time_t GetDatetimeFromBsonIterator(bson_iter_t *iter)
Get Datetime from the iterator (bson_iter_t) of bson_t
string GetStringFromBson(bson_t *bmeta, const char *key)
Get String from bson_t
bool GetNumericFromBsonIterator(bson_iter_t *iter, T &numericvalue)
Get numeric value from the iterator (bson_iter_t) of bson_taccording to a given key.
Definition: db_mongoc.h:191
time_t GetDatetimeFromBson(bson_t *bmeta, const char *key)
Get Datetime from bson_t
bool GetBoolFromBson(bson_t *bmeta, const char *key)
Get String from bson_t
Common Cross-platform Geographic Library (CCGL)
std::map< string, string > STRING_MAP
Map of string key and string value.
Definition: basic.h:349
void StatusMessage(const char *msg)
Print status messages for Debug.