From 4ed8461cacced97717bf9a7525e12ba69fe168c0 Mon Sep 17 00:00:00 2001 From: Jade Lovelace Date: Sun, 4 Aug 2024 20:20:59 -0700 Subject: [PATCH] sqlite: add a Use::fromStrNullable There were several usages of the raw sqlite primitives along with C style casts, seemingly because nobody thought to use an optional for getting a string or NULL. Let's fix this API given we already *have* a wrapper. Change-Id: I526cceedc2e356209d8fb62e11b3572282c314e8 --- src/libstore/local-store.cc | 27 +++++++++++++++------------ src/libstore/sqlite.cc | 15 ++++++++++++--- src/libstore/sqlite.hh | 1 + 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/libstore/local-store.cc b/src/libstore/local-store.cc index af9c4ace1..4c8e2ea2f 100644 --- a/src/libstore/local-store.cc +++ b/src/libstore/local-store.cc @@ -11,7 +11,6 @@ #include "finally.hh" #include "compression.hh" -#include #include #include @@ -539,9 +538,10 @@ void LocalStore::openDB(State & state, bool create) { SQLiteStmt stmt; stmt.create(db, "pragma main.journal_mode;"); - if (sqlite3_step(stmt) != SQLITE_ROW) + auto use = stmt.use(); + if (use.step() != SQLITE_ROW) SQLiteError::throw_(db, "querying journal mode"); - prevMode = std::string((const char *) sqlite3_column_text(stmt, 0)); + prevMode = use.getStr(0); } if (prevMode != mode && sqlite3_exec(db, ("pragma main.journal_mode = " + mode + ";").c_str(), 0, 0, 0) != SQLITE_OK) @@ -916,19 +916,22 @@ std::shared_ptr LocalStore::queryPathInfoInternal(State & s info->registrationTime = useQueryPathInfo.getInt(2); - auto s = (const char *) sqlite3_column_text(state.stmts->QueryPathInfo, 3); - if (s) info->deriver = parseStorePath(s); + if (auto deriver = useQueryPathInfo.getStrNullable(3); deriver.has_value()) { + info->deriver = parseStorePath(*deriver); + } /* Note that narSize = NULL yields 0. */ info->narSize = useQueryPathInfo.getInt(4); info->ultimate = useQueryPathInfo.getInt(5) == 1; - s = (const char *) sqlite3_column_text(state.stmts->QueryPathInfo, 6); - if (s) info->sigs = tokenizeString(s, " "); + if (auto sigs = useQueryPathInfo.getStrNullable(6); sigs.has_value()) { + info->sigs = tokenizeString(*sigs, " "); + } - s = (const char *) sqlite3_column_text(state.stmts->QueryPathInfo, 7); - if (s) info->ca = ContentAddress::parseOpt(s); + if (auto ca = useQueryPathInfo.getStrNullable(7); ca.has_value()) { + info->ca = ContentAddress::parseOpt(*ca); + } /* Get the references. */ auto useQueryReferences(state.stmts->QueryReferences.use()(info->id)); @@ -1063,9 +1066,9 @@ std::optional LocalStore::queryPathFromHashPart(const std::string & h if (!useQueryPathFromHashPart.next()) return {}; - const char * s = (const char *) sqlite3_column_text(state->stmts->QueryPathFromHashPart, 0); - if (s && prefix.compare(0, prefix.size(), s, prefix.size()) == 0) - return parseStorePath(s); + auto s = useQueryPathFromHashPart.getStrNullable(0); + if (s.has_value() && s->starts_with(prefix)) + return parseStorePath(*s); return {}; }); } diff --git a/src/libstore/sqlite.cc b/src/libstore/sqlite.cc index f40217734..3114aad48 100644 --- a/src/libstore/sqlite.cc +++ b/src/libstore/sqlite.cc @@ -199,11 +199,20 @@ bool SQLiteStmt::Use::next() return r == SQLITE_ROW; } +std::optional SQLiteStmt::Use::getStrNullable(int col) +{ + auto s = reinterpret_cast(sqlite3_column_text(stmt, col)); + return s != nullptr ? std::make_optional((s)) : std::nullopt; +} + std::string SQLiteStmt::Use::getStr(int col) { - auto s = (const char *) sqlite3_column_text(stmt, col); - assert(s); - return s; + if (auto res = getStrNullable(col); res.has_value()) { + return *res; + } else { + // FIXME: turn into fatal non-exception error with actual formatting when we have those + assert(false && "sqlite3 retrieved unexpected null"); + } } int64_t SQLiteStmt::Use::getInt(int col) diff --git a/src/libstore/sqlite.hh b/src/libstore/sqlite.hh index 003e4d101..ca021087f 100644 --- a/src/libstore/sqlite.hh +++ b/src/libstore/sqlite.hh @@ -107,6 +107,7 @@ struct SQLiteStmt bool next(); std::string getStr(int col); + std::optional getStrNullable(int col); int64_t getInt(int col); bool isNull(int col); };