-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongodriverutils.h
102 lines (89 loc) · 3.51 KB
/
mongodriverutils.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
97
98
99
100
101
102
#ifndef MONGODRIVERUTILS_H
#define MONGODRIVERUTILS_H
#include<QString>
#include<QtDebug>
class CollectionDocument{
public:
CollectionDocument(){}
CollectionDocument(QString id){this->id = id;}
QString getId(){return this->id;}
void addValue(QString key,QVariant value){this->data.insert(key,value);}
void setJsonFormat(QString json){this->jsonFormat = json;}
QString getJsonFormat(){return jsonFormat;}
private:
QString id;
QMap<QString,QVariant> data;
QString jsonFormat ;
};
class DataBaseCollection{
public:
void setName(QString name){this->name = name;}
QString getName() {return this->name;}
void setType(QString type){this->type = type;}
QString getType(){return this->type;}
void setJsonFormat(QString json){this->jsonFormat = json;}
QString getJsonFormat(){return this->jsonFormat;}
void addDocument(CollectionDocument document){this->documents.insert(document.getId(),document);}
QList<CollectionDocument> getDocuments(){return this->documents.values();}
private:
QString name;
QString type;
QString jsonFormat;
QMap<QString,CollectionDocument> documents;
};
class DataBase{
public:
void setName(QString name){this->name = name;}
void setSizeOnDisk(double sizeOnDisk){this->sizeOnDisk = sizeOnDisk;}
void setIsEmty(bool isEmpty){this->_isEmpty = isEmpty;}
void setJsonFormat(QString jsonData){this->jsonData = jsonData;}
QString getName(){return this->name;}
bool isEmpty(){return this->_isEmpty;}
double getSizeOnDisk(){return this->sizeOnDisk;}
QString toJson(){return this->jsonData;}
void addCollection(DataBaseCollection collection){this->collections.insert(collection.getName(),collection);}
DataBaseCollection getCollection(QString collectionName){return this->collections.value(collectionName);}
QList<QString> getCollectionsNames(){return this->collections.keys();}
QList<DataBaseCollection> getCollections (){return this->collections.values();}
QString toString(){
QString string_collections = "";
for(DataBaseCollection c : getCollections()){
string_collections += c.getJsonFormat() + ",";
}
string_collections.remove(string_collections.length() - 1,1);
return "{\"databaseinfo\":" + jsonData + ",\"collections\":[" + string_collections + "]},";
}
private:
QString name;
bool _isEmpty;
double sizeOnDisk;
QString jsonData;
QMap<QString,DataBaseCollection> collections;
};
class ConnectionUri{
public:
ConnectionUri(){}
ConnectionUri(QString protocol,QString host,unsigned int port) {
setHost(host);
setPort(port);
setProtocol(protocol);
}
inline int getPort()const{return this->port;}
inline QString getHost()const{return this->host;}
inline QString getProtocol()const {return this->protocol;}
inline void setPort(unsigned int &port){this->port = port?port:27072;}
inline void setHost(QString &host){ this->host = host != NULL?host:"localhost";}
inline void setProtocol(QString &protocol){this->protocol = protocol != NULL? protocol:QString("mongo");}
virtual QString toString(){return this->protocol + "://" + this->host + ":" + QString::number(this->port);}
virtual std::string toStdString(){return this->toString().toStdString();}
private:
int port;
QString host;
QString protocol;
};
enum ConnectionExceptionReason {DOWNSERVER,PRESET};
struct ConnectionException{
ConnectionExceptionReason reason;
ConnectionUri uri;
};
#endif // MONGODRIVERUTILS_H