⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datadevelopermanual.page

📁 很好用的网络封装库,不熟悉网络编程的人也可以使用。使用风格良好的标准c++编写。
💻 PAGE
📖 第 1 页 / 共 2 页
字号:
POCO Data Connectors Developer Guide
Data

!!!Overview
Developing one's own <*Data Connector*> implementation is rather straight-forward.
Just implement the following interfaces:
  * Poco::Data::AbstractBinder
  * Poco::Data::AbstractExtractor
  * Poco::Data::StatementImpl
  * Poco::Data::SessionImpl
  * Poco::Data::Connector
  * optional: Poco::Data::AbstractPreparation

It is recommended to implement the classes from top to down (ie. start with Binder and Extractor) and to use a 
namespace that has <[ Poco::Data ]> as parent, e.g.<[ Poco::Data::SQLite ]>.

!!!AbstractBinder
An <[AbstractBinder]> is a class that maps values to placeholders. It is also responsible to bind primitive C++ data types to database 
data types. The constructor of the subclass should receive everything needed to bind variables to 
placeholders by position. An example taken from the SQLite implementation would be:

    Binder::Binder(sqlite3_stmt* pStmt):
        _pStmt(pStmt)
    {
    }
    
    void Binder::bind(std::size_t pos, const Poco::Int32& val)
    {
        int rc = sqlite3_bind_int(_pStmt, (int)pos, val);
        checkReturn(rc);
    }
    
    void Binder::bind(std::size_t pos, const Poco::Int16& val)
    {
        Poco::Int32 tmp = val;
        bind(pos, tmp);
    }
----
SQLite only needs an <*sqlite3_stmt*> as internal state, Int32 is bound via <*sqlite3_bind_int*> and Int16 values are mapped to Int32 values.

!!Complete Interface
All methods are public.

    AbstractBinder();
        /// Creates the AbstractBinder.
    
    virtual ~AbstractBinder();
        /// Destroys the AbstractBinder.
    
    virtual void bind(std::size_t pos, const Poco::Int8 &val) = 0;
        /// Binds an Int8.
    
    virtual void bind(std::size_t pos, const Poco::UInt8 &val) = 0;
        /// Binds an UInt8.
    
    virtual void bind(std::size_t pos, const Poco::Int16 &val) = 0;
        /// Binds an Int16.
    
    virtual void bind(std::size_t pos, const Poco::UInt16 &val) = 0;
        /// Binds an UInt16.
    
    virtual void bind(std::size_t pos, const Poco::Int32 &val) = 0;
        /// Binds an Int32.
    
    virtual void bind(std::size_t pos, const Poco::UInt32 &val) = 0;
        /// Binds an UInt32.
    
    virtual void bind(std::size_t pos, const Poco::Int64 &val) = 0;
        /// Binds an Int64.
    
    virtual void bind(std::size_t pos, const Poco::UInt64 &val) = 0;
        /// Binds an UInt64.
    
    virtual void bind(std::size_t pos, const bool &val) = 0;
        /// Binds a boolean.
    
    virtual void bind(std::size_t pos, const float &val) = 0;
        /// Binds a float.
    
    virtual void bind(std::size_t pos, const double &val) = 0;
        /// Binds a double.
    
    virtual void bind(std::size_t pos, const char &val) = 0;
        /// Binds a single character.
    
    virtual void bind(std::size_t pos, const char* const &pVal) = 0;
        /// Binds a const char ptr.
    
    virtual void bind(std::size_t pos, const std::string& val) = 0;
        /// Binds a string.
    
    virtual void bind(std::size_t pos, const BLOB& val) = 0;
        /// Binds a BLOB.
    
    virtual void reset() = 0;
        /// Resets the internal state, called before a rebind
----

