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:
parent
a318c96851
commit
4ed8461cac
3 changed files with 28 additions and 15 deletions
|
@ -11,7 +11,6 @@
|
|||
#include "finally.hh"
|
||||
#include "compression.hh"
|
||||
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
|
||||
|
@ -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<const ValidPathInfo> 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<StringSet>(s, " ");
|
||||
if (auto sigs = useQueryPathInfo.getStrNullable(6); sigs.has_value()) {
|
||||
info->sigs = tokenizeString<StringSet>(*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<StorePath> 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 {};
|
||||
});
|
||||
}
|
||||
|
|
|
@ -199,11 +199,20 @@ bool SQLiteStmt::Use::next()
|
|||
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)
|
||||
{
|
||||
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)
|
||||
|
|
|
@ -107,6 +107,7 @@ struct SQLiteStmt
|
|||
bool next();
|
||||
|
||||
std::string getStr(int col);
|
||||
std::optional<std::string> getStrNullable(int col);
|
||||
int64_t getInt(int col);
|
||||
bool isNull(int col);
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue