| 1 |
/*! \page connstr Connecting to Database |
| 2 |
|
| 3 |
\section cs Connection Strings |
| 4 |
\subsection fmt Connection String Format |
| 5 |
|
| 6 |
CppDB connection string consists of the following parts: |
| 7 |
|
| 8 |
-# Driver Name separated from rest of the string with ":" |
| 9 |
-# Set of key = value pairs separated with ";" symbol. Where value can be put in single quotation marks ' and double quotation mark represents a single one (like in SQL syntax). |
| 10 |
|
| 11 |
For example |
| 12 |
\verbatim |
| 13 |
mysql:database=test;user=joe;password='d''eep secret' |
| 14 |
\endverbatim |
| 15 |
|
| 16 |
Represent a driver "mysql", user "joe" and password "d'eep secret". |
| 17 |
|
| 18 |
There are special keys that used as internal cppdb options rather then connection options. Such |
| 19 |
keys are always start with \@ symbol. For example "@use_prepared=off". |
| 20 |
|
| 21 |
\subsection speckeys Special Options |
| 22 |
|
| 23 |
These are CppDB special key values that are used as meta data for the connection: |
| 24 |
|
| 25 |
- \@stmt_cache_size - integer - the maximal number of cached statements. Default is 64. |
| 26 |
\n |
| 27 |
Each time new statement is created it is stored back in |
| 28 |
cache for future reuse and thus next time when the statement |
| 29 |
is created it would be fetched from cache rather then |
| 30 |
being prepared again. It gives significant performance |
| 31 |
boost for query and statements execution. The cache is handled using LRU queue. |
| 32 |
- \@use_prepared - "on" or "off" by default create prepared statements or ordinary statements. Default is "on". |
| 33 |
- \@pool_size - integer - the size of connection pool. Default is 0 - no connection pooling. |
| 34 |
\n |
| 35 |
This provides connection pool for efficient |
| 36 |
connection reuse that significantly improves the performance. |
| 37 |
- \@pool_max_idle - integer - the number if seconds to keep idle connection in pool. Default 600 - 10 minutes. |
| 38 |
\n |
| 39 |
This is useful for keeping maximal amount of time for holding an idle connection in pool. |
| 40 |
- \@modules_path - string - the path to search cppdb modules (drivers) in. |
| 41 |
\n |
| 42 |
Several paths can be given, under POSIX platform they should be separated |
| 43 |
using ':' symbol and under Windows (not Cygwin) it should be ';' symbol. For example: |
| 44 |
\n |
| 45 |
\verbatim |
| 46 |
mydriver:@modules_path=/opt/cppd/lib:/usr/local/lib;database=foo |
| 47 |
mydriver:@modules_path='c:/cppdb/lib;c:/mydrv';database=foo |
| 48 |
\endverbatim |
| 49 |
- \@module - string - the path to loadable cppdb module (shared object or dll). |
| 50 |
\n |
| 51 |
The explicit path to cppdb driver, for example "mydriver:@module=/opt/lib/libmy.so" |
| 52 |
|
| 53 |
\section Establishing Connection |
| 54 |
|
| 55 |
Connecting to the database can be done using either by creating a session object with connection string parameter cppdb::session::session(std::string const &) or |
| 56 |
by calling cppdb::session::open() function. |
| 57 |
|
| 58 |
When the connection established first time for specific driver, it loads its shared object or dll and makes it available for you. |
| 59 |
If you want to unload the drivers that are not used any more you should call cppdb::driver_manager::collect_unused(). And if all |
| 60 |
drivers that do not have opened connections will be closed. |
| 61 |
|
| 62 |
\note If you use connection pooling you would also want to call cppdb::connections_manager::gc() to remove all sessions that were idle |
| 63 |
for long period of time. |
| 64 |
|
| 65 |
Both classes cppdb::connections_manager and cppdb::driver_manager are singleton classes that used for connection management and pooling |
| 66 |
and in fact, cppdb::session::open() just calls cppdb::connections_manager::open() to get the underlying cppdb::backend::connection object. |
| 67 |
|
| 68 |
*/ |
| 69 |
|
| 70 |
|
| 71 |
//-------------------------- |
| 72 |
/*! \page advanced_drivers Linking Existing Drivers Statically |
| 73 |
|
| 74 |
\section advanced_drivers_before_you_begin Before You Begin |
| 75 |
|
| 76 |
In some cases you want to have drivers statically linked into your application. The simplest way is |
| 77 |
to \ref build "build" it with internal option for specific backend you need. This is the best |
| 78 |
and less error prone way. |
| 79 |
|
| 80 |
However you may also to use it directly without rebuilding cppdb library. |
| 81 |
|
| 82 |
\section advanced_drivers_linking Linking the Module |
| 83 |
|
| 84 |
CppDB comes with following libraries (the names below are for Linux, under other OS they may be slightly different): |
| 85 |
|
| 86 |
- libcppdb.so and libcppdb.a - shared and static version of cppdb without modules |
| 87 |
- libcppdb_XXX.so and libcppdb_XXX.a - shared and static version of modules, where XXX is the name of the module. |
| 88 |
|
| 89 |
By default, cppdb tries to load the module's shared object on the run time. However, if you want to link the |
| 90 |
module statically into the library you may do following. |
| 91 |
|
| 92 |
-# During linking use static library for the cppdb and for the module you need. |
| 93 |
-# Link with the appropriate native client library for the module. |
| 94 |
|
| 95 |
For example: |
| 96 |
|
| 97 |
\verbatim |
| 98 |
g++ my_program.o /usr/lib/libcppdb.a /usr/lib/libcppdb_sqlite3.a -lpthread -ldl -lsqlite3 |
| 99 |
\endverbatim |
| 100 |
|
| 101 |
Where libcppdb.a and libcppdb_sqlite3.a are the static version of the library and the module, "-lpthread -ldl" are |
| 102 |
dependencies of libcppdb and "-lsqlite3" is the dependency of sqlite3 module. |
| 103 |
|
| 104 |
\section advanced_drivers_installing Adding Backend to Driver |
| 105 |
|
| 106 |
Linking only with the backend is not enough, you need also install the driver to cppdb::driver_manager so it would |
| 107 |
know to use it directly rather then try to load it dynamically. |
| 108 |
|
| 109 |
Every backend as a single entry point called "cppdb_XXX_get_connection" where XXX is the backend name, so together |
| 110 |
with cppdb::backend::static_driver you can install it to the \ref cppdb::driver_manager driver_manger as shown in the example below: |
| 111 |
|
| 112 |
\code |
| 113 |
extern "C" { |
| 114 |
cppdb::backend::connection *cppdb_sqlite3_get_connection(cppdb::connection_info const &); |
| 115 |
} |
| 116 |
void add_driver() |
| 117 |
{ |
| 118 |
cppdb::driver_manager::instance().install_driver("sqlite3",new cppdb::backend::static_driver(cppdb_sqlite3_get_connection)); |
| 119 |
} |
| 120 |
\endcode |
| 121 |
|
| 122 |
*/ |
| 123 |
|