| 1 |
/*! \page backenddev Developing Your Own Backends |
| 2 |
|
| 3 |
Even if your database is not supported directly by one of the backends, take a look on \ref odbc "ODBC" backend, |
| 4 |
and see how well your database driver behaves with it. It may solve the problem for you. |
| 5 |
|
| 6 |
Before you begin make sure you are familiar with CppDB library and know how to use it. Take a look to the |
| 7 |
source code of existing backends, it is good to take a look on \ref postgresql "PostgreSQL" and \ref sqlite3 "Sqlite3" backends |
| 8 |
implementations. |
| 9 |
|
| 10 |
\section backenddev_what_needed What Need to Implement |
| 11 |
|
| 12 |
You need to implement following 3 classes: |
| 13 |
|
| 14 |
-# cppdb::backend::connection - the object that holds connection to the database - similar to cppdb::session. |
| 15 |
-# cppdb::backend::statement - the object that holds prepared (or unprepared) statement - similar to cppdb::statement |
| 16 |
-# cppdb::backend::result - the object that holds the result of query - similar to cppdb::result |
| 17 |
|
| 18 |
You may safely assume that: |
| 19 |
|
| 20 |
- As long as \ref cppdb::backend::result "result" exists, the \ref cppdb::backend::statement "statement" that created it would exist. |
| 21 |
- As long as \ref cppdb::backend::statement "statement" exists, the \ref cppdb::backend::connection "connection" that created it would exist. |
| 22 |
|
| 23 |
The frontend classes make sure that this happens. |
| 24 |
|
| 25 |
|
| 26 |
\section backend_ref_dynamic Making Loadable Module |
| 27 |
|
| 28 |
You need to implement a single entry point in your library with the \ref cppdb::backend::cppdb_backend_connect_function "following prototype". |
| 29 |
It is very important to make the entry point function \c extern \c "C" so it would be resolved using dlsym or GetProcAddress functions |
| 30 |
and it should be marked as "__declspec(dllexport)" if it is compiled as Windows DLL. |
| 31 |
|
| 32 |
It should be named it as \c cppdb_xyz_get_connection where \c xyz is the name of your backend. |
| 33 |
|
| 34 |
It should receive as a parameter a const reference to cppdb::connection_info returning newly created \ref cppdb::backend::connection "connection". |
| 35 |
|
| 36 |
For example, in sqlite3 backend this function looks like this: |
| 37 |
|
| 38 |
\code |
| 39 |
extern "C" { |
| 40 |
CPPDB_DRIVER_API cppdb::backend::connection *cppdb_sqlite3_get_connection(cppdb::connection_info const &cs) |
| 41 |
{ |
| 42 |
return new cppdb::sqlite3_backend::connection(cs); |
| 43 |
} |
| 44 |
} |
| 45 |
\endcode |
| 46 |
|
| 47 |
The shared object or DLL themselves should be named as cppdb_xyz - so under Linux it would be libcppdb_xyz.so and under Windows MSVC it would |
| 48 |
be "cppdb_xyz.dll". It is also good idea to give it same soname as the libcppdb.so itself as it would allow keeping several backend |
| 49 |
versions for several versions of libcppdb in future as CppDB always tries to load a module as "libcppdb_xyz.so.V" and only |
| 50 |
then "libcppdb_xyz.so" where V is the current CppDB library soname. |
| 51 |
|
| 52 |
\section backend_ref_static Making Statically Linked only Module |
| 53 |
|
| 54 |
Of course if you want to link your module statically only, you do not have to create this single entry point |
| 55 |
function, you may derive from cppdb::backend::driver class such as its \ref cppdb::backend::driver::open() "open()" function |
| 56 |
would return a new connection object and \ref cppdb::backend::driver::in_use() "in_use()" function would always return |
| 57 |
true so the module would not be "unloaded". |
| 58 |
|
| 59 |
Such module can be installed using \ref cppdb::driver_manager driver_manager singleton class using its \ref cppdb::driver_manager::install_driver() "install_driver()" member function. For example: |
| 60 |
|
| 61 |
\code |
| 62 |
class my_cool_sql_driver { |
| 63 |
public: |
| 64 |
cppdb::backend::connection *open(cppdb::connection_info const &ci) { |
| 65 |
return new my_cool_sql::connection(ci); |
| 66 |
} |
| 67 |
bool in_use() { return true; } |
| 68 |
}; |
| 69 |
... |
| 70 |
cppdb::driver_manager::instance().install_driver("mycoolsql",new my_cool_sql_driver()); |
| 71 |
|
| 72 |
\endcode |
| 73 |
|
| 74 |
Of course if you have the \c extern \c "C" function as shown above you can do the same trick using cppdb::backend::static_driver. |
| 75 |
|
| 76 |
\code |
| 77 |
extern "C" { |
| 78 |
cppdb::backend::connection *my_cool_sql_open(cppdb::connection_info const &ci) { |
| 79 |
return new my_cool_sql::connection(ci); |
| 80 |
} |
| 81 |
} |
| 82 |
... |
| 83 |
cppdb::driver_manager::instance().install_driver("mycoolsql",new cppdb::backend::static_driver(my_cool_sql_open)); |
| 84 |
|
| 85 |
\endcode |
| 86 |
|
| 87 |
\section backend_tips Tips |
| 88 |
|
| 89 |
- Use functions from utils.h and numeric_util.h they are very helpful especially in converting data to and from textual representation. |
| 90 |
- When converting numbers to strings and backwards make sure you are using std::locale::classic(), otherwise when inserting |
| 91 |
12345.678 you may accidentally get "12.345,678" which is probably not what you want. Do it as following: |
| 92 |
\code |
| 93 |
std::ostringstream ss; |
| 94 |
ss.imbue(std::locale::classic()); |
| 95 |
ss << number; |
| 96 |
std::string text_value = ss.str(); |
| 97 |
\endcode |
| 98 |
And do the same for parsing. |
| 99 |
- Try to reduce memory coping as much as possible when handing large texts and objects, remember that when std::string is binded |
| 100 |
it remains valid till actual statement execution so it is likely that you can just keep a reference on it rather then |
| 101 |
copying the string itself. |
| 102 |
- Don't bother too much about statements caching or connection pooling. Front-end classes do this for you. |
| 103 |
|
| 104 |
*/ |
| 105 |
|