summaryrefslogtreecommitdiff
path: root/lib/input/mysqlRecordSet.h
blob: d07978a4765b02946a1c45799729973f598a934c (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
#ifndef MYGRATE_INPUT_MYSQLRECORDSET_H
#define MYGRATE_INPUT_MYSQLRECORDSET_H

#include "mysqlStmt.h"
#include <cstddef>
#include <dbRecordSet.h>
#include <dbTypes.h>
#include <memory>
#include <mysql.h>
#include <vector>

namespace MyGrate::Input {
	class ResultData;

	class MySQLData {
	public:
		using ResultDataPtr = std::unique_ptr<ResultData>;
		using ResPtr = std::unique_ptr<MYSQL_RES, decltype(&mysql_free_result)>;

		explicit MySQLData(StmtPtr s);

		std::size_t columns() const;

		DbValue at(std::size_t col) const;

		StmtPtr stmt;
		std::vector<MYSQL_BIND> fields;
		std::vector<ResultDataPtr> extras;
	};

	class MySQLCursor : public MySQLData, public Cursor {
	public:
		using MySQLData::MySQLData;

		bool fetch() override;
		std::size_t columns() const override;
		DbValue at(std::size_t col) const override;
	};

	class MySQLRecordSet : public MySQLData, public RecordSet {
	public:
		using StmtResPtr = std::unique_ptr<MYSQL_STMT, decltype(&mysql_stmt_free_result)>;

		explicit MySQLRecordSet(StmtPtr s);

		std::size_t rows() const override;

		std::size_t columns() const override;

		DbValue at(std::size_t row, std::size_t col) const override;

	private:
		StmtResPtr stmtres;
		mutable std::size_t currentRow {static_cast<size_t>(-1)};
	};
}

#endif