diff options
author | methodus <methodus@web.de> | 2012-10-28 22:11:36 +0100 |
---|---|---|
committer | methodus <methodus@web.de> | 2012-10-28 22:11:36 +0100 |
commit | 9478cdc825981329f3f864f14d61f51ae1ac0ace (patch) | |
tree | 57ae794c41fe6ce964a54be295b98b48baf28c04 | |
parent | 6dd746005c921dc2c49bf19a3524ea3bfb5e2d30 (diff) | |
download | vdr-plugin-upnp-9478cdc825981329f3f864f14d61f51ae1ac0ace.tar.gz vdr-plugin-upnp-9478cdc825981329f3f864f14d61f51ae1ac0ace.tar.bz2 |
Continued database wrapper
-rw-r--r-- | include/db/connection.h | 10 | ||||
-rw-r--r-- | include/db/statement.h | 51 | ||||
-rw-r--r-- | tests/test_database.cpp | 18 |
3 files changed, 73 insertions, 6 deletions
diff --git a/include/db/connection.h b/include/db/connection.h index b3143e4..b46c133 100644 --- a/include/db/connection.h +++ b/include/db/connection.h @@ -46,16 +46,20 @@ public: return Statement(m_connection, statement); } + int Execute(const std::string& stmt){ + return Prepare(stmt).Execute(); + } + void BeginTransaction(){ - Prepare("BEGIN TRANSACTION").execute(); + Execute("BEGIN TRANSACTION"); } void CommitTransaction(){ - Prepare("COMMIT TRANSACTION").execute(); + Execute("COMMIT TRANSACTION"); } void RollbackTransaction(){ - Prepare("ROLLBACK TRANSACTION").execute(); + Execute("ROLLBACK TRANSACTION"); } }; diff --git a/include/db/statement.h b/include/db/statement.h index 8c2ade2..22da47c 100644 --- a/include/db/statement.h +++ b/include/db/statement.h @@ -29,9 +29,56 @@ public: virtual ~Statement(){} - Statement& execute(){ - if(sqlite3_step(m_statement.get()) != SQLITE_OK) + void Reset(){ + if(sqlite3_reset(m_statement.get()) != SQLITE_OK) throw SQLiteException(sqlite3_errmsg(m_connection.get())); + } + + int Execute(){ + int ret = sqlite3_step(m_statement.get()); + + if(ret != SQLITE_OK && ret != SQLITE_DONE ) + throw SQLiteException(sqlite3_errmsg(m_connection.get())); + + int numChanges = sqlite3_changes(m_connection.get()); + + Reset(); + + return numChanges; + } + + int GetBindIndex(const std::string& col){ + int index = 0; + + return index; + } + + Statement& SetNull(const std::string& c){ + int col = GetBindIndex(c); + + return *this; + } + + Statement& SetString(const std::string& c, const std::string& value){ + int col = GetBindIndex(c); + + return *this; + } + + Statement& SetLong(const std::string& c, long value){ + int col = GetBindIndex(c); + + return *this; + } + + Statement& SetDouble(const std::string& c, double value){ + int col = GetBindIndex(c); + + return *this; + } + + Statement& SetBlob(const std::string& c, const char* buf, size_t bufLen){ + int col = GetBindIndex(c); return *this; } diff --git a/tests/test_database.cpp b/tests/test_database.cpp index da11cb9..2aa12b3 100644 --- a/tests/test_database.cpp +++ b/tests/test_database.cpp @@ -6,6 +6,7 @@ */ #include <iostream> +#include <sstream> #include "../include/db/connection.h" int main(){ @@ -15,7 +16,22 @@ int main(){ connection.BeginTransaction(); - connection.RollbackTransaction(); + std::stringstream ss; + + ss << "CREATE TABLE details" + << "(" + << " `propertyID` INTEGER PRIMARY KEY," + << " `@id` TEXT " + << " REFERENCES metadata (`@id`) ON DELETE CASCADE ON UPDATE CASCADE," + << " `property` TEXT," + << " `value` TEXT" + << ")"; + + db::Statement statement = connection.Prepare(ss.str()); + + statement.Execute(); + + connection.CommitTransaction(); } catch (const db::SQLiteException& e){ std::cerr << "Exception: "<< e.what() << std::endl; |