C++的ORM框架,ODB 2.1.0 發布
ODB 是一個開源的,支持多平臺,支持多數據庫的 C++ 的 ORM 框架,可將 C++ 對象數據庫表映射,進行輕松的數據庫查詢和操作。
使用ODB進行對象持久化具有以下優點:
Ease of use. ODB automatically generates database conversion
code from your C++ classes and allows you to manipulate persistent
objects using a simple, object-oriented database API.
Concise code. With ODB hiding the details of the underlying
database, the application logic is written using the natural object
vocabulary making it simpler and thus easier to read and understand.
Safety. The ODB object persistence and query APIs are
statically typed. You use C++ identifiers instead of strings
to refer to object members and the generated code makes sure
database and C++ types are compatible. All this helps catch
programming errors at compile-time rather than at runtime.
Database portability. Because the database conversion code
is automatically generated, it is easy to switch from one database
vendor to another.
Optimal performance. ODB has been designed for high performance
and low memory overhead. All the available optimization techniques,
such as prepared statements and extensive connection, statement,
and buffer caching, are used to provide the most efficient
implementation for each database operation. Persistent classes
have zero per-object memory overhead. There are no hidden "database"
members that each class must have nor are there per-object data
structures allocated by ODB.
Maintainability. Automatic code generation minimizes the
effort needed to adapt the application to changes in persistent
classes. The database conversion code is kept separately from
the class declarations and application logic. This makes the
application easier to debug and maintain.
ODB 2.1.0 發布,該版本包含很多新特性,包括:
- 支持通過訪問符、修飾符、函數和表達式來訪問數據成員
- 支持虛擬數據成員
- 可在數據成員上直接定義數據庫索引
- 支持映射擴展數據庫類型,例如空間數據、用戶自定義類型和集合
- 更新了 profile 庫
- 通過 Boost profile 庫提供對 Uuid 和多索引容器哭的持久化支持
- 支持 Visual Studio 2012 和 Clang 3.1
示例代碼:
odb::sqlite::database db ("people.db");
person john ("john@doe.org", "John Doe", 31);
person jane ("jane@doe.org", "Jane Doe", 29);
odb::transaction t (db.begin ());
db.persist (john);
db.persist (jane);
typedef odb::query<person> person_query;
for (person& p: db.query<person> (person_query::age < 30));
cerr << p << endl;
jane.age (jane.age () + 1);
db.update (jane);
t.commit ();