Client interface reference

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.

commonly used types

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.

class Session

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:

See Connections and simple queries for more examples.

function into

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.

function use

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.

class Statement

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:

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.

class Procedure

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.

class TypeConversion

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.

class Row

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:

See Dynamic resultset binding for examples.

class ColumnProperties

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:

See Dynamic resultset binding for examples.

class Values

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:

See Object-relational mapping for examples.

class BLOB

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:

See Large objects (BLOBs) for more discussion.

class RowID

The RowID class encapsulates the "row identifier" object.

class RowID
{
public:
    RowID(Session &s);
    ~RowID();

    details::RowIDBackEnd * getBackEnd();
};

This class contains the following members:

class BackEndFactory

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