libsl3 1.2.41002
A C++ interface for SQLite
Loading...
Searching...
No Matches
dataset.hpp
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
20namespace sl3
21{
22 /**
23 * \brief A utility for processing the result queries.
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 wiht 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 Rvalues assignment
95 * \return reference to this
96 */
97 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
111 * so that the actual instance can be reused for populate with a different
112 * select statement / sql command.
113 * Passing an empty DbValuesTypeList mean that all fields will be
114 * DsVariandField and field count will be detected.
115 *
116 * \param types new Types requirement
117 */
118 void reset (const Types& types);
119
120 /**
121 * \brief Merge an other Dataset.
122 *
123 * Appends the data of the given Dataset to the end of the actual data.
124 * The field names and types of the given Dataset must match the actuals
125 * one.
126 *
127 * \throw sl3::ErrTypeMisMatch if field names types are not equal or
128 * size differs.
129 *
130 * \param other Dataset which shall be added to this one.
131 */
132 void merge (const Dataset& other);
133
134 /**
135 * \brief Merge DbValues.
136 *
137 * Appends the DbValues to the end of the actual data.
138 *
139 * \throw sl3::ErrTypeMisMatch if size differs from existing row size or
140 * if types are not compatible
141 *
142 * \param row A row which shall be added.
143 */
144 void merge (const DbValues& row);
145
146 /**
147 * \brief Get the index of a field by namespace
148 *
149 * \throw sl3::OutOfRanage if name is not found
150 * \param name field name
151 * \return field index
152 */
153 std::size_t getIndex (const std::string& name) const;
154
155 /**
156 * \brief Typedef for a relation function signature
157 *
158 * Used to specify the the less function that shall be used for sorting
159 * a Dataset.
160 *
161 * \see Dataset::sort
162 */
163 typedef bool (*DbValueSort) (const DbValue&, const DbValue&);
164
165 // using DbValueSort = std::function<bool(const DbValue&, const DbValue&)>
166 // ;
167
168 /**
169 * \brief Sort the Dataset
170 *
171 * Sort according to the given field indexes.
172 * The Dataset will be sorted according to sqlite rules.
173 *
174 * \throw sl2::OutOfRange if a given index is invalid
175 * \param idxs list of field indexes
176 * \param cmp pointer to a less than compare function, default dbval_lt
177 */
178 void sort (const std::vector<size_t>& idxs, DbValueSort cmp = &dbval_lt);
179
180 private:
181 Types _fieldtypes;
182 std::vector<std::string> _names;
183 };
184}
185
186#endif
A compiled SQL command.
Definition: command.hpp:40
Wrapper to provide begin, end and random access of a container.
Definition: container.hpp:29
A utility for processing the result queries.
Definition: dataset.hpp:48
Dataset() noexcept
Constructor.
This class models the duck typing sqlite uses. It supports int, real, text, blob and null values.
Definition: dbvalue.hpp:43
A row of DbValues.
Definition: dbvalues.hpp:30
A Container holding sl3::Type values.
Definition: types.hpp:62
Namespace of libSL3.
Definition: columns.hpp:18
STL namespace.