Warning, /sdk/codevis/thirdparty/soci/docs/api/backend.md is written in an unsupported language. File is not indexed.
0001 # Backends reference 0002 0003 This part of the documentation is provided for those who want towrite (and contribute!) their 0004 own backends. It is anyway recommendedthat authors of new backend see the code of some existing 0005 backend forhints on how things are really done. 0006 0007 The backend interface is a set of base classes that the actual backendsare supposed to specialize. 0008 The main SOCI interface uses only theinterface and respecting the protocol (for example, 0009 the order of function calls) described here. 0010 Note that both the interface and theprotocol were initially designed with the Oracle database in 0011 mind, which means that whereas it is quite natural with respect to the way Oracle API (OCI) works, 0012 it might impose some implementation burden on otherbackends, where things are done differently 0013 and therefore have to beadjusted, cached, converted, etc. 0014 0015 The interface to the common SOCI interface is defined in the `core/soci-backend.h` header file. 0016 This file is dissected below. 0017 0018 All names are defined in either `soci` or `soci::details` namespace. 0019 0020 ```cpp 0021 // data types, as seen by the user 0022 enum data_type 0023 { 0024 dt_string, dt_date, dt_double, dt_integer, dt_long_long, dt_unsigned_long_long 0025 }; 0026 0027 // the enum type for indicator variables 0028 enum indicator { i_ok, i_null, i_truncated }; 0029 0030 // data types, as used to describe exchange format 0031 enum exchange_type 0032 { 0033 x_char, 0034 x_stdstring, 0035 x_short, 0036 x_integer, 0037 x_long_long, 0038 x_unsigned_long_long, 0039 x_double, 0040 x_stdtm, 0041 x_statement, 0042 x_rowid, 0043 x_blob 0044 }; 0045 0046 struct cstring_descriptor 0047 { 0048 cstring_descriptor(char * str, std::size_t bufSize) 0049 : str_(str), bufSize_(bufSize) {} 0050 0051 char * str_; 0052 std::size_t bufSize_; 0053 }; 0054 0055 // actually in error.h: 0056 class soci_error : public std::runtime_error 0057 { 0058 public: 0059 soci_error(std::string const & msg); 0060 }; 0061 ``` 0062 0063 The `data_type` enumeration type defines all types that form the core type support for SOCI. 0064 The enum itself can be used by clients when dealing with dynamic rowset description. 0065 0066 The `indicator` enumeration type defines all recognized *states* of data. 0067 The `i_truncated` state is provided for the case where the string is retrieved from the database 0068 into the char buffer that is not long enough to hold the whole value. 0069 0070 The `exchange_type` enumeration type defines all possible types that can be used 0071 with the `into` and `use` elements. 0072 0073 The `cstring_descriptor` is a helper class that allows to store the address of `char` buffer 0074 together with its size. 0075 The objects of this class are passed to the backend when the `x_cstring` type is involved. 0076 0077 The `soci_error` class is an exception type used for database-related (and also usage-related) errors. 0078 The backends should throw exceptions of this or derived type only. 0079 0080 ```cpp 0081 class standard_into_type_backend 0082 { 0083 public: 0084 standard_into_type_backend() {} 0085 virtual ~standard_into_type_backend() {} 0086 0087 virtual void define_by_pos(int& position, void* data, exchange_type type) = 0; 0088 0089 virtual void pre_fetch() = 0; 0090 virtual void post_fetch(bool gotData, bool calledFromFetch, indicator* ind) = 0; 0091 0092 virtual void clean_up() = 0; 0093 }; 0094 ``` 0095 0096 The `standard_into_type_back_end` class implements the dynamic interactions with the simple 0097 (non-bulk) `into` elements. 0098 The objects of this class (or, rather, of the derived class implemented by the actual backend) 0099 are created by the `statement` object when the `into` element is bound - in terms of lifetime 0100 management, `statement` is the master of this class. 0101 0102 * `define_by_pos` - Called when the `into` element is bound, once and before the statement is executed. The `data` pointer points to the variable used for `into` element (or to the `cstring_descriptor` object, which is artificially created when the plain `char` buffer is used for data exchange). The `position` parameter is a "column number", assigned by the library. The backend should increase this parameter, according to the number of fields actually taken (usually 1). 0103 * `pre_fetch` - Called before each row is fetched. 0104 * `post_fetch` - Called after each row is fetched. The `gotData` parameter is `true` if the fetch operation really retrievedsome data and `false` otherwise; `calledFromFetch` is `true` when the call is from the fetch operation and `false` if it is from the execute operation (this is also the case for simple, one-time queries). In particular, `(calledFromFetch && !gotData)` indicates that there is an end-of-rowset condition. `ind` points to the indicator provided by the user, or is `NULL`, if there is no indicator. 0105 * `clean_up` - Called once when the statement is destroyed. 0106 0107 The intended use of `pre_fetch` and `post_fetch` functions is to manage any internal buffer and/or data conversion foreach value retrieved from the database. 0108 If the given server supportsbinary data transmission and the data format for the given type agreeswith what is used on the client machine, then these two functions neednot do anything; otherwise buffer management and data conversionsshould go there. 0109 0110 ```cpp 0111 class vector_into_type_backend 0112 { 0113 public: 0114 vector_into_type_backend() {} 0115 virtual ~vector_into_type_backend() {} 0116 0117 virtual void define_by_pos(int& position, void* data, exchange_type type) = 0; 0118 0119 virtual void pre_fetch() = 0; 0120 virtual void post_fetch(bool gotData, indicator* ind) = 0; 0121 0122 virtual void resize(std::size_t sz) = 0; 0123 virtual std::size_t size() = 0; 0124 0125 virtual void clean_up() = 0; 0126 }; 0127 ``` 0128 0129 The `vector_into_type_back_end` has similar structure and purpose as the previous one, but is used for vectors (bulk data retrieval). 0130 0131 The `data` pointer points to the variable of type `std::vector<T>;` (and *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. 0132 The important difference with regard to the previous class is that `ind` points (if not `NULL`) to the beginning of the *array* of indicators. 0133 The backend should fill this array according to the actual state of the retrieved data. 0134 0135 * `bind_by_pos` - Called for each `use` element, once and before the statement is executed - for those `use` elements that do not provide explicit names for parameter binding. The meaning of parameters is same as in previous classes. 0136 * `bind_by_name` - Called for those `use` elements that provide the explicit name. 0137 * `pre_use` - Called before the data is transmitted to the server (this means before the statement is executed, which can happen many times for the prepared statement). `ind` points to the indicator provided by the user (or is `NULL`). 0138 * `post_use` - Called after statement execution. `gotData` and `ind` have the same meaning as in `standard_into_type_back_end::post_fetch`, and this can be used by those backends whose respective servers support two-way data exchange (like in/out parameters in stored procedures). 0139 0140 The intended use for `pre_use` and `post_use` methods is to manage any internal buffers and/or data conversion. 0141 They can be called many times with the same statement. 0142 0143 ```cpp 0144 class vector_use_type_backend 0145 { 0146 public: 0147 virtual ~vector_use_type_backend() {} 0148 0149 virtual void bind_by_pos(int& position, 0150 void* data, exchange_type type) = 0; 0151 virtual void bind_by_name(std::string const& name, 0152 void* data, exchange_type type) = 0; 0153 0154 virtual void pre_use(indicator const* ind) = 0; 0155 0156 virtual std::size_t size() = 0; 0157 0158 virtual void clean_up() = 0; 0159 }; 0160 ``` 0161 0162 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. 0163 The `data` pointer points to the whole vector object provided by the user (and *not* to its internal buffer); `ind` points to the beginning of the array of indicators (or is `NULL`). 0164 The meaning of this interface is analogous to those presented above. 0165 0166 ```cpp 0167 class statement_backend 0168 { 0169 public: 0170 statement_backend() {} 0171 virtual ~statement_backend() {} 0172 0173 virtual void alloc() = 0; 0174 virtual void clean_up() = 0; 0175 0176 virtual void prepare(std::string const& query, statement_type eType) = 0; 0177 0178 enum exec_fetch_result 0179 { 0180 ef_success, 0181 ef_no_data 0182 }; 0183 0184 virtual exec_fetch_result execute(int number) = 0; 0185 virtual exec_fetch_result fetch(int number) = 0; 0186 0187 virtual long long get_affected_rows() = 0; 0188 virtual int get_number_of_rows() = 0; 0189 0190 virtual std::string rewrite_for_procedure_call(std::string const& query) = 0; 0191 0192 virtual int prepare_for_describe() = 0; 0193 virtual void describe_column(int colNum, data_type& dtype, 0194 std::string& column_name) = 0; 0195 0196 virtual standard_into_type_backend* make_into_type_backend() = 0; 0197 virtual standard_use_type_backend* make_use_type_backend() = 0; 0198 virtual vector_into_type_backend* make_vector_into_type_backend() = 0; 0199 virtual vector_use_type_backend* make_vector_use_type_backend() = 0; 0200 }; 0201 ``` 0202 0203 The `statement_backend` type implements the internals of the `statement` objects. 0204 The objects of this class are created by the `session` object. 0205 0206 * `alloc` - Called once to allocate everything that is needed for the statement to work correctly. 0207 * `clean_up` - Supposed to clean up the resources, called once. 0208 * `prepare` - Called once with the text of the SQL query. For servers that support explicit query preparation, this is the place to do it. 0209 * `execute` - Called to execute the query; if number is zero, the intent is not to exchange data with the user-provided objects (`into` and `use` elements); positive values mean the number of rows to exchange (more than 1 is used only for bulk operations). 0210 * `fetch` - Called to fetch next bunch of rows; number is positive and determines the requested number of rows (more than 1 is used only for bulk operations). 0211 * `get_affected_rows` - Called to determine the actual number of rows affected by data modifying statement. 0212 * `get_number_of_rows` - Called to determine the actual number of rows retrieved by the previous call to `execute` or `fetch`. 0213 * `rewrite_for_procedure_call` - Used when the `procedure` is used instead of `statement`, to call the stored procedure. This function should rewrite the SQL query (if necessary) to the form that will allow to execute the given procedure. 0214 * `prepare_for_describe` - Called once when the `into` element is used with the `row` type, which means that dynamic rowset description should be performed. It is supposed to do whatever is needed to later describe the column properties and should return the number of columns. 0215 * `describe_column` - Called once for each column (column numbers - `colNum` - start from 1), should fill its parameters according to the column properties. 0216 * `make_into_type_backend`, `make_use_type_backend`, `make_vector_into_type_backend`, `make_vector_use_type_backend` - Called once for each `into` or `use` element, to create the objects of appropriate classes (described above). 0217 0218 **Notes:** 0219 0220 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. 0221 2. All `into` and `use` elements are bound (their `define_by_pos` or `bind_by_pos`/`bind_by_name` functions are called) *between* statement preparation and execution. 0222 0223 ```cpp 0224 class rowid_backend 0225 { 0226 public: 0227 virtual ~rowid_backend() {} 0228 }; 0229 ``` 0230 0231 The `rowid_backend` 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. 0232 0233 ```cpp 0234 class blob_backend 0235 { 0236 public: 0237 virtual ~blob_backend() {} 0238 0239 virtual std::size_t get_len() = 0; 0240 virtual std::size_t read(std::size_t offset, char * buf, 0241 std::size_t toRead) = 0; 0242 virtual std::size_t write(std::size_t offset, char const * buf, 0243 std::size_t toWrite) = 0; 0244 virtual std::size_t append(char const * buf, std::size_t toWrite) = 0; 0245 virtual void trim(std::size_t newLen) = 0; 0246 }; 0247 ``` 0248 0249 The `blob_backend` interface provides the entry points for the `blob` methods. 0250 0251 ```cpp 0252 class session_backend 0253 { 0254 public: 0255 virtual ~session_backend() {} 0256 0257 virtual void begin() = 0; 0258 virtual void commit() = 0; 0259 virtual void rollback() = 0; 0260 0261 virtual bool get_next_sequence_value(session&, std::string const&, long long&); 0262 virtual bool get_last_insert_id(session&, std::string const&, long long&); 0263 0264 virtual std::string get_backend_name() const = 0; 0265 0266 virtual statement_backend * make_statement_backend() = 0; 0267 virtual rowid_backend * make_rowid_backend() = 0; 0268 virtual blob_backend * make_blob_backend() = 0; 0269 }; 0270 ``` 0271 0272 The object of the class derived from `session_backend` implements the internals of the `session` object. 0273 0274 * `begin`, `commit`, `rollback` - Forward-called when the same functions of `session` are called by user. 0275 * `get_next_sequence_value`, `get_last_insert_id` - Called to retrieve sequences or auto-generated values and every backend should define at least one of them to allow the code using auto-generated values to work. 0276 * `make_statement_backend`, `make_rowid_backend`, `make_blob_backend` - Called to create respective implementations for the `statement`, `rowid` and `blob` classes. 0277 0278 ```cpp 0279 struct backend_factory 0280 { 0281 virtual ~backend_factory() {} 0282 0283 virtual details::session_backend * make_session( 0284 std::string const& connectString) const = 0; 0285 }; 0286 ``` 0287 0288 The `backend_factory` is a base class for backend-provided factory class that is able to create valid sessions. The `connectString` parameter passed to `make_session` is provided here by the `session` constructor and contains only the backend-related parameters, without the backend name (if the dynamic backend loading is used). 0289 0290 The actual backend factory object is supposed to be provided by the backend implementation and declared in its header file. In addition to this, the `factory_ABC` function with the "C" calling convention and returning the pointer to concrete factory object should be provided, where `ABC` is the backend name. 0291 0292 The following example is taken from `soci-postgresql.h`, which declares entities of the PostgreSQL backend: 0293 0294 ```cpp 0295 struct postgresql_backend_factory : backend_factory 0296 { 0297 virtual postgresql_session_backend* make_session( 0298 std::string const& connectString) const; 0299 }; 0300 extern postgresql_backend_factory const postgresql; 0301 0302 extern "C" 0303 { 0304 0305 // for dynamic backend loading 0306 backend_factory const * factory_postgresql(); 0307 0308 } // extern "C" 0309 ``` 0310 0311 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 `make_XYZ` functions. Alternatively, the `factory_postgresql` function will be called automatically by the backend loader if the backend name is provided at run-time instead. 0312 0313 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.