libstore: create statement object from SQLite instance

it's just cleaner this way, same as with transactions.

Change-Id: Ibe8fe0bb3d5846f7b7b51e3454bd2fde44d4a5bb
This commit is contained in:
eldritch horrors
2025-01-20 23:07:44 +01:00
parent 57f5a61ab4
commit d5dc8361f5
7 changed files with 46 additions and 41 deletions
+4 -4
View File
@@ -47,16 +47,16 @@ struct AttrDb
state->db.isCache();
state->db.exec(schema);
state->insertAttribute.create(state->db,
state->insertAttribute = state->db.create(
"insert or replace into Attributes(parent, name, type, value) values (?, ?, ?, ?)");
state->insertAttributeWithContext.create(state->db,
state->insertAttributeWithContext = state->db.create(
"insert or replace into Attributes(parent, name, type, value, context) values (?, ?, ?, ?, ?)");
state->queryAttribute.create(state->db,
state->queryAttribute = state->db.create(
"select rowid, type, value, context from Attributes where parent = ? and name = ?");
state->queryAttributes.create(state->db,
state->queryAttributes = state->db.create(
"select name from Attributes where parent = ?");
state->txn = std::make_unique<SQLiteTxn>(state->db.beginTransaction());
+2 -2
View File
@@ -50,10 +50,10 @@ struct CacheImpl : Cache
state->db.isCache();
state->db.exec(schema);
state->add.create(state->db,
state->add = state->db.create(
"insert or replace into Cache(input, info, path, immutable, timestamp) values (?, ?, ?, ?, ?)");
state->lookup.create(state->db,
state->lookup = state->db.create(
"select info, path, immutable, timestamp from Cache where input = ?");
}
+19 -20
View File
@@ -365,39 +365,39 @@ LocalStore::LocalStore(LocalStoreConfig config)
}
/* Prepare SQL statements. */
state->stmts->RegisterValidPath.create(state->db,
state->stmts->RegisterValidPath = state->db.create(
"insert into ValidPaths (path, hash, registrationTime, deriver, narSize, ultimate, sigs, ca) values (?, ?, ?, ?, ?, ?, ?, ?);");
state->stmts->UpdatePathInfo.create(state->db,
state->stmts->UpdatePathInfo = state->db.create(
"update ValidPaths set narSize = ?, hash = ?, ultimate = ?, sigs = ?, ca = ? where path = ?;");
state->stmts->AddReference.create(state->db,
state->stmts->AddReference = state->db.create(
"insert or replace into Refs (referrer, reference) values (?, ?);");
state->stmts->QueryPathInfo.create(state->db,
state->stmts->QueryPathInfo = state->db.create(
"select id, hash, registrationTime, deriver, narSize, ultimate, sigs, ca from ValidPaths where path = ?;");
state->stmts->QueryReferences.create(state->db,
state->stmts->QueryReferences = state->db.create(
"select path from Refs join ValidPaths on reference = id where referrer = ?;");
state->stmts->QueryReferrers.create(state->db,
state->stmts->QueryReferrers = state->db.create(
"select path from Refs join ValidPaths on referrer = id where reference = (select id from ValidPaths where path = ?);");
state->stmts->InvalidatePath.create(state->db,
state->stmts->InvalidatePath = state->db.create(
"delete from ValidPaths where path = ?;");
state->stmts->AddDerivationOutput.create(state->db,
state->stmts->AddDerivationOutput = state->db.create(
"insert or replace into DerivationOutputs (drv, id, path) values (?, ?, ?);");
state->stmts->QueryValidDerivers.create(state->db,
state->stmts->QueryValidDerivers = state->db.create(
"select v.id, v.path from DerivationOutputs d join ValidPaths v on d.drv = v.id where d.path = ?;");
state->stmts->QueryDerivationOutputs.create(state->db,
state->stmts->QueryDerivationOutputs = state->db.create(
"select id, path from DerivationOutputs where drv = ?;");
// Use "path >= ?" with limit 1 rather than "path like '?%'" to
// ensure efficient lookup.
state->stmts->QueryPathFromHashPart.create(state->db,
state->stmts->QueryPathFromHashPart = state->db.create(
"select path from ValidPaths where path >= ? limit 1;");
state->stmts->QueryValidPaths.create(state->db, "select path from ValidPaths");
state->stmts->QueryValidPaths = state->db.create("select path from ValidPaths");
if (experimentalFeatureSettings.isEnabled(Xp::CaDerivations)) {
state->stmts->RegisterRealisedOutput.create(state->db,
state->stmts->RegisterRealisedOutput = state->db.create(
R"(
insert into Realisations (drvPath, outputName, outputPath, signatures)
values (?, ?, (select id from ValidPaths where path = ?), ?)
;
)");
state->stmts->UpdateRealisedOutput.create(state->db,
state->stmts->UpdateRealisedOutput = state->db.create(
R"(
update Realisations
set signatures = ?
@@ -406,27 +406,27 @@ LocalStore::LocalStore(LocalStoreConfig config)
outputName = ?
;
)");
state->stmts->QueryRealisedOutput.create(state->db,
state->stmts->QueryRealisedOutput = state->db.create(
R"(
select Realisations.id, Output.path, Realisations.signatures from Realisations
inner join ValidPaths as Output on Output.id = Realisations.outputPath
where drvPath = ? and outputName = ?
;
)");
state->stmts->QueryAllRealisedOutputs.create(state->db,
state->stmts->QueryAllRealisedOutputs = state->db.create(
R"(
select outputName, Output.path from Realisations
inner join ValidPaths as Output on Output.id = Realisations.outputPath
where drvPath = ?
;
)");
state->stmts->QueryRealisationReferences.create(state->db,
state->stmts->QueryRealisationReferences = state->db.create(
R"(
select drvPath, outputName from Realisations
join RealisationsRefs on realisationReference = Realisations.id
where referrer = ?;
)");
state->stmts->AddRealisationReference.create(state->db,
state->stmts->AddRealisationReference = state->db.create(
R"(
insert or replace into RealisationsRefs (referrer, realisationReference)
values (
@@ -522,8 +522,7 @@ void LocalStore::openDB(DBState & state, bool create)
std::string mode = settings.useSQLiteWAL ? "wal" : "truncate";
std::string prevMode;
{
SQLiteStmt stmt;
stmt.create(db, "pragma main.journal_mode;");
SQLiteStmt stmt = db.create("pragma main.journal_mode;");
auto use = stmt.use();
if (use.step() != SQLITE_ROW)
SQLiteError::throw_(db, "querying journal mode");
+11 -11
View File
@@ -99,35 +99,35 @@ public:
state->db.exec(schema);
state->insertCache.create(state->db,
state->insertCache = state->db.create(
"insert into BinaryCaches(url, timestamp, storeDir, wantMassQuery, priority) values (?1, ?2, ?3, ?4, ?5) on conflict (url) do update set timestamp = ?2, storeDir = ?3, wantMassQuery = ?4, priority = ?5 returning id;");
state->queryCache.create(state->db,
state->queryCache = state->db.create(
"select id, storeDir, wantMassQuery, priority from BinaryCaches where url = ? and timestamp > ?");
state->insertNAR.create(state->db,
state->insertNAR = state->db.create(
"insert or replace into NARs(cache, hashPart, namePart, url, compression, fileHash, fileSize, narHash, "
"narSize, refs, deriver, sigs, ca, timestamp, present) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1)");
state->insertMissingNAR.create(state->db,
state->insertMissingNAR = state->db.create(
"insert or replace into NARs(cache, hashPart, timestamp, present) values (?, ?, ?, 0)");
state->queryNAR.create(state->db,
state->queryNAR = state->db.create(
"select present, namePart, url, compression, fileHash, fileSize, narHash, narSize, refs, deriver, sigs, ca from NARs where cache = ? and hashPart = ? and ((present = 0 and timestamp > ?) or (present = 1 and timestamp > ?))");
state->insertRealisation.create(state->db,
state->insertRealisation = state->db.create(
R"(
insert or replace into Realisations(cache, outputId, content, timestamp)
values (?, ?, ?, ?)
)");
state->insertMissingRealisation.create(state->db,
state->insertMissingRealisation = state->db.create(
R"(
insert or replace into Realisations(cache, outputId, timestamp)
values (?, ?, ?)
)");
state->queryRealisation.create(state->db,
state->queryRealisation = state->db.create(
R"(
select content from Realisations
where cache = ? and outputId = ? and
@@ -139,11 +139,11 @@ public:
retrySQLite([&]() {
auto now = time(0);
SQLiteStmt queryLastPurge(state->db, "select value from LastPurge");
SQLiteStmt queryLastPurge = state->db.create("select value from LastPurge");
auto queryLastPurge_(queryLastPurge.use());
if (!queryLastPurge_.next() || queryLastPurge_.getInt(0) < now - purgeInterval) {
SQLiteStmt(state->db,
state->db.create(
"delete from NARs where ((present = 0 and timestamp < ?) or (present = 1 and timestamp < ?))")
.use()
// Use a minimum TTL to prevent --refresh from
@@ -154,7 +154,7 @@ public:
debug("deleted %d entries from the NAR info disk cache", state->db.getRowsChanged());
SQLiteStmt(state->db,
state->db.create(
"insert or replace into LastPurge(dummy, value) values ('', ?)")
.use()(now).exec();
}
+6 -1
View File
@@ -104,6 +104,11 @@ void SQLite::exec(const std::string & stmt)
});
}
SQLiteStmt SQLite::create(const std::string & stmt)
{
return SQLiteStmt(db, stmt);
}
SQLiteTxn SQLite::beginTransaction()
{
return SQLiteTxn(db);
@@ -127,7 +132,7 @@ uint64_t SQLite::getRowsChanged()
return sqlite3_changes64(db);
}
void SQLiteStmt::create(sqlite3 * db, const std::string & sql)
SQLiteStmt::SQLiteStmt(sqlite3 * db, const std::string & sql)
{
checkInterrupt();
sqlite3_stmt * stmt;
+3 -2
View File
@@ -56,6 +56,8 @@ struct SQLite
void exec(const std::string & stmt);
SQLiteStmt create(const std::string & stmt);
SQLiteTxn beginTransaction();
void setPersistWAL(bool persist);
@@ -80,8 +82,7 @@ class SQLiteStmt
public:
SQLiteStmt() = default;
SQLiteStmt(sqlite3 * db, const std::string & sql) { create(db, sql); }
void create(sqlite3 * db, const std::string & s);
SQLiteStmt(sqlite3 * db, const std::string & sql);
/**
* Helper for binding / executing statements.
+1 -1
View File
@@ -49,7 +49,7 @@ TEST(NarInfoDiskCacheImpl, create_and_read) {
// We're going to pay special attention to the id field because we had a bug
// that changed it.
db = SQLite(dbPath);
getIds.create(db, "select id from BinaryCaches where url = 'http://foo'");
getIds = db.create("select id from BinaryCaches where url = 'http://foo'");
{
auto q(getIds.use());