db_interface.xml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE section PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
  4. <section id="db_interface" xmlns:xi="http://www.w3.org/2001/XInclude">
  5. <sectioninfo>
  6. <revhistory>
  7. <revision>
  8. <revnumber>$Revision$</revnumber>
  9. <date>$Date$</date>
  10. </revision>
  11. </revhistory>
  12. </sectioninfo>
  13. <title>The Database Interface</title>
  14. <para>
  15. This is a generic database interface for modules that need to utilize a
  16. database. The interface should be used by all modules that access
  17. database. The interface will be independent of the underlying database
  18. server.
  19. </para>
  20. <note>
  21. <para>
  22. If possible, use predefined macros if you need to access any
  23. structure attributes.
  24. </para>
  25. <para>
  26. For additional description, see comments in sources of mysql
  27. module.
  28. </para>
  29. <para>
  30. If you want to see more complicated examples of how the API could
  31. be used, see sources of dbexample, usrloc or auth modules.
  32. </para>
  33. </note>
  34. <section id="data_types">
  35. <title>Data types</title>
  36. <para>
  37. There are several data types. All of them are defined in header
  38. files under <filename>db</filename> subdirectory, a client must
  39. include <filename>db.h</filename> header file to be able to use
  40. them.
  41. </para>
  42. <xi:include href="db_con_t.xml"/>
  43. <xi:include href="db_key_t.xml"/>
  44. <xi:include href="db_type_t.xml"/>
  45. <xi:include href="db_val_t.xml"/>
  46. <xi:include href="db_row_t.xml"/>
  47. <xi:include href="db_res_t.xml"/>
  48. </section>
  49. <section id="db_functions">
  50. <title>Functions</title>
  51. <para>
  52. There are several functions that implement the database
  53. <acronym>API</acronym> logic. All function names start with db_
  54. prefix, except <function>bind_dbmod</function>.
  55. <function>bind_dbmod</function> is implemented in
  56. <filename>db.c</filename> file, all other functions are implemented
  57. in a standalone module. Detailed description of functions follows.
  58. </para>
  59. <section id="bind_dbmod">
  60. <title><function>bind_dbmod</function></title>
  61. <para>
  62. This function is special, it's only purpose is to call
  63. <function>find_export</function> function in the
  64. <acronym>SER</acronym> core and find addresses of all other
  65. functions (starting with db_ prefix). This function
  66. <emphasis>MUST</emphasis> be called <emphasis>FIRST</emphasis>
  67. !
  68. </para>
  69. <funcsynopsis>
  70. <funcprototype>
  71. <funcdef>int <function>bind_dbmod</function></funcdef>
  72. <void/>
  73. </funcprototype>
  74. </funcsynopsis>
  75. <para>
  76. The function takes no parameters.
  77. </para>
  78. <para>
  79. The function returns 0 if it was able to find addresses of all
  80. other functions, otherwise value &lt; 0 is returned.
  81. </para>
  82. </section> <!-- bind_dbmod -->
  83. <section id="db_init">
  84. <title><function>db_init</function></title>
  85. <para>
  86. Use this function to initialize the database
  87. <acronym>API</acronym> and open a new database connection. This
  88. function must be called after <function>bind_dbmod</function>
  89. but before any other function is called.
  90. </para>
  91. <funcsynopsis>
  92. <funcprototype>
  93. <funcdef>db_con_t* <function>db_init</function></funcdef>
  94. <paramdef>const char* <parameter>_sql_url</parameter></paramdef>
  95. </funcprototype>
  96. </funcsynopsis>
  97. <para>
  98. The function takes one parameter, the parameter must contain
  99. database connection <acronym>URL</acronym>. The
  100. <acronym>URL</acronym> is of the form
  101. mysql://username:password@host:port/database where:
  102. <itemizedlist>
  103. <listitem>
  104. <para>
  105. <emphasis>username</emphasis> - Username to use
  106. when logging into database (optional).
  107. </para>
  108. </listitem>
  109. <listitem>
  110. <para>
  111. <emphasis>password</emphasis> - Password if it was
  112. set (optional).
  113. </para>
  114. </listitem>
  115. <listitem>
  116. <para>
  117. <emphasis>host</emphasis> - Hostname or IP address
  118. of the host where database server lives
  119. (mandatory).
  120. </para>
  121. </listitem>
  122. <listitem>
  123. <para>
  124. <emphasis>port</emphasis> - Port number of the
  125. server if the port differs from default value
  126. (optional).
  127. </para>
  128. </listitem>
  129. <listitem>
  130. <para>
  131. <emphasis>database</emphasis> - If the database server supports
  132. multiple databases, you must specify name of the database (optional).
  133. </para>
  134. </listitem>
  135. </itemizedlist>
  136. </para>
  137. <para>
  138. The function returns pointer to <type>db_con_t</type>*
  139. representing the connection if it was successful, otherwise 0
  140. is returned.
  141. </para>
  142. </section> <!-- db-init -->
  143. <section id="db_close">
  144. <title><function>db_close</function></title>
  145. <para>
  146. The function closes previously open connection and frees all
  147. previously allocated memory. The function
  148. <function>db_close</function> must be the very last function
  149. called.
  150. </para>
  151. <funcsynopsis>
  152. <funcprototype>
  153. <funcdef>void <function>db_close</function></funcdef>
  154. <paramdef>db_con_t* <parameter>_h</parameter></paramdef>
  155. </funcprototype>
  156. </funcsynopsis>
  157. <para>
  158. The function takes one parameter, this parameter is a pointer
  159. to <type>db_con_t</type> structure representing database
  160. connection that should be closed.
  161. </para>
  162. <para>
  163. Function doesn't return anything.
  164. </para>
  165. </section> <!-- db-close -->
  166. <section id="db_query">
  167. <title><function>db_query</function></title>
  168. <para>
  169. This function implements SELECT <acronym>SQL</acronym> directive.
  170. </para>
  171. <funcsynopsis>
  172. <funcprototype>
  173. <funcdef>int <function>db_query</function></funcdef>
  174. <paramdef>db_con_t* <parameter>_h</parameter></paramdef>
  175. <paramdef>db_key_t* <parameter>_k</parameter></paramdef>
  176. <paramdef>db_val_t* <parameter>_v</parameter></paramdef>
  177. <paramdef>db_key_t* <parameter>_c</parameter></paramdef>
  178. <paramdef>int <parameter>_n</parameter></paramdef>
  179. <paramdef>int <parameter>_nc</parameter></paramdef>
  180. <paramdef>db_key_t* <parameter>_o</parameter></paramdef>
  181. <paramdef>db_res_t** <parameter>_r</parameter></paramdef>
  182. </funcprototype>
  183. </funcsynopsis>
  184. <para>
  185. The function takes 8 parameters:
  186. <itemizedlist>
  187. <listitem>
  188. <para>
  189. <emphasis>_h</emphasis> - Database connection handle.
  190. </para>
  191. </listitem>
  192. <listitem>
  193. <para>
  194. <emphasis>_k</emphasis> - Array of column names
  195. that will be compared and their values must
  196. match.
  197. </para>
  198. </listitem>
  199. <listitem>
  200. <para>
  201. <emphasis>_v</emphasis> - Array of values,
  202. columns specified in _k parameter must match
  203. these values.
  204. </para>
  205. </listitem>
  206. <listitem>
  207. <para>
  208. <emphasis>_c</emphasis> - Array of column names
  209. that you are interested in.
  210. </para>
  211. </listitem>
  212. <listitem>
  213. <para>
  214. <emphasis>_n</emphasis> - Number of key-value
  215. pairs to match in _k and _v parameters.
  216. </para>
  217. </listitem>
  218. <listitem>
  219. <para>
  220. <emphasis>_nc</emphasis> - Number of columns in
  221. _c parameter.
  222. </para>
  223. </listitem>
  224. <listitem>
  225. <para>
  226. <emphasis>_o</emphasis> - Order by.
  227. </para>
  228. </listitem>
  229. <listitem>
  230. <para>
  231. <emphasis>_r</emphasis> - Address of variable
  232. where pointer to the result will be stored.
  233. </para>
  234. </listitem>
  235. </itemizedlist>
  236. If _k and _v parameters are NULL and _n is zero, you will get
  237. the whole table. If _c is NULL and _nc is zero, you will get
  238. all table columns in the result
  239. </para>
  240. <para>
  241. _r will point to a dynamically allocated structure, it is
  242. necessary to call db_free_result function once you are finished
  243. with the result.
  244. </para>
  245. <para>
  246. Strings in the result are not duplicated, they will be
  247. discarded if you call db_free_result, make a copy yourself if
  248. you need to keep it after db_free_result.
  249. </para>
  250. <para>
  251. You must call db_free_result <emphasis>BEFORE</emphasis> you
  252. can call db_query again !
  253. </para>
  254. <para>
  255. The function returns 0 if everything is OK, otherwise value
  256. &lt; 0 is returned.
  257. </para>
  258. </section> <!-- db_query -->
  259. <section id="db_free_result">
  260. <title><function>db_free_result</function></title>
  261. <para>
  262. This function frees all memory allocated previously in
  263. <function>db_query</function>, it is necessary to call this
  264. function for a <type>db_res_t</type> structure if you don't
  265. need the structure anymore. You must call this function
  266. <emphasis>BEFORE</emphasis> you call
  267. <function>db_query</function> again !
  268. </para>
  269. <funcsynopsis>
  270. <funcprototype>
  271. <funcdef>int <function>db_free_result</function></funcdef>
  272. <paramdef>db_con_t* <parameter>_h</parameter></paramdef>
  273. <paramdef>db_res_t* <parameter>_r</parameter></paramdef>
  274. </funcprototype>
  275. </funcsynopsis>
  276. <para>
  277. The function takes 2 parameters:
  278. <itemizedlist>
  279. <listitem>
  280. <para>
  281. <parameter>_h</parameter> - Database connection handle.
  282. </para>
  283. </listitem>
  284. <listitem>
  285. <para>
  286. <parameter>_r</parameter> - Pointer to db_res_t
  287. structure to destroy.
  288. </para>
  289. </listitem>
  290. </itemizedlist>
  291. </para>
  292. <para>
  293. The function returns 0 if everything is OK, otherwise the
  294. function returns value &lt; 0.
  295. </para>
  296. </section> <!-- db_free_result -->
  297. <section id="db_insert">
  298. <title><function>db_insert</function></title>
  299. <para>
  300. This function implements INSERT SQL directive, you can
  301. insert one or more rows in a table using this function.
  302. </para>
  303. <funcsynopsis>
  304. <funcprototype>
  305. <funcdef>int <function>db_insert</function></funcdef>
  306. <paramdef>db_con_t* <parameter>_h</parameter></paramdef>
  307. <paramdef>db_key_t* <parameter>_k</parameter></paramdef>
  308. <paramdef>db_val_t* <parameter>_v</parameter></paramdef>
  309. <paramdef>int <parameter>_n</parameter></paramdef>
  310. </funcprototype>
  311. </funcsynopsis>
  312. <para>
  313. The function takes 4 parameters:
  314. <itemizedlist>
  315. <listitem>
  316. <para>
  317. <parameter>_h</parameter> - Database connection handle.
  318. </para>
  319. </listitem>
  320. <listitem>
  321. <para>
  322. <parameter>_k</parameter> - Array of keys (column names).
  323. </para>
  324. </listitem>
  325. <listitem>
  326. <para>
  327. <parameter>_v</parameter> - Array of values for
  328. keys specified in _k parameter.
  329. </para>
  330. </listitem>
  331. <listitem>
  332. <para>
  333. <parameter>_n</parameter> - Number of
  334. keys-value pairs int _k and _v parameters.
  335. </para>
  336. </listitem>
  337. </itemizedlist>
  338. </para>
  339. <para>
  340. The function returns 0 if everything is OK, otherwise the
  341. function returns value &lt; 0.
  342. </para>
  343. </section> <!-- db_insert -->
  344. <section id="db_delete">
  345. <title><function>db_delete</function></title>
  346. <para>
  347. This function implements DELETE SQL directive, it is
  348. possible to delete one or more rows from a table.
  349. </para>
  350. <funcsynopsis>
  351. <funcprototype>
  352. <funcdef>int <function>db_delete</function></funcdef>
  353. <paramdef>db_con_t* <parameter>_h</parameter></paramdef>
  354. <paramdef>db_key_t* <parameter>_k</parameter></paramdef>
  355. <paramdef>db_val_t* <parameter>_v</parameter></paramdef>
  356. <paramdef>int <parameter>_n</parameter></paramdef>
  357. </funcprototype>
  358. </funcsynopsis>
  359. <para>
  360. The function takes 4 parameters:
  361. <itemizedlist>
  362. <listitem>
  363. <para>
  364. <parameter>_h</parameter> - Database connection handle.
  365. </para>
  366. </listitem>
  367. <listitem>
  368. <para>
  369. <parameter>_k</parameter> - Array of keys
  370. (column names) that will be matched.
  371. </para>
  372. </listitem>
  373. <listitem>
  374. <para>
  375. <parameter>_v</parameter> - Array of values
  376. that the row must match to be deleted.
  377. </para>
  378. </listitem>
  379. <listitem>
  380. <para>
  381. <parameter>_n</parameter> - Number of
  382. keys-value parameters in _k and _v parameters.
  383. </para>
  384. </listitem>
  385. </itemizedlist>
  386. If _k is NULL and _v is NULL and _n is zero, all rows are
  387. deleted (table will be empty).
  388. </para>
  389. <para>
  390. The function returns 0 if everything is OK, otherwise the
  391. function returns value &lt; 0.
  392. </para>
  393. </section> <!-- db_delete -->
  394. <section id="db_update">
  395. <title><function>db_update</function></title>
  396. <para>
  397. The function implements UPDATE SQL directive. It is possible to
  398. modify one or more rows in a table using this function.
  399. </para>
  400. <funcsynopsis>
  401. <funcprototype>
  402. <funcdef>int <function>db_update</function></funcdef>
  403. <paramdef>db_con_t* <parameter>_h</parameter></paramdef>
  404. <paramdef>db_key_t* <parameter>_k</parameter></paramdef>
  405. <paramdef>db_val_t* <parameter>_v</parameter></paramdef>
  406. <paramdef>db_key_t* <parameter>_uk</parameter></paramdef>
  407. <paramdef>db_val_t* <parameter>_uv</parameter></paramdef>
  408. <paramdef>int <parameter>_n</parameter></paramdef>
  409. <paramdef>int <parameter>_un</parameter></paramdef>
  410. </funcprototype>
  411. </funcsynopsis>
  412. <para>
  413. The function takes 7 parameters:
  414. <itemizedlist>
  415. <listitem>
  416. <para>
  417. <parameter>_h</parameter> - Database connection handle.
  418. </para>
  419. </listitem>
  420. <listitem>
  421. <para>
  422. <parameter>_k</parameter> - Array of keys (column names) that will be matched.
  423. </para>
  424. </listitem>
  425. <listitem>
  426. <para>
  427. <parameter>_v</parameter> - Array of values that the row must match
  428. to be modified.
  429. </para>
  430. </listitem>
  431. <listitem>
  432. <para>
  433. <parameter>_uk</parameter> - Array of keys (column names) that will be modified.
  434. </para>
  435. </listitem>
  436. <listitem>
  437. <para>
  438. <parameter>_uv</parameter> - New values for keys specified in _k parameter.
  439. </para>
  440. </listitem>
  441. <listitem>
  442. <para>
  443. <parameter>_n</parameter> - Number of key-value pairs in _k and _v parameters.
  444. </para>
  445. </listitem>
  446. <listitem>
  447. <para>
  448. <parameter>_un</parameter> - Number of key-value pairs
  449. in _uk and _uv parameters.
  450. </para>
  451. </listitem>
  452. </itemizedlist>
  453. </para>
  454. <para>
  455. The function returns 0 if everything is OK, otherwise the function returns
  456. value &lt; 0.
  457. </para>
  458. </section> <!-- db_update -->
  459. <section id="db_use_table">
  460. <title><function>db_use_table</function></title>
  461. <para>
  462. The function db_use_table takes a table name and stores it
  463. in db_con_t structure. All subsequent operations (insert,
  464. delete, update, query) are performed on that table.
  465. </para>
  466. <funcsynopsis>
  467. <funcprototype>
  468. <funcdef>int <function>db_use-table</function></funcdef>
  469. <paramdef>db_con_t* <parameter>_h</parameter></paramdef>
  470. <paramdef>cons char* <parameter>_t</parameter></paramdef>
  471. </funcprototype>
  472. </funcsynopsis>
  473. <para>
  474. The function takes 2 parameters:
  475. <itemizedlist>
  476. <listitem>
  477. <para>
  478. <parameter>_h</parameter> - Database connection handle.
  479. </para>
  480. </listitem>
  481. <listitem>
  482. <para>
  483. <parameter>_t</parameter> - Table name.
  484. </para>
  485. </listitem>
  486. </itemizedlist>
  487. </para>
  488. <para>
  489. The function returns 0 if everything is OK, otherwise the
  490. function returns value &lt; 0.
  491. </para>
  492. </section>
  493. </section>
  494. </section>