source: http://www.codesynthesis.com/products/odb/doc/manual.xhtml#16
params);
C++ Object Persistence with ODB
Copyright © 2009-2013 Code Synthesis Tools CC
Permission is granted to copy, distribute and/or modify this
document under the terms of the
GNU Free
Documentation License, version 1.3; with no Invariant Sections,
no Front-Cover Texts and no Back-Cover Texts.
Revision 2.2, February 2013
This revision of the manual describes ODB 2.2.0 and is available
in the following formats:
XHTML,
PDF, and
PostScript.
Table of Contents
Preface
About This Document
More Information
PART I OBJECT-RELATIONAL MAPPING
1Introduction
1.1Architecture and Workflow
1.2Benefits
1.3Supported C++ Standards
2Hello World Example
2.1Declaring a Persistent Class
2.2Generating Database Support Code
2.3Compiling and Running
2.4Making Objects Persistent
2.5Querying the Database for Objects
2.6Updating Persistent Objects
2.7Defining and Using Views
2.8Deleting Persistent Objects
2.9Accessing Multiple Databases
2.10Summary
3Working with Persistent Objects
3.1Concepts and Terminology
3.2Declaring Persistent Objects and Values
3.3Object and View Pointers
3.4Database
3.5Transactions
3.6Connections
3.7Error Handling and Recovery
3.8Making Objects Persistent
3.9Loading Persistent Objects
3.10Updating Persistent Objects
3.11Deleting Persistent Objects
3.12Executing Native SQL Statements
3.13Tracing SQL Statement Execution
3.14ODB Exceptions
4Querying the Database
4.1ODB Query Language
4.2Parameter Binding
4.3Executing a Query
4.4Query Result
4.5Prepared Queries
5Containers
5.1Ordered Containers
5.2Set and Multiset Containers
5.3Map and Multimap Containers
5.4Change-Tracking Containers
5.4.1Change-Tracking vector
5.5Using Custom Containers
6Relationships
6.1Unidirectional Relationships
6.1.1To-One Relationships
6.1.2To-Many Relationships
6.2Bidirectional Relationships
6.2.1One-to-One Relationships
6.2.2One-to-Many Relationships
6.2.3Many-to-Many Relationships
6.3Circular Relationships
6.4Lazy Pointers
6.5Using Custom Smart Pointers
7Value Types
7.1Simple Value Types
7.2Composite Value Types
7.2.1Composite Object Ids
7.2.2Composite Value Column and Table Names
7.3Pointers and NULL Value Semantics
8Inheritance
8.1Reuse Inheritance
8.2Polymorphism Inheritance
8.2.1Performance and Limitations
8.3Mixed Inheritance
9Views
9.1Object Views
9.2Table Views
9.3Mixed Views
9.4View Query Conditions
9.5Native Views
9.6Other View Features and Limitations
10Session
10.1Object Cache
10.2Custom Sessions
11Optimistic Concurrency
12ODB Pragma Language
12.1Object Type Pragmas
12.1.1table
12.1.2pointer
12.1.3abstract
12.1.4readonly
12.1.5optimistic
12.1.6no_id
12.1.7callback
12.1.8schema
12.1.9polymorphic
12.1.10session
12.1.11definition
12.1.12transient
12.2View Type Pragmas
12.2.1object
12.2.2table
12.2.3query
12.2.4pointer
12.2.5callback
12.2.6definition
12.2.7transient
12.3Value Type Pragmas
12.3.1type
12.3.2id_type
12.3.3null/not_null
12.3.4default
12.3.5options
12.3.6readonly
12.3.7definition
12.3.8transient
12.3.9unordered
12.3.10index_type
12.3.11key_type
12.3.12value_type
12.3.13value_null/value_not_null
12.3.14id_options
12.3.15index_options
12.3.16key_options
12.3.17value_options
12.3.18id_column
12.3.19index_column
12.3.20key_column
12.3.21value_column
12.4Data Member Pragmas
12.4.1id
12.4.2auto
12.4.3type
12.4.4id_type
12.4.5get/set/access
12.4.6null/not_null
12.4.7default
12.4.8options
12.4.9column (object, composite value)
12.4.10column (view)
12.4.11transient
12.4.12readonly
12.4.13virtual
12.4.14inverse
12.4.15version
12.4.16index
12.4.17unique
12.4.18unordered
12.4.19table
12.4.20index_type
12.4.21key_type
12.4.22value_type
12.4.23value_null/value_not_null
12.4.24id_options
12.4.25index_options
12.4.26key_options
12.4.27value_options
12.4.28id_column
12.4.29index_column
12.4.30key_column
12.4.31value_column
12.5Namespace Pragmas
12.5.1pointer
12.5.2table
12.5.3schema
12.5.4session
12.6Index Definition Pragmas
12.7Database Type Mapping Pragmas
12.8C++ Compiler Warnings
12.8.1GNU C++
12.8.2Visual C++
12.8.3Sun C++
12.8.4IBM XL C++
12.8.5HP aC++
12.8.6Clang
13Advanced Techniques and Mechanisms
13.1Transaction Callbacks
PART II DATABASE SYSTEMS
14Multi-Database Support
14.1Static Multi-Database Support
14.2Dynamic Multi-Database Support
14.2.214.2.2 Dynamic Loading of Database Support Code
15MySQL Database
15.1MySQL Type Mapping
15.1.1String Type Mapping
15.1.2Binary Type Mapping
15.2MySQL Database Class
15.3MySQL Connection and Connection Factory
15.4MySQL Exceptions
15.5MySQL Limitations
15.5.1Foreign Key Constraints
15.6MySQL Index Definition
16SQLite Database
16.1SQLite Type Mapping
16.1.1String Type Mapping
16.1.2Binary Type Mapping
16.2SQLite Database Class
16.3SQLite Connection and Connection Factory
16.4SQLite Exceptions
16.5SQLite Limitations
16.5.1Query Result Caching
16.5.2Automatic Assignment of Object Ids
16.5.3Foreign Key Constraints
16.5.4Constraint Violations
16.5.5Sharing of Queries
16.6SQLite Index Definition
17PostgreSQL Database
17.1PostgreSQL Type Mapping
17.1.1String Type Mapping
17.1.2Binary Type and UUID Mapping
17.2PostgreSQL Database Class
17.3PostgreSQL Connection and Connection Factory
17.4PostgreSQL Exceptions
17.5PostgreSQL Limitations
17.5.1Query Result Caching
17.5.2Foreign Key Constraints
17.5.3Unique Constraint Violations
17.5.4Date-Time Format
17.5.5Timezones
17.5.6NUMERIC Type Support
17.6PostgreSQL Index Definition
18Oracle Database
18.1Oracle Type Mapping
18.1.1String Type Mapping
18.1.2Binary Type Mapping
18.2Oracle Database Class
18.3Oracle Connection and Connection Factory
18.4Oracle Exceptions
18.5Oracle Limitations
18.5.1Identifier Truncation
18.5.2Query Result Caching
18.5.3Foreign Key Constraints
18.5.4Unique Constraint Violations
18.5.5Large FLOAT and NUMBER Types
18.5.6Timezones
18.5.7LONG Types
18.5.8LOB Types and By-Value Accessors/Modifiers
18.6Oracle Index Definition
19Microsoft SQL Server Database
19.1SQL Server Type Mapping
19.1.1String Type Mapping
19.1.2Binary Type and UNIQUEIDENTIFIER Mapping
19.1.3ROWVERSION Mapping
19.1.4Long String and Binary Types
19.2SQL Server Database Class
19.3SQL Server Connection and Connection Factory
19.4SQL Server Exceptions
19.5SQL Server Limitations
19.5.1Query Result Caching
19.5.2Foreign Key Constraints
19.5.3Unique Constraint Violations
19.5.4Multi-threaded Windows Applications
19.5.5Affected Row Count and DDL Statements
19.5.6Long Data and Auto Object Ids, ROWVERSION
19.5.7Long Data and By-Value Accessors/Modifiers
19.6SQL Server Index Definition
PART III PROFILES
20Profiles Introduction
21Boost Profile
21.1Smart Pointers Library
21.2Unordered Containers Library
21.3Multi-Index Container Library
21.4Optional Library
21.5Date Time Library
21.5.1MySQL Database Type Mapping
21.5.2SQLite Database Type Mapping
21.5.3PostgreSQL Database Type Mapping
21.5.4Oracle Database Type Mapping
21.5.5SQL Server Database Type Mapping
21.6Uuid Library
21.6.1MySQL Database Type Mapping
21.6.2SQLite Database Type Mapping
21.6.3PostgreSQL Database Type Mapping
21.6.4Oracle Database Type Mapping
21.6.5SQL Server Database Type Mapping
22Qt Profile
22.1Basic Types Library
22.1.1MySQL Database Type Mapping
22.1.2SQLite Database Type Mapping
22.1.3PostgreSQL Database Type Mapping
22.1.4Oracle Database Type Mapping
22.1.5SQL Server Database Type Mapping
22.2Smart Pointers Library
22.3Containers Library
22.3.1Change-Tracking QList
22.4Date Time Library
22.4.1MySQL Database Type Mapping
22.4.2SQLite Database Type Mapping
22.4.3PostgreSQL Database Type Mapping
22.4.4Oracle Database Type Mapping
22.4.5SQL Server Database Type Mapping
Preface
As more critical aspects of our lives become dependant on software
systems, more and more applications are required to save the data
they work on in persistent and reliable storage. Database management
systems and, in particular, relational database management systems
(RDBMS) are commonly used for such storage. However, while the
application development techniques and programming languages have
evolved significantly over the past decades, the relational database
technology in this area stayed relatively unchanged. In particular,
this led to the now infamous mismatch between the object-oriented
model used by many modern applications and the relational model still
used by RDBMS.
While relational databases may be inconvenient to use from modern
programming languages, they are still the main choice for many
applications due to their maturity, reliability, as well as the
availability of tools and alternative implementations.
To allow application developers to utilize relational databases
from their object-oriented applications, a technique called
object-relational mapping (ORM) is often used. It involves a
conversion layer that maps between objects in the application's
memory and their relational representation in the database. While
the object-relational mapping code can be written manually,
automated ORM systems are available for most object-oriented
programming languages in use today.
ODB is an ORM system for the C++ programming language. It was
designed and implemented with the following main goals:
Provide a fully-automatic ORM system. In particular, the
application developer should not have to manually write any
mapping code, neither for persistent classes nor for their
data member.
Provide clean and easy to use object-oriented persistence
model and database APIs that support the development of realistic
applications for a wide variety of domains.
Provide a portable and thread-safe implementation. ODB should be
written in standard C++ and capable of persisting any standard
C++ classes.
Provide profiles that integrate ODB with type systems of
widely-used frameworks and libraries such as Qt and Boost.
Provide a high-performance and low overhead implementation. ODB
should make efficient use of database and application resources.
About This Document
The goal of this manual is to provide you with an understanding
of the object persistence model and APIs which are implemented by ODB.
As such, this document is intended for C++ application developers and
software architects who are looking for a C++ object persistence
solution. Prior experience with C++ is required to understand
this document. A basic understanding of relational database systems
is advantageous but not expected or required.
More Information
Beyond this manual, you may also find the following sources of
information useful:
ODB
Compiler Command Line Manual.
The INSTALL files in the ODB source packages provide
build instructions for various platforms.
The odb-examples package contains a collection of
examples and a README file with an overview of each example.
The odb-users
mailing list is the place to ask technical questions about ODB.
Furthermore, the searchable
archives
may already have answers to some of your questions.
PART I
OBJECT-RELATIONAL MAPPING
Part I describes the essential database concepts, APIs, and tools that
together comprise the object-relational mapping for C++ as implemented
by ODB. It consists of the following chapters.
1Introduction
2Hello World Example
3Working with Persistent Objects
4Querying the Database
5Containers
6Relationships
7Value Types
8Inheritance
9Views
10Session
11Optimistic Concurrency
12ODB Pragma Language
1 Introduction
ODB is an object-relational mapping (ORM) system for C++. It provides
tools, APIs, and library support that allow you to persist C++ objects
to a relational database (RDBMS) without having to deal with tables,
columns, or SQL and without manually writing any of the mapping code.
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 run native SQL SELECT queries.
In fact, at an extreme, ODB can be used as just a convenient
way to handle results of native SQL queries.
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 object persistence
and not interfering with any other functionality. 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 has been designed for high performance and low memory
overhead. Prepared statements are used to send and receive
object state in binary format instead of text which reduces
the load on the application and the database server. Extensive
caching of connections, prepared statements, and buffers saves
time and resources on connection establishment, statement parsing,
and memory allocations. For each supported database system the
native C API is used instead of ODBC or higher-level wrapper
APIs to reduce overhead and provide the most efficient implementation
for each database operation. Finally, persistent classes have
zero memory overhead. There are no hidden "database" members
that each class must have nor are there per-object data structures
allocated by ODB.
In this chapter we present a high-level overview of ODB.
We will start with the ODB architecture and then outline the
workflow of building an application that uses ODB. We will
then continue by contrasting the drawbacks of the traditional
way of saving C++ objects to relational databases with the
benefits of using ODB for object persistence. We conclude the
chapter by discussing the C++ standards supported by ODB. The
next chapter takes a more hands-on approach and shows the
concrete steps necessary to implement object persistence in
a simple "Hello World" application.
1.1 Architecture and Workflow
From the application developer's perspective, ODB
consists of three main components: the ODB compiler, the common
runtime library, called libodb, and the
database-specific runtime libraries, called
libodb-
the name of the database system this runtime
is for, for example, libodb-mysql. For instance,
if the application is going to use the MySQL database for
object persistence, then the three ODB components that this
application will use are the ODB compiler, libodb
and libodb-mysql.
The ODB compiler generates the database support code for
persistent classes in your application. The input to the ODB
compiler is one or more C++ header files defining C++ classes
that you want to make persistent. For each input header file
the ODB compiler generates a set of C++ source files implementing
conversion between persistent C++ classes defined in this
header and their database representation. The ODB compiler
can also generate a database schema file that creates tables
necessary to store the persistent classes.
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 common runtime library defines database system-independent
interfaces that your application can use to manipulate persistent
objects. The database-specific runtime library provides implementations
of these interfaces for a concrete database as well as other
database-specific utilities that are used by the generated code.
Normally, the application does not use the database-specific
runtime library directly but rather works with it via the common
interfaces from libodb. The following diagram shows
the object persistence architecture of an application that uses
MySQL as the underlying database system:
The ODB system also defines two special-purpose languages:
the ODB Pragma Language and ODB Query Language. The ODB Pragma
Language is used to communicate various properties of persistent
classes to the ODB compiler by means of special #pragma
directives embedded in the C++ header files. It controls aspects
of the object-relational mapping such as names of tables and columns
that are used for persistent classes and their members or mapping between
C++ types and database types.
The ODB Query Language is an object-oriented database query
language that can be used to search for objects matching
certain criteria. It is modeled after and is integrated into
C++ allowing you to write expressive and safe queries that look
and feel like ordinary C++.
The use of the ODB compiler to generate database support code
adds an additional step to your application build sequence. The
following diagram outlines the typical build workflow of an
application that uses ODB:
1.2 Benefits
The traditional way of saving C++ objects to relational databases
requires that you manually write code which converts between the database
and C++ representations of each persistent class. The actions that
such code usually performs include conversion between C++ values and
strings or database types, preparation and execution of SQL queries,
as well as handling the result sets. Writing this code manually has
the following drawbacks:
Difficult and time consuming. Writing database conversion
code for any non-trivial application requires extensive
knowledge of the specific database system and its APIs.
It can also take a considerable amount of time to write
and maintain. Supporting multi-threaded applications can
complicate this task even further.
Suboptimal performance. Optimal conversion often
requires writing large amounts of extra code, such as
parameter binding for prepared statements and caching
of connections, statements, and buffers. Writing code
like this in an ad-hoc manner is often too difficult
and time consuming.
Database vendor lock-in. The conversion code is written for
a specific database which makes it hard to switch to another
database vendor.
Lack of type safety. It is easy to misspell column names or
pass incompatible values in SQL queries. Such errors will
only be detected at runtime.
Complicates the application. The database conversion code
often ends up interspersed throughout the application making it
hard to debug, change, and maintain.
In contrast, using ODB for C++ object persistence has the
following benefits:
Ease of use. ODB automatically generates database conversion
code from your C++ class declarations and allows you to manipulate
persistent objects using simple and thread-safe object-oriented
database APIs.
Concise code. With ODB hiding the details of the underlying
database, the application logic is written using the natural object
vocabulary instead of tables, columns and SQL. The resulting code
is simpler and thus easier to read and understand.
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.
Database portability. Because the database conversion code
is automatically generated, it is easy to switch from one database
vendor to another. In fact, it is possible to test your application
on several database systems before making a choice.
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.
Maintainability. Automatic code generation minimizes the
effort needed to adapt the application to changes in persistent
classes. The database support code is kept separately from the
class declarations and application logic. This makes the
application easier to debug and maintain.
Overall, ODB provides an easy to use yet flexible and powerful
object-relational mapping (ORM) system for C++. Unlike other
ORM implementations for C++ that still require you to write
database conversion or member registration code for each
persistent class, ODB keeps persistent classes purely
declarative. The functional part, the database conversion
code, is automatically generated by the ODB compiler from
these declarations.
1.3 Supported C++ Standards
ODB provides support for ISO/IEC C++ 1998/2003 (C++98/03),
ISO/IEC TR 19768 C++ Library Extensions (C++ TR1), and
ISO/IEC C++ 2011 (C++11). While the majority of the examples in
this manual use C++98/03, support for the new functionality and
library components introduced in TR1 and C++11 are discussed
throughout the document. The c++11 example in the
odb-examples package also shows ODB support for
various C++11 features.
2 Hello World Example
In this chapter we will show how to create a simple C++
application that relies on ODB for object persistence using
the traditional "Hello World" example. In particular, we will
discuss how to declare persistent classes, generate database
support code, as well as compile and run our application. We
will also learn how to make objects persistent, load, update
and delete persistent objects, as well as query the database
for persistent objects that match certain criteria. The example
also shows how to define and use views, a mechanism that allows
us to create projections of persistent objects, database tables,
or to handle results of native SQL queries.
The code presented in this chapter is based on the
hello example which can be found in the
odb-examples package of the ODB distribution.
2.1 Declaring a Persistent Class
In our "Hello World" example we will depart slightly from
the norm and say hello to people instead of the world. People
in our application will be represented as objects of C++ class
person which is saved in person.hxx:
// person.hxx
//
#include
class person
{
public:
person (const std::string& first,
const std::string& last,
unsigned short age);
const std::string& first () const;
const std::string& last () const;
unsigned short age () const;
void age (unsigned short);
private:
std::string first_;
std::string last_;
unsigned short age_;
};
In order not to miss anyone whom we need to greet, we would like
to save the person objects in a database. To achieve this
we declare the person class as persistent:
// person.hxx
//
#include
#include
#pragma db object // (2)
class person
{
...
private:
person () {} // (3)
friend class odb::access; // (4)
#pragma db id auto // (5)
unsigned long id_; // (5)
std::string first_;
std::string last_;
unsigned short age_;
};
To be able to save the person objects in the database
we had to make five changes, marked with (1) to (5), to the original
class definition. The first change is the inclusion of the ODB
header
of core ODB declarations, such as odb::access, that
are used to define persistent classes.
The second change is the addition of db object
pragma just before the class definition. This pragma tells the
ODB compiler that the class that follows is persistent. Note
that making a class persistent does not mean that all objects
of this class will automatically be stored in the database.
You would still create ordinary or transient instances
of this class just as you would before. The difference is that
now you can make such transient instances persistent, as we will
see shortly.
The third change is the addition of the default constructor.
The ODB-generated database support code will use this constructor
when instantiating an object from the persistent state. Just as we have
done for the person class, you can make the default
constructor private or protected if you don't want to make it
available to the users of your class. Note also that with some
limitations it is possible to have a persistent class without
the default constructor.
With the fourth change we make the odb::access class a
friend of our person class. This is necessary to make
the default constructor and the data members accessible to the
database support code. If your class has a public default constructor and
either public data members or public accessors and modifiers for the
data members, then the friend declaration is unnecessary.
The final change adds a data member called id_ which
is preceded by another pragma. In ODB every persistent object normally
has a unique, within its class, identifier. Or, in other words, no two
persistent instances of the same type have equal identifiers. While it
is possible to define a persistent class without an object id, the number
of database operations that can be performed on such a class is limited.
For our class we use an integer id. The db id auto
pragma that precedes the id_ member tells the ODB compiler
that the following member is the object's identifier. The
auto specifier indicates that it is a database-assigned
id. A unique id will be automatically generated by the database and
assigned to the object when it is made persistent.
In this example we chose to add an identifier because none of
the existing members could serve the same purpose. However, if
a class already has a member with suitable properties, then it
is natural to use that member as an identifier. For example,
if our person class contained some form of personal
identification (SSN in the United States or ID/passport number
in other countries), then we could use that as an id. Or, if
we stored an email associated with each person, then we could
have used that if each person is presumed to have a unique
email address.
As another example, consider the following alternative version
of the person class. Here we use one of
the existing data members as id. Also the data members are kept
private and are instead accessed via public accessor and modifier
functions. Finally, the ODB pragmas are grouped together and are
placed after the class definition. They could have also been moved
into a separate header leaving the original class completely
unchanged (for more information on such a non-intrusive conversion
refer to Chapter 12, "ODB Pragma Language").
class person
{
public:
person ();
const std::string& email () const;
void email (const std::string&);
const std::string& get_name () const;
std::string& set_name ();
unsigned short getAge () const;
void setAge (unsigned short);
private:
std::string email_;
std::string name_;
unsigned short age_;
};
#pragma db object(person)
#pragma db member(person::email_) id
Now that we have the header file with the persistent class, let's
see how we can generate that database support code.
2.2 Generating Database Support Code
The persistent class definition that we created in the previous
section was particularly light on any code that could actually
do the job and store the person's data to a database. There
was no serialization or deserialization code, not even data member
registration, that you would normally have to write by hand in
other ORM libraries for C++. This is because in ODB code
that translates between the database and C++ representations
of an object is automatically generated by the ODB compiler.
To compile the person.hxx header we created in the
previous section and generate the support code for the MySQL
database, we invoke the ODB compiler from a terminal (UNIX) or
a command prompt (Windows):
odb -d mysql --generate-query person.hxx
We will use MySQL as the database of choice in the remainder of
this chapter, though other supported database systems can be used
instead.
If you haven't installed the common ODB runtime library
(libodb) or installed it into a directory where
C++ compilers don't search for headers by default,
then you may get the following error:
person.hxx:10:24: fatal error: odb/core.hxx: No such file or directory
To resolve this you will need to specify the libodb headers
location with the -I preprocessor option, for example:
odb -I.../libodb -d mysql --generate-query person.hxx
Here .../libodb represents the path to the
libodb directory.
The above invocation of the ODB compiler produces three C++ files:
person-odb.hxx, person-odb.ixx,
person-odb.cxx. You normally don't use types
or functions contained in these files directly. Rather, all
you have to do is include person-odb.hxx in
C++ files where you are performing database operations
with classes from person.hxx as well as compile
person-odb.cxx and link the resulting object
file to your application.
You may be wondering what the --generate-query
option is for. It instructs the ODB compiler to generate
optional query support code that we will use later in our
"Hello World" example. Another option that we will find
useful is --generate-schema. This option
makes the ODB compiler generate a fourth file,
person.sql, which is the database schema
for the persistent classes defined in person.hxx:
odb -d mysql --generate-query --generate-schema person.hxx
The database schema file contains SQL statements that creates
tables necessary to store the persistent classes. We will learn
how to use it in the next section.
If you would like to see a list of all the available ODB compiler
options, refer to the
ODB
Compiler Command Line Manual.
Now that we have the persistent class and the database support
code, the only part that is left is the application code that
does something useful with all of this. But before we move on to
the fun part, let's first learn how to build and run an application
that uses ODB. This way when we have some application code
to try, there are no more delays before we can run it.
2.3 Compiling and Running
Assuming that the main() function with the application
code is saved in driver.cxx and the database support
code and schema are generated as described in the previous section,
to build our application we will first need to compile all the C++
source files and then link them with two ODB runtime libraries.
On UNIX, the compilation part can be done with the following commands
(substitute c++ with your C++ compiler name; for Microsoft
Visual Studio setup, see the odb-examples package):
c++ -c driver.cxx
c++ -c person-odb.cxx
Similar to the ODB compilation, if you get an error stating that
a header in odb/ or odb/mysql directory
is not found, you will need to use the -I
preprocessor option to specify the location of the common ODB runtime
library (libodb) and MySQL ODB runtime library
(libodb-mysql).
Once the compilation is done, we can link the application with
the following command:
c++ -o driver driver.o person-odb.o -lodb-mysql -lodb
Notice that we link our application with two ODB libraries:
libodb which is a common runtime library and
libodb-mysql which is a MySQL runtime library
(if you use another database, then the name of this library
will change accordingly). If you get an error saying that
one of these libraries could not be found, then you will need
to use the -L linker option to specify their locations.
Before we can run our application we need to create a database
schema using the generated person.sql file. For MySQL
we can use the mysql client program, for example:
mysql --user=odb_test --database=odb_test < person.sql
The above command will log in to a local MySQL server as user
odb_test without a password and use the database
named odb_test. Beware that after executing this
command, all the data stored in the odb_test database
will be deleted.
Note also that using a standalone generated SQL file is not the
only way to create a database schema in ODB. We can also embed
the schema directly into our application or use custom schemas
that were not generated by the ODB compiler. Refer to
Section 3.4, "Database" for details.
Once the database schema is ready, we run our application
using the same login and database name:
./driver --user odb_test --database odb_test
2.4 Making Objects Persistent
Now that we have the infrastructure work out of the way, it
is time to see our first code fragment that interacts with the
database. In this section we will learn how to make person
objects persistent:
// driver.cxx
//
#include
#include
#include
#include
#include
#include "person.hxx"
#include "person-odb.hxx"
using namespace std;
using namespace odb::core;
int
main (int argc, char* argv[])
{
try
{
auto_ptr
unsigned long john_id, jane_id, joe_id;
// Create a few persistent person objects.
//
{
person john ("John", "Doe", 33);
person jane ("Jane", "Doe", 32);
person joe ("Joe", "Dirt", 30);
transaction t (db->begin ());
// Make objects persistent and save their ids for later use.
//
john_id = db->persist (john);
jane_id = db->persist (jane);
joe_id = db->persist (joe);
t.commit ();
}
}
catch (const odb::exception& e)
{
cerr << e.what () << endl;
return 1;
}
}
Let's examine this code piece by piece. At the beginning we include
a bunch of headers. After the standard C++ headers we include
and
system-independent odb::database and
odb::transaction interfaces. Then we include
MySQL implementation of the database interface. Finally,
we include person.hxx and person-odb.hxx
which define our persistent person class.
Then we have two using namespace directives. The first
one brings in the names from the standard namespace and the second
brings in the ODB declarations which we will use later in the file.
Notice that in the second directive we use the odb::core
namespace instead of just odb. The former only brings
into the current namespace the essential ODB names, such as the
database and transaction classes, without
any of the auxiliary objects. This minimizes the likelihood of name
conflicts with other libraries. Note also that you should continue
using the odb namespace when qualifying individual names.
For example, you should write odb::database, not
odb::core::database.
Once we are in main(), the first thing we do is create
the MySQL database object. Notice that this is the last line in
driver.cxx that mentions MySQL explicitly; the rest
of the code works through the common interfaces and is database
system-independent. We use the argc/argv
mysql::database constructor which automatically
extract the database parameters, such as login name, password,
database name, etc., from the command line. In your own applications
you may prefer to use other mysql::database
constructors which allow you to pass this information directly
(Section 15.2, "MySQL Database Class").
Next, we create three person objects. Right now they are
transient objects, which means that if we terminate the application
at this point, they will be gone without any evidence of them ever
existing. The next line starts a database transaction. We discuss
transactions in detail later in this manual. For now, all we need
to know is that all ODB database operations must be performed within
a transaction and that a transaction is an atomic unit of work; all
database operations performed within a transaction either succeed
(committed) together or are automatically undone (rolled back).
Once we are in a transaction, we call the persist()
database function on each of our person objects.
At this point the state of each object is saved in the database.
However, note that this state is not permanent until and unless
the transaction is committed. If, for example, our application
crashes at this point, there will still be no evidence of our
objects ever existing.
In our case, one more thing happens when we call persist().
Remember that we decided to use database-assigned identifiers for our
person objects. The call to persist() is
where this assignment happens. Once this function returns, the
id_ member contains this object's unique identifier.
As a convenience, the persist() function also returns
a copy of the object's identifier that it made persistent. We
save the returned identifier for each object in a local variable.
We will use these identifiers later in the chapter to perform other
database operations on our persistent objects.
After we have persisted our objects, it is time to commit the
transaction and make the changes permanent. Only after the
commit() function returns successfully, are we
guaranteed that the objects are made persistent. Continuing
with the crash example, if our application terminates after
the commit for whatever reason, the objects' state in the
database will remain intact. In fact, as we will discover
shortly, our application can be restarted and load the
original objects from the database. Note also that a
transaction must be committed explicitly with the
commit() call. If the transaction
object leaves scope without the transaction being
explicitly committed or rolled back, it will automatically be
rolled back. This behavior allows you not to worry about
exceptions being thrown within a transaction; if they
cross the transaction boundary, the transaction will
automatically be rolled back and all the changes made
to the database undone.
The final bit of code in our example is the catch
block that handles the database exceptions. We do this by catching
the base ODB exception (Section 3.14, "ODB
Exceptions") and printing the diagnostics.
Let's now compile (Section 2.3, "Compiling and
Running") and then run our first ODB application:
mysql --user=odb_test --database=odb_test < person.sql
./driver --user odb_test --database odb_test
Our first application doesn't print anything except for error
messages so we can't really tell whether it actually stored the
objects' state in the database. While we will make our application
more entertaining shortly, for now we can use the mysql
client to examine the database content. It will also give us a feel
for how the objects are stored:
mysql --user=odb_test --database=odb_test
Welcome to the MySQL monitor.
mysql> select * from person;
+----+-------+------+-----+
| id | first | last | age |
+----+-------+------+-----+
| 1 | John | Doe | 33 |
| 2 | Jane | Doe | 32 |
| 3 | Joe | Dirt | 30 |
+----+-------+------+-----+
3 rows in set (0.00 sec)
mysql> quit
Another way to get more insight into what's going on under the hood,
is to trace the SQL statements executed by ODB as a result of
each database operation. Here is how we can enable tracing just for
the duration of our transaction:
// Create a few persistent person objects.
//
{
...
transaction t (db->begin ());
t.tracer (stderr_tracer);
// Make objects persistent and save their ids for later use.
//
john_id = db->persist (john);
jane_id = db->persist (jane);
joe_id = db->persist (joe);
t.commit ();
}
With this modification our application now produces the following
output:
INSERT INTO `person` (`id`,`first`,`last`,`age`) VALUES (?,?,?,?)
INSERT INTO `person` (`id`,`first`,`last`,`age`) VALUES (?,?,?,?)
INSERT INTO `person` (`id`,`first`,`last`,`age`) VALUES (?,?,?,?)
Note that we see question marks instead of the actual values
because ODB uses prepared statements and sends the data to the
database in binary form. For more information on tracing, refer
to Section 3.13, "Tracing SQL Statement Execution".
In the next section we will see how to access persistent objects
from our application.
2.5 Querying the Database for Objects
So far our application doesn't resemble a typical "Hello World"
example. It doesn't print anything except for error messages.
Let's change that and teach our application to say hello to
people from our database. To make it a bit more interesting,
let's say hello only to people over 30:
// driver.cxx
//
...
int
main (int argc, char* argv[])
{
try
{
...
// Create a few persistent person objects.
//
{
...
}
typedef odb::query
typedef odb::result
// Say hello to those over 30.
//
{
transaction t (db->begin ());
result r (db->query
for (result::iterator i (r.begin ()); i != r.end (); ++i)
{
cout << "Hello, " << i->first () << "!" << endl;
}
t.commit ();
}
}
catch (const odb::exception& e)
{
cerr << e.what () << endl;
return 1;
}
}
The first half of our application is the same as before and is
replaced with "..." in the above listing for brevity. Again, let's
examine the rest of it piece by piece.
The two typedefs create convenient aliases for two
template instantiations that will be used a lot in our application.
The first is the query type for the person objects
and the second is the result type for that query.
Then we begin a new transaction and call the query()
database function. We pass a query expression
(query::age > 30) which limits the returned objects
only to those with the age greater than 30. We also save the result
of the query in a local variable.
The next few lines perform a standard for-loop iteration
over the result sequence printing hello for every returned person.
Then we commit the transaction and that's it. Let's see what
this application will print:
mysql --user=odb_test --database=odb_test < person.sql
./driver --user odb_test --database odb_test
Hello, John!
Hello, Jane!
That looks about right, but how do we know that the query actually
used the database instead of just using some in-memory artifacts of
the earlier persist() calls? One way to test this
would be to comment out the first transaction in our application
and re-run it without re-creating the database schema. This way the
objects that were persisted during the previous run will be returned.
Alternatively, we can just re-run the same application without
re-creating the schema and notice that we now show duplicate
objects:
./driver --user odb_test --database odb_test
Hello, John!
Hello, Jane!
Hello, John!
Hello, Jane!
What happens here is that the previous run of our application
persisted a set of person objects and when we re-run
the application, we persist another set with the same names but
with different ids. When we later run the query, matches from
both sets are returned. We can change the line where we print
the "Hello" string as follows to illustrate this point:
cout << "Hello, " << i->first () << " (" << i->id () << ")!" << endl;
If we now re-run this modified program, again without re-creating
the database schema, we will get the following output:
./driver --user odb_test --database odb_test
Hello, John (1)!
Hello, Jane (2)!
Hello, John (4)!
Hello, Jane (5)!
Hello, John (7)!
Hello, Jane (8)!
The identifiers 3, 6, and 9 that are missing from the above list belong
to the "Joe Dirt" objects which are not selected by this query.
2.6 Updating Persistent Objects
While making objects persistent and then selecting some of them using
queries are two useful operations, most applications will also need
to change the object's state and then make these changes persistent.
Let's illustrate this by updating Joe's age who just had a birthday:
// driver.cxx
//
...
int
main (int argc, char* argv[])
{
try
{
...
unsigned long john_id, jane_id, joe_id;
// Create a few persistent person objects.
//
{
...
// Save object ids for later use.
//
john_id = john.id ();
jane_id = jane.id ();
joe_id = joe.id ();
}
// Joe Dirt just had a birthday, so update his age.
//
{
transaction t (db->begin ());
auto_ptr
joe->age (joe->age () + 1);
db->update (*joe);
t.commit ();
}
// Say hello to those over 30.
//
{
...
}
}
catch (const odb::exception& e)
{
cerr << e.what () << endl;
return 1;
}
}
The beginning and the end of the new transaction are the same as
the previous two. Once within a transaction, we call the
load() database function to instantiate a
person object with Joe's persistent state. We
pass Joe's object identifier that we stored earlier when we
made this object persistent. While here we use
std::auto_ptr to manage the returned object, we
could have also used another smart pointer, for example
std::unique_ptr from C++11 or shared_ptr
from TR1, C++11, or Boost. For more information
on the object lifetime management and the smart pointers that we
can use for that, see Section 3.3, "Object
and View Pointers".
With the instantiated object in hand we increment the age
and call the update() function to update
the object's state in the database. Once the transaction is
committed, the changes are made permanent.
If we now run this application, we will see Joe in the output
since he is now over 30:
mysql --user=odb_test --database=odb_test < person.sql
./driver --user odb_test --database odb_test
Hello, John!
Hello, Jane!
Hello, Joe!
What if we didn't have an identifier for Joe? Maybe this object
was made persistent in another run of our application or by another
application altogether. Provided that we only have one Joe Dirt
in the database, we can use the query facility to come up with
an alternative implementation of the above transaction:
// Joe Dirt just had a birthday, so update his age. An
// alternative implementation without using the object id.
//
{
transaction t (db->begin ());
result r (db->query
query::last == "Dirt"));
result::iterator i (r.begin ());
if (i != r.end ())
{
auto_ptr
joe->age (joe->age () + 1);
db->update (*joe);
}
t.commit ();
}
2.7 Defining and Using Views
Suppose that we need to gather some basic statistics about the people
stored in our database. Things like the total head count, as well as
the minimum and maximum ages. One way to do it would be to query
the database for all the person objects and then
calculate this information as we iterate over the query result.
While this approach may work fine for our database with just three
people in it, it would be very inefficient if we had a large
number of objects.
While it may not be conceptually pure from the object-oriented
programming point of view, a relational database can perform
some computations much faster and much more economically than
if we performed the same operations ourselves in the application's
process.
To support such cases ODB provides 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 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.
While you can find a much more detailed description of views in
Chapter 9, "Views", here is how we can define
the person_stat view that returns the basic statistics
about the person objects:
#pragma db view object(person)
struct person_stat
{
#pragma db column("count(" + person::id_ + ")")
std::size_t count;
#pragma db column("min(" + person::age_ + ")")
unsigned short min_age;
#pragma db column("max(" + person::age_ + ")")
unsigned short max_age;
};
To get the result of a view we use the same query()
function as when querying the database for an object. Here is
how we can load and print our statistics using the view we have
just created:
// Print some statistics about all the people in our database.
//
{
transaction t (db->begin ());
odb::result
// The result of this query always has exactly one element.
//
const person_stat& ps (*r.begin ());
cout << "count : " << ps.count << endl
<< "min age: " << ps.min_age << endl
<< "max age: " << ps.max_age << endl;
t.commit ();
}
If we now add the person_stat view to the
person.hxx header, the above transaction
to driver.cxx, as well as re-compile and
re-run our example, then we will see the following
additional lines in the output:
count : 3
min age: 31
max age: 33
2.8 Deleting Persistent Objects
The last operation that we will discuss in this chapter is deleting
the persistent object from the database. The following code
fragment shows how we can delete an object given its identifier:
// John Doe is no longer in our database.
//
{
transaction t (db->begin ());
db->erase
t.commit ();
}
To delete John from the database we start a transaction, call
the erase() database function with John's object
id, and commit the transaction. After the transaction is committed,
the erased object is no longer persistent.
If we don't have an object id handy, we can use queries to find
and delete the object:
// John Doe is no longer in our database. An alternative
// implementation without using the object id.
//
{
transaction t (db->begin ());
result r (db->query
query::last == "Doe"));
result::iterator i (r.begin ());
if (i != r.end ())
{
auto_ptr
db->erase (*john);
}
t.commit ();
}
2.9 Working with Multiple Databases
Accessing multiple databases (that is, data stores) is simply a
matter of creating multiple odb::
instances representing each database. For example:
odb::mysql::database db1 ("john", "secret", "test_db1");
odb::mysql::database db2 ("john", "secret", "test_db2");
A more interesting question is how we access multiple database
systems (that is, database implementations) from the same application.
For example, our application may need to store some objects in a
remote MySQL database and others in a local SQLite file. Or, our
application may need to be able to store its objects in a database
system that is selected by the user at runtime.
ODB provides comprehensive multi-database support that ranges from
tight integration with specific database systems to being able to
write database-agnostic code and loading individual database systems
support dynamically. While all these aspects are covered in detail
in Chapter 14, "Multi-Database Support", in this
section we will get a taste of this functionality by extending our
"Hello World" example to be able to store its data either in MySQL
or PostgreSQL (other database systems supported by ODB can be added
in a similar manner).
The first step in adding multi-database support is to re-compile
our person.hxx header to generate database support
code for additional database systems:
odb --multi-database dynamic -d common -d mysql -d pgsql \
--generate-query --generate-schema person.hxx
The --multi-database ODB compiler option turns on
multi-database support. For now it is not important what the
dynamic value that we passed to this option means, but
if you are curious, see Chapter 14. The result of this
command are three sets of generated files: person-odb.?xx
(common interface; corresponds to the common database),
person-odb-mysql.?xx (MySQL support code), and
person-odb-pgsql.?xx (PostgreSQL support code). There
are also two schema files: person-mysql.sql and
person-pgsql.sql.
The only part that we need to change in driver.cxx
is how we create the database instance. Specifically, this line:
auto_ptr
Now our example is capable of storing its data either in MySQL or
PostgreSQL so we need to somehow allow the caller to specify which
database we must use. To keep things simple, we will make the first
command line argument specify the database system we must use while
the rest will contain the database-specific options which we will
pass to the odb::
before. Let's put all this logic into a separate function which we
will call create_database(). Here is what the beginning
of our modified driver.cxx will look like (the remainder
is unchanged):
// driver.cxx
//
#include
#include
#include
#include
#include
#include
#include
#include "person.hxx"
#include "person-odb.hxx"
using namespace std;
using namespace odb::core;
auto_ptr
create_database (int argc, char* argv[])
{
auto_ptr
if (argc < 2)
{
cerr << "error: database system name expected" << endl;
return r;
}
string db (argv[1]);
if (db == "mysql")
r.reset (new odb::mysql::database (argc, argv));
else if (db == "pgsql")
r.reset (new odb::pgsql::database (argc, argv));
else
cerr << "error: unknown database system " << db << endl;
return r;
}
int
main (int argc, char* argv[])
{
try
{
auto_ptr
if (db.get () == 0)
return 1; // Diagnostics has already been issued.
...
And that's it. The only thing left is to build and run our
example:
c++ -c driver.cxx
c++ -c person-odb.cxx
c++ -c person-odb-mysql.cxx
c++ -c person-odb-pgsql.cxx
c++ -o driver driver.o person-odb.o person-odb-mysql.o \
person-odb-pgsql.o -lodb-mysql -lodb-pgsql -lodb
Here is how we can access a MySQL database:
mysql --user=odb_test --database=odb_test < person-mysql.sql
./driver mysql --user odb_test --database odb_test
Or a PostgreSQL database:
psql --user=odb_test --dbname=odb_test -f person-pgsql.sql
./driver pgsql --user odb_test --database odb_test
2.10 Summary
This chapter presented a very simple application which, nevertheless,
exercised all of the core database functions: persist(),
query(), load(), update(),
and erase(). We also saw that writing an application
that uses ODB involves the following steps:
Declare persistent classes in header files.
Compile these headers to generate database support code.
Link the application with the generated code and two ODB runtime
libraries.
Do not be concerned if, at this point, much appears unclear. The intent
of this chapter is to give you only a general idea of how to persist C++
objects with ODB. We will cover all the details throughout the remainder
of this manual.
3 Working with Persistent Objects
The previous chapters gave us a high-level overview of ODB and
showed how to use it to store C++ objects in a database. In this
chapter we will examine the ODB object persistence model as
well as the core database APIs in greater detail. We will
start with basic concepts and terminology in Section
3.1 and Section 3.3 and continue with the
discussion of the odb::database class in
Section 3.4, transactions in
Section 3.5, and connections in
Section 3.6. The remainder of this chapter
deals with the core database operations and concludes with
the discussion of ODB exceptions.
In this chapter we will continue to use and expand the
person persistent class that we have developed in the
previous chapter.
3.1 Concepts and Terminology
The term database can refer to three distinct things:
a general notion of a place where an application stores its data,
a software implementation for managing this data (for example
MySQL), and, finally, some database software implementations
may manage several data stores which are usually distinguished
by name. This name is also commonly referred to as a database.
In this manual, when we use the word database, we
refer to the first meaning above, for example,
"The update() function saves the object's state to
the database." The term Database Management System (DBMS) is
often used to refer to the second meaning of the word database.
In this manual we will use the term database system
for short, for example, "Database system-independent
application code." Finally, to distinguish the third meaning
from the other two, we will use the term database name,
for example, "The second option specifies the database name
that the application should use to store its data."
In C++ there is only one notion of a type and an instance
of a type. For example, a fundamental type, such as int,
is, for the most part, treated the same as a user defined class
type. However, when it comes to persistence, we have to place
certain restrictions and requirements on certain C++ types that
can be stored in the database. As a result, we divide persistent
C++ types into two groups: object types and value
types. An instance of an object type is called an object
and an instance of a value type — a value.
An object is an independent entity. It can be stored, updated,
and deleted in the database independent of other objects.
Normally, an object has an identifier, called object id,
that is unique among all instances of an object type within a
database. In contrast, a value can only be stored in the database
as part of an object and doesn't have its own unique identifier.
An object consists of data members which are either values
(Chapter 7, "Value Types"), pointers
to other objects (Chapter 6, "Relationships"), or
containers of values or pointers to other objects (Chapter
5, "Containers"). Pointers to other objects and containers can
be viewed as special kinds of values since they also can only
be stored in the database as part of an object.
An object type is a C++ class. Because of this one-to-one
relationship, we will use terms object type
and object class interchangeably. In contrast,
a value type can be a fundamental C++ type, such as
int or a class type, such as std::string.
If a value consists of other values, then it is called a
composite value and its type — a
composite value type (Section 7.2,
"Composite Value Types"). Otherwise, the value is
called simple value and its type — a
simple value type (Section 7.1,
"Simple Value Types"). Note that the distinction between
simple and composite values is conceptual rather than
representational. For example, std::string
is a simple value type because conceptually string is a
single value even though the representation of the string
class may contain several data members each of which could be
considered a value. In fact, the same value type can be
viewed (and mapped) as both simple and composite by different
applications.
While not strictly necessary in a purely object-oriented application,
practical considerations often require us to only load a
subset of an object's data members or a combination of members
from several objects. We may also need to factor out some
computations to the relational database instead of performing
them in the application's process. To support such requirements
ODB distinguishes a third kind of C++ types, called views
(Chapter 9, "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.
Understanding how all these concepts map to the relational model
will hopefully make these distinctions clearer. In a relational
database an object type is mapped to a table and a value type is
mapped to one or more columns. A simple value type is mapped
to a single column while a composite value type is mapped to
several columns. An object is stored as a row in this
table and a value is stored as one or more cells in this row.
A simple value is stored in a single cell while a composite
value occupies several cells. A view is not a persistent
entity and it is not stored in the database. Rather, it is a
data structure that is used to capture a single row of an SQL
query result.
Going back to the distinction between simple and composite
values, consider a date type which has three integer
members: year, month, and day. In one application it can be
considered a composite value and each member will get its
own column in a relational database. In another application
it can be considered a simple value and stored in a single
column as a number of days from some predefined date.
Until now, we have been using the term persistent class
to refer to object classes. We will continue to do so even though
a value type can also be a class. The reason for this asymmetry
is the subordinate nature of value types when it comes to
database operations. Remember that values are never stored
directly but rather as part of an object that contains them.
As a result, when we say that we want to make a C++ class
persistent or persist an instance of a class in the database,
we invariably refer to an object class rather than a value
class.
Normally, you would use object types to model real-world entities,
things that have their own identity. For example, in the
previous chapter we created a person class to model
a person, which is a real-world entity. Name and age, which we
used as data members in our person class are clearly
values. It is hard to think of age 31 or name "Joe" as having their
own identities.
A good test to determine whether something is an object or
a value, is to consider if other objects might reference
it. A person is clearly an object because it can be referred
to by other objects such as a spouse, an employer, or a
bank. On the other hand, a person's age or name is not
something that other objects would normally refer to.
Also, when an object represents a real entity, it is easy to
choose a suitable object id. For example, for a
person there is an established notion of an identifier
(SSN, student id, passport number, etc). Another alternative
is to use a person's email address as an identifier.
Note, however, that these are only guidelines. There could
be good reasons to make something that would normally be
a value an object. Consider, for example, a database that
stores a vast number of people. Many of the person
objects in this database have the same names and surnames and
the overhead of storing them in every object may negatively
affect the performance. In this case, we could make the first name
and last name each an object and only store pointers to
these objects in the person class.
An instance of a persistent class can be in one of two states:
transient and persistent. A transient
instance only has a representation in the application's
memory and will cease to exist when the application terminates,
unless it is explicitly made persistent. In other words, a
transient instance of a persistent class behaves just like an
instance of any ordinary C++ class. A persistent instance
has a representation in both the application's memory and the
database. A persistent instance will remain even after the
application terminates unless and until it is explicitly
deleted from the database.
3.2 Declaring Persistent Objects and Values
To make a C++ class a persistent object class we declare
it as such using the db object pragma, for
example:
#pragma db object
class person
{
...
};
The other pragma that we often use is db id
which designates one of the data members as an object id, for
example:
#pragma db object
class person
{
...
#pragma db id
unsigned long id_;
};
The object id can be of a simple or composite (Section
7.2.1, "Composite Object Ids") value type. This type should be
default-constructible. It is also possible to declare a persistent
class without an object id, however, such a class will have limited
functionality (Section 12.1.6,
"no_id").
The above two pragmas are the minimum required to declare a
persistent class with an object id. Other pragmas can be used to
fine-tune the database-related properties of a class and its
members (Chapter 12, "ODB Pragma Language").
Normally, a persistent class should define the default constructor. The
generated database support code uses this constructor when
instantiating an object from the persistent state. If we add the
default constructor only for the database support code, then we
can make it private provided we also make the odb::access
class, defined in the
friend of this object class. For example:
#include
#pragma db object
class person
{
...
private:
friend class odb::access;
person () {}
};
It is also possible to have an object class without the default
constructor. However, in this case, the database operations will
only be able to load the persistent state into an existing instance
(Section 3.9, "Loading Persistent Objects",
Section 4.4, "Query Result").
The ODB compiler also needs access to the non-transient
(Section 12.4.11, "transient")
data members of a persistent class. The ODB compiler can access
such data members directly if they are public. It can also do
so if they are private or protected and the odb::access
class is declared a friend of the object type. For example:
#include
#pragma db object
class person
{
...
private:
friend class odb::access;
person () {}
#pragma db id
unsigned long id_;
std::string name_;
};
If data members are not accessible directly, then the ODB
compiler will try to automatically find suitable accessor and
modifier functions. To accomplish this, the ODB compiler will
try to lookup common accessor and modifier names derived from
the data member name. Specifically, for the name_
data member in the above example, the ODB compiler will look
for accessor functions with names: get_name(),
getName(), getname(), and just
name() as well as for modifier functions with
names: set_name(), setName(),
setname(), and just name(). You can
also add support for custom name derivations with the
--accessor-regex and --modifier-regex
ODB compiler options. Refer to the
ODB
Compiler Command Line Manual for details on these options.
The following example illustrates automatic accessor and modifier
discovery:
#pragma db object
class person
{
public:
person () {}
...
unsigned long id () const;
void id (unsigned long);
const std::string& get_name () const;
std::string& set_name ();
private:
#pragma db id
unsigned long id_; // Uses id() for access.
std::string name_; // Uses get_name()/set_name() for access.
};
Finally, if a data member is not directly accessible and the
ODB compiler was unable to discover suitable accessor and
modifier functions, then we can provide custom accessor
and modifier expressions using the db get
and db set pragmas. For more information
on custom accessor and modifier expressions refer to
Section 12.4.5,
"get/set/access".
You may be wondering whether we also have to declare value types
as persistent. We don't need to do anything special for simple value
types such as int or std::string since the
ODB compiler knows how to map them to suitable database types and
how to convert between the two. On the other hand, if a simple value
is unknown to the ODB compiler then we will need to provide the
mapping to the database type and, possibly, the code to
convert between the two. For more information on how to achieve
this refer to the db type pragma description
in Section 12.3.1, "type".
Similar to object classes, composite value types have to be
explicitly declared as persistent using the db value
pragma, for example:
#pragma db value
class name
{
...
std::string first_;
std::string last_;
};
Note that a composite value cannot have a data member designated
as an object id since, as we have discussed earlier, values do
not have a notion of identity. A composite value type also doesn't
have to define the default constructor, unless it is used as an
element of a container. The ODB compiler uses the same mechanisms
to access data members in composite value types as in object types.
Composite value types are discussed in more detail in
Section 7.2, "Composite Value Types".
3.3 Object and View Pointers
As we have seen in the previous chapter, some database operations
create dynamically allocated instances of persistent classes and
return pointers to these instances. As we will see in later chapters,
pointers are also used to establish relationships between objects
(Chapter 6, "Relationships") as well as to cache
persistent objects in a session (Chapter 10,
"Session"). While in most cases you won't need to deal with
pointers to views, it is possible to a obtain a dynamically allocated
instance of a view using the result_iterator::load()
function (Section 4.4, "Query Results").
By default, all these mechanisms use raw pointers to return
objects and views as well as to pass and cache objects. This
is normally sufficient for applications
that have simple object lifetime requirements and do not use sessions
or object relationships. In particular, a dynamically allocated object
or view that is returned as a raw pointer from a database operation
can be assigned to a smart pointer of our choice, for example
std::auto_ptr, std::unique_ptr from C++11, or
shared_ptr from TR1, C++11, or Boost.
However, to avoid any possibility of a mistake, such as forgetting
to use a smart pointer for a returned object or view, as well as to
simplify the use of more advanced ODB functionality, such as sessions
and bidirectional object relationships, it is recommended that you use
smart pointers with the sharing semantics as object pointers.
The shared_ptr smart pointer from TR1, C++11, or Boost
is a good default choice. However, if sharing is not required and
sessions are not used, then std::unique_ptr or
std::auto_ptr can be used just as well.
ODB provides several mechanisms for changing the object or view pointer
type. To specify the pointer type on the per object or per view basis
we can use the db pointer pragma, for example:
#pragma db object pointer(std::tr1::shared_ptr)
class person
{
...
};
We can also specify the default pointer for a group of objects or
views at the namespace level:
#pragma db namespace pointer(std::tr1::shared_ptr)
namespace accounting
{
#pragma db object
class employee
{
...
};
#pragma db object
class employer
{
...
};
}
Finally, we can use the --default-pointer option to specify
the default pointer for the whole file. Refer to the
ODB
Compiler Command Line Manual for details on this option's argument.
The typical usage is shown below:
--default-pointer std::tr1::shared_ptr
An alternative to this method with the same effect is to specify the
default pointer for the global namespace:
#pragma db namespace() pointer(std::tr1::shared_ptr)
Note that we can always override the default pointer specified
at the namespace level or with the command line option using
the db pointer object or view pragma. For
example:
#pragma db object pointer(std::shared_ptr)
namespace accounting
{
#pragma db object
class employee
{
...
};
#pragma db object pointer(std::unique_ptr)
class employer
{
...
};
}
Refer to Section 12.1.2, "pointer
(object)", Section 12.2.4, "pointer
(view)", and Section 12.5.1, "pointer
(namespace)" for more information on these mechanisms.
Built-in support that is provided by the ODB runtime library allows us
to use shared_ptr (TR1 or C++11),
std::unique_ptr (C++11), or std::auto_ptr as
pointer types. Plus, ODB profile libraries, that are available for
commonly used frameworks and libraries (such as Boost and Qt),
provide support for smart pointers found in these frameworks and
libraries (Part III, "Profiles"). It is also
easy to add support for our own smart pointers, as described in
Section 6.5, "Using Custom Smart Pointers".
3.4 Database
Before an application can make use of persistence services
offered by ODB, it has to create a database class instance. A
database instance is the representation of the place where
the application stores its persistent objects. We create
a database instance by instantiating one of the database
system-specific classes. For example, odb::mysql::database
would be such a class for the MySQL database system. We will
also normally pass a database name as an argument to the
class' constructor. The following code fragment
shows how we can create a database instance for the MySQL
database system:
#include
#include
auto_ptr
new odb::mysql::database (
"test_user" // database login name
"test_password" // database password
"test_database" // database name
));
The odb::database class is a common interface for
all the database system-specific classes provided by ODB. You
would normally work with the database
instance via this interface unless there is a specific
functionality that your application depends on and which is
only exposed by a particular system's database
class. You will need to include the
header file to make this class available in your application.
The odb::database interface defines functions for
starting transactions and manipulating persistent objects.
These are discussed in detail in the remainder of this chapter
as well as the next chapter which is dedicated to the topic of
querying the database for persistent objects. For details on the
system-specific database classes, refer to
Part II, "Database Systems".
Before we can persist our objects, the corresponding database schema has
to be created in the database. The schema contains table definitions and
other relational database artifacts that are used to store the state of
persistent objects in the database.
There are several ways to create the database schema. The easiest is to
instruct the ODB compiler to generate the corresponding schema from the
persistent classes (--generate-schema option). The ODB
compiler can generate the schema as a standalone SQL file,
embedded into the generated C++ code, or as a separate C++ source file
(--schema-format option). If we are using the SQL file
to create the database schema, then this file should be executed,
normally only once, before the application is started.
Alternatively, if the schema is embedded directly into the generated
code or produced as a separate C++ source file, then we can use the
odb::schema_catalog class to create it in the database
from within our application, for example:
#include
odb::transaction t (db->begin ());
odb::schema_catalog::create_schema (*db);
t.commit ();
Refer to the next section for information on the
odb::transaction class. The complete version of the above
code fragment is available in the schema/embedded example in
the odb-examples package.
The odb::schema_catalog class has the following interface.
You will need to include the
header file to make this class available in your application.
namespace odb
{
class schema_catalog
{
public:
static void
create_schema (database&, const std::string& name = "");
static bool
exists (database_id, const std::string& name);
static bool
exists (const database&, const std::string& name)
};
}
The first argument to the create_schema() function
is the database instance that we would like to create the schema in.
The second argument is the schema name. By default, the ODB
compiler generates all embedded schemas with the default schema
name (empty string). However, if your application needs to
have several separate schemas, you can use the
--schema-name ODB compiler option to assign
custom schema names and then use these names as a second argument
to create_schema(). If the schema is not found,
create_schema() throws the
odb::unknown_schema exception. You can use the
exists() function to check whether a schema for the
specified database and with the specified name exists in the
catalog. Note also that the create_schema() function
should be called within a transaction.
Finally, we can also use a custom database schema with ODB. This approach
can work similarly to the standalone SQL file described above except that
the database schema is hand-written or produced by another program. Or we
could execute custom SQL statements that create the schema directly from
our application. To map persistent classes to custom database schemas, ODB
provides a wide range of mapping customization pragmas, such
as db table, db column,
and db type (Chapter 12, "ODB Pragma
Language"). For sample code that shows how to perform such mapping
for various C++ constructs, refer to the schema/custom
example in the odb-examples package.
3.5 Transactions
A transaction is an atomic, consistent, isolated and durable
(ACID) unit of work. Database operations can only be
performed within a transaction and each thread of execution
in an application can have only one active transaction at a
time.
By atomicity we mean that when it comes to making changes to
the database state within a transaction,
either all the changes are applied or none at all. Consider,
for example, a transaction that transfers funds between two
objects representing bank accounts. If the debit function
on the first object succeeds but the credit function on
the second fails, the transaction is rolled back and the
database state of the first object remains unchanged.
By consistency we mean that a transaction must take all the
objects stored in the database from one consistent state
to another. For example, if a bank account object must
reference a person object as its owner and we forget to
set this reference before making the object persistent,
the transaction will be rolled back and the database
will remain unchanged.
By isolation we mean that the changes made to the database
state during a transaction are only visible inside this
transaction until and unless it is committed. Using the
above example with the bank transfer, the results of the
debit operation performed on the first object is not
visible to other transactions until the credit operation
is successfully completed and the transaction is committed.
By durability we mean that once the transaction is committed,
the changes that it made to the database state are permanent
and will survive failures such as an application crash. From
now on the only way to alter this state is to execute and commit
another transaction.
A transaction is started by calling either the
database::begin() or connection::begin()
function. The returned transaction handle is stored in
an instance of the odb::transaction class.
You will need to include the
header file to make this class available in your application.
For example:
#include
transaction t (db.begin ())
// Perform database operations.
t.commit ();
The odb::transaction class has the following
interface:
namespace odb
{
class transaction
{
public:
typedef odb::database database_type;
typedef odb::connection connection_type;
transaction (transaction_impl*, bool make_current = true);
void
reset (transaction_impl*, bool make_current = true);
void
commit ();
void
rollback ();
database_type&
database ();
connection_type&
connection ();
static bool
has_current ();
static transaction&
current ();
static void
current (transaction&);
static bool
reset_current ();
// Callback API.
//
...
};
}
The commit() function commits a transaction and
rollback() rolls it back. Unless the transaction
has been finalized, that is, explicitly committed or rolled
back, the destructor of the transaction class will
automatically roll it back when the transaction instance goes
out of scope. If we try to commit or roll back a finalized
transaction, the odb::transaction_already_finalized
exception is thrown.
The database() accessor returns the database this
transaction is working on. Similarly, the connection()
accessor returns the database connection this transaction is on
(Section 3.6, "Connections").
The static current() accessor returns the
currently active transaction for this thread. If there is no active
transaction, this function throws the odb::not_in_transaction
exception. We can check whether there is a transaction in effect in
this thread using the has_current() static function.
The make_current argument in the transaction
constructor as well as the static current() modifier and
reset_current() function give us additional
control over the nomination of the currently active transaction.
If we pass false as the make_current
argument, then the newly created transaction will not
automatically be made the active transaction for this
thread. Later, we can use the static current() modifier
to set this transaction as the active transaction.
The reset_current() static function clears the
currently active transaction. Together, these mechanisms
allow for more advanced use cases, such as multiplexing
two or more transactions on the same thread. For example:
transaction t1 (db1.begin ()); // Active transaction.
transaction t2 (db2.begin (), false); // Not active.
// Perform database operations on db1.
transaction::current (t2); // Deactivate t1, activate t2.
// Perform database operations on db2.
transaction::current (t1); // Switch back to t1.
// Perform some more database operations on db1.
t1.commit ();
transaction::current (t2); // Switch to t2.
// Perform some more database operations on db2.
t2.commit ();
The reset() modifier allows us to reuse the same
transaction instance to complete several database
transactions. Similar to the destructor, reset()
will roll the current transaction back if it hasn't been finalized.
Here is how we can use this function to commit the current transaction
and start a new one every time a certain number of database operations
has been performed:
transaction t (db.begin ());
for (size_t i (0); i < n; ++i)
{
// Perform a database operation, such as persist an object.
// Commit the current transaction and start a new one after
// every 100 operations.
//
if (i % 100 == 0)
{
t.commit ();
t.reset (db.begin ());
}
}
t.commit ();
For more information on the transaction callback support, refer
to Section 13.1, "Transaction Callbacks".
Note that in the above discussion of atomicity, consistency,
isolation, and durability, all of those guarantees only apply
to the object's state in the database as opposed to the object's
state in the application's memory. It is possible to roll
a transaction back but still have changes from this
transaction in the application's memory. An easy way to
avoid this potential inconsistency is to instantiate
persistent objects only within the transaction scope. Consider,
for example, these two implementations of the same transaction:
void
update_age (database& db, person& p)
{
transaction t (db.begin ());
p.age (p.age () + 1);
db.update (p);
t.commit ();
}
In the above implementation, if the update() call fails
and the transaction is rolled back, the state of the person
object in the database and the state of the same object in the
application's memory will differ. Now consider an
alternative implementation which only instantiates the
person object for the duration of the transaction:
void
update_age (database& db, unsigned long id)
{
transaction t (db.begin ());
auto_ptr
p.age (p.age () + 1);
db.update (p);
t.commit ();
}
Of course, it may not always be possible to write the
application in this style. Oftentimes we need to access and
modify the application's state of persistent objects out of
transactions. In this case it may make sense to try to
roll back the changes made to the application state if
the transaction was rolled back and the database state
remains unchanged. One way to do this is to re-load
the object's state from the database, for example:
void
update_age (database& db, person& p)
{
try
{
transaction t (db.begin ());
p.age (p.age () + 1);
db.update (p);
t.commit ();
}
catch (...)
{
transaction t (db.begin ());
db.load (p.id (), p);
t.commit ();
throw;
}
}
See also Section 13.1, "Transaction Callbacks"
for an alternative approach.
3.6 Connections
The odb::connection class represents a connection
to the database. Normally, you wouldn't work with connections
directly but rather let the ODB runtime obtain and release
connections as needed. However, certain use cases may require
obtaining a connection manually. For completeness, this section
describes the connection class and discusses some
of its use cases. You may want to skip this section if you are
reading through the manual for the first time.
Similar to odb::database, the odb::connection
class is a common interface for all the database system-specific
classes provided by ODB. For details on the system-specific
connection classes, refer to Part II,
"Database Systems".
To make the odb::connection class available in your
application you will need to include the
header file. The odb::connection class has the
following interface:
namespace odb
{
class connection
{
public:
typedef odb::database database_type;
transaction
begin () = 0;
unsigned long long
execute (const char* statement);
unsigned long long
execute (const std::string& statement);
unsigned long long
execute (const char* statement, std::size_t length);
database_type&
database ();
};
typedef details::shared_ptr
}
The begin() function is used to start a transaction
on the connection. The execute() functions allow
us to execute native database statements on the connection.
Their semantics are equivalent to the database::execute()
functions (Section 3.12, "Executing Native SQL
Statements") except that they can be legally called outside
a transaction. Finally, the database() accessor
returns a reference to the odb::database instance
to which this connection corresponds.
To obtain a connection we call the database::connection()
function. The connection is returned as odb::connection_ptr,
which is an implementation-specific smart pointer with the shared
pointer semantics. This, in particular, means that the connection
pointer can be copied and returned from functions. Once the last
instance of connection_ptr pointing to the same
connection is destroyed, the connection is returned to the
database instance. The following code fragment
shows how we can obtain, use, and release a connection:
using namespace odb::core;
database& db = ...
connection_ptr c (db.connection ());
// Temporarily disable foreign key constraints.
//
c->execute ("SET FOREIGN_KEY_CHECKS = 0");
// Start a transaction on this connection.
//
transaction t (c->begin ());
...
t.commit ();
// Restore foreign key constraints.
//
c->execute ("SET FOREIGN_KEY_CHECKS = 1");
// When 'c' goes out of scope, the connection is returned to 'db'.
Some of the use cases which may require direct manipulation of
connections include out-of-transaction statement execution,
such as the execution of connection configuration statements,
the implementation of a connection-per-thread policy, and making
sure that a set of transactions is executed on the same
connection.
3.7 Error Handling and Recovery
ODB uses C++ exceptions to report database operation errors. Most
ODB exceptions signify hard errors or errors that cannot
be corrected without some intervention from the application.
For example, if we try to load an object with an unknown object
id, the odb::object_not_persistent exception is
thrown. Our application may be able to correct this error, for
instance, by obtaining a valid object id and trying again.
The hard errors and corresponding ODB exceptions that can be
thrown by each database function are described in the remainder
of this chapter with Section 3.14, "ODB Exceptions"
providing a quick reference for all the ODB exceptions.
The second group of ODB exceptions signify soft or
recoverable errors. Such errors are temporary
failures which normally can be corrected by simply re-executing
the transaction. ODB defines three such exceptions:
odb::connection_lost, odb::timeout,
and odb::deadlock. All recoverable ODB exceptions
are derived from the common odb::recoverable base
exception which can be used to handle all the recoverable
conditions with a single catch block.
The odb::connection_lost exception is thrown if
a connection to the database is lost in the middle of
a transaction. In this situation the transaction is aborted but
it can be re-tried without any changes. Similarly, the
odb::timeout exception is thrown if one of the
database operations or the whole transaction has timed out.
Again, in this case the transaction is aborted but can be
re-tried as is.
If two or more transactions access or modify more than one object
and are executed concurrently by different applications or by
different threads within the same application, then it is possible
that these transactions will try to access objects in an incompatible
order and deadlock. The canonical example of a deadlock are
two transactions in which the first has modified object1
and is waiting for the second transaction to commit its changes to
object2 so that it can also update object2.
At the same time the second transaction has modified object2
and is waiting for the first transaction to commit its changes to
object1 because it also needs to modify object1.
As a result, none of the two transactions can be completed.
The database system detects such situations and automatically
aborts the waiting operation in one of the deadlocked transactions.
In ODB this translates to the odb::deadlock
recoverable exception being thrown from one of the database functions.
The following code fragment shows how to handle the recoverable
exceptions by restarting the affected transaction:
const unsigned short max_retries = 5;
for (unsigned short retry_count (0); ; retry_count++)
{
try
{
transaction t (db.begin ());
...
t.commit ();
break;
}
catch (const odb::recoverable& e)
{
if (retry_count > max_retries)
throw retry_limit_exceeded (e.what ());
else
continue;
}
}
3.8 Making Objects Persistent
A newly created instance of a persistent class is transient.
We use the database::persist() function template
to make a transient instance persistent. This function has four
overloaded versions with the following signatures:
template
typename object_traits
persist (const T& object);
template
typename object_traits
persist (const object_traits
template
typename object_traits
persist (T& object);
template
typename object_traits
persist (const object_traits
Here and in the rest of the manual,
object_traits
object_traits
unrestricted and constant object pointer types (Section
3.3, "Object and View Pointers"), respectively.
Similarly, object_traits
id type. The odb::object_traits template is part of the
database support code generated by the ODB compiler.
The first persist() function expects a constant reference
to an instance being persisted. The second function expects a constant
object pointer. Both of these functions can only be used on objects with
application-assigned object ids (Section 12.4.2,
"auto").
The second and third persist() functions are similar to the
first two except that they operate on unrestricted references and object
pointers. If the identifier of the object being persisted is assigned
by the database, these functions update the id member of the passed
instance with the assigned value. All four functions return the object
id of the newly persisted object.
If the database already contains an object of this type with this
identifier, the persist() functions throw the
odb::object_already_persistent exception. This should
never happen for database-assigned object ids as long as the
number of objects persisted does not exceed the value space of
the id type.
When calling the persist() functions, we don't need to
explicitly specify the template type since it will be automatically
deduced from the argument being passed. The following example shows
how we can call these functions:
person john ("John", "Doe", 33);
shared_ptr
transaction t (db.begin ());
db.persist (john);
unsigned long jane_id (db.persist (jane));
t.commit ();
cerr << "Jane's id: " << jane_id << endl;
Notice that in the above code fragment we have created instances
that we were planning to make persistent before starting the
transaction. Likewise, we printed Jane's id after we have committed
the transaction. As a general rule, you should avoid performing
operations within the transaction scope that can be performed
before the transaction starts or after it terminates. An active
transaction consumes both your application's resources, such as
a database connection, as well as the database server's
resources, such as object locks. By following the above rule you
make sure these resources are released and made available to other
threads in your application and to other applications as soon as
possible.
3.9 Loading Persistent Objects
Once an object is made persistent, and you know its object id, it
can be loaded by the application using the database::load()
function template. This function has two overloaded versions with
the following signatures:
template
typename object_traits
load (const typename object_traits
template
void
load (const typename object_traits
Given an object id, the first function allocates a new instance
of the object class in the dynamic memory, loads its state from
the database, and returns the pointer to the new instance. The
second function loads the object's state into an existing instance.
Both functions throw odb::object_not_persistent if
there is no object of this type with this id in the database.
When we call the first load() function, we need to
explicitly specify the object type. We don't need to do this for
the second function because the object type will be automatically
deduced from the second argument, for example:
transaction t (db.begin ());
auto_ptr
db.load (jane_id, *jane);
t.commit ();
In certain situations it may be necessary to reload the state
of an object from the database. While this is easy to achieve
using the second load() function, ODB provides
the database::reload() function template that
has a number of special properties. This function has two
overloaded versions with the following signatures:
template
void
reload (T& object);
template
void
reload (const object_traits
The first reload() function expects an object
reference, while the second expects an object pointer. Both
functions expect the id member in the passed object to contain
a valid object identifier and, similar to load(),
both will throw odb::object_not_persistent if
there is no object of this type with this id in the database.
The first special property of reload()
compared to the load() function is that it
does not interact with the session's object cache
(Section 10.1, "Object Cache"). That is, if
the object being reloaded is already in the cache, then it will
remain there after reload() returns. Similarly, if the
object is not in the cache, then reload() won't
put it there either.
The second special property of the reload() function
only manifests itself when operating on an object with the optimistic
concurrency model. In this case, if the states of the object
in the application memory and in the database are the same, then
no reloading will occur. For more information on optimistic
concurrency, refer to Chapter 11, "Optimistic
Concurrency".
If we don't know for sure whether an object with a given id
is persistent, we can use the find() function
instead of load(), for example:
template
typename object_traits
find (const typename object_traits
template
bool
find (const typename object_traits
If an object with this id is not found in the database, the first
find() function returns a NULL pointer
while the second function leaves the passed instance unmodified and
returns false.
If we don't know the object id, then we can use queries to
find the object (or objects) matching some criteria
(Chapter 4, "Querying the Database"). Note,
however, that loading an object's state using its
identifier can be significantly faster than executing a query.
3.10 Updating Persistent Objects
If a persistent object has been modified, we can store the updated
state in the database using the database::update()
function template. This function has three overloaded versions with
the following signatures:
template
void
update (const T& object);
template
void
update (const object_traits
template
void
update (const object_traits
The first update() function expects an object reference,
while the other two expect object pointers. If the object passed to
one of these functions does not exist in the database,
update() throws the odb::object_not_persistent
exception (but see a note on optimistic concurrency below).
Below is an example of the funds transfer that we talked about
in the earlier section on transactions. It uses the hypothetical
bank_account persistent class:
void
transfer (database& db,
unsigned long from_acc,
unsigned long to_acc,
unsigned int amount)
{
bank_account from, to;
transaction t (db.begin ());
db.load (from_acc, from);
if (from.balance () < amount)
throw insufficient_funds ();
db.load (to_acc, to);
to.balance (to.balance () + amount);
from.balance (from.balance () - amount);
db.update (to);
db.update (from);
t.commit ();
}
The same can be accomplished using dynamically allocated objects
and the update() function with object pointer argument,
for example:
transaction t (db.begin ());
shared_ptr
if (from->balance () < amount)
throw insufficient_funds ();
shared_ptr
to->balance (to->balance () + amount);
from->balance (from->balance () - amount);
db.update (to);
db.update (from);
t.commit ();
If any of the update() functions are operating on a
persistent class with the optimistic concurrency model, then they will
throw the odb::object_changed exception if the state of the
object in the database has changed since it was last loaded into the
application memory. Furthermore, for such classes, update()
no longer throws the object_not_persistent exception if
there is no such object in the database. Instead, this condition is
treated as a change of object state and object_changed
is thrown instead. For a more detailed discussion of optimistic
concurrency, refer to Chapter 11, "Optimistic
Concurrency".
In ODB, persistent classes, composite value types, as well as individual
data members can be declared read-only (see Section
12.1.4, "readonly (object)", Section
12.3.6, "readonly (composite value)", and
Section 12.4.12, "readonly
(data member)").
If an individual data member is declared read-only, then
any changes to this member will be ignored when updating the database
state of an object using any of the above update()
functions. A const data member is automatically treated
as read-only. If a composite value is declared read-only then all its
data members are treated as read-only.
If the whole object is declared read-only then the database state of
this object cannot be changed. Calling any of the above
update() functions for such an object will result in a
compile-time error.
3.11 Deleting Persistent Objects
To delete a persistent object's state from the database we use the
database::erase() or database::erase_query()
function templates. If the application still has an instance of the
erased object, this instance becomes transient. The erase()
function has the following overloaded versions:
template
void
erase (const T& object);
template
void
erase (const object_traits
template
void
erase (const object_traits
template
void
erase (const typename object_traits
The first erase() function uses an object itself, in
the form of an object reference, to delete its state from the
database. The next two functions accomplish the same result but using
object pointers. Note that all three functions leave the passed
object unchanged. It simply becomes transient. The last function
uses the object id to identify the object to be deleted. If the
object does not exist in the database, then all four functions
throw the odb::object_not_persistent exception
(but see a note on optimistic concurrency below).
We have to specify the object type when calling the last
erase() function. The same is unnecessary for the
first three functions because the object type will be automatically
deduced from their arguments. The following example shows how we
can call these functions:
person& john = ...
shared_ptr
unsigned long joe_id = ...
transaction t (db.begin ());
db.erase (john);
db.erase (jane);
db.erase
t.commit ();
If any of the erase() functions except the last one are
operating on a persistent class with the optimistic concurrency
model, then they will throw the odb::object_changed exception
if the state of the object in the database has changed since it was
last loaded into the application memory. Furthermore, for such
classes, erase() no longer throws the
object_not_persistent exception if there is no such
object in the database. Instead, this condition is treated as a
change of object state and object_changed is thrown
instead. For a more detailed discussion of optimistic concurrency,
refer to Chapter 11, "Optimistic Concurrency".
The erase_query() function allows us to delete
the state of multiple objects matching certain criteria. It uses
the query expression of the database::query() function
(Chapter 4, "Querying the Database") and,
because the ODB query facility is optional, it is only available
if the --generate-query ODB compiler option was
specified. The erase_query() function has the
following overloaded versions:
template
unsigned long long
erase_query ();
template
unsigned long long
erase_query (const odb::query
The first erase_query() function is used to delete
the state of all the persistent objects of a given type stored
in the database. The second function uses the passed query instance
to only delete the state of objects matching the query criteria.
Both functions return the number of objects erased. When calling
the erase_query() function, we have to explicitly
specify the object type we are erasing. For example:
typedef odb::query
transaction t (db.begin ());
db.erase_query
t.commit ();
Unlike the query() function, when calling
erase_query() we cannot use members from pointed-to
objects in the query expression. However, we can still use
a member corresponding to a pointer as an ordinary object
member that has the id type of the pointed-to object
(Chapter 6, "Relationships"). This allows us
to compare object ids as well as test the pointer for
NULL. As an example, the following transaction
makes sure that all the employee objects that
reference an employer object that is about to
be deleted are deleted as well. Here we assume that the
employee class contains a pointer to the
employer class. Refer to Chapter 6,
"Relationships" for complete definitions of these
classes.
typedef odb::query
transaction t (db.begin ());
employer& e = ... // Employer object to be deleted.
db.erase_query
db.erase (e);
t.commit ();
3.12 Executing Native SQL Statements
In some situations we may need to execute native SQL statements
instead of using the object-oriented database API described above.
For example, we may want to tune the database schema generated
by the ODB compiler or take advantage of a feature that is
specific to the database system we are using. The
database::execute() function, which has three
overloaded versions, provides this functionality:
unsigned long long
execute (const char* statement);
unsigned long long
execute (const std::string& statement);
unsigned long long
execute (const char* statement, std::size_t length)
The first execute() function expects the SQL statement
as a zero-terminated C-string. The last version expects the explicit
statement length as the second argument and the statement itself
may contain '\0' characters, for example, to represent
binary data, if the database system supports it. All three functions
return the number of rows that were affected by the statement. For
example:
transaction t (db.begin ());
db.execute ("DROP TABLE test");
db.execute ("CREATE TABLE test (n INT PRIMARY KEY)");
t.commit ();
While these functions must always be called within a transaction,
it may be necessary to execute a native statement outside a
transaction. This can be done using the
connection::execute() functions as described in
Section 3.6, "Connections".
3.13 Tracing SQL Statement Execution
Oftentimes it is useful to understand what SQL statements are
executed as a result of high-level database operations. For
example, we can use this information to figure out why certain
transactions don't produce desired results or why they take
longer than expected.
While this information can usually be obtained from the database
logs, ODB provides an application-side SQL statement tracing
support that is both more convenient and finer-grained.
For example, in a typical situation that calls for tracing
we would like to see the SQL statements executed as a result
of a specific transaction. While it may be difficult to
extract such a subset of statements from the database logs,
it is easy to achieve with ODB tracing support:
transaction t (db.begin ());
t.tracer (stderr_tracer);
...
t.commit ();
ODB allows us to specify a tracer on the database, connection,
and transaction levels. If specified for the database, then
all the statements executed on this database will be traced.
On the other hand, if a tracer is specified for the
connection, then only the SQL statements executed on this
connection will be traced. Similarly, a tracer specified
for a transaction will only show statements that are
executed as part of this transaction. All three classes
(odb::database, odb::connection,
and odb::transaction) provide the identical
tracing API:
void
tracer (odb::tracer&);
void
tracer (odb::tracer*);
odb::tracer*
tracer () const;
The first two tracer() functions allow us to set
the tracer object with the second one allowing us to clear the
current tracer by passing a NULL pointer. The
last tracer() function allows us to get the
current tracer object. It returns a NULL pointer
if there is no tracer in effect. Note that the tracing API
does not manage the lifetime of the tracer object. The tracer
should be valid for as long as it is being used. Furthermore,
the tracing API is not thread-safe. Trying to set a tracer
from multiple threads simultaneously will result in
undefined behavior.
The odb::tracer class defines a callback interface
that can be used to create custom tracer implementations. The
odb::stderr_tracer is a built-in tracer implementation
provided by the ODB runtime. It prints each executed SQL statement
to the standard error stream.
The odb::tracer class is defined in the
include in order to make this class available in your application.
The odb::tracer interface provided the following
callback functions:
namespace odb
{
class tracer
{
public:
virtual void
prepare (connection&, const statement&);
virtual void
execute (connection&, const statement&);
virtual void
execute (connection&, const char* statement) = 0;
virtual void
deallocate (connection&, const statement&);
};
}
The prepare() and deallocate() functions
are called when a prepared statement is created and destroyed,
respectively. The first execute() function is called
when a prepared statement is executed while the second one is called
when a normal statement is executed. The default implementations
for the prepare() and deallocate()
functions do nothing while the first execute() function
calls the second one passing the statement text as the second
argument. As a result, if all you are interested in are the
SQL statements being executed, then you only need to override the
second execute() function.
In addition to the common odb::tracer interface,
each database runtime provides a database-specific version
as odb::
the same interface as the common version except that the
connection and statement types
are database-specific, which gives us access to additional,
database-specific information.
As an example, consider a more elaborate, PostgreSQL-specific
tracer implementation. Here we rely on the fact that the PostgreSQL
ODB runtime uses names to identify prepared statements and this
information can be obtained from the odb::pgsql::statement
object:
#include
#include
#include
#include
class pgsql_tracer: public odb::pgsql::tracer
{
virtual void
prepare (odb::pgsql::connection& c, const odb::pgsql::statement& s)
{
cerr << c.database ().db () << ": PREPARE " << s.name ()
<< " AS " << s.text () << endl;
}
virtual void
execute (odb::pgsql::connection& c, const odb::pgsql::statement& s)
{
cerr << c.database ().db () << ": EXECUTE " << s.name () << endl;
}
virtual void
execute (odb::pgsql::connection& c, const char* statement)
{
cerr << c.database ().db () << ": " << statement << endl;
}
virtual void
deallocate (odb::pgsql::connection& c, const odb::pgsql::statement& s)
{
cerr << c.database ().db () << ": DEALLOCATE " << s.name () << endl;
}
};
Note also that you can only set a database-specific tracer object
using a database-specific database instance, for example:
pgsql_tracer tracer;
odb::database& db = ...;
db.tracer (tracer); // Compile error.
odb::pgsql::database& db = ...;
db.tracer (tracer); // Ok.
3.14 ODB Exceptions
In the previous sections we have already mentioned some of the
exceptions that can be thrown by the database functions. In this
section we will discuss the ODB exception hierarchy and document
all the exceptions that can be thrown by the common ODB
runtime.
The root of the ODB exception hierarchy is the abstract
odb::exception class. This class derives
from std::exception and has the following
interface:
namespace odb
{
struct exception: std::exception
{
virtual const char*
what () const throw () = 0;
};
}
Catching this exception guarantees that we will catch all the
exceptions thrown by ODB. The what() function
returns a human-readable description of the condition that
triggered the exception.
The concrete exceptions that can be thrown by ODB are presented
in the following listing:
namespace odb
{
struct null_pointer: exception
{
virtual const char*
what () const throw ();
};
// Transaction exceptions.
//
struct already_in_transaction: exception
{
virtual const char*
what () const throw ();
};
struct not_in_transaction: exception
{
virtual const char*
what () const throw ();
};
struct transaction_already_finalized: exception
{
virtual const char*
what () const throw ();
};
// Session exceptions.
//
struct already_in_session: exception
{
virtual const char*
what () const throw ();
};
struct not_in_session: exception
{
virtual const char*
what () const throw ();
};
struct session_required: exception
{
virtual const char*
what () const throw ();
};
// Database operations exceptions.
//
struct recoverable: exception
{
};
struct connection_lost: recoverable
{
virtual const char*
what () const throw ();
};
struct timeout: recoverable
{
virtual const char*
what () const throw ();
};
struct deadlock: recoverable
{
virtual const char*
what () const throw ();
};
struct object_not_persistent: exception
{
virtual const char*
what () const throw ();
};
struct object_already_persistent: exception
{
virtual const char*
what () const throw ();
};
struct object_changed: exception
{
virtual const char*
what () const throw ();
};
struct result_not_cached: exception
{
virtual const char*
what () const throw ();
};
struct database_exception: exception
{
};
// Polymorphism support exceptions.
//
struct abstract_class: exception
{
virtual const char*
what () const throw ();
};
struct no_type_info: exception
{
virtual const char*
what () const throw ();
};
// Prepared query support exceptions.
//
struct prepared_already_cached: exception
{
const char*
name () const;
virtual const char*
what () const throw ();
};
struct prepared_type_mismatch: exception
{
const char*
name () const;
virtual const char*
what () const throw ();
};
// Schema catalog exceptions.
//
struct unknown_schema: exception
{
const std::string&
name () const;
virtual const char*
what () const throw ();
};
}
The null_pointer exception is thrown when a
pointer to a persistent object declared non-NULL
with the db not_null or
db value_not_null pragma has the NULL
value. See Chapter 6, "Relationships" for details.
The next three exceptions (already_in_transaction,
not_in_transaction,
transaction_already_finalized) are thrown by the
odb::transaction class and are discussed
in Section 3.5, "Transactions".
The next two exceptions (already_in_session, and
not_in_session) are thrown by the odb::session
class and are discussed in Chapter 10, "Session".
The session_required exception is thrown when ODB detects
that correctly loading a bidirectional object relationship requires a
session but one is not used. See Section 6.2,
"Bidirectional Relationships" for more information on this
exception.
The recoverable exception serves as a common base
for all the recoverable exceptions, which are: connection_lost,
timeout, and deadlock. The
connection_lost exception is thrown when a connection
to the database is lost. Similarly, the timeout exception
is thrown if one of the database operations or the whole transaction
has timed out. The deadlock exception is thrown when a
transaction deadlock is detected by the database system. These
exceptions can be thrown by any database function. See
Section 3.7, "Error Handling and Recovery"
for details.
The object_already_persistent exception is thrown
by the persist() database function. See
Section 3.8, "Making Objects Persistent"
for details.
The object_not_persistent exception is thrown
by the load(), update(), and
erase() database functions. Refer to
Section 3.9, "Loading Persistent Objects",
Section 3.10, "Updating Persistent Objects", and
Section 3.11, "Deleting Persistent Objects" for
more information.
The object_changed exception is thrown
by the update() database function and certain
erase() database functions when
operating on objects with the optimistic concurrency model. See
Chapter 11, "Optimistic Concurrency" for details.
The result_not_cached exception is thrown by
the query result class. Refer to Section 4.4,
"Query Result" for details.
The database_exception exception is a base class for all
database system-specific exceptions that are thrown by the
database system-specific runtime library. Refer to Part
II, "Database Systems" for more information.
The abstract_class exception is thrown by the database
functions when we attempt to persist, update, load, or erase an
instance of a polymorphic abstract class. For more information
on abstract classes, refer to Section 12.1.3,
"abstract".
The no_type_info exception is thrown by the database
functions when we attempt to persist, update, load, or erase an
instance of a polymorphic class for which no type information
is present in the application. This normally means that the
generated database support code for this class has not been
linked (or dynamically loaded) into the application or the
discriminator value has not been mapped to a persistent
class. For more information on polymorphism support, refer to
Section 8.2, "Polymorphism Inheritance".
The prepared_already_cached exception is thrown by the
cache_query() function if a prepared query with the
specified name is already cached. The prepared_type_mismatch
exception is thrown by the lookup_query() function if
the specified prepared query object type or parameters type
does not match the one in the cache. Refer to Section
4.5, "Prepared Queries" for details.
The unknown_schema exception is thrown by the
odb::schema_catalog class if a schema with the specified
name is not found. Refer to Section 3.4, "Database"
for details.
The odb::exception class is defined in the
concrete ODB exceptions are defined in
need to include either of these two headers because they are
automatically included by
However, if the source file that handles ODB exceptions
does not include
you will need to explicitly include one of these headers.
4 Querying the Database
If we don't know the identifiers of the objects that we are looking
for, we can use queries to search the database for objects matching
certain criteria. The ODB query facility is optional and we need to
explicitly request the generation of the necessary database support
code with the --generate-query ODB compiler option.
ODB provides a flexible query API that offers two distinct levels of
abstraction from the database system query language such as SQL.
At the high level we are presented with an easy to use yet powerful
object-oriented query language, called ODB Query Language. This
query language is modeled after and is integrated into C++ allowing
us to write expressive and safe queries that look and feel like
ordinary C++. We have already seen examples of these queries in the
introductory chapters. Below is another, more interesting, example:
typedef odb::query
typedef odb::result
unsigned short age;
query q (query::first == "John" && query::age < query::_ref (age));
for (age = 10; age < 100; age += 10)
{
result r (db.query
...
}
At the low level, queries can be written as predicates using
the database system-native query language such as the
WHERE predicate from the SQL SELECT
statement. This language will be referred to as native query
language. At this level ODB still takes care of converting
query parameters from C++ to the database system format. Below
is the re-implementation of the above example using SQL as
the native query language:
query q ("first = 'John' AND age = " + query::_ref (age));
Note that at this level we lose the static typing of
query expressions. For example, if we wrote something like this:
query q (query::first == 123 && query::agee < query::_ref (age));
We would get two errors during the C++ compilation. The first would
indicate that we cannot compare query::first to an
integer and the second would pick the misspelling in
query::agee. On the other hand, if we wrote something
like this:
query q ("first = 123 AND agee = " + query::_ref (age));
It would compile fine and would trigger an error only when executed
by the database system.
We can also combine the two query languages in a single query, for
example:
query q ("first = 'John' AND" + (query::age < query::_ref (age)));
4.1 ODB Query Language
An ODB query is an expression that tells the database system whether
any given object matches the desired criteria. As such, a query expression
always evaluates as true or false. At
the higher level, an expression consists of other expressions
combined with logical operators such as && (AND),
|| (OR), and ! (NOT). For example:
typedef odb::query
query q (query::first == "John" || query::age == 31);
At the core of every query expression lie simple expressions which
involve one or more object members, values, or parameters. To
refer to an object member we use an expression such as
query::first above. The names of members in the
query class are derived from the names of data members
in the object class by removing the common member name decorations,
such as leading and trailing underscores, the m_ prefix,
etc.
In a simple expression an object member can be compared to a value,
parameter, or another member using a number of predefined operators
and functions. The following table gives an overview of the available
expressions:
Operator
Description
Example
==
equal
query::age == 31
!=
unequal
query::age != 31
<
less than
query::age < 31
>
greater than
query::age > 31
<=
less than or equal
query::age <= 31
>=
greater than or equal
query::age >= 31
in()
one of the values
query::age.in (30, 32, 34)
in_range()
one of the values in range
query::age.in_range (begin, end)
is_null()
value is NULL
query::age.is_null ()
is_not_null()
value is not NULL
query::age.is_not_null ()
The in() function accepts a maximum of five arguments.
Use the in_range() function if you need to compare
to more than five values. This function accepts a pair of
standard C++ iterators and compares to all the values from
the begin position inclusive and until and
excluding the end position. The following
code fragment shows how we can use these functions:
std::vector
names.push_back ("John");
names.push_back ("Jack");
names.push_back ("Jane");
query q1 (query::first.in ("John", "Jack", "Jane"));
query q2 (query::first.in_range (names.begin (), names.end ()));
The operator precedence in the query expressions are the same
as for equivalent C++ operators. We can use parentheses to
make sure the expression is evaluated in the desired order.
For example:
query q ((query::first == "John" || query::first == "Jane") &&
query::age < 31);
4.2 Parameter Binding
An instance of the odb::query class encapsulates two
parts of information about the query: the query expression and
the query parameters. Parameters can be bound to C++ variables
either by value or by reference.
If a parameter is bound by value, then the value for this parameter
is copied from the C++ variable to the query instance at the query
construction time. On the other hand, if a parameter is bound by
reference, then the query instance stores a reference to the
bound variable. The actual value of the parameter is only extracted
at the query execution time. Consider, for example, the following
two queries:
string name ("John");
query q1 (query::first == query::_val (name));
query q2 (query::first == query::_ref (name));
name = "Jane";
db.query
db.query
The odb::query class provides two special functions,
_val() and _ref(), that allow us to
bind the parameter either by value or by reference, respectively.
In the ODB query language, if the binding is not specified
explicitly, the value semantic is used by default. In the
native query language, binding must always be specified
explicitly. For example:
query q1 (query::age < age); // By value.
query q2 (query::age < query::_val (age)); // By value.
query q3 (query::age < query::_ref (age)); // By reference.
query q4 ("age < " + age); // Error.
query q5 ("age < " + query::_val (age)); // By value.
query q6 ("age < " + query::_ref (age)); // By reference.
A query that only has by-value parameters does not depend on any
other variables and is self-sufficient once constructed. A query
that has one or more by-reference parameters depends on the
bound variables until the query is executed. If one such variable
goes out of scope and we execute the query, the behavior is
undefined.
4.3 Executing a Query
Once we have the query instance ready and by-reference parameters
initialized, we can execute the query using the
database::query() function template. It has two
overloaded versions:
template
result
query (bool cache = true);
template
result
query (const odb::query
The first query() function is used to return all the
persistent objects of a given type stored in the database.
The second function uses the passed query instance to only return
objects matching the query criteria. The cache argument
determines whether the objects' states should be cached in the
application's memory or if they should be returned by the database
system one by one as the iteration over the result progresses. The
result caching is discussed in detail in the next section.
When calling the query() function, we have to
explicitly specify the object type we are querying. For example:
typedef odb::query
typedef odb::result
result all (db.query
result johns (db.query
Note that it is not required to explicitly create a named
query variable before executing it. For example, the following
two queries are equivalent:
query q (query::first == "John");
result r1 (db.query
result r1 (db.query
Normally, we would create a named query instance if we are
planning to run the same query multiple times and would use the
in-line version for those that are executed only once (see also
Section 4.5, "Prepared Queries" for a more
optimal way to re-execute the same query multiple times). A named
query instance that does not have any by-reference parameters is
immutable and can be shared between multiple threads without
synchronization. On the other hand, a query instance with
by-reference parameters is modified every time it is executed.
If such a query is shared among multiple threads, then access
to this query instance must be synchronized from the execution
point and until the completion of the iteration over the result.
It is also possible to create queries from other queries by
combining them using logical operators. For example:
result
find_minors (database& db, const query& name_query)
{
return db.query
}
result r (find_minors (db, query::first == "John"));
4.4 Query Result
The result of executing a query is zero, one, or more objects
matching the query criteria. The result is returned as an instance
of the odb::result class template, for example:
typedef odb::query
typedef odb::result
result johns (db.query
It is best to view an instance of odb::result
as a handle to a stream, such as a file stream. While we can
make a copy of a result or assign one result to another, the
two instances will refer to the same result stream. Advancing
the current position in one instance will also advance it in
another. The result instance is only usable within the transaction
it was created in. Trying to manipulate the result after the
transaction has terminated leads to undefined behavior.
The odb::result class template conforms to the
standard C++ sequence requirements and has the following
interface:
namespace odb
{
template
class result
{
public:
typedef odb::result_iterator
public:
result ();
result (const result&);
result&
operator= (const result&);
void
swap (result&)
public:
iterator
begin ();
iterator
end ();
public:
void
cache ();
bool
empty () const;
std::size_t
size () const;
};
}
The default constructor creates an empty result set. The
cache() function caches the returned objects'
state in the application's memory. We have already mentioned
result caching when we talked about query execution. As you
may remember the database::query() function
caches the result unless instructed not to by the caller.
The cache() function allows us to
cache the result at a later stage if it wasn't already
cached during query execution.
If the result is cached, the database state of all the returned
objects is stored in the application's memory. Note that
the actual objects are still only instantiated on demand
during result iteration. It is the raw database state that
is cached in memory. In contrast, for uncached results
the object's state is sent by the database system one object
at a time as the iteration progresses.
Uncached results can improve the performance of both the application
and the database system in situations where we have a large
number of objects in the result or if we will only examine
a small portion of the returned objects. However, uncached
results have a number of limitations. There can only be one
uncached result in a transaction. Creating another result
(cached or uncached) by calling database::query()
will invalidate the existing uncached result. Furthermore,
calling any other database functions, such as update()
or erase() will also invalidate the uncached result.
The empty() function returns true if
there are no objects in the result and false otherwise.
The size() function can only be called for cached results.
It returns the number of objects in the result. If we call this
function on an uncached result, the odb::result_not_cached
exception is thrown.
To iterate over the objects in a result we use the
begin() and end() functions
together with the odb::result
type, for example:
result r (db.query
for (result::iterator i (r.begin ()); i != r.end (); ++i)
{
...
}
In C++11 we can use the auto-typed variabe instead
of spelling the iterator type explicitly, for example:
for (auto i (r.begin ()); i != r.end (); ++i)
{
...
}
The C++11 range-based for-loop can be used to further
simplify the iteration:
for (person& p: r)
{
...
}
The result iterator is an input iterator which means that the
only two position operations that it supports are to move to the
next object and to determine whether the end of the result stream
has been reached. In fact, the result iterator can only be in two
states: the current position and the end position. If we have
two iterators pointing to the current position and then we
advance one of them, the other will advance as well. This,
for example, means that it doesn't make sense to store an
iterator that points to some object of interest in the result
stream with the intent of dereferencing it after the iteration
is over. Instead, we would need to store the object itself.
The result iterator has the following dereference functions
that can be used to access the pointed-to object:
namespace odb
{
template
class result_iterator
{
public:
T*
operator-> () const;
T&
operator* () const;
typename object_traits
load ();
void
load (T& x);
typename object_traits
id ();
};
}
When we call the * or -> operator,
the iterator will allocate a new instance of the object class
in the dynamic memory, load its state from the database
state, and return a reference or pointer to the new instance. The
iterator maintains the ownership of the returned object and will
return the same pointer for subsequent calls to either of these
operators until it is advanced to the next object or we call
the first load() function (see below). For example:
result r (db.query
for (result::iterator i (r.begin ()); i != r.end ();)
{
cout << i->last () << endl; // Create an object.
person& p (*i); // Reference to the same object.
cout << p.age () << endl;
++i; // Free the object.
}
The overloaded result_iterator::load() functions are
similar to database::load(). The first function
returns a dynamically allocated instance of the current
object. As an optimization, if the iterator already owns an object
as a result of an earlier
call to the * or -> operator, then it
relinquishes the ownership of this object and returns it instead.
This allows us to write code like this without worrying about
a double allocation:
result r (db.query
for (result::iterator i (r.begin ()); i != r.end (); ++i)
{
if (i->last == "Doe")
{
auto_ptr p (i.load ());
...
}
}
Note, however, that because of this optimization, a subsequent
to load() call to the * or ->
operator results in the allocation of a new object.
The second load() function allows
us to load the current object's state into an existing instance.
For example:
result r (db.query
person p;
for (result::iterator i (r.begin ()); i != r.end (); ++i)
{
i.load (p);
cout << p.last () << endl;
cout << i.age () << endl;
}
The id() function return the object id of the current
object. While we can achieve the same by loading the object and getting
its id, this function is more efficient since it doesn't actually
create the object. This can be useful when all we need is the object's
identifier. For example:
std::set
result r (db.query
for (result::iterator i (r.begin ()); i != r.end (); ++i)
{
if (set.find (i.id ()) != set.end ()) // No object loaded.
{
cout << i->first () << endl; // Object loaded.
}
}
4.5 Prepared Queries
Most modern relational database systems have the notion of a prepared
statement. Prepared statements allow us to perform the potentially
expensive tasks of parsing SQL, preparing the query execution
plan, etc., once and then executing the same query multiple
times, potentially using different values for parameters in
each execution.
In ODB all the non-query database operations such as
persist(), load(), update(),
etc., are implemented in terms of prepared statements that are
cached and reused. While the query() database
operation also uses the prepared statement, this statement
is not cached or reused by default since ODB has no knowledge
of whether a query will be executed multiple times or only
once. Instead, ODB provides a mechanism, called prepared queries,
that allows us to prepare a query once and execute it multiple
times. In other words, ODB prepared queries are a thin wrapper
around the underlying database's prepared statement functionality.
In most cases ODB shields the application developer from database
connection management and multi-threading issues. However, when it
comes to prepared queries, a basic understanding of how ODB manages
these aspects is required. Conceptually, the odb::database
class represents a specific database, that is, a data store. However,
underneath, it maintains one or more connections to this database.
A connection can be used only by a single thread at a time. When
we start a transaction (by calling database::begin()),
the transaction instance obtains a connection and holds on to it
until the transaction is committed or rolled back. During this time
no other thread can use this connection. When the transaction
releases the connection, it may be closed or reused by another
transaction in this or another thread. What exactly happens to
a connection after it has been released depends on the connection
factory that is used by the odb::database instance.
For more information on connection factories, refer to
Part II, "Database Systems".
A query prepared on one connection cannot be executed on another.
In other words, a prepared query is associated with the connection.
One important implication of this restriction is that we cannot
prepare a query in one transaction and then try to execute it
in another without making sure that both transactions use the
same connection.
To enable the prepared query functionality we need to specify
the --generate-prepared ODB compiler option. If
we are planning to always prepare our queries, then we can
disable the once-off query execution support by also specifying
the --omit-unprepared option.
To prepare a query we use the prepare_query() function
template. This function can be called on both the odb::database
and odb::connection instances. The odb::database
version simply obtains the connection used by the currently active
transaction and calls the corresponding odb::connection
version. If no transaction is currently active, then this function
throws the odb::not_in_transaction exception
(Section 3.5, "Transactions"). The
prepare_query() function has the following signature:
template
prepared_query
prepare_query (const char* name, const odb::query
The first argument to the prepare_query() function is
the prepared query name. This name is used as a key for prepared
query caching (discussed later) and must be unique. For some databases,
notably PostgreSQL, it is also used as a name of the underlying prepared
statement. The name "object_query" (for example,
"person_query") is reserved for the once-off queries
executed by the database::query() function. Note that
the prepare_query() function makes only a shallow copy
of this argument, which means that the name must be valid for the
lifetime of the returned prepared_query instance.
The second argument to the prepare_query() function
is the query criteria. It has the same semantics as in the
query() function discussed in Section
4.3, "Executing a Query". Similar to query(), we
also have to explicitly specify the object type that we will be
querying. For example:
typedef odb::query
typedef odb::prepared_query
prep_query pq (
db.prepare_query
The result of executing the prepare_query() function is
the prepared_query instance that represent the prepared
query. It is best to view prepared_query as a handle to
the underlying prepared statement. While we can make a copy of it or
assign one prepared_query to another, the two instances
will refer to the same prepared statement. Once the last instance of
prepared_query referencing a specific prepared statement
is destroyed, this statement is released. The prepared_query
class template has the following interface:
namespace odb
{
template
struct prepared_query
{
prepared_query ();
prepared_query (const prepared_query&)
prepared_query& operator= (const prepared_query&)
result
execute (bool cache = true);
const char*
name () const;
statement&
statement () const;
operator unspecified_bool_type () const;
};
}
The default constructor creates an empty prepared_query
instance, that is, an instance that does not reference a prepared
statement and therefore cannot be executed. The only way to create
a non-empty prepared query is by calling the prepare_query()
function discussed above. To test whether the prepared query is empty,
we can use the implicit conversion operator to a boolean type. For
example:
prepared_query
if (pq)
{
// Not empty.
...
}
The execute() function executes the query and returns
the result instance. The cache argument indicates
whether the result should be cached and has the same semantics
as in the query() function. In fact, conceptually,
prepare_query() and execute() are just
the query() function split into two:
prepare_query() takes the first
query() argument (the query condition) while
execute() takes the second (the cache flag). Note
also that re-executing a prepared query invalidates the
previous execution result, whether cached or uncached.
The name() function returns the prepared query name.
This is the same name as was passed as the first argument in the
prepare_query() call. The statement()
function returns a reference to the underlying prepared statement.
Note also that calling any of these functions on an empty
prepared_query instance results in undefined behavior.
The simplest use-case for a prepared query is the need to
execute the same query multiple times within a single transaction.
Consider the following example that queries for people that are older
than a number of different ages. This and subsequent code fragments
are taken from the prepared example in the
odb-examples package.
typedef odb::query
typedef odb::prepared_query
typedef odb::result
transaction t (db.begin ());
unsigned short age;
query q (query::age > query::_ref (age));
prep_query pq (db.prepare_query
for (age = 90; age > 40; age -= 10)
{
result r (pq.execute ());
...
}
t.commit ();
Another scenario is the need to reuse the same query in multiple
transactions that are executed at once. As was mentioned above,
in this case we need to make sure that the prepared query and
all the transactions use the same connection. Consider an
alternative version of the above example that executes each
query in a separate transaction:
connection_ptr conn (db.connection ());
unsigned short age;
query q (query::age > query::_ref (age));
prep_query pq (conn->prepare_query
for (age = 90; age > 40; age -= 10)
{
transaction t (conn->begin ());
result r (pq.execute ());
...
t.commit ();
}
Note that with this approach we hold on to the database connection
until all the transactions involving the prepared query are
executed. In particular, this means that while we are busy, the
connection cannot be reused by another thread. Therefore, this
approach is only recommended if all the transactions are executed
close to each other. Also note that an uncached (see below)
prepared query is invalidated once we release the connection
on which it was prepared.
If we need to reuse a prepared query in transactions that are
executed at various times, potentially in different threads, then
the recommended approach is to cache the prepared query on the
connection. To support this functionality the odb::database
and odb::connection classes provide the following
function templates. Similar to prepare_query(),
the odb::database versions of the below
functions call the corresponding odb::connection
versions using the currently active transaction to resolve
the connection.
template
void
cache_query (const prepared_query
template
void
cache_query (const prepared_query
std::[auto|unique]_ptr
template
prepared_query
lookup_query (const char* name) const;
template
prepared_query
lookup_query (const char* name, P*& params) const;
The cache_query() function caches the passed prepared
query on the connection. The second overloaded version of
cache_query() also takes a pointer to the
by-reference query parameters. In C++98/03 it should be
std::auto_ptr while in C++11 std::auto_ptr
or std::unique_ptr can be used. The
cache_query() function assumes ownership of the
passed params argument. If a prepared query
with the same name is already cached on this connection,
then the odb::prepared_already_cached exception
is thrown.
The lookup_query() function looks up a previously
cached prepared query given its name. The second overloaded
version of lookup_query() also returns a pointer
to the by-reference query parameters. If a prepared query
with this name has not been cached, then an empty
prepared_query instance is returned. If a
prepared query with this name has been cached but either
the object type or the parameters type does not match
that which was cached, then the odb::prepared_type_mismatch
exception is thrown.
As a first example of the prepared query cache functionality,
consider the case that does not use any by-reference parameters:
for (unsigned short i (0); i < 5; ++i)
{
transaction t (db.begin ());
prep_query pq (db.lookup_query
if (!pq)
{
pq = db.prepare_query
"person-val-age-query", query::age > 50);
db.cache_query (pq);
}
result r (pq.execute ());
...
t.commit ();
// Do some other work.
//
...
}
The following example shows how to do the same but for a query that
includes by-reference parameters. In this case the parameters are
cached together with the prepared query.
for (unsigned short age (90); age > 40; age -= 10)
{
transaction t (db.begin ());
unsigned short* age_param;
prep_query pq (
db.lookup_query
if (!pq)
{
auto_ptr
age_param = p.get ();
query q (query::age > query::_ref (*age_param));
pq = db.prepare_query
db.cache_query (pq, p); // Assumes ownership of p.
}
*age_param = age; // Initialize the parameter.
result r (pq.execute ());
...
t.commit ();
// Do some other work.
//
...
}
As is evident from the above examples, when we use a prepared
query cache, each transaction that executes a query must also
include code that prepares and caches this query if it hasn't already
been done. If a prepared query is used in a single place in the
application, then this is normally not an issue since all the
relevant code is kept in one place. However, if the same query
is used in several different places in the application, then
we may end up duplicating the same preparation and caching
code, which makes it hard to maintain.
To resolve this issue ODB allows us to register a prepared
query factory that will be called to prepare and cache a
query during the call to lookup_query(). To
register a factory we use the database::query_factory()
function. In C++98/03 it has the following signature:
void
query_factory (const char* name,
void (*factory) (const char* name, connection&));
While in C++11 it uses the std::function class
template:
void
query_factory (const char* name,
std::function
The first argument to the query_factory() function is
the prepared query name that this factory will be called to prepare
and cache. An empty name is treated as a fallback wildcard factory
that is capable of preparing any query. The second argument is the
factory function or, in C++11, function object or lambda.
The example fragment shows how we can use the prepared query
factory:
struct params
{
unsigned short age;
string first;
};
static void
query_factory (const char* name, connection& c)
{
auto_ptr
query q (query::age > query::_ref (p->age) &&
query::first == query::_ref (p->first));
prep_query pq (c.prepare_query
c.cache_query (pq, p);
}
db.query_factory ("person-age-name-query", &query_factory);
for (unsigned short age (90); age > 40; age -= 10)
{
transaction t (db.begin ());
params* p;
prep_query pq (db.lookup_query
assert (pq);
p->age = age;
p->first = "John";
result r (pq.execute ());
...
t.commit ();
}
In C++11 we could have instead used a lambda function as well as
unique_ptr rather than auto_ptr:
db.query_factory (
"person-age-name-query",
[] (const char* name, connection& c)
{
unique_ptr
query q (query::age > query::_ref (p->age) &&
query::first == query::_ref (p->first));
prep_query pq (c.prepare_query
c.cache_query (pq, std::move (p));
});
5 Containers
The ODB runtime library provides built-in persistence support for all the
commonly used standard C++98/03 containers, namely,
std::vector, std::list, std::set,
std::multiset, std::map, and
std::multimap as well as C++11 std::array,
std::forward_list, std::unordered_set,
std::unordered_multiset, std::unordered_map,
and std::unordered_multimap.
Plus, ODB profile libraries, that are
available for commonly used frameworks and libraries (such as Boost and
Qt), provide persistence support for containers found in these frameworks
and libraries (Part III, "Profiles"). Both the
ODB runtime library and profile libraries also provide a number of
change-tracking container equivalents which can be used to minimize
the number of database operations necessary to synchronize the container
state with the database (Section 5.4, "Change-Tracking
Containers"). It is also easy to persist custom container types
as discussed later in Section 5.5, "Using Custom
Containers".
We don't need to do anything special to declare a member of a
container type in a persistent class. For example:
#pragma db object
class person
{
...
private:
std::vector
...
};
The complete version of the above code fragment and the other code
samples presented in this chapter can be found in the container
example in the odb-examples package.
A data member in a persistent class that is of a container type
behaves like a value type. That is, when an object is made persistent,
the elements of the container are stored in the database. Similarly,
when a persistent object is loaded from the database, the contents
of the container are automatically loaded as well. A data member
of a container type can also use a smart pointer, as discussed
in Section 7.3, "Pointers and NULL
Value Semantics".
While an ordinary member is mapped to one or more columns in the
object's table, a member of a container type is mapped to a separate
table. The exact schema of such a table depends on the kind of
container. ODB defines the following container kinds: ordered,
set, multiset, map, and multimap. The container kinds and the
contents of the tables to which they are mapped are discussed
in detail in the following sections.
Containers in ODB can contain simple value types (Section
7.1, "Simple Value Types"), composite value types
(Section 7.2, "Composite Value Types"), and pointers
to objects (Chapter 6, "Relationships"). Containers of
containers, either directly or indirectly via a composite value
type, are not allowed. A key in a map or multimap container can
be a simple or composite value type but not a pointer to an object.
An index in the ordered container should be a simple integer value
type.
The value type in the ordered, set, and map containers as well as
the key type in the map containers should be default-constructible.
The default constructor in these types can be made private in which
case the odb::access class should be made a friend of
the value or key type. For example:
#pragma db value
class name
{
public:
name (const std::string&, const std::string&);
...
private:
friend class odb::access;
name ();
...
};
#pragma db object
class person
{
...
private:
std::vector
...
};
5.1 Ordered Containers
In ODB an ordered container is any container that maintains (explicitly
or implicitly) an order of its elements in the form of an integer index.
Standard C++ containers that are ordered include std::vector
and std::list as well as C++11 std::array and
std::forward_list. While elements in std::set
are also kept in a specific order, this order is not based on an
integer index but rather on the relationship between elements. As
a result, std::set is not considered an ordered
container for the purpose of persistence.
The database table for an ordered container consists of at least
three columns. The first column contains the object id of a
persistent class instance of which the container is a member.
The second column contains the element index within a container.
And the last column contains the element value. If the object
id or element value are composite, then, instead of a single
column, they can occupy multiple columns. For an ordered
container table the ODB compiler also defines two indexes:
one for the object id column(s) and the other for the index
column. Refer to Section 12.6, "Index Definition
Pragmas" for more information on how to customize these
indexes.
Consider the following persistent object as an example:
#pragma db object
class person
{
...
private:
#pragma db id auto
unsigned long id_;
std::vector
...
};
The resulting database table (called person_nicknames) will
contain the object id column of type unsigned long
(called object_id), the index column of an integer type
(called index), and the value column of type
std::string (called value).
A number of ODB pragmas allow us to customize the table name, column
names, and native database types of an ordered container both, on
the per-container and per-member basis. For more information on
these pragmas, refer to Chapter 12, "ODB Pragma
Language". The following example shows some of the possible
customizations:
#pragma db object
class person
{
...
private:
#pragma db table("nicknames") \
id_column("person_id") \
index_type("SMALLINT UNSIGNED") \
index_column("nickname_number") \
value_type("VARCHAR(255)") \
value_column("nickname")
std::vector
...
};
While the C++ container used in a persistent class may be ordered,
sometimes we may wish to store such a container in the database without
the order information. In the example above, for instance, the order
of person's nicknames is probably not important. To instruct the ODB
compiler to ignore the order in ordered containers we can use the
db unordered pragma (Section 12.3.9,
"unordered", Section 12.4.18,
"unordered"). For example:
#pragma db object
class person
{
...
private:
#pragma db unordered
std::vector
...
};
The table for an ordered container that is marked unordered won't
have the index column and the order in which elements are retrieved
from the database may not be the same as the order in which they
were stored.
5.2 Set and Multiset Containers
In ODB set and multiset containers (referred to as just set
containers) are associative containers that contain elements
based on some relationship between them. A set container may
or may not guarantee a particular order of the elements that
it stores. Standard C++ containers that are considered set
containers for the purpose of persistence include
std::set and std::multiset as well
as C++11 std::unordered_set and
std::unordered_multiset.
The database table for a set container consists of at least
two columns. The first column contains the object id of a
persistent class instance of which the container is a member.
And the second column contains the element value. If the object
id or element value are composite, then, instead of a single
column, they can occupy multiple columns. ODB compiler also
defines an index on a set container table for the object id
column(s). Refer to Section 12.6, "Index Definition
Pragmas" for more information on how to customize this
index.
Consider the following persistent object as an example:
#pragma db object
class person
{
...
private:
#pragma db id auto
unsigned long id_;
std::set
...
};
The resulting database table (called person_emails) will
contain the object id column of type unsigned long
(called object_id) and the value column of type
std::string (called value).
A number of ODB pragmas allow us to customize the table name,
column names, and native database types of a set container, both on
the per-container and per-member basis. For more information on
these pragmas, refer to Chapter 12, "ODB Pragma
Language". The following example shows some of the possible
customizations:
#pragma db object
class person
{
...
private:
#pragma db table("emails") \
id_column("person_id") \
value_type("VARCHAR(255)") \
value_column("email")
std::set
...
};
5.3 Map and Multimap Containers
In ODB map and multimap containers (referred to as just map
containers) are associative containers that contain key-value
elements based on some relationship between keys. A map container
may or may not guarantee a particular order of the elements that
it stores. Standard C++ containers that are considered map
containers for the purpose of persistence include
std::map and std::multimap as well
as C++11 std::unordered_map and
std::unordered_multimap.
The database table for a map container consists of at least
three columns. The first column contains the object id of a
persistent class instance of which the container is a member.
The second column contains the element key. And the last column
contains the element value. If the object id, element key, or
element value are composite, then instead of a single column
they can occupy multiple columns. ODB compiler also
defines an index on a map container table for the object id
column(s). Refer to Section 12.6, "Index Definition
Pragmas" for more information on how to customize this
index.
Consider the following persistent object as an example:
#pragma db object
class person
{
...
private:
#pragma db id auto
unsigned long id_;
std::map
...
};
The resulting database table (called person_age_weight_map)
will contain the object id column of type unsigned long
(called object_id), the key column of type
unsigned short (called key), and the value
column of type float (called value).
A number of ODB pragmas allow us to customize the table name,
column names, and native database types of a map container, both on
the per-container and per-member basis. For more information on
these pragmas, refer to Chapter 12, "ODB Pragma
Language". The following example shows some of the possible
customizations:
#pragma db object
class person
{
...
private:
#pragma db table("weight_map") \
id_column("person_id") \
key_type("INT UNSIGNED") \
key_column("age") \
value_type("DOUBLE") \
value_column("weight")
std::map
...
};
5.4 Change-Tracking Containers
When a persistent object containing one of the standard containers
is updated in the database, ODB has no knowledge of which elements
were inserted, erased, or modified. As a result, ODB has no choice
but to assume the whole container has changed and update the state
of every single element. This can result in a significant overhead
if a container contains a large number of elements and we only
changed a small subset of them.
To eliminate this overhead, ODB provides a notion of change-tracking
containers. A change-tracking container, besides containing
its elements, just like an ordinary container, also includes the
change state for each element. When it is time to update such a
container in the database, ODB can use this change information to
perform a minimum number of database operations necessary to
synchronize the container state with the database.
The current version of the ODB runtime library provides a change-tracking
equivalent of std::vector (Section 5.4.1,
"Change-Tracking vector") with support for other
standard container equivalents planned for future releases. ODB
profile libraries also provide change-tracking equivalents for some
containers found in the corresponding frameworks and libraries
(Part III, "Profiles").
A change-tracking container equivalent can normally be used as a drop-in
replacement for an ordinary container except for a few minor
interface differences (discussed in the corresponding sub-sections).
In particular, we don't need to do anything extra to effect
change tracking. ODB will automatically start, stop, and reset
change tracking when necessary. The following example illustrates
this point using odb::vector as a replacement for
std::vector.
#pragma db object
class person
{
...
odb::vector
};
person p; // No change tracking (not persistent).
p.names.push_back ("John Doe");
{
transaction t (db.begin ());
db.persist (p); // Start change tracking (persistent).
t.commit ();
}
p.names.push_back ("Johnny Doo");
{
transaction t (db.begin ());
db.update (p); // One INSERT; reset change state.
t.commit ();
}
p.names.modify (0) = "Doe, John"; // Instead of operator[].
p.names.pop_back ();
{
transaction t (db.begin ());
db.update (p); // One UPDATE, one DELETE; reset change state.
t.commit ();
}
{
transaction t (db.begin ());
auto_ptr
p1->names.insert (p1->names.begin (), "Joe Do");
db.update (*p1); // One UPDATE, one INSERT; reset change state.
t.commit ();
}
{
transaction t (db.begin ());
db.erase (p); // One DELETE; stop change tracking (not persistent).
t.commit ();
}
One interesting aspect of change tracking is what happens when a
transaction that contains an update is later rolled back. In this
case, while the change-tracking container has reset the change
state (after update), actual changes were not committed to the
database. Change-tracking containers handle this case by
automatically registering a rollback callback and then, if it is
called, marking the container as "completely changed". In this
state, the container no longer tracks individual element changes
and, when updated, falls back to the complete state update, just
like an ordinary container. The following example illustrates
this point:
person p;
p.names.push_back ("John Doe");
{
transaction t (db.begin ());
db.persist (p); // Start change tracking (persistent).
t.commit ();
}
p.names.push_back ("Johnny Doo");
for (;;)
{
try
{
transaction t (db.begin ());
// First try: one INSERT.
// Next try: one DELETE, two INSERTs.
//
db.update (p); // Reset change state.
t.commit (); // If throws (rollback), mark as completely changed.
break;
}
catch (const odb::recoverable&)
{
continue;
}
}
5.4.1 Change-Tracking vector
Class template odb::vector, defined in
equivalent for std::vector. It
is implemented in terms of std::vector and is
implicit-convertible to and implicit-constructible from
const std::vector&. In particular, this
means that we can use odb::vector instance
anywhere const std::vector& is
expected. In addition, odb::vector constant
iterator (const_iterator) is the same type as
that of std::vector.
odb::vector incurs 2-bit per element overhead
in order to store the change state. It cannot
be stored unordered in the database (Section
12.4.18 "unordered") but can be used as an inverse
side of a relationship (6.2 "Bidirectional
Relationships"). In this case, no change tracking is performed
since no state for such a container is stored in the database.
The number of database operations required to update the state
of odb::vector corresponds well to the complexity
of std::vector functions. In particular, adding or
removing an element from the back of the vector (for example,
with push_back() and pop_back()),
requires only a single database statement execution. In contrast,
inserting or erasing an element somewhere in the middle of the
vector will require a database statement for every element that
follows it.
odb::vector replicates most of the std::vector
interface as defined in both C++98/03 and C++11 standards. However,
functions and operators that provide direct write access to
the elements had to be altered or disabled in order to support
change tracking. Additional functions used to interface with
std::vector and to control the change tracking state
were also added. The following listing summarizes the differences
between the odb::vector and std::vector
interfaces. Any std::vector function or operator
not mentioned in this listing has exactly the same signature
and semantics in odb::vector. Functions and
operators that were disabled are shown as commented out and
are followed by functions/operators that replace them.
namespace odb
{
template
class vector
{
...
// Element access.
//
//reference operator[] (size_type);
reference modify (size_type);
//reference at (size_type);
reference modify_at (size_type);
//reference front ();
reference modify_front ();
//reference back ();
reference modify_back ();
//T* data () noexcept;
T* modify_data () noexcept; // C++11 only.
// Iterators.
//
typedef typename std::vector
class iterator
{
...
// Element Access.
//
//reference operator* () const;
const_reference operator* () const;
reference modify () const;
//pointer operator-> () const;
const_pointer operator-> () const;
//reference operator[] (difference_type);
const_reference operator[] (difference_type);
reference modify (difference_type) const;
// Interfacing with std::vector::iterator.
//
typename std::vector
};
// Return std::vector iterators. The begin() functions mark
// all the elements as modified.
//
typename std::vector
typename std::vector
typename std::vector
typename std::vector
// Interfacing with std::vector.
//
vector (const std::vector
vector (std::vector
vector& operator= (const std::vector
vector& operator= (std::vector
operator const std::vector
std::vector
const std::vector
// Change tracking.
//
bool _tracking () const;
void _start () const;
void _stop () const;
void _arm (transaction&) const;
};
}
The following example highlights some of the differences between
the two interfaces. std::vector versions are commented
out.
#include
#include
void f (const std::vector
odb::vector
f (v); // Ok, implicit conversion.
if (v[1] == 2) // Ok, const access.
//v[1]++;
v.modify (1)++;
//v.back () = 4;
v.modify_back () = 4;
for (auto i (v.begin ()); i != v.end (); ++i)
{
if (*i != 0) // Ok, const access.
//*i += 10;
i.modify () += 10;
}
std::sort (v.mbegin (), v.mend ());
Note also the subtle difference between copy/move construction
and copy/move assignment of odb::vector instances.
While copy/move constructor will copy/move both the elements as
well as their change state, in contrast, assignment is tracked
as any other change to the vector content.
5.5 Using Custom Containers
While the ODB runtime and profile libraries provide support for
a wide range of containers, it is also easy to persist custom
container types or make a change-tracking version out of one.
To achieve this you will need to implement the
container_traits class template specialization for
your container. First, determine the container kind (ordered, set,
multiset, map, or multimap) for your container type. Then use a
specialization for one of the standard C++ containers found in
the common ODB runtime library (libodb) as a base
for your own implementation.
Once the container traits specialization is ready for your container,
you will need to include it into the ODB compilation process using
the --odb-epilogue option and into the generated header
files with the --hxx-prologue option. As an example,
suppose we have a hash table container for which we have the traits
specialization implemented in the hashtable-traits.hxx
file. Then, we can create an ODB compiler options file for this
container and save it to hashtable.options:
# Options file for the hash table container.
#
--odb-epilogue '#include "hashtable-traits.hxx"'
--hxx-prologue '#include "hashtable-traits.hxx"'
Now, whenever we compile a header file that uses the hashtable
container, we can specify the following command line option to
make sure it is recognized by the ODB compiler as a container
and the traits file is included in the generated code:
--options-file hashtable.options
6 Relationships
Relationships between persistent objects are expressed with pointers or
containers of pointers. The ODB runtime library provides built-in support
for shared_ptr/weak_ptr (TR1 or C++11),
std::unique_ptr (C++11),
std::auto_ptr, and raw pointers. Plus, ODB profile
libraries, that are available for commonly used frameworks and libraries
(such as Boost and Qt), provide support for smart pointers found in these
frameworks and libraries (Part III, "Profiles"). It is
also easy to add support for a custom smart pointer as discussed later
in Section 6.5, "Using Custom Smart Pointers". Any
supported smart pointer can be used in a data member as long as it can be
explicitly constructed from the canonical object pointer
(Section 3.3, "Object and View Pointers"). For
example, we can use weak_ptr if the object pointer
is shared_ptr.
When an object containing a pointer to another object is loaded,
the pointed-to object is loaded as well. In some situations this
eager loading of the relationships is undesirable since it
can lead to a large number of otherwise unused objects being
instantiated from the database. To support finer control
over relationships loading, the ODB runtime and profile
libraries provide the so-called lazy versions of
the supported pointers. An object pointed-to by a lazy pointer
is not loaded automatically when the containing object is loaded.
Instead, we have to explicitly request the instantiation of the
pointed-to object. Lazy pointers are discussed in
detail in Section 6.4, "Lazy Pointers".
As a simple example, consider the following employee-employer
relationship. Code examples presented in this chapter
will use the shared_ptr and weak_ptr
smart pointers from the TR1 (std::tr1) namespace.
#pragma db object
class employer
{
...
#pragma db id
std::string name_;
};
#pragma db object
class employee
{
...
#pragma db id
unsigned long id_;
std::string first_name_;
std::string last_name_;
shared_ptr
};
By default, an object pointer can be NULL. To
specify that a pointer always points to a valid object we can
use the not_null pragma (Section
12.4.6, "null/not_null") for
single object pointers and the value_not_null pragma
(Section
12.4.23, "value_null/value_not_null")
for containers of object pointers. For example:
#pragma db object
class employee
{
...
#pragma db not_null
shared_ptr
#pragma db value_not_null
std::vector
};
In this case, if we perform a database operation on the
employee object and the current_employer_
pointer or one of the pointers stored in the
previous_employers_ container is NULL,
then the odb::null_pointer exception will be thrown.
We don't need to do anything special to establish or navigate a
relationship between two persistent objects, as shown in the
following code fragment:
// Create an employer and a few employees.
//
unsigned long john_id, jane_id;
{
shared_ptr
shared_ptr
shared_ptr
john->employer_ = er;
jane->employer_ = er;
transaction t (db.begin ());
db.persist (er);
john_id = db.persist (john);
jane_id = db.persist (jane);
t.commit ();
}
// Load a few employee objects and print their employer.
//
{
session s;
transaction t (db.begin ());
shared_ptr
shared_ptr
cout << john->employer_->name_ << endl;
cout << jane->employer_->name_ << endl;
t.commit ();
}
The only notable line in the above code is the creation of a
session before the second transaction starts. As discussed in
Chapter 10, "Session", a session acts as a cache
of persistent objects.
By creating a session before loading the employee
objects we make sure that their employer_ pointers
point to the same employer object. Without a
session, each employee would have ended up pointing
to its own, private instance of the Example Inc employer.
As a general guideline, you should use a session when loading
objects that have pointers to other persistent objects. A
session makes sure that for a given object id, a single instance
is shared among all other objects that relate to it.
We can also use data members from pointed-to
objects in database queries (Chapter 4, "Querying the
Database"). For each pointer in a persistent class, the query
class defines a smart pointer-like member that contains members
corresponding to the data members in the pointed-to object. We
can then use the access via a pointer syntax (->)
to refer to data members in pointed-to objects.
For example, the query class for the employee object
contains the employer member (its name is derived from the
employer_ pointer) which in turn contains the
name member (its name is derived from the
employer::name_ data member of the pointed-to object).
As a result, we can use the query::employer->name
expression while querying the database for the employee
objects. For example, the following transaction finds all the
employees of Example Inc that have the Doe last name:
typedef odb::query
typedef odb::result
session s;
transaction t (db.begin ());
result r (db.query
query::employer->name == "Example Inc" && query::last == "Doe"));
for (result::iterator i (r.begin ()); i != r.end (); ++i)
cout << i->first_ << " " << i->last_ << endl;
t.commit ();
A query class member corresponding to a non-inverse
(Section 6.2, "Bidirectional Relationships") object
pointer can also be used as a normal member that has the id type
of the pointed-to object. For example, the following query locates
all the employee objects that don't have an associated
employer object:
result r (db.query
An important concept to keep in mind when working with object
relationships is the independence of persistent objects. In particular,
when an object containing a pointer to another object is made persistent
or is updated, the pointed-to object is not automatically persisted
or updated. Rather, only a reference to the object (in the form of the
object id) is stored for the pointed-to object in the database.
The pointed-to object itself is a separate entity and should
be made persistent or updated independently.
When persisting or updating an object containing a pointer to another
object, the pointed-to object must have a valid object id. This,
however, may not always be easy to achieve in complex relationships that
involve objects with automatically assigned identifiers. In such
cases it may be necessary to first persist an object with a pointer
set to NULL and then, once the pointed-to object is
made persistent and its identifier assigned, set the pointer
to the correct value and update the object in the database.
Persistent object relationships can be divided into two groups:
unidirectional and bidirectional. Each group in turn contains
several configurations that vary depending on the cardinality
of the sides of the relationship. All possible unidirectional
and bidirectional configurations are discussed in the following
sections.
6.1 Unidirectional Relationships
In unidirectional relationships we are only interested in navigating
from object to object in one direction. Because there is no interest
in navigating in the opposite direction, the cardinality of the other
end of the relationship is unimportant. As a result, there are only
two possible unidirectional relationships: to-one and to-many. Each
of these relationships is described in the following sections. For
sample code that shows how to work with these relationships, refer
to the relationship example in the odb-examples
package.
6.1.1 To-One Relationships
An example of a unidirectional to-one relationship is the
employee-employer relationship (an employee has one employer).
The following persistent C++ classes model this relationship:
#pragma db object
class employer
{
...
#pragma db id
std::string name_;
};
#pragma db object
class employee
{
...
#pragma db id
unsigned long id_;
#pragma db not_null
shared_ptr
};
The corresponding database tables look like this:
CREATE TABLE employer (
name VARCHAR (255) NOT NULL PRIMARY KEY);
CREATE TABLE employee (
id BIGINT UNSIGNED NOT NULL PRIMARY KEY,
employer VARCHAR (255) NOT NULL REFERENCES employer (name));
6.1.2 To-Many Relationships
An example of a unidirectional to-many relationship is the
employee-project relationship (an employee can be involved
in multiple projects). The following persistent C++ classes
model this relationship:
#pragma db object
class project
{
...
#pragma db id
std::string name_;
};
#pragma db object
class employee
{
...
#pragma db id
unsigned long id_;
#pragma db value_not_null unordered
std::vector
};
The corresponding database tables look like this:
CREATE TABLE project (
name VARCHAR (255) NOT NULL PRIMARY KEY);
CREATE TABLE employee (
id BIGINT UNSIGNED NOT NULL PRIMARY KEY);
CREATE TABLE employee_projects (
object_id BIGINT UNSIGNED NOT NULL,
value VARCHAR (255) NOT NULL REFERENCES project (name));
To obtain a more canonical database schema, the names of tables
and columns above can be customized using ODB pragmas
(Chapter 12, "ODB Pragma Language"). For example:
#pragma db object
class employee
{
...
#pragma db value_not_null unordered \
id_column("employee_id") value_column("project_name")
std::vector
};
The resulting employee_projects table would then
look like this:
CREATE TABLE employee_projects (
employee_id BIGINT UNSIGNED NOT NULL,
project_name VARCHAR (255) NOT NULL REFERENCES project (name));
6.2 Bidirectional Relationships
In bidirectional relationships we are interested in navigating
from object to object in both directions. As a result, each
object class in a relationship contains a pointer to the other
object. If smart pointers are used, then a weak pointer should
be used as one of the pointers to avoid ownership cycles. For
example:
class employee;
#pragma db object
class position
{
...
#pragma db id
unsigned long id_;
weak_ptr
};
#pragma db object
class employee
{
...
#pragma db id
unsigned long id_;
#pragma db not_null
shared_ptr
};
Note that when we establish a bidirectional relationship, we
have to set both pointers consistently. One way to make sure
that a relationship is always in a consistent state is to
provide a single function that updates both pointers at the
same time. For example:
#pragma db object
class position: public enable_shared_from_this
{
...
void
fill (shared_ptr
{
employee_ = e;
e->positions_ = shared_from_this ();
}
private:
weak_ptr
};
#pragma db object
class employee
{
...
private:
friend class position;
#pragma db not_null
shared_ptr
};
At the beginning of this chapter we examined how to use a session
to make sure a single object is shared among all other objects pointing
to it. With bidirectional relationships involving weak pointers the
use of a session becomes even more crucial. Consider the following
transaction that tries to load the position object
from the above example without using a session:
transaction t (db.begin ())
shared_ptr
...
t.commit ();
When we load the position object, the employee
object, which it points to, is also loaded. While employee
is initially stored as shared_ptr, it is then assigned to
the employee_ member which is weak_ptr. Once
the assignment is complete, the shared pointer goes out of scope
and the only pointer that points to the newly loaded
employee object is the employee_ weak
pointer. And that means the employee object is deleted
immediately after being loaded. To help avoid such pathological
situations ODB detects cases where a newly loaded object will
immediately be deleted and throws the odb::session_required
exception.
As the exception name suggests, the easiest way to resolve this
problem is to use a session:
session s;
transaction t (db.begin ())
shared_ptr