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 +++++++++++++++++++++++++++ tests/Makefile | 8 +++-- tests/test_database.cpp | 25 ++++++++++++++++ 5 files changed, 183 insertions(+), 3 deletions(-) create mode 100644 include/db/connection.h create mode 100644 include/db/exception.h create mode 100644 include/db/statement.h create mode 100644 tests/test_database.cpp 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_ */ diff --git a/tests/Makefile b/tests/Makefile index c8e6192..35914be 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,6 +1,8 @@ - parser: test_parser.cpp ../common/parser.cpp g++ -o test_parser test_parser.cpp ../common/parser.cpp -g -I../include -I/usr/include -I/usr/local/include - + dllloader: dll_loader.cpp ../media/pluginManager.cpp ../common/tools.cpp - g++ -o test_dllloader dll_loader.cpp ../media/pluginManager.cpp ../common/tools.cpp -g -ldl -Wl,--export-dynamic -I../include -I/usr/include -I/usr/local/include \ No newline at end of file + g++ -o test_dllloader dll_loader.cpp ../media/pluginManager.cpp ../common/tools.cpp -g -ldl -Wl,--export-dynamic -I../include -I/usr/include -I/usr/local/include + +database: test_database.cpp + g++ -o test_database test_database.cpp -g -lsqlite3 -I../include -I/usr/include -I/usr/local/include \ No newline at end of file diff --git a/tests/test_database.cpp b/tests/test_database.cpp new file mode 100644 index 0000000..da11cb9 --- /dev/null +++ b/tests/test_database.cpp @@ -0,0 +1,25 @@ +/* + * test_database.cpp + * + * Created on: 28.10.2012 + * Author: savop + */ + +#include +#include "../include/db/connection.h" + +int main(){ + + try { + db::Connection connection = db::Connect("/tmp/test_db.sqlite"); + + connection.BeginTransaction(); + + connection.RollbackTransaction(); + + } catch (const db::SQLiteException& e){ + std::cerr << "Exception: "<< e.what() << std::endl; + } + + return 0; +} -- cgit v1.2.3