summaryrefslogtreecommitdiff
path: root/libsqlitepp/sqlite-connection.cpp
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2015-12-24 04:13:24 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2015-12-24 04:13:24 +0000
commit5dd91c7b80443c1f0e747088159c4d8b78d1856d (patch)
tree226331b6026e722f6a69765963ff19a9436155c1 /libsqlitepp/sqlite-connection.cpp
parentUse parent glibmm (diff)
downloadlibdbpp-sqlite-5dd91c7b80443c1f0e747088159c4d8b78d1856d.tar.bz2
libdbpp-sqlite-5dd91c7b80443c1f0e747088159c4d8b78d1856d.tar.xz
libdbpp-sqlite-5dd91c7b80443c1f0e747088159c4d8b78d1856d.zip
SQLite files prefixed with sqlite-
Diffstat (limited to 'libsqlitepp/sqlite-connection.cpp')
-rw-r--r--libsqlitepp/sqlite-connection.cpp134
1 files changed, 134 insertions, 0 deletions
diff --git a/libsqlitepp/sqlite-connection.cpp b/libsqlitepp/sqlite-connection.cpp
new file mode 100644
index 0000000..5eead63
--- /dev/null
+++ b/libsqlitepp/sqlite-connection.cpp
@@ -0,0 +1,134 @@
+#include "sqlite-connection.h"
+#include "sqlite-error.h"
+#include "sqlite-selectcommand.h"
+#include "sqlite-modifycommand.h"
+
+SQLite::Connection::Connection(const std::string & str) :
+ txDepth(0),
+ rolledback(false)
+{
+ if (sqlite3_open(str.c_str(), &db) != SQLITE_OK) {
+ if (db) {
+ std::string err(sqlite3_errmsg(db));
+ sqlite3_close(db);
+ throw Error(err.c_str());
+ }
+ throw Error("Unknown error opening database");
+ }
+}
+
+SQLite::Connection::~Connection()
+{
+ sqlite3_close(db);
+}
+
+void
+SQLite::Connection::finish() const
+{
+ if (txDepth != 0) {
+ rollbackTx();
+ throw Error("Transaction still open");
+ }
+}
+
+int
+SQLite::Connection::beginTx() const
+{
+ if (txDepth == 0) {
+ if (sqlite3_exec(db, "BEGIN TRANSACTION", NULL, NULL, NULL) != SQLITE_OK) {
+ throw Error(sqlite3_errmsg(db));
+ }
+ rolledback = false;
+ }
+ return ++txDepth;
+}
+
+int
+SQLite::Connection::commitTx() const
+{
+ if (rolledback) {
+ return rollbackTx();
+ }
+ if (--txDepth == 0) {
+ if (sqlite3_exec(db, "COMMIT TRANSACTION", NULL, NULL, NULL) != SQLITE_OK) {
+ throw Error(sqlite3_errmsg(db));
+ }
+ }
+ return txDepth;
+}
+
+int
+SQLite::Connection::rollbackTx() const
+{
+ if (--txDepth == 0) {
+ if (sqlite3_exec(db, "ROLLBACK TRANSACTION", NULL, NULL, NULL) != SQLITE_OK) {
+ throw Error(sqlite3_errmsg(db));
+ }
+ }
+ else {
+ rolledback = true;
+ }
+ return txDepth;
+}
+
+bool
+SQLite::Connection::inTx() const
+{
+ return txDepth;
+}
+
+DB::BulkDeleteStyle
+SQLite::Connection::bulkDeleteStyle() const
+{
+ return DB::BulkDeleteUsingUsingAlias;
+}
+
+DB::BulkUpdateStyle
+SQLite::Connection::bulkUpdateStyle() const
+{
+ return DB::BulkUpdateUsingJoin;
+}
+
+void
+SQLite::Connection::ping() const
+{
+ // Can this fail?
+}
+
+
+DB::SelectCommand *
+SQLite::Connection::newSelectCommand(const std::string & sql) const
+{
+ return new SelectCommand(this, sql);
+}
+
+DB::ModifyCommand *
+SQLite::Connection::newModifyCommand(const std::string & sql) const
+{
+ return new ModifyCommand(this, sql);
+}
+
+void
+SQLite::Connection::beginBulkUpload(const char *, const char *) const
+{
+ throw Error("Not implemented");
+}
+
+void
+SQLite::Connection::endBulkUpload(const char *) const
+{
+ throw Error("Not implemented");
+}
+
+size_t
+SQLite::Connection::bulkUploadData(const char *, size_t) const
+{
+ throw Error("Not implemented");
+}
+
+int64_t
+SQLite::Connection::insertId() const
+{
+ return sqlite3_last_insert_rowid(db);
+}
+