Branch data Line data Source code
1 : : /******************************************************************************
2 : : ------------- Copyright (c) 2009-2023 H a r a l d A c h i t z ---------------
3 : : ---------- < h a r a l d dot a c h i t z at g m a i l dot c o m > ------------
4 : : ---- This Source Code Form is subject to the terms of the Mozilla Public -----
5 : : ---- License, v. 2.0. If a copy of the MPL was not distributed with this -----
6 : : ---------- file, You can obtain one at http://mozilla.org/MPL/2.0/. ----------
7 : : ******************************************************************************/
8 : :
9 : : #ifndef SL3_DATASET_HPP_
10 : : #define SL3_DATASET_HPP_
11 : :
12 : : #include <map>
13 : : #include <vector>
14 : :
15 : : #include <sl3/config.hpp>
16 : : #include <sl3/dbvalues.hpp>
17 : : #include <sl3/error.hpp>
18 : : #include <sl3/rowcallback.hpp>
19 : :
20 : : namespace sl3
21 : : {
22 : : /**
23 : : * \brief A utility for processing query results.
24 : : *
25 : : * This class is a RowCallback that loads the result of a query into a list
26 : : * of DbValues.
27 : : * The loaded list can be browsed and the loaded values can be accessed.
28 : : *
29 : : * A Dataset can either be created:
30 : : * - giving a DbValuesTypeList that describes the fields \n
31 : : * Giving a DbValuesTypeList the types of DbValue will use the given
32 : : * storage
33 : : * type when reading
34 : : * the data from sqlite. \n
35 : : * When the Dataset becomes populated with data the field count will be
36 : : * validated.
37 : : * In case the number of fields is different sl3::ErrTypeMisMatch will
38 : : * be thrown.
39 : : *
40 : : * - Without any specification \n
41 : : * In this case all fields will be DsVariantField,
42 : : * using the storage type sqlite reports for the actual value.
43 : : *
44 : : *
45 : : *
46 : : */
47 : : class LIBSL3_API Dataset final : public Container<std::vector<DbValues>>
48 : : {
49 : : friend class Command;
50 : :
51 : : public:
52 : : /**
53 : : * \brief Constructor
54 : : *
55 : : * All fields will be DsVariantField, using the storage type sqlite
56 : : * reports for the actual value.
57 : : * Field count will be detected and applied.
58 : : */
59 : : Dataset () noexcept;
60 : :
61 : : /**
62 : : * \brief Constructor with DbValuesTypeList as description
63 : : *
64 : : * Types of DbValue will use the given description when creating the
65 : : * DbValue list.
66 : : * If the given list is not empty, field count will be validated when
67 : : * the actual instance becomes populated with data.
68 : : * \param types Types the fields must satisfy
69 : : */
70 : : Dataset (Types types);
71 : :
72 : : /**
73 : : * \brief Copy Constructor
74 : : */
75 : : Dataset (const Dataset&) = default;
76 : :
77 : : /**
78 : : * \brief Move Constructor
79 : : */
80 : : Dataset (Dataset&&) noexcept (
81 : : std::is_nothrow_move_constructible<Container<DbValues>>::value
82 : : && std::is_nothrow_move_constructible<Types>::value
83 : : && std::is_nothrow_move_constructible<
84 : : std::vector<std::string>>::value);
85 : : // = default; no mscv does not like it
86 : :
87 : : /**
88 : : * \brief Value assignment
89 : : * \return reference to this
90 : : */
91 : : Dataset& operator= (const Dataset&) = default;
92 : :
93 : : /**
94 : : * \brief Rvalue assignment
95 : : * \return reference to this
96 : : */
97 : 2 : Dataset& operator= (Dataset&&) = default;
98 : :
99 : : /**
100 : : * \brief Clear all states.
101 : : * Removes loaded data so that the actual instance can be reused/refilled.
102 : : *
103 : : */
104 : : void reset ();
105 : :
106 : : /**
107 : : * \brief Clear all states.
108 : : *
109 : : * Removes loaded data and sets a new specification for the field
110 : : * description so that the actual instance can be reused to populate with a
111 : : * different select statement / SQL command. Passing an empty
112 : : * DbValuesTypeList
113 : : * means that all fields will be
114 : : * DsVariantField and field count will
115 : : * be detected.
116 : : *
117 : : * \param types new Types requirement
118 : : */
119 : : void reset (const Types& types);
120 : :
121 : : /**
122 : : * \brief Merge another Dataset.
123 : : *
124 : : * Appends the data of the given Dataset to the end of the actual data.
125 : : * The field names and types of the given Dataset must match the current
126 : : * one.
127 : : *
128 : : * \throw sl3::ErrTypeMisMatch if field names types are not equal or
129 : : * size differs.
130 : : *
131 : : * \param other Dataset which shall be added to this one.
132 : : */
133 : : void merge (const Dataset& other);
134 : :
135 : : /**
136 : : * \brief Merge DbValues.
137 : : *
138 : : * Appends the DbValues to the end of the actual data.
139 : : *
140 : : * \throw sl3::ErrTypeMisMatch if size differs from existing row size or
141 : : * if types are not compatible
142 : : *
143 : : * \param row A row which shall be added.
144 : : */
145 : : void merge (const DbValues& row);
146 : :
147 : : /**
148 : : * \brief Get the index of a field by name
149 : : *
150 : : * \throw sl3::OutOfRange if name is not found
151 : : * \param name field
152 : : * name
153 : : * \return field index
154 : : */
155 : : std::size_t getIndex (const std::string& name) const;
156 : :
157 : : /**
158 : : * \brief Typedef for a relation function signature
159 : : *
160 : : * Used to specify the less function that shall be used for sorting
161 : : *
162 : : * a Dataset.
163 : : *
164 : : * \see Dataset::sort
165 : : */
166 : : typedef bool (*DbValueSort) (const DbValue&, const DbValue&);
167 : :
168 : : // using DbValueSort = std::function<bool(const DbValue&, const DbValue&)>
169 : : // ;
170 : :
171 : : /**
172 : : * \brief Sort the Dataset
173 : : *
174 : : * Sort according to the given field indexes.
175 : : * The Dataset will be sorted according to sqlite rules.
176 : : *
177 : : * \throw sl3::OutOfRange if a given index is invalid
178 : : * \param idxs list of field indexes
179 : : * \param cmp pointer to a less than compare function, default dbval_lt
180 : : */
181 : : void sort (const std::vector<size_t>& idxs, DbValueSort cmp = &dbval_lt);
182 : :
183 : : private:
184 : : Types _fieldtypes;
185 : : std::vector<std::string> _names;
186 : : };
187 : : }
188 : :
189 : : #endif
|