summaryrefslogtreecommitdiff
path: root/libsqlitepp/sqlite-selectcommand.cpp
blob: e360dcdf6777382071c5073f052818f28bcc3526 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "sqlite-selectcommand.h"
#include "sqlite-connection.h"
#include "sqlite-error.h"
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index_container.hpp>
#include <cstring>

namespace SQLite {
	class Column : public DB::Column {
	public:
		Column(const Glib::ustring & n, unsigned int i, sqlite3_stmt * s) : DB::Column(n, i), stmt(s) { }

		[[nodiscard]] bool
		isNull() const override
		{
			return (SQLITE_NULL == sqlite3_column_type(stmt, static_cast<int>(colNo)));
		}

		void
		apply(DB::HandleField & h) const override
		{
			switch (sqlite3_column_type(stmt, static_cast<int>(colNo))) {
				case SQLITE_INTEGER:
					h.integer(sqlite3_column_int64(stmt, static_cast<int>(colNo)));
					return;
				case SQLITE_FLOAT:
					h.floatingpoint(sqlite3_column_double(stmt, static_cast<int>(colNo)));
					return;
				case SQLITE_TEXT: {
					auto t = sqlite3_column_text(stmt, static_cast<int>(colNo));
					auto l = sqlite3_column_bytes(stmt, static_cast<int>(colNo));
					h.string({reinterpret_cast<const char *>(t), static_cast<std::size_t>(l)});
					return;
				}
				case SQLITE_NULL:
					h.null();
					return;
				case SQLITE_BLOB:
					throw DB::ColumnTypeNotSupported();
			}
		}

	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 (!columnCount()) {
				for (int c = sqlite3_data_count(stmt) - 1; c >= 0; c -= 1) {
					insertColumn(std::make_unique<Column>(sqlite3_column_name(stmt, c), c, stmt));
				}
			}
			return true;
		case SQLITE_DONE:
			return false;
		default:
			throw Error(c->db);
	}
}