db_val_t.xml 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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_val_t" 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>Type <type>db_val_t</type></title>
  14. <para>
  15. This structure represents a value in the database. Several data-types
  16. are recognized and converted by the database <acronym>API</acronym>:
  17. <itemizedlist>
  18. <listitem>
  19. <para>
  20. <emphasis>DB_INT</emphasis> - Value in the database
  21. represents an integer number.
  22. </para>
  23. </listitem>
  24. <listitem>
  25. <para>
  26. <emphasis>DB_DOUBLE</emphasis> - Value in the database
  27. represents a decimal number.
  28. </para>
  29. </listitem>
  30. <listitem>
  31. <para>
  32. <emphasis>DB_STRING</emphasis> - Value in the database
  33. represents a string.
  34. </para>
  35. </listitem>
  36. <listitem>
  37. <para>
  38. <emphasis>DB_STR</emphasis> - Value in the database
  39. represents a string.
  40. </para>
  41. </listitem>
  42. <listitem>
  43. <para>
  44. <emphasis>DB_DATETIME</emphasis> - Value in the database
  45. represents date and time.
  46. </para>
  47. </listitem>
  48. <listitem>
  49. <para>
  50. <emphasis>DB_BLOB</emphasis> - Value in the database
  51. represents binary large object.
  52. </para>
  53. </listitem>
  54. </itemizedlist>
  55. These data-types are automatically recognized, converted from internal
  56. database representation and stored in a variable of corresponding type.
  57. </para>
  58. <programlisting>
  59. typedef struct db_val {
  60. db_type_t type; /* Type of the value */
  61. int nul; /* NULL flag */
  62. union {
  63. int int_val; /* Integer value */
  64. double double_val; /* Double value */
  65. time_t time_val; /* Unix time_t value */
  66. const char* string_val; /* Zero terminated string */
  67. str str_val; /* str structure */
  68. str blob_val; /* Structure describing blob */
  69. } val;
  70. } db_val_t;
  71. </programlisting>
  72. <note>
  73. <para>
  74. All macros expect pinter to <type>db_val_t</type> variable as a
  75. parameter.
  76. </para>
  77. </note>
  78. <itemizedlist>
  79. <listitem>
  80. <para>
  81. <function>VAL_TYPE(value)</function> Macro.
  82. </para>
  83. <para>
  84. Use this macro if you need to set/get the type of the value
  85. </para>
  86. <example>
  87. <title>VAL_TYPE Macro</title>
  88. <programlisting>
  89. ...
  90. VAL_TYPE(val) = DB_INT;
  91. if (VAL_TYPE(val) == DB_FLOAT)
  92. ...
  93. </programlisting>
  94. </example>
  95. </listitem>
  96. <listitem>
  97. <para>
  98. <function>VAL_NULL(value)</function> Macro.
  99. </para>
  100. <para>
  101. Use this macro if you need to set/get the null flag. Non-zero
  102. flag means that the corresponding cell in the database
  103. contained no data (NULL value in <acronym>MySQL</acronym>
  104. terminology).
  105. </para>
  106. <example>
  107. <title>VAL_NULL Macro</title>
  108. <programlisting>
  109. ...
  110. if (VAL_NULL(val) == 1) {
  111. printf("The cell is NULL");
  112. }
  113. ...
  114. </programlisting>
  115. </example>
  116. </listitem>
  117. <listitem>
  118. <para>
  119. <function>VAL_INT(value)</function> Macro.
  120. </para>
  121. <para>
  122. Use this macro if you need to access <type>integer</type> value
  123. in <type>db_val_t</type> structure.
  124. </para>
  125. <example>
  126. <title>VAL_INT Macro</title>
  127. <programlisting>
  128. ...
  129. if (VAL_TYPE(val) == DB_INT) {
  130. printf("%d", VAL_INT(val));
  131. }
  132. ...
  133. </programlisting>
  134. </example>
  135. </listitem>
  136. <listitem>
  137. <para>
  138. <function>VAL_DOUBLE(value)</function> Macro.
  139. </para>
  140. <para>
  141. Use this macro if you need to access <type>double</type> value
  142. in the <type>db_val_t</type> structure.
  143. </para>
  144. <example>
  145. <title>VAL_DOUBLE Macro</title>
  146. <programlisting>
  147. ...
  148. if (VAL_TYPE(val) == DB_DOUBLE) {
  149. printf("%f", VAL_DOUBLE(val));
  150. }
  151. ...
  152. </programlisting>
  153. </example>
  154. </listitem>
  155. <listitem>
  156. <para>
  157. <function>VAL_TIME(value)</function> Macro.
  158. </para>
  159. <para>
  160. Use this macro if you need to access <type>time_t</type> value
  161. in <type>db_val_t</type> structure.
  162. </para>
  163. <example>
  164. <title>VAL_TIME Macro</title>
  165. <programlisting>
  166. ...
  167. time_t tim;
  168. if (VAL_TYPE(val) == DB_DATETIME) {
  169. tim = VAL_TIME(val);
  170. }
  171. ...
  172. </programlisting>
  173. </example>
  174. </listitem>
  175. <listitem>
  176. <para>
  177. <function>VAL_STRING(value)</function> Macro.
  178. </para>
  179. <para>
  180. Use this macro if you need to access <type>string</type> value
  181. in <type>db_val_t</type> structure.
  182. </para>
  183. <example>
  184. <title>VAL_STRING Macro</title>
  185. <programlisting>
  186. ...
  187. if (VAL_TYPE(val) == DB_STRING) {
  188. printf("%s", VAL_STRING(val));
  189. }
  190. ...
  191. </programlisting>
  192. </example>
  193. </listitem>
  194. <listitem>
  195. <para>
  196. <function>VAL_STR(value)</function> Macro.
  197. </para>
  198. <para>
  199. Use this macro if you need to access <type>str</type> structure
  200. in <type>db_val_t</type> structure.
  201. </para>
  202. <example>
  203. <title>VAL_STR Macro</title>
  204. <programlisting>
  205. ...
  206. if (VAL_TYPE(val) == DB_STR) {
  207. printf("%.*s", VAL_STR(val).len, VAL_STR(val).s);
  208. }
  209. ...
  210. </programlisting>
  211. </example>
  212. </listitem>
  213. <listitem>
  214. <para>
  215. <function>VAL_BLOB(value)</function> Macro.
  216. </para>
  217. <para>
  218. Use this macro if you need to access <type>blob</type> value in <type>db_val_t</type> structure.
  219. </para>
  220. <example>
  221. <title>VAL_STR Macro</title>
  222. <programlisting>
  223. ...
  224. if (VAL_TYPE(val) == DB_BLOB) {
  225. printf("%.*s", VAL_BLOB(val).len, VAL_BLOB(val).s);
  226. }
  227. ...
  228. </programlisting>
  229. </example>
  230. </listitem>
  231. </itemizedlist>
  232. </section>