Aggregate Types and Object-Relational Mapping
The potential custom types I have discussed so far all map directly to fundamental database types. For example, no matter what your preferred String class is, you would add support for it by defining a TypeConversion<> specialization using the base_type std::string. SOCI in turn uses it to read/write to a single character-based column in the database.
In fact, you can also provide a specialization of the TypeConversion traits class to support custom aggregate typesSOCI provides a class called Values specifically to facilitate this. The Values class acts as an intermediary between your custom aggregate class and the database. It provides get() and set() methods that allow you to define how your class maps to columns in the database. Listing Nine is a mapping of a Person struct to its database columns.
#include "listing6.h" #include <soci.h> #include <boost/date_time/gregorian/gregorian.hpp> using boost::gregorian::date; struct Person { int id; std::string name; date birthday; }; namespace SOCI { template<> struct TypeConversion<Person> { typedef Values base_type; static Person from(Values const &v) { Person p; p.id = v.get<int>("ID"); p.name = v.get<std::string>("NAME"); p.birthday = v.get<date>("BIRTHDAY"); return p; } static Values to(Person &p) { Values v; v.set("ID", p.id); v.set("NAME", p.name); v.set("BIRTHDAY", p.birthday); return v; } }; };
TypeConversion-based mappings to the Values class are simple to define, yet they provide a powerful and flexible means of abstraction. In fact, they provide a way to implement object-relational mapping, which is an area of much interest in the world of database programming using object-oriented languages (see Martin Fowler's Patterns of Enterprise Application Architecture; Addison-Wesley, 2002).
Listing Ten is a simple database access class that makes use of the TypeConversion<Person> specialization. The PersonDBStorage class provides methods GetPersonByName(), AddPerson(), and UpdatePerson(). As you can see, its implementation is simplified by the use of the TypeConversion-based object-relational mapping. If your class has more than a few data members, the simplicity gain from using such a mapping will be more pronounced.
#include "listing6.h" #include "listing9.h" #include <soci.h> #include <boost/date_time/gregorian/gregorian.hpp> using namespace SOCI; using boost::gregorian::date; class PersonDBStorage { public: PersonDBStorage(SOCI::Session& sql) : mSql(sql) {} virtual Person getPersonByName(std::string& name) { Person p; mSql << "select * from Person where name = :NAME", use(name), into(p); return p; } virtual void addPerson(Person& p) { mSql << "insert into Person values(:ID, :NAME, :BIRTHDAY)", use(p); } virtual void updatePerson(Person& p) { mSql << "update Person set name = :NAME, " "birthday = :BIRTHDAY where id = :ID", use(p); } private: Session& mSql; }; int main() { try { Session sql("oracle", "service=gen1 user=scott" " password=tiger"); sql << "create table person(id number(5), name varchar2(20)," " birthday date)"; PersonDBStorage storage(sql); Person p; p.id = 100; p.name = "Bjarne"; p.birthday = date(2001, boost::gregorian::Jan, 1); storage.addPerson(p); Person p2 = storage.getPersonByName(p.name); assert(p2.birthday == p.birthday); std::cout << p2.name << "'s birthday is " << p2.birthday <<std::endl; sql << "drop table person"; } catch(std::exception& e) { std::cout << e.what() << std::endl; } }
Conclusion
Supporting user customization via TypeConversion<T> is viable and potentially beneficial for any library that is driven by template specialization. The benefit is that it gives library users a succinct and noninvasive mechanism for making the library work transparently with their preferred user-defined types.
If you plan to implement TypeConversion support within your library, one technique to consider is to utilize a partial class template specialization for type TypeConversion<T>::base_type, which derives from the class template specialization for type T.
Acknowledgments
Thanks to Maciej Sobczak, founder of the SOCI project, for suggesting the use of the BaseValueHolder class in Listing Five.