The client interface is a set of classes and free functions declared in
the soci.h
header file. All names are declared in the SOCI
namespace.
There are also additional names declared in the SOCI::details
namespace, but they are not supposed to be directly used by the users
of the library and are therefore not documented here. When such types
are used in the declarations that are part of the "public" interface,
they are replaced by "IT", which means "internal type". Types related
to the backend interface are named here, but documented on the next page.
The following types are commonly used in the rest of the interface:
// data types, as seen by the user enum eDataType { eString, eChar, eDate, eDouble, eInteger, eUnsignedLong }; // the enum type for indicator variables enum eIndicator { eOK, eNoData, eNull, eTruncated }; // the type used for reporting exceptions class SOCIError : public std::runtime_error { /* ... */ };
The eDataType
type defines the basic SOCI data types.
User provided data types need to be associated with one of these basic
types.
The eIndicator
type defines the possible states of data.
The SOCIError
type is used for error reporting.
The Session
class encapsulates the connection to the
database.
class Session { public: Session(BackEndFactory const &factory, std::string const & connectString); ~Session(); void begin(); void commit(); void rollback(); IT once; IT prepare; template <typename T> IT operator<<(T const &t); void setLogStream(std::ostream *s); std::ostream * getLogStream() const; std::string getLastQuery() const; details::SessionBackEnd * getBackEnd(); };
This class contains the following members:
Session sql(postgresql, "dbname=mydb");
begin
, commit
and rollback
functions for transaction control. Example:
sql.rollback();
once
member, which is used for performing "instant"
queries that do not need to be separately prepared. Example:
sql.once << "drop table persons";
prepare
member, which is used for statement
preparation - the result of the statement preparation must be provided
to the constructor of the Statement
class. Example:
int i; Statement st = (sql.prepare << "insert into numbers(value) values(:val)", use(i));
operator<<
that is a shortcut forwarder to the
equivalent operator of the once
member. Example:
sql << "drop table persons";
setLogStream
and getLogStream
functions for setting and getting the current stream object used for basic query logging. By default, it is NULL
, which means no logging. The string value that is actually logged into the stream is one-line verbatim copy of the query string provided by the user, without including any data from the use
elements. The query is logged exactly once, before the preparation step.getLastQuery
function for retrieving the text of the last used query.getBackEnd
function that returns the internal
pointer to the concrete backend implementation of the session. This is
provided for advanced users that need access to the functionality that
is not otherwise available.See Connections and simple queries for more examples.
The function into
is used for binding local output data
(in other words, it defines where the results of the query are stored).
template <typename T> IT into(T &t); template <typename T, typename T1> IT into(T &t, T1 p1); template <typename T> IT into(T &t, eIndicator &indicator); template <typename T, typename T1> IT into(T &t, eIndicator &ind, T1 p1); template <typename T> IT into(T &t, std::vector<eIndicator> &indicator);
Example:
int count; sql << "select count(*) from person", into(count);
See Binding local data for more examples.
The function use
is used for binding local input data (in
other words, it defines where the parameters of the query come from).
template <typename T> IT use(T &t); template <typename T, typename T1> IT use(T &t, T1 p1); template <typename T> IT use(T &t, eIndicator &indicator); template <typename T, typename T1> IT use(T &t, eIndicator &ind, T1 p1); template <typename T> IT use(T &t, std::vector<eIndicator> const &indicator); template <typename T, typename T1> IT use(T &t, std::vector<eIndicator> const &ind, T1 p1);
Example:
int val = 7; sql << "insert into numbers(val) values(:val)", use(val);
See Binding local data for more examples.
The Statement
class encapsulates the prepared statement.
class Statement { public: Statement(Session &s); Statement(IT const &prep); ~Statement(); void alloc(); void bind(Values& values); void exchange(IT const &i); // for into void exchange(IT const &u); // for use void cleanUp(); void prepare(std::string const &query); void defineAndBind(); bool execute(bool withDataExchange = false); bool fetch(); void describe(); void setRow(Row* r); details::StatementBackEnd * getBackEnd(); Session &session_; };
This class contains the following members:
Session
object. This can
be used for later query preparation. Example:
Statement stmt(sql);
prepare
on the Session
object, see example provided above for the
Session
class.alloc
function, which allocates necessary resources.
This is normally called automatically.bind
function, which is used to bind the Values
object - this is used in the object-relational mapping and normally
called automatically.into
or use
functions and are normally invoked automatically.cleanUp
function for cleaning up resources, normally
called automatically.prepare
function for preparing the statement for
repeated execution.defineAndBind
function for actually executing the
registered bindings, normally called automatically.
execute
function for executing the statement. If its
parameter is false
then there is no data exchange with
locally bound variables (this form should be used if later fetch
of multiple rows is foreseen).fetch
function for retrieving the next portion of
the result.describe
function for extracting the type
information for the result (but no data is exchanged). This is normally
called automatically and only when dynamic resultset binding is used.setRow
function for associating the Statement
and Row
objects, normally called automatically.getBackEnd
function that returns the internal
pointer to
the concrete backend implementation of the statement object. This is
provided
for advanced users that need access to the functionality that is not
otherwise available.session_
reference to the "parent" Session
object that was used to create this object.Most of the functions from the Statement
class
interface are called automatically, but can be also used explicitly.
Example:
Statement stmt(sql); stmt.alloc(); stmt.prepare("select count(*) from persons"); int count; stmt.exchange(into(count)); // repeat for all variables if there are more stmt.defineAndBind(); stmt.execute(true); // or execute() followed by fetch() stmt.cleanUp(); // optional, destructor will do this anyway
See Statement preparation and repeated execution for example uses.
The Procedure
class encapsulates the call to the stored
procedure. It provides the same public interface as the Statement
class, but automatically adds the necessary "decorations" to the SQL
call (when they are required) for higher portability of the client code.
class Procedure : public Statement { public: Procedure(Session &s); Procedure(IT const &prep); };
The second constructor above expects the result of using prepare
on the Session
object.
See Stored procedures for examples.
The TypeConversion
class is a traits class that is
supposed to be provided (specialized) by the user for defining
conversions to and from one of the basic SOCI types.
template <class T> struct TypeConversion { typedef SomeBasicType base_type; static T from(SomeBasicType &t); static SomeBasicType to(T &t); };
Users are supposed to properly implement the from
and to
functions in their specializations of this template class.
See Extending SOCI to support custom (user-defined) C++ types.
The Row
class encapsulates the data and type information
retrieved for the single row when the dynamic rowset binding is used.
class Row { public: Row(); ~Row(); void addProperties(ColumnProperties const &cp); std::size_t size() const; eIndicator indicator(std::size_t pos) const; eIndicator indicator(std::string const &name) const; template <typename T> void addHolder(T* t, eIndicator* ind); ColumnProperties const & getProperties (std::size_t pos) const; ColumnProperties const & getProperties (std::string const &name) const; template <typename T> T get(std::size_t pos) const; template <typename T> T get(std::size_t pos, T const &nullValue) const; template <typename T> T get(std::string const &name) const; template <typename T> T get(std::string const &name, T const &nullValue) const; template <typename T> Row const & operator>>(T &value) const; };
This class contains the following members:
Row
variable.addProperties
function for associating the ColumnProperties
object with the Row
, called automatically.size
function that returns the number of columns in
the row.indicator
function that returns the indicator value
for the given column (column is specified by position - starting from 0
- or by name).addHolder
function that adds new holder object that
registers the given object for later bind, called automatically.getProperties
function that returns the properties
of the column given by position (starting from 0) or by name.get
functions that return the value of the column
given by position or name. If the column contains null, then these
functions either return the provided "default" nullValue
or throw an exception.operator>>
for convenience stream-like
extraction interface. Subsequent calls to this function are equivalent
to calling get
with increasing position parameter,
starting from the beginning.See Dynamic resultset binding for examples.
The ColumnProperties
class provides the type and name
information about the particular column in a rowset.
enum eDataType { eString, eChar, eDate, eDouble, eInteger, eUnsignedLong }; class ColumnProperties { public: std::string getName() const; eDataType getDataType() const; };
This class contains the following members:
getName
function that returns the name of the column.getDataType
that returns the type of the column.See Dynamic resultset binding for examples.
The Values
class encapsulates the data and type
information and is used for object-relational mapping.
class Values { public: Values(); eIndicator indicator(std::size_t pos) const; eIndicator indicator(std::string const &name) const; template <typename T> T get(std::size_t pos) const; template <typename T> T get(std::size_t pos, T const &nullValue) const; template <typename T> T get(std::string const &name) const; template <typename T> T get(std::string const &name, T const &nullValue) const; template <typename T> Values const & operator>>(T &value) const; template <typename T> void set(std::string const &name, T &value, eIndicator indicator=eOK); };
This class contains the following members:
indicator
functions with the same meaning as in the Row
class.get
functions with the same meaning as in the Row
class.operator>>
for convenience stream-like
extraction interface. Subsequent calls to this function are equivalent
to calling get
with increasing position parameter,
starting from the beginning.set
function for storing values in named columns.See Object-relational mapping for examples.
The BLOB
class encapsulates the "large object"
functionality.
class BLOB { public: BLOB(Session &s); ~BLOB(); std::size_t getLen(); std::size_t read(std::size_t offset, char *buf, std::size_t toRead); std::size_t write(std::size_t offset, char const *buf, std::size_t toWrite); std::size_t append(char const *buf, std::size_t toWrite); void trim(std::size_t newLen); details::BLOBBackEnd * getBackEnd(); };
This class contains the following members:
getLen
function that returns the size of the BLOB
object.read
function that reads the BLOB data into provided
buffer.write
function that writes the BLOB data from
provided buffer.append
function that appends to the existing BLOB
data.trim
function that truncates the existing data to
the new length.getBackEnd
function that returns the internal
pointer to
the concrete backend implementation of the BLOB object. This is
provided
for advanced users that need access to the functionality that is not
otherwise available.See Large objects (BLOBs) for more discussion.
The RowID
class encapsulates the "row identifier" object.
class RowID { public: RowID(Session &s); ~RowID(); details::RowIDBackEnd * getBackEnd(); };
This class contains the following members:
RowID
object with the Session
object.getBackEnd
function that returns the internal
pointer to
the concrete backend implementation of the RowID
object.The BackEndFactory
class provides the abstract interface
for concrete backend factories.
struct BackEndFactory { virtual details::SessionBackEnd * makeSession( std::string const &connectString) const = 0; };
The only member of this class is the makeSession
function
that is supposed to create concrete backend implementation of the
session object.
Objects of this type are declared by each backend and should be
provided to the constructor of the Session
class.
In simple programs users do not need to use this class directly, but
the example use is:
BackEndFactory &factory = postgresql; std::string connectionParameters = "dbname=mydb"; Session sql(factory, parameters);
Previous (Beyond SOCI) | Next (Backends reference) |
Copyright © 2004-2006 Maciej Sobczak, Stephen Hutton