Skip to content

Commit

Permalink
db: improve error reporting on parse error
Browse files Browse the repository at this point in the history
Closes #108
  • Loading branch information
blastrock committed Jul 9, 2018
1 parent e26b46c commit 28a0aa9
Showing 1 changed file with 71 additions and 52 deletions.
123 changes: 71 additions & 52 deletions src/db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,62 +343,81 @@ void TitleDatabase::parse_tsv_file(std::string& db_data)

while (ptr < end && *ptr)
{
const auto fields = pkgi_split_row(&ptr, end);

auto content = get_or_empty(mode, fields, Column::Content);
const auto region = get_or_empty(mode, fields, Column::Region);
const auto name = get_or_empty(mode, fields, Column::Name);
const auto name_org = get_or_empty(mode, fields, Column::NameOrg);
const auto url = get_or_empty(mode, fields, Column::Url);
const auto zrif = get_or_empty(mode, fields, Column::Zrif);
const auto digest = get_or_empty(mode, fields, Column::Digest);
const auto size = get_or_empty(mode, fields, Column::Size);
const auto fw_version = get_or_empty(mode, fields, Column::FwVersion);
const auto last_modification =
get_or_empty(mode, fields, Column::LastModification);

if (*url == '\0' || std::string(url) == "MISSING" ||
std::string(url) == "CART ONLY" || std::string(zrif) == "MISSING")
continue;

if (mode == ModeUpdates)
try
{
std::reverse_iterator<const char*> rbegin(url + strlen(url) - 1);
std::reverse_iterator<const char*> rend(url - 1);
auto const it =
std::find_if(rbegin, rend, [](char c) { return c == '/'; });
if (it != rend)
content = it.base();
const auto fields = pkgi_split_row(&ptr, end);

auto content = get_or_empty(mode, fields, Column::Content);
const auto region = get_or_empty(mode, fields, Column::Region);
const auto name = get_or_empty(mode, fields, Column::Name);
const auto name_org = get_or_empty(mode, fields, Column::NameOrg);
const auto url = get_or_empty(mode, fields, Column::Url);
const auto zrif = get_or_empty(mode, fields, Column::Zrif);
const auto digest = get_or_empty(mode, fields, Column::Digest);
const auto size = get_or_empty(mode, fields, Column::Size);
const auto fw_version =
get_or_empty(mode, fields, Column::FwVersion);
const auto last_modification =
get_or_empty(mode, fields, Column::LastModification);

if (*url == '\0' || std::string(url) == "MISSING" ||
std::string(url) == "CART ONLY" ||
std::string(zrif) == "MISSING")
continue;

if (mode == ModeUpdates)
{
std::reverse_iterator<const char*> rbegin(
url + strlen(url) - 1);
std::reverse_iterator<const char*> rend(url - 1);
auto const it = std::find_if(
rbegin, rend, [](char c) { return c == '/'; });
if (it != rend)
content = it.base();
}

sqlite3_reset(stmt);
sqlite3_bind_text(stmt, 1, content, strlen(content), nullptr);
sqlite3_bind_text(stmt, 2, name, strlen(name), nullptr);
sqlite3_bind_text(stmt, 3, name_org, strlen(name_org), nullptr);
sqlite3_bind_text(stmt, 4, zrif, strlen(zrif), nullptr);
sqlite3_bind_text(stmt, 5, url, strlen(url), nullptr);
std::array<uint8_t, 32> digest_array;
if (std::all_of(digest, digest + 64, [](const auto c) {
return c != 0;
}))
{
digest_array = pkgi_hexbytes(digest, SHA256_DIGEST_SIZE);
sqlite3_bind_blob(
stmt,
6,
digest_array.data(),
digest_array.size(),
nullptr);
}
else
sqlite3_bind_null(stmt, 6);
sqlite3_bind_text(stmt, 7, size, strlen(size), nullptr);
sqlite3_bind_text(stmt, 8, fw_version, strlen(fw_version), nullptr);
sqlite3_bind_text(
stmt,
9,
last_modification,
strlen(last_modification),
nullptr);
sqlite3_bind_text(stmt, 10, region, strlen(region), nullptr);

auto err = sqlite3_step(stmt);
if (err != SQLITE_DONE)
throw std::runtime_error(fmt::format(
"can't execute SQL statement:\n{}",
sqlite3_errmsg(_sqliteDb.get())));
}

sqlite3_reset(stmt);
sqlite3_bind_text(stmt, 1, content, strlen(content), nullptr);
sqlite3_bind_text(stmt, 2, name, strlen(name), nullptr);
sqlite3_bind_text(stmt, 3, name_org, strlen(name_org), nullptr);
sqlite3_bind_text(stmt, 4, zrif, strlen(zrif), nullptr);
sqlite3_bind_text(stmt, 5, url, strlen(url), nullptr);
std::array<uint8_t, 32> digest_array;
if (std::all_of(
digest, digest + 64, [](const auto c) { return c != 0; }))
catch (const std::exception& e)
{
digest_array = pkgi_hexbytes(digest, SHA256_DIGEST_SIZE);
sqlite3_bind_blob(
stmt, 6, digest_array.data(), digest_array.size(), nullptr);
throw formatEx<std::runtime_error>(
"failed to parse line\n{}\n{}", ptr, e.what());
}
else
sqlite3_bind_null(stmt, 6);
sqlite3_bind_text(stmt, 7, size, strlen(size), nullptr);
sqlite3_bind_text(stmt, 8, fw_version, strlen(fw_version), nullptr);
sqlite3_bind_text(
stmt, 9, last_modification, strlen(last_modification), nullptr);
sqlite3_bind_text(stmt, 10, region, strlen(region), nullptr);

auto err = sqlite3_step(stmt);
if (err != SQLITE_DONE)
throw std::runtime_error(fmt::format(
"can't execute SQL statement:\n{}",
sqlite3_errmsg(_sqliteDb.get())));
#undef NEXT_FIELD
}
}

Expand Down

0 comments on commit 28a0aa9

Please sign in to comment.