Flox Package Database 1
CRUD Operations on Nix Package Metadata
Loading...
Searching...
No Matches
write.hh
Go to the documentation of this file.
1/* ========================================================================== *
2 *
3 * @file flox/pkgdb/write.hh
4 *
5 * @brief Interfaces for writing to a SQLite3 package set database.
6 *
7 *
8 * -------------------------------------------------------------------------- */
9
10#pragma once
11
12#include <tuple>
13
14#include "flox/pkgdb/read.hh"
15
16
17/* -------------------------------------------------------------------------- */
18
19namespace flox::pkgdb {
20
21/* -------------------------------------------------------------------------- */
22
24using Target = std::tuple<flox::AttrPath, flox::Cursor, row_id>;
25
27using Todos = std::queue<Target, std::list<Target>>;
28
29
30/* -------------------------------------------------------------------------- */
31
36class PkgDb : public PkgDbReadOnly
37{
38
39 /* --------------------------------------------------------------------------
40 */
41
42 /* Internal Helpers */
43
44protected:
45
47 void
48 initTables();
49
50
52 void
53 initViews();
54
61 void
63
64
66 void
68
69
76 void
77 init();
78
79
84 void
85 writeInput();
86
87
88 /* --------------------------------------------------------------------------
89 */
90
91 /* Constructors */
92
93public:
94
101 explicit PkgDb( std::string_view dbPath )
102 {
103 this->dbPath = dbPath;
104 if ( ! std::filesystem::exists( this->dbPath ) )
105 {
107 *dynamic_cast<PkgDbReadOnly *>( this ) );
108 }
109 this->db.connect( this->dbPath.c_str(),
110 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE );
111 this->init();
112 this->loadLockedFlake();
113 }
114
122 PkgDb( const Fingerprint & fingerprint, std::string_view dbPath )
123 {
124 this->dbPath = dbPath;
125 this->fingerprint = fingerprint;
126 if ( ! std::filesystem::exists( this->dbPath ) )
127 {
129 *dynamic_cast<PkgDbReadOnly *>( this ) );
130 }
131 this->db.connect( this->dbPath.c_str(),
132 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE );
133 this->init();
134 this->loadLockedFlake();
135 }
136
143 explicit PkgDb( const Fingerprint & fingerprint )
144 : PkgDb( fingerprint, genPkgDbName( fingerprint ).string() )
145 {}
146
154 PkgDb( const nix::flake::LockedFlake & flake, std::string_view dbPath )
155 {
156 this->dbPath = dbPath;
157 this->fingerprint = flake.getFingerprint();
158 this->db.connect( this->dbPath.c_str(),
159 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE );
160 init();
161 this->lockedRef
162 = { flake.flake.lockedRef.to_string(),
163 nix::fetchers::attrsToJSON( flake.flake.lockedRef.toAttrs() ) };
164 writeInput();
165 }
166
173 explicit PkgDb( const nix::flake::LockedFlake & flake )
174 : PkgDb( flake, genPkgDbName( flake.getFingerprint() ).string() )
175 {}
176
177
178 /* --------------------------------------------------------------------------
179 */
180
181 /* Basic Operations */
182
183 // public:
184
190 inline sql_rc
191 execute( const char * stmt )
192 {
193 sqlite3pp::command cmd( this->db, stmt );
194 return cmd.execute();
195 }
196
202 inline sql_rc
203 execute_all( const char * stmt )
204 {
205 sqlite3pp::command cmd( this->db, stmt );
206 return cmd.execute_all();
207 }
208
209
210 /* --------------------------------------------------------------------------
211 */
212
213 /* Insert */
214
215 // public:
216
228 row_id
229 addOrGetAttrSetId( const std::string & attrName, row_id parent = 0 );
230
238 row_id
239 addOrGetAttrSetId( const flox::AttrPath & path );
240
248 row_id
249 addOrGetDescriptionId( const std::string & description );
250
263 row_id
264 addPackage( row_id parentId,
265 std::string_view attrName,
266 const flox::Cursor & cursor,
267 bool replace = false,
268 bool checkDrv = true );
269
270
271 /* --------------------------------------------------------------------------
272 */
273
274 /* Updates */
275
282 void
283 setPrefixDone( row_id prefixId, bool done );
284
291 void
292 setPrefixDone( const flox::AttrPath & prefix, bool done );
293
294
295 /* --------------------------------------------------------------------------
296 */
297
309 void
310 scrape( nix::SymbolTable & syms, const Target & target, Todos & todo );
311
312
313 /* --------------------------------------------------------------------------
314 */
315
316}; /* End class `PkgDb' */
317
318
319/* -------------------------------------------------------------------------- */
320
321} // namespace flox::pkgdb
322
323
324/* -------------------------------------------------------------------------- *
325 *
326 *
327 *
328 * ========================================================================== */
A SQLite3 database used to cache derivation/package information about a single locked flake.
Definition: read.hh:135
Fingerprint fingerprint
Definition: read.hh:141
void loadLockedFlake()
Definition: read.cc:101
std::filesystem::path dbPath
Definition: read.hh:142
struct LockedFlakeRef lockedRef
Definition: read.hh:152
SQLiteDb db
Definition: read.hh:143
A SQLite3 database used to cache derivation/package information about a single locked flake.
Definition: write.hh:37
row_id addPackage(row_id parentId, std::string_view attrName, const flox::Cursor &cursor, bool replace=false, bool checkDrv=true)
Adds a package to the database.
Definition: write.cc:267
row_id addOrGetDescriptionId(const std::string &description)
Get the Descriptions.id for a given string if it exists, or insert a new row for description and retu...
Definition: write.cc:226
PkgDb(const nix::flake::LockedFlake &flake, std::string_view dbPath)
Opens a DB associated with a locked flake.
Definition: write.hh:154
row_id addOrGetAttrSetId(const std::string &attrName, row_id parent=0)
Get the AttrSet.id for a given child of the attribute set associated with parent if it exists,...
Definition: write.cc:182
void scrape(nix::SymbolTable &syms, const Target &target, Todos &todo)
Scrape package definitions from an attribute set.
Definition: write.cc:406
PkgDb(const Fingerprint &fingerprint)
Opens a DB directly by its fingerprint hash.
Definition: write.hh:143
PkgDb(std::string_view dbPath)
Opens an existing database.
Definition: write.hh:101
void initVersions()
Create DbVersions rows if they do not exist.
Definition: write.cc:122
void writeInput()
Write this PkgDb lockedRef and fingerprint fields to database metadata.
Definition: write.cc:159
void setPrefixDone(row_id prefixId, bool done)
Update the done column for an attribute set and all of its children recursively.
Definition: write.cc:365
void initViews()
Create views in database if they do not exist.
Definition: write.cc:26
PkgDb(const nix::flake::LockedFlake &flake)
Opens a DB associated with a locked flake.
Definition: write.hh:173
void init()
Create/update tables/views schema in database. Create tables if they do not exist....
Definition: write.cc:144
PkgDb(const Fingerprint &fingerprint, std::string_view dbPath)
Opens a DB directly by its fingerprint hash.
Definition: write.hh:122
sql_rc execute_all(const char *stmt)
Execute raw sqlite statements on the database.
Definition: write.hh:203
void initTables()
Create tables in database if they do not exist.
Definition: write.cc:83
sql_rc execute(const char *stmt)
Execute a raw sqlite statement on the database.
Definition: write.hh:191
void updateViews()
Update the database's VIEWs schemas.
Definition: write.cc:40
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::tuple< flox::AttrPath, flox::Cursor, row_id > Target
A set of arguments used by flox::pkgdb::PkgDb::scrape.
Definition: write.hh:24
nix::flake::Fingerprint Fingerprint
Definition: read.hh:93
uint64_t row_id
Definition: pkg-query.hh:48
int sql_rc
Definition: read.hh:95
std::queue< Target, std::list< Target > > Todos
A queue of flox::pkgdb::Target to be completed.
Definition: write.hh:27
nix::ref< nix::eval_cache::AttrCursor > Cursor
A non-nullptr std::shared_ptr<nix::eval_cache::AttrCursor>.
Definition: types.hh:50
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 reading a SQLite3 package set database.
Thrown when a database is not found.
Definition: read.hh:161