summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rw-r--r--include/db/connection.h77
-rw-r--r--include/db/exception.h33
-rw-r--r--include/db/statement.h43
3 files changed, 153 insertions, 0 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_ */