-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathconnection.hpp
90 lines (77 loc) · 2.74 KB
/
connection.hpp
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
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2011 Artem Pavlenko
* 2013 Oleksandr Novychenko
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
#ifndef MONGODB_CONNECTION_HPP
#define MONGODB_CONNECTION_HPP
// mongo
#include <mongo/client/dbclient.h>
#include <mongo/client/connpool.h>
#include <mongo/client/dbclientcursor.h>
// boost
#include <boost/shared_ptr.hpp>
#include <boost/scoped_ptr.hpp>
// std
#include <sstream>
#include <iostream>
class Connection {
boost::scoped_ptr<mongo::ScopedDbConnection> conn_;
std::string ns_;
bool closed_;
public:
Connection(const std::string &connection_str, const std::string ns)
: ns_(ns), closed_(false) {
try {
conn_.reset(mongo::ScopedDbConnection::getScopedDbConnection(connection_str));
} catch (mongo::DBException &de) {
std::string err_msg = "Mongodb Plugin: ";
err_msg += de.toString();
err_msg += "\n";
throw mapnik::datasource_exception(err_msg);
}
}
~Connection() {
close();
}
boost::shared_ptr<mongo::DBClientCursor> query(const std::string &json, int limit = 0, int skip = 0) {
try {
mongo::DBClientCursor *ptr = conn_->get()->query(ns_, mongo::Query(json), limit, skip).release();
if (!ptr)
throw conn_->get()->getLastError();
return boost::shared_ptr<mongo::DBClientCursor>(ptr);
} catch(mongo::DBException &de) {
std::string err_msg = "Mongodb Plugin: ";
err_msg += de.toString();
err_msg += "\n";
throw mapnik::datasource_exception(err_msg);
}
}
bool isOK() const {
return (!closed_) && (conn_->ok());
}
void close() {
if (!closed_) {
conn_->done();
closed_ = true;
}
}
};
#endif // MONGODB_CONNECTION_HPP