!!!AbstractExtractor
An <[AbstractExtractor]> takes a result row and extracts from a given position one single value. It performs the reverse operation to the <[AbstractBinder]>,
ie. it maps database types to primitive C++ types. An <[AbstractExtractor]> also has to handle null values. If it detects a null value, it is not allowed to modify
the incoming value but will simply return false. An example taken from the SQLite implementation:

    Extractor::Extractor(sqlite3_stmt* pStmt):
        _pStmt(pStmt)
    {
    }
    
    bool Extractor::extract(std::size_t pos, Poco::Int32& val)
    {
        if (isNull(pos<[
            return false;
        val = sqlite3_column_int(_pStmt, (int)pos);
        return true;
    }
    
    bool Extractor::extract(std::size_t pos, Poco::Int16& val)
    {
        if (isNull(pos<[
            return false;
        val = sqlite3_column_int(_pStmt, (int)pos);
        return true;
    }
----

!!Complete Interface
All methods are public.

    AbstractExtractor();
        /// Creates the AbstractExtractor.
    
    virtual ~AbstractExtractor();
        /// Destroys the AbstractExtractor.
    
    virtual bool extract(std::size_t pos, Poco::Int8& val) = 0;
        /// Extracts an Int8. Returns false if null was received.
    
    virtual bool extract(std::size_t pos, Poco::UInt8& val) = 0;
        /// Extracts an UInt8. Returns false if null was received.
    
    virtual bool extract(std::size_t pos, Poco::Int16& val) = 0;
        /// Extracts an Int16. Returns false if null was received.
    
    virtual bool extract(std::size_t pos, Poco::UInt16& val) = 0;
        /// Extracts an UInt16. Returns false if null was received.
    
    virtual bool extract(std::size_t pos, Poco::Int32& val) = 0;
        /// Extracts an Int32. Returns false if null was received.
    
    virtual bool extract(std::size_t pos, Poco::UInt32& val) = 0;
        /// Extracts an UInt32. Returns false if null was received.
    
    virtual bool extract(std::size_t pos, Poco::Int64& val) = 0;
        /// Extracts an Int64. Returns false if null was received.
    
    virtual bool extract(std::size_t pos, Poco::UInt64& val) = 0;
        /// Extracts an UInt64. Returns false if null was received.
    
    virtual bool extract(std::size_t pos, bool& val) = 0;
        /// Extracts a boolean. Returns false if null was received.
    
    virtual bool extract(std::size_t pos, float& val) = 0;
        /// Extracts a float. Returns false if null was received.
    
    virtual bool extract(std::size_t pos, double& val) = 0;
        /// Extracts a double. Returns false if null was received.
    
    virtual bool extract(std::size_t pos, char& val) = 0;
        /// Extracts a single character. Returns false if null was received.
    
    virtual bool extract(std::size_t pos, std::string& val) = 0;
        /// Extracts a string. Returns false if null was received.
    
    virtual bool extract(std::size_t pos, BLOB& val) = 0;
        /// Extracts a BLOB. Returns false if null was received.
----

!!!AbstractPreparation
<[AbstractPreparation]> is an optional interface responsible for preparing an extract. If you need it depends on the <[DataConnector]> you implement. For example, SQLite can do perfectly without it, ODBC instead requires it.
SQLite doesn't need it because it works as follows:
  * sendQuery
  * getNextResult
  * extract single row values from result set

This works because SQLites <*getNextResult*> provides the data as string, i.e. it doesn't need any type information.

The ODBC implementation is different:
  * register/prepare for each column an output location
  * getNextResult
  * extract for each row the value by copying the content of the previously registered output location

<[AbstractPreparation]> is responsible for the first step. A typical prepare implementation will look like that:

    void prepare(std::size_t pos, Poco::Int32 val)
    {
       _myVec[pos] =  Poco::Any a(val);
       int* i = AnyCast<int>(&_myVec[pos]);
       //register int* i for output, Db specific
    } 
----
Extract now changes to:

    bool Extractor::extract(std::size_t pos, Poco::Int16& val)
    {
        if (isNull(pos))
            return false;
        val = AnyCast<int>(_myVec[pos]);
        return true;
    }
----

!!Complete Interface

    AbstractPreparation();
        /// Creates the AbstractPreparation.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -