ODB: C++ Object-Relational Mapping (ORM)

ODB is an open-source, cross-platform, and cross-database object-relational mapping (ORM) system for C++. It allows you 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 supports MySQL, SQLite, PostgreSQL, Oracle, and Microsoft SQL Server relational databases as well as C++98/03 and C++11 language standards. It also comes with optional profiles for Boost and Qt which allow you to seamlessly use value types, containers, and smart pointers from these libraries in your persistent C++ classes.

The following example shows an ordinary C++ class on the left and its persistent version on the right.

  #pragma db object
class person
{
...
private:
friend class odb::access;
person () {} #pragma db id
string email_; string name_;
unsigned short age_;
};
 
class person
{
...
private: string email_; string name_;
unsigned short age_;
};

ODB is not a framework. It does not dictate how you should write your application. Rather, it is designed to fit into your style and architecture by only handling C++ object persistence and not interfering with any other functionality. As you can see, existing classes can be made persistent with only a few modifications. In particular, a persistent class can be declared without the default constructor, existing accessor and modifier functions can be automatically used to access the data members, and ODB pragmas can be moved out of the class and into a separate header, making the object-relational mapping completely non-intrusive. Support for automatic database schema evolution further allows you to treat ODB persistent objects like any other C++ classes in your application.

Given the above declarations, we can perform various database operations with objects of the person class using SQLite as an example:

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

For the complete version of the above code fragment as well as a detailed explanation of each line, refer to the Hello World Example in the ODB Manual.

Using ODB for object persistence has the following advantages:

  • 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 and database schema evolution minimize 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 is highly flexible and customizable. It can either completely hide the relational nature of the underlying database or expose some of the details as required. For example, you can automatically map basic C++ types to suitable SQL types, generate the relational database schema for your persistent classes, and use simple, safe, and yet powerful object query language instead of SQL. Or you can assign SQL types to individual data members, use the existing database schema, and execute native SQL queries. In fact, at an extreme, ODB can be used as just a convenient way to handle results of native SQL queries.

The C++ code that performs the conversion between persistent classes and their database representation is automatically generated by the ODB compiler. The ODB compiler is a real C++ compiler except that it produces C++ instead of assembly or machine code. In particular, it is not an ad-hoc header pre-processor that is only capable of recognizing a subset of C++. ODB is capable of parsing any standard C++ code.

The ODB compiler uses the GCC compiler frontend for C++ parsing and is implemented using the new GCC plugin architecture. While ODB uses GCC internally, its output is standard C++ which means that you can use any C++ compiler to build your application.

 

