ODB (C++)

ODB (C++)
ODB
Developer(s) Code Synthesis
Stable release 1.6.0 / October 4, 2011; 20 days ago (2011-10-04)
Development status Active
Written in C++
Operating system Cross-platform C++
Type Object-relational mapping
License GNU General Public License and Proprietary License
Website http://www.codesynthesis.com/products/odb

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[1].

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.

To achieve high performance, low overhead, and reliability ODB does not use common database APIs, such as ODBC, to access the relational database. Rather, each database-specific runtime library uses low-level, native C APIs that are specific to each database. ODB currently supports the MySQL, SQLite, and PostgreSQL databases.

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.

Contents

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

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:

  typedef odb::query<person> query;
  typedef odb::result<person> result;
 
  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.

Views

Besides persistent objects, ODB also has the notion of views. An ODB view is a C++ class that embodies a light-weight, read-only projection of one or more persistent objects or database tables or the result of a native SQL query execution.

Some of the common applications of views include loading a subset of data members from objects or columns from database tables, executing and handling results of arbitrary SQL queries, including aggregate queries, as well as joining multiple objects and/or database tables using object relationships or custom join conditions.

Many relational databases also define the concept of views. However, ODB views are not mapped to database views. Rather, by default, an ODB view is mapped to an SQL SELECT query. But, if desired, it is possible to create an ODB view that is based on a relational database view.

As an example, consider a view that returns the number of people stored in the database:

  #pragma db view object(person)
  struct person_count
  {
    #pragma db column("count(" + person::id_ + ")")
    std::size_t count;
  };

The following code fragment shows how we can use this view to find out how many people are younger than 30:

  typedef odb::query<person_count> query;
  typedef odb::result<person_count> result;
 
  transaction t (db.begin ());
 
  result r (db.query<person_count> (query::age < 30));
 
  const person_count& c (*r.begin ());
  cout << c.count << endl;
 
  t.commit ();

ODB views can be defined in terms of one or more persistent objects, database tables, a combination of the two, or as a native SQL query.

References

  1. ^ GCC. GCC Plugins, 2010-10-01. Retrieved on 2010-10-01.

External links


Wikimedia Foundation. 2010.

Игры ⚽ Поможем сделать НИР

Look at other dictionaries:

  • ODB — can refer to: the IATA airport code of Córdoba Airport Ol Dirty Bastard (1968–2004), American rapper and founding member of the Wu Tang Clan ODB, an object relational mapping (ORM) system for C++ the Omniscient Debugger, a Java programming… …   Wikipedia

  • ODB — puede referirse a: Ol Dirty Bastard (Viejo y sucio bastardo), sobrenombre de Russell Tyrone Jones, rapero y miembro fundador del Wu Tang Clan; One Dirty Bitch (Una puta sucia), el sobrenombre que usa la luchadora Jessica Kresa; Aeropuerto de… …   Wikipedia Español

  • ODB++ — Not to be confused with ODB (C++), an object relational mapping system for the C++ language. Logo of ODB++ from Mentor Graphics ODB++ is a proprietary CAD to CAM data exchange format[1] used in the design and manufacture of electronic devices.… …   Wikipedia

  • ODB — Die Abkürzung ODB steht für: Flughafen Córdoba (Spanien), als IATA Code ODB, Ringname der US amerikanischen Wrestlerin Jessica Kresa Ol’ Dirty Bastard (1968–2004), amerikanischer Rapper OpenDocumentDataBase, Dateiformat (.odb), siehe OpenDocument …   Deutsch Wikipedia

  • Odb — Die Abkürzung ODB steht für: Oxford Dictionary of Byzantium OpenDocumentDataBase, Dateiformat (.odb), siehe OpenDocument Ol’ Dirty Bastard (1968–2004), amerikanischer Rapper …   Deutsch Wikipedia

  • ODB — Cette page d’homonymie répertorie les différents sujets et articles partageant un même nom.   Sigles d’une seule lettre   Sigles de deux lettres > Sigles de trois lettres   Sigles de quatre lettres …   Wikipédia en Français

  • ODB (wrestler) — ODB Ring name(s) Jessica Dalton[1] Jessica James[2] ODB (One Dirty Bitch) …   Wikipedia

  • ODB-3 Bass Overdrive — The ODB 3 Bass Overdrive is an Overdrive pedal made by the Boss Corporation specifically for the electric bass. It contains a level knob, a 2 band equalizer, a wet dry knob and a gain control. It is used by many well known bassists …   Wikipedia

  • ODB — operational detachment Bravo …   Military dictionary

  • .odb — Vorlage:Infobox Dateiformat/Wartung/magic fehltVorlage:Infobox Dateiformat/Wartung/website fehlt OpenDocument Dateiendung .od* (Vorlagen: .ot*) MIME Type application/vnd.oasis.opendocument.* Entwickelt von …   Deutsch Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”