From 6dd746005c921dc2c49bf19a3524ea3bfb5e2d30 Mon Sep 17 00:00:00 2001 From: methodus Date: Sun, 28 Oct 2012 20:16:19 +0100 Subject: Added database wrapper replacement for tntdb. Added a test_programm to verify its functionality. --- include/db/connection.h | 77 +++++++++++++++++++++++++++++++++++++++++++++++++ include/db/exception.h | 33 +++++++++++++++++++++ include/db/statement.h | 43 +++++++++++++++++++++++++++ 3 files changed, 153 insertions(+) create mode 100644 include/db/connection.h create mode 100644 include/db/exception.h create mode 100644 include/db/statement.h (limited to 'include/db') diff --git a/include/db/connection.h b/include/db/connection.h new file mode 100644 index 0000000..b3143e4 --- /dev/null +++ b/include/db/connection.h @@ -0,0 +1,77 @@ +/* + * connection.h + * + * Created on: 28.10.2012 + * Author: savop + */ + +#ifndef CONNECTION_H_ +#define CONNECTION_H_ + +#include +#include +#include + +#include "exception.h" +#include "statement.h" + +namespace db { + +class Connection { + +private: + + boost::shared_ptr m_connection; + +public: + + Connection() {} + + Connection(boost::shared_ptr& conn) + : m_connection(conn) + {} + + virtual ~Connection(){} + + void Close(){ + m_connection.reset(); + } + + Statement Prepare(const std::string& stmt){ + sqlite3_stmt* _stmt; + if(sqlite3_prepare(m_connection.get(), stmt.c_str(), stmt.length(), &_stmt, NULL) != SQLITE_OK) + throw SQLiteException(sqlite3_errmsg(m_connection.get())); + + boost::shared_ptr statement(_stmt, std::ptr_fun(sqlite3_finalize)); + return Statement(m_connection, statement); + } + + void BeginTransaction(){ + Prepare("BEGIN TRANSACTION").execute(); + } + + void CommitTransaction(){ + Prepare("COMMIT TRANSACTION").execute(); + } + + void RollbackTransaction(){ + Prepare("ROLLBACK TRANSACTION").execute(); + } + +}; + +Connection Connect(const std::string url){ + sqlite3* _conn; + if(sqlite3_open_v2(url.c_str(), &_conn, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL) != SQLITE_OK){ + sqlite3_close(_conn); + throw SQLiteException(std::string("Failed to open ") + url); + } + boost::shared_ptr connection(_conn, std::ptr_fun(sqlite3_close)); + + return Connection(connection); +} + +} // namespace db + + +#endif /* CONNECTION_H_ */ diff --git a/include/db/exception.h b/include/db/exception.h new file mode 100644 index 0000000..47c99c8 --- /dev/null +++ b/include/db/exception.h @@ -0,0 +1,33 @@ +/* + * exception.h + * + * Created on: 28.10.2012 + * Author: savop + */ + +#ifndef EXCEPTION_H_ +#define EXCEPTION_H_ + +#include +#include + +namespace db { + +class SQLiteException : public std::exception { +public: + SQLiteException(std::string msg) throw() + : message(msg) + {} + virtual ~SQLiteException() throw() {} + virtual const char* what() const throw() + { + return message.c_str(); + } +private: + std::string message; +}; + +} // namespace db + + +#endif /* EXCEPTION_H_ */ diff --git a/include/db/statement.h b/include/db/statement.h new file mode 100644 index 0000000..8c2ade2 --- /dev/null +++ b/include/db/statement.h @@ -0,0 +1,43 @@ +/* + * statement.h + * + * Created on: 28.10.2012 + * Author: savop + */ + +#ifndef STATEMENT_H_ +#define STATEMENT_H_ + +#include + +namespace db { + +class Statement { +private: + + boost::shared_ptr m_statement; + boost::shared_ptr m_connection; + +public: + + Statement() {} + + Statement(boost::shared_ptr& conn, boost::shared_ptr& stmt) + : m_connection(conn) + , m_statement(stmt) + {} + + virtual ~Statement(){} + + Statement& execute(){ + if(sqlite3_step(m_statement.get()) != SQLITE_OK) + throw SQLiteException(sqlite3_errmsg(m_connection.get())); + + return *this; + } + +}; + +} // namespace db + +#endif /* STATEMENT_H_ */ -- cgit v1.2.3