summaryrefslogtreecommitdiff
path: root/libmysqlpp/my-selectcommand.cpp
blob: dbfc5c871ad3d48976d9aa71252dc98a711bd8db (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "my-selectcommand.h"
#include "command.h"
#include "my-column.h"
#include "my-command.h"
#include "my-error.h"
#include "selectcommand.h"
#include <cstdint>
#include <cstring>
#include <memory>
#include <mysql.h>
#include <vector>

MySQL::SelectCommand::SelectCommand(const Connection * conn, const std::string & sql) :
	DB::Command(sql), DB::SelectCommand(sql), MySQL::Command(conn, sql), prepared(false), executed(false)
{
}

void
MySQL::SelectCommand::execute()
{
	if (!prepared) {
		bindParams();
		fields.resize(mysql_stmt_field_count(stmt.get()));
		for (auto & b : fields) {
			memset(&b, 0, sizeof(MYSQL_BIND));
		}
		MYSQL_RES * prepare_meta_result = mysql_stmt_result_metadata(stmt.get());
		MYSQL_FIELD * fieldDefs = mysql_fetch_fields(prepare_meta_result);
		for (std::size_t i = 0; i < fields.size(); i += 1) {
			switch (fieldDefs[i].type) {
				case MYSQL_TYPE_TINY:
				case MYSQL_TYPE_SHORT:
				case MYSQL_TYPE_LONG:
				case MYSQL_TYPE_INT24:
				case MYSQL_TYPE_LONGLONG:
				case MYSQL_TYPE_YEAR:
					insertColumn(
							std::make_unique<Column<int64_t, MYSQL_TYPE_LONGLONG>>(fieldDefs[i].name, i, &fields[i]));
					break;
				case MYSQL_TYPE_DECIMAL:
				case MYSQL_TYPE_NEWDECIMAL:
				case MYSQL_TYPE_FLOAT:
				case MYSQL_TYPE_DOUBLE:
					insertColumn(std::make_unique<Column<double, MYSQL_TYPE_DOUBLE>>(fieldDefs[i].name, i, &fields[i]));
					break;
				case MYSQL_TYPE_TIMESTAMP:
				case MYSQL_TYPE_DATE:
				case MYSQL_TYPE_DATETIME:
					insertColumn(std::make_unique<Column<MYSQL_TIME, MYSQL_TYPE_DATETIME>>(
							fieldDefs[i].name, i, &fields[i]));
					break;
				case MYSQL_TYPE_TIME:
					insertColumn(
							std::make_unique<Column<MYSQL_TIME, MYSQL_TYPE_TIME>>(fieldDefs[i].name, i, &fields[i]));
					break;
				case MYSQL_TYPE_STRING:
				case MYSQL_TYPE_VAR_STRING:
					insertColumn(std::make_unique<StringColumn>(fieldDefs[i].name, i, &fields[i], fieldDefs[i].length));
					break;
				case MYSQL_TYPE_NULL:
					insertColumn(std::make_unique<NullColumn>(fieldDefs[i].name, i, &fields[i]));
					break;
				case MYSQL_TYPE_BIT:
				case MYSQL_TYPE_BLOB:
				case MYSQL_TYPE_SET:
				case MYSQL_TYPE_ENUM:
				case MYSQL_TYPE_GEOMETRY:
				default:
					mysql_free_result(prepare_meta_result);
					throw DB::ColumnTypeNotSupported();
			}
		}
		mysql_free_result(prepare_meta_result);
		if (mysql_stmt_bind_result(stmt.get(), &fields.front())) {
			throw Error(stmt.get());
		}
		prepared = true;
	}
	if (!executed) {
		if (mysql_stmt_execute(stmt.get())) {
			throw Error(stmt.get());
		}
		if (mysql_stmt_store_result(stmt.get())) {
			throw Error(stmt.get());
		}
		executed = true;
	}
}

bool
MySQL::SelectCommand::fetch()
{
	execute();
	switch (mysql_stmt_fetch(stmt.get())) {
		case 0:
			return true;
		case MYSQL_NO_DATA:
			executed = false;
			return false;
		default:
			throw Error(stmt.get());
	}
}