C++DB is the database layer that was designed to work with C++CMS. This customized version is used within Ye Ol' Pi Shack.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

124 lines
5.4 KiB

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