-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathschema.hpp
228 lines (194 loc) · 4.55 KB
/
schema.hpp
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#pragma once
#include <fstream>
#include <set>
#include <type_traits>
#include <utility>
#include <vector>
#include "cexpr/string.hpp"
#include "sql/column.hpp"
#include "sql/index.hpp"
#include "sql/row.hpp"
namespace sql
{
template <cexpr::string Name, typename Index, typename... Cols>
class schema
{
public:
static constexpr auto name{ Name };
using row_type = sql::variadic_row<Cols...>::row_type;
using container = typename
std::conditional_t<
std::is_same_v<Index, sql::index<>>,
std::vector<row_type>,
std::multiset<row_type, typename Index::template comparator<row_type>>
>;
using const_iterator = typename container::const_iterator;
schema() = default;
template <typename Type, typename... Types>
schema(std::vector<Type> const& col, Types const&... cols) : schema{}
{
insert(col, cols...);
}
template <typename Type, typename... Types>
schema(std::vector<Type>&& col, Types&&... cols) : schema{}
{
insert(std::forward<Type>(col), std::forward<Types>(cols)...);
}
template <typename... Types>
inline void emplace(Types const&... vals)
{
if constexpr (std::is_same_v<Index, sql::index<>>)
{
table_.emplace_back(vals...);
}
else
{
table_.emplace(vals...);
}
}
template <typename... Types>
inline void emplace(Types&&... vals)
{
if constexpr (std::is_same_v<Index, sql::index<>>)
{
table_.emplace_back(vals...);
}
else
{
table_.emplace(vals...);
}
}
template <typename Type, typename... Types>
void insert(std::vector<Type> const& col, Types const&... cols)
{
for (std::size_t i{}; i < col.size(); ++i)
{
emplace(col[i], cols[i]...);
}
}
template <typename Type, typename... Types>
void insert(std::vector<Type>&& col, Types&&... cols)
{
for (std::size_t i{}; i < col.size(); ++i)
{
emplace(std::forward<Type>(col[i]), std::forward<Types>(cols[i])...);
}
}
void insert(row_type const& row)
{
if constexpr (std::is_same_v<Index, sql::index<>>)
{
table_.push_back(row);
}
else
{
table_.insert(row);
}
}
void insert(row_type&& row)
{
if constexpr (std::is_same_v<Index, sql::index<>>)
{
table_.push_back(std::forward<row_type>(row));
}
else
{
table_.insert(std::forward<row_type>(row));
}
}
inline const_iterator begin() const noexcept
{
return table_.begin();
}
inline const_iterator end() const noexcept
{
return table_.end();
}
private:
container table_;
};
namespace
{
template <typename Row>
void fill(std::fstream& fstr, Row& row, [[maybe_unused]] char delim)
{
if constexpr (!std::is_same_v<Row, sql::void_row>)
{
if constexpr (std::is_same_v<typename Row::column::type, std::string>)
{
if constexpr (std::is_same_v<typename Row::next, sql::void_row>)
{
std::getline(fstr, row.head());
}
else
{
std::getline(fstr, row.head(), delim);
}
}
else
{
fstr >> row.head();
}
fill<typename Row::next>(fstr, row.tail(), delim);
}
}
template <typename Row>
void fill(std::fstream& fstr, Row const& row, char delim)
{
if constexpr (!std::is_same_v<Row, sql::void_row>)
{
fstr << row.head();
if constexpr (std::is_same_v<typename Row::next, sql::void_row>)
{
fstr << '\n';
}
else
{
fstr << delim;
}
fill<typename Row::next>(fstr, row.tail(), delim);
}
}
} // namespace
// helper function for users to load a data into a schema from a file
template <typename Schema>
Schema load(std::string const& file, char delim)
{
auto fstr{ std::fstream(file, fstr.in) };
Schema table{};
typename Schema::row_type row{};
while (fstr)
{
fill<typename Schema::row_type>(fstr, row, delim);
table.insert(std::move(row));
// in case last stream extraction did not remove newline
if (fstr.get() != '\n')
{
fstr.unget();
}
}
return table;
}
// for compat with previous versions
template <typename Schema, char Delim>
inline Schema load(std::string const& file)
{
return load<Schema>(file, Delim);
}
// will work with schema and query objects
template <typename Type>
void store(Type const& data, std::string const& file, char delim)
{
auto fstr{ std::fstream(file, fstr.out) };
for (auto const& row : data)
{
fill<typename Type::row_type>(fstr, row, delim);
}
}
// for devs who want to use the previous format
template <typename Type, char Delim>
inline void store(Type const& data, std::string const& file)
{
store<Type>(data, file, Delim);
}
} // namespace sql