summaryrefslogtreecommitdiff
path: root/libsqlitepp/selectcommand.cpp
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2015-04-30 00:27:27 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2015-04-30 00:27:27 +0100
commitf634bafc38bb948e41f829b16f03e2f0b8ee8f93 (patch)
treec4f8ff17614b68531355e8047d81e96719101d10 /libsqlitepp/selectcommand.cpp
parentIgnore Vim swap files (diff)
downloadlibdbpp-sqlite-f634bafc38bb948e41f829b16f03e2f0b8ee8f93.tar.bz2
libdbpp-sqlite-f634bafc38bb948e41f829b16f03e2f0b8ee8f93.tar.xz
libdbpp-sqlite-f634bafc38bb948e41f829b16f03e2f0b8ee8f93.zip
Adds support for SQLite, SQLite mocking and some basic tests
Diffstat (limited to 'libsqlitepp/selectcommand.cpp')
-rw-r--r--libsqlitepp/selectcommand.cpp80
1 files changed, 80 insertions, 0 deletions
diff --git a/libsqlitepp/selectcommand.cpp b/libsqlitepp/selectcommand.cpp
new file mode 100644
index 0000000..a3d5ac7
--- /dev/null
+++ b/libsqlitepp/selectcommand.cpp
@@ -0,0 +1,80 @@
+#include "selectcommand.h"
+#include "connection.h"
+#include "error.h"
+#include <string.h>
+
+class Column : public DB::Column {
+ public:
+ Column(const Glib::ustring & n, unsigned int i, sqlite3_stmt * s) :
+ DB::Column(n, i),
+ stmt(s)
+ {
+ }
+
+ bool isNull() const {
+ return (SQLITE_NULL == sqlite3_column_type(stmt, colNo));
+ }
+
+ void apply(DB::HandleField & h) const {
+ switch (sqlite3_column_type(stmt, colNo)) {
+ case SQLITE_INTEGER:
+ h.integer(sqlite3_column_int64(stmt, colNo));
+ return;
+ case SQLITE_FLOAT:
+ h.floatingpoint(sqlite3_column_double(stmt, colNo));
+ return;
+ case SQLITE_TEXT:
+ {
+ auto t = sqlite3_column_text(stmt, colNo);
+ auto l = sqlite3_column_bytes(stmt, colNo);
+ h.string(reinterpret_cast<const char *>(t), l);
+ return;
+ }
+ case SQLITE_NULL:
+ h.null();
+ return;
+ case SQLITE_BLOB:
+ throw std::runtime_error("Blobs not supported");
+ }
+
+ }
+
+ void rebind(DB::Command*, unsigned int) const {
+ throw std::runtime_error("Not implemented");
+ }
+
+ private:
+ sqlite3_stmt * const stmt;
+};
+
+SQLite::SelectCommand::SelectCommand(const Connection * conn, const std::string & sql) :
+ DB::Command(sql),
+ DB::SelectCommand(sql),
+ SQLite::Command(conn, sql)
+{
+}
+
+void
+SQLite::SelectCommand::execute()
+{
+ // No explicit execute required
+}
+
+bool
+SQLite::SelectCommand::fetch()
+{
+ switch (sqlite3_step(stmt)) {
+ case SQLITE_ROW:
+ if (columns.empty()) {
+ for (int c = sqlite3_data_count(stmt) - 1; c >= 0; c -= 1) {
+ columns.insert(DB::ColumnPtr(new Column(sqlite3_column_name(stmt, c), c, stmt)));
+ }
+ }
+ return true;
+ case SQLITE_DONE:
+ return false;
+ default:
+ throw Error(sqlite3_errmsg(c->db));
+ }
+}
+