-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathsqlite.cpp
More file actions
64 lines (52 loc) · 1.87 KB
/
sqlite.cpp
File metadata and controls
64 lines (52 loc) · 1.87 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
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
#include <libp2p/storage/sqlite.hpp>
namespace libp2p::storage {
SQLite::SQLite(const std::string &db_file)
: db_(db_file), db_file_(db_file), log_(log::createLogger(kLoggerTag)) {}
SQLite::SQLite(const std::string &db_file, const std::string &logger_tag)
: db_(db_file), db_file_(db_file), log_(log::createLogger(logger_tag)) {}
SQLite::~SQLite() {
// without the following, all the prepared statements
// might be executed when db_'s destructor is called
for (auto &st : statements_) {
st.used(true);
}
}
int SQLite::getErrorCode() const {
return sqlite3_extended_errcode(db_.connection().get());
}
std::string SQLite::getErrorMessage() const {
const int ec{getErrorCode()};
return (0 == ec) ? std::string()
: std::string(sqlite3_errstr(ec)) + ": "
+ sqlite3_errmsg(db_.connection().get());
}
SQLite::StatementHandle SQLite::createStatement(const std::string &sql) {
auto handle{statements_.size()};
statements_.emplace_back(db_ << sql);
return handle;
}
SQLite::database_binder &SQLite::getStatement(
SQLite::StatementHandle handle) {
if (handle >= statements_.size()) {
throw std::invalid_argument("SQLite: statement handle " +
std::to_string(handle) +
" does not exist (max: " +
std::to_string(statements_.size() - 1) + ")");
}
return statements_[handle];
}
int SQLite::countChanges() const {
return sqlite3_changes(db_.connection().get());
}
const std::string &SQLite::getDatabaseFile() const {
return db_file_;
}
size_t SQLite::getStatementCount() const {
return statements_.size();
}
} // namespace libp2p::storage