The class Session
encapsulates the database connection
and other backend-related details, which are common to all the
statements
that will be later executed. Its constructor expects two parameters:
the requested backend factory object and the generic connection string,
which meaning is backend-dependent.
Example:
Session sql(oracle, "service=orcl user=scott password=tiger");
Another example might be:
Session sql(postgresql, "dbname=mydb");
Above, the sql
object is a local (automatic) object
that encapsulates the connection.
The Session
constructor either connects successfully, or
throws the exception.
It is possible to have many active Session
s at the same
time, even using different backends.
Portability note:
The following backend factories are currently available:
In many cases, the SQL query is intended to be executed only once,
which means that statement parsing and execution can go together.
The Session
class provides a special once
member, which triggers parsing and execution of such one-time
statements:
sql.once << "drop table persons";
For shorter syntax, the following form is also allowed:
sql << "drop table persons";
The IOStream-like interface is exactly what it looks like, so that
the
statement text can be composed of many parts, involving anything that
is streamable (including custom classes, if they have
appropriate operator<<
):
string tableName = "persons"; sql << "drop table " << tableName; int id = 123; sql << "delete from companies where id = " << id;
Previous (Errors) | Next (Exchanging data) |
Copyright © 2004-2006 Maciej Sobczak, Stephen Hutton