diff options
Diffstat (limited to 'libsqlitepp/sqlite-connection.cpp')
-rw-r--r-- | libsqlitepp/sqlite-connection.cpp | 134 |
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); +} + |