summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormethodus <methodus@web.de>2012-10-28 20:16:19 +0100
committermethodus <methodus@web.de>2012-10-28 20:16:19 +0100
commit6dd746005c921dc2c49bf19a3524ea3bfb5e2d30 (patch)
tree876a7075f299982ef4c0eb188731349c357ad683
parent735900e78b42329b6548ff67612c672f5a3fdceb (diff)
downloadvdr-plugin-upnp-6dd746005c921dc2c49bf19a3524ea3bfb5e2d30.tar.gz
vdr-plugin-upnp-6dd746005c921dc2c49bf19a3524ea3bfb5e2d30.tar.bz2
Added database wrapper replacement for tntdb. Added a test_programm to verify its functionality.
-rw-r--r--include/db/connection.h77
-rw-r--r--include/db/exception.h33
-rw-r--r--include/db/statement.h43
-rw-r--r--tests/Makefile8
-rw-r--r--tests/test_database.cpp25
5 files changed, 183 insertions, 3 deletions
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 <string>
+#include <boost/shared_ptr.hpp>
+#include <sqlite3.h>
+
+#include "exception.h"
+#include "statement.h"
+
+namespace db {
+
+class Connection {
+
+private:
+
+ boost::shared_ptr<sqlite3> m_connection;
+
+public:
+
+ Connection() {}
+
+ Connection(boost::shared_ptr<sqlite3>& 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<sqlite3_stmt> 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<sqlite3> 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 <exception>
+#include <string>
+
+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 <sqlite3.h>
+
+namespace db {
+
+class Statement {
+private:
+
+ boost::shared_ptr<sqlite3_stmt> m_statement;
+ boost::shared_ptr<sqlite3> m_connection;
+
+public:
+
+ Statement() {}
+
+ Statement(boost::shared_ptr<sqlite3>& conn, boost::shared_ptr<sqlite3_stmt>& 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 <iostream>
+#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;
+}