Search code examples
c++c++20fmtstdformat

Building query dynamically with fmt or std::format


I am trying to build a SQL query whose parameter list is dynamic in a more modern way than with a stringstream.

What I want to obtain is

WHERE param=@p0 OR param=@p1 OR ...

I tried this

std::vector<int> ids(input.size());
std::iota(ids.begin(), ids.end(), 0);

// Results in 0param=@p{} OR 1
std::string clause = fmt::format("{}", fmt::join(ids, "param=@p{} OR ");

// Results in param=@p0 OR 1
std::string clause = fmt::format("param=@p{}", fmt::join(ids, " OR ");

Solution

  • Maybe something like this can

    std::string clause = fmt::format("param=@p{}", fmt::join(ids, " OR param=@p"));