Building query dynamically with fmt or std::format

58 Views Asked by At

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 ");
1

There are 1 best solutions below

0
On BEST ANSWER

Maybe something like this can

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