-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongolistmodel.cpp
148 lines (126 loc) · 3.77 KB
/
mongolistmodel.cpp
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "mongolistmodel.h"
#include <stdlib.h>
static const int bufferSize(200);
MongoListModel::MongoListModel(QObject *parent)
: QAbstractListModel(parent)
{
host = "ds049848.mongolab.com";
port = 49848;
dbName = "mongolab_database";
user = "mongo";
passwd = "mongolab";
connect();
mongoHeader();
mongoQueryDown(0);
mongoRowCount = bufferSize;
}
MongoListModel::~MongoListModel()
{
}
int MongoListModel::rowCount(const QModelIndex &) const
{
return mongoRowCount;
}
int MongoListModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return mongoHeaderDataList.size();
}
QVariant MongoListModel::data(const QModelIndex &index, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
int row = index.row();
if (row >= mongoDataList.size() || row < 0)
return QVariant();
if (role == Qt::DisplayRole){
return mongoDataList.at(row)->getItem(mongoHeaderDataList.at(index.column()));
}
return QVariant();
}
QVariant MongoListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if(role != Qt::DisplayRole)
return QVariant();
if(orientation == Qt::Horizontal && role == Qt::DisplayRole){
return mongoHeaderDataList.at(section);
}else{
return QString("%1").arg( section + 1 );
}
}
bool MongoListModel::canFetchMore(const QModelIndex &parent) const
{
Q_UNUSED(parent);
if (mongoRowCount-1 < mongoDataList.size()){
return true;
}
else {
return false;
}
}
void MongoListModel::fetchMore(const QModelIndex &parent)
{
Q_UNUSED(parent);
int remainder = mongoDataList.size() - mongoRowCount;
int itemsToFetch = qMin(bufferSize, remainder);
mongoQueryDown(mongoRowCount);
beginInsertRows(QModelIndex(), mongoRowCount, mongoRowCount+itemsToFetch-1);
mongoRowCount += itemsToFetch;
endInsertRows();
}
void MongoListModel::connect()
{
try {
mongoDatabase = new DBClientConnection();
mongo::client::initialize();
this->mongoDatabase->connect(host.toStdString()+":"+QString::number(port).toStdString(),errmsg);
errors = QString::fromStdString(errmsg);
if (errors == "") {
try
{
this->mongoDatabase->auth(dbName.toStdString(),user.toStdString(),passwd.toStdString(), errmsg);
errors = QString::fromStdString(errmsg);
}
catch ( const DBException &e )
{
errors = e.what();
}
}
}
catch (const DBException &e )
{
errors = e.what();
}
}
void MongoListModel::mongoQueryDown(int position, QString value) const
{
BSONObj query;
if (value != 0)
query = BSONObjBuilder().appendRegex("data", value.toStdString()).obj();
auto_ptr<DBClientCursor> cursor = mongoDatabase->query("mongolab_database.random_data",query, bufferSize, position);
while (cursor->more()) {
BSONObj object = cursor->next();
MongoItems * it = new MongoItems(object);
mongoDataList.append(it);
}
}
void MongoListModel::mongoHeader() const
{
BSONObj objOne = mongoDatabase->findOne("mongolab_database.random_data",Query());
mongoHeaderDataList.clear();
for ( BSONObj::iterator iter = objOne.begin(); iter.more(); )
{
BSONElement element = iter.next();
QString key = QString::fromStdString( element.fieldName() );
if ( key == "_id" ) continue;
mongoHeaderDataList.append(key);
}
}
void MongoListModel::searchBy(const QString &value)
{
beginResetModel();
mongoDataList.clear();
mongoQueryDown(0, value);
mongoRowCount = bufferSize;
endResetModel();
}