summaryrefslogtreecommitdiff
path: root/libdbpp/selectcommandUtil.impl.h
blob: 9ef901e5aa5588f324c3ba87dc5ef25522c5bffa (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
104
105
106
107
108
109
#ifndef DB_SELECTCOMMANDUTIL_IMPL_H
#define DB_SELECTCOMMANDUTIL_IMPL_H

#include "selectcommand.h"
#include <type_traits>

/// @cond
namespace DB {
	template<typename Fields, typename Func, unsigned int field, typename... Fn, typename... Args>
	inline void
	forEachField(DB::SelectCommand * sel, const Func & func, Args &&... args)
	{
		if constexpr (field >= std::tuple_size<Fields>::value) {
			(void)sel;
			func(std::forward<Args>(args)...);
		}
		else {
			typename std::tuple_element<field, Fields>::type a;
			(*sel)[field] >> a;
			forEachField<Fields, Func, field + 1, Fn...>(sel, func, args..., a);
		}
	}

	template<typename... Fn, typename Func>
	inline void
	SelectCommand::forEachRow(const Func & func)
	{
		while (fetch()) {
			forEachField<std::tuple<Fn...>, Func, 0>(this, func);
		}
	}

	template<typename... Fn>
	inline RowRange<Fn...>
	SelectCommand::as()
	{
		return RowRange<Fn...>(this);
	}

	template<typename... Fn> inline RowRange<Fn...>::RowRange(SelectCommand * s) : sel(s) { }

	template<typename... Fn>
	inline RowRangeIterator<Fn...>
	RowRange<Fn...>::begin() const
	{
		return RowRangeIterator<Fn...>(sel);
	}

	template<typename... Fn>
	inline RowRangeIterator<Fn...>
	RowRange<Fn...>::end() const
	{
		return RowRangeIterator<Fn...>(nullptr);
	}

	template<typename... Fn> inline RowRangeIterator<Fn...>::RowRangeIterator(SelectCommand * s) : sel(s)
	{
		if (sel) {
			validRow = sel->fetch();
		}
		else {
			validRow = false;
		}
	}

	template<typename... Fn>
	inline bool
	RowRangeIterator<Fn...>::operator!=(const RowRangeIterator &) const
	{
		return validRow;
	}

	template<typename... Fn>
	inline void
	RowRangeIterator<Fn...>::operator++()
	{
		validRow = sel->fetch();
	}

	template<typename... Fn>
	inline Row<Fn...>
	RowRangeIterator<Fn...>::operator*() const
	{
		return Row<Fn...>(sel);
	}

	template<typename... Fn> inline Row<Fn...>::Row(SelectCommand * s) : RowBase(s) { }

	template<typename... Fn>
	template<unsigned int C>
	inline typename Row<Fn...>::template FieldType<C>
	Row<Fn...>::value() const
	{
		return get<C>();
	}

	template<typename... Fn>
	template<unsigned int C>
	inline typename Row<Fn...>::template FieldType<C>
	Row<Fn...>::get() const
	{
		FieldType<C> a;
		sel->operator[](C) >> a;
		return a;
	}
}
/// @endcond

#endif