ODB (C++)
ODB is an object-relational mapping (ORM) system for the C++ language. It allows an application developer to persist C++ objects to a relational database without having to deal with tables, columns, or SQL and without manually writing any mapping code. ODB is free software and is dual-licensed under the GPL and a proprietary license.
One notable difference between ODB and other ORM implementations for C++ is its automatic generation of the database mapping code and, optionally, the database schema from the C++ headers that declare the classes. This task is performed by the ODB compiler. The ODB compiler is a real C++ compiler except that instead of producing assembly or machine code, it generates portable C++ which can in turn be compiled by any C++ compiler. The ODB compiler uses the GCC compiler front-end for C++ parsing and is implemented using the new GCC plugin architecture.
The other components of the ODB system include the common runtime library (libodb) and the database-specific runtime libraries (for example, libodb-mysql). The common runtime library defines object-oriented database interfaces that are used by the application developer to perform various database operations on persistent objects. The database-specific runtimes implement these interfaces for concrete database systems and provide support functionality for the generated database mapping code.
ODB is not a framework. There is no common base type that all persistent classes should derive from nor are there any restrictions on the data member types in persistent classes. Existing classes can be made persistent with a few or no modifications. ODB is also flexible in the level of insulation it provides to the application developer. It can either completely hide the relational nature of the underlying database or expose some of the details as required.
Mapping
The mapping of C++ classes to database tables is accomplished through a set of custom #pragma directives that are placed in the C++ header file. They allow the application developer to control various aspects of the mapping such as table and column names, C++ types to SQL types mapping, etc. The following example illustrates a typical persistent class declaration:
#pragma db object table("people")
class person
{
...
private:
friend class odb::access;
person ();
#pragma db id auto
unsigned long id_;
string first_;
string last_;
#pragma type("INT UNSIGNED")
unsigned short age_;
};The ODB-specific pragmas can also be placed outside of the class declaration.
Persistence
ODB provides an object-oriented database API that allows the application developer to perform various operations on persistent objects. The following code fragment illustrates the use of the most common operations:
unsigned long joe_id = 1;
person john ("John", "Doe", 31);
person jane ("Jane", "Doe", 29);
transaction t (db.begin ());
// Persist in the database.
//
db.persist (john);
db.persist (jane);
// Load from the database.
//
auto_ptr<person> joe (db.load<person> (joe_id));
// Update in the database.
//
jane.age (jane.age () + 1);
db.update (jane);
// Delete from the database.
//
db.erase (jane);
t.commit ();ODB Query Language
ODB provides an object-oriented query language, called ODB Query Language, which is integrated into C++ allowing the application developer to write expressive and type-safe queries that look and feel like ordinary C++. For example:
transaction t (db.begin ());
result r (db.query<person> (query::last == "Doe" && query::age < 30));
for (result::iterator i (r.begin ()); i != r.end (); ++i)
{
cout << "Hello, " << i->first () << endl;
}
t.commit ();Query parameters can be bound to C++ variables either by value or by reference. If desired, native SQL queries can be executed in which case ODB still provides support for parameter binding.