I am trying to code a tiny C++ Sql ORM based on the Data Mapper design pattern. I m playing with template and metaprogramming to get introspection features. I can't do it after several tries..
From the mapper, I would like to map column names with getter method from the object I want to store in .
Let see an example :
class User
{
public :
int age() {
cout<<"int\n";
return 5;
}
float size(){
cout<<"float\n";
return 4.2;
}
};
I would like a Mapper class which make me possible to bind column with method:
Mapper<User> mapper;
mapper.bind("age", &User::age);
mapper.bind("size", &User::size);
So I can write on my database like this :
Mapper::insert(User * user)
{
//bindNames = {"size", "age"}
for (const auto& field : bindsNames)
{
// Generate
Query q("INSERT into users (name, age) VALUES(:name, :age)");
q.repace(":size", (user->*readSizeFct)();
q.repace(":age", (user->*readAgeFct)();
}
}
After several tries, the main problem is I cannot store a list of function pointer dynamically with different signature...
Do you have any suggestion to make my tiny ORM working ? I guess I have to read documentation about template metaprogramming ...