Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

best way to implement a sql statement binding in C++

Ok so I am writing a DB wrapper in c++0x, the API is in C.

I have preprepared statements which I can bind at runtime.

I would like to bind and execute the statement in 1 function call to the wrapper.

My initial though is to use variation templates. but with the documentation I have seen I have not found out how to limit the types that are entered as template types to fixed set (int, string, double), and how to be able to do basic logic on those types.

something like (pseudo code)

foreach arg in args
    if arg1==std::string
        bindToString(arg);
    else if int...

thanks

like image 778
111111 Avatar asked Jun 12 '11 20:06

111111


1 Answers

Take advantage of function overloading.

void bind(std::string& arg) { bindstring(arg); }
void bind(int& arg) { bindint(arg); }
...

std::vector<boost::variant<double,std::string,int>> args =  {...}
for (auto arg : args)
    bind(arg);

-- edit --
Another approach using variadic template

void bind(std::string& arg) { bindstring(arg); }
void bind(int& arg) { bindint(arg); }
void bind(void) {}

template <typename T, typename... Args>
void bind(T& arg, Args& args)
{
  bind(arg);  bind(args...);
}
like image 123
log0 Avatar answered Sep 30 '22 17:09

log0