Flox Package Database 1
CRUD Operations on Nix Package Metadata
Loading...
Searching...
No Matches
read.hh
Go to the documentation of this file.
1/* ========================================================================== *
2 *
3 * @file flox/pkgdb/read.hh
4 *
5 * @brief Interfaces for reading a SQLite3 package set database.
6 *
7 *
8 * -------------------------------------------------------------------------- */
9
10#pragma once
11
12#include <filesystem>
13#include <functional>
14#include <queue>
15#include <string>
16#include <vector>
17
18#include <nix/eval-cache.hh>
19#include <nix/flake/flake.hh>
20#include <nlohmann/json.hpp>
21#include <sqlite3pp.hh>
22
23#include "flox/core/command.hh"
25#include "flox/core/types.hh"
26#include "flox/package.hh"
28
29
30/* -------------------------------------------------------------------------- */
31
32/* This is passed in by `make' and is set by `<pkgdb>/version' */
33#ifndef FLOX_PKGDB_VERSION
34# define FLOX_PKGDB_VERSION "NO.VERSION"
35#endif
36
37
38/* -------------------------------------------------------------------------- */
39
41namespace flox::pkgdb {
42
43/* -------------------------------------------------------------------------- */
44
47{
48
53 unsigned tables;
54
60 unsigned views;
61
63 constexpr bool
64 operator==( const SqlVersions & other ) const
65 {
66 return ( this->tables == other.tables ) && ( this->views == other.views );
67 }
68
70 constexpr bool
71 operator!=( const SqlVersions & other ) const
72 {
73 return ! ( ( *this ) == other );
74 }
75
76 friend std::ostream &
77 operator<<( std::ostream & oss, const SqlVersions & versions );
78
79}; /* End struct `SqlVersions' */
80
82std::ostream &
83operator<<( std::ostream & oss, const SqlVersions & versions );
84
85
87constexpr SqlVersions sqlVersions = { .tables = 2, .views = 3 };
88
89
90/* -------------------------------------------------------------------------- */
91
93using Fingerprint = nix::flake::Fingerprint;
94using SQLiteDb = sqlite3pp::database;
95using sql_rc = int;
98/* -------------------------------------------------------------------------- */
99
106FLOX_DEFINE_EXCEPTION( PkgDbException, EC_PKG_DB, "error running pkgdb" )
110/* -------------------------------------------------------------------------- */
111
112
119std::filesystem::path
121
123std::filesystem::path
124genPkgDbName( const Fingerprint & fingerprint,
125 const std::filesystem::path & cacheDir = getPkgDbCachedir() );
126
127
128/* -------------------------------------------------------------------------- */
129
135{
136
137 /* Data */
138
139public:
140
142 std::filesystem::path dbPath;
143 SQLiteDb db;
147 {
148 std::string string;
150 nlohmann::json attrs = nlohmann::json::object();
151 };
152 struct LockedFlakeRef lockedRef;
155 /* Errors */
156
157 // public:
158
161 {
162 explicit NoSuchDatabase( const PkgDbReadOnly & pdb )
164 std::string( "No such database '" + pdb.dbPath.string() + "'." ) )
165 {}
166 }; /* End struct `NoSuchDatabase' */
167
168
169 /* Internal Helpers */
170
171protected:
172
174 void
175 loadLockedFlake();
176
177
178private:
179
185 void
186 init();
187
188
189 /* Constructors */
190
191protected:
192
199 PkgDbReadOnly() : fingerprint( nix::htSHA256 ) {}
200
201
202public:
203
210 explicit PkgDbReadOnly( std::string_view dbPath )
211 : fingerprint( nix::htSHA256 ) /* Filled by `loadLockedFlake' later */
212 , dbPath( dbPath )
213 {
214 this->init();
215 }
216
224 PkgDbReadOnly( const Fingerprint & fingerprint, std::string_view dbPath )
225 : fingerprint( fingerprint ), dbPath( dbPath )
226 {
227 this->init();
228 }
229
236 explicit PkgDbReadOnly( const Fingerprint & fingerprint )
237 : PkgDbReadOnly( fingerprint, genPkgDbName( fingerprint ).string() )
238 {}
239
240
241 /* Queries */
242
243 // public:
244
247 getDbVersion();
248
255 row_id
256 getAttrSetId( const flox::AttrPath & path );
257
264 bool
265 hasAttrSet( const flox::AttrPath & path );
266
274 bool
275 completedAttrSet( row_id row );
276
285 bool
286 completedAttrSet( const flox::AttrPath & path );
287
295 getAttrSetPath( row_id row );
296
304 row_id
305 getPackageId( const flox::AttrPath & path );
306
314 getPackagePath( row_id row );
315
324 bool
325 hasPackage( const flox::AttrPath & path );
326
327
333 std::string
334 getDescription( row_id descriptionId );
335
336
344 std::vector<row_id>
345 getPackages( const PkgQueryArgs & params );
346
347
356 nlohmann::json
357 getPackage( row_id row );
358
359
368 nlohmann::json
369 getPackage( const flox::AttrPath & path );
370
371
372 nix::FlakeRef
373 getLockedFlakeRef() const
374 {
375 return nix::FlakeRef::fromAttrs(
376 nix::fetchers::jsonToAttrs( this->lockedRef.attrs ) );
377 }
378
379
380}; /* End class `PkgDbReadOnly' */
381
382
383/* -------------------------------------------------------------------------- */
384
389template<typename T>
390concept pkgdb_typename = std::is_base_of<PkgDbReadOnly, T>::value;
391
392
393/* -------------------------------------------------------------------------- */
394
400bool
401isSQLError( int rcode );
402
403
404/* -------------------------------------------------------------------------- */
405
406} // namespace flox::pkgdb
407
408
409/* -------------------------------------------------------------------------- *
410 *
411 *
412 *
413 * ========================================================================== */
A generic exception thrown by flox::pkgdb::* classes.
A SQLite3 database used to cache derivation/package information about a single locked flake.
Definition: read.hh:135
Fingerprint fingerprint
Definition: read.hh:141
PkgDbReadOnly(const Fingerprint &fingerprint, std::string_view dbPath)
Opens a DB directly by its fingerprint hash.
Definition: read.hh:224
std::filesystem::path dbPath
Definition: read.hh:142
PkgDbReadOnly(std::string_view dbPath)
Opens an existing database.
Definition: read.hh:210
PkgDbReadOnly()
Dummy constructor required for child classes so that they can open databases in read-only mode.
Definition: read.hh:199
SQLiteDb db
Definition: read.hh:143
PkgDbReadOnly(const Fingerprint &fingerprint)
Opens a DB directly by its fingerprint hash.
Definition: read.hh:236
Restricts template parameters to classes that extend flox::pkgdb::PkgDbReadOnly.
Definition: read.hh:390
Executable command helpers, argument parsers, etc.
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
std::filesystem::path genPkgDbName(const Fingerprint &fingerprint, const std::filesystem::path &cacheDir)
Get an absolute path to the ‘PkgDb’ for a given fingerprint hash.
Definition: read.cc:76
std::filesystem::path getPkgDbCachedir()
Get the default pkgdb cache directory to save databases.
Definition: read.cc:53
nix::flake::Fingerprint Fingerprint
Definition: read.hh:93
uint64_t row_id
Definition: pkg-query.hh:48
int sql_rc
Definition: read.hh:95
bool isSQLError(int rcode)
Predicate to detect failing SQLite3 return codes.
Definition: read.cc:29
std::ostream & operator<<(std::ostream &oss, const SqlVersions &versions)
Emit version information to an output stream.
Definition: read.cc:44
constexpr SqlVersions sqlVersions
Definition: read.hh:87
@ EC_PKG_DB
Definition: exceptions.hh:63
std::vector< std::string > AttrPath
A list of key names addressing a location in a nested JSON-like object.
Definition: types.hh:33
Interfaces for analyzing version numbers.
Definition: versions.cc:31
Abstract representation of a package.
Interfaces for constructing complex ‘Packages’ queries.
Locked flake reference for database's flake.
Definition: read.hh:147
std::string string
Definition: read.hh:148
Thrown when a database is not found.
Definition: read.hh:161
SQLite3 schema versions.
Definition: read.hh:47
constexpr bool operator!=(const SqlVersions &other) const
Definition: read.hh:71
unsigned tables
Definition: read.hh:53
unsigned views
Definition: read.hh:60
constexpr bool operator==(const SqlVersions &other) const
Definition: read.hh:64
friend std::ostream & operator<<(std::ostream &oss, const SqlVersions &versions)
Emit version information to an output stream.
Definition: read.cc:44
Miscellaneous typedefs and aliases.