Flox Package Database 1
CRUD Operations on Nix Package Metadata
Loading...
Searching...
No Matches
pkg-query.hh
Go to the documentation of this file.
1/* ========================================================================== *
2 *
3 * @file flox/pkgdb/pkg-query.hh
4 *
5 * @brief Interfaces for constructing complex `Packages' queries.
6 *
7 *
8 * -------------------------------------------------------------------------- */
9
10#pragma once
11
12#include <cstdint>
13#include <memory>
14#include <optional>
15#include <sstream>
16#include <string>
17#include <string_view>
18#include <unordered_map>
19#include <unordered_set>
20#include <utility>
21#include <vector>
22
23#include <nix/config.hh>
24#include <nix/globals.hh>
25#include <nlohmann/json.hpp>
26
27#include "compat/concepts.hh"
29#include "flox/core/types.hh"
30
31
32/* -------------------------------------------------------------------------- */
33
34/* Forward Declarations. */
35
36namespace sqlite3pp {
37class database;
38class query;
39} // namespace sqlite3pp
40
41
42/* -------------------------------------------------------------------------- */
43
44namespace flox::pkgdb {
45
46/* -------------------------------------------------------------------------- */
47
48using row_id = uint64_t;
51/* -------------------------------------------------------------------------- */
52
62 "invalid package query argument" )
66/* -------------------------------------------------------------------------- */
67
68
74struct PkgQueryArgs
75{
76
77 std::optional<std::string> name;
78 std::optional<std::string> pname;
79 std::optional<std::string> version;
80 std::optional<std::string> semver;
83 std::optional<std::string> partialMatch;
84
86 std::optional<std::string> partialNameMatch;
87
89 std::optional<std::string> pnameOrAttrName;
90
96 std::optional<std::vector<std::string>> licenses;
97
99 bool allowBroken = false;
100
102 bool allowUnfree = true;
103
105 bool preferPreReleases = false;
106
113 std::optional<std::vector<Subtree>> subtrees;
114
116 std::vector<System> systems = { nix::settings.thisSystem.get() };
117
124 std::optional<flox::AttrPath> relPath;
125
126
128 void
129 clear();
130
141 void
142 check() const;
143
144
145}; /* End struct `PkgQueryArgs' */
146
147/* -------------------------------------------------------------------------- */
148
153void
154to_json( nlohmann::json & jto, const PkgQueryArgs & descriptor );
155
156
157/* -------------------------------------------------------------------------- */
158
165class PkgQuery : public PkgQueryArgs
166{
167
168private:
169
171 std::stringstream selects;
173 bool firstSelect = true;
174
176 std::stringstream orders;
178 bool firstOrder = true;
179
181 std::stringstream wheres;
183 bool firstWhere = true;
184
186 std::unordered_map<std::string, std::string> binds;
187
197 std::vector<std::string> exportedColumns = { "id", "semver" };
198
199
200 /* Member Functions */
201
209 void
210 clearBuilt();
211
221 void
222 addSelection( std::string_view column );
223
225 void
226 addOrderBy( std::string_view order );
227
231 void
232 addWhere( std::string_view cond );
233
240 [[nodiscard]] std::unordered_set<std::string>
241 filterSemvers( const std::unordered_set<std::string> & versions ) const;
242
244 void
245 initMatch();
246
250 void
251 initSubtrees();
252
256 void
257 initSystems();
258
260 void
261 initOrderBy();
262
270 void
271 init();
272
273
274public:
275
276 PkgQuery() { this->init(); }
277
278 explicit PkgQuery( const PkgQueryArgs & params ) : PkgQueryArgs( params )
279 {
280 this->init();
281 }
282
283 PkgQuery( const PkgQueryArgs & params,
284 std::vector<std::string> exportedColumns )
285 : PkgQueryArgs( params ), exportedColumns( std::move( exportedColumns ) )
286 {
287 this->init();
288 }
289
298 [[nodiscard]] std::string
299 str() const;
300
308 [[nodiscard]] std::shared_ptr<sqlite3pp::query>
309 bind( sqlite3pp::database & pdb ) const;
310
317 [[nodiscard]] std::vector<row_id>
318 execute( sqlite3pp::database & pdb ) const;
319
320
321}; /* End class `PkgQuery' */
322
323
324/* -------------------------------------------------------------------------- */
325
326} // namespace flox::pkgdb
327
328
329/* -------------------------------------------------------------------------- *
330 *
331 *
332 *
333 * ========================================================================== */
Indicates invalid arguments were set in a flox::resolver::PkgQueryArgs struct.
A query used to lookup packages in a database.
Definition: pkg-query.hh:166
std::stringstream orders
Definition: pkg-query.hh:176
std::shared_ptr< sqlite3pp::query > bind(sqlite3pp::database &pdb) const
Create a bound SQLite query ready for execution.
Definition: pkg-query.cc:532
std::stringstream wheres
Definition: pkg-query.hh:181
void clearBuilt()
Clear member PkgQuery member variables of any state from past initialization runs.
Definition: pkg-query.cc:169
std::vector< row_id > execute(sqlite3pp::database &pdb) const
Query a given database returning an ordered list of satisfactory Packages.ids.
Definition: pkg-query.cc:548
void initMatch()
A helper of init() which handles match filtering/ranking.
Definition: pkg-query.cc:201
void init()
Translate floco::pkgdb::PkgQueryArgs parameters to a built SQL statement held in std::stringstream me...
Definition: pkg-query.cc:406
std::unordered_map< std::string, std::string > binds
Definition: pkg-query.hh:186
std::stringstream selects
Definition: pkg-query.hh:171
void initSubtrees()
A helper of init() which handles subtrees filtering/ranking.
Definition: pkg-query.cc:275
void addOrderBy(std::string_view order)
Appends the ORDER BY block.
Definition: pkg-query.cc:150
bool firstSelect
Definition: pkg-query.hh:173
std::string str() const
Produce an unbound SQL statement from various member variables.
Definition: pkg-query.cc:483
void addSelection(std::string_view column)
Add a new column to the inner SELECT statement.
Definition: pkg-query.cc:142
std::vector< std::string > exportedColumns
Definition: pkg-query.hh:197
void addWhere(std::string_view cond)
Appends the WHERE block with a new AND ( <COND> ) statement.
Definition: pkg-query.cc:158
std::unordered_set< std::string > filterSemvers(const std::unordered_set< std::string > &versions) const
Filter a set of semantic version numbers by the range indicated in the semvers member variable.
Definition: pkg-query.cc:508
void initOrderBy()
A helper of init() which constructs the ORDER BY block.
Definition: pkg-query.cc:353
bool firstOrder
Definition: pkg-query.hh:178
void initSystems()
A helper of init() which handles systems filtering/ranking.
Definition: pkg-query.cc:319
bool firstWhere
Definition: pkg-query.hh:183
Provides backports of some concepts features that are missing in Clang v11.
Definitions of various std::exception children used for throwing errors with nice messages and typed ...
#define FLOX_DEFINE_EXCEPTION(NAME, ERROR_CODE, CATEGORY_MSG)
Generate a class definition with an error code and category message.
Definition: exceptions.hh:206
Interfaces for caching package metadata in SQLite3 databases.
Definition: command.cc:44
uint64_t row_id
Definition: pkg-query.hh:48
void to_json(nlohmann::json &jto, const PkgQueryArgs &args)
Convert an flox::pkgdb::PkgQueryArgs to a JSON object.
Definition: pkg-query.cc:97
@ EC_INVALID_PKG_QUERY_ARG
Definition: exceptions.hh:41
Interfaces for analyzing version numbers.
Definition: versions.cc:31
Miscellaneous typedefs and aliases.