Repository navigation
Expand file tree
/
Copy pathDbConnection.cpp
More file actions
189 lines (162 loc) · 4.51 KB
/
Copy pathDbConnection.cpp
File metadata and controls
189 lines (162 loc) · 4.51 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include "pch.h"
#include "DbConnection.h"
DbConnection::DbConnection(
const std::string& path,
const bool allow_ext,
const int flags,
const std::string& vfs,
bool with_alt_types,
bool check_utf8
)
: pConn_(NULL),
pDeferred_(NULL),
n_streams_(0),
n_cleared_results_(0),
with_alt_types_(with_alt_types),
check_utf8_(check_utf8),
busy_callback_(NULL) {
// Get the underlying database connection
// Always ensure URI filenames are enabled so that parameters like vfs= and
// immutable= work.
int rc = sqlite3_open_v2(
path.c_str(),
&pConn_,
flags | SQLITE_OPEN_URI,
vfs.empty() ? NULL : vfs.c_str()
);
if (rc != SQLITE_OK) {
// SQLite usually allocates a handle even when opening fails,
// and the destructor does not run when the constructor throws,
// so close the handle here.
// The message comes from the handle and must be read first.
std::string msg = getException();
sqlite3_close_v2(pConn_);
pConn_ = NULL;
cpp11::stop("Could not connect to database:\n%s", msg.c_str());
}
if (allow_ext) {
sqlite3_enable_load_extension(pConn_, 1);
}
}
DbConnection::~DbConnection() {
if (is_valid()) {
disconnect();
}
// in case this is still lingering for an invalid connection
release_callback_data();
}
sqlite3* DbConnection::conn() const {
if (!is_valid()) {
cpp11::stop("disconnected");
}
return pConn_;
}
bool DbConnection::is_valid() const {
return (pConn_ != NULL);
}
void DbConnection::set_current_result(const DbResult*) const {}
void DbConnection::reset_current_result(const DbResult*) const {}
bool DbConnection::is_current_result(const DbResult*) const {
return true;
}
void DbConnection::check_connection() const {
if (!is_valid()) {
cpp11::stop("Invalid or closed connection");
}
}
std::string DbConnection::getException() const {
if (is_valid()) {
return std::string(sqlite3_errmsg(pConn_));
} else {
return std::string();
}
}
void DbConnection::copy_to(const DbConnectionPtr& pDest) {
sqlite3_backup* backup =
sqlite3_backup_init(pDest->conn(), "main", pConn_, "main");
int rc = sqlite3_backup_step(backup, -1);
if (rc != SQLITE_DONE) {
cpp11::stop("Failed to copy all data:\n%s", getException().c_str());
}
rc = sqlite3_backup_finish(backup);
if (rc != SQLITE_OK) {
cpp11::stop("Could not finish copy:\n%s", getException().c_str());
}
}
void DbConnection::disconnect() {
if (n_streams_ > 0) {
// The busy handler stays until the handle is closed
pDeferred_ = pConn_;
} else {
close_handle(pConn_);
}
pConn_ = NULL;
}
void DbConnection::close_handle(sqlite3* conn) {
// The statements of open results can still step the handle,
// which stays a zombie until they are finalized:
// they get SQLITE_BUSY at once rather than call the released R handler.
// A busy timeout calls no R code and stays.
if (busy_callback_ && !Rf_isInteger(busy_callback_)) {
sqlite3_busy_handler(conn, NULL, NULL);
}
release_callback_data();
sqlite3_close_v2(conn);
}
bool DbConnection::is_closed() const {
return pConn_ == NULL && pDeferred_ == NULL;
}
void DbConnection::add_stream() {
++n_streams_;
}
void DbConnection::remove_stream() {
if (--n_streams_ == 0 && pDeferred_ != NULL) {
close_handle(pDeferred_);
pDeferred_ = NULL;
}
}
void DbConnection::add_cleared_result(const int n) {
n_cleared_results_ += n;
}
long DbConnection::n_cleared_results() const {
return n_cleared_results_;
}
bool DbConnection::with_alt_types() const {
return with_alt_types_;
}
bool DbConnection::check_utf8() const {
return check_utf8_;
}
void DbConnection::set_busy_handler(SEXP r_callback) {
check_connection();
release_callback_data();
if (!Rf_isNull(r_callback)) {
R_PreserveObject(r_callback);
busy_callback_ = r_callback;
}
if (busy_callback_ && Rf_isInteger(busy_callback_)) {
sqlite3_busy_timeout(pConn_, INTEGER(busy_callback_)[0]);
} else {
sqlite3_busy_handler(pConn_, busy_callback_helper, busy_callback_);
}
}
void DbConnection::release_callback_data() {
if (busy_callback_) {
R_ReleaseObject(busy_callback_);
busy_callback_ = NULL;
}
}
int DbConnection::busy_callback_helper(void* data, int num) {
SEXP r_callback = reinterpret_cast<SEXP>(data);
cpp11::function rfun = r_callback;
int ret = cpp11::as_integers(rfun(num))[0];
return ret;
}
bool DbConnection::in_transaction() const {
int status = sqlite3_get_autocommit(pConn_);
if (status == 0) {
return true;
} else {
return false;
}
}