数据持久层(三)ODB介绍的更多相关文章

  1. .NET开源项目介绍及资源推荐:数据持久层

    在.NET平台下,关于数据持久层框架非常多,本文主要对如下几种做简要的介绍并推荐一些学习的资源: 1.NHibernate 2.NBear 3.Castle ActiveRecord 4.iBATIS ...

  2. c++ 数据持久层研究(一)

    C++ORM框架自动生成代码数据库  用过Java的都知道SSH框架,特别对于数据库开发,Java领域有无数的ORM框架,供数据持久层调用,如Hibernate,iBatis(现在改名叫MyBatis ...

  3. .NET平台下,关于数据持久层框架

    在.NET平台下,关于数据持久层框架非常多,本文主要对如下几种做简要的介绍并推荐一些学习的资源: 1.NHibernate 2.NBear 3.Castle ActiveRecord 4.iBATIS ...

  4. Java数据持久层

    一.前言 1.持久层 Java数据持久层,其本身是为了实现与数据源进行数据交互的存在,其目的是通过分层架构风格,进行应用&数据的解耦. 我从整体角度,依次阐述JDBC.Mybatis.Myba ...

  5. UWP开发之ORM实践:如何使用Entity Framework Core做SQLite数据持久层?

    选择SQLite的理由 在做UWP开发的时候我们首选的本地数据库一般都是Sqlite,我以前也不知道为啥?后来仔细研究了一下也是有原因的: 1,微软做的UWP应用大部分也是用Sqlite.或者说是微软 ...

  6. Restful.Data v1.0 - 轻量级数据持久层组件, 正式开源发布了

    经过几个星期的优化调整,今天 Restful.Data 正式开源发布. 源码地址:https://github.com/linli8/Restful 今天不写那么多废话了,还是重新介绍一下 Restf ...

  7. Java数据持久层框架 MyBatis之背景知识一

    对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...

  8. .NET平台数据持久层框架

    在.NET平台下的几个数据持久层框架: 1.NHibernate 2.NBear 3.Castle ActiveRecord 4.iBATIS.NET 5.DAAB 6.DLinq

  9. [置顶] 数据持久层(DAO)常用功能–通用API的实现

    在Web开发中,一般都分3层. Controller/Action 控制层, Service/Business 服务层/业务逻辑层, Dao 数据访问层/数据持久层. 在学习和工作的实践过程中,我发现 ...

  10. Hibernate: 数据持久层框架

    Hibernate 是一种Java语言下的对象关系映射解决方案. 它是使用GNU宽通用公共许可证发行的自由.开源的软件.它为面向对象的领域模型到传统的关系型数据库的映射,提供了一个使用方便的框架.Hi ...

随机推荐

  1. Native Application 开发详解(直接在程序中调用 ntdll.dll 中的 Native API,有内存小、速度快、安全、API丰富等8大优点)

    文章目录:                   1. 引子: 2. Native Application Demo 展示: 3. Native Application 简介: 4. Native Ap ...

  2. Two-Phase Locking

    两阶段封锁(Two-Phase Locking) 两段锁协议的内容 1. 在对任何数据进行读.写操作之前,事务首先要获得对该数据的封锁 2. 在释放一个封锁之后,事务不再获得任何其他封锁. “两段”锁 ...

  3. 正则 ?<= 和 ?= 用法

    参考网址:http://baike.baidu.com/link?url=2zORJF9GOjU8AkmuHDLz9cyl9yiL68PdW3frayzLwWQhDvDEM51V_CcY_g1mZ7O ...

  4. 【POJ】3076 Sudoku

    DLX第一题,模板留念. /* 3076 */ #include <iostream> #include <string> #include <map> #incl ...

  5. Oracle备忘录

    习惯是一个数据库中有多个用户,但是一个用户对应一个系统 数据库管理员主要职责: 每个Oracle数据库应该至少有一个数据库管理员(dba),对于一个小的数据库,一个dba就够了,但是对于一个大的数据库 ...

  6. 关于JQuery与AJAX验证

    AJAX验证,其实就是JS代码,他就是先利用Jquery或JS获取一个值,然后偷偷的把值传送到验证界面,然后在偷偷的把验证后的结果给传回来,利用传回来的结果在进行JS判断,从而不会刷新界面. 用图片解 ...

  7. Wordpress prettyPhoto插件跨站脚本漏洞

    漏洞名称: Wordpress prettyPhoto插件跨站脚本漏洞 CNNVD编号: CNNVD-201311-413 发布时间: 2013-11-28 更新时间: 2013-11-28 危害等级 ...

  8. 教程 打造OS X Mavericks原版 EFI Clover 引导安装

    自从 Mavericks 10.9 发布DP版到现在的GM版以来,以前Clover引导原版InstallESD.dmg方式安装原版的方法已经不能使用,而且已经不能引导安装了,所以从GM版发布以前,终于 ...

  9. java 页面url传值中文乱码的解决方法

    parent.window.location.href 和 iframe中src的乱码问题.要在这两个url地址中传中文,必须加编码,然后再解码.编码:encodeURI(encodeURI(&quo ...

  10. 指尖下的js ——多触式web前端开发之一:对于Touch的处理

    指尖下的js ——多触式web前端开发之一:对于Touch的处理 水果公司的那些small and cute的设备给我们提供了前所未有的用户体验.当用户在iphone和ipad上运指如飞的时候,那些使 ...