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
This commit is contained in:
Jade Lovelace 2024-08-04 20:20:59 -07:00
parent a318c96851
commit 4ed8461cac
3 changed files with 28 additions and 15 deletions

View file

@ -11,7 +11,6 @@
#include "finally.hh" #include "finally.hh"
#include "compression.hh" #include "compression.hh"
#include <iostream>
#include <algorithm> #include <algorithm>
#include <cstring> #include <cstring>
@ -539,9 +538,10 @@ void LocalStore::openDB(State & state, bool create)
{ {
SQLiteStmt stmt; SQLiteStmt stmt;
stmt.create(db, "pragma main.journal_mode;"); 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"); SQLiteError::throw_(db, "querying journal mode");
prevMode = std::string((const char *) sqlite3_column_text(stmt, 0)); prevMode = use.getStr(0);
} }
if (prevMode != mode && if (prevMode != mode &&
sqlite3_exec(db, ("pragma main.journal_mode = " + mode + ";").c_str(), 0, 0, 0) != SQLITE_OK) sqlite3_exec(db, ("pragma main.journal_mode = " + mode + ";").c_str(), 0, 0, 0) != SQLITE_OK)
@ -916,19 +916,22 @@ std::shared_ptr<const ValidPathInfo> LocalStore::queryPathInfoInternal(State & s
info->registrationTime = useQueryPathInfo.getInt(2); info->registrationTime = useQueryPathInfo.getInt(2);
auto s = (const char *) sqlite3_column_text(state.stmts->QueryPathInfo, 3); if (auto deriver = useQueryPathInfo.getStrNullable(3); deriver.has_value()) {
if (s) info->deriver = parseStorePath(s); info->deriver = parseStorePath(*deriver);
}
/* Note that narSize = NULL yields 0. */ /* Note that narSize = NULL yields 0. */
info->narSize = useQueryPathInfo.getInt(4); info->narSize = useQueryPathInfo.getInt(4);
info->ultimate = useQueryPathInfo.getInt(5) == 1; info->ultimate = useQueryPathInfo.getInt(5) == 1;
s = (const char *) sqlite3_column_text(state.stmts->QueryPathInfo, 6); if (auto sigs = useQueryPathInfo.getStrNullable(6); sigs.has_value()) {
if (s) info->sigs = tokenizeString<StringSet>(s, " "); info->sigs = tokenizeString<StringSet>(*sigs, " ");
}
s = (const char *) sqlite3_column_text(state.stmts->QueryPathInfo, 7); if (auto ca = useQueryPathInfo.getStrNullable(7); ca.has_value()) {
if (s) info->ca = ContentAddress::parseOpt(s); info->ca = ContentAddress::parseOpt(*ca);
}
/* Get the references. */ /* Get the references. */
auto useQueryReferences(state.stmts->QueryReferences.use()(info->id)); auto useQueryReferences(state.stmts->QueryReferences.use()(info->id));
@ -1063,9 +1066,9 @@ std::optional<StorePath> LocalStore::queryPathFromHashPart(const std::string & h
if (!useQueryPathFromHashPart.next()) return {}; if (!useQueryPathFromHashPart.next()) return {};
const char * s = (const char *) sqlite3_column_text(state->stmts->QueryPathFromHashPart, 0); auto s = useQueryPathFromHashPart.getStrNullable(0);
if (s && prefix.compare(0, prefix.size(), s, prefix.size()) == 0) if (s.has_value() && s->starts_with(prefix))
return parseStorePath(s); return parseStorePath(*s);
return {}; return {};
}); });
} }

View file

@ -199,11 +199,20 @@ bool SQLiteStmt::Use::next()
return r == SQLITE_ROW; return r == SQLITE_ROW;
} }
std::optional<std::string> SQLiteStmt::Use::getStrNullable(int col)
{
auto s = reinterpret_cast<const char *>(sqlite3_column_text(stmt, col));
return s != nullptr ? std::make_optional<std::string>((s)) : std::nullopt;
}
std::string SQLiteStmt::Use::getStr(int col) std::string SQLiteStmt::Use::getStr(int col)
{ {
auto s = (const char *) sqlite3_column_text(stmt, col); if (auto res = getStrNullable(col); res.has_value()) {
assert(s); return *res;
return s; } 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) int64_t SQLiteStmt::Use::getInt(int col)

View file

@ -107,6 +107,7 @@ struct SQLiteStmt
bool next(); bool next();
std::string getStr(int col); std::string getStr(int col);
std::optional<std::string> getStrNullable(int col);
int64_t getInt(int col); int64_t getInt(int col);
bool isNull(int col); bool isNull(int col);
}; };