summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/db.cc13
-rw-r--r--src/db.hh53
-rw-r--r--src/sqlite3_db.cc8
-rw-r--r--src/sqlite3_db.hh3
4 files changed, 72 insertions, 5 deletions
diff --git a/src/db.cc b/src/db.cc
index b1efc21..d1742ad 100644
--- a/src/db.cc
+++ b/src/db.cc
@@ -57,4 +57,17 @@ double DB::Value::d() const {
return data_.d;
}
+std::shared_ptr<DB::Snapshot> DB::select(
+ const std::string& table, const OrderBy& order_by) {
+ std::vector<OrderBy> order_by_vector(1, order_by);
+ return select(table, Condition(), order_by_vector);
+}
+
+std::shared_ptr<DB::Snapshot> DB::select(
+ const std::string& table, const Condition& condition,
+ const OrderBy& order_by) {
+ std::vector<OrderBy> order_by_vector(1, order_by);
+ return select(table, condition, order_by_vector);
+}
+
} // namespace stuff
diff --git a/src/db.hh b/src/db.hh
index a4f65e4..dee3d74 100644
--- a/src/db.hh
+++ b/src/db.hh
@@ -252,6 +252,10 @@ public:
: mode_(COMP_BINARY), c_(c), v_(v) {
op_.comp_binary = op;
}
+ Condition(const std::string& name, BinaryOperator op, const Value& v)
+ : mode_(COMP_BINARY), c_(Column(name)), v_(v) {
+ op_.comp_binary = op;
+ }
Condition(UnaryOperator op, const Column& c)
: mode_(COMP_UNARY), c_(c) {
op_.comp_unary = op;
@@ -340,6 +344,50 @@ public:
bool ascending_;
};
+ class Transaction {
+ public:
+ Transaction(std::shared_ptr<DB> db)
+ : db_(db), ptr_(db.get()) {
+ start();
+ }
+ Transaction(DB* db)
+ : ptr_(db) {
+ start();
+ }
+ ~Transaction() {
+ rollback();
+ }
+ bool rollback() {
+ if (ptr_ && good_) {
+ good_ = ptr_->rollback_transaction();
+ db_.reset();
+ ptr_ = nullptr;
+ }
+ return good_;
+ }
+ bool commit() {
+ if (ptr_ && good_) {
+ good_ = ptr_->commit_transaction();
+ if (!good_) {
+ ptr_->rollback_transaction();
+ }
+ db_.reset();
+ ptr_ = nullptr;
+ }
+ return good_;
+ }
+ private:
+ Transaction(const Transaction&) = delete;
+ Transaction& operator=(const Transaction&) = delete;
+ void start() {
+ good_ = ptr_ && ptr_->start_transaction();
+ }
+
+ std::shared_ptr<DB> db_;
+ DB* ptr_;
+ bool good_;
+ };
+
// Create a table with the given declarations.
// If a table with that name already exists nothing happens.
// Returns false in case of error
@@ -362,6 +410,11 @@ public:
virtual std::shared_ptr<Snapshot> select(
const std::string& table, const Condition& condition = Condition(),
const std::vector<OrderBy>& order_by = std::vector<OrderBy>()) = 0;
+ std::shared_ptr<Snapshot> select(
+ const std::string& table, const OrderBy& order_by);
+ std::shared_ptr<Snapshot> select(
+ const std::string& table, const Condition& condition,
+ const OrderBy& order_by);
// Remove all rows matching condition in table. Returns number of rows
// removed or -1 in case of error
virtual int64_t remove(const std::string& table,
diff --git a/src/sqlite3_db.cc b/src/sqlite3_db.cc
index 3415d15..d1f0e1b 100644
--- a/src/sqlite3_db.cc
+++ b/src/sqlite3_db.cc
@@ -712,10 +712,10 @@ public:
} // namespace
// static
-DB* SQLite3::open(const std::string& path) {
- std::unique_ptr<DBImpl> db(new DBImpl());
- db->open(path);
- return db.release();
+std::unique_ptr<DB> SQLite3::open(const std::string& path) {
+ std::unique_ptr<DB> db(new DBImpl());
+ static_cast<DBImpl*>(db.get())->open(path);
+ return db;
}
} // namespace stuff
diff --git a/src/sqlite3_db.hh b/src/sqlite3_db.hh
index 6e266e9..1a24df4 100644
--- a/src/sqlite3_db.hh
+++ b/src/sqlite3_db.hh
@@ -3,13 +3,14 @@
#include "db.hh"
+#include <memory>
#include <string>
namespace stuff {
class SQLite3 {
public:
- static DB* open(const std::string& path);
+ static std::unique_ptr<DB> open(const std::string& path);
};
} // namespace stuff