Best way to implement sql statement binding in C ++

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

I have prepared instructions that I can link at runtime.

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

First, I want to use variation patterns. but with the documentation that I saw, I did not learn how to limit the types that are entered as template types into a fixed set (int, string, double), and how to be able to perform basic logic for these types.

something like (pseudocode)

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

thanks

+6
source share
1 answer

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

- change -
Another approach using a variation pattern

 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...); } 
+5
source

Source: https://habr.com/ru/post/890360/


All Articles