-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.h
97 lines (83 loc) · 2.83 KB
/
client.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* =============================================================================
* Filename: client.h
* Description:
* Version: 1.0
* Created: 2013-07-05 20:16:55
* Author: newk (newk), [email protected]
* Company:
* Copyright (c) 2013, newk
* =============================================================================
*/
#ifndef CLIENT_H
#define CLIENT_H
#include "c_driver/src/mongo.h"
#include "bsonobj.h"
#include <string>
class CMongoClient;
class CMongoCursor
{
public:
CMongoCursor(CMongoClient& client, const std::string& ns);
CMongoCursor(mongo_cursor* cursor);
CMongoCursor(CMongoCursor&& other);
~CMongoCursor();
CBsonObj get_object() const;
bool next();
void set_query(const CBsonObj& query);
void set_fields(const CBsonObj& fields);
void set_limit(int limit);
void set_skip(int skip);
void set_options(int options);
mongo_cursor* raw_data() const
{
return m_cursor;
}
private:
mongo_cursor* m_cursor;
std::unique_ptr<CBsonObj> m_query;
std::unique_ptr<CBsonObj> m_fields;
CMongoCursor(const CMongoCursor& other) = delete;
CMongoCursor(CMongoCursor& other) = delete;
};
class CMongoClient
{
friend class CMongoCursor;
public:
CMongoClient();
~CMongoClient();
mongo_error_t last_error() const;
int connect(const std::string& host, int port);
bool has_namespace(const std::string& ns) const;
int set_op_timeout(int milliseconds);
int check_connection();
int reconnect();
void disconnect();
// CRUD
int insert(const std::string& ns, const CBsonObj& data);
int insert(const std::string& ns, std::vector<const CBsonObj*> datas,
int flags = MONGO_CONTINUE_ON_ERROR);
int update(const std::string& ns, const CBsonObj& query,
const CBsonObj& op, int flags);
int update_all(const std::string& ns, const CBsonObj& query,
const CBsonObj& op)
{ return update(ns, query, op, MONGO_UPDATE_MULTI); }
int upsert(const std::string& ns, const CBsonObj& query,
const CBsonObj& op);
int remove(const std::string& ns, const CBsonObj& query);
// Cursor
static std::vector<std::string> AllFields()
{ return std::vector<std::string>(); }
CMongoCursor find(const std::string& ns, const CBsonObj& query,
const std::vector<std::string> fields = AllFields(),
int limit = 0, int skip = 0, int options = 0);
CMongoCursor find(const std::string& ns, const CBsonObj& query,
const CBsonObj& orderby,
const std::vector<std::string> fields = AllFields(),
int limit = 0, int skip = 0, int options = 0);
CBsonObj find_one(const std::string& ns, const CBsonObj& query,
const std::vector<std::string> fields = AllFields());
private:
mongo* m_mongo;
};
#endif // CLIENT_H