db_ut.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. * Copyright (C) 2007-2008 1&1 Internet AG
  6. *
  7. * This file is part of Kamailio, a free SIP server.
  8. *
  9. * Kamailio is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version
  13. *
  14. * Kamailio is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /**
  24. * \file lib/srdb1/db_ut.h
  25. * \brief Utility functions for database drivers.
  26. *
  27. * This utility methods are used from the database SQL driver to convert
  28. * values and print SQL queries from the internal API representation.
  29. * \ingroup db1
  30. */
  31. #ifndef DB1_UT_H
  32. #define DB1_UT_H
  33. #include "../../pvar.h"
  34. #include "db_key.h"
  35. #include "db.h"
  36. /**
  37. * Converts a char into an integer value.
  38. *
  39. * \param _s source value
  40. * \param _v target value
  41. * \return zero on sucess, negative on conversion errors
  42. */
  43. int db_str2int(const char* _s, int* _v);
  44. /**
  45. * Converts a char into an long long value.
  46. *
  47. * \param _s source value
  48. * \param _v target value
  49. * \return zero on sucess, negative on conversion errors
  50. */
  51. int db_str2longlong(const char* _s, long long* _v);
  52. /**
  53. * Converts a char into a double value.
  54. *
  55. * \param _s source value
  56. * \param _v target value
  57. * \return zero on sucess, negative on conversion errors
  58. */
  59. int db_str2double(const char* _s, double* _v);
  60. /**
  61. * Converts a integer value in a char pointer.
  62. *
  63. * \param _v source value
  64. * \param _s target value
  65. * \param _l available length and target length
  66. * \return zero on sucess, negative on conversion errors
  67. */
  68. int db_int2str(int _v, char* _s, int* _l);
  69. /**
  70. * Converts a long long value in a char pointer.
  71. *
  72. * \param _v source value
  73. * \param _s target value
  74. * \param _l available length and target length
  75. * \return zero on sucess, negative on conversion errors
  76. */
  77. int db_longlong2str(long long _v, char* _s, int* _l);
  78. /**
  79. * Converts a double value into a char pointer.
  80. *
  81. * \param _v source value
  82. * \param _s target value
  83. * \param _l available length and target length
  84. * \return zero on sucess, negative on conversion errors
  85. */
  86. int db_double2str(double _v, char* _s, int* _l);
  87. /**
  88. * Convert a time_t value to string.
  89. *
  90. * \param _v source value
  91. * \param _s target value
  92. * \param _l available length and target length
  93. * \return zero on sucess, negative on conversion errors
  94. * \todo This functions add quotes to the time value. This
  95. * should be done in the val2str function, as some databases
  96. * like db_berkeley don't need or like this at all.
  97. */
  98. int db_time2str(time_t _v, char* _s, int* _l);
  99. /**
  100. * Converts a char into a time_t value.
  101. *
  102. * \param _s source value
  103. * \param _v target value
  104. * \return zero on sucess, negative on conversion errors
  105. */
  106. int db_str2time(const char* _s, time_t* _v);
  107. /**
  108. * Print columns for a SQL statement, separated by commas.
  109. *
  110. * \param _b target char
  111. * \param _l length of the target
  112. * \param _c keys that should be printed
  113. * \param _n number of keys
  114. * \return the length of the printed result on success, negative on errors
  115. */
  116. int db_print_columns(char* _b, const int _l, const db_key_t* _c, const int _n);
  117. /**
  118. * Print values for a SQL statement.
  119. *
  120. * \param _c structure representing database connection
  121. * \param _b target char
  122. * \param _l length of the target
  123. * \param _v values that should be printed
  124. * \param _n number of values
  125. * \param (*val2str) function pointer to a db specific conversion function
  126. * \return the length of the printed result on success, negative on errors
  127. */
  128. int db_print_values(const db1_con_t* _c, char* _b, const int _l, const db_val_t* _v,
  129. const int _n, int (*val2str)(const db1_con_t*, const db_val_t*, char*, int*));
  130. /**
  131. * Print where clause for a SQL statement.
  132. *
  133. * \param _c structure representing database connection
  134. * \param _b target char
  135. * \param _l length of the target
  136. * \param _k keys that should be printed
  137. * \param _o optional operators
  138. * \param _v values that should be printed
  139. * \param _n number of key/value pairs
  140. * \param (*val2str) function pointer to a db specific conversion function
  141. * \return the length of the printed result on success, negative on errors
  142. */
  143. int db_print_where(const db1_con_t* _c, char* _b, const int _l, const db_key_t* _k,
  144. const db_op_t* _o, const db_val_t* _v, const int _n, int (*val2str)
  145. (const db1_con_t*, const db_val_t*, char*, int*));
  146. /**
  147. * Print set clause for a SQL statement.
  148. *
  149. * \param _c structure representing database connection
  150. * \param _b target char
  151. * \param _l length of the target
  152. * \param _k keys that should be printed
  153. * \param _v vals that should be printed
  154. * \param _n number of key/value pairs
  155. * \param (*val2str) function pointer to a db specific conversion function
  156. * \return the length of the printed result on success, negative on errors
  157. */
  158. int db_print_set(const db1_con_t* _c, char* _b, const int _l,
  159. const db_key_t* _k, const db_val_t* _v, const int _n, int (*val2str)
  160. (const db1_con_t*, const db_val_t*, char*, int*));
  161. /**
  162. * Convert db_val_t to pv_spec_t
  163. *
  164. * \param msg sip msg structure
  165. * \param dbval database value
  166. * \param pvs pv_spec where to put the database value
  167. * \return 0 on success, -1 on failure
  168. */
  169. int db_val2pv_spec(struct sip_msg* msg, db_val_t *dbval, pv_spec_t *pvs);
  170. #endif