Data Persistence Layer (iii) ODB INTRODUCTION

Source: Internet
Author: User

Odb:c++ object-relational Mapping (ORM)

ODB is a open-source, Cross-platform, and cross-database object-relational Mapping (ORM) system for C + +. It allows persist C + + objects to a relational database without have to deal with tables, columns, or SQL and with Out 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++1 1 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 person    {    ...  privateFriend class oDB::accessstring Email_string name_unsigned short age_;} ;
Person     {    ...  private:    string Email_string name_unsigned short age_;} ;

ODB is not a framework. It does not dictate how do you should write your application. Rather, it is designed-fit into your style and architecture by only handling C + + object persistence and not interfering With any other functionality. As can see, existing classes can is made persistent with only a few modifications. In particular, a persistent class can is declared without the default constructor, existing accessor and modifier function S can is automatically used to access the data members, and ODB Pragmas can is 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 the treat ODB persistent objects like any other C + + Clas SES in your application.

Given The above declarations, we can perform various database operations with objects of the person class using SQLite as a n Example:

Odb::sqlite::Database db ("People.db");Person John ("[Email protected]","John Doe",31);Person Jane ( "[email protected]"  "Jane Doe" 29) transaction t (Db.begin ()) typedef odb::query<person> person_query< span class= "PC" >; for (person& p: Db.query<person> ( span class= "T" >person_query::age < 30)) ; Jane.age (Jane.age () + 1)                 

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

Using ODB for object persistence have 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 Vocabula Ry 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 + + t Ypes is compatible. All the helps catch programming errors at Compile-time rather than at runtime.
  • Database portability. Because The database conversion code is automatically generated, it's easy-to-switch from-one database vendor to another.
  • Optimal performance. ODB have been designed for high performance and low memory overhead. All the available optimization techniques, such as prepared statements and extensive connection, statement, and buffer CAC Hing, is used to provide the very efficient implementation for each database operation. Persistent classes has zero per-object memory overhead. There is no hidden "database" members, each class must has nor is there per-object data structures allocated by ODB .
  • maintainability. Automatic code generation and database schema evolution minimize the effort needed to adapt the application-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 can assign SQL types to individual data members, use the existing database schema, and execute native SQL queries. In fact, at a extreme, ODB can be used as  just  a convenient it to handle results of native SQL Querie S.

The C + + code that performs the conversion between persistent classes and their database representation is automatically ge Nerated 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 isn't an Ad-hoc header pre-processor that's 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, it output is standard C + + which means that you can use any C + + compiler to build your APPL Ication.

Data Persistence Layer (iii) ODB INTRODUCTION

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.