Skip to content

Commit

Permalink
Merge pull request #6511 from LandSandBoat/percent_sign
Browse files Browse the repository at this point in the history
db: Properly escape % in blobs, more error handling in query()
  • Loading branch information
zach2good authored Dec 11, 2024
2 parents 32cd0a9 + 0adbadb commit 667027d
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/common/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,21 @@ namespace db
// @return A unique pointer to the result set of the query.
// @note Everything in database-land is 1-indexed, not 0-indexed.
template <typename... Args>
auto query(std::string const& query, Args&&... args)
auto query(std::string const& query, Args&&... args) -> std::unique_ptr<db::detail::ResultSetWrapper>
{
return queryStr(fmt::sprintf(query, std::forward<Args>(args)...));
TracyZoneScoped;
try
{
const auto formattedQuery = fmt::sprintf(query, std::forward<Args>(args)...);
return queryStr(formattedQuery);
}
catch (const std::exception& e)
{
ShowError("Query Failed: %s", e.what());
ShowError("Query Failed: %s", str(query.c_str()));
}

return nullptr;
}

// @brief Execute a prepared statement with the given query string and arguments.
Expand Down Expand Up @@ -528,6 +540,9 @@ namespace db
case '\\': // Backslash
result += "\\\\";
break;
case '%': // Percent (reserved by sprintf, etc.)
result += "%%";
break;
default:
result += ch;
break;
Expand Down

0 comments on commit 667027d

Please sign in to comment.