Backends reference

This part of the documentation is provided for those who want to write (and contribute!) their own backends. It is anyway recommended that authors of new backend see the code of some existing backend for hints on how things are really done.

The backend interface is a set of base classes that the actual backends are supposed to specialize. The main SOCI interface uses only the interface and respecting the protocol (for example, the order of function calls) described here. Note that both the interface and the protocol were initially designed with the Oracle database in mind, which means that whereas it is quite natural with respect to the way Oracle API (OCI) works, it might impose some implementation burden on other backends, where things are done differently and therefore have to be adjusted, cached, converted, etc.

The interface to the common SOCI interface is defined in the core/soci-backend.h header file. This file is dissected below.

All names are defined in either SOCI or SOCI::details namespace.

// 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 };

// data types, as used to describe exchange format
enum eExchangeType { eXChar, eXCString, eXStdString, eXShort, eXInteger,
                     eXUnsignedLong, eXDouble, eXStdTm, eXStatement,
                     eXRowID, eXBLOB };

struct CStringDescriptor
{
    CStringDescriptor(char *str, std::size_t bufSize)
        : str_(str), bufSize_(bufSize) {}

    char *str_;
    std::size_t bufSize_;
};

class SOCIError : public std::runtime_error
{
public:
    SOCIError(std::string const & msg, int errNum = 0);

    int errNum_;
};

The eDataType enumeration type defines all types that form the core type support for SOCI. The enum itself can be used by clients when dealing with dynamic rowset description.

The eIndicator enumeration type defines all recognized states of data. The eTruncated state is provided for the case where the string is retrieved from the database into the char buffer that is not long enough to hold the whole value.

The eExchangeType enumeration type defines all possible types that can be used with the into and use elements.

The CStringDescriptor is a helper class that allows to store the address of char buffer together with its size. The objects of this class are passed to the backend when the eXCString type is involved.

The SOCIError class is an exception type used for database-related (and also usage-related) errors. The backends should throw exceptions of this or derived type only.

class StandardIntoTypeBackEnd
{
public:
    virtual ~StandardIntoTypeBackEnd() {}

    virtual void defineByPos(int &position,
        void *data, eExchangeType type) = 0;

    virtual void preFetch() = 0;
    virtual void postFetch(bool gotData, bool calledFromFetch,
        eIndicator *ind) = 0;

    virtual void cleanUp() = 0;
};

The StandardIntoTypeBackEnd class implements the dynamic interactions with the simple (non-bulk) into elements. The objects of this class (or, rather, of the derived class implemented by the actual backend) are created by the Statement object when the into element is bound - in terms of lifetime management, Statement is the master of this class.

The intended use of preFetch and postFetch functions is to manage any internal buffer and/or data conversion for each value retrieved from the database. If the given server supports binary data transmission and the data format for the given type agrees with what is used on the client machine, then these two functions need not do anything; otherwise buffer management and data conversions should go there.

class VectorIntoTypeBackEnd
{
public:
    virtual ~VectorIntoTypeBackEnd() {}

    virtual void defineByPos(int &position,
        void *data, eExchangeType type) = 0;

    virtual void preFetch() = 0;
    virtual void postFetch(bool gotData, eIndicator *ind) = 0;

    virtual void resize(std::size_t sz) = 0;
    virtual std::size_t size() = 0;

    virtual void cleanUp() = 0;
};

The VectorIntoTypeBackEnd has similar structure and purpose as the previous one, but is used for vectors (bulk data retrieval).

The data pointer points to the variable of type std::vector<T> (not to its internal buffer), resize is supposed to really resize the user-provided vector and size is supposed to return the current size of this vector. The important difference with regard to the previous class is that ind points (if not NULL) to the beginning of the array of indicators. The backend should fill this array according to the actual state of the retrieved data.

class StandardUseTypeBackEnd
{
public:
    virtual ~StandardUseTypeBackEnd() {}

    virtual void bindByPos(int &position,
        void *data, eExchangeType type) = 0;
    virtual void bindByName(std::string const &name,
        void *data, eExchangeType type) = 0;

    virtual void preUse(eIndicator const *ind) = 0;
    virtual void postUse(bool gotData, eIndicator *ind) = 0;

    virtual void cleanUp() = 0;
};

The StandardUseTypeBackEnd implements the interactions with the simple (non-bulk) use elements, created and destroyed by the Statement object.

The intended use fot preUse and postUse methods is to manage any internal buffers and/or data conversion. They can be called many times with the same statement.

class VectorUseTypeBackEnd
{
public:
    virtual ~VectorUseTypeBackEnd() {}

    virtual void bindByPos(int &position,
        void *data, eExchangeType type) = 0;
    virtual void bindByName(std::string const &name,
        void *data, eExchangeType type) = 0;

    virtual void preUse(eIndicator const *ind) = 0;

    virtual std::size_t size() = 0;

    virtual void cleanUp() = 0;
};

Objects of this type (or rather of type derived from this one) are used to implement interactions with user-provided vector (bulk) use elements and are managed by the Statement object. The data pointer points to the whole vector object provided by the user (not to its internal buffer); ind points to the beginning of the array of indicators (or is NULL). The meaning of this interface is analogous to those presented above.

class StatementBackEnd
{
public:
    virtual ~StatementBackEnd() {}

    virtual void alloc() = 0;
    virtual void cleanUp() = 0;
    virtual void prepare(std::string const &query) = 0;

    enum execFetchResult { eSuccess, eNoData };
    virtual execFetchResult execute(int number) = 0;
    virtual execFetchResult fetch(int number) = 0;

    virtual int getNumberOfRows() = 0;

    virtual std::string rewriteForProcedureCall(std::string const &query) = 0;

    virtual int prepareForDescribe() = 0;
    virtual void describeColumn(int colNum, eDataType &dtype,
        std::string &columnName, int &size, int &precision, int &scale,
        bool &nullOk) = 0;

    virtual StandardIntoTypeBackEnd * makeIntoTypeBackEnd() = 0;
    virtual StandardUseTypeBackEnd * makeUseTypeBackEnd() = 0;
    virtual VectorIntoTypeBackEnd * makeVectorIntoTypeBackEnd() = 0;
    virtual VectorUseTypeBackEnd * makeVectorUseTypeBackEnd() = 0;
};

The StatementBackEnd type implements the internals of the Statement objects (in fact, it is a basic Strategy design pattern). The objects of this class are created by the Session object.

Notes:

  1. Whether the query is executed using the simple one-time syntax or is prepared, the alloc, prepare and execute functions are always called, in this order.
  2. All into and use elements are bound (their defineByPos or bindByPos/bindByName functions are called) between statement preparation and execution.
class RowIDBackEnd
{
public:
    virtual ~RowIDBackEnd() {}
};

The RowIDBackEnd class is a hook for the backends to provide their own state for the row identifier. It has no functions, since the only portable interaction with the row identifier object is to use it with into and use elements.

class BLOBBackEnd
{
public:
    virtual ~BLOBBackEnd() {}

    virtual std::size_t getLen() = 0;
    virtual std::size_t read(std::size_t offset, char *buf,
        std::size_t toRead) = 0;
    virtual std::size_t write(std::size_t offset, char const *buf,
        std::size_t toWrite) = 0;
    virtual std::size_t append(char const *buf, std::size_t toWrite) = 0;
    virtual void trim(std::size_t newLen) = 0;
};

The BLOBBackEnd interface provides the entry points for the BLOB methods.

class SessionBackEnd
{
public:
    virtual ~SessionBackEnd() {}

    virtual void begin() = 0;
    virtual void commit() = 0;
    virtual void rollback() = 0;

    virtual StatementBackEnd * makeStatementBackEnd() = 0;
    virtual RowIDBackEnd * makeRowIDBackEnd() = 0;
    virtual BLOBBackEnd * makeBLOBBackEnd() = 0;
};

The object of the class derived from SessionBackEnd implements the internals of the Session object.

struct BackEndFactory
{
    virtual SessionBackEnd * makeSession(
        std::string const &connectString) const = 0;
};

The BackEndFactory is a base class for backend-provided factory class that is able to create valid sessions. The connectString parameter passed to makeSession is provided here by the Session constructor.

The actual backend factory object is supposed to be provided by the backend implementation and declared in its header file.

The following example is taken from soci-postgresql.h, which declares entities of the PostgreSQL backend:

// concrete backend factory for PostgreSQL
struct PostgreSQLBackEndFactory : BackEndFactory
{
    virtual PostgreSQLSessionBackEnd * makeSession(
        std::string const &connectString) const;
};

// globally visible factory object
extern PostgreSQLBackEndFactory const postgresql;

With the above declarations, it is enough to pass the postgresql factory name to the constructor of the Session object, which will use this factory to create concrete implementations for any other objects that are needed, with the help of appropriate makeXYZ functions.

Note that the backend source code is placed in the backends/name directory (for example, backends/oracle) and the test driver is in backends/name/test. There is also backends/empty directory provided as a skeleton for development of new backends and their tests. It is recommended that all backends respect naming conventions by just appending their name to the base-class names. The backend name used for the global factory object should clearly identify the given database engine, like oracle, postgresql, mysql, and so on.