libsl3 1.2.41002
A C++ interface for SQLite
Loading...
Searching...
No Matches
command.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_SQLCOMMAND_HPP
10#define SL3_SQLCOMMAND_HPP
11
12#include <memory>
13#include <string>
14
15#include <sl3/config.hpp>
16#include <sl3/dataset.hpp>
17#include <sl3/dbvalue.hpp>
18#include <sl3/rowcallback.hpp>
19
20struct sqlite3;
21struct sqlite3_stmt;
22
23namespace sl3
24{
25
26 namespace internal
27 {
28 class Connection;
29 }
30
31 /**
32 * \brief A compiled SQL command
33 *
34 * This class holds a compiled SQL statement so that it can be reused.
35 *
36 * A command can have parameters.
37 *
38 */
39 class LIBSL3_API Command
40 {
41 friend class Database;
42 using Connection = std::shared_ptr<internal::Connection>;
43
44 Command (Connection connection, const std::string& sql);
45
46 Command (Connection connection,
47 const std::string& sql,
49
50 Command () = delete;
51 Command (const Command&) = delete;
52 Command operator= (const Command&) = delete;
53 Command operator= (Command&&) = delete;
54
55 public:
56 /**
57 * \brief Move constructor.
58 *
59 * A command is movable
60 *
61 */
63
64 /**
65 * \brief Destructor.
66 *
67 * The destructor calls release.
68 */
69 ~Command (); // cause of unique pointer, check if protected is possible
70
71 /**
72 * \brief Run the Command and get the result
73 *
74 * Runs the command, applying possible already given parameters
75 * and returns the result in a Dataset.
76 *
77 *
78 * \return A Dataset containing the query result
79 */
81
82 /**
83 * \brief Run the Command and get the result
84 *
85 * Runs the command, applying given parameters
86 * and returns the result in a Dataset.
87 * It types are given, they are used for the returned Dataset.
88 * It no types are given all fields in the returned Dataset will be
89 * of Type::Variant
90 *
91 * \throw sl3::ErrTypeMisMatch if types are given which are invalid or
92 * given parameters are of the wrong size.
93 * \param parameters a list of parameters
94 * \param types Types the Dataset shall use
95 * \return A Dataset containing the query result
96 */
97 Dataset select (const DbValues& parameters, const Types& types = {});
98
99 /**
100 * \brief Run the Command and get the result
101 *
102 * Runs the command, applying possible given parameters
103 * and returns the result in a Dataset in which given types are
104 * used for the fields.
105 *
106 * \throw sl3::ErrTypeMisMatch if types are given which are invalid or
107 * given parameters are of the wrong size.
108 * \param parameters a list of parameters
109 * \param types Types the Dataset shall use
110 * \return A Dataset containing the query result
111 */
112 Dataset select (const Types& types, const DbValues& parameters = {});
113
114 /**
115 * \brief function object for handling a command result.
116 *
117 * Functor called for each processed row of Command or a
118 * sql statement.
119 *
120 * \return false if processing the query result shall stop
121 * \n true otherwise
122 */
123 using Callback = std::function<bool (Columns)>;
124
125 /**
126 * \brief Execute the command
127 *
128 * Runs the current command.
129 * If parameters are set,
130 *
131 */
132 void execute ();
133
134 /**
135 * \brief Execute the command
136 *
137 * Applies given parameters and run the current command.
138 *
139 * \throw sl3::ErrTypeMisMatch given parameters are of the wrong size.
140 * \param parameters a list of parameters
141 */
142 void execute (const DbValues& parameters);
143
144 /**
145 * \brief Execute the command applying given callback
146 *
147 * Applies given parameters and run the current command.
148 *
149 * \throw sl3::ErrTypeMisMatch given parameters are of the wrong size.
150 * \param cb a callback
151 * \param parameters a list of parameters
152 */
153 void execute (RowCallback& cb, const DbValues& parameters = {});
154
155 /**
156 * \brief Execute the command applying given callback
157 *
158 * Applies given parameters and run the current command.
159 *
160 * \throw sl3::ErrTypeMisMatch given parameters are of the wrong size.
161 * \param cb a callback
162 * \param parameters a list of parameters
163 */
164 void execute (Callback cb, const DbValues& parameters = {});
165
166 /**
167 * \brief Parameters of command.
168 *
169 * \return referenct to the parameters
170 */
172
173 /**
174 * \brief Parameters of command.
175 *
176 * \return const referenct to the parameters
177 */
178 const DbValues& getParameters () const;
179
180 /**
181 * \brief get Parameter at given index.
182 * \param idx index
183 * \throw sl3::ErrOutOfRange if index is invalid
184 * \return referenct to the parameters at given index
185 */
187
188 /**
189 * \brief get Parameter at given index.
190 * \param idx index
191 * \throw sl3::ErrOutOfRange if index is invalid
192 * \return const referenct to the parameters at given index
193 */
194 const DbValue& getParameter (int idx) const;
195
196 /**
197 * \brief Set parameter values
198 *
199 * The Types ot the given values must be compatible with the
200 * current ones.
201 *
202 * \param values new paramter values
203 * \throw sl3::ErrTypeMisMatch if size of values differs from the actual
204 * parameter size or DbValue assignment throws.
205 * \see DbValue
206 *
207 */
208 void setParameters (const DbValues& values);
209
210 /**
211 * \brief Set new parameter values
212 *
213 * In contrast to setParameters, where the DbValue types must match,
214 * this can be used to set other Types.
215 *
216 * \param values new parameter values
217 * \throw sl3::ErrTypeMisMatch if size of values differs from the actual
218 * one
219 */
221
222 /**
223 * \brief get a list of the parameter names
224 *
225 * If the command has no parameters, the list will be empty.
226 * Parameters with no name will be an empty entry in the list.
227 *
228 * \return list of names
229 */
230 std::vector<std::string> getParameterNames () const;
231
232 private:
233 Connection _connection;
234 sqlite3_stmt* _stmt;
235 DbValues _parameters;
236 };
237
238 /**
239 * \brief Syntax sugar to create command parameters
240 *
241 * Creates DbValues with types based on the given arguments.
242 * \code
243 * cmd.execute(parameters(1,"foo",3.3))
244 * \endcode
245 * Will create DbValues of Type::Int, Type::Text and Type::Real
246 *
247 * \tparam VALS variadic argument types
248 * \param vals variadic argument values
249 *
250 * \return DbValues constructed by given arguments
251 */
252 template <typename... VALS>
254 parameters (VALS&&... vals)
255 {
256 return {DbValue{vals}...};
257 }
258}
259
260#endif
Class to access data of query results.
Definition: columns.hpp:38
A compiled SQL command.
Definition: command.hpp:40
const DbValues & getParameters() const
Parameters of command.
~Command()
Destructor.
Dataset select()
Run the Command and get the result.
void execute(const DbValues &parameters)
Execute the command.
void execute(RowCallback &cb, const DbValues &parameters={})
Execute the command applying given callback.
const DbValue & getParameter(int idx) const
get Parameter at given index.
std::function< bool(Columns)> Callback
function object for handling a command result.
Definition: command.hpp:123
Dataset select(const DbValues &parameters, const Types &types={})
Run the Command and get the result.
void execute(Callback cb, const DbValues &parameters={})
Execute the command applying given callback.
DbValues & getParameters()
Parameters of command.
std::vector< std::string > getParameterNames() const
get a list of the parameter names
DbValue & getParameter(int idx)
get Parameter at given index.
void resetParameters(DbValues values)
Set new parameter values.
Command(Command &&)
Move constructor.
Dataset select(const Types &types, const DbValues &parameters={})
Run the Command and get the result.
void execute()
Execute the command.
void setParameters(const DbValues &values)
Set parameter values.
represents a SQLite3 Database
Definition: database.hpp:43
A utility for processing the result queries.
Definition: dataset.hpp:48
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
Callback for SQL Select statements.
Definition: rowcallback.hpp:30
A Container holding sl3::Type values.
Definition: types.hpp:62
Namespace of libSL3.
Definition: columns.hpp:18
DbValues parameters(VALS &&... vals)
Syntax sugar to create command parameters.
Definition: command.hpp